[FIX] ctrlTerm ok (ctrlTerm<->sgca<->plane)

This commit is contained in:
xdrm-brackets 2017-04-29 17:04:07 +02:00
parent a8a44d9941
commit e4b9d5daa6
4 changed files with 101 additions and 60 deletions

View File

@ -31,7 +31,7 @@ boot: lib/network/common.o lib/network/tcp/server.o lib/network/udp/server.o lib
# Run full compilation
all: boot
all: clean boot
# cleans the compiled files
clean:

View File

@ -35,8 +35,8 @@ int main(int argc, char* argv[]){
=========================================================*/
/* 1. Variables globales */
sgca.n = 0;
sgca.data = (struct plane*) malloc( sizeof(struct plane) );
sgca.socket = (int*) malloc( sizeof(int) );
sgca.data = (struct plane*) malloc( sizeof(struct plane) );
sgca.socket = (int*) malloc( sizeof(int) );
struct in_addr* SERV_HOST;
/* 2. On récupère l'IP locale' */

View File

@ -67,6 +67,26 @@ void* managePlane(void* THREADABLE_ARGS){
/* (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, MAX_BUF_LEN, 0);
@ -76,9 +96,8 @@ void* managePlane(void* THREADABLE_ARGS){
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);
continue;
}
@ -279,9 +298,6 @@ void* manageViewTerm(void* THREADABLE_ARGS){
/* Gestion d'une connexion TERMINAL
*
* @THREADABLE_SOCKET<void*> SOCKET de la connexion client
@ -309,12 +325,13 @@ void* manageCtrlTerm(void* THREADABLE_ARGS){
socklen_t len;
size_t dataLen;
char loop = 1, update = 0, fbk = 0;
int count, last, sent, nb; // compteurs d'envoi
int i, index = -1; // Compteurs globaux
int pindex; // index of the current plane
char buffer[MAX_BUF_LEN]; // Buffer d'envoi
int count, last; // compteurs d'envoi
int i, index = -1; // Compteurs globaux
int pindex; // index of the current plane
char buffer[MAX_BUF_LEN]; // Buffer d'envoi
char* dataBuffer = malloc(1);
struct term_req request; // Requête
struct term_req request; // Requête
/* 2. On récupère les arguments */
struct handler_arg* arg = THREADABLE_ARGS;
@ -330,9 +347,8 @@ void* manageCtrlTerm(void* THREADABLE_ARGS){
if( recvfrom(arg->socket, buffer, MAX_BUF_LEN*sizeof(char), 0, (struct sockaddr*) &clientInfo, &len) < 0 ){
printf("{udp_cterm}{com}(%d) No terminal detected, exiting\n", index);
loop = 0;
}
if( loop ) printf("{udp_cterm}{com}(%d) Terminal connected\n", index);
}else
printf("{udp_cterm}{com}(%d) Terminal connected\n", index);
while( loop ){
@ -347,8 +363,11 @@ void* manageCtrlTerm(void* THREADABLE_ARGS){
count = recvfrom(arg->socket, buffer, MAX_BUF_LEN, 0, (struct sockaddr*) &clientInfo, &len);
/* 2. Si erreur reception */
if( count <= 0 ) // because of timeout or error
continue;
if( count < TERMREQ_LEN ){
send(arg->socket, "0", sizeof(char)*2, 0);
send(arg->socket, "\0\0", sizeof(char)*2, 0);
if( DEBUGMOD&BUF ) printf("{udp_cterm}{com}(%d) Error receiving request\n", index);
continue;
}
@ -362,11 +381,8 @@ void* manageCtrlTerm(void* THREADABLE_ARGS){
count += last; last = sizeof(int); memcpy(&request.update.z, buffer+count, last );
count += last; last = sizeof(int); memcpy(&request.update.cap, buffer+count, last );
count += last; last = sizeof(int); memcpy(&request.update.spd, buffer+count, last );
// indian-switch
request.update.cap = ntohl(request.update.cap);
request.update.spd = ntohl(request.update.spd);
printf("{udp_cterm}{com}(%d) { flags: %d; plane: #%s { z=%d; cap=%d; speed=%d} }\n", index, request.flags, request.update.code, request.update.z, request.update.cap, request.update.spd);
printf("{udp_cterm}{com}(%d) Plane req { flags: %d; plane: #%s { z=%d; cap=%d; speed=%d } }\n", index, request.flags, request.update.code, ntohl(request.update.z), ntohl(request.update.cap), ntohl(request.update.spd));
/* [3] Gestion de la requête
@ -375,18 +391,22 @@ void* manageCtrlTerm(void* THREADABLE_ARGS){
update = ( request.flags&TERMREQ_ALT || request.flags&TERMREQ_CAP || request.flags&TERMREQ_SPD );
fbk = request.flags&TERMREQ_FBK;
if( !( update || fbk ) ){
send(arg->socket, "0", sizeof(char)*2, 0);
printf("{udp_cterm}{com}(%d) Invalid flag, passing\n", index);
send(arg->socket, "\0\0", sizeof(char)*2, 0);
printf("{udp_cterm}{com}(%d) Invalid flag, passing\n", index);
continue;
}
if( update ){
/* [4] Gestion de la mise à jour de valeurs
=========================================================*/
while( update ){
/* 2. On regarde si on trouve l'avion */
/* (1) Recherche de l'avion
---------------------------------------------------------*/
/* 1. On cherche l'avion par code */
for( i = 0 ; i < arg->sgca->n ; i++ ){
// Si l'avion existe et online -> on passe à la suite
/* 2. Si l'avion existe et online -> on passe à la suite */
if( strcmp(arg->sgca->data[i].code, request.update.code) == 0 && arg->sgca->data[i].online ){
pindex = i;
break;
@ -395,38 +415,42 @@ void* manageCtrlTerm(void* THREADABLE_ARGS){
}
/* 3. Si on a pas trouvé l'avion ou qu'il est déconnecté, on passe */
/* 3. Si on a pas trouvé -> on quitte l'udpdate */
if( pindex <= -1 || arg->sgca->socket[pindex] <= -1 ){
printf("{udp_cterm}{com}(%d) Plane unknown or unreachable, passing\n", index);
continue;
break;
}
/* 4. On transfère la requête à l'avion */
/* (2) Transfert de la requête à l'avion
---------------------------------------------------------*/
/* 1. On prépare la requête */
bzero(buffer, sizeof(char)*MAX_BUF_LEN);
// indian-switch
request.update.z = htonl(request.update.z);
request.update.cap = htonl(request.update.cap);
request.update.spd = htonl(request.update.spd);
/* 2. On remplit le buffer d'envoi */
count = 0; last = sizeof(char); memcpy(buffer+count, &request.flags, last );
count += last; last = sizeof(int); memcpy(buffer+count, &request.update.z, last );
count += last; last = sizeof(int); memcpy(buffer+count, &request.update.cap, last );
count += last; last = sizeof(int); memcpy(buffer+count, &request.update.spd, last );
count += last;
if( send(arg->sgca->socket[pindex], buffer, count/sizeof(char)+1, 0) <= 0 ){
printf("{udp_cterm}{com}(%d) Cannot send request to plane\n", index);
continue;
/* 3. On envoie la requête à l'avion */
if( send(arg->sgca->socket[pindex], buffer, count/sizeof(char), 0) <= 0 ){
printf("{udp_cterm}{com}(%d) Cannot send request to plane\n", index);
break;
}
/* [4] Gestion de la réponse de l'avion
=========================================================*/
/* 1. Réception */
bzero(buffer, sizeof(char)*MAX_BUF_LEN);
if( recv(arg->sgca->socket[pindex], buffer, MAX_BUF_LEN, 0) <= 0 ){
send(arg->socket, "0", sizeof(char)*2, 0);
printf("{udp_cterm}{com}(%d) Cannot get response from plane\n", index);
continue;
/* (3) Gestion de la réponse de l'avion
---------------------------------------------------------*/
/* 1. Réception de la réponse */
count = recv(arg->sgca->socket[pindex], buffer, sizeof(char), 0);
if( count != sizeof(char) ){
printf("{udp_cterm}{com}(%d) Cannot get response from plane %d (%d)\n", index, count, PLANE_LEN);
break;
}
printf("{udp_cterm}{com}(%d) Plane res { flags: %d }\n", index, buffer[0]);
/* 2. Gestion de la validation de l'update */
if( !(buffer[0]&TERMREQ_ALT) && request.flags&TERMREQ_ALT )
@ -438,6 +462,15 @@ void* manageCtrlTerm(void* THREADABLE_ARGS){
if( !(buffer[0]&TERMREQ_SPD) && request.flags&TERMREQ_SPD )
request.flags -= TERMREQ_SPD;
/* 3. Si pas de demande des données -> on*/
if( !fbk ){
dataBuffer = realloc(dataBuffer, sizeof(char)*2 );
dataBuffer[0] = request.flags;
dataBuffer[1] = 0;
}
break;
}
if( fbk ){
@ -449,15 +482,16 @@ void* manageCtrlTerm(void* THREADABLE_ARGS){
/* 2. Si update, on mixe les flags */
if( update )
dataBuffer[0] |= request.flags;
dataBuffer[0] |= request.flags;
}
/* [6] Réponse au terminal
=========================================================*/
if( send(arg->socket, dataBuffer, dataLen+1, 0) < 0 )
printf("{udp_cterm}{com}(%d) Cannot answer to terminal\n", index);
len = sizeof(struct sockaddr_in);
if( sendto(arg->socket, dataBuffer, dataLen, 0, (struct sockaddr*) &clientInfo, len) < 0 )
printf("{udp_cterm}{com}(%d) Cannot answer to terminal\n", index);
}
@ -471,6 +505,7 @@ void* manageCtrlTerm(void* THREADABLE_ARGS){
/* 2. On ferme la socket */
close(arg->socket);
free(dataBuffer);
/* 3. On arrête le THREAD */
if( DEBUGMOD&THR ) printf("{udp_cterm}{com}(%d) freed\n", index);

View File

@ -117,6 +117,7 @@ void close_communication(){
void send_data(){
/* [0] Initialisation des variables
@ -141,13 +142,13 @@ void send_data(){
request.spd = htonl( ctrl.speed );
/* 3. Copie buffer */
strcpy(buffer, request.code);
memcpy(buffer+sizeof(char)*0+sizeof(int)*0, &request.code, sizeof(char)*6 );
memcpy(buffer+sizeof(char)*6+sizeof(int)*0, &request.x, sizeof(int) );
memcpy(buffer+sizeof(char)*6+sizeof(int)*1, &request.y, sizeof(int) );
memcpy(buffer+sizeof(char)*6+sizeof(int)*2, &request.z, sizeof(int) );
memcpy(buffer+sizeof(char)*6+sizeof(int)*3, &request.cap, sizeof(int) );
memcpy(buffer+sizeof(char)*6+sizeof(int)*4, &request.spd, sizeof(int) );
buffer[0] = 0x55;
memcpy(buffer+sizeof(char)*1+sizeof(int)*0, &request.code, sizeof(char)*6 );
memcpy(buffer+sizeof(char)*7+sizeof(int)*0, &request.x, sizeof(int) );
memcpy(buffer+sizeof(char)*7+sizeof(int)*1, &request.y, sizeof(int) );
memcpy(buffer+sizeof(char)*7+sizeof(int)*2, &request.z, sizeof(int) );
memcpy(buffer+sizeof(char)*7+sizeof(int)*3, &request.cap, sizeof(int) );
memcpy(buffer+sizeof(char)*7+sizeof(int)*4, &request.spd, sizeof(int) );
read = send(commu_socket, buffer, PLANE_DATA_LEN/sizeof(char) + 1, 0);
@ -300,9 +301,11 @@ void update(){
received = recv(commu_socket, buffer, MAX_BUF_LEN, 0);
/* 2. Si rien -> on reboucle */
if( received <= 0 || received < (sizeof(char)+3*sizeof(int)) )
if( received != (sizeof(char)+3*sizeof(int)) )
continue;
printf("** Received update request **\n");
/* [2] Gestion de requête de mise à jour
=========================================================*/
@ -324,22 +327,25 @@ void update(){
request.flags -= REQ_ALT;
if( request.flags&REQ_SPD )
if( update_z(request.spd) == -1 )
if( update_speed(request.spd) == -1 )
request.flags -= REQ_SPD;
if( request.flags&REQ_CAP )
if( update_z(request.cap) == -1 )
if( update_cap(request.cap) == -1 )
request.flags -= REQ_CAP;
/* [3] Gestion de la réponse
=========================================================*/
/* 1. On prépare la réponse */
bzero(buffer, sizeof(char)*MAX_BUF_LEN);
memcpy(buffer, &request.flags, sizeof(char));
buffer[0] = 0;
buffer[1] = request.flags;
printf("Response flags : %d\n", request.flags);
/* 2. Envoi de la réponse */
send(commu_socket, buffer, sizeof(char)*2, 0);
if( send(commu_socket, buffer, sizeof(char)*2, 0) <= 0 ){
printf("Cannot send response\n");
}
}