From 7acf3f3dcad921101d7e555b5ae9a451f3e83aa4 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 18 Aug 2010 17:23:37 +0200 Subject: [PATCH] collectd-tg: Implemented continuous sending of values. --- src/collectd-tg.c | 65 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/src/collectd-tg.c b/src/collectd-tg.c index 5b31cc9d..7aadecf5 100644 --- a/src/collectd-tg.c +++ b/src/collectd-tg.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "utils_heap.h" @@ -42,18 +43,27 @@ static lcc_network_t *net; static c_heap_t *values_heap = NULL; +static struct sigaction sigint_action; +static struct sigaction sigterm_action; + +static _Bool loop = 1; + +static void signal_handler (int signal) /* {{{ */ +{ + loop = 0; +} /* }}} void signal_handler */ + static int compare_time (const void *v0, const void *v1) /* {{{ */ { - lcc_value_list_t * const *vl0 = v0; - lcc_value_list_t * const *vl1 = v1; + const lcc_value_list_t *vl0 = v0; + const lcc_value_list_t *vl1 = v1; - if ((*vl0)->time < (*vl1)->time) + if (vl0->time < vl1->time) return (-1); - else if ((*vl0)->time > (*vl1)->time) + else if (vl0->time > vl1->time) return (1); else - return (lcc_identifier_compare (&(*vl0)->identifier, /* Ouch, somebody */ - &(*vl1)->identifier)); /* is going to hate me for this. */ + return (0); } /* }}} int compare_time */ static int get_boundet_random (int min, int max) /* {{{ */ @@ -205,6 +215,15 @@ static int send_value (lcc_value_list_t *vl) /* {{{ */ int main (int argc, char **argv) /* {{{ */ { int i; + time_t last_time; + int values_sent = 0; + + sigint_action.sa_handler = signal_handler; + sigaction (SIGINT, &sigint_action, /* old = */ NULL); + + sigterm_action.sa_handler = signal_handler; + sigaction (SIGTERM, &sigterm_action, /* old = */ NULL); + values_heap = c_heap_create (compare_time); if (values_heap == NULL) @@ -254,17 +273,47 @@ int main (int argc, char **argv) /* {{{ */ } fprintf (stdout, "done\n"); - while (42) + last_time = 0; + while (loop) { lcc_value_list_t *vl = c_heap_get_root (values_heap); if (vl == NULL) break; + if (vl->time != last_time) + { + printf ("%i values have been sent.\n", values_sent); + + /* Check if we need to sleep */ + time_t now = time (NULL); + + while (now < vl->time) + { + /* 1 / 100 second */ + struct timespec ts = { 0, 10000000 }; + nanosleep (&ts, /* remaining = */ NULL); + now = time (NULL); + } + last_time = vl->time; + } + send_value (vl); - destroy_value_list (vl); + values_sent++; + + c_heap_insert (values_heap, vl); } + fprintf (stdout, "Shutting down.\n"); + fflush (stdout); + + while (42) + { + lcc_value_list_t *vl = c_heap_get_root (values_heap); + if (vl == NULL) + break; + destroy_value_list (vl); + } c_heap_destroy (values_heap); lcc_network_destroy (net); -- 2.30.2