Mutex management for plane socket used by ctlrTerm
This commit is contained in:
parent
6cfa351c9a
commit
7b3f8c101c
|
@ -56,8 +56,10 @@
|
||||||
|
|
||||||
|
|
||||||
struct context_unit{
|
struct context_unit{
|
||||||
int socket; // socket associée à un avion
|
int socket; // socket associée à un avion
|
||||||
struct plane data; // données d'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{
|
struct context{
|
||||||
|
|
|
@ -65,30 +65,17 @@ void* managePlane(void* THREADABLE_ARGS){
|
||||||
|
|
||||||
while( 1 ){
|
while( 1 ){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* (2) Récupération de la requête
|
/* (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 */
|
/* 1. On lit sur la socket */
|
||||||
read = recv(SOCKET, buffer, 1, 0);
|
if( pindex > -1 && arg->sgca->unit[pindex].token != 0 )
|
||||||
|
sleep(1);
|
||||||
/* 2. Si erreur reception (-1:erreur, 0:fermeture client propre) */
|
if( pindex > -1 ) pthread_mutex_lock(&arg->sgca->unit[pindex].mutex);
|
||||||
if( read <= 0 ){
|
read = recv(SOCKET, buffer, MAX_BUF_LEN, 0);
|
||||||
if( DEBUGMOD&BUF ) printf("{tcp_com}(%d) read: %d -> must exit thread\n", index, read);
|
if( pindex > -1 ) pthread_mutex_unlock(&arg->sgca->unit[pindex].mutex);
|
||||||
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);
|
|
||||||
|
|
||||||
/* 2.1. Si erreur reception (-1:erreur, 0:fermeture client propre) */
|
/* 2.1. Si erreur reception (-1:erreur, 0:fermeture client propre) */
|
||||||
if( read <= 0 ){
|
if( read <= 0 ){
|
||||||
|
@ -97,8 +84,8 @@ void* managePlane(void* THREADABLE_ARGS){
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 2.2. Si message trop court */
|
/* 2.2. Si message trop court */
|
||||||
if( read != PLANE_LEN-1 ){
|
if( read != PLANE_LEN ){
|
||||||
if( DEBUGMOD&BUF ) printf("{tcp_com}(%d) read: %d (expected: %d)\n", index, read+1, (int) PLANE_LEN);
|
if( DEBUGMOD&BUF ) printf("{tcp_com}(%d) read: %d (expected: %d)\n", index, read, (int) PLANE_LEN);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,6 +134,8 @@ void* managePlane(void* THREADABLE_ARGS){
|
||||||
arg->sgca->n++;
|
arg->sgca->n++;
|
||||||
arg->sgca->unit = (struct context_unit*) realloc(arg->sgca->unit, sizeof(struct context_unit)*arg->sgca->n + 1);
|
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].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);
|
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; last = sizeof(int); memcpy(buffer+count, &request.update.spd, last );
|
||||||
count += 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 */
|
/* 3. On envoie la requête à l'avion */
|
||||||
if( send(arg->sgca->unit[pindex].socket, buffer, count/sizeof(char), 0) <= 0 ){
|
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);
|
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 */
|
/* 1. Réception de la réponse */
|
||||||
count = recv(arg->sgca->unit[pindex].socket, buffer, sizeof(char), 0);
|
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) ){
|
if( count != sizeof(char) ){
|
||||||
printf("{udp_cterm}{com}(%d) Cannot get response from plane %d (%d)\n", index, count, (int) PLANE_LEN);
|
printf("{udp_cterm}{com}(%d) Cannot get response from plane %d (%d)\n", index, count, (int) PLANE_LEN);
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -337,12 +337,11 @@ void update(){
|
||||||
/* [3] Gestion de la réponse
|
/* [3] Gestion de la réponse
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* 1. On prépare la réponse */
|
/* 1. On prépare la réponse */
|
||||||
buffer[0] = 0x55;
|
buffer[0] = request.flags;
|
||||||
buffer[1] = request.flags;
|
|
||||||
printf("Response flags : %d\n", request.flags);
|
printf("Response flags : %d\n", request.flags);
|
||||||
|
|
||||||
/* 2. Envoi de la réponse */
|
/* 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");
|
printf("Cannot send response\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue