Added server status

This commit is contained in:
elvis
2022-03-12 10:05:58 +01:00
parent d99a19228b
commit b4ca73b8d6
4 changed files with 129 additions and 76 deletions

View File

@ -13,6 +13,7 @@
#include <util.h>
#include <serverWorker.h>
#include <ini.h>
#include <serverStatus.h>
/**
@ -27,6 +28,7 @@ typedef struct {
} sigHandler_t;
// funzione eseguita dal signal handler thread
static void *sigHandler(void *arg) {
sigset_t *set = ((sigHandler_t*)arg)->set;
@ -78,7 +80,7 @@ int main(int argc, char *argv[]) {
// TODO read config file
checkargs(argc, argv);
ini_t *config = ini_load(argv[1]);
int threadsInPool = (int) strtol(ini_get(config, "threadpool", "quantity"), null, 10);
int threadsInPool = (int) strtol(ini_get(config, "threadpool", "quantity"), NULL, 10);
ini_free(config);
sigset_t mask;
@ -166,8 +168,28 @@ int main(int argc, char *argv[]) {
// tengo traccia del file descriptor con id piu' grande
int fdmax = (listenfd > signal_pipe[0]) ? listenfd : signal_pipe[0];
volatile long termina=0;
while(!termina) {
serverStatus* status = malloc(sizeof(serverStatus));
if (!status) {
// TODO logging utility
perror("ERROR FATAL malloc");
goto _cleanup;
}
if (pthread_mutex_init(status->mtx, NULL) != 0) {
fprintf(stderr, "ERRORE: creazione mutex");
goto _cleanup;
}
if (pthread_cond_init(status->cond, NULL) != 0) {
fprintf(stderr, "ERRORE: creazione condition variable");
goto _cleanup;
}
status->max_space_occupied = 100000000; // in bytes
status->cur_space_occupied = 0;
status->max_files = 100;
status->cur_files = 0;
status->exiting = 0;
while(!status->exiting) {
// copio il set nella variabile temporanea per la select
tmpset = set;
if (select(fdmax+1, &tmpset, NULL, NULL, NULL) == -1) {
@ -177,24 +199,30 @@ int main(int argc, char *argv[]) {
// cerchiamo di capire da quale fd abbiamo ricevuto una richiesta
for(int i=0; i <= fdmax; ++i) {
if (FD_ISSET(i, &tmpset)) {
long connfd;
long* connfd = malloc(sizeof(long));
if (!connfd) {
// TODO logging utility
perror("ERROR FATAL malloc");
goto _cleanup;
}
if (i == listenfd) { // e' una nuova richiesta di connessione
if ((connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL)) == -1) {
if ((*connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL)) == -1) {
// TODO logging utility
perror("accept");
goto _cleanup;
}
long* args = malloc(2*sizeof(long));
void** args = NULL;
*args = malloc(2*sizeof(void*));
if (!args) {
// TODO logging utility
perror("ERROR FATAL malloc");
goto _cleanup;
}
args[0] = connfd;
args[1] = (long)&termina;
args[1] = status;
int r = addToThreadPool(pool, threadF, (void*)args);
int r = addToThreadPool(pool, threadF, args);
if (r == 0)
continue; // aggiunto con successo
if (r < 0)// errore interno
@ -204,17 +232,19 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "ERROR SERVER TOO BUSY\n");
// TODO logging utility
free(args);
close(connfd);
close(*connfd);
free(connfd);
continue;
}
if (i == signal_pipe[0]) {
// ricevuto un segnale, esco ed inizio il protocollo di terminazione
termina = 1;
status->exiting = 1;
break;
}
}
}
}
free(status);
destroyThreadPool(pool, 0); // notifico che i thread dovranno uscire

View File

@ -6,14 +6,15 @@
#include <conn.h>
#include <message.h>
#include <serverStatus.h>
// converte tutti i carattere minuscoli in maiuscoli
static void toup(char *str) {
char *p = str;
while(*p != '\0') {
*p = (islower(*p)?toupper(*p):*p);
while(*p != '\0') {
*p = (islower(*p)?toupper(*p):*p);
++p;
}
}
}
// funzione eseguita dal Worker thread del pool
@ -21,41 +22,46 @@ static void toup(char *str) {
//
void threadF(void *arg) {
assert(arg);
long* args = (long*)arg;
long connfd = args[0];
long* termina = (long*)(args[1]);
free(arg);
long* connfd = (long*)arg[0];
serverStatus* status = (serverStatus*)(arg[1]);
fd_set set, tmpset;
FD_ZERO(&set);
FD_SET(connfd, &set);
FD_SET(*connfd, &set);
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) {
perror("select");
perror("Select");
break;
}
if (r==0) {
if (*termina) break;
if ((status->exiting) != 0)
break;
continue;
}
// comunicate with the client
msg_t str;
int n;
if ((n=readn(connfd, &str.len, sizeof(int))) == -1) {
long n;
if ((n=readn(connfd, &str.len, sizeof(long))) == -1) {
perror("read1");
break;
}
if (n==0) break;
str.str = calloc((str.len), sizeof(char));
if (n==0)
break;
str.str = calloc(str.len, sizeof(char));
if (!str.str) {
perror("calloc");
fprintf(stderr, "Memoria esaurita....\n");
break;
}
}
if ((n=readn(connfd, str.str, str.len * sizeof(char))) == -1) {
perror("read2");
free(str.str);
@ -75,6 +81,6 @@ void threadF(void *arg) {
break;
}
free(str.str);
} while(*termina == 0);
close(connfd);
} while(status->exiting == 0);
close(connfd);
}