diff options
Diffstat (limited to 'ffd/ffd.c')
-rw-r--r-- | ffd/ffd.c | 74 |
1 files changed, 42 insertions, 32 deletions
@@ -28,6 +28,7 @@ #include "neigh.h" #include "netif.h" #include "packet.h" +#include "queue.h" #include "tlv.h" #include "tlv_types.h" @@ -51,10 +52,10 @@ static char *mesh = "bat0"; int sockfd; struct timespec now; -ffd_node_id_t self; +static ffd_queue_head *tasks; +ffd_node_id_t self; ffd_iface_t *iface_list = NULL; - ffd_announce_t *announce_list = NULL; @@ -489,6 +490,8 @@ static void receive_packet(void) { } static void send_updates(void) { + fprintf(stderr, "debug: sending periodic updates.\n"); + ffd_iface_t *iface; for (iface = iface_list; iface; iface = iface->next) { ffd_send_update(iface, NULL, NULL, false); @@ -558,6 +561,36 @@ static void maintenance(void) { } } + +typedef struct _periodic_task_info { + void (*handle)(void); + unsigned delay; + unsigned interval; +} periodic_task_info; + + +static const periodic_task_info periodic_tasks [] = { + {maintenance, 0, FFD_MAINTENANCE_INTERVAL}, + {ffd_send_hellos, FFD_HELLO_INTERVAL, FFD_HELLO_INTERVAL}, + {send_updates, 1, FFD_UPDATE_INTERVAL}, + {NULL} +}; + + +static void handle_periodic_task(const struct timespec *timeout, void *arg) { + const periodic_task_info *info = arg; + + info->handle(); + ffd_queue_put_delayed(&tasks, handle_periodic_task, timeout, info->interval, arg); +} + +static void register_periodic_tasks(void) { + const periodic_task_info *info; + for (info = periodic_tasks; info->handle; info++) + ffd_queue_put_delayed(&tasks, handle_periodic_task, &now, info->delay, (void*)info); +} + + int main() { if (!check_config()) return 1; @@ -569,44 +602,21 @@ int main() { return 1; update_time(); - - struct timespec next_hello = now; - struct timespec next_update = now; - struct timespec next_maintenance = now; + register_periodic_tasks(); while (true) { - int maintenance_timeout = timespec_diff(&next_maintenance, &now); - if (maintenance_timeout <= 0) { - maintenance(); - - add_interval(&next_maintenance, FFD_MAINTENANCE_INTERVAL); - } - - int hello_timeout = timespec_diff(&next_hello, &now); - if (hello_timeout <= 0) { - ffd_send_hellos(); - - add_interval(&next_hello, FFD_HELLO_INTERVAL); - } - - int update_timeout = timespec_diff(&next_update, &now); - if (update_timeout <= 0) { - fprintf(stderr, "Sending periodic update.\n"); - send_updates(); - - add_interval(&next_update, FFD_UPDATE_INTERVAL); - } - - int timeout = min(min(hello_timeout, update_timeout), maintenance_timeout); - if (timeout <= 0) - continue; + ffd_queue_run(&tasks); struct pollfd fds[1]; + int timeout = 10*ffd_queue_timeout(&tasks); + if (timeout < 0) + timeout = -1; + fds[0].fd = sockfd; fds[0].events = POLLIN; - poll(fds, 1, 10*timeout); + poll(fds, 1, timeout); update_time(); |