-GArik: fix typo
[oweals/gnunet.git] / src / statistics / gnunet-service-statistics.c
index 5e0be11832e6a0b95991a84ce5991d813b27bae4..a890d6d8ec339871ec0d002b8c0a84c2394dad11 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009, 2010 Christian Grothoff (and other contributing authors)
+     (C) 2009, 2010, 2012 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
  * @file statistics/gnunet-service-statistics.c
  * @brief program that tracks statistics
  * @author Christian Grothoff
- * 
- * TODO:
- * - use BIO for IO operations
  */
 #include "platform.h"
+#include "gnunet_bio_lib.h"
 #include "gnunet_container_lib.h"
 #include "gnunet_disk_lib.h"
 #include "gnunet_getopt_lib.h"
@@ -175,14 +173,14 @@ static void
 load (struct GNUNET_SERVER_Handle *server)
 {
   char *fn;
-  struct GNUNET_DISK_FileHandle *fh;
-  struct GNUNET_DISK_MapHandle *mh;
+  struct GNUNET_BIO_ReadHandle *rh;
   struct stat sb;
   char *buf;
   struct GNUNET_SERVER_MessageStreamTokenizer *mst;
+  char *emsg;
 
-  fn = GNUNET_DISK_get_home_filename (cfg,
-                                      "statistics", "statistics.data", NULL);
+  fn = GNUNET_DISK_get_home_filename (cfg, "statistics", "statistics.data",
+                                      NULL);
   if (fn == NULL)
     return;
   if ((0 != stat (fn, &sb)) || (sb.st_size == 0))
@@ -190,17 +188,20 @@ load (struct GNUNET_SERVER_Handle *server)
     GNUNET_free (fn);
     return;
   }
-  fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
-  if (!fh)
+  buf = GNUNET_malloc (sb.st_size);
+  rh = GNUNET_BIO_read_open (fn);
+  if (!rh)
   {
+    GNUNET_free (buf);
     GNUNET_free (fn);
     return;
   }
-  buf = GNUNET_DISK_file_map (fh, &mh, GNUNET_DISK_MAP_TYPE_READ, sb.st_size);
-  if (NULL == buf)
+  if (GNUNET_OK != GNUNET_BIO_read (rh, fn, buf, sb.st_size))
   {
-    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "mmap", fn);
-    GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
+    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", fn);
+    GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
+    GNUNET_free (buf);
+    GNUNET_free_non_null (emsg);
     GNUNET_free (fn);
     return;
   }
@@ -209,13 +210,12 @@ load (struct GNUNET_SERVER_Handle *server)
               (unsigned long long) sb.st_size, fn);
   mst = GNUNET_SERVER_mst_create (&inject_message, server);
   GNUNET_break (GNUNET_OK ==
-                GNUNET_SERVER_mst_receive (mst,
-                                           NULL,
-                                           buf,
-                                           sb.st_size, GNUNET_YES, GNUNET_NO));
+                GNUNET_SERVER_mst_receive (mst, NULL, buf, sb.st_size,
+                                           GNUNET_YES, GNUNET_NO));
   GNUNET_SERVER_mst_destroy (mst);
-  GNUNET_break (GNUNET_OK == GNUNET_DISK_file_unmap (mh));
-  GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
+  GNUNET_free (buf);
+  GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
+  GNUNET_free_non_null (emsg);
   GNUNET_free (fn);
 }
 
@@ -227,40 +227,39 @@ save ()
 {
   struct StatsEntry *pos;
   char *fn;
-  struct GNUNET_DISK_FileHandle *fh;
+  struct GNUNET_BIO_WriteHandle *wh;
+  
   uint16_t size;
   unsigned long long total;
 
-  fh = NULL;
-  fn = GNUNET_DISK_get_home_filename (cfg,
-                                      "statistics", "statistics.data", NULL);
+  wh = NULL;
+  fn = GNUNET_DISK_get_home_filename (cfg, "statistics", "statistics.data",
+                                      NULL);
   if (fn != NULL)
-    fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_WRITE
-                                | GNUNET_DISK_OPEN_CREATE |
-                                GNUNET_DISK_OPEN_TRUNCATE,
-                                GNUNET_DISK_PERM_USER_READ |
-                                GNUNET_DISK_PERM_USER_WRITE);
+    wh = GNUNET_BIO_write_open (fn);
   total = 0;
   while (NULL != (pos = start))
   {
     start = pos->next;
-    if ((pos->persistent) && (NULL != fh))
+    if ((pos->persistent) && (NULL != wh))
     {
       size = htons (pos->msg->header.size);
-      if (size != GNUNET_DISK_file_write (fh, pos->msg, size))
+      if (GNUNET_OK != GNUNET_BIO_write (wh, pos->msg, size))
       {
         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
-        GNUNET_DISK_file_close (fh);
-        fh = NULL;
+        if (GNUNET_OK != GNUNET_BIO_write_close (wh))
+         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
+        wh = NULL;
       }
       else
         total += size;
     }
     GNUNET_free (pos);
   }
-  if (NULL != fh)
+  if (NULL != wh)
   {
-    GNUNET_DISK_file_close (fh);
+    if (GNUNET_OK != GNUNET_BIO_write_close (wh))
+      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
     if (total == 0)
       GNUNET_break (0 == UNLINK (fn));
     else
@@ -292,13 +291,13 @@ transmit (struct GNUNET_SERVER_Client *client, const struct StatsEntry *e)
     m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
   m->value = GNUNET_htonll (e->value);
   size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
-  GNUNET_assert (size == GNUNET_STRINGS_buffer_fill ((char *) &m[1],
-                                                     size,
-                                                     2, e->service, e->name));
+  GNUNET_assert (size ==
+                 GNUNET_STRINGS_buffer_fill ((char *) &m[1], size, 2,
+                                             e->service, e->name));
 #if DEBUG_STATISTICS
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Transmitting value for `%s:%s' (%d): %llu\n",
-              e->service, e->name, e->persistent, e->value);
+              "Transmitting value for `%s:%s' (%d): %llu\n", e->service,
+              e->name, e->persistent, e->value);
 #endif
   GNUNET_SERVER_notification_context_unicast (nc, client, &m->header,
                                               GNUNET_NO);
@@ -312,9 +311,8 @@ transmit (struct GNUNET_SERVER_Client *client, const struct StatsEntry *e)
 static int
 matches (const struct StatsEntry *e, const char *service, const char *name)
 {
-  return ((0 == strlen (service)) ||
-          (0 == strcmp (service, e->service)))
-      && ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
+  return ((0 == strlen (service)) || (0 == strcmp (service, e->service))) &&
+      ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
 }
 
 
@@ -350,8 +348,7 @@ make_client_entry (struct GNUNET_SERVER_Client *client)
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static void
-handle_get (void *cls,
-            struct GNUNET_SERVER_Client *client,
+handle_get (void *cls, struct GNUNET_SERVER_Client *client,
             const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_MessageHeader end;
@@ -363,8 +360,9 @@ handle_get (void *cls,
   if (client != NULL)
     make_client_entry (client);
   size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
-  if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
-                                              size, 2, &service, &name))
+  if (size !=
+      GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
+                                      &service, &name))
   {
     GNUNET_break (0);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
@@ -407,9 +405,8 @@ notify_change (struct StatsEntry *se)
       wvm.wid = htonl (pos->wid);
       wvm.reserved = htonl (0);
       wvm.value = GNUNET_htonll (se->value);
-      GNUNET_SERVER_notification_context_unicast (nc,
-                                                  pos->client,
-                                                  &wvm.header, GNUNET_NO);
+      GNUNET_SERVER_notification_context_unicast (nc, pos->client, &wvm.header,
+                                                  GNUNET_NO);
       pos->last_value = se->value;
     }
     pos = pos->next;
@@ -424,8 +421,7 @@ notify_change (struct StatsEntry *se)
  * @param message the actual message
  */
 static void
-handle_set (void *cls,
-            struct GNUNET_SERVER_Client *client,
+handle_set (void *cls, struct GNUNET_SERVER_Client *client,
             const struct GNUNET_MessageHeader *message)
 {
   char *service;
@@ -452,8 +448,9 @@ handle_set (void *cls,
   size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
   msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
 
-  if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
-                                              size, 2, &service, &name))
+  if (size !=
+      GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1], size, 2, &service,
+                                      &name))
   {
     GNUNET_break (0);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
@@ -504,8 +501,8 @@ handle_set (void *cls,
       }
 #if DEBUG_STATISTICS
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Statistic `%s:%s' updated to value %llu.\n",
-                  service, name, pos->value);
+                  "Statistic `%s:%s' updated to value %llu.\n", service, name,
+                  pos->value);
 #endif
       if (changed)
         notify_change (pos);
@@ -530,8 +527,8 @@ handle_set (void *cls,
   start = pos;
 #if DEBUG_STATISTICS
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "New statistic on `%s:%s' with value %llu created.\n",
-              service, name, pos->value);
+              "New statistic on `%s:%s' with value %llu created.\n", service,
+              name, pos->value);
 #endif
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
@@ -545,8 +542,7 @@ handle_set (void *cls,
  * @param message the actual message
  */
 static void
-handle_watch (void *cls,
-              struct GNUNET_SERVER_Client *client,
+handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
               const struct GNUNET_MessageHeader *message)
 {
   char *service;
@@ -567,8 +563,9 @@ handle_watch (void *cls,
     return;
   }
   size = msize - sizeof (struct GNUNET_MessageHeader);
-  if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
-                                              size, 2, &service, &name))
+  if (size !=
+      GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
+                                      &service, &name))
   {
     GNUNET_break (0);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
@@ -576,8 +573,8 @@ handle_watch (void *cls,
   }
 #if DEBUG_STATISTICS
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Received request to watch statistic on `%s:%s'\n",
-              service, name);
+              "Received request to watch statistic on `%s:%s'\n", service,
+              name);
 #endif
   pos = start;
   while (pos != NULL)
@@ -588,8 +585,9 @@ handle_watch (void *cls,
   }
   if (pos == NULL)
   {
-    pos = GNUNET_malloc (sizeof (struct StatsEntry) +
-                         sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
+    pos =
+        GNUNET_malloc (sizeof (struct StatsEntry) +
+                       sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
     pos->next = start;
     pos->uid = uidgen++;
     pos->msg = (void *) &pos[1];
@@ -702,8 +700,7 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
  * @param c configuration to use
  */
 static void
-run (void *cls,
-     struct GNUNET_SERVER_Handle *server,
+run (void *cls, struct GNUNET_SERVER_Handle *server,
      const struct GNUNET_CONFIGURATION_Handle *c)
 {
   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
@@ -717,8 +714,8 @@ run (void *cls,
   nc = GNUNET_SERVER_notification_context_create (server, 16);
   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
   load (server);
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
-                                &shutdown_task, NULL);
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
+                                NULL);
 }
 
 
@@ -733,9 +730,7 @@ int
 main (int argc, char *const *argv)
 {
   return (GNUNET_OK ==
-          GNUNET_SERVICE_run (argc,
-                              argv,
-                              "statistics",
+          GNUNET_SERVICE_run (argc, argv, "statistics",
                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
 }