- cmd line arg
[oweals/gnunet.git] / src / statistics / gnunet-service-statistics.c
index 3305dc6f4ce84b349f5a4c216ab95952a32d766e..a1a2c3abdcf79b6d492e4c52874ca6408f8743a9 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"
 struct WatchEntry
 {
 
+  /**
+   * Watch entries are kept in a linked list.
+   */
   struct WatchEntry *next;
 
+  /**
+   * Watch entries are kept in a linked list.
+   */
   struct WatchEntry *prev;
 
+  /**
+   * For which client is this watch entry?
+   */
   struct GNUNET_SERVER_Client *client;
 
+  /**
+   * Last value we communicated to the client for this watch entry.
+   */
   uint64_t last_value;
 
+  /**
+   * Unique watch number for this client and this watched value.
+   */
   uint32_t wid;
 
 };
@@ -61,13 +74,24 @@ struct WatchEntry
  */
 struct ClientEntry
 {
-
+  /**
+   * Clients are kept in a linked list.
+   */
   struct ClientEntry *next;
 
+  /**
+   * Clients are kept in a linked list.
+   */
   struct ClientEntry *prev;
 
+  /**
+   * Corresponding server handle.
+   */
   struct GNUNET_SERVER_Client *client;
 
+  /**
+   * Maximum watch ID used by this client so far.
+   */
   uint32_t max_wid;
 
 };
@@ -140,10 +164,21 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg;
  */
 static struct StatsEntry *start;
 
+/**
+ * Head of linked list of connected clients.
+ */
 static struct ClientEntry *client_head;
 
+/**
+ * Tail of linked list of connected clients.
+ */
 static struct ClientEntry *client_tail;
 
+/**
+ * Handle to our server.
+ */
+static struct GNUNET_SERVER_Handle *srv;
+
 /**
  * Our notification context.
  */
@@ -154,7 +189,19 @@ static struct GNUNET_SERVER_NotificationContext *nc;
  */
 static uint32_t uidgen;
 
+/**
+ * Set to YES if we are shutting down as soon as possible.
+ */
+static int in_shutdown;
 
+
+/**
+ * Inject a message to our server with a client of 'NULL'.
+ *
+ * @param cls the 'struct GNUNET_SERVER_Handle'
+ * @param client unused
+ * @param msg message to inject
+ */
 static void
 inject_message (void *cls, void *client, const struct GNUNET_MessageHeader *msg)
 {
@@ -175,48 +222,53 @@ static void
 load (struct GNUNET_SERVER_Handle *server)
 {
   char *fn;
-  struct GNUNET_DISK_FileHandle *fh;
-  struct GNUNET_DISK_MapHandle *mh;
-  struct stat sb;
+  struct GNUNET_BIO_ReadHandle *rh;
+  uint64_t fsize;
   char *buf;
   struct GNUNET_SERVER_MessageStreamTokenizer *mst;
+  char *emsg;
 
   fn = GNUNET_DISK_get_home_filename (cfg, "statistics", "statistics.data",
                                       NULL);
   if (fn == NULL)
     return;
-  if ((0 != stat (fn, &sb)) || (sb.st_size == 0))
+  if ((GNUNET_OK != GNUNET_DISK_file_size (fn, &fsize, GNUNET_NO, GNUNET_YES)) || (fsize == 0))
   {
     GNUNET_free (fn);
     return;
   }
-  fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
-  if (!fh)
+  buf = GNUNET_malloc (fsize);
+  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, fsize))
   {
-    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;
   }
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               _("Loading %llu bytes of statistics from `%s'\n"),
-              (unsigned long long) sb.st_size, fn);
+              fsize, fn);
   mst = GNUNET_SERVER_mst_create (&inject_message, server);
   GNUNET_break (GNUNET_OK ==
-                GNUNET_SERVER_mst_receive (mst, NULL, buf, sb.st_size,
+                GNUNET_SERVER_mst_receive (mst, NULL, buf, fsize,
                                            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);
 }
 
+
 /**
  * Write persistent statistics to disk.
  */
@@ -225,40 +277,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;
+  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
@@ -271,6 +322,9 @@ save ()
 
 /**
  * Transmit the given stats value.
+ *
+ * @param client receiver of the value
+ * @param e value to transmit
  */
 static void
 transmit (struct GNUNET_SERVER_Client *client, const struct StatsEntry *e)
@@ -293,11 +347,9 @@ transmit (struct GNUNET_SERVER_Client *client, const struct StatsEntry *e)
   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);
-#endif
   GNUNET_SERVER_notification_context_unicast (nc, client, &m->header,
                                               GNUNET_NO);
   GNUNET_free (m);
@@ -306,6 +358,11 @@ transmit (struct GNUNET_SERVER_Client *client, const struct StatsEntry *e)
 
 /**
  * Does this entry match the request?
+ *
+ * @param e an entry
+ * @param service name of service to match
+ * @param name value to match
+ * @return 1 if they match, 0 if not
  */
 static int
 matches (const struct StatsEntry *e, const char *service, const char *name)
@@ -315,6 +372,12 @@ matches (const struct StatsEntry *e, const char *service, const char *name)
 }
 
 
+/**
+ * Find a client entry for the given client handle, or create one.
+ *
+ * @param client handle to match
+ * @return corresponding client entry struct
+ */
 static struct ClientEntry *
 make_client_entry (struct GNUNET_SERVER_Client *client)
 {
@@ -328,6 +391,11 @@ make_client_entry (struct GNUNET_SERVER_Client *client)
       return ce;
     ce = ce->next;
   }
+  if (NULL == nc)
+  {
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return NULL;
+  }
   ce = GNUNET_malloc (sizeof (struct ClientEntry));
   ce->client = client;
   GNUNET_SERVER_client_keep (client);
@@ -356,8 +424,9 @@ handle_get (void *cls, struct GNUNET_SERVER_Client *client,
   struct StatsEntry *pos;
   size_t size;
 
-  if (client != NULL)
-    make_client_entry (client);
+  if ( (NULL != client) &&
+       (NULL == make_client_entry (client)) )
+    return; /* new client during shutdown */
   size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
   if (size !=
       GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
@@ -367,18 +436,12 @@ handle_get (void *cls, struct GNUNET_SERVER_Client *client,
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
-#if DEBUG_STATISTICS
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Received request for statistics on `%s:%s'\n",
               strlen (service) ? service : "*", strlen (name) ? name : "*");
-#endif
-  pos = start;
-  while (pos != NULL)
-  {
+  for (pos = start; NULL != pos; pos = pos->next)
     if (matches (pos, service, name))
       transmit (client, pos);
-    pos = pos->next;
-  }
   end.size = htons (sizeof (struct GNUNET_MessageHeader));
   end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
   GNUNET_SERVER_notification_context_unicast (nc, client, &end, GNUNET_NO);
@@ -386,29 +449,31 @@ handle_get (void *cls, struct GNUNET_SERVER_Client *client,
 }
 
 
+/**
+ * Notify all clients listening about a change to a value.
+ *
+ * @param se value that changed
+ */
 static void
 notify_change (struct StatsEntry *se)
 {
   struct GNUNET_STATISTICS_WatchValueMessage wvm;
   struct WatchEntry *pos;
 
-  pos = se->we_head;
-  while (pos != NULL)
+  for (pos = se->we_head; NULL != pos; pos = pos->next)
   {
-    if (pos->last_value != se->value)
-    {
-      wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
-      wvm.header.size =
-          htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
-      wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
-      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);
-      pos->last_value = se->value;
-    }
-    pos = pos->next;
+    if (pos->last_value == se->value)
+      continue;
+    wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
+    wvm.header.size =
+      htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
+    wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
+    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);
+    pos->last_value = se->value;
   }
 }
 
@@ -435,8 +500,9 @@ handle_set (void *cls, struct GNUNET_SERVER_Client *client,
   int64_t delta;
   int changed;
 
-  if (client != NULL)
-    make_client_entry (client);
+  if ( (NULL != client) &&
+       (NULL == make_client_entry (client)) )
+    return; /* new client during shutdown */
   msize = ntohs (message->size);
   if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
   {
@@ -457,11 +523,9 @@ handle_set (void *cls, struct GNUNET_SERVER_Client *client,
   }
   flags = ntohl (msg->flags);
   value = GNUNET_ntohll (msg->value);
-#if DEBUG_STATISTICS
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
               service, name, (unsigned int) flags, (unsigned long long) value);
-#endif
   pos = start;
   prev = NULL;
   while (pos != NULL)
@@ -498,11 +562,9 @@ handle_set (void *cls, struct GNUNET_SERVER_Client *client,
         pos->next = start;
         start = pos;
       }
-#if DEBUG_STATISTICS
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "Statistic `%s:%s' updated to value %llu.\n", service, name,
                   pos->value);
-#endif
       if (changed)
         notify_change (pos);
       GNUNET_SERVER_receive_done (client, GNUNET_OK);
@@ -524,11 +586,9 @@ handle_set (void *cls, struct GNUNET_SERVER_Client *client,
   pos->name = &pos->service[strlen (pos->service) + 1];
 
   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);
-#endif
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
@@ -553,6 +613,11 @@ handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
   struct WatchEntry *we;
   size_t slen;
 
+  if (NULL == nc)
+  {
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
+  }
   ce = make_client_entry (client);
   msize = ntohs (message->size);
   if (msize < sizeof (struct GNUNET_MessageHeader))
@@ -570,11 +635,9 @@ handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
-#if DEBUG_STATISTICS
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Received request to watch statistic on `%s:%s'\n", service,
               name);
-#endif
   pos = start;
   while (pos != NULL)
   {
@@ -612,27 +675,20 @@ handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
 
 
 /**
- * Task run during shutdown.
- *
- * @param cls unused
- * @param tc unused
+ * Actually perform the shutdown.
  */
-static void
-shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+static void 
+do_shutdown ()
 {
-  struct ClientEntry *ce;
   struct WatchEntry *we;
   struct StatsEntry *se;
 
+  if (NULL == nc)
+    return;
   save ();
   GNUNET_SERVER_notification_context_destroy (nc);
-  nc = NULL;
-  while (NULL != (ce = client_head))
-  {
-    GNUNET_SERVER_client_drop (ce->client);
-    GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
-    GNUNET_free (ce);
-  }
+  nc = NULL;  
+  GNUNET_assert (NULL == client_head);
   while (NULL != (se = start))
   {
     start = se->next;
@@ -647,6 +703,22 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
+/**
+ * Task run during shutdown.
+ *
+ * @param cls unused
+ * @param tc unused
+ */
+static void
+shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  in_shutdown = GNUNET_YES;
+  if (NULL != client_head)
+    return;
+  do_shutdown ();
+}
+
+
 /**
  * A client disconnected.  Remove all of its data structure entries.
  *
@@ -688,6 +760,9 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
     }
     se = se->next;
   }
+  if ( (NULL == client_head) &&
+       (GNUNET_YES == in_shutdown) )
+    do_shutdown ();
 }
 
 
@@ -709,7 +784,9 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     {NULL, NULL, 0, 0}
   };
   cfg = c;
+  srv = server;
   GNUNET_SERVER_add_handlers (server, handlers);
+  GNUNET_SERVER_ignore_shutdown (server, GNUNET_YES);
   nc = GNUNET_SERVER_notification_context_create (server, 16);
   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
   load (server);