From 7b3f8c101cc8ca98dd3b36748417a310105107dd Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 3 May 2017 12:35:18 +0200 Subject: [PATCH] Mutex management for plane socket used by ctlrTerm --- central-manager/lib/header.h | 6 +++-- central-manager/lib/local/handler.c | 42 +++++++++++++---------------- plane/plane.c | 5 ++-- 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/central-manager/lib/header.h b/central-manager/lib/header.h index f3ec03c..55c16cb 100644 --- a/central-manager/lib/header.h +++ b/central-manager/lib/header.h @@ -56,8 +56,10 @@ struct context_unit{ - int socket; // socket associée à un avion - struct plane data; // données d'un avion + int socket; // socket associée à un avion + struct plane data; // données d'un avion + pthread_mutex_t mutex; // socket mutual exclusion + char token; // if want to have mutex }; struct context{ diff --git a/central-manager/lib/local/handler.c b/central-manager/lib/local/handler.c index ae96e0b..fea6c7e 100644 --- a/central-manager/lib/local/handler.c +++ b/central-manager/lib/local/handler.c @@ -65,30 +65,17 @@ void* managePlane(void* THREADABLE_ARGS){ while( 1 ){ + + /* (2) Récupération de la requête ---------------------------------------------------------*/ - /* (1) Premier byte pour savoir si la réponse est pour nous - ---------------------------------------------------------*/ + /* 1. On lit sur la socket */ - read = recv(SOCKET, buffer, 1, 0); - - /* 2. Si erreur reception (-1:erreur, 0:fermeture client propre) */ - if( read <= 0 ){ - if( DEBUGMOD&BUF ) printf("{tcp_com}(%d) read: %d -> must exit thread\n", index, read); - break; - } - - /* 3. Si pas pour nous -> on attends puis on reboucle */ - if( buffer[0] == 0x55 ){ // '01010101'(2) '55'(16) - if( DEBUGMOD&BUF ) printf("{tcp_com}(%d) Let request to update routine\n", index); - sleep(0.5); - continue; - } - - /* (2) Données - ---------------------------------------------------------*/ - /* 1. On lit sur la socket */ - read = recv(SOCKET, buffer+1, MAX_BUF_LEN-1, 0); + if( pindex > -1 && arg->sgca->unit[pindex].token != 0 ) + sleep(1); + if( pindex > -1 ) pthread_mutex_lock(&arg->sgca->unit[pindex].mutex); + read = recv(SOCKET, buffer, MAX_BUF_LEN, 0); + if( pindex > -1 ) pthread_mutex_unlock(&arg->sgca->unit[pindex].mutex); /* 2.1. Si erreur reception (-1:erreur, 0:fermeture client propre) */ if( read <= 0 ){ @@ -97,8 +84,8 @@ void* managePlane(void* THREADABLE_ARGS){ } /* 2.2. Si message trop court */ - if( read != PLANE_LEN-1 ){ - if( DEBUGMOD&BUF ) printf("{tcp_com}(%d) read: %d (expected: %d)\n", index, read+1, (int) PLANE_LEN); + if( read != PLANE_LEN ){ + if( DEBUGMOD&BUF ) printf("{tcp_com}(%d) read: %d (expected: %d)\n", index, read, (int) PLANE_LEN); continue; } @@ -147,6 +134,8 @@ void* managePlane(void* THREADABLE_ARGS){ arg->sgca->n++; arg->sgca->unit = (struct context_unit*) realloc(arg->sgca->unit, sizeof(struct context_unit)*arg->sgca->n + 1); arg->sgca->unit[pindex].socket = SOCKET; + arg->sgca->unit[pindex].token = 0; + pthread_mutex_init(&arg->sgca->unit[pindex].mutex, NULL); printf("{tcp_com}(%d) plane '%s' (#%d) created\n", index, data.code, pindex); } @@ -433,6 +422,10 @@ void* manageCtrlTerm(void* THREADABLE_ARGS){ count += last; last = sizeof(int); memcpy(buffer+count, &request.update.spd, last ); count += last; + // utilisation socket + arg->sgca->unit[pindex].token = 1; + pthread_mutex_lock(&arg->sgca->unit[pindex].mutex); + /* 3. On envoie la requête à l'avion */ if( send(arg->sgca->unit[pindex].socket, buffer, count/sizeof(char), 0) <= 0 ){ printf("{udp_cterm}{com}(%d) Cannot send request to plane\n", index); @@ -445,6 +438,9 @@ void* manageCtrlTerm(void* THREADABLE_ARGS){ /* 1. Réception de la réponse */ count = recv(arg->sgca->unit[pindex].socket, buffer, sizeof(char), 0); + // libération socket + pthread_mutex_unlock(&arg->sgca->unit[pindex].mutex); + if( count != sizeof(char) ){ printf("{udp_cterm}{com}(%d) Cannot get response from plane %d (%d)\n", index, count, (int) PLANE_LEN); break; diff --git a/plane/plane.c b/plane/plane.c index d7c195a..2220bc5 100644 --- a/plane/plane.c +++ b/plane/plane.c @@ -337,12 +337,11 @@ void update(){ /* [3] Gestion de la réponse =========================================================*/ /* 1. On prépare la réponse */ - buffer[0] = 0x55; - buffer[1] = request.flags; + buffer[0] = request.flags; printf("Response flags : %d\n", request.flags); /* 2. Envoi de la réponse */ - if( send(commu_socket, buffer, sizeof(char)*2, 0) <= 0 ){ + if( send(commu_socket, buffer, sizeof(char), 0) <= 0 ){ printf("Cannot send response\n"); }