Added log utility and improved server

This commit is contained in:
elvis
2022-03-22 19:35:06 +01:00
parent b11640aff0
commit c1977248d7
7 changed files with 111 additions and 23 deletions

View File

@ -15,6 +15,7 @@
#include <ini.h>
#include <serverUtil.h>
#include <fileQueue.h>
#include <taglialegna.h>
/**
@ -61,6 +62,9 @@ int main(int argc, char *argv[]) {
int pendingSize; CONFGETINT(pendingSize, config, "threadpool", "pending", NULL, 10);
int maxFiles; CONFGETINT(maxFiles, config, "files", "MaxFiles", NULL, 10);
int maxSize; CONFGETINT(maxSize, config, "files", "MaxSize", NULL, 10);
char *logFile, *buf; CONFGETSTR(logFile, config, "log", "logFile", buf);
char *socketName, *buf; CONFGETSTR(socketName, config, "socket", "name", buf);
int maxBacklog; CONFGETINT(maxBacklog, config, "socket", "backlog", NULL, 10);
ini_free(config);
sigset_t mask;
@ -84,16 +88,26 @@ int main(int argc, char *argv[]) {
// remove("mysock"); maybe necessary???
printf("File Server ready.");
fflush(stdout);
// creo la struttura per il log
taglia_t *taglia = taglia_init(logFile);
free(logFile); // free del nome del file
if(taglia==NULL) {
perror("taglia_init");
goto _cleanup;
}
// pipes di comunicazione fra thread main e sigHandler thread/threadF threads
int signal_pipe[2];
int request_pipe[2];
if (pipe(signal_pipe) == -1) {
perror("pipe");
goto _cleanup;
}
// todo logging + statistiche
if (pipe(request_pipe) == -1) {
perror("pipe");
goto _cleanup;
}
@ -105,30 +119,56 @@ int main(int argc, char *argv[]) {
goto _cleanup;
}
// scrivo sul log
char buf[2048];
int n;
n = snprintf(buf, sizeof(buf), "Creato signal thread con id: %l\n", (long) sighandler_thread);
if( n<0 || m<n ) {
perror("snprintf");
goto _cleanup;
}
if( taglia_log(taglia, buf) < 0)
goto _cleanup;
// creo socket
int listenfd;
if ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
goto _cleanup;
}
// thread per i segnali
struct sockaddr_un serv_addr;
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sun_family = AF_UNIX;
strncpy(serv_addr.sun_path, SOCKNAME, strlen(SOCKNAME)+1);
strncpy(serv_addr.sun_path, socketName, strlen(socketName)+1);
if (bind(listenfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) == -1) {
perror("bind");
goto _cleanup;
}
if (listen(listenfd, MAXBACKLOG) == -1) {
if (listen(listenfd, maxBacklog) == -1) {
perror("listen");
goto _cleanup;
}
// scrivo sul log
char buf[2048];
int n;
n = snprintf(buf, sizeof(buf), "Creato socket: %s\n", socketName);
if( n<0 || m<n ) {
perror("snprintf");
goto _cleanup;
}
if( taglia_log(taglia, buf) < 0)
goto _cleanup;
// creo la queue
queueT *queue = createQueue(maxFiles, maxSize);
// creo la lista dei client in attesa a una lock
waiting_t *waiting;
threadpool_t *pool = NULL;
pool = createThreadPool(threadsInPool, pendingSize);
@ -137,15 +177,43 @@ int main(int argc, char *argv[]) {
goto _cleanup;
}
// scrivo sul log
char buf[2048];
int n;
n = snprintf(buf, sizeof(buf), "Creato threadpool di dimensione %d e lending size %d\n", threadsInPool, pendingSize);
if( n<0 || m<n ) {
perror("snprintf");
goto _cleanup;
}
if( taglia_log(taglia, buf) < 0)
goto _cleanup;
fd_set set, tmpset;
FD_ZERO(&set);
FD_ZERO(&tmpset);
FD_SET(listenfd, &set); // aggiungo il listener fd al master set
FD_SET(signal_pipe[0], &set); // aggiungo il descrittore di lettura della signal_pipe
FD_SET(request_pipe[0], &set); // aggiungo il descrittore di lettura della request_pipe
// tengo traccia del file descriptor con id piu' grande
int fdmax = (listenfd > signal_pipe[0]) ? listenfd : signal_pipe[0];
fdmax = (fdmax > request_pipe[0]) ? fdmax : request_pipe[0];
// scrivo sul log
char buf[2048];
int n;
n = snprintf(buf, sizeof(buf), "File Server ready.\n\tMaxFiles: %d\n\tMaxSize: %d\n", maxFiles, maxSize);
if( n<0 || m<n ) {
perror("snprintf");
goto _cleanup;
}
if( taglia_log(taglia, buf) < 0)
goto _cleanup;
printf("File Server ready.");
fflush(stdout);
@ -165,13 +233,15 @@ int main(int argc, char *argv[]) {
if (FD_ISSET(i, &tmpset)) {
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(stopNewConnections) { // non vogliamo nuove connessioni
// TODO log
// scrivo sul log
if( taglia_log(taglia, "Nuova connessione rifiutata, server in terminazione\n") < 0)
goto _cleanup;
FD_CLR(i, &set);
close(i);
continue;
@ -262,7 +332,7 @@ int main(int argc, char *argv[]) {
// aspetto la terminazione de signal handler thread
pthread_join(sighandler_thread, NULL);
unlink(SOCKNAME);
unlink(socketName);
printf("File Storage Server terminato.\n");
fflush(stdout);
@ -270,7 +340,7 @@ int main(int argc, char *argv[]) {
_cleanup:
unlink(SOCKNAME);
unlink(socketName);
return -1;
}