Cleaned context structure
This commit is contained in:
parent
c5e59ccbd8
commit
6cfa351c9a
|
@ -35,15 +35,14 @@ 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.unit = (struct context_unit*) malloc( sizeof(struct context_unit) );
|
||||
struct in_addr* SERV_HOST;
|
||||
|
||||
/* 2. On récupère l'IP locale' */
|
||||
SERV_HOST = GET_LOCAL_IP();
|
||||
if( SERV_HOST == NULL ){
|
||||
printf("/!\\ Cannot fetch local ip address. Aborting!\n");
|
||||
free(sgca.data);
|
||||
free(sgca.unit);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
printf("LOCAL IP: %s\n", inet_ntoa(*SERV_HOST));
|
||||
|
@ -82,8 +81,7 @@ int main(int argc, char* argv[]){
|
|||
|
||||
/* [3] On libère les variables globales
|
||||
==========================================================*/
|
||||
free(sgca.data);
|
||||
free(sgca.socket);
|
||||
free(sgca.unit);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
|
@ -55,11 +55,14 @@
|
|||
#define PUBL_TIMEOUT 2 // 2sec entre chaque publication sur multicast UDP (pour avions)
|
||||
|
||||
|
||||
struct context_unit{
|
||||
int socket; // socket associée à un avion
|
||||
struct plane data; // données d'un avion
|
||||
};
|
||||
|
||||
struct context{
|
||||
unsigned int n; // nombre d'avions
|
||||
int* socket; // sockets associées aux avions
|
||||
struct plane* data; // buffer tes données avion
|
||||
struct plane_update* request; // Stack des requêtes de modifications
|
||||
struct context_unit* unit; // les données des avions
|
||||
};
|
||||
|
||||
struct middleware_arg{
|
||||
|
|
|
@ -130,7 +130,7 @@ void* managePlane(void* THREADABLE_ARGS){
|
|||
for( i = 0 ; i < arg->sgca->n ; i++ ){
|
||||
|
||||
// Si l'avion existe -> on passe à la suite
|
||||
if( strcmp(arg->sgca->data[i].code, data.code) == 0 ){
|
||||
if( strcmp(arg->sgca->unit[i].data.code, data.code) == 0 ){
|
||||
pindex = i;
|
||||
break;
|
||||
}
|
||||
|
@ -145,17 +145,16 @@ void* managePlane(void* THREADABLE_ARGS){
|
|||
// On ajoute une entrée à data + socket
|
||||
pindex = arg->sgca->n;
|
||||
arg->sgca->n++;
|
||||
arg->sgca->data = (struct plane*) realloc(arg->sgca->data, sizeof(struct plane)*arg->sgca->n + 1);
|
||||
arg->sgca->socket = (int*) realloc(arg->sgca->socket, sizeof(struct plane)*arg->sgca->n + 1);
|
||||
arg->sgca->socket[pindex] = SOCKET;
|
||||
arg->sgca->unit = (struct context_unit*) realloc(arg->sgca->unit, sizeof(struct context_unit)*arg->sgca->n + 1);
|
||||
arg->sgca->unit[pindex].socket = SOCKET;
|
||||
printf("{tcp_com}(%d) plane '%s' (#%d) created\n", index, data.code, pindex);
|
||||
|
||||
}
|
||||
|
||||
/* 4. On copie/met à jour les valeurs */
|
||||
memcpy(&arg->sgca->data[pindex], &data, sizeof(struct plane));
|
||||
memcpy(&arg->sgca->unit[pindex].data, &data, sizeof(struct plane));
|
||||
|
||||
if( DEBUGMOD&COM ) 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);
|
||||
if( DEBUGMOD&COM ) printf("{tcp_com}(%d) stored (%d)'%s': {x = %d; y = %d; z = %d; cap = %d; spd = %d}\n", index, pindex, arg->sgca->unit[pindex].data.code, arg->sgca->unit[pindex].data.x, arg->sgca->unit[pindex].data.y, arg->sgca->unit[pindex].data.z, arg->sgca->unit[pindex].data.cap, arg->sgca->unit[pindex].data.spd);
|
||||
|
||||
}
|
||||
|
||||
|
@ -163,8 +162,8 @@ void* managePlane(void* THREADABLE_ARGS){
|
|||
/* [5] On notifie que l'avoin est déconnecté (crash)
|
||||
=========================================================*/
|
||||
/* 1. On notifie le crash */
|
||||
arg->sgca->data[pindex].online = 0;
|
||||
arg->sgca->socket[pindex] = -1;
|
||||
arg->sgca->unit[pindex].data.online = 0;
|
||||
arg->sgca->unit[pindex].socket = -1;
|
||||
|
||||
|
||||
|
||||
|
@ -407,7 +406,7 @@ void* manageCtrlTerm(void* THREADABLE_ARGS){
|
|||
for( i = 0 ; i < arg->sgca->n ; i++ ){
|
||||
|
||||
/* 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 ){
|
||||
if( strcmp(arg->sgca->unit[i].data.code, request.update.code) == 0 && arg->sgca->unit[i].data.online ){
|
||||
pindex = i;
|
||||
break;
|
||||
}
|
||||
|
@ -417,7 +416,7 @@ void* manageCtrlTerm(void* THREADABLE_ARGS){
|
|||
|
||||
|
||||
/* 3. Si on a pas trouvé -> on quitte l'udpdate */
|
||||
if( pindex <= -1 || arg->sgca->socket[pindex] <= -1 ){
|
||||
if( pindex <= -1 || arg->sgca->unit[pindex].socket <= -1 ){
|
||||
printf("{udp_cterm}{com}(%d) Plane unknown or unreachable, passing\n", index);
|
||||
break;
|
||||
}
|
||||
|
@ -435,7 +434,7 @@ void* manageCtrlTerm(void* THREADABLE_ARGS){
|
|||
count += last;
|
||||
|
||||
/* 3. On envoie la requête à l'avion */
|
||||
if( send(arg->sgca->socket[pindex], buffer, count/sizeof(char), 0) <= 0 ){
|
||||
if( send(arg->sgca->unit[pindex].socket, buffer, count/sizeof(char), 0) <= 0 ){
|
||||
printf("{udp_cterm}{com}(%d) Cannot send request to plane\n", index);
|
||||
break;
|
||||
}
|
||||
|
@ -444,7 +443,7 @@ void* manageCtrlTerm(void* THREADABLE_ARGS){
|
|||
/* (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);
|
||||
count = recv(arg->sgca->unit[pindex].socket, buffer, sizeof(char), 0);
|
||||
|
||||
if( count != sizeof(char) ){
|
||||
printf("{udp_cterm}{com}(%d) Cannot get response from plane %d (%d)\n", index, count, (int) PLANE_LEN);
|
||||
|
@ -552,13 +551,13 @@ void getPlaneData(char** pBuffer, size_t* pLen, struct context* pContext){
|
|||
/* 2. On récupère la liste des avions (network byte order) */
|
||||
for( i = 0 ; i < nb ; i++ ){
|
||||
|
||||
memcpy(&response.data[i].code, &pContext->data[i].code, sizeof(char)*6);
|
||||
response.data[i].x = htonl( pContext->data[i].x );
|
||||
response.data[i].y = htonl( pContext->data[i].y );
|
||||
response.data[i].z = htonl( pContext->data[i].z );
|
||||
response.data[i].cap = htonl( pContext->data[i].cap );
|
||||
response.data[i].spd = htonl( pContext->data[i].spd );
|
||||
response.data[i].online = pContext->data[i].online;
|
||||
memcpy(&response.data[i].code, &pContext->unit[i].data.code, sizeof(char)*6);
|
||||
response.data[i].x = htonl( pContext->unit[i].data.x );
|
||||
response.data[i].y = htonl( pContext->unit[i].data.y );
|
||||
response.data[i].z = htonl( pContext->unit[i].data.z );
|
||||
response.data[i].cap = htonl( pContext->unit[i].data.cap );
|
||||
response.data[i].spd = htonl( pContext->unit[i].data.spd );
|
||||
response.data[i].online = pContext->unit[i].data.online;
|
||||
|
||||
// Incrément du compte si online
|
||||
response.n += response.data[i].online;
|
||||
|
|
Loading…
Reference in New Issue