Mutex management for plane socket used by ctlrTerm
This commit is contained in:
parent
6cfa351c9a
commit
7b3f8c101c
|
@ -58,6 +58,8 @@
|
|||
struct context_unit{
|
||||
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{
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue