From e190ac30086bfaaba117b9369e5b8b96e7df7572 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 12 Apr 2017 10:32:48 +0200 Subject: [PATCH] =?UTF-8?q?[done]=20Gestion=20partielle=20r=C3=A9ponse=20a?= =?UTF-8?q?vion=20depuis=20SGCA=20[todo]=20gestion=20des=20requ=C3=AAtes?= =?UTF-8?q?=20d'update=20[todo]=20Fix=20car=202=20avions=20en=20m=C3=AAme?= =?UTF-8?q?=20temps=20entrent=20en=20collision=20(le=20dernier=20prend=20l?= =?UTF-8?q?e=20dessus).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- central-manager/lib/local/handler.c | 61 ++++++++++++++++++++++++----- plane/plane.c | 13 ++++-- 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/central-manager/lib/local/handler.c b/central-manager/lib/local/handler.c index ad15992..0d3e529 100644 --- a/central-manager/lib/local/handler.c +++ b/central-manager/lib/local/handler.c @@ -39,6 +39,8 @@ void* managePlane(void* THREADABLE_ARGS){ int read, i, index, pindex; // compteurs char buffer[MAX_BUF_LEN]; // buffer struct plane data; // données de l'avion + struct sockaddr_in clientInfo; // Info client + socklen_t len; /* 2. On récupère les arguments */ @@ -61,7 +63,8 @@ void* managePlane(void* THREADABLE_ARGS){ /* (2) Récupération de la requête ---------------------------------------------------------*/ /* 1. On lit sur la socket */ - read = recv(arg->socket, buffer, MAX_BUF_LEN, 0); + len = sizeof(struct sockaddr_in); + read = recvfrom(arg->socket, buffer, MAX_BUF_LEN, 0, (struct sockaddr*) &clientInfo, &len); /* 2.1. Si erreur reception (-1:erreur, 0:fermeture client propre) */ if( read <= 0 ){ @@ -84,7 +87,6 @@ void* managePlane(void* THREADABLE_ARGS){ 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 = '%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 @@ -94,23 +96,62 @@ void* managePlane(void* THREADABLE_ARGS){ /* 1. On regarde si l'avion existe */ for( i = 0 ; i < arg->sgca->n ; i++ ){ - pindex = arg->sgca->n; - - // On ajoute une entrée à data - arg->sgca->data = (struct plane*) realloc(arg->sgca->data, sizeof(struct plane)*arg->sgca->n + 1); - arg->sgca->n++; + // Si l'avion existe -> on passe à la suite + if( strcmp(arg->sgca->data[i].code, data.code) == 0 ){ + pindex = i; + break; + } } - /* 3. On met à jour les données*/ + /* 2. Création si n'existe pas */ + if( pindex == -1 ){ + + // On ajoute une entrée à data + pindex = arg->sgca->n; + arg->sgca->n++; + arg->sgca->data = (struct plane*) realloc(arg->sgca->data, sizeof(struct plane)*arg->sgca->n + 1); + printf("{tcp_com}(%d) plane '%s' (#%d) created\n", index, data.code, pindex); + + } + + /* 3. On copie les constantes */ memcpy(&arg->sgca->data[pindex], &data, sizeof(struct plane)); + printf("{tcp_com}(%d) stored (%d)'%s': {x = %d; y = %d; z = %d; cap = %d; spd = %d}\n", index, pindex, arg->sgca->data[pindex].code, arg->sgca->data[pindex].x, arg->sgca->data[pindex].y, arg->sgca->data[pindex].z, arg->sgca->data[pindex].cap, arg->sgca->data[pindex].spd); - /* (4) Envoi de la réponse + + /* (4) On prépare la réponse ---------------------------------------------------------*/ - printf("send: %d\n", (int) send(arg->socket, "coucou\n", 8, 0)); + /* 1. ACK data */ + strcpy(data.code, arg->sgca->data[pindex].code); + data.x = arg->sgca->data[pindex].x; + data.y = arg->sgca->data[pindex].y; + data.z = arg->sgca->data[pindex].z; + data.cap = arg->sgca->data[pindex].cap; + data.spd = arg->sgca->data[pindex].spd; + + /* 2. Vérification du FLAG (modifications) si requêtes d'update */ + //TODO: Gestion arg->sgca->update[pindex].flags|UPD_SPEED etc.. pour ajout du flag+elements dans reponse + + /* 3. On sérialise la réponse */ + bzero(buffer, MAX_BUF_LEN*sizeof(char)); + memcpy(buffer+sizeof(char)*0+sizeof(int)*0, &data.code, sizeof(char)*6); + memcpy(buffer+sizeof(char)*6+sizeof(int)*0, &data.x, sizeof(int)); + memcpy(buffer+sizeof(char)*6+sizeof(int)*1, &data.y, sizeof(int)); + memcpy(buffer+sizeof(char)*6+sizeof(int)*2, &data.z, sizeof(int)); + memcpy(buffer+sizeof(char)*6+sizeof(int)*4, &data.spd, sizeof(int)); + memcpy(buffer+sizeof(char)*6+sizeof(int)*3, &data.cap, sizeof(int)); + + + /* (5) Envoi de la réponse + ---------------------------------------------------------*/ + len = sizeof(struct sockaddr_in); + read = sendto(arg->socket, buffer, PLANE_LEN/sizeof(char) + 1, 0, (struct sockaddr*) &clientInfo, len); + + printf("send: %d\n", read); diff --git a/plane/plane.c b/plane/plane.c index 718517b..b751c4f 100644 --- a/plane/plane.c +++ b/plane/plane.c @@ -115,9 +115,9 @@ void send_data(){ /* [0] Initialisation des variables =========================================================*/ - char buffer[MAX_BUF_LEN] = {0}; - struct plane_data request; - int read; + char buffer[MAX_BUF_LEN] = {0}; + struct plane_data request; + int read; /* [1] Envoi des caractéristiques @@ -151,7 +151,12 @@ void send_data(){ } /* 4. Récupération réponse */ - //TODO: recv() + read = recv(commu_socket, buffer, MAX_BUF_LEN, 0); + + if( read < 0 ){ + printf("Erreur de réception\n"); + return; + } /* 5. Gestion mise à jour */ //TODO: update_cap etc avec flags