From 790c9bdaf7bf7e68f26d169d244fd94273b59a3e Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Fri, 6 Aug 2010 12:29:39 +0200 Subject: [PATCH] [Aggregator] Handle AP list --- .../owlps-aggregator/owlps-aggregator.h | 28 ++- .../owlps-aggregator/owlps-aggregatord.c | 168 ++++++++++++++++++ 2 files changed, 195 insertions(+), 1 deletion(-) diff --git a/infrastructure-centred/owlps-aggregator/owlps-aggregator.h b/infrastructure-centred/owlps-aggregator/owlps-aggregator.h index 7b05d96..c9c3494 100644 --- a/infrastructure-centred/owlps-aggregator/owlps-aggregator.h +++ b/infrastructure-centred/owlps-aggregator/owlps-aggregator.h @@ -63,6 +63,17 @@ typedef struct _couple_list } couple_list ; +/* Linked list of the known APs */ +typedef struct _ap_list +{ + unsigned char mac_address_bytes[6] ; + struct timeval last_seen ; + + struct _ap_list *previous ; + struct _ap_list *next ; +} ap_list ; + + /* Function headers */ void initialise_configuration(int argc, char **argv) ; void parse_config_file(int argc, char **argv) ; @@ -71,13 +82,28 @@ void check_configuration(void) ; int read_loop(int sockfd) ; void got_couple_info(couple_message message) ; + +void* monitor_couples(void) ; void free_couple_list(void) ; #ifdef DEBUG void print_couple_list(void) ; void print_couple_info(couple_info_list *info) ; #endif // DEBUG -void* monitor_couples(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]) ; +void update_ap_seen(ap_list *ap) ; +void push_ap(ap_list *ap) ; + +void monitor_aps(void) ; +void delete_old_aps(void) ; +void delete_ap(ap_list *ap) ; +void unlink_ap(ap_list *ap) ; +void order_send(ap_list *ap) ; +void move_ap_front(ap_list *ap) ; +void free_ap_list(void) ; + char* ip_bytes_to_string(unsigned char *ip_binary) ; void print_usage(void) ; diff --git a/infrastructure-centred/owlps-aggregator/owlps-aggregatord.c b/infrastructure-centred/owlps-aggregator/owlps-aggregatord.c index 7049aed..306cd91 100644 --- a/infrastructure-centred/owlps-aggregator/owlps-aggregatord.c +++ b/infrastructure-centred/owlps-aggregator/owlps-aggregatord.c @@ -5,11 +5,15 @@ #include "owlps-aggregator.h" +#include + char *program_name = NULL ; cfg_t *cfg = NULL ; // Configuration structure couple_list *couples = NULL ; // Computed data list +ap_list *aps = NULL ; +unsigned int nb_aps = 0 ; @@ -49,6 +53,7 @@ int main(int argc, char **argv) (void) close(sockfd) ; // Close socket free_couple_list() ; + free_ap_list() ; cfg_free(cfg) ; // Clean configuration printf("%s: end.\n", program_name) ; @@ -597,6 +602,169 @@ void free_couple_list() +/* + * 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. + */ +void update_ap(unsigned char mac_address_bytes[6]) +{ + ap_list *found ; + + if ((found = find_ap(mac_address_bytes)) == NULL) + add_ap_front(mac_address_bytes) ; + + else + update_ap_seen(found) ; +} + + +/* + * Searches the AP list for an AP with the given MAC address and returns + * it. + */ +ap_list* find_ap(unsigned char mac_address_bytes[6]) +{ + ap_list *found = aps ; + + while (found != NULL) + { + if (mac_cmp(found->mac_address_bytes, mac_address_bytes)) + return found ; + found = found->next ; + } + + return NULL ; +} + + +/* Adds a new AP in front of the AP list. */ +inline void add_ap_front(unsigned char mac_address_bytes[6]) +{ + ap_list *ap = malloc(sizeof(ap_list)) ; + memcpy(ap->mac_address_bytes, mac_address_bytes, 6) ; + update_ap_seen(ap) ; + push_ap(ap) ; + ++nb_aps ; +} + + +/* Updates the timestamp of the given AP. */ +inline void update_ap_seen(ap_list *ap) +{ + assert(ap) ; + gettimeofday(&ap->last_seen, NULL) ; +} + + +/* + * Puts an existing AP in front of the AP list. The AP must not be in + * the list yet. + */ +inline void push_ap(ap_list *ap) +{ + assert(ap) ; + ap->previous = NULL ; + ap->next = aps ; + if (aps != NULL) + aps->previous = ap ; + aps = ap ; +} + + + +/* + * Monitors the AP list: sends orders to APs following their order in + * the list, and deletes old APs. + */ +void monitor_aps() +{ + while (run) + { + delete_old_aps() ; + + if (nb_aps > 1) + order_send(aps) ; + + usleep(cfg_getint(cfg, "ap_check_interval")) ; + } +} + + +/* + * Deletes APs that did not send any Hello packet for a while, following + * the list order. Stops on the first not-to-be-deleted AP. + */ +void delete_old_aps() +{ + struct timeval now ; + gettimeofday(&now, NULL) ; + + while (aps != NULL) + if (sub_date(aps->last_seen, now) > + (unsigned long) cfg_getint(cfg, "keep_ap_timeout")) + delete_ap(aps) ; + else + return ; +} + + +/* Deletes an AP from the AP list. */ +inline void delete_ap(ap_list *ap) +{ + unlink_ap(ap) ; + free(ap) ; + --nb_aps ; +} + + +/* Extracts the given AP from the AP list. */ +void unlink_ap(ap_list *ap) +{ + assert(ap) ; + ap_list + *ap_previous = ap->previous, + *ap_next = ap->next ; + + if (ap_previous != NULL) + ap_previous->next = ap_next ; + + if (ap_next != NULL) + ap_next->previous = ap_previous ; + + if (ap == aps) + aps = ap_next ; +} + + +void order_send(ap_list *ap) +{ + +} + + +/* Move the given AP, existing in the AP list, in front of the list. */ +inline void move_ap_front(ap_list *ap) +{ + unlink_ap(ap) ; + push_ap(ap) ; +} + + +/* Empty the AP list. */ +void free_ap_list() +{ + ap_list *ap_ptr = aps ; + + while (ap_ptr != NULL) + { + ap_ptr = ap_ptr->next ; + free(aps) ; + aps = ap_ptr ; + } +} + + + #ifdef DEBUG /* Prints the couple list */ void print_couple_list()