Added server status
This commit is contained in:
50
src/server.c
50
src/server.c
@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user