From 791ca5ab09a146ca46372a5ef73ff55205cada65 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 12 Apr 2017 09:57:59 +0200 Subject: [PATCH] =?UTF-8?q?[done]=20Gestion=20com=20plane=202-sides=20:=20?= =?UTF-8?q?envoi=20des=20donn=C3=A9es=20-=20[todo]=20gestion=20tampon=20+?= =?UTF-8?q?=20update=20avec=20flags?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- central-manager/lib/header.h | 2 +- central-manager/lib/local/middleware.c | 11 +++-- plane/lib/header.h | 2 + plane/lib/network/udp/client.c | 10 ++++ plane/plane.c | 65 ++++++++++++++++++++++---- plane/plane.h | 21 +++++++++ 6 files changed, 95 insertions(+), 16 deletions(-) diff --git a/central-manager/lib/header.h b/central-manager/lib/header.h index 82788ad..c77dca5 100644 --- a/central-manager/lib/header.h +++ b/central-manager/lib/header.h @@ -52,7 +52,7 @@ #define maxHostLen 64 #define maxPortLen 6 - #define SOCK_TIMEOUT 2 // 2sec timeout + #define SOCK_TIMEOUT 3 // 3sec timeout (1+ temps refresh plane) struct context{ diff --git a/central-manager/lib/local/middleware.c b/central-manager/lib/local/middleware.c index a358013..51a7c2b 100644 --- a/central-manager/lib/local/middleware.c +++ b/central-manager/lib/local/middleware.c @@ -47,13 +47,14 @@ int multicastPlanes(struct middleware_arg* arg){ /* (1) Envoi socket de communication (TCP) ---------------------------------------------------------*/ - /* 1. Si on veut un port de communicatin */ - if( request.flags&BINDHEAD_SCK ){ + /* 1. Si flag manquant -> on quitte */ + if( !(request.flags&BINDHEAD_TCP) ) + return -1; - strcpy(request.addr, SERV_HOST); - request.port = TCP_LIST; - } + + strcpy(request.addr, SERV_HOST); + request.port = TCP_LIST; /* [2] Envoi de la réponse =========================================================*/ diff --git a/plane/lib/header.h b/plane/lib/header.h index 710f628..68112c5 100644 --- a/plane/lib/header.h +++ b/plane/lib/header.h @@ -32,6 +32,8 @@ #define MCST_HOST "224.0.0.1" // adresse groupe multicast UDP #define MCST_PORT 4444 // port multicast UDP + #define SOCK_TIMEOUT 3 + #define MAX_BUF_LEN 512 diff --git a/plane/lib/network/udp/client.c b/plane/lib/network/udp/client.c index 3c6b1b1..3bf0ee8 100644 --- a/plane/lib/network/udp/client.c +++ b/plane/lib/network/udp/client.c @@ -7,6 +7,7 @@ int UDP_SOCKET(int* pSocket, const char* pAddr, const int pPort, struct sockaddr /* [0] Initialisation des variables =========================================================*/ *pSocket = -1; + struct timeval timeout; /* [1] Création de la socket @@ -18,6 +19,15 @@ int UDP_SOCKET(int* pSocket, const char* pAddr, const int pPort, struct sockaddr if( *pSocket < 0 ) return -1; + /* 3. Timeout */ + timeout.tv_sec = SOCK_TIMEOUT; + timeout.tv_usec = 0; + + if( setsockopt(*pSocket, SOL_SOCKET, SO_RCVTIMEO|SO_SNDTIMEO, (char*) &timeout, sizeof(struct timeval) ) < 0 ){ + close(*pSocket); + return -1; + } + /* [2] On définit les infos de la socket =========================================================*/ diff --git a/plane/plane.c b/plane/plane.c index ed495d1..718517b 100644 --- a/plane/plane.c +++ b/plane/plane.c @@ -29,10 +29,8 @@ int open_communication(){ /* 0. Initialisation des variables */ struct sockaddr_in udp, tcp; // données des sockets char buffer[MAX_BUF_LEN] = {0}; // buffer - unsigned short serverPort; // port TCP donné par requête multicast UDP - char serverAddr[INET_ADDRSTRLEN+1]; // adresse TCP donné par requête multicast UDP int status; - const int REQ_SIZE = sizeof(char)*INET_ADDRSTRLEN+sizeof(unsigned short); + struct bind_header request; /* [1] Socket Multicast UDP =========================================================*/ @@ -48,10 +46,15 @@ int open_communication(){ ---------------------------------------------------------*/ /* 1. Construction séquence d'initialisation */ // memcpy(buffer, numero_vol, sizeof(char)*6); - strcpy(buffer, "\003000.000.000.000\x01\x00"); + request.flags = BINDHEAD_TCP; + strcpy(request.addr, "000.000.000.000"); + request.port = 0; + memcpy(buffer+sizeof(char)*0, &request.flags, sizeof(char)); + memcpy(buffer+sizeof(char)*1, &request.addr, sizeof(char)*INET_ADDRSTRLEN); + memcpy(buffer+sizeof(char)*(1+INET_ADDRSTRLEN), &request.port, sizeof(unsigned short)); /* 2. Envoi séquence */ - if( sendto(mcast_socket, buffer, REQ_SIZE+1, 0, (struct sockaddr*) &udp, sizeof(struct sockaddr_in)) < 0 ){ + if( sendto(mcast_socket, buffer, BINDHDR_LEN/sizeof(char) +1, 0, (struct sockaddr*) &udp, sizeof(struct sockaddr_in)) < 0 ){ close(mcast_socket); return 0; } @@ -69,16 +72,19 @@ int open_communication(){ } /* 3. Vérification taille */ - if( status < REQ_SIZE ){ + if( status < BINDHDR_LEN ){ close(mcast_socket); return 0; } /* 4. On récupère les données */ - memcpy(serverAddr, buffer+sizeof(char), sizeof(char)*INET_ADDRSTRLEN ); - memcpy(&serverPort, buffer+sizeof(char)*(INET_ADDRSTRLEN+1), sizeof(unsigned short) ); + bzero(&request, sizeof(struct bind_header)); - printf("bind_header{flags = %d; addr = %s; port = %d;}\n\n", (int) buffer[0], serverAddr, serverPort); + memcpy(&request.addr, buffer, sizeof(char) ); + memcpy(&request.addr, buffer+sizeof(char), sizeof(char)*INET_ADDRSTRLEN ); + memcpy(&request.port, buffer+sizeof(char)*(1+INET_ADDRSTRLEN), sizeof(unsigned short) ); + + printf("bind_header{flags = %d; addr = %s; port = %d;}\n\n", request.flags, request.addr, request.port); @@ -91,7 +97,7 @@ int open_communication(){ /* (1) Création socket TCP + connection ---------------------------------------------------------*/ /* 1. Création socket TCP + connection serveur */ - if( TCP_CONNECT(&commu_socket, serverAddr, serverPort, &tcp) < 0 ) + if( TCP_CONNECT(&commu_socket, request.addr, request.port, &tcp) < 0 ) return 0; return 1; @@ -109,10 +115,46 @@ void send_data(){ /* [0] Initialisation des variables =========================================================*/ + char buffer[MAX_BUF_LEN] = {0}; + struct plane_data request; + int read; + /* [1] Envoi des caractéristiques + =========================================================*/ + /* 1. Vérification socket */ + if( commu_socket < 0 ) + return; + /* 2. création objet */ + strcpy(request.code, numero_vol); + request.x = crd.x; + request.y = crd.y; + request.z = crd.z; + request.cap = ctrl.cap; + request.spd = 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) ); + + read = send(commu_socket, buffer, PLANE_DATA_LEN/sizeof(char) + 1, 0); + + if( read < 0 ){ + printf("Erreur d'envoi\n"); + return; + } + + /* 4. Récupération réponse */ + //TODO: recv() + + /* 5. Gestion mise à jour */ + //TODO: update_cap etc avec flags } @@ -139,6 +181,9 @@ void init_plane(){ // formée de 2 lettres puis 3 chiffres numero_vol[0] = (random() % 26) + 'A'; numero_vol[1] = (random() % 26) + 'A'; + numero_vol[2] = (random() % 10) + '0'; + numero_vol[3] = (random() % 10) + '0'; + numero_vol[4] = (random() % 10) + '0'; numero_vol[5] = 0; } diff --git a/plane/plane.h b/plane/plane.h index 723c834..7ea90b0 100644 --- a/plane/plane.h +++ b/plane/plane.h @@ -39,4 +39,25 @@ int speed; }; + #define PLANE_DATA_LEN (sizeof(char)*6+sizeof(int)*5) + struct plane_data{ + char code[6]; + int x; + int y; + int z; + int cap; + int spd; + }; + + #define BINDHDR_LEN ( sizeof(char)*(1+INET_ADDRSTRLEN)+sizeof(unsigned short) ) + #define BINDHEAD_CTL 0x01 // is command terminal (else: view terminal) + #define BINDHEAD_SCK 0x02 // ask for com socket + #define BINDHEAD_TCP 0x04 // ask TCP instead of UDP + + struct bind_header{ + char flags; + char addr[INET_ADDRSTRLEN]; + unsigned short port; + }; + #endif \ No newline at end of file