[done] Gestion partielle réponse avion depuis SGCA [todo] gestion des requêtes d'update [todo] Fix car 2 avions en même temps entrent en collision (le dernier prend le dessus).
This commit is contained in:
parent
791ca5ab09
commit
e190ac3008
|
@ -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);
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue