[done] Gestion com plane 2-sides : envoi des données - [todo] gestion tampon + update avec flags

This commit is contained in:
xdrm-brackets 2017-04-12 09:57:59 +02:00
parent e1e8d56d0c
commit 791ca5ab09
6 changed files with 95 additions and 16 deletions

View File

@ -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{

View File

@ -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
=========================================================*/

View File

@ -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

View File

@ -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
=========================================================*/

View File

@ -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;
}

View File

@ -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