This commit is contained in:
xdrm-brackets 2017-04-08 18:20:17 +02:00
parent 0dc72353cd
commit 23af65d69e
4 changed files with 41 additions and 31 deletions

View File

@ -23,8 +23,10 @@ int main(int argc, char* argv[]){
/* [0] Initialisation des variables globales /* [0] Initialisation des variables globales
=========================================================*/ =========================================================*/
data = (struct plane*) malloc( 0 ); sgca_data = (struct plane*) malloc( sizeof(struct plane) );
update = (struct plane*) malloc( 0 ); sgca_update = (struct plane*) malloc( sizeof(struct plane) );
sgca_index.data = 0;
sgca_index.update = 0;
/* [1] Lancement des THREADS d'écoute /* [1] Lancement des THREADS d'écoute
=========================================================*/ =========================================================*/
@ -53,8 +55,8 @@ int main(int argc, char* argv[]){
/* [3] On libère les variables globales /* [3] On libère les variables globales
==========================================================*/ ==========================================================*/
free(data); free(sgca_data);
free(update); free(sgca_update);
} }
@ -378,6 +380,11 @@ void* managePlane(void* THREADABLE_SOCKET){
for( i = 0 ; i < MAX_TCP_THR ; i++ ) for( i = 0 ; i < MAX_TCP_THR ; i++ )
if( TCPManagers[i] == pthread_self() ){ index = i; break; } if( TCPManagers[i] == pthread_self() ){ index = i; break; }
// Erreur de thread
if( index == -1 ){
if( DEBUGMOD&THR ) printf("{tcp_com}(%d) Unknown thread index. Aborting\n", index);
pthread_exit(NULL);
}
while( 1 ){ while( 1 ){
@ -387,12 +394,22 @@ void* managePlane(void* THREADABLE_SOCKET){
/* 1. On lit sur la socket */ /* 1. On lit sur la socket */
read = recv(TCP_SOCKET, buffer, MAX_BUF_LEN, 0); read = recv(TCP_SOCKET, buffer, MAX_BUF_LEN, 0);
/* 2. Si erreur reception */ /* 2.1. 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;
}
/* 2.2. Si message trop court */
if( read < PLANE_LEN ){ if( read < PLANE_LEN ){
if( DEBUGMOD&BUF ) printf("{tcp_com}(%d) read: %d (expected: %d)\n", index, read, (int) PLANE_LEN); if( DEBUGMOD&BUF ) printf("{tcp_com}(%d) read: %d (expected: %d)\n", index, read, (int) PLANE_LEN);
continue; continue;
} }
/* 3. On parse la requête*/ /* 3. On parse la requête*/
memcpy(&data.code, buffer+sizeof(char)*0+sizeof(int)*0, sizeof(char)*6); memcpy(&data.code, buffer+sizeof(char)*0+sizeof(int)*0, sizeof(char)*6);
memcpy(&data.x, buffer+sizeof(char)*6+sizeof(int)*0, sizeof(int)); memcpy(&data.x, buffer+sizeof(char)*6+sizeof(int)*0, sizeof(int));
@ -400,7 +417,7 @@ void* managePlane(void* THREADABLE_SOCKET){
memcpy(&data.z, buffer+sizeof(char)*6+sizeof(int)*2, sizeof(int)); memcpy(&data.z, buffer+sizeof(char)*6+sizeof(int)*2, sizeof(int));
memcpy(&data.spd, buffer+sizeof(char)*6+sizeof(int)*4, sizeof(int)); memcpy(&data.spd, buffer+sizeof(char)*6+sizeof(int)*4, sizeof(int));
memcpy(&data.cap, buffer+sizeof(char)*6+sizeof(int)*3, sizeof(int)); memcpy(&data.cap, buffer+sizeof(char)*6+sizeof(int)*3, sizeof(int));
printf("{tcp_com}(%d) received: plane_req{code = '%6s'; x = %d; y = %d; z = %d; cap = %d; spd = %d}\n", index, data.code, data.x, data.y, data.z, data.cap, data.spd); printf("{tcp_com}(%d) received: plane_req{code = '%s'; x = %d; y = %d; z = %d; cap = %d; spd = %d}\n", index, data.code, data.x, data.y, data.z, data.cap, data.spd);
/* (3) Gestion de la requête -> enregistrement /* (3) Gestion de la requête -> enregistrement
@ -408,33 +425,25 @@ void* managePlane(void* THREADABLE_SOCKET){
pindex = -1; pindex = -1;
/* 1. On regarde si l'avion existe */ /* 1. On regarde si l'avion existe */
for( i = 0 ; i < planeIndex.data ; i++ ) for( i = 0 ; i < sgca_index.data ; i++ ){
// Si on trouve le meme code, on garde l'indice pindex = sgca_index.data;
if( strcmp(data[i].code, data.code) == 0 ){
pindex = i;
break;
}
/* 2. Si l'avion est nouveau -> on l'enregistre */
if( pindex == -1 ){
index = planeIndex.data;
// On ajoute une entrée à data // On ajoute une entrée à data
data = (struct plane*) realloc(data, sizeof(struct plane)*planeIndex.data); sgca_data = (struct plane*) realloc(sgca_data, sizeof(struct plane)*sgca_index.data + 1);
planeIndex.data++; sgca_index.data++;
} }
/* 3. On met à jour les données*/ /* 3. On met à jour les données*/
memcpy(&plane[index], &data, sizeof(struct plane)); memcpy(&sgca_data[pindex], &data, sizeof(struct plane));
/* (4) Envoi de la réponse /* (4) Envoi de la réponse
---------------------------------------------------------*/ ---------------------------------------------------------*/
printf("send: %d\n", (int) send(TCP_SOCKET, "coucou\n", 8, 0));

View File

@ -33,10 +33,10 @@ static pthread_t UDPManagers[MAX_UDP_THR]; // contiendra les THREADS UDP
static int activeUDPManagers[MAX_UDP_THR] = {0}; // contiendra les THREADS UDP actifs static int activeUDPManagers[MAX_UDP_THR] = {0}; // contiendra les THREADS UDP actifs
// ENREGISTREMENT DES DONNEES AVIONS // ENREGISTREMENT DES DONNEES AVIONS
struct planeIndexStruct{ struct {
unsigned int data; unsigned int data;
unsigned int update; unsigned int update;
} planeIndex; } sgca_index;
struct plane* data; struct plane* sgca_data;
struct plane* update; struct plane* sgca_update;

View File

@ -8,6 +8,7 @@
#include <string.h> #include <string.h>
#include <pthread.h> #include <pthread.h>
#include <stdint.h> #include <stdint.h>
#include <errno.h>
/* sys */ /* sys */
#include <sys/types.h> #include <sys/types.h>