[Aggregator] Receive hello messages from listener

This commit is contained in:
Matteo Cypriani 2010-10-11 09:54:35 +02:00
parent c583e0bb54
commit 729c20c8ff
4 changed files with 90 additions and 17 deletions

View File

@ -15,7 +15,7 @@
/* Arguments & program configuration */
#define OPTIONS "a:c:f:hi:k:l:o:p:" // getopt string
#define OPTIONS "Aa:c:f:hi:k:l:o:p:t:" // getopt string
#define DEFAULT_CONFIG_FILE "/usr/local/etc/owlps/owlps-aggregator.conf"
#define DEFAULT_AGGREGATE_TIMEOUT 1500 // milliseconds
#define DEFAULT_KEEP_TIMEOUT 3000 // milliseconds
@ -90,6 +90,7 @@ void print_couple_list(void) ;
void print_couple_info(couple_info_list *info) ;
#endif // DEBUG
void listen_for_aps(void) ;
void update_ap(unsigned char mac_address_bytes[6]) ;
ap_list* find_ap(unsigned char mac_address_bytes[6]) ;
void add_ap_front(unsigned char mac_address_bytes[6]) ;

View File

@ -21,7 +21,9 @@ int main(int argc, char **argv)
{
int ret = 0 ; // Program return value
struct sigaction action ; // Signal handler structure
pthread_t thread ; // Thread for aggregated data monitoring function
pthread_t
monitor_thread, // Aggregated data monitoring thread
autocalibration_hello_thread ; // Hello messages reception thread
unsigned int listening_port ;
int sockfd ; // UDP listening socket
@ -44,8 +46,10 @@ int main(int argc, char **argv)
return ERR_CREATING_SOCKET ;
}
/* Set up thread */
pthread_create(&thread, NULL,
/* Set up threads */
pthread_create(&monitor_thread, NULL,
(void *) &monitor_couples, NULL) ;
pthread_create(&autocalibration_hello_thread, NULL,
(void *) &monitor_couples, NULL) ;
run = TRUE ;
@ -96,6 +100,12 @@ void parse_config_file(int argc, char **argv)
// Time between two list checks (in microseconds):
CFG_INT("check_interval", DEFAULT_CHECK_INTERVAL, CFGF_NONE),
// Autocalibration activated?
CFG_BOOL("autocalibration", cfg_false, CFGF_NONE),
// Port on which autocalibration data are exchanged:
CFG_INT("autocalbration_port", AUTOCALIBRATION_DEFAULT_PORT,
CFGF_NONE),
CFG_END()
} ;
@ -148,9 +158,13 @@ void parse_command_line(int argc, char **argv)
{
switch (opt)
{
case 'a' :
cfg_setint(cfg, "aggregate_timeout", strtol(optarg, NULL, 0)) ;
break ;
case 'A' :
cfg_setbool(cfg, "autocalibration", cfg_true) ;
break ;
case 'a' :
cfg_setint(cfg, "autocalibration_port",
strtol(optarg, NULL, 0)) ;
break ;
case 'c' :
cfg_setint(cfg, "check_interval", strtol(optarg, NULL, 0)) ;
break ;
@ -174,6 +188,9 @@ void parse_command_line(int argc, char **argv)
case 'p' :
cfg_setint(cfg, "positioner_port", strtol(optarg, NULL, 0)) ;
break ;
case 't' :
cfg_setint(cfg, "aggregate_timeout", strtol(optarg, NULL, 0)) ;
break ;
default :
print_usage() ;
exit(ERR_BAD_USAGE) ;
@ -457,9 +474,10 @@ void* monitor_couples()
usleep(cfg_getint(cfg, "check_interval")) ; // Wait to check again
}
/* Close output file */
/* Close output file & socket */
if (fclose(fd) != 0)
perror("Error closing output file") ;
(void) close(sockfd) ;
return NULL ;
}
@ -602,6 +620,49 @@ void free_couple_list()
/*
* Thread function. Listens for hello messages from APs.
*/
void listen_for_aps(void)
{
int listen_sockfd ;
int nread ; // recvfrom return value
struct sockaddr_in client; // UDP client structure
socklen_t client_len = sizeof(client) ; // Size of clients
autocalibration_hello message ;
unsigned char ap_mac_addr_bytes[6] ;
listen_sockfd =
create_udp_listening_socket(cfg_getint(cfg,
"autocalibration_port")) ;
if (listen_sockfd < 0)
{
perror("Error! Cannot create UDP listening socket from the"
" listeners") ;
exit(ERR_CREATING_SOCKET) ;
}
while (run)
{
nread = recvfrom(listen_sockfd, &message, sizeof(message), 0,
(struct sockaddr *) &client, &client_len) ;
if (nread <= 0 && run)
{
if (run)
fprintf(stderr, "No message received from aggregator!\n") ;
continue ;
}
memcpy(&ap_mac_addr_bytes, message.ap_mac_addr_bytes, 6) ;
update_ap(ap_mac_addr_bytes) ;
}
(void) close(listen_sockfd) ;
}
/*
* Updates the timestamp of the AP with the given MAC address if it is in
* the AP list, or add a new AP with this MAC address to the AP list.
@ -858,8 +919,9 @@ void print_usage()
{
printf("Usage:\n"
"\t%s [-f config_file] [-l listening_port] [-i positionner_ip]"
" [-p positioner_port] [-a aggregate_timeout] [-k keep_timeout]"
" [-c check_interval] [-o output_file]\n"
" [-p positioner_port] [-t aggregate_timeout] [-k keep_timeout]"
" [-c check_interval] [-o output_file] [-A]"
" [-a autocalibration_port]\n"
"Main options:\n"
"\t-h\t\tPrint this help.\n"
@ -875,13 +937,18 @@ void print_usage()
" the localisation server on this port (default: %d).\n"
"Aggregation options:\n"
"\t-a aggregate_timeout\tRequests are stored during"
"\t-t aggregate_timeout\tRequests are stored during"
" 'aggregate_timeout' before to be grouped (default:"
" %d millisecondes).\n"
"\t-k keep_timeout\t\tAggregated requests are kept during"
" 'keep_timeout' milliseconds (default: %d milliseconds).\n"
"\t-c check_interval\tTime between two checks of the stored"
" requests (default\t%d microseconds).\n"
"Autocalibration options:\n"
"\t-A\tEnable autocalibration (default: disabled).\n"
"\t-a port\tPort on which autocalibration data"
" are exchanged with the listeners (default: %d).\n"
,
program_name,
AGGREGATE_DEFAULT_PORT,
@ -889,6 +956,7 @@ void print_usage()
POSITIONER_DEFAULT_PORT,
DEFAULT_AGGREGATE_TIMEOUT,
DEFAULT_KEEP_TIMEOUT,
DEFAULT_CHECK_INTERVAL
DEFAULT_CHECK_INTERVAL,
AUTOCALIBRATION_DEFAULT_PORT
) ;
}

View File

@ -764,15 +764,17 @@ void autocalibrate_hello()
struct sockaddr_in serv;
struct sockaddr_in client ;
socklen_t serv_len = sizeof(serv);
autocalibration_hello message ;
send_sockfd = create_udp_sending_socket(GET_AGGREGATION_IP(),
GET_AUTOCALIBRATION_PORT(),
&serv, &client) ;
memcpy(&message.ap_mac_addr_bytes, mac, 6) ;
while (run)
{
sendto(send_sockfd, AUTOCALIBRATION_HELLO_STRING,
sizeof(AUTOCALIBRATION_HELLO_STRING), 0,
sendto(send_sockfd, (void *)&message, sizeof(message), 0,
(struct sockaddr *)&serv, serv_len) ;
sleep(GET_AUTOCALIBRATION_HELLO_DELAY()) ;
}

View File

@ -48,9 +48,6 @@
// Port on which the mobile listens for its position:
#define MOBILE_DEFAULT_PORT 9910
// Other parameters:
#define AUTOCALIBRATION_HELLO_STRING "Ay oop aggregator!"
/* Type booléen */
typedef enum {FALSE, TRUE} BOOL ;
@ -89,6 +86,11 @@ typedef struct _request
int nb_couples; // Nombre couples (MAC AP;Puissance)
} request;
typedef struct _autocalibration_hello
{
unsigned char ap_mac_addr_bytes[6];
} autocalibration_hello ;
/* Types de demandes de localisation */
#define PACKET_TYPE_NORMAL 0
#define PACKET_TYPE_CALIBRATION 1