Separated code between `main`, `middlewares` which creates a new socket (to do for UDP : copypaste the code from main to middlewares), and `handlers` which have the socket and do the actual management (communications)
This commit is contained in:
parent
02989312d1
commit
63436afc35
|
@ -12,10 +12,16 @@ lib/network/tcp/server.o: lib/header.h lib/network/tcp/server.h lib/network/tcp/
|
||||||
lib/network/udp/server.o: lib/header.h lib/network/udp/server.h lib/network/udp/server.c
|
lib/network/udp/server.o: lib/header.h lib/network/udp/server.h lib/network/udp/server.c
|
||||||
gcc $(CFLAGS) -c -o lib/network/udp/server.o lib/network/udp/server.c
|
gcc $(CFLAGS) -c -o lib/network/udp/server.o lib/network/udp/server.c
|
||||||
|
|
||||||
|
lib/local/middleware.o: lib/header.h lib/local/middleware.h lib/local/middleware.c
|
||||||
|
gcc $(CFLAGS) -c -o lib/local/middleware.o lib/local/middleware.c
|
||||||
|
|
||||||
|
lib/local/handler.o: lib/header.h lib/local/handler.h lib/local/handler.c
|
||||||
|
gcc $(CFLAGS) -c -o lib/local/handler.o lib/local/handler.c
|
||||||
|
|
||||||
|
|
||||||
# Compiles the SGCA
|
# Compiles the SGCA
|
||||||
boot: lib/network/tcp/server.o lib/network/udp/server.o central-manager.h central-manager.c
|
boot: lib/network/tcp/server.o lib/network/udp/server.o lib/local/middleware.o lib/local/handler.o central-manager.h central-manager.c
|
||||||
gcc $(CFLAGS) -o boot lib/network/udp/server.o lib/network/tcp/server.o central-manager.c
|
gcc $(CFLAGS) -o boot lib/network/udp/server.o lib/network/tcp/server.o lib/local/middleware.o lib/local/handler.o central-manager.c
|
||||||
|
|
||||||
|
|
||||||
# Run full compilation
|
# Run full compilation
|
||||||
|
@ -25,3 +31,4 @@ all: boot
|
||||||
clean:
|
clean:
|
||||||
rm boot;
|
rm boot;
|
||||||
rm lib/network/**/*.o;
|
rm lib/network/**/*.o;
|
||||||
|
rm lib/local/*.o;
|
||||||
|
|
|
@ -33,16 +33,14 @@ int main(int argc, char* argv[]){
|
||||||
/* [0] Initialisation des variables
|
/* [0] Initialisation des variables
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* 1. Variables globales */
|
/* 1. Variables globales */
|
||||||
sgca_data = (struct plane*) malloc( sizeof(struct plane) );
|
sgca.n = 0;
|
||||||
sgca_update = (struct plane*) malloc( sizeof(struct plane) );
|
sgca.data = (struct plane*) malloc( sizeof(struct plane) );
|
||||||
sgca_index.data = 0;
|
|
||||||
sgca_index.update = 0;
|
|
||||||
|
|
||||||
/* 2. Variables locales */
|
/* 2. Variables locales */
|
||||||
struct listn_thrd_arg tcp_listn_arg = { SERV_HOST, TCP_LIST, &managePlane };
|
struct listn_thrd_arg tcp_listn_arg = { SERV_HOST, TCP_LIST, NULL, &managePlane };
|
||||||
struct listn_thrd_arg udp_mcast_arg = { MCST_HOST, UDP_MCST, &initPlane };
|
struct listn_thrd_arg udp_mcast_arg = { MCST_HOST, UDP_MCST, NULL, &dispatchPlanes };
|
||||||
struct listn_thrd_arg udp_vterm_arg = { MCST_VTER, UDP_VTER, &manageViewTerm };
|
struct listn_thrd_arg udp_vterm_arg = { MCST_VTER, UDP_VTER, NULL, &manageViewTerm };
|
||||||
struct listn_thrd_arg udp_cterm_arg = { MCST_CTER, UDP_CTER, &manageCtrlTerm };
|
struct listn_thrd_arg udp_cterm_arg = { MCST_CTER, UDP_CTER, NULL, &manageCtrlTerm };
|
||||||
|
|
||||||
|
|
||||||
/* [1] Lancement des THREADS d'écoute
|
/* [1] Lancement des THREADS d'écoute
|
||||||
|
@ -72,8 +70,7 @@ int main(int argc, char* argv[]){
|
||||||
|
|
||||||
/* [3] On libère les variables globales
|
/* [3] On libère les variables globales
|
||||||
==========================================================*/
|
==========================================================*/
|
||||||
free(sgca_data);
|
free(sgca.data);
|
||||||
free(sgca_update);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -91,14 +88,14 @@ int main(int argc, char* argv[]){
|
||||||
* [4] On ferme la SOCKET d'écoute TCP globale
|
* [4] On ferme la SOCKET d'écoute TCP globale
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
void* LISTEN_TCP(void* THREADABLE_ARG){
|
void* LISTEN_TCP(void* THREADABLE_ARGS){
|
||||||
/* [0] Initialisation des variables
|
/* [0] Initialisation des variables
|
||||||
==========================================================*/
|
==========================================================*/
|
||||||
int CLIENT_SOCKET; // contiendra la socket TCP à envoyer sur un THREAD
|
int CLIENT_SOCKET; // contiendra la socket TCP à envoyer sur un THREAD
|
||||||
struct sockaddr_in clientInfo; // contiendra les infos client
|
struct sockaddr_in clientInfo; // contiendra les infos client
|
||||||
socklen_t len; // taille de la socket
|
socklen_t len; // taille de la socket
|
||||||
int index, i; // compteurs
|
int index, i; // compteurs
|
||||||
struct listn_thrd_arg* arg = THREADABLE_ARG; // Addr + Port serveur
|
struct listn_thrd_arg* arg = THREADABLE_ARGS; // Addr + Port serveur
|
||||||
|
|
||||||
|
|
||||||
// retour de @DROP_TCP_SERVER
|
// retour de @DROP_TCP_SERVER
|
||||||
|
@ -151,8 +148,11 @@ void* LISTEN_TCP(void* THREADABLE_ARG){
|
||||||
// si on a trouvé un "manager" libre
|
// si on a trouvé un "manager" libre
|
||||||
if( index != -1 ){
|
if( index != -1 ){
|
||||||
|
|
||||||
|
// Construction arguments thread
|
||||||
|
struct hndlr_thrd_arg thread_args = { TCPManagers, activeTCPManagers, CLIENT_SOCKET, &sgca };
|
||||||
|
|
||||||
/* 5. On lance un thread pour le traitement de ce client */
|
/* 5. On lance un thread pour le traitement de ce client */
|
||||||
pthread_create(&TCPManagers[index], NULL, arg->func, (void*)(intptr_t) CLIENT_SOCKET);
|
pthread_create(&TCPManagers[index], NULL, arg->handler, (void*) &thread_args);
|
||||||
|
|
||||||
if( DEBUGMOD&THR ) printf("{tcp_listn}{com}(%d) démarré\n", index);
|
if( DEBUGMOD&THR ) printf("{tcp_listn}{com}(%d) démarré\n", index);
|
||||||
|
|
||||||
|
@ -202,7 +202,7 @@ void* LISTEN_TCP(void* THREADABLE_ARG){
|
||||||
* [N] On ferme la SOCKET d'écoute globale
|
* [N] On ferme la SOCKET d'écoute globale
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
void* LISTEN_UDP(void* THREADABLE_ARG){
|
void* LISTEN_UDP(void* THREADABLE_ARGS){
|
||||||
/* [0] Initialisation des variables
|
/* [0] Initialisation des variables
|
||||||
==========================================================*/
|
==========================================================*/
|
||||||
int CLIENT_SOCKET; // contiendra la socket UDP à envoyer sur un THREAD
|
int CLIENT_SOCKET; // contiendra la socket UDP à envoyer sur un THREAD
|
||||||
|
@ -214,7 +214,7 @@ void* LISTEN_UDP(void* THREADABLE_ARG){
|
||||||
char buffer[MAX_BUF_LEN]; // buffer requête
|
char buffer[MAX_BUF_LEN]; // buffer requête
|
||||||
struct bind_header request; // requête parsée
|
struct bind_header request; // requête parsée
|
||||||
int i, index; // compteurs
|
int i, index; // compteurs
|
||||||
struct listn_thrd_arg* arg = THREADABLE_ARG; // Addr + Port serveur
|
struct listn_thrd_arg* arg = THREADABLE_ARGS; // Addr + Port serveur
|
||||||
char entity[9+1];
|
char entity[9+1];
|
||||||
|
|
||||||
if( strcmp(arg->addr, MCST_VTER) == 0 && arg->port == UDP_VTER ) strcpy(entity, "udp_vterm");
|
if( strcmp(arg->addr, MCST_VTER) == 0 && arg->port == UDP_VTER ) strcpy(entity, "udp_vterm");
|
||||||
|
@ -341,8 +341,11 @@ void* LISTEN_UDP(void* THREADABLE_ARG){
|
||||||
/* 2. si on a trouvé un "manager" libre */
|
/* 2. si on a trouvé un "manager" libre */
|
||||||
if( index != -1 ){
|
if( index != -1 ){
|
||||||
|
|
||||||
|
// Construction arguments thread
|
||||||
|
struct hndlr_thrd_arg thread_args = { UDPManagers, activeUDPManagers, CLIENT_SOCKET, &sgca };
|
||||||
|
|
||||||
/* 2.1. On lance un thread pour le traitement de ce client */
|
/* 2.1. On lance un thread pour le traitement de ce client */
|
||||||
pthread_create(&UDPManagers[index], NULL, arg->func, (void*)(intptr_t) CLIENT_SOCKET);
|
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}{udp_com}(%d) démarré\n", entity, index);
|
||||||
|
|
||||||
|
@ -362,314 +365,4 @@ void* LISTEN_UDP(void* THREADABLE_ARG){
|
||||||
close(SOCKET);
|
close(SOCKET);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void* initPlane(){
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Gestion d'une connexion PLANE
|
|
||||||
*
|
|
||||||
* @THREADABLE_SOCKET<void*> SOCKET de la connexion client
|
|
||||||
*
|
|
||||||
* @history
|
|
||||||
* [1] Initialisation des variables
|
|
||||||
* @loop
|
|
||||||
* (2) Attente de requête
|
|
||||||
* (3) Gestion de la requête
|
|
||||||
* (4) Envoi de la réponse
|
|
||||||
* [5] On libère la mémoire
|
|
||||||
* [6] Fermeture de la connection (SOCKET)
|
|
||||||
* [n] Arrêt du THREAD
|
|
||||||
* 1. On met à jour "activeManagers"
|
|
||||||
* 2. On arrête le THREAD
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
void* managePlane(void* THREADABLE_SOCKET){
|
|
||||||
|
|
||||||
/* [1] Initialisation des variables
|
|
||||||
=========================================================*/
|
|
||||||
/* 1. Variables utiles */
|
|
||||||
int read, i, index, pindex; // compteurs
|
|
||||||
char buffer[MAX_BUF_LEN]; // buffer
|
|
||||||
struct plane data; // données de l'avion
|
|
||||||
|
|
||||||
/* 2. On récupère la socket */
|
|
||||||
int TCP_SOCKET = (intptr_t) THREADABLE_SOCKET; // Socket client
|
|
||||||
|
|
||||||
/* 3. On récupère le rang du thread parmi les "managers" */
|
|
||||||
index = -1;
|
|
||||||
for( i = 0 ; i < MAX_TCP_THR ; i++ )
|
|
||||||
if( TCPManagers[i] == pthread_self() ){ index = i; break; }
|
|
||||||
|
|
||||||
// Erreur de thread
|
|
||||||
if( index == -1 ){
|
|
||||||
if( DEBUGMOD&THR ) printf("{tcp_com}(%d) Unknown thread index. Aborting\n", index);
|
|
||||||
pthread_exit(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
while( 1 ){
|
|
||||||
|
|
||||||
/* (2) Récupération de la requête
|
|
||||||
---------------------------------------------------------*/
|
|
||||||
/* 1. On lit sur la socket */
|
|
||||||
read = recv(TCP_SOCKET, buffer, MAX_BUF_LEN, 0);
|
|
||||||
|
|
||||||
/* 2.1. Si erreur reception (-1:erreur, 0:fermeture client propre) */
|
|
||||||
if( read <= 0 ){
|
|
||||||
|
|
||||||
if( DEBUGMOD&BUF ) printf("{tcp_com}(%d) read: %d -> must exit thread\n", index, read);
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* 2.2. Si message trop court */
|
|
||||||
if( read < PLANE_LEN ){
|
|
||||||
if( DEBUGMOD&BUF ) printf("{tcp_com}(%d) read: %d (expected: %d)\n", index, read, (int) PLANE_LEN);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* 3. On parse la requête*/
|
|
||||||
memcpy(&data.code, buffer+sizeof(char)*0+sizeof(int)*0, sizeof(char)*6);
|
|
||||||
memcpy(&data.x, buffer+sizeof(char)*6+sizeof(int)*0, sizeof(int));
|
|
||||||
memcpy(&data.y, buffer+sizeof(char)*6+sizeof(int)*1, sizeof(int));
|
|
||||||
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
|
|
||||||
---------------------------------------------------------*/
|
|
||||||
pindex = -1;
|
|
||||||
|
|
||||||
/* 1. On regarde si l'avion existe */
|
|
||||||
for( i = 0 ; i < sgca_index.data ; i++ ){
|
|
||||||
|
|
||||||
pindex = sgca_index.data;
|
|
||||||
|
|
||||||
// On ajoute une entrée à data
|
|
||||||
sgca_data = (struct plane*) realloc(sgca_data, sizeof(struct plane)*sgca_index.data + 1);
|
|
||||||
sgca_index.data++;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 3. On met à jour les données*/
|
|
||||||
memcpy(&sgca_data[pindex], &data, sizeof(struct plane));
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* (4) Envoi de la réponse
|
|
||||||
---------------------------------------------------------*/
|
|
||||||
printf("send: %d\n", (int) send(TCP_SOCKET, "coucou\n", 8, 0));
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* [5] On libère la mémoire
|
|
||||||
=========================================================*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* [6] Fermeture de la connection (SOCKET)
|
|
||||||
=========================================================*/
|
|
||||||
printf("{tcp_com}(%d) Fermeture de la socket de communication!\n", index);
|
|
||||||
close(TCP_SOCKET);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* [n] Arrêt du THREAD
|
|
||||||
============================================================================*/
|
|
||||||
/* 1. On met à jour "activeManagers" */
|
|
||||||
if( index != -1 )
|
|
||||||
activeTCPManagers[index] = 0;
|
|
||||||
|
|
||||||
/* 2. On arrête le THREAD */
|
|
||||||
if( DEBUGMOD&THR ) printf("{tcp_com}(%d) libéré\n", index);
|
|
||||||
pthread_exit(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Gestion d'une connexion TERMINAL
|
|
||||||
*
|
|
||||||
* @THREADABLE_SOCKET<void*> SOCKET de la connexion client
|
|
||||||
*
|
|
||||||
* @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
|
|
||||||
* [6] On vide les buffers
|
|
||||||
* [7] Fermeture de la connection (SOCKET)
|
|
||||||
* [n] Arrêt du THREAD
|
|
||||||
* 1. On récupère le rang dans les "managers"
|
|
||||||
* 2. On met à jour "activeManagers"
|
|
||||||
* 3. On arrête le THREAD
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
void* manageViewTerm(void* THREADABLE_SOCKET){
|
|
||||||
|
|
||||||
/* [1] Initialisation des variables
|
|
||||||
=========================================================*/
|
|
||||||
int read; // compteur
|
|
||||||
struct sockaddr_in clientInfo;
|
|
||||||
socklen_t len;
|
|
||||||
int UDP_SOCKET = (intptr_t) THREADABLE_SOCKET; // Socket client
|
|
||||||
char request[MAX_BUF_LEN]; // Requête
|
|
||||||
// char response[MAX_BUF_LEN]; // Réponse
|
|
||||||
|
|
||||||
do{
|
|
||||||
|
|
||||||
printf("{udp_x-term}{udp_com} waiting for terminal request\n");
|
|
||||||
/* [2] Récupération de la requête
|
|
||||||
=========================================================*/
|
|
||||||
/* 1. On lit sur la socket */
|
|
||||||
len = sizeof(struct sockaddr_in);
|
|
||||||
read = recvfrom(UDP_SOCKET, request, MAX_BUF_LEN, 0, (struct sockaddr*) &clientInfo, &len);
|
|
||||||
|
|
||||||
/* 2. Si erreur reception */
|
|
||||||
if( DEBUGMOD&BUF ) printf("{udp_x-term}{udp_com} READ = %d\n", read);
|
|
||||||
|
|
||||||
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
|
|
||||||
=========================================================*/
|
|
||||||
|
|
||||||
|
|
||||||
/* [4] Envoi reponse
|
|
||||||
=========================================================*/
|
|
||||||
strcpy(request+strlen(request), "-bla\0");
|
|
||||||
send(UDP_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( UDPManagers[i] == pthread_self() ){ index = i; break; }
|
|
||||||
|
|
||||||
/* 2. On met à jour "activeManagers" */
|
|
||||||
if( index != -1 )
|
|
||||||
activeUDPManagers[index] = 0;
|
|
||||||
|
|
||||||
/* 3. On arrête le THREAD */
|
|
||||||
if( DEBUGMOD&THR ) printf("{udp_x-term}{udp_com}(%d) libéré\n", index);
|
|
||||||
pthread_exit(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Gestion d'une connexion TERMINAL
|
|
||||||
*
|
|
||||||
* @THREADABLE_SOCKET<void*> SOCKET de la connexion client
|
|
||||||
*
|
|
||||||
* @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
|
|
||||||
* [6] On vide les buffers
|
|
||||||
* [7] Fermeture de la connection (SOCKET)
|
|
||||||
* [n] Arrêt du THREAD
|
|
||||||
* 1. On récupère le rang dans les "managers"
|
|
||||||
* 2. On met à jour "activeManagers"
|
|
||||||
* 3. On arrête le THREAD
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
void* manageCtrlTerm(void* THREADABLE_SOCKET){
|
|
||||||
|
|
||||||
/* [1] Initialisation des variables
|
|
||||||
=========================================================*/
|
|
||||||
int read; // compteur
|
|
||||||
struct sockaddr_in clientInfo;
|
|
||||||
socklen_t len;
|
|
||||||
int UDP_SOCKET = (intptr_t) THREADABLE_SOCKET; // Socket client
|
|
||||||
char request[MAX_BUF_LEN]; // Requête
|
|
||||||
// char response[MAX_BUF_LEN]; // Réponse
|
|
||||||
|
|
||||||
do{
|
|
||||||
|
|
||||||
printf("{udp_x-term}{udp_com} waiting for terminal request\n");
|
|
||||||
/* [2] Récupération de la requête
|
|
||||||
=========================================================*/
|
|
||||||
/* 1. On lit sur la socket */
|
|
||||||
len = sizeof(struct sockaddr_in);
|
|
||||||
read = recvfrom(UDP_SOCKET, request, MAX_BUF_LEN, 0, (struct sockaddr*) &clientInfo, &len);
|
|
||||||
|
|
||||||
/* 2. Si erreur reception */
|
|
||||||
if( DEBUGMOD&BUF ) printf("{udp_x-term}{udp_com} READ = %d\n", read);
|
|
||||||
|
|
||||||
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
|
|
||||||
=========================================================*/
|
|
||||||
|
|
||||||
|
|
||||||
/* [4] Envoi reponse
|
|
||||||
=========================================================*/
|
|
||||||
strcpy(request+strlen(request), "-bla\0");
|
|
||||||
send(UDP_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( UDPManagers[i] == pthread_self() ){ index = i; break; }
|
|
||||||
|
|
||||||
/* 2. On met à jour "activeManagers" */
|
|
||||||
if( index != -1 )
|
|
||||||
activeUDPManagers[index] = 0;
|
|
||||||
|
|
||||||
/* 3. On arrête le THREAD */
|
|
||||||
if( DEBUGMOD&THR ) printf("{udp_x-term}{udp_com}(%d) libéré\n", index);
|
|
||||||
pthread_exit(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -14,15 +14,13 @@
|
||||||
/* local dependencies */
|
/* local dependencies */
|
||||||
#include "lib/network/tcp/server.h"
|
#include "lib/network/tcp/server.h"
|
||||||
#include "lib/network/udp/server.h"
|
#include "lib/network/udp/server.h"
|
||||||
|
#include "lib/local/middleware.h"
|
||||||
|
#include "lib/local/handler.h"
|
||||||
|
|
||||||
|
|
||||||
/* headers */
|
/* headers */
|
||||||
void* LISTEN_TCP(void* THREADABLE_ARG);
|
void* LISTEN_TCP(void* THREADABLE_ARG);
|
||||||
void* LISTEN_UDP(void* THREADABLE_ARG);
|
void* LISTEN_UDP(void* THREADABLE_ARG);
|
||||||
void* managePlane(void* THREADABLE_SOCKET);
|
|
||||||
void* initPlane();
|
|
||||||
void* manageViewTerm(void* THREADABLE_SOCKET);
|
|
||||||
void* manageCtrlTerm(void* THREADABLE_SOCKET);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -42,18 +40,5 @@ static pthread_t UDPManagers[MAX_UDP_THR]; // contiendra les THREADS UDP
|
||||||
static int activeUDPManagers[MAX_UDP_THR] = {0}; // contiendra les THREADS UDP actifs
|
static int activeUDPManagers[MAX_UDP_THR] = {0}; // contiendra les THREADS UDP actifs
|
||||||
|
|
||||||
|
|
||||||
struct listn_thrd_arg{
|
|
||||||
const char addr[16]; // socket address
|
|
||||||
const unsigned short port; // socket port
|
|
||||||
void* (*func)(); // management function
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// ENREGISTREMENT DES DONNEES AVIONS
|
// ENREGISTREMENT DES DONNEES AVIONS
|
||||||
struct{
|
struct context sgca;
|
||||||
unsigned int data;
|
|
||||||
unsigned int update;
|
|
||||||
} sgca_index;
|
|
||||||
|
|
||||||
struct plane* sgca_data;
|
|
||||||
struct plane* sgca_update;
|
|
||||||
|
|
|
@ -54,4 +54,28 @@
|
||||||
|
|
||||||
#define SOCK_TIMEOUT 2 // 2sec timeout
|
#define SOCK_TIMEOUT 2 // 2sec timeout
|
||||||
|
|
||||||
|
|
||||||
|
struct context{
|
||||||
|
unsigned int n; // nombre d'avions
|
||||||
|
struct plane* data; // buffer tes données avion
|
||||||
|
};
|
||||||
|
|
||||||
|
struct listn_thrd_arg{
|
||||||
|
const char addr[16]; // socket address
|
||||||
|
const unsigned short port; // socket port
|
||||||
|
void* (*middleware)(); // Middleware (redirection etc)
|
||||||
|
void* (*handler)(); // management function
|
||||||
|
};
|
||||||
|
|
||||||
|
struct midwr_arg{
|
||||||
|
int bla;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct hndlr_thrd_arg{
|
||||||
|
pthread_t* managers;
|
||||||
|
int* activeManagers;
|
||||||
|
int socket;
|
||||||
|
struct context* sgca;
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -0,0 +1,319 @@
|
||||||
|
/**************************
|
||||||
|
* Handler Dependency *
|
||||||
|
***************************
|
||||||
|
* Designed & Developed by *
|
||||||
|
* Adrien Marquès *
|
||||||
|
* <xdrm-brackets> *
|
||||||
|
***************************
|
||||||
|
* doowap31@gmail.com *
|
||||||
|
**************************/
|
||||||
|
#include "handler.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Gestion d'une connexion PLANE
|
||||||
|
*
|
||||||
|
* @THREADABLE_SOCKET<void*> SOCKET de la connexion client
|
||||||
|
*
|
||||||
|
* @history
|
||||||
|
* [1] Initialisation des variables
|
||||||
|
* @loop
|
||||||
|
* (2) Attente de requête
|
||||||
|
* (3) Gestion de la requête
|
||||||
|
* (4) Envoi de la réponse
|
||||||
|
* [5] On libère la mémoire
|
||||||
|
* [6] Fermeture de la connection (SOCKET)
|
||||||
|
* [n] Arrêt du THREAD
|
||||||
|
* 1. On met à jour "activeManagers"
|
||||||
|
* 2. On arrête le THREAD
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void* managePlane(void* THREADABLE_ARGS){
|
||||||
|
|
||||||
|
/* [1] Initialisation des variables
|
||||||
|
=========================================================*/
|
||||||
|
/* 1. Variables utiles */
|
||||||
|
int read, i, index, pindex; // compteurs
|
||||||
|
char buffer[MAX_BUF_LEN]; // buffer
|
||||||
|
struct plane data; // données de l'avion
|
||||||
|
|
||||||
|
|
||||||
|
/* 2. On récupère les arguments */
|
||||||
|
struct hndlr_thrd_arg* arg = THREADABLE_ARGS;
|
||||||
|
|
||||||
|
/* 3. On récupère le rang du thread parmi les "managers" */
|
||||||
|
index = -1;
|
||||||
|
for( i = 0 ; i < MAX_TCP_THR ; i++ )
|
||||||
|
if( arg->managers[i] == pthread_self() ){ index = i; break; }
|
||||||
|
|
||||||
|
// Erreur de thread
|
||||||
|
if( index == -1 ){
|
||||||
|
if( DEBUGMOD&THR ) printf("{tcp_com}(%d) Unknown thread index. Aborting\n", index);
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
while( 1 ){
|
||||||
|
|
||||||
|
/* (2) Récupération de la requête
|
||||||
|
---------------------------------------------------------*/
|
||||||
|
/* 1. On lit sur la socket */
|
||||||
|
read = recv(arg->socket, buffer, MAX_BUF_LEN, 0);
|
||||||
|
|
||||||
|
/* 2.1. Si erreur reception (-1:erreur, 0:fermeture client propre) */
|
||||||
|
if( read <= 0 ){
|
||||||
|
if( DEBUGMOD&BUF ) printf("{tcp_com}(%d) read: %d -> must exit thread\n", index, read);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* 2.2. Si message trop court */
|
||||||
|
if( read < PLANE_LEN ){
|
||||||
|
if( DEBUGMOD&BUF ) printf("{tcp_com}(%d) read: %d (expected: %d)\n", index, read, (int) PLANE_LEN);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* 3. On parse la requête*/
|
||||||
|
memcpy(&data.code, buffer+sizeof(char)*0+sizeof(int)*0, sizeof(char)*6);
|
||||||
|
memcpy(&data.x, buffer+sizeof(char)*6+sizeof(int)*0, sizeof(int));
|
||||||
|
memcpy(&data.y, buffer+sizeof(char)*6+sizeof(int)*1, sizeof(int));
|
||||||
|
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
|
||||||
|
---------------------------------------------------------*/
|
||||||
|
pindex = -1;
|
||||||
|
|
||||||
|
/* 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++;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 3. On met à jour les données*/
|
||||||
|
memcpy(&arg->sgca->data[pindex], &data, sizeof(struct plane));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* (4) Envoi de la réponse
|
||||||
|
---------------------------------------------------------*/
|
||||||
|
printf("send: %d\n", (int) send(arg->socket, "coucou\n", 8, 0));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* [5] On libère la mémoire
|
||||||
|
=========================================================*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* [6] Fermeture de la connection (SOCKET)
|
||||||
|
=========================================================*/
|
||||||
|
printf("{tcp_com}(%d) Fermeture de la socket de communication!\n", index);
|
||||||
|
close(arg->socket);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* [n] Arrêt du THREAD
|
||||||
|
============================================================================*/
|
||||||
|
/* 1. On met à jour "activeManagers" */
|
||||||
|
if( index != -1 )
|
||||||
|
arg->activeManagers[index] = 0;
|
||||||
|
|
||||||
|
/* 2. On arrête le THREAD */
|
||||||
|
if( DEBUGMOD&THR ) printf("{tcp_com}(%d) libéré\n", index);
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void* dispatchPlanes(void* THREADABLE_ARGS){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Gestion d'une connexion TERMINAL
|
||||||
|
*
|
||||||
|
* @THREADABLE_SOCKET<void*> SOCKET de la connexion client
|
||||||
|
*
|
||||||
|
* @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
|
||||||
|
* [6] On vide les buffers
|
||||||
|
* [7] Fermeture de la connection (SOCKET)
|
||||||
|
* [n] Arrêt du THREAD
|
||||||
|
* 1. On récupère le rang dans les "managers"
|
||||||
|
* 2. On met à jour "activeManagers"
|
||||||
|
* 3. On arrête le THREAD
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void* manageViewTerm(void* THREADABLE_ARGS){
|
||||||
|
|
||||||
|
/* [1] Initialisation des variables
|
||||||
|
=========================================================*/
|
||||||
|
int read; // compteur
|
||||||
|
struct sockaddr_in clientInfo;
|
||||||
|
socklen_t len;
|
||||||
|
char request[MAX_BUF_LEN]; // Requête
|
||||||
|
// char response[MAX_BUF_LEN]; // Réponse
|
||||||
|
|
||||||
|
/* 2. On récupère les arguments */
|
||||||
|
struct hndlr_thrd_arg* arg = THREADABLE_ARGS;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
do{
|
||||||
|
|
||||||
|
printf("{udp_x-term}{udp_com} waiting for terminal request\n");
|
||||||
|
/* [2] Récupération de la requête
|
||||||
|
=========================================================*/
|
||||||
|
/* 1. On lit sur la socket */
|
||||||
|
len = sizeof(struct sockaddr_in);
|
||||||
|
read = recvfrom(arg->socket, request, MAX_BUF_LEN, 0, (struct sockaddr*) &clientInfo, &len);
|
||||||
|
|
||||||
|
/* 2. Si erreur reception */
|
||||||
|
if( DEBUGMOD&BUF ) printf("{udp_x-term}{udp_com} READ = %d\n", read);
|
||||||
|
|
||||||
|
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
|
||||||
|
=========================================================*/
|
||||||
|
|
||||||
|
|
||||||
|
/* [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);
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Gestion d'une connexion TERMINAL
|
||||||
|
*
|
||||||
|
* @THREADABLE_SOCKET<void*> SOCKET de la connexion client
|
||||||
|
*
|
||||||
|
* @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
|
||||||
|
* [6] On vide les buffers
|
||||||
|
* [7] Fermeture de la connection (SOCKET)
|
||||||
|
* [n] Arrêt du THREAD
|
||||||
|
* 1. On récupère le rang dans les "managers"
|
||||||
|
* 2. On met à jour "activeManagers"
|
||||||
|
* 3. On arrête le THREAD
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void* manageCtrlTerm(void* THREADABLE_ARGS){
|
||||||
|
|
||||||
|
/* [1] Initialisation des variables
|
||||||
|
=========================================================*/
|
||||||
|
int read; // compteur
|
||||||
|
struct sockaddr_in clientInfo;
|
||||||
|
socklen_t len;
|
||||||
|
char request[MAX_BUF_LEN]; // Requête
|
||||||
|
// char response[MAX_BUF_LEN]; // Réponse
|
||||||
|
|
||||||
|
/* 2. On récupère les arguments */
|
||||||
|
struct hndlr_thrd_arg* arg = THREADABLE_ARGS;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
do{
|
||||||
|
|
||||||
|
printf("{udp_x-term}{udp_com} waiting for terminal request\n");
|
||||||
|
/* [2] Récupération de la requête
|
||||||
|
=========================================================*/
|
||||||
|
/* 1. On lit sur la socket */
|
||||||
|
len = sizeof(struct sockaddr_in);
|
||||||
|
read = recvfrom(arg->socket, request, MAX_BUF_LEN, 0, (struct sockaddr*) &clientInfo, &len);
|
||||||
|
|
||||||
|
/* 2. Si erreur reception */
|
||||||
|
if( DEBUGMOD&BUF ) printf("{udp_x-term}{udp_com} READ = %d\n", read);
|
||||||
|
|
||||||
|
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
|
||||||
|
=========================================================*/
|
||||||
|
|
||||||
|
|
||||||
|
/* [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);
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
/**************************
|
||||||
|
* Handler Dependency *
|
||||||
|
***************************
|
||||||
|
* Designed & Developed by *
|
||||||
|
* Adrien Marquès *
|
||||||
|
* <xdrm-brackets> *
|
||||||
|
***************************
|
||||||
|
* doowap31@gmail.com *
|
||||||
|
**************************/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _LIB_LOCAL_HANDLER_H_
|
||||||
|
#define _LIB_LOCAL_HANDLER_H_
|
||||||
|
|
||||||
|
|
||||||
|
#include "../header.h"
|
||||||
|
|
||||||
|
void* managePlane(void* THREADABLE_ARGS);
|
||||||
|
void* dispatchPlanes(void* THREADABLE_ARGS);
|
||||||
|
|
||||||
|
void* manageViewTerm(void* THREADABLE_ARGS);
|
||||||
|
void* manageCtrlTerm(void* THREADABLE_ARGS);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,17 @@
|
||||||
|
/**************************
|
||||||
|
* Middleware Dependency *
|
||||||
|
***************************
|
||||||
|
* Designed & Developed by *
|
||||||
|
* Adrien Marquès *
|
||||||
|
* <xdrm-brackets> *
|
||||||
|
***************************
|
||||||
|
* doowap31@gmail.com *
|
||||||
|
**************************/
|
||||||
|
#include "middleware.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void* multicastPlanes(){ return 0; }
|
||||||
|
|
||||||
|
|
||||||
|
void* multicastTerminal(){ return 0; }
|
|
@ -0,0 +1,20 @@
|
||||||
|
/**************************
|
||||||
|
* Middleware Dependency *
|
||||||
|
***************************
|
||||||
|
* Designed & Developed by *
|
||||||
|
* Adrien Marquès *
|
||||||
|
* <xdrm-brackets> *
|
||||||
|
***************************
|
||||||
|
* doowap31@gmail.com *
|
||||||
|
**************************/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _LIB_LOCAL_MIDDLEWARE_H_
|
||||||
|
#define _LIB_LOCAL_MIDDLEWARE_H_
|
||||||
|
|
||||||
|
|
||||||
|
void* multicastPlanes();
|
||||||
|
void* multicastTerminal();
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
|
@ -7,8 +7,6 @@
|
||||||
***************************
|
***************************
|
||||||
* doowap31@gmail.com *
|
* doowap31@gmail.com *
|
||||||
**************************/
|
**************************/
|
||||||
|
|
||||||
|
|
||||||
#include "server.h"
|
#include "server.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue