Added basic logic for client: -t -f, checks for -D and -d, -w, -W, -D, -d

This commit is contained in:
elvis
2022-04-17 19:00:20 +02:00
parent 6eb723ce6b
commit 26b2b4d7f9
2 changed files with 276 additions and 15 deletions

View File

@ -3,7 +3,7 @@ CFLAGS += -std=c99 -Wall -Werror -pedantic -g
INCDIR = ./lib/utils ./lib/ini ./lib/threadpool ./src ./lib/log ./lib/api INCDIR = ./lib/utils ./lib/ini ./lib/threadpool ./src ./lib/log ./lib/api
INCLUDES := $(patsubst %,-I %,$(INCDIR)) INCLUDES := $(patsubst %,-I %,$(INCDIR))
LDFLAGS = -L . LDFLAGS = -L .
OPTFLAGS = #-O3 OPTFLAGS = -O3
LIBS = -lpthread LIBS = -lpthread
SOCKET = ./cs_sock SOCKET = ./cs_sock
@ -34,53 +34,53 @@ all: $(BUILD_DIR)/server $(BUILD_DIR)/client
# -SERVER-RULES----------------------------------------------------------------- # -SERVER-RULES-----------------------------------------------------------------
$(OBJ_DIR)/fileQueue.o: fileQueue.c fileQueue.h conn.h fileQueue.h util.h $(OBJ_DIR)/fileQueue.o: fileQueue.c fileQueue.h conn.h fileQueue.h util.h
@$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ @$(CC) $(CFLAGS) $(OPTFLAGS) $(INCLUDES) -c $< -o $@
@echo $@ @echo $@
$(OBJ_DIR)/apiFile.o: apiFile.c apiFile.h fileQueue.h taglialegna.h conn.h $(OBJ_DIR)/apiFile.o: apiFile.c apiFile.h fileQueue.h taglialegna.h conn.h
@$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ @$(CC) $(CFLAGS) $(OPTFLAGS) $(INCLUDES) -c $< -o $@
@echo $@ @echo $@
$(OBJ_DIR)/taglialegna.o: taglialegna.c taglialegna.h fileQueue.h $(OBJ_DIR)/taglialegna.o: taglialegna.c taglialegna.h fileQueue.h
@$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ @$(CC) $(CFLAGS) $(OPTFLAGS) $(INCLUDES) -c $< -o $@
@echo $@ @echo $@
$(OBJ_DIR)/threadpool.o: threadpool.c util.h threadpool.h $(OBJ_DIR)/threadpool.o: threadpool.c util.h threadpool.h
@$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ @$(CC) $(CFLAGS) $(OPTFLAGS) $(INCLUDES) -c $< -o $@
@echo $@ @echo $@
$(OBJ_DIR)/ini.o: ini.c ini.h $(OBJ_DIR)/ini.o: ini.c ini.h
@$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ @$(CC) $(CFLAGS) $(OPTFLAGS) $(INCLUDES) -c $< -o $@
@echo $@ @echo $@
$(OBJ_DIR)/strsep_gnu.o: strsep_gnu.c strsep_gnu.h $(OBJ_DIR)/strsep_gnu.o: strsep_gnu.c strsep_gnu.h
@$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ @$(CC) $(CFLAGS) $(OPTFLAGS) $(INCLUDES) -c $< -o $@
@echo $@ @echo $@
$(OBJ_DIR)/serverWorker.o: serverWorker.c serverWorker.h apiFile.h fileQueue.h taglialegna.h threadpool.h conn.h message.h strsep_gnu.h $(OBJ_DIR)/serverWorker.o: serverWorker.c serverWorker.h apiFile.h fileQueue.h taglialegna.h threadpool.h conn.h message.h strsep_gnu.h
@$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ @$(CC) $(CFLAGS) $(OPTFLAGS) $(INCLUDES) -c $< -o $@
@echo $@ @echo $@
$(OBJ_DIR)/server.o: server.c threadpool.h conn.h util.h serverWorker.h ini.h serverUtil.h fileQueue.h taglialegna.h $(OBJ_DIR)/server.o: server.c threadpool.h conn.h util.h serverWorker.h ini.h serverUtil.h fileQueue.h taglialegna.h
@$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ @$(CC) $(CFLAGS) $(OPTFLAGS) $(INCLUDES) -c $< -o $@
@echo $@ @echo $@
$(BUILD_DIR)/server: $(OBJ_SERVER) $(BUILD_DIR)/server: $(OBJ_SERVER)
@$(CC) $(CFLAGS) $(LIBS) $(OBJ_SERVER) -o $(BUILD_DIR)/server @$(CC) $(CFLAGS) $(OPTFLAGS) $(OPTFLAGS) $(LIBS) $(OBJ_SERVER) -o $(BUILD_DIR)/server
@echo $@ @echo $@
# -CLIENT-RULES----------------------------------------------------------------- # -CLIENT-RULES-----------------------------------------------------------------
$(OBJ_DIR)/api.o: api.c api.h $(OBJ_DIR)/api.o: api.c api.h
@$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ @$(CC) $(CFLAGS) $(OPTFLAGS) $(INCLUDES) -c $< -o $@
@echo $@ @echo $@
$(OBJ_DIR)/client.o: client.c api.h $(OBJ_DIR)/client.o: client.c api.h
@$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@ @$(CC) $(CFLAGS) $(OPTFLAGS) $(INCLUDES) -c $< -o $@
@echo $@ @echo $@
$(BUILD_DIR)/client: $(OBJ_CLIENT) $(BUILD_DIR)/client: $(OBJ_CLIENT)
@$(CC) $(CFLAGS) $(LIBS) $(OBJ_CLIENT) -o $(BUILD_DIR)/client @$(CC) $(CFLAGS) $(OPTFLAGS) $(LIBS) $(OBJ_CLIENT) -o $(BUILD_DIR)/client
@echo $@ @echo $@

View File

@ -36,6 +36,8 @@ typedef struct cmdw_s {
void destroyCommandList(cmd_t *l); void destroyCommandList(cmd_t *l);
// aggiunge un comando alla lista // aggiunge un comando alla lista
int addCommand(cmd_t **l, char cmd, char *arg); int addCommand(cmd_t **l, char cmd, char *arg);
// esegue tutti i comandi nella lista
int execute(cmd_t *l, int print);
// per chiudere la connessione prima dell'uscita // per chiudere la connessione prima dell'uscita
void cleanup() { void cleanup() {
@ -50,6 +52,13 @@ static void usage(const char *argv0) {
fprintf(stdout, "use: %s <config file location>\n", argv0); fprintf(stdout, "use: %s <config file location>\n", argv0);
} }
// -f
int cmd_f(char *socket);
// -w
int cmd_w(char *dirname, char *Dir, int print);
// -W
int cmd_W(char *filelist, char *Dir, int print);
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// MAIN // MAIN
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
@ -76,9 +85,10 @@ int main(int argc, char* argv[]) {
// struttra per -w // struttra per -w
cmdw_t *cmdw = NULL; cmdw_t *cmdw = NULL;
int opt; int opt = 0;
char args[MAXARGLENGTH]; char args[MAXARGLENGTH];
int print = 0;
char f = 0; char f = 0;
// add args to list // add args to list
@ -95,12 +105,13 @@ int main(int argc, char* argv[]) {
} }
memset(args, 0, MAXARGLENGTH); memset(args, 0, MAXARGLENGTH);
strncpy(args, optarg, strnlen(optarg, MAXARGLENGTH-1)+1); strncpy(args, optarg, strnlen(optarg, MAXARGLENGTH-1)+1);
addCommand(&cmds, opt, optarg); addCommand(&cmds, opt, args);
++f; ++f;
} }
break; break;
case 'p': // print to stdout case 'p': // print to stdout
printInfo(1); printInfo(1);
++print;
break; break;
case 'w': // send files from folder (n is specified after) case 'w': // send files from folder (n is specified after)
if (optarg && strnlen(optarg, MAXARGLENGTH) > 0 && optarg[0] == '-') { if (optarg && strnlen(optarg, MAXARGLENGTH) > 0 && optarg[0] == '-') {
@ -118,9 +129,54 @@ int main(int argc, char* argv[]) {
strncpy(args, optarg, strnlen(optarg, MAXARGLENGTH-1)+1); strncpy(args, optarg, strnlen(optarg, MAXARGLENGTH-1)+1);
addCommand(&cmds, opt, args); addCommand(&cmds, opt, args);
break; break;
case 'W': // files to send separated by ','
case 'D': // directory to store recived files
case 'r': // files to read from server separated by ','
case 'd': // directory to store read files
case 't': // time in ms between requests
case 'l': // file to request lock of separated by ','
case 'u': // file to relese lock of separated by ','
case 'c': // files to remove separated by ','
if (optarg && strnlen(optarg, MAXARGLENGTH) > 0 && optarg[0] == '-') {
fprintf(stderr, "Il comando -%c necessita di un argomento.\n", optopt);
goto _cleanup;
}
memset(args, 0, MAXARGLENGTH);
strncpy(args, optarg, strnlen(optarg, MAXARGLENGTH-1)+1);
addCommand(&cmds, opt, args);
break;
case 'R': // read n random files
if (optarg && strnlen(optarg, MAXARGLENGTH) > 0 && optarg[0] == '-') {
optind -= 1;
addCommand(&cmds, opt, NULL);
break;
}
memset(args, 0, MAXARGLENGTH);
strncpy(args, optarg, strnlen(optarg, MAXARGLENGTH-1)+1);
addCommand(&cmds, opt, args);
break;
case ':': // command with no argument (:: is a GNU extension)
switch(optopt) {
case 'R':
addCommand(&cmds, opt, NULL);
break;
default:
fprintf(stderr, "Il comando -%c necessita di un argomento.\n", optopt);
goto _cleanup;
}
break;
case '?': // unknown
default: // unknown
fprintf(stderr, "Comando non riconosciuto: -%c.\n", optopt);
usage(argv[0]);
goto _cleanup;
} }
} }
if(execute(cmds, print) < 0) {
perror("execute");
goto _cleanup;
}
return 0; return 0;
@ -188,3 +244,208 @@ int addCommand(cmd_t **l, char cmd, char *arg) {
tail->next = new; tail->next = new;
return 0; return 0;
} }
int execute(cmd_t *l, int print) {
if(!l) {
errno = EINVAL;
return -1;
}
cmd_t *tmp = l;
struct timespec interval;
interval.tv_nsec = 0;
interval.tv_sec = 0;
// loop that serches for -t
while(tmp) {
switch(tmp->name) {
case 't': {// time in ms between requests
long num;
num = strtol(tmp->arg, NULL, 10);
if(num==0 && errno==EINVAL) {
errno = EINVAL;
perror("Invalid time specified after -t");
return -1;
}
// milliseconds converted to nanoseconds
interval.tv_nsec = (num%1000) * 1000000;
// seconds
interval.tv_sec = (num/1000);
if (print)
printf("\nt - Tempo fra due richieste: %ld ms\tEsito: ok\n", num);
break;
}
default:
break;
}
tmp = tmp->next;
}
// loop that serches for -f
tmp = l;
while(tmp) {
switch(tmp->name) {
case 'f': {
int ok = 1;
strncpy(globalSocket, tmp->arg, strnlen(tmp->arg, MAXARGLENGTH)+1);
if(cmd_f(tmp->arg) != 0) {
if (print)
perror("-f");
ok = 0;
}
if (print) {
printf("\nf - Connessione al socket: %s\tEsito: ", globalSocket);
if(ok)
printf("ok\n");
if(!ok)
printf("errore\n");
}
if(!ok)
return 0; // no socket to connect, nothing to do
// we only read the first -f, no error reported if more than one is
// specified
tmp = NULL;
break;
}
default:
break;
}
tmp = tmp->next;
}
// loop that checks for consistencies:
// 1) -D with no -w or -W after
// 2) -d with no -r or -R after
tmp = l;
int unmachedD = 0;
int unmachedd = 0;
while(tmp){
switch (tmp->name) {
case 'D':
if(unmachedD) {
printf("\nError: -D has no -w or -W matching after\n");
return 0;
}
unmachedD = 1;
break;
case 'w':
case 'W':
unmachedD = 0;
break;
case 'd':
if(unmachedd) {
printf("\nError: -d has no -r or -R matching after\n");
return 0;
}
unmachedd = 1;
break;
case 'r':
case 'R':
unmachedd = 0;
break;
default:
break;
}
tmp = tmp->next;
}
if(unmachedD) {
printf("\nError: -D has no -w or -W matching after\n");
return 0;
}
if(unmachedd) {
printf("\nError: -d has no -r or -R matching after\n");
return 0;
}
char *Dir = NULL; // -D folder
char *dir = NULL; // -d folder
// loop that executes -w, -W, -D; -r, -R, -d; -l, -u, -c
tmp = l;
while(tmp) {
switch (tmp->name) {
case 'w':
cmd_w(tmp->arg, Dir, print);
break;
case 'W':
cmd_W(tmp->arg, Dir, print);
break;
case 'D':
if(Dir)
free(Dir);
Dir = malloc(strnlen(tmp->arg, MAXARGLENGTH)+1);
strncpy(Dir, tmp->arg, strnlen(tmp->arg, MAXARGLENGTH));
if (setDirectory(Dir, 1) == -1) {
if(print)
perror("-D");
}
if (print)
printf("\nD - Cartella per le scritture: %s\tEsito: ok\n", Dir);
break;
case 'r':
break;
case 'R':
break;
case 'd':
if(dir)
free(dir);
dir = malloc(strnlen(tmp->arg, MAXARGLENGTH)+1);
strncpy(dir, tmp->arg, strnlen(tmp->arg, MAXARGLENGTH));
if(setDirectory(dir, 0) == -1) {
if(print)
perror("-d");
}
if(print)
printf("\nd - Cartella per le letture: %s\tEsito: ok\n", dir);
break;
case 'l':
break;
case 'u':
break;
case 'c':
break;
default:
break;
}
tmp = tmp->next;
}
return 0;
}
// -----------------------------------------------------------------------------
// funzioni relative ai comandi
int cmd_f(char *socket) {
if(!socket) {
errno = EINVAL;
return -1;
}
struct timespec ts;
ts.tv_sec = 2;
ts.tv_nsec = 0;
if (openConnection(socket, 100, ts) != 0)
return -1;
return 0;
}
int cmd_w(char *dirname, char *Dir, int print) {
return 0;
}
int cmd_W(char *filelist, char *Dir, int print) {
return 0;
}