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

@ -3,6 +3,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <errno.h>
#include <taglialegna.h> #include <taglialegna.h>
#include <fileQueue.h> #include <fileQueue.h>
@ -136,7 +137,7 @@ int taglia_update(taglia_t *taglia, queueT *q, int miss) {
taglia->max_files = (len > taglia->max_files)?len:taglia->max_files; taglia->max_files = (len > taglia->max_files)?len:taglia->max_files;
taglia->max_size = (size > taglia->max_size)?size:taglia->max_size; taglia->max_size = (size > taglia->max_size)?size:taglia->max_size;
logFileT->cache_miss += (miss>0)?miss:0; taglia->cache_misses += (miss>0)?miss:0;
pthread_mutex_unlock(&taglia->m); pthread_mutex_unlock(&taglia->m);
@ -155,7 +156,7 @@ int taglia_stats(taglia_t *taglia, FILE *stream) {
fprintf(stream, "Numero di file massimo memorizzato nel server: %zu\n", taglia->max_files); fprintf(stream, "Numero di file massimo memorizzato nel server: %zu\n", taglia->max_files);
fprintf(stream, "Dimensione massima in Mbytes raggiunta dal file storage: %.2lf MB\n", res); fprintf(stream, "Dimensione massima in Mbytes raggiunta dal file storage: %.2lf MB\n", res);
fprintf(stream, "Numero di volte in cui lalgoritmo di rimpiazzamento della cache è stato eseguito per selezionare uno o più file \"vittima\": %zu\n", taglia->cache_miss); fprintf(stream, "Numero di volte in cui lalgoritmo di rimpiazzamento della cache è stato eseguito per selezionare uno o più file \"vittima\": %zu\n", taglia->cache_misses);
fflush(stream); fflush(stream);
@ -166,7 +167,7 @@ int taglia_stats(taglia_t *taglia, FILE *stream) {
if(m<n) goto _error_taglia_stats; if(m<n) goto _error_taglia_stats;
n += snprintf(tmp_buf+n, m-n, "Dimensione massima in Mbytes raggiunta dal file storage: %.2lf MB\n", res); n += snprintf(tmp_buf+n, m-n, "Dimensione massima in Mbytes raggiunta dal file storage: %.2lf MB\n", res);
if(m<n) goto _error_taglia_stats; if(m<n) goto _error_taglia_stats;
n += snprintf(tmp_buf+n, m-n, "Numero di volte in cui lalgoritmo di rimpiazzamento della cache è stato eseguito per selezionare uno o più file \"vittima\": %zu\n", taglia->cache_miss); n += snprintf(tmp_buf+n, m-n, "Numero di volte in cui lalgoritmo di rimpiazzamento della cache è stato eseguito per selezionare uno o più file \"vittima\": %zu\n", taglia->cache_misses);
if(m<n) goto _error_taglia_stats; if(m<n) goto _error_taglia_stats;
pthread_mutex_unlock(&taglia->m); pthread_mutex_unlock(&taglia->m);

View File

@ -1,3 +1,5 @@
#include <strings.h>
#include <apiFile.h> #include <apiFile.h>
#include <taglialegna.h> #include <taglialegna.h>
@ -21,6 +23,30 @@
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
/* funzioni ausiliarie */ /* funzioni ausiliarie */
// invio il messaggio al client e poi l'errno
void serror(char *m, long fd_c, taglia_t *taglia, char *mlog) {
if(!m) {
errno = EINVAL;
m = MESY;
}
if(writen(fd_c, m, strnlen(m, MAXLENMESS)+1) < 0) {
perror("writen");
goto _serror_cleanup;
}
if(writen(fd_c, &errno, sizeof(errno)) < 0) {
perror("writen");
goto _serror_cleanup;
}
if(taglia_write(taglia, mlog) < 0)
goto _serror_cleanup;
return;
_serror_cleanup:
return;
}
// invio il messaggio al client // invio il messaggio al client
void sendMessage(char *m, long fd_c, taglia_t *taglia, char *mlog) { void sendMessage(char *m, long fd_c, taglia_t *taglia, char *mlog) {
if(!m) { if(!m) {
@ -45,11 +71,13 @@ _sendM_cleanup:
void sendMessageFile(char *m, fileT *f, long fd_c, taglia_t *taglia, char *mlog) { void sendMessageFile(char *m, fileT *f, long fd_c, taglia_t *taglia, char *mlog) {
if(!f) { if(!f) {
errno = EINVAL; errno = EINVAL;
serror(MESY, fd_c); char errmlog[2048] = "Errore negli argomenti alla funzione sendMessagefile (fileT == NULL). Messaggio originale:\n\t";
strncat(errmlog, mlog, sizeof(errmlog)-strlen(errmlog)-1);
serror(MESY, fd_c, taglia, errmlog);
goto _sendMF_cleanup; goto _sendMF_cleanup;
} }
if(!m) { if(!m) {
m = MEPF; m = MEFP;
} }
if(writen(fd_c, m, strnlen(m, MAXLENMESS)+1) < 0) { if(writen(fd_c, m, strnlen(m, MAXLENMESS)+1) < 0) {
@ -72,30 +100,32 @@ _sendMF_cleanup:
} }
// invio il messaggio al client e poi n file // invio il messaggio al client e poi n file
void sendMessageFileN(char *m, fileT **f, int n, long fd_c, taglia *taglia, char *mlog) { void sendMessageFileN(char *m, fileT **f, int n, long fd_c, taglia_t *taglia, char *mlog) {
if(!f) { if(!f) {
errno = EINVAL; errno = EINVAL;
serror(MESY, fd_c); char errmlog[2048] = "Errore negli argomenti alla funzione sendMessagefile (fileT == NULL). Messaggio originale:\n\t";
strncat(errmlog, mlog, sizeof(errmlog)-strlen(errmlog)-1);
serror(MESY, fd_c, taglia, errmlog);
goto _sendMFN_cleanup; goto _sendMFN_cleanup;
} }
if(!m) { if(!m) {
m = MEPF; m = MEFP;
} }
if(writen(fd_c, m, strnlen(m, MAXLENMESS)+1) < 0) { if(writen(fd_c, m, strnlen(m, MAXLENMESS)+1) < 0) {
perror("writen"); perror("writen");
goto _sendMF_cleanup; goto _sendMFN_cleanup;
} }
for(int i=0; i<n && (f[i]!=NULL); ++i) { for(int i=0; i<n && (f[i]!=NULL); ++i) {
if(sendFile(f[i], fd_c, taglia) < 0) { if(sendFile(f[i], fd_c, taglia) < 0) {
perror("sendFile"); perror("sendFile");
goto _sendMF_cleanup; goto _sendMFN_cleanup;
} }
} }
if(taglia_write(taglia, mlog) < 0) if(taglia_write(taglia, mlog) < 0)
goto _sendMF_cleanup; goto _sendMFN_cleanup;
return; return;
@ -103,30 +133,6 @@ _sendMFN_cleanup:
return; return;
} }
// invio il messaggio al client e poi l'errno
void serror(char *m, long fd_c, taglia_t *taglia, char *mlog) {
if(!m) {
errno = EINVAL;
m = MESY;
}
if(writen(fd_c, m, strnlen(m, MAXLENMESS)+1) < 0) {
perror("writen");
goto _serror_cleanup;
}
if(writen(fd_c, &errno, sizeof(errno)) < 0) {
perror("writen");
goto _serror_cleanup;
}
if(taglia_write(taglia, mlog) < 0)
goto _serror_cleanup;
return;
_serror_cleanup:
return;
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia) { void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia) {
@ -162,7 +168,7 @@ void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia)
} }
if(found && !create) { if(found && !create) {
if(openFileInQueue(q, file, lock, fd_c) == -1) { if(openFileInQueue(q, filepath, lock, fd_c) == -1) {
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con errore\n", fd_c, flags, filepath); n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con errore\n", fd_c, flags, filepath);
perror("openFileInQueue"); perror("openFileInQueue");
serror(MESE, fd_c, taglia, tmp_buf); serror(MESE, fd_c, taglia, tmp_buf);
@ -174,7 +180,7 @@ void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia)
} }
if(!found && create) { // not found and creating new file if(!found && create) { // not found and creating new file
if(getLen(q) == queue->maxLen) { // capacity miss if(getLen(q) == q->maxLen) { // capacity miss
removed = dequeue(q); removed = dequeue(q);
if(!removed) { if(!removed) {
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con errore\n", fd_c, flags, filepath); n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con errore\n", fd_c, flags, filepath);
@ -188,14 +194,14 @@ void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia)
if(!f) { if(!f) {
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con errore del server\n", fd_c, flags, filepath); n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con errore del server\n", fd_c, flags, filepath);
perror("createFileT"); perror("createFileT");
serror(MESE, fd_c, taglia); serror(MESE, fd_c, taglia, tmp_buf);
return; return;
} }
if(enqueue(q, f) != 0) { if(enqueue(q, f) != 0) {
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con errore del server\n", fd_c, flags, filepath); n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con errore del server\n", fd_c, flags, filepath);
perror("enqueue"); perror("enqueue");
serror(MESE, fd_c, taglia); serror(MESE, fd_c, taglia, tmp_buf);
return; return;
} }
@ -211,14 +217,14 @@ void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia)
if(!f) { if(!f) {
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con errore del server\n", fd_c, flags, filepath); n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con errore del server\n", fd_c, flags, filepath);
perror("createFileT"); perror("createFileT");
serror(MESE, fd_c, taglia); serror(MESE, fd_c, taglia, tmp_buf);
return; return;
} }
if(enqueue(q, f) != 0) { if(enqueue(q, f) != 0) {
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con errore del server\n", fd_c, flags, filepath); n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con errore del server\n", fd_c, flags, filepath);
perror("enqueue"); perror("enqueue");
serror(MESE, fd_c, taglia); serror(MESE, fd_c, taglia, tmp_buf);
return; return;
} }
// abbiamo aggiunto un file quindi il numero di file è cambiato // abbiamo aggiunto un file quindi il numero di file è cambiato
@ -238,7 +244,7 @@ void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia) {
size_t m = sizeof(tmp_buf); size_t m = sizeof(tmp_buf);
if(!filepath || !q || !taglia) { if(!filepath || !q || !taglia) {
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile (flags = %x) sul file \"%s\" e' terminata con errore\n", fd_c, flags, filepath); n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con errore\n", fd_c, filepath);
errno = EINVAL; errno = EINVAL;
serror(MESY, fd_c, taglia, tmp_buf); serror(MESY, fd_c, taglia, tmp_buf);
return; return;
@ -247,21 +253,21 @@ void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia) {
fileT *f = NULL; fileT *f = NULL;
f = find(q, filepath); f = find(q, filepath);
if(!f) { if(!f) {
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile (flags = %x) sul file \"%s\" e' terminata con errore\n", fd_c, flags, filepath); n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con errore\n", fd_c, filepath);
errno = ENOENT; errno = ENOENT;
serror(MESE, fd_c, taglia, tmp_buf); serror(MESE, fd_c, taglia, tmp_buf);
return; return;
} }
if(f->open != 0) { // file already open if(f->open != 0) { // file already open
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile (flags = %x) sul file \"%s\" e' terminata con errore\n", fd_c, flags, filepath); n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con errore\n", fd_c, filepath);
errno = EPERM; errno = EPERM;
serror(MENT, fd_c, taglia, tmp_buf); serror(MENT, fd_c, taglia, tmp_buf);
destroyFile(f); // f is a copy so we need to cleen up destroyFile(f); // f is a copy so we need to cleen up
return; return;
} }
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile (flags = %x) sul file \"%s\" e' terminata con successo\n", fd_c, flags, filepath); n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con successo\n", fd_c, filepath);
sendMessageFile(MEOK, f, fd_c, taglia, tmp_buf); sendMessageFile(MEOK, f, fd_c, taglia, tmp_buf);
destroyFile(f); // f is a copy so we need to cleen up destroyFile(f); // f is a copy so we need to cleen up
return; return;
@ -355,19 +361,19 @@ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *tagl
if(append) { if(append) {
if(appendFileInQueue(q, filepath, content, size, fd_c) == -1) { if(appendFileInQueue(q, filepath, content, size, fd_c) == -1) {
perror("appendFileInQueue"); perror("appendFileInQueue");
free(content) free(content);
return; return;
} }
} else { } else {
if(writeFileInQueue(q, filepath, content, size, fd_c) == -1) { if(writeFileInQueue(q, filepath, content, size, fd_c) == -1) {
perror("writeFileInQueue"); perror("writeFileInQueue");
free(content) free(content);
return; return;
} }
} }
taglia_update(taglia, q, 0); taglia_update(taglia, q, 0);
free(content) free(content);
return; return;
} }
@ -388,62 +394,62 @@ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *tagl
if(append) { if(append) {
if(appendFileInQueue(q, filepath, content, size, fd_c) == -1) { if(appendFileInQueue(q, filepath, content, size, fd_c) == -1) {
perror("appendFileInQueue"); perror("appendFileInQueue");
free(content) free(content);
return; return;
} }
} else { } else {
if(writeFileInQueue(q, filepath, content, size, fd_c) == -1) { if(writeFileInQueue(q, filepath, content, size, fd_c) == -1) {
perror("writeFileInQueue"); perror("writeFileInQueue");
free(content) free(content);
return; return;
} }
} }
taglia_write(taglia, mlog); taglia_write(taglia, tmp_buf);
taglia_update(taglia, q, 0); taglia_update(taglia, q, 0);
free(content) free(content);
return; return;
} }
void lockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waitingT **waiting) { void lockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting) {
return; return;
} }
void unlockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waitingT **waiting) { void unlockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting) {
return; return;
} }
void closeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waitingT **waiting) { void closeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting) {
return; return;
} }
void removeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waitingT **waiting) { void removeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting) {
return; return;
} }
int sendFile(fileT *f, long fd_c, taglia_t *taglia) { int sendFile(fileT *f, long fd_c, taglia_t *taglia) {
return; return 0;
} }
int addWaiting(waitingT **waiting, char *file, int fd) { int addWaiting(waiting_t **waiting, char *file, int fd) {
return; return 0;
} }
int removeFirstWaiting(waitingT **waiting, char *file) { int removeFirstWaiting(waiting_t **waiting, char *file) {
return; return 0;
} }
void clearWaiting(waitingT **waiting) { void clearWaiting(waiting_t **waiting) {
return; return;
} }

View File

@ -1,7 +1,7 @@
#include <fileQueue.h> #include <fileQueue.h>
#include <util.h> #include <util.h>
#define NAMELEN 256 #define MAXNAMELEN 256
// creazione di un fileT // creazione di un fileT
fileT* createFileT(char *f, int O_LOCK, int client, int open){ fileT* createFileT(char *f, int O_LOCK, int client, int open){
@ -12,30 +12,31 @@ fileT* createFileT(char *f, int O_LOCK, int client, int open){
fileT *file = malloc(sizeof(fileT)); fileT *file = malloc(sizeof(fileT));
if (f == NULL) { if (file == NULL) {
perror("Malloc createFileT"); perror("Malloc createFileT");
return NULL; return NULL;
} }
f->O_LOCK = (O_LOCK == 0)? 0 : 1; file->O_LOCK = (O_LOCK == 0)? 0 : 1;
f->owner = client; file->owner = client;
f->open = (open == 0)? 0 : 1; file->open = (open == 0)? 0 : 1;
f->size = 0; file->size = 0;
f->valid = 0; file->valid = 0;
if ((f->filepath = malloc(sizeof(char)*NAMELEN)) == NULL) { if ((file->filepath = malloc(sizeof(char)*MAXNAMELEN)) == NULL) {
perror("Malloc filepath"); perror("Malloc filepath");
destroyFile(f); destroyFile(file);
return NULL; return NULL;
} }
strncpy(f->filepath, f, NAMELEN); strncpy(file->filepath, f, MAXNAMELEN);
if ((f->data = malloc(1)) == NULL) { // così semplicemente facciamo realloc // in seguito semplicemente facciamo realloc
if ((file->data = malloc(1)) == NULL) {
perror("Malloc content"); perror("Malloc content");
return NULL; return NULL;
} }
return f; return file;
} }
// append su fileT // append su fileT
@ -53,6 +54,7 @@ int writeFileT(fileT *f, void *data, size_t size) {
memcpy((char*) f->data + f->valid, data, size); memcpy((char*) f->data + f->valid, data, size);
f->valid = f->valid + size; f->valid = f->valid + size;
f->size = (f->valid>f->size)?f->valid:f->size; f->size = (f->valid>f->size)?f->valid:f->size;
return 0;
} }
// destroy fileT // destroy fileT
@ -126,15 +128,15 @@ int enqueue(queueT *q, fileT* data) {
newNode->data = data; newNode->data = data;
newNode->next = NULL; newNode->next = NULL;
nodeT *tmp = queue->head; nodeT *tmp = q->head;
if (q->head == NULL) if (q->head == NULL)
q->head = newNode; q->head = newNode;
else { else {
while (tmp->next) while (tmp->next)
tmp = temp->next; tmp = tmp->next;
temp->next = newNode; tmp->next = newNode;
} }
q->tail = newNode; q->tail = newNode;
@ -228,14 +230,8 @@ fileT ** dequeueN(queueT *q, char *filepath, size_t s) {
goto _end_dequeueN; goto _end_dequeueN;
} }
if ((tmp->data)->open == 0 || ((tmp->data)->O_LOCK && (tmp->data)->owner != owner)) {
// if file non è aperto o la lock non è del owner
errno = EPERM;
goto _end_dequeueN;
}
fileT **returnList = NULL; // lista dei file rimossi fileT **returnList = NULL; // lista dei file rimossi
nodeT *tmp = NULL; tmp = NULL;
returnList = calloc(1, sizeof(fileT*)); returnList = calloc(1, sizeof(fileT*));
returnList[0] = NULL; returnList[0] = NULL;

View File

@ -2,6 +2,10 @@
#ifndef _FILE_QUEUE #ifndef _FILE_QUEUE
#define _FILE_QUEUE #define _FILE_QUEUE
#include <stdio.h>
#include <pthread.h>
#include <conn.h>
// struttura dati per gestire i file in memoria principale // struttura dati per gestire i file in memoria principale
typedef struct { typedef struct {

View File

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

View File

@ -10,6 +10,11 @@
#include <fileQueue.h> #include <fileQueue.h>
#include <apiFile.h> #include <apiFile.h>
#include <taglialegna.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 // funzione eseguita dal Worker thread del pool
void threadF(void *arg) { void threadF(void *arg) {
@ -18,13 +23,20 @@ void threadF(void *arg) {
return; return;
} }
// TODO add necessary variables from main threadT *argl = (threadT *) arg;
long* connfd = ; 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_set set, tmpset;
FD_ZERO(&set); FD_ZERO(&set);
FD_SET(*connfd, &set); FD_SET(connfd, &set);
while(*quit == 0) { while(*quit == 0) {
tmpset=set; tmpset=set;
@ -47,58 +59,92 @@ 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
if ((n=readn(connfd, &str.len, sizeof(long))) == -1) { if ((n=readn(connfd, &str.len, sizeof(long))) == -1) {
perror("read1"); perror("read1");
break; goto _cleanup;
} }
if (n==0) if (n==0)
break; goto _cleanup;;
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("calloc");
fprintf(stderr, "Calloc.\n"); fprintf(stderr, "Calloc.\n");
break; goto _cleanup;
} }
// leggo il messaggio
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("read2");
free(str.str); 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 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 // comunico al manager che ho chiuso la connessione
// ... if (writen(request_pipe, &close, sizeof(int)) == -1) {
perror("writen");
goto _cleanup;
}
// log chiusura connessione // 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; goto _cleanup;
} }
// eseguo quello che mi chiede il client di fare // eseguo quello che mi chiede il client di fare
if (parser(str.len, str.str) == -1) { if (parser(str.len, str.str, q, connfd, taglia, lock, waiting) == -1) {
// str.str non è più valido perchè parser fa free goto _cleanup;
} }
// str.str non è più valido perchè parser fa free
// comunico al manager che è stata servita la richiesta // comunico al manager che è stata servita la richiesta
if (writen(request_pipe, &connfd, sizeof(int)) == -1) {
perror("writen");
}
// log // 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: _cleanup:
if(arg) // nothing to do because no memory needs to be freed
free(arg); return;
close(connfd);
} }
int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, pthread_mutex_t *lock, waitingT **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 || !logFileT || !waiting) { if(len<0 || !command || !queue || !taglia || !waiting) {
errno = EINVAL; errno = EINVAL;
return -1; 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 *token = NULL;
char *token2 = NULL; char *token2 = NULL;
char *token3 = 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); int arg = (int) strtol(token3, NULL, 10);
openFile(token2, arg, queue, fd_c, logFileT); openFile(token2, arg, queue, fd_c, taglia);
goto _parser_end; goto _parser_end;
} }
@ -124,7 +170,7 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
if(!token2) if(!token2)
goto _parser_cleanup; goto _parser_cleanup;
readFile(token2, queue, fd_c, logFileT); readFile(token2, queue, fd_c, taglia);
goto _parser_end; goto _parser_end;
} }
@ -132,7 +178,7 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
if(!token2) if(!token2)
goto _parser_cleanup; goto _parser_cleanup;
readNFiles(token2, queue, fd_c, logFileT); readNFiles(token2, queue, fd_c, taglia);
goto _parser_end; 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; goto _parser_cleanup;
size_t sz = (size_t) strtol(token3, NULL, 10); 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; 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; goto _parser_cleanup;
size_t sz = (size_t) strtol(token3, NULL, 10); 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; goto _parser_end;
} }
@ -158,7 +204,7 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
if(!token2) if(!token2)
goto _parser_cleanup; goto _parser_cleanup;
lockFile(token2, queue, fd_c, logFileT, lock, waiting); lockFile(token2, queue, fd_c, taglia, lock, waiting);
goto _parser_end; goto _parser_end;
} }
@ -166,7 +212,7 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
if(!token2) if(!token2)
goto _parser_cleanup; goto _parser_cleanup;
unlockFile(token2, queue, fd_c, logFileT, lock, waiting); unlockFile(token2, queue, fd_c, taglia, lock, waiting);
goto _parser_end; goto _parser_end;
} }
@ -174,7 +220,7 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
if(!token2) if(!token2)
goto _parser_cleanup; goto _parser_cleanup;
closeFile(token2, queue, fd_c, logFileT, lock, waiting); closeFile(token2, queue, fd_c, taglia, lock, waiting);
goto _parser_end; goto _parser_end;
} }
@ -182,7 +228,7 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
if(!token2) if(!token2)
goto _parser_cleanup; goto _parser_cleanup;
removeFile(token2, queue, fd_c, logFileT, lock, waiting); removeFile(token2, queue, fd_c, taglia, lock, waiting);
goto _parser_end; goto _parser_end;
} }
// se arrivo qui non ho riconosciuto il comando // se arrivo qui non ho riconosciuto il comando

View File

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