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;
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",
fd_c,
num);
/* extract n files, we dont check if the client can actually modify
* the files since we add them back immediatly */
/* extract n files, but dont actually dequeue them */
toSend = request(q, (int) ntosend);
if(toSend == NULL) {
n = 0;
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readNFiles (n = %d) e' terminato con errore del server\n",
fd_c,
oldNum);
perror("readNFiles: request");
serror(MESE, fd_c, taglia, tmp_buf);
return;
}
size_t tot = 0;
for(int i=0;i<ntosend;++i) {
toSend[i] = dequeue(q);
if(!toSend[i]) {
if(toSend[i] == NULL) {
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,
oldNum);
perror("readNFiles: dequeue");
serror(MESE, fd_c, taglia, tmp_buf);
return;
}
int tmp = enqueue(q, toSend[i]);
if(tmp<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",
fd_c,
oldNum);
perror("readNFiles: enqueue");
oldNum,
i,
ntosend);
perror("readNFiles: request");
serror(MESE, fd_c, taglia, tmp_buf);
return;
}