Added server status
This commit is contained in:
16
lib/utils/serverStatus.h
Normal file
16
lib/utils/serverStatus.h
Normal 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 */
|
||||
@ -139,6 +139,7 @@ static inline int isNumber(const char* s, long* n) {
|
||||
fprintf(stderr, "ERRORE FATALE broadcast\n"); \
|
||||
pthread_exit((void*)EXIT_FAILURE); \
|
||||
}
|
||||
|
||||
static inline int TRYLOCK(pthread_mutex_t* l) {
|
||||
int r=0;
|
||||
if ((r=pthread_mutex_trylock(l))!=0 && r!=EBUSY) {
|
||||
|
||||
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
|
||||
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
|
||||
#include <conn.h>
|
||||
#include <message.h>
|
||||
#include <serverStatus.h>
|
||||
|
||||
// converte tutti i carattere minuscoli in maiuscoli
|
||||
static void toup(char *str) {
|
||||
@ -21,13 +22,13 @@ 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;
|
||||
@ -35,22 +36,27 @@ void threadF(void *arg) {
|
||||
// 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");
|
||||
@ -75,6 +81,6 @@ void threadF(void *arg) {
|
||||
break;
|
||||
}
|
||||
free(str.str);
|
||||
} while(*termina == 0);
|
||||
} while(status->exiting == 0);
|
||||
close(connfd);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user