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
=========================================================*/
data = (struct plane*) malloc( 0 );
update = (struct plane*) malloc( 0 );
sgca_data = (struct plane*) malloc( sizeof(struct plane) );
sgca_update = (struct plane*) malloc( sizeof(struct plane) );
sgca_index.data = 0;
sgca_index.update = 0;
/* [1] Lancement des THREADS d'écoute
=========================================================*/
@ -53,8 +55,8 @@ int main(int argc, char* argv[]){
/* [3] On libère les variables globales
==========================================================*/
free(data);
free(update);
free(sgca_data);
free(sgca_update);
}
@ -368,7 +370,7 @@ void* managePlane(void* THREADABLE_SOCKET){
/* 1. Variables utiles */
int read, i, index, pindex; // compteurs
char buffer[MAX_BUF_LEN]; // buffer
struct plane data; // données de l'avion
struct plane data; // données de l'avion
/* 2. On récupère la socket */
int TCP_SOCKET = (intptr_t) THREADABLE_SOCKET; // Socket client
@ -378,6 +380,11 @@ void* managePlane(void* THREADABLE_SOCKET){
for( i = 0 ; i < MAX_TCP_THR ; i++ )
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 ){
@ -387,12 +394,22 @@ void* managePlane(void* THREADABLE_SOCKET){
/* 1. On lit sur la socket */
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( DEBUGMOD&BUF ) printf("{tcp_com}(%d) read: %d (expected: %d)\n", index, read, (int) PLANE_LEN);
continue;
}
/* 3. On parse la requête*/
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));
@ -400,7 +417,7 @@ void* managePlane(void* THREADABLE_SOCKET){
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.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
@ -408,33 +425,25 @@ void* managePlane(void* THREADABLE_SOCKET){
pindex = -1;
/* 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
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;
pindex = sgca_index.data;
// On ajoute une entrée à data
data = (struct plane*) realloc(data, sizeof(struct plane)*planeIndex.data);
planeIndex.data++;
sgca_data = (struct plane*) realloc(sgca_data, sizeof(struct plane)*sgca_index.data + 1);
sgca_index.data++;
}
/* 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
---------------------------------------------------------*/
printf("send: %d\n", (int) send(TCP_SOCKET, "coucou\n", 8, 0));
@ -460,7 +469,7 @@ void* managePlane(void* THREADABLE_SOCKET){
activeTCPManagers[index] = 0;
/* 2. On arrête le THREAD */
if( DEBUGMOD&THR ) printf("{tcp_com}(%d) libéré\n", index);
if( DEBUGMOD&THR ) printf("{tcp_com}(%d) libéré\n", index);
pthread_exit(NULL);
}

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
// ENREGISTREMENT DES DONNEES AVIONS
struct planeIndexStruct{
struct {
unsigned int data;
unsigned int update;
} planeIndex;
} sgca_index;
struct plane* data;
struct plane* update;
struct plane* sgca_data;
struct plane* sgca_update;

View File

@ -9,11 +9,11 @@
#define PLANE_LEN ( sizeof(char)*6+sizeof(int)*5 )
struct plane{
char code[6];
int x;
int y;
int z;
int cap;
int spd;
int x;
int y;
int z;
int cap;
int spd;
};
struct term_req{

View File

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