Added worker logic and started apiFile
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <ctype.h>
|
||||
#include <assert.h>
|
||||
@ -6,31 +7,24 @@
|
||||
|
||||
#include <conn.h>
|
||||
#include <message.h>
|
||||
#include <serverStatus.h>
|
||||
|
||||
// converte tutti i carattere minuscoli in maiuscoli
|
||||
static void toup(char *str) {
|
||||
char *p = str;
|
||||
while(*p != '\0') {
|
||||
*p = (islower(*p)?toupper(*p):*p);
|
||||
++p;
|
||||
}
|
||||
}
|
||||
#include <serverQueue.h>
|
||||
#include <apiFile.h>
|
||||
|
||||
// funzione eseguita dal Worker thread del pool
|
||||
// gestisce una intera connessione di un client
|
||||
//
|
||||
void threadF(void *arg) {
|
||||
assert(arg);
|
||||
if(!arg){
|
||||
errno = EINVAL;
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO add necessary variables from main
|
||||
long* connfd = (long*)arg[0];
|
||||
serverStatus* status = (serverStatus*)(arg[1]);
|
||||
|
||||
fd_set set, tmpset;
|
||||
FD_ZERO(&set);
|
||||
FD_SET(*connfd, &set);
|
||||
|
||||
do {
|
||||
while(*quit == 0) {
|
||||
tmpset=set;
|
||||
int r;
|
||||
// ogni tanto controllo se devo terminare
|
||||
@ -40,47 +34,163 @@ void threadF(void *arg) {
|
||||
break;
|
||||
}
|
||||
if (r==0) {
|
||||
if ((status->exiting) != 0)
|
||||
break;
|
||||
if (*quit)
|
||||
goto _cleanup;
|
||||
continue;
|
||||
}
|
||||
break; // r==0 and quit==0
|
||||
}
|
||||
|
||||
|
||||
// comunicate with the client
|
||||
msg_t str;
|
||||
long n;
|
||||
if ((n=readn(connfd, &str.len, sizeof(long))) == -1) {
|
||||
perror("read1");
|
||||
break;
|
||||
}
|
||||
// comunicate with the client
|
||||
msg_t str;
|
||||
long n;
|
||||
if ((n=readn(connfd, &str.len, sizeof(long))) == -1) {
|
||||
perror("read1");
|
||||
break;
|
||||
}
|
||||
|
||||
if (n==0)
|
||||
break;
|
||||
str.str = calloc(str.len, sizeof(char));
|
||||
if (!str.str) {
|
||||
perror("calloc");
|
||||
fprintf(stderr, "Memoria esaurita....\n");
|
||||
break;
|
||||
}
|
||||
if ((n=readn(connfd, str.str, str.len * sizeof(char))) == -1) {
|
||||
perror("read2");
|
||||
free(str.str);
|
||||
break;
|
||||
}
|
||||
|
||||
toup(str.str);
|
||||
|
||||
if ((n=writen(connfd, &str.len, sizeof(int))) == -1) {
|
||||
perror("write1");
|
||||
free(str.str);
|
||||
break;
|
||||
}
|
||||
if ((n=writen(connfd, str.str, str.len*sizeof(char))) == -1) {
|
||||
perror("write2");
|
||||
free(str.str);
|
||||
break;
|
||||
}
|
||||
if (n==0)
|
||||
break;
|
||||
str.str = calloc(str.len+1, sizeof(char));
|
||||
if (!str.str) {
|
||||
perror("calloc");
|
||||
fprintf(stderr, "Calloc.\n");
|
||||
break;
|
||||
}
|
||||
if ((n=readn(connfd, str.str, str.len * sizeof(char))) == -1) {
|
||||
perror("read2");
|
||||
free(str.str);
|
||||
} while(status->exiting == 0);
|
||||
break;
|
||||
}
|
||||
str.str[str.len+1] = '\0';
|
||||
|
||||
if(strncmp(str.str, "quit", 5)) { // il client vuole chiudere la connessione
|
||||
// comunico al manager che ho chiuso la connessione
|
||||
// ...
|
||||
// log chiusura connessione
|
||||
// ...
|
||||
goto _cleanup;
|
||||
}
|
||||
|
||||
// eseguo quello che mi chiede il client di fare
|
||||
if (parser(str.len, str.str) == -1) {
|
||||
// str.str non è più valido perchè parser fa free
|
||||
|
||||
}
|
||||
|
||||
// comunico al manager che è stata servita la richiesta
|
||||
|
||||
// log
|
||||
|
||||
_cleanup:
|
||||
if(arg)
|
||||
free(arg);
|
||||
close(connfd);
|
||||
}
|
||||
|
||||
|
||||
int parser(int len, char *command, queueT *queue, long fd_c, logT* logFileT, pthread_mutex_t *lock, waitingT **waiting) {
|
||||
if(len<0 || !command || !queue || !logFileT || !waiting) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *save = command;
|
||||
char *token = NULL;
|
||||
char *token2 = NULL;
|
||||
char *token3 = NULL;
|
||||
|
||||
token = strsep(&string, "|");
|
||||
token2 = strsep(&string, "|");
|
||||
token3 = strsep(&string, "|");
|
||||
|
||||
if(!token)
|
||||
goto _parser_cleanup;
|
||||
|
||||
if(strcmp(token, "openFile") == 0) {
|
||||
if(!token3 || !token2)
|
||||
goto _parser_cleanup;
|
||||
|
||||
int arg = (int) strtol(token3, NULL, 10);
|
||||
|
||||
openFile(token2, arg, queue, fd_c, logFileT);
|
||||
goto _parser_end;
|
||||
}
|
||||
|
||||
if (strcmp(token, "readFile") == 0) {
|
||||
if(!token2)
|
||||
goto _parser_cleanup;
|
||||
|
||||
readFile(token2, queue, fd_c, logFileT);
|
||||
goto _parser_end;
|
||||
}
|
||||
|
||||
if (strcmp(token, "readNFiles") == 0) {
|
||||
if(!token2)
|
||||
goto _parser_cleanup;
|
||||
|
||||
readNFiles(token2, queue, fd_c, logFileT);
|
||||
goto _parser_end;
|
||||
}
|
||||
|
||||
if (strcmp(token, "writeFile") == 0) {
|
||||
if(!token3 || !token2)
|
||||
goto _parser_cleanup;
|
||||
size_t sz = (size_t) strtol(token3, NULL, 10);
|
||||
|
||||
writeFile(token2, sz, queue, fd_c, logFileT, 0);
|
||||
goto _parser_end;
|
||||
}
|
||||
|
||||
if (strcmp(token, "appendToFile") == 0) {
|
||||
if(!token3 || !token2)
|
||||
goto _parser_cleanup;
|
||||
size_t sz = (size_t) strtol(token3, NULL, 10);
|
||||
|
||||
writeFile(token2, sz, queue, fd_c, logFileT, 1);
|
||||
goto _parser_end;
|
||||
}
|
||||
|
||||
if (strcmp(token, "lockFile") == 0) {
|
||||
if(!token2)
|
||||
goto _parser_cleanup;
|
||||
|
||||
lockFile(token2, queue, fd_c, logFileT, lock, waiting);
|
||||
goto _parser_end;
|
||||
}
|
||||
|
||||
if (strcmp(token, "unlockFile") == 0) {
|
||||
if(!token2)
|
||||
goto _parser_cleanup;
|
||||
|
||||
unlockFile(token2, queue, fd_c, logFileT, lock, waiting);
|
||||
goto _parser_end;
|
||||
}
|
||||
|
||||
if (strcmp(token, "closeFile") == 0) {
|
||||
if(!token2)
|
||||
goto _parser_cleanup;
|
||||
|
||||
closeFile(token2, queue, fd_c, logFileT, lock, waiting);
|
||||
goto _parser_end;
|
||||
}
|
||||
|
||||
if (strcmp(token, "removeFile") == 0) {
|
||||
if(!token2)
|
||||
goto _parser_cleanup;
|
||||
|
||||
removeFile(token2, queue, fd_c, logFileT, lock, waiting);
|
||||
goto _parser_end;
|
||||
}
|
||||
|
||||
goto _parser_cleanup; // nessun comando riconosciuto
|
||||
|
||||
_parser_end:
|
||||
free(command);
|
||||
return 0;
|
||||
|
||||
_parser_cleanup:
|
||||
free(command);
|
||||
return -1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user