2017-04-09 17:33:27 +00:00
/**************************
* 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 */
2017-04-11 17:12:34 +00:00
struct handler_arg * arg = THREADABLE_ARGS ;
2017-04-09 17:33:27 +00:00
/* 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 ) ;
}
/* 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 */
2017-04-11 17:12:34 +00:00
struct handler_arg * arg = THREADABLE_ARGS ;
2017-04-09 17:33:27 +00:00
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 */
2017-04-11 17:12:34 +00:00
struct handler_arg * arg = THREADABLE_ARGS ;
2017-04-09 17:33:27 +00:00
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 ) ;
}