2022-03-09 19:24:49 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <sys/select.h>
|
|
|
|
|
|
|
|
|
|
#include <conn.h>
|
|
|
|
|
#include <message.h>
|
2022-03-12 10:05:58 +01:00
|
|
|
#include <serverStatus.h>
|
2022-03-09 19:24:49 +01:00
|
|
|
|
|
|
|
|
// converte tutti i carattere minuscoli in maiuscoli
|
|
|
|
|
static void toup(char *str) {
|
|
|
|
|
char *p = str;
|
2022-03-12 10:05:58 +01:00
|
|
|
while(*p != '\0') {
|
|
|
|
|
*p = (islower(*p)?toupper(*p):*p);
|
2022-03-09 19:24:49 +01:00
|
|
|
++p;
|
2022-03-12 10:05:58 +01:00
|
|
|
}
|
2022-03-09 19:24:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// funzione eseguita dal Worker thread del pool
|
|
|
|
|
// gestisce una intera connessione di un client
|
|
|
|
|
//
|
|
|
|
|
void threadF(void *arg) {
|
|
|
|
|
assert(arg);
|
2022-03-12 10:05:58 +01:00
|
|
|
|
|
|
|
|
long* connfd = (long*)arg[0];
|
|
|
|
|
serverStatus* status = (serverStatus*)(arg[1]);
|
|
|
|
|
|
2022-03-09 19:24:49 +01:00
|
|
|
fd_set set, tmpset;
|
|
|
|
|
FD_ZERO(&set);
|
2022-03-12 10:05:58 +01:00
|
|
|
FD_SET(*connfd, &set);
|
|
|
|
|
|
2022-03-09 19:24:49 +01:00
|
|
|
do {
|
|
|
|
|
tmpset=set;
|
|
|
|
|
int r;
|
|
|
|
|
// ogni tanto controllo se devo terminare
|
|
|
|
|
struct timeval timeout={0, 100000}; // 100 milliseconds
|
|
|
|
|
if ((r=select(connfd+1, &tmpset, NULL, NULL, &timeout)) < 0) {
|
2022-03-12 10:05:58 +01:00
|
|
|
perror("Select");
|
2022-03-09 19:24:49 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (r==0) {
|
2022-03-12 10:05:58 +01:00
|
|
|
if ((status->exiting) != 0)
|
|
|
|
|
break;
|
2022-03-09 19:24:49 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
2022-03-12 10:05:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// comunicate with the client
|
2022-03-09 19:24:49 +01:00
|
|
|
msg_t str;
|
2022-03-12 10:05:58 +01:00
|
|
|
long n;
|
|
|
|
|
if ((n=readn(connfd, &str.len, sizeof(long))) == -1) {
|
2022-03-09 19:24:49 +01:00
|
|
|
perror("read1");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-03-12 10:05:58 +01:00
|
|
|
|
|
|
|
|
if (n==0)
|
|
|
|
|
break;
|
|
|
|
|
str.str = calloc(str.len, sizeof(char));
|
2022-03-09 19:24:49 +01:00
|
|
|
if (!str.str) {
|
|
|
|
|
perror("calloc");
|
|
|
|
|
fprintf(stderr, "Memoria esaurita....\n");
|
|
|
|
|
break;
|
2022-03-12 10:05:58 +01:00
|
|
|
}
|
2022-03-09 19:24:49 +01:00
|
|
|
if ((n=readn(connfd, str.str, str.len * sizeof(char))) == -1) {
|
|
|
|
|
perror("read2");
|
|
|
|
|
free(str.str);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toup(str.str);
|
|
|
|
|
|
|
|
|
|
if ((n=writen(connfd, &str.len, sizeof(int))) == -1) {
|
|
|
|
|
perror("write1");
|
|
|
|
|
free(str.str);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if ((n=writen(connfd, str.str, str.len*sizeof(char))) == -1) {
|
|
|
|
|
perror("write2");
|
|
|
|
|
free(str.str);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
free(str.str);
|
2022-03-12 10:05:58 +01:00
|
|
|
} while(status->exiting == 0);
|
|
|
|
|
close(connfd);
|
2022-03-09 19:24:49 +01:00
|
|
|
}
|