Files
progettoso/src/serverWorker.c
2022-03-16 23:44:53 +01:00

197 lines
3.9 KiB
C

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <assert.h>
#include <sys/select.h>
#include <conn.h>
#include <message.h>
#include <serverQueue.h>
#include <apiFile.h>
// funzione eseguita dal Worker thread del pool
void threadF(void *arg) {
if(!arg){
errno = EINVAL;
return;
}
// TODO add necessary variables from main
long* connfd = (long*)arg[0];
fd_set set, tmpset;
FD_ZERO(&set);
FD_SET(*connfd, &set);
while(*quit == 0) {
tmpset=set;
int r;
// ogni tanto controllo se devo terminare
struct timeval timeout={0, 100000}; // 100 milliseconds
if ((r=select(connfd+1, &tmpset, NULL, NULL, &timeout)) < 0) {
perror("Select");
break;
}
if (r==0) {
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;
}
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);
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;
}