Gestion viewTerm fonctionnel [todo] gestion envoi en plusieurs fois
This commit is contained in:
parent
a20cc18377
commit
387e55c092
|
@ -298,7 +298,7 @@ void* LISTEN_UDP(void* THREADABLE_ARGS){
|
|||
/* 2.1. On lance un thread pour le traitement de ce client */
|
||||
pthread_create(&UDPManagers[index], NULL, arg->handler, (void*) &thread_args);
|
||||
|
||||
if( DEBUGMOD&THR ) printf("{%s}{udp_com}(%d) démarré\n", entity, index);
|
||||
if( DEBUGMOD&THR ) printf("{%s}{com}(%d) démarré\n", entity, index);
|
||||
|
||||
/* 2.2. On signale que ce "manager" est maintenant actif */
|
||||
activeUDPManagers[index] = 1;
|
||||
|
|
|
@ -29,13 +29,13 @@
|
|||
};
|
||||
|
||||
struct term_req{
|
||||
char flags;
|
||||
char flags;
|
||||
struct plane udpate;
|
||||
};
|
||||
|
||||
struct term_res{
|
||||
char flags;
|
||||
char n;
|
||||
char flags;
|
||||
char n;
|
||||
struct plane* data;
|
||||
};
|
||||
|
||||
|
|
|
@ -188,10 +188,13 @@ void* managePlane(void* THREADABLE_ARGS){
|
|||
*
|
||||
* @history
|
||||
* [1] Initialisation des variables
|
||||
* [2] Récupération de la requête
|
||||
* [3] Traitement de la requête
|
||||
* [4] Création de la réponse
|
||||
* [5] Envoi de la réponse
|
||||
*
|
||||
* @loop
|
||||
* [2] Récupération des données
|
||||
* [3] Construction de la requête
|
||||
* [4] Envoi de la requête
|
||||
* [5] Timeout
|
||||
*
|
||||
* [6] On vide les buffers
|
||||
* [7] Fermeture de la connection (SOCKET)
|
||||
* [n] Arrêt du THREAD
|
||||
|
@ -204,60 +207,103 @@ void* manageViewTerm(void* THREADABLE_ARGS){
|
|||
|
||||
/* [1] Initialisation des variables
|
||||
=========================================================*/
|
||||
int read; // compteur
|
||||
/* 1. Initialisation des variables */
|
||||
struct sockaddr_in clientInfo;
|
||||
socklen_t len;
|
||||
char request[MAX_BUF_LEN]; // Requête
|
||||
// char response[MAX_BUF_LEN]; // Réponse
|
||||
socklen_t len;
|
||||
char loop = 1;
|
||||
int count, /*to_send, */sent; // compteurs d'envoi
|
||||
int i, index = -1; // Compteurs globaux
|
||||
char buffer[MAX_BUF_LEN]; // Buffer d'envoi
|
||||
struct term_res request; // Requête
|
||||
|
||||
/* 2. On récupère les arguments */
|
||||
struct handler_arg* arg = THREADABLE_ARGS;
|
||||
|
||||
/* 3. On récupère le rang dans les "managers" */
|
||||
for( i = 0 ; i < MAX_UDP_THR ; i++ )
|
||||
if( arg->managers[i] == pthread_self() ){ index = i; break; }
|
||||
|
||||
printf("{udp_vterm}{com}(%d) starting terminal routine\n", index);
|
||||
|
||||
|
||||
do{
|
||||
/* 4. Attente d'un client */
|
||||
len = sizeof(struct sockaddr_in);
|
||||
if( recvfrom(arg->socket, buffer, MAX_BUF_LEN*sizeof(char), 0, (struct sockaddr*) &clientInfo, &len) < 0 ){
|
||||
printf("{udp_vterm}{com}(%d) No terminal detected, exiting\n", index);
|
||||
loop = 0;
|
||||
}
|
||||
|
||||
printf("{udp_x-term}{udp_com} waiting for terminal request\n");
|
||||
/* [2] Récupération de la requête
|
||||
|
||||
while( loop ){
|
||||
|
||||
/* [2] Récupération des données
|
||||
=========================================================*/
|
||||
/* 1. On lit sur la socket */
|
||||
/* 1. On initialise les variables utiles */
|
||||
request.flags = TERMREQ_FBK;
|
||||
request.n = arg->sgca->n;
|
||||
|
||||
free(request.data);
|
||||
request.data = malloc( request.n * sizeof(struct plane) + 1 );
|
||||
|
||||
/* 2. On récupère la liste des avions */
|
||||
for( i = 0 ; i < request.n ; i++ )
|
||||
memcpy(&request.data[i], &arg->sgca->data[i], sizeof(struct plane));
|
||||
|
||||
|
||||
|
||||
/* [3] Construction de la requête
|
||||
=========================================================*/
|
||||
/* 1. Initialisation du buffer */
|
||||
bzero(buffer, MAX_BUF_LEN*sizeof(char));
|
||||
count = 0;
|
||||
|
||||
/* 2. Copie des données globales */
|
||||
memcpy(buffer+count, &request.flags, sizeof(char)); count += sizeof(char);
|
||||
memcpy(buffer+count, &request.n, sizeof(char)); count += sizeof(char);
|
||||
|
||||
/* 3. Copie des données des avions */
|
||||
for( i = 0 ; i < request.n ; i++ ){
|
||||
memcpy(buffer+count, &request.data[i].code, sizeof(char)*6 ); count += sizeof(char)*6;
|
||||
memcpy(buffer+count, &request.data[i].x, sizeof(int) ); count += sizeof(int);
|
||||
memcpy(buffer+count, &request.data[i].y, sizeof(int) ); count += sizeof(int);
|
||||
memcpy(buffer+count, &request.data[i].z, sizeof(int) ); count += sizeof(int);
|
||||
memcpy(buffer+count, &request.data[i].cap, sizeof(int) ); count += sizeof(int);
|
||||
memcpy(buffer+count, &request.data[i].spd, sizeof(int) ); count += sizeof(int);
|
||||
}
|
||||
|
||||
|
||||
/* [4] Envoi de la requête
|
||||
=========================================================*/
|
||||
/* 1. Gestion de l'envoi en plusieurs requêtes */
|
||||
|
||||
/* 2. Envoi */
|
||||
|
||||
len = sizeof(struct sockaddr_in);
|
||||
read = recvfrom(arg->socket, request, MAX_BUF_LEN, 0, (struct sockaddr*) &clientInfo, &len);
|
||||
sent = sendto(arg->socket, buffer, count/sizeof(char), 0, (struct sockaddr*) &clientInfo, len);
|
||||
|
||||
/* 2. Si erreur reception */
|
||||
if( DEBUGMOD&BUF ) printf("{udp_x-term}{udp_com} READ = %d\n", read);
|
||||
/* 3. Gestion erreur */
|
||||
if( sent <= 0 ){
|
||||
printf("{udp_vterm}{com}(%d) Unable to send data\n", index);
|
||||
break;
|
||||
}
|
||||
|
||||
if( read < 0 )
|
||||
continue;
|
||||
|
||||
/* 3. On désérialise la requête*/
|
||||
printf("{udp_x-term}{udp_com} TERMINAL Request(%d bytes) : '%s'\n", read, request);
|
||||
|
||||
/* [3] Gestion de la requête
|
||||
/* [5] Timeout
|
||||
=========================================================*/
|
||||
sleep(PUBL_TIMEOUT);
|
||||
|
||||
}
|
||||
|
||||
/* [4] Envoi reponse
|
||||
=========================================================*/
|
||||
strcpy(request+strlen(request), "-bla\0");
|
||||
send(arg->socket, request, strlen(request), 0);
|
||||
|
||||
|
||||
}while( 0 );
|
||||
|
||||
/* [n] Arrêt du THREAD
|
||||
============================================================================*/
|
||||
/* 1. On récupère le rang dans les "managers" */
|
||||
int i, index = -1;
|
||||
for( i = 0 ; i < MAX_UDP_THR ; i++ )
|
||||
if( arg->managers[i] == pthread_self() ){ index = i; break; }
|
||||
|
||||
/* 2. On met à jour "activeManagers" */
|
||||
if( index != -1 )
|
||||
arg->activeManagers[index] = 0;
|
||||
|
||||
/* 3. On arrête le THREAD */
|
||||
if( DEBUGMOD&THR ) printf("{udp_x-term}{udp_com}(%d) libéré\n", index);
|
||||
if( DEBUGMOD&THR ) printf("{udp_vterm}{com}(%d) libéré\n", index);
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
|
|
|
@ -102,7 +102,7 @@ int multicastTerminal(struct middleware_arg* arg){
|
|||
}else{
|
||||
strcpy(request.addr, SERV_HOST);
|
||||
request.port = comInfo.sin_port;
|
||||
if( DEBUGMOD&SCK ) printf("{%s}{udp_com} socket opened on %s:%d\n", arg->entity, request.addr, ntohs(request.port));
|
||||
if( DEBUGMOD&SCK ) printf("{%s}{com} socket opened on %s:%d\n", arg->entity, request.addr, ntohs(request.port));
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue