comments and perror for serverWorker.c

This commit is contained in:
elvis
2022-05-05 23:42:04 +02:00
parent 1779fed379
commit b602e8e7dd

View File

@ -19,7 +19,7 @@
int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, pthread_mutex_t *lock, waiting_t **waiting); 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 // worker function
void threadF(void *arg) { void threadF(void *arg) {
if(!arg){ if(!arg){
errno = EINVAL; errno = EINVAL;
@ -37,6 +37,7 @@ void threadF(void *arg) {
pthread_mutex_t *lock = argl->lock; pthread_mutex_t *lock = argl->lock;
waiting_t **waiting = argl->waiting; waiting_t **waiting = argl->waiting;
// search for the id of the thread
int myid = -1; int myid = -1;
pthread_t tid = pthread_self(); pthread_t tid = pthread_self();
@ -54,10 +55,10 @@ void threadF(void *arg) {
while(*quit == 0) { while(*quit == 0) {
tmpset=set; tmpset=set;
int r; int r;
// ogni tanto controllo se devo terminare // check if we need to quit
struct timeval timeout={0, 100000}; // 100 milliseconds struct timeval timeout={0, 100000}; // 100 milliseconds
if ((r=select(connfd+1, &tmpset, NULL, NULL, &timeout)) < 0) { if ((r=select(connfd+1, &tmpset, NULL, NULL, &timeout)) < 0) {
perror("Select"); perror("threadF: select");
break; break;
} }
if (r==0) { if (r==0) {
@ -71,9 +72,9 @@ void threadF(void *arg) {
// comunicate with the client // comunicate with the client
msg_t str; msg_t str;
long n; long n;
// leggo la dimensione del messaggio // read the size of the message
if ((n=readn(connfd, &str.len, sizeof(long))) == -1) { if ((n=readn(connfd, &str.len, sizeof(long))) == -1) {
perror("read1"); perror("threadF: readn, dimension");
goto _cleanup; goto _cleanup;
} }
@ -84,34 +85,34 @@ void threadF(void *arg) {
str.str = calloc(str.len+1, sizeof(char)); str.str = calloc(str.len+1, sizeof(char));
if (!str.str) { if (!str.str) {
perror("calloc"); perror("threadF: calloc");
fprintf(stderr, "Calloc.\n");
goto _cleanup; goto _cleanup;
} }
// leggo il messaggio // read the message
if ((n=readn(connfd, str.str, str.len * sizeof(char))) == -1) { if ((n=readn(connfd, str.str, str.len * sizeof(char))) == -1) {
perror("read2"); perror("threadF: readn, message");
free(str.str); free(str.str);
goto _cleanup; goto _cleanup;
} }
str.str[str.len] = '\0'; str.str[str.len] = '\0';
closeConnection: closeConnection:
if(n==0 || strncmp(str.str, "quit", 5) == 0) { // il client vuole chiudere la connessione // the clients wants to disconnect or already diconnected
if(n==0 || strncmp(str.str, "quit", 5) == 0) {
close(connfd); close(connfd);
long close = -1; long close = -1;
// comunico al manager che ho chiuso la connessione // tell main that the client disconnected
if (writen(request_pipe, &close, sizeof(long)) == -1) { if (writen(request_pipe, &close, sizeof(long)) == -1) {
perror("writen"); perror("threadF: writen");
goto _cleanup; goto _cleanup;
} }
// log chiusura connessione // log closing connection
int n = 0; int n = 0;
char buf[1024]; char buf[1024];
n = snprintf(buf, sizeof(buf), "Chiusa connessione con il client %ld.\n", connfd); n = snprintf(buf, sizeof(buf), "Chiusa connessione con il client %ld.\n", connfd);
if( n<0 ) { if( n<0 ) {
perror("snprintf"); perror("threadF: snprintf");
goto _cleanup; goto _cleanup;
} }
if( taglia_log(taglia, buf) < 0 ) if( taglia_log(taglia, buf) < 0 )
@ -119,22 +120,23 @@ closeConnection:
goto _cleanup; goto _cleanup;
} }
// eseguo quello che mi chiede il client di fare // execute what the client requested
if (parser(str.len, str.str, q, connfd, taglia, lock, waiting) == -1) { if (parser(str.len, str.str, q, connfd, taglia, lock, waiting) == -1) {
goto _cleanup; goto _cleanup;
} }
// str.str non è più valido perchè parser fa free // str.str is not valid anymore because parser frees
// comunico al manager che è stata servita la richiesta // tell main that the request has been handled
if (writen(request_pipe, &connfd, sizeof(long)) == -1) { if (writen(request_pipe, &connfd, sizeof(long)) == -1) {
perror("writen"); perror("threadF: writen");
} }
// log
// write to logfile
int m = 0; int m = 0;
char buf[1024]; char buf[1024];
m = snprintf(buf, sizeof(buf), "Thread %d ha servito una richiesta del client %ld.\n", myid, connfd); m = snprintf(buf, sizeof(buf), "Thread %d ha servito una richiesta del client %ld.\n", myid, connfd);
if( m<0 ) { if( m<0 ) {
perror("snprintf"); perror("threadF: snprintf");
goto _cleanup; goto _cleanup;
} }
if( taglia_log(taglia, buf) < 0 ) if( taglia_log(taglia, buf) < 0 )
@ -155,9 +157,10 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
return -1; return -1;
} }
// copy command because we modify it later
char *string = calloc(1, len+1); char *string = calloc(1, len+1);
if(string == NULL) { if(string == NULL) {
perror("calloc"); perror("parser: calloc");
return -1; return -1;
} }
strncpy(string, command, len); strncpy(string, command, len);
@ -251,7 +254,7 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
removeFile(token2, queue, fd_c, taglia, lock, waiting); removeFile(token2, queue, fd_c, taglia, lock, waiting);
goto _parser_end; goto _parser_end;
} }
// se arrivo qui non ho riconosciuto il comando // if here no match
_parser_cleanup: _parser_cleanup:
free(command); free(command);