src/rrd_{fetch,graph,update}.c: Use the `RRDCACHED_ADDRESS' environment variable..
[rrdtool.git] / src / rrd_client.c
index ef39d48..d9a7468 100644 (file)
@@ -34,9 +34,6 @@
 
 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
 static int sd = -1;
-#if 0
-static FILE *sh;
-#endif
 
 static ssize_t sread (void *buffer_void, size_t buffer_size) /* {{{ */
 {
@@ -62,9 +59,6 @@ static ssize_t sread (void *buffer_void, size_t buffer_size) /* {{{ */
     {
       close (sd);
       sd = -1;
-#if 0
-      sh = NULL;
-#endif
       errno = EPROTO;
       return (-1);
     }
@@ -184,8 +178,7 @@ static int rrdc_connect_unix (const char *path) /* {{{ */
   struct sockaddr_un sa;
   int status;
 
-  if (path == NULL)
-    path = RRDD_SOCK_PATH;
+  assert (path != NULL);
 
   pthread_mutex_lock (&lock);
 
@@ -215,18 +208,6 @@ static int rrdc_connect_unix (const char *path) /* {{{ */
     return (status);
   }
 
-#if 0
-  sh = fdopen (sd, "w+");
-  if (sh == NULL)
-  {
-    status = errno;
-    close (sd);
-    sd = -1;
-    pthread_mutex_unlock (&lock);
-    return (status);
-  }
-#endif
-
   pthread_mutex_unlock (&lock);
 
   return (0);
@@ -240,7 +221,7 @@ int rrdc_connect (const char *addr) /* {{{ */
   int status;
 
   if (addr == NULL)
-    addr = RRDD_SOCK_PATH;
+    addr = RRDCACHED_DEFAULT_ADDRESS;
 
   if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
     return (rrdc_connect_unix (addr + strlen ("unix:")));
@@ -264,7 +245,7 @@ int rrdc_connect (const char *addr) /* {{{ */
   ai_hints.ai_socktype = SOCK_STREAM;
 
   ai_res = NULL;
-  status = getaddrinfo (addr, DEFAULT_PORT, &ai_hints, &ai_res);
+  status = getaddrinfo (addr, RRDCACHED_DEFAULT_PORT, &ai_hints, &ai_res);
   if (status != 0)
   {
     pthread_mutex_unlock (&lock);
@@ -290,17 +271,6 @@ int rrdc_connect (const char *addr) /* {{{ */
       continue;
     }
 
-#if 0
-    sh = fdopen (sd, "w+");
-    if (sh == NULL)
-    {
-      status = errno;
-      close (sd);
-      sd = -1;
-      continue;
-    } 
-#endif
-
     assert (status == 0);
     break;
   } /* for (ai_ptr) */
@@ -311,30 +281,20 @@ int rrdc_connect (const char *addr) /* {{{ */
 
 int rrdc_disconnect (void) /* {{{ */
 {
-  int status;
-
   pthread_mutex_lock (&lock);
 
   if (sd < 0)
   {
     pthread_mutex_unlock (&lock);
-    return (-1);
+    return (0);
   }
 
-#if 0
-  status = fclose (sh);
-  if (status != 0)
-    status = errno;
-
-  sh = NULL;
-#else
-  status = 0;
-#endif
+  close (sd);
   sd = -1;
 
   pthread_mutex_unlock (&lock);
 
-  return (status);
+  return (0);
 } /* }}} int rrdc_disconnect */
 
 int rrdc_update (const char *filename, int values_num, /* {{{ */
@@ -403,7 +363,69 @@ int rrdc_update (const char *filename, int values_num, /* {{{ */
 
   status = atoi (buffer);
   return (status);
-} /* }}} int rrd_update_daemon */
+} /* }}} int rrdc_update */
+
+int rrdc_flush (const char *filename) /* {{{ */
+{
+  char buffer[4096];
+  char *buffer_ptr;
+  size_t buffer_free;
+  size_t buffer_size;
+  int status;
+
+  if (filename == NULL)
+    return (-1);
+
+  memset (buffer, 0, sizeof (buffer));
+  buffer_ptr = &buffer[0];
+  buffer_free = sizeof (buffer);
+
+  status = buffer_add_string ("flush", &buffer_ptr, &buffer_free);
+  if (status != 0)
+    return (ENOBUFS);
+
+  status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
+  if (status != 0)
+    return (ENOBUFS);
+
+  assert (buffer_free < sizeof (buffer));
+  buffer_size = sizeof (buffer) - buffer_free;
+  assert (buffer[buffer_size - 1] == ' ');
+  buffer[buffer_size - 1] = '\n';
+
+  pthread_mutex_lock (&lock);
+
+  if (sd < 0)
+  {
+    pthread_mutex_unlock (&lock);
+    return (ENOTCONN);
+  }
+
+  status = swrite (buffer, buffer_size);
+  if (status != 0)
+  {
+    pthread_mutex_unlock (&lock);
+    return (status);
+  }
+
+  status = sread (buffer, sizeof (buffer));
+  if (status < 0)
+  {
+    status = errno;
+    pthread_mutex_unlock (&lock);
+    return (status);
+  }
+  else if (status == 0)
+  {
+    pthread_mutex_unlock (&lock);
+    return (ENODATA);
+  }
+
+  pthread_mutex_unlock (&lock);
+
+  status = atoi (buffer);
+  return (status);
+} /* }}} int rrdc_flush */
 
 /*
  * vim: set sw=2 sts=2 ts=8 et fdm=marker :