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

16
lib/utils/serverStatus.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#ifndef _SERVER_STATUS
#define _SERVER_STATUS
typedef struct {
pthread_mutex_t *mtx;
pthread_cond_t *cond;
long max_space_occupied;
long cur_space_occupied;
long max_files;
long cur_files;
char exiting;
} serverStatus;
#endif /* _SERVER_STATUS */

View File

@ -139,6 +139,7 @@ static inline int isNumber(const char* s, long* n) {
fprintf(stderr, "ERRORE FATALE broadcast\n"); \ fprintf(stderr, "ERRORE FATALE broadcast\n"); \
pthread_exit((void*)EXIT_FAILURE); \ pthread_exit((void*)EXIT_FAILURE); \
} }
static inline int TRYLOCK(pthread_mutex_t* l) { static inline int TRYLOCK(pthread_mutex_t* l) {
int r=0; int r=0;
if ((r=pthread_mutex_trylock(l))!=0 && r!=EBUSY) { if ((r=pthread_mutex_trylock(l))!=0 && r!=EBUSY) {

View File

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

View File

@ -6,6 +6,7 @@
#include <conn.h> #include <conn.h>
#include <message.h> #include <message.h>
#include <serverStatus.h>
// converte tutti i carattere minuscoli in maiuscoli // converte tutti i carattere minuscoli in maiuscoli
static void toup(char *str) { static void toup(char *str) {
@ -21,13 +22,13 @@ static void toup(char *str) {
// //
void threadF(void *arg) { void threadF(void *arg) {
assert(arg); assert(arg);
long* args = (long*)arg;
long connfd = args[0]; long* connfd = (long*)arg[0];
long* termina = (long*)(args[1]); serverStatus* status = (serverStatus*)(arg[1]);
free(arg);
fd_set set, tmpset; fd_set set, tmpset;
FD_ZERO(&set); FD_ZERO(&set);
FD_SET(connfd, &set); FD_SET(*connfd, &set);
do { do {
tmpset=set; tmpset=set;
@ -35,22 +36,27 @@ void threadF(void *arg) {
// ogni tanto controllo se devo terminare // ogni tanto controllo se devo terminare
struct timeval timeout={0, 100000}; // 100 milliseconds struct timeval timeout={0, 100000}; // 100 milliseconds
if ((r=select(connfd+1, &tmpset, NULL, NULL, &timeout)) < 0) { if ((r=select(connfd+1, &tmpset, NULL, NULL, &timeout)) < 0) {
perror("select"); perror("Select");
break; break;
} }
if (r==0) { if (r==0) {
if (*termina) break; if ((status->exiting) != 0)
break;
continue; continue;
} }
// comunicate with the client
msg_t str; msg_t str;
int n; long n;
if ((n=readn(connfd, &str.len, sizeof(int))) == -1) { if ((n=readn(connfd, &str.len, sizeof(long))) == -1) {
perror("read1"); perror("read1");
break; break;
} }
if (n==0) break; if (n==0)
str.str = calloc((str.len), sizeof(char)); break;
str.str = calloc(str.len, sizeof(char));
if (!str.str) { if (!str.str) {
perror("calloc"); perror("calloc");
fprintf(stderr, "Memoria esaurita....\n"); fprintf(stderr, "Memoria esaurita....\n");
@ -75,6 +81,6 @@ void threadF(void *arg) {
break; break;
} }
free(str.str); free(str.str);
} while(*termina == 0); } while(status->exiting == 0);
close(connfd); close(connfd);
} }