client now disconnects

This commit is contained in:
elvis
2022-05-01 00:10:28 +02:00
parent 85231d073f
commit e095f4306f
4 changed files with 55 additions and 16 deletions

View File

@ -100,13 +100,39 @@ print-%: ; @echo $* = $($*)
# -TESTS------------------------------------------------------------------------ # -TESTS------------------------------------------------------------------------
test1: all test1: all
@echo "[threadpool]\n\nquantity = 1\npending = 10\n\n[files]\n\nMaxFiles = 10000\nMaxSize = 128000000\n\n[log]\n\nlogFile = ./logs/l.log\n\n[socket]\n\nname = ./socket\nbacklog = 100\n" > build/confix.txt @echo "[threadpool]" > $(BUILD_DIR)/config.txt
valgrind --leak-check=full --track-origins=yes $(BUILD_DIR)/server & @echo "quantity = 1" >> $(BUILD_DIR)/config.txt
@echo "pending = 10" >> $(BUILD_DIR)/config.txt
@echo "" >> $(BUILD_DIR)/config.txt
@echo "[files]" >> $(BUILD_DIR)/config.txt
@echo "MaxSize = 128000000" >> $(BUILD_DIR)/config.txt
@echo "MaxFiles = 10000" >> $(BUILD_DIR)/config.txt
@echo "" >> $(BUILD_DIR)/config.txt
@echo "[log]" >> $(BUILD_DIR)/config.txt
@echo "logFile = ./logs/l.log" >> $(BUILD_DIR)/config.txt
@echo "" >> $(BUILD_DIR)/config.txt
@echo "[socket]" >> $(BUILD_DIR)/config.txt
@echo "name = ./socket" >> $(BUILD_DIR)/config.txt
@echo "backlog = 100" >> $(BUILD_DIR)/config.txt
valgrind --leak-check=full --track-origins=yes $(BUILD_DIR)/server $(BUILD_DIR)/config.txt &
bash scripts/test1.sh bash scripts/test1.sh
pkill -1 memcheck-amd64 pkill --signal SIGHUP memcheck
test2: all test2: all
@echo "[threadpool]\n\nquantity = 4\npending = 100\n\n[files]\n\nMaxFiles = 10\nMaxSize = 1000\n\n[log]\n\nlogFile = ./logs/l.log\n\n[socket]\n\nname = ./socket\nbacklog = 100\n" > build/confix.txt @echo "[threadpool]" > $(BUILD_DIR)/config.txt
$(BUILD_DIR)/server & @echo "quantity = 4" >> $(BUILD_DIR)/config.txt
@echo "pending = 100" >> $(BUILD_DIR)/config.txt
@echo "" >> $(BUILD_DIR)/config.txt
@echo "[files]" >> $(BUILD_DIR)/config.txt
@echo "MaxSize = 1000" >> $(BUILD_DIR)/config.txt
@echo "MaxFiles = 10" >> $(BUILD_DIR)/config.txt
@echo "" >> $(BUILD_DIR)/config.txt
@echo "[log]" >> $(BUILD_DIR)/config.txt
@echo "logFile = ./logs/l.log" >> $(BUILD_DIR)/config.txt
@echo "" >> $(BUILD_DIR)/config.txt
@echo "[socket]" >> $(BUILD_DIR)/config.txt
@echo "name = ./socket" >> $(BUILD_DIR)/config.txt
@echo "backlog = 100" >> $(BUILD_DIR)/config.txt
$(BUILD_DIR)/server $(BUILD_DIR)/config.txt &
bash scripts/test2.sh bash scripts/test2.sh
pkill -1 server pkill -1 server

View File

@ -1,8 +1,8 @@
#!/bin/sh #!/bin/sh
./build/client -t 200 -f socket -w testFiles/1 -D Wdir -W testFiles/1/file1 -r testFiles/1/file1 -d Rdir -R n=3 -p ./build/client -t 200 -f socket -w testFiles/1 -D build/Wdir -W testFiles/1/file1 -r testFiles/1/file1 -d build/Rdir -R n=3 -p
exit 0 exit 0
./client -t 200 -f socket -w testFiles -D Wdir -W testFiles/6/f1 -r testFiles/6/f1 -d Rdir -R n=3 -l testFiles/6/f1 -u testFiles/6/f1 -c testFiles/6/f1 -p ./client -t 200 -f socket -w testFiles -D build/Wdir -W testFiles/6/f1 -r testFiles/6/f1 -d build/Rdir -R n=3 -l testFiles/6/f1 -u testFiles/6/f1 -c testFiles/6/f1 -p
exit 0 exit 0

View File

@ -309,22 +309,32 @@ int main(int argc, char *argv[]) {
continue; continue;
} }
if (i == request_pipe[0]) { // un worker ha finito di servire un client if (i == request_pipe[0]) { // un worker ha finito di servire un client
int pdr; // ottengo il descrittore della pipe long pdr; // ottengo il descrittore della pipe
if (readn(request_pipe[0], &pdr, sizeof(int)) == -1) { if (readn(request_pipe[0], &pdr, sizeof(long)) == -1) {
perror("readn"); perror("readn");
free(connfd); free(connfd);
break; break;
} }
switch (pdr) { switch (pdr) {
case -1: // client disconnected case -1: // client disconnected
--numberOfConnections; --numberOfConnections;
n = snprintf(buf, sizeof(buf), "Client %ld disconnected.\n", pdr);
if( n<0 ) {
perror("snprintf");
free(connfd);
goto _cleanup;
}
if( taglia_log(taglia, buf) < 0) {
free(connfd);
goto _cleanup;
}
if (stopNewConnections && numberOfConnections <= 0) { if (stopNewConnections && numberOfConnections <= 0) {
quit = 1; quit = 1;
// termino il signalThread // termino il signalThread
pthread_cancel(sighandler_thread); pthread_cancel(sighandler_thread);
break; break;
} }
free(connfd);
continue; continue;
default: // client served but not disconnected default: // client served but not disconnected
FD_SET(pdr, &set); FD_SET(pdr, &set);
@ -399,7 +409,6 @@ int main(int argc, char *argv[]) {
// aggiungo al threadpool // aggiungo al threadpool
int r = addToThreadPool(pool, threadF, args); int r = addToThreadPool(pool, threadF, args);
if (r == 0) { if (r == 0) {
numberOfConnections++;
free(connfd); free(connfd);
continue; // aggiunto con successo continue; // aggiunto con successo
} }

View File

@ -66,8 +66,11 @@ void threadF(void *arg) {
goto _cleanup; goto _cleanup;
} }
if (n==0) if (n==-1)
goto _cleanup; goto _cleanup;
if (n==0)
goto closeConnection;
str.str = calloc(str.len+1, sizeof(char)); str.str = calloc(str.len+1, sizeof(char));
if (!str.str) { if (!str.str) {
perror("calloc"); perror("calloc");
@ -82,12 +85,13 @@ void threadF(void *arg) {
} }
str.str[str.len] = '\0'; str.str[str.len] = '\0';
if(strncmp(str.str, "quit", 5) == 0) { // il client vuole chiudere la connessione closeConnection:
if(n==0 || strncmp(str.str, "quit", 5) == 0) { // il client vuole chiudere la connessione
close(connfd); close(connfd);
int close = -1; long close = -1;
// comunico al manager che ho chiuso la connessione // comunico al manager che ho chiuso la connessione
if (writen(request_pipe, &close, sizeof(int)) == -1) { if (writen(request_pipe, &close, sizeof(long)) == -1) {
perror("writen"); perror("writen");
goto _cleanup; goto _cleanup;
} }
@ -111,7 +115,7 @@ void threadF(void *arg) {
// str.str non è più valido perchè parser fa free // str.str non è più valido perchè parser fa free
// comunico al manager che è stata servita la richiesta // comunico al manager che è stata servita la richiesta
if (writen(request_pipe, &connfd, sizeof(int)) == -1) { if (writen(request_pipe, &connfd, sizeof(long)) == -1) {
perror("writen"); perror("writen");
} }
// log // log