function 'request' for handling readN serverside

This commit is contained in:
elvis
2022-05-17 18:41:50 +02:00
parent cbef01f7be
commit a1e7c2e995
3 changed files with 89 additions and 23 deletions

View File

@ -417,32 +417,33 @@ void readNFiles(int num, queueT *q, long fd_c, taglia_t *taglia) {
int oldNum = num; int oldNum = num;
int64_t ntosend = (num<=0 || num>getLen(q))? getLen(q) : num; int64_t ntosend = (num<=0 || num>getLen(q))? getLen(q) : num;
fileT **toSend = calloc(ntosend, sizeof(fileT *)); fileT **toSend = NULL;
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readNFile (n = %d) e' terminata con successo. File inviati:\n", n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readNFile (n = %d) e' terminata con successo. File inviati:\n",
fd_c, fd_c,
num); num);
/* extract n files, we dont check if the client can actually modify /* extract n files, but dont actually dequeue them */
* the files since we add them back immediatly */ toSend = request(q, (int) ntosend);
size_t tot = 0; if(toSend == NULL) {
for(int i=0;i<ntosend;++i) {
toSend[i] = dequeue(q);
if(!toSend[i]) {
n = 0; n = 0;
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readNFiles (n = %d) e' terminato con errore del server\n", n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readNFiles (n = %d) e' terminato con errore del server\n",
fd_c, fd_c,
oldNum); oldNum);
perror("readNFiles: dequeue"); perror("readNFiles: request");
serror(MESE, fd_c, taglia, tmp_buf); serror(MESE, fd_c, taglia, tmp_buf);
return; return;
} }
int tmp = enqueue(q, toSend[i]);
if(tmp<0) { size_t tot = 0;
for(int i=0;i<ntosend;++i) {
if(toSend[i] == NULL) {
n = 0; n = 0;
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readNFiles (n = %d) e' terminato con errore del server\n", n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readNFiles (n = %d) e' terminato con errore del server (file estratti [%d] inferiori a file richiesti [%ld])\n",
fd_c, fd_c,
oldNum); oldNum,
perror("readNFiles: enqueue"); i,
ntosend);
perror("readNFiles: request");
serror(MESE, fd_c, taglia, tmp_buf); serror(MESE, fd_c, taglia, tmp_buf);
return; return;
} }

View File

@ -198,7 +198,6 @@ _end_dequeue:
return NULL; return NULL;
} }
/* dequeue until we have s free space and expand the file by s */ /* dequeue until we have s free space and expand the file by s */
fileT ** dequeueN(queueT *q, char *filepath, size_t s) { fileT ** dequeueN(queueT *q, char *filepath, size_t s) {
if(!q) { if(!q) {
@ -211,6 +210,8 @@ fileT ** dequeueN(queueT *q, char *filepath, size_t s) {
LOCK_RETURN(&q->m, NULL); /* begin me */ LOCK_RETURN(&q->m, NULL); /* begin me */
fileT **returnList = NULL; /* list of removed files */
if (q->head == NULL || q->len == 0) { /* empty queue */ if (q->head == NULL || q->len == 0) { /* empty queue */
errno = ENOENT; errno = ENOENT;
goto _end_dequeueN; goto _end_dequeueN;
@ -235,10 +236,13 @@ fileT ** dequeueN(queueT *q, char *filepath, size_t s) {
goto _end_dequeueN; goto _end_dequeueN;
} }
fileT **returnList = NULL; /* list of removed files */
tmp = NULL; tmp = NULL;
returnList = calloc(1, sizeof(fileT*)); returnList = calloc(1, sizeof(*returnList));
if(!returnList) {
perror("dequeueN: calloc");
goto _end_dequeueN;
}
returnList[0] = NULL; returnList[0] = NULL;
int purged = 0; int purged = 0;
@ -272,6 +276,8 @@ fileT ** dequeueN(queueT *q, char *filepath, size_t s) {
_end_dequeueN: _end_dequeueN:
UNLOCK_RETURN(&q->m, NULL); UNLOCK_RETURN(&q->m, NULL);
if(returnList)
free(returnList);
return NULL; return NULL;
} }
@ -765,6 +771,57 @@ _end_find_in_queue:
return NULL; return NULL;
} }
/* return n files */
fileT ** request(queueT *q, int n) {
if(!q) {
errno = EINVAL;
return NULL;
}
if(n<=0) {
return NULL;
}
LOCK_RETURN(&q->m, NULL); /* begin me */
fileT **returnList = NULL; /* list of requested files */
if (q->head == NULL || q->len == 0) { /* empty queue */
errno = ENOENT;
goto _end_request;
}
if(q->size < n) {
errno = EINVAL;
goto _end_request;
}
returnList = calloc(n+1, sizeof(*returnList));
if(!returnList) {
perror("dequeueN: calloc");
goto _end_request;
}
returnList[n] = NULL;
nodeT *tmp = q->head;
for(int i=0; i<n; ++i) {
if(tmp == NULL) {
errno = ENOENT;
goto _end_request;
}
returnList[i] = tmp->data;
tmp = tmp->next;
}
UNLOCK_RETURN(&q->m, NULL); /* end me */
return returnList;
_end_request:
UNLOCK_RETURN(&q->m, NULL);
if(returnList)
free(returnList);
return NULL;
}
/* search for file and return result of search */ /* search for file and return result of search */
int searchFile(queueT *q, char *filepath) { int searchFile(queueT *q, char *filepath) {
if(!q || !filepath) { if(!q || !filepath) {

View File

@ -67,8 +67,7 @@ void destroyFile(fileT *f);
/** /**
* Alloca ed inizializza una coda di fileT. Deve essere chiamata da un solo * Alloca ed inizializza una coda di fileT.
* thread.
* @param maxLen lunghezza massima della coda * @param maxLen lunghezza massima della coda
* @param maxSize dimensione massima della coda in bytes * @param maxSize dimensione massima della coda in bytes
* *
@ -215,6 +214,15 @@ int removeFileFromQueue(queueT *q, char *filepath, int owner);
*/ */
fileT* find(queueT *q, char *filepath); fileT* find(queueT *q, char *filepath);
/**
* Estrae n fileT dalla coda senza eliminarli
* @param q puntatore alla coda
* @param n numero di file da estrarre
*
* @return puntatore i file estratti (NULL terminated), NULL se errore
*/
fileT ** request(queueT *q, int n);
/** /**
* Cerca il fileT nella coda e ritorna il siultato dell'operazione * Cerca il fileT nella coda e ritorna il siultato dell'operazione
* @param q puntatore alla coda sulla quale cercare il fileT * @param q puntatore alla coda sulla quale cercare il fileT