diff --git a/scripts/test1.sh b/scripts/test1.sh index f31a079..7f2f254 100644 --- a/scripts/test1.sh +++ b/scripts/test1.sh @@ -1,5 +1,6 @@ #!/bin/sh -./build/client -t 200 -f socket -w testFiles/1 -D build/Wdir -W testFiles/2/file6 -r testFiles/2/file6 -d build/Rdir -R n=3 -p -./build/client -t 200 -f socket -w testFiles/2 -D build/Wdir -W testFiles/6/f1 -r testFiles/6/f1 -d build/Rdir -R n=3 -l testFiles/6/f1 -u testFiles/6/f1 -l testFiles/6/f1 -c testFiles/6/f1 -p + +./build/client -t 200 -f socket -w testFiles/1 -W testFiles/2/file6 -D build/Wdir -r testFiles/2/file6 -R n=3 -d build/Rdir -p +./build/client -t 200 -f socket -w testFiles/2 -W testFiles/6/f1 -D build/Wdir -r testFiles/6/f1 -R n=3 -d build/Rdir -l testFiles/6/f1 -u testFiles/6/f1 -l testFiles/6/f1 -c testFiles/6/f1 -p exit 0 diff --git a/scripts/test2.sh b/scripts/test2.sh index 23074e8..bcf9b60 100644 --- a/scripts/test2.sh +++ b/scripts/test2.sh @@ -1,10 +1,10 @@ #!/bin/sh -./build/client -t 200 -f socket -w testFiles/1 -D ./build/Wdir -W testFiles/1/file1 -r testFiles/1/file1 -d ./build/Rdir -R n=3 -l testFiles/1/file1 -u testFiles/1/file1 -c testFiles/1/file1 -p & -./build/client -t 200 -f socket -w testFiles/2 -D ./build/Wdir -W testFiles/2/file6 -r testFiles/2/file6 -d ./build/Rdir -R n=3 -l testFiles/2/file6 -u testFiles/2/file6 -c testFiles/2/file6 -p & -./build/client -t 200 -f socket -w testFiles/3 -D ./build/Wdir -W testFiles/3/file11 -r testFiles/3/file11 -d ./build/Rdir -R n=3 -l testFiles/3/file11 -u testFiles/3/file11 -c testFiles/3/file11 -p & -./build/client -t 200 -f socket -w testFiles/4 -D ./build/Wdir -W testFiles/3/file11 -r testFiles/3/file11 -d ./build/Rdir -R n=3 -l testFiles/3/file11 -u testFiles/3/file11 -c testFiles/3/file11 -p & -./build/client -t 200 -f socket -w testFiles/5 -D ./build/Wdir -W testFiles/3/file11 -r testFiles/3/file11 -d ./build/Rdir -R n=3 -l testFiles/3/file11 -u testFiles/3/file11 -c testFiles/3/file11 -p & +./build/client -t 200 -f socket -w testFiles/1 -l testFiles/1/file1 -W testFiles/6/f1 -D build/Wdir -r testFiles/1/file1 -R n=3 -d build/Rdir -u testFiles/1/file1 -p & +./build/client -t 200 -f socket -w testFiles/2 -W testFiles/6/f2 -D build/Wdir -l testFiles/1/file1 -r testFiles/2/file6 -R n=3 -d build/Rdir -u testFiles/1/file1 -p & +./build/client -t 200 -f socket -w testFiles/3 -W testFiles/6/f3 -D build/Wdir -r testFiles/3/file11 -R n=3 -d build/Rdir -l testFiles/3/file11 -c testFiles/3/file11 -p & +./build/client -t 200 -f socket -w testFiles/4 -W testFiles/6/f4 -D build/Wdir -r testFiles/4/file16 -R n=3 -d build/Rdir -l testFiles/3/file12 -c testFiles/3/file12 -p & +./build/client -t 200 -f socket -w testFiles/5 -W testFiles/6/f5 -D build/Wdir -r testFiles/5/file21 -R n=3 -d build/Rdir -l testFiles/3/file13 -c testFiles/3/file13 -p & wait exit 0 diff --git a/src/client.c b/src/client.c index 90f3408..aa27fbc 100644 --- a/src/client.c +++ b/src/client.c @@ -37,9 +37,14 @@ typedef struct cmd_s { void destroyCommandList(cmd_t *l); // aggiunge un comando alla lista int addCommand(cmd_t **l, char cmd, char *arg); +// riordina i comandi +int reorderCommandList(cmd_t **l); // esegue tutti i comandi nella lista int execute(cmd_t *l, int print); +int printList(cmd_t *l); + + // per chiudere la connessione prima dell'uscita void cleanup() { if (strncmp(globalSocket, "", 2) != 0) { @@ -137,9 +142,9 @@ int main(int argc, char* argv[]) { break; case 'p': // print to stdout printInfo(1, stdout); - ++print; + print|=1; break; - case 'w': // send files from folder (n is specified after) + case 'w': // send files from folder (n is read after) if (optarg && strnlen(optarg, MAXARGLENGTH) > 0 && optarg[0] == '-') { fprintf(stderr, "Il comando -w necessita di un argomento.\n"); goto _cleanup; @@ -192,6 +197,11 @@ int main(int argc, char* argv[]) { } } + if(reorderCommandList(&cmds) < 0) { + perror("reorder"); + goto _cleanup; + } + if(execute(cmds, print) < 0) { perror("execute"); goto _cleanup; @@ -229,6 +239,20 @@ void destroyCommandList(cmd_t *l) { } } +int printList(cmd_t *l) { + if(!l) { + errno = EINVAL; + return -1; + } + + cmd_t *tmp = l; + while(tmp) { + fprintf(stdout, "%c with args: [%s]\n", tmp->name, tmp->arg); + tmp = tmp->next; + } + return 0; +} + int addCommand(cmd_t **l, char cmd, char *arg) { if(!l) { errno = EINVAL; @@ -268,6 +292,115 @@ int addCommand(cmd_t **l, char cmd, char *arg) { return 0; } +// inverts the order of -D and -d angainst -w, -W or -r, -R +int reorderCommandList(cmd_t **l) { + if(!l) { + errno = EINVAL; + return -1; + } + if(!*l) { + return 0; + } + + cmd_t *tmp = *l; + cmd_t *prev = NULL; + + while(tmp) { + switch (tmp->name) { + case 'd': { + if(prev == NULL || (prev->name != 'r' && prev->name != 'R')) { + fprintf(stdout, "\nError: -d has no -r or -R matching before\n"); + return -1; + } + // invert tmp and prev + cmd_t t; + t.arg = tmp->arg; + t.name = tmp->name; + + tmp->arg = prev->arg; + tmp->name = prev->name; + + prev->arg = t.arg; + prev->name = t.name; + + if(!prev->next) + break; + + // add redirect to dev/null for next command if it's r or R + if(tmp->next->name == 'r' || tmp->next->name == 'R') { + cmd_t *new = calloc(1, sizeof(*new)); + if(!new){ + perror("calloc new"); + return -1; + } + + new->name = 'd'; + new->next = tmp->next; + + new->arg = malloc(MAXARGLENGTH); + if (new->arg == NULL) { + perror("malloc arg"); + free(new); + return -1; + } + strcpy(new->arg, "/dev/null"); + tmp->next = new; + tmp = new; + } + break; + } + case 'D': { + if(prev == NULL || (prev->name != 'w' && prev->name != 'W')) { + fprintf(stdout, "\nError: -D has no -w or -W matching before\n"); + return -1; + } + // invert tmp and prev + cmd_t t; + t.arg = tmp->arg; + t.name = tmp->name; + + tmp->arg = prev->arg; + tmp->name = prev->name; + + prev->arg = t.arg; + prev->name = t.name; + + if(!tmp->next) + break; + + // add redirect to dev/null for next command if it's w or W + if(tmp->next->name == 'w' || tmp->next->name == 'W') { + cmd_t *new = calloc(1, sizeof(*new)); + if(!new){ + perror("calloc new"); + return -1; + } + + new->name = 'D'; + new->next = tmp->next; + + new->arg = malloc(MAXARGLENGTH); + if (new->arg == NULL) { + perror("malloc arg"); + free(new); + return -1; + } + strcpy(new->arg, "/dev/null"); + tmp->next = new; + tmp = new; + } + break; + } + default: + break; + } + prev = tmp; + tmp = tmp->next; + } + + return 0; +} + int execute(cmd_t *l, int print) { if(!l) { errno = EINVAL; @@ -286,7 +419,6 @@ int execute(cmd_t *l, int print) { long num; num = strtol(tmp->arg, NULL, 10); if(num==0 && errno==EINVAL) { - errno = EINVAL; perror("Invalid time specified after -t"); return -1; } @@ -298,12 +430,14 @@ int execute(cmd_t *l, int print) { if (print) fprintf(stdout, "t - Tempo fra due richieste: %ld ms\tEsito: ok\n", num); + tmp = NULL; break; } default: break; } - tmp = tmp->next; + if(tmp) + tmp = tmp->next; } fflush(stdout); diff --git a/src/server.c b/src/server.c index 55a4aa3..8209c4e 100644 --- a/src/server.c +++ b/src/server.c @@ -108,7 +108,7 @@ int main(int argc, char *argv[]) { int request_pipe[2]; if (pipe(signal_pipe) == -1) { perror("pipe"); - goto _cleanup; + goto _cleanup_beforesigthread; } if (pipe(request_pipe) == -1) { perror("pipe");