started fixing bugs

This commit is contained in:
elvis
2022-04-08 21:32:52 +02:00
parent 044b0f527a
commit 9f4d092af5
7 changed files with 170 additions and 116 deletions

View File

@ -236,7 +236,7 @@ int main(int argc, char *argv[]) {
// cerchiamo di capire da quale fd abbiamo ricevuto una richiesta
for(int i=0; i <= fdmax; ++i) {
if (FD_ISSET(i, &tmpset)) {
long* connfd = malloc(sizeof(long));
int* connfd = malloc(sizeof(long));
if (!connfd) {
perror("ERROR FATAL malloc");
goto _cleanup;
@ -260,7 +260,7 @@ int main(int argc, char *argv[]) {
// scrivo sul log
n = snprintf(buf, sizeof(buf), "New client: %ld\n", (long) *connfd);
n = snprintf(buf, sizeof(buf), "New client: %d\n", *connfd);
if( n<0 ) {
perror("snprintf");
goto _cleanup;

View File

@ -10,6 +10,11 @@
#include <fileQueue.h>
#include <apiFile.h>
#include <taglialegna.h>
#include <serverWorker.h>
#include <threadpool.h>
int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, pthread_mutex_t *lock, waiting_t **waiting);
// funzione eseguita dal Worker thread del pool
void threadF(void *arg) {
@ -18,13 +23,20 @@ void threadF(void *arg) {
return;
}
// TODO add necessary variables from main
long* connfd = ;
threadT *argl = (threadT *) arg;
int connfd = *argl->connfd;
volatile int *quit = argl->quit;
int request_pipe = argl->request_pipe;
queueT *q = argl->q;
taglia_t *taglia = argl->taglia;
// threadpool_t *pool = argl->pool;
pthread_mutex_t *lock = argl->lock;
waiting_t **waiting = argl->waiting;
fd_set set, tmpset;
FD_ZERO(&set);
FD_SET(*connfd, &set);
FD_SET(connfd, &set);
while(*quit == 0) {
tmpset=set;
@ -47,58 +59,92 @@ void threadF(void *arg) {
// comunicate with the client
msg_t str;
long n;
// leggo la dimensione del messaggio
if ((n=readn(connfd, &str.len, sizeof(long))) == -1) {
perror("read1");
break;
goto _cleanup;
}
if (n==0)
break;
goto _cleanup;;
str.str = calloc(str.len+1, sizeof(char));
if (!str.str) {
perror("calloc");
fprintf(stderr, "Calloc.\n");
break;
goto _cleanup;
}
// leggo il messaggio
if ((n=readn(connfd, str.str, str.len * sizeof(char))) == -1) {
perror("read2");
free(str.str);
break;
goto _cleanup;
}
str.str[str.len+1] = '\0';
str.str[str.len] = '\0';
if(strncmp(str.str, "quit", 5)) { // il client vuole chiudere la connessione
close(connfd);
int close = -1;
// comunico al manager che ho chiuso la connessione
// ...
if (writen(request_pipe, &close, sizeof(int)) == -1) {
perror("writen");
goto _cleanup;
}
// log chiusura connessione
// ...
int n = 0;
char buf[1024];
n = snprintf(buf, sizeof(buf), "Chiusa connessione con il client %d.\n", connfd);
if( n<0 ) {
perror("snprintf");
goto _cleanup;
}
if( taglia_log(taglia, buf) < 0 )
goto _cleanup;
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
if (parser(str.len, str.str, q, connfd, taglia, lock, waiting) == -1) {
goto _cleanup;
}
// str.str non è più valido perchè parser fa free
// comunico al manager che è stata servita la richiesta
if (writen(request_pipe, &connfd, sizeof(int)) == -1) {
perror("writen");
}
// log
int m = 0;
char buf[1024];
m = snprintf(buf, sizeof(buf), "Servito una richiesta del client %d.\n", connfd);
if( m<0 ) {
perror("snprintf");
goto _cleanup;
}
if( taglia_log(taglia, buf) < 0 )
goto _cleanup;
return;
_cleanup:
if(arg)
free(arg);
close(connfd);
// nothing to do because no memory needs to be freed
return;
}
int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, pthread_mutex_t *lock, waitingT **waiting) {
if(len<0 || !command || !queue || !logFileT || !waiting) {
int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, pthread_mutex_t *lock, waiting_t **waiting) {
if(len<0 || !command || !queue || !taglia || !waiting) {
errno = EINVAL;
return -1;
}
char *save = command;
char *string = calloc(1, len);
if(string == NULL) {
perror("calloc");
return -1;
}
strncpy(string, command, len-1); // strlcpy is only bsd :(
string[len-1] = '\0';
char *token = NULL;
char *token2 = NULL;
char *token3 = NULL;
@ -116,7 +162,7 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
int arg = (int) strtol(token3, NULL, 10);
openFile(token2, arg, queue, fd_c, logFileT);
openFile(token2, arg, queue, fd_c, taglia);
goto _parser_end;
}
@ -124,7 +170,7 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
if(!token2)
goto _parser_cleanup;
readFile(token2, queue, fd_c, logFileT);
readFile(token2, queue, fd_c, taglia);
goto _parser_end;
}
@ -132,7 +178,7 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
if(!token2)
goto _parser_cleanup;
readNFiles(token2, queue, fd_c, logFileT);
readNFiles(token2, queue, fd_c, taglia);
goto _parser_end;
}
@ -141,7 +187,7 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
goto _parser_cleanup;
size_t sz = (size_t) strtol(token3, NULL, 10);
writeFile(token2, sz, queue, fd_c, logFileT, 0);
writeFile(token2, sz, queue, fd_c, taglia, 0);
goto _parser_end;
}
@ -150,7 +196,7 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
goto _parser_cleanup;
size_t sz = (size_t) strtol(token3, NULL, 10);
writeFile(token2, sz, queue, fd_c, logFileT, 1);
writeFile(token2, sz, queue, fd_c, taglia, 1);
goto _parser_end;
}
@ -158,7 +204,7 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
if(!token2)
goto _parser_cleanup;
lockFile(token2, queue, fd_c, logFileT, lock, waiting);
lockFile(token2, queue, fd_c, taglia, lock, waiting);
goto _parser_end;
}
@ -166,7 +212,7 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
if(!token2)
goto _parser_cleanup;
unlockFile(token2, queue, fd_c, logFileT, lock, waiting);
unlockFile(token2, queue, fd_c, taglia, lock, waiting);
goto _parser_end;
}
@ -174,7 +220,7 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
if(!token2)
goto _parser_cleanup;
closeFile(token2, queue, fd_c, logFileT, lock, waiting);
closeFile(token2, queue, fd_c, taglia, lock, waiting);
goto _parser_end;
}
@ -182,7 +228,7 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
if(!token2)
goto _parser_cleanup;
removeFile(token2, queue, fd_c, logFileT, lock, waiting);
removeFile(token2, queue, fd_c, taglia, lock, waiting);
goto _parser_end;
}
// se arrivo qui non ho riconosciuto il comando

View File

@ -5,12 +5,13 @@
#include <apiFile.h>
#include <fileQueue.h>
#include <taglialegna.h>
#include <threadpool.h>
// struttura dati che contiene gli argomenti da passare ai worker threads
typedef struct struct_thread {
volatile int *quit;
int request_pipe;
long *connfd;
int *connfd;
queueT *q; // puntatore alla queue dei file
taglia_t *taglia; // puntatore alla struct del file di log
threadpool_t *pool; // puntatore alla threadpool