From: Florian Forster Date: Sat, 21 Jun 2008 02:09:59 +0000 (+0200) Subject: src/librrdc.c: Sending to the daemon has been added. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=fb14438263cc85fc40a79b5c801a6c40f7ab4d94;p=rrdd.git src/librrdc.c: Sending to the daemon has been added. Handling by the daemon is still missing. --- diff --git a/src/librrdc.c b/src/librrdc.c index c0ede89..8b168d6 100644 --- a/src/librrdc.c +++ b/src/librrdc.c @@ -28,6 +28,38 @@ static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; static int sd; static FILE *sh; +static int buffer_add_string (const char *str, + char **buffer_ret, size_t *buffer_size_ret) +{ + size_t str_size; + + str_size = strlen (str) + 1; + + if (*buffer_size_ret < str_size) + return (-1); + + memcpy (*buffer_ret, str, str_size); + *buffer_ret += str_size; + *buffer_size_ret -= str_size; + + return (0); +} /* int buffer_add_string */ + +static int buffer_add_value (const char *value, + char **buffer_ret, size_t *buffer_size_ret) +{ + char temp[4096]; + + if (strncmp (value, "N:", 2) == 0) + snprintf (temp, sizeof (temp), "%lu:%s", + (unsigned long) time (NULL), value + 2); + else + strncpy (temp, value, sizeof (temp)); + temp[sizeof (temp) - 1] = 0; + + return (buffer_add_string (temp, buffer_ret, buffer_size_ret)); +} /* int buffer_add_value */ + int rrdd_connect (const char *path) { struct sockaddr_un sa; @@ -103,6 +135,20 @@ int rrdd_disconnect (void) int rrdd_update (const char *filename, int values_num, const char * const *values) { + char buffer[4096]; + char *buffer_ptr; + size_t buffer_size; + int status; + int i; + + memset (buffer, 0, sizeof (buffer)); + buffer_ptr = &buffer[0]; + buffer_size = sizeof (buffer) - 1; + + buffer_add_string (filename, &buffer_ptr, &buffer_size); + for (i = 0; i < values_num; i++) + buffer_add_value (values[i], &buffer_ptr, &buffer_size); + pthread_mutex_lock (&lock); if (sh == NULL) @@ -111,6 +157,8 @@ int rrdd_update (const char *filename, int values_num, return (-1); } + status = write (sd, buffer, sizeof (buffer) - buffer_size); + pthread_mutex_unlock (&lock); return (0);