make it easy to start service by hand
[oweals/gnunet.git] / src / statistics / gnunet-service-statistics.c
index 1c0981b80520dd3a2408a3f92c4f5065f38b0bbb..c34a00ddc2611f60f9082cbdec37d3be4debfdf3 100644 (file)
@@ -22,6 +22,9 @@
  * @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_disk_lib.h"
@@ -80,23 +83,24 @@ struct StatsEntry
 };
 
 /**
- * Linked list of our active statistics.
+ * Our configuration.
  */
-static struct StatsEntry *start;
+static const struct GNUNET_CONFIGURATION_Handle *cfg;
 
 /**
- * Counter used to generate unique values.
+ * Linked list of our active statistics.
  */
-static uint32_t uidgen;
+static struct StatsEntry *start;
 
 /**
  * Load persistent values from disk.  Disk format is
  * exactly the same format that we also use for
  * setting the values over the network.
+ *
+ * @param server handle to the server context
  */
 static void
-load (struct GNUNET_SERVER_Handle *server,
-      struct GNUNET_CONFIGURATION_Handle *cfg)
+load (struct GNUNET_SERVER_Handle *server)
 {
   char *fn;
   struct GNUNET_DISK_FileHandle *fh;
@@ -115,13 +119,14 @@ load (struct GNUNET_SERVER_Handle *server,
       GNUNET_free (fn);
       return;
     }
-  fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ);
+  fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ,
+                             GNUNET_DISK_PERM_NONE);
   if (!fh)
     {
       GNUNET_free (fn);
       return;
     }
-  buf = GNUNET_DISK_file_map (fh, &mh, GNUNET_DISK_MAP_READ, sb.st_size);
+  buf = GNUNET_DISK_file_map (fh, &mh, GNUNET_DISK_MAP_TYPE_READ, sb.st_size);
   if (NULL == buf)
     {
       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "mmap", fn);
@@ -149,15 +154,11 @@ load (struct GNUNET_SERVER_Handle *server,
   GNUNET_free (fn);
 }
 
-
 /**
  * Write persistent statistics to disk.
- *
- * @param cls closure
- * @param cfg configuration to use
  */
 static void
-save (void *cls, struct GNUNET_CONFIGURATION_Handle *cfg)
+save ()       
 {
   struct StatsEntry *pos;
   char *fn;
@@ -212,16 +213,15 @@ transmit (struct GNUNET_SERVER_TransmitContext *tc,
           const struct StatsEntry *e)
 {
   struct GNUNET_STATISTICS_ReplyMessage *m;
-  struct GNUNET_MessageHeader *h;
   size_t size;
-  uint16_t msize;
 
   size =
     sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
     strlen (e->name) + 1;
   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
-  msize = size - sizeof (struct GNUNET_MessageHeader);
   m = GNUNET_malloc (size);
+  m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
+  m->header.size = htons (size);
   m->uid = htonl (e->uid);
   if (e->persistent)
     m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
@@ -235,11 +235,7 @@ transmit (struct GNUNET_SERVER_TransmitContext *tc,
               "Transmitting value for `%s:%s': %llu\n",
               e->service, e->name, e->value);
 #endif
-  h = &m->header;
-  GNUNET_SERVER_transmit_context_append (tc,
-                                         &h[1],
-                                         msize,
-                                         GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
+  GNUNET_SERVER_transmit_context_append_message (tc, &m->header);
   GNUNET_free (m);
 }
 
@@ -297,8 +293,8 @@ handle_get (void *cls,
         transmit (tc, pos);
       pos = pos->next;
     }
-  GNUNET_SERVER_transmit_context_append (tc, NULL, 0,
-                                         GNUNET_MESSAGE_TYPE_STATISTICS_END);
+  GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
+                                             GNUNET_MESSAGE_TYPE_STATISTICS_END);
   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
 }
 
@@ -315,6 +311,11 @@ handle_set (void *cls,
             struct GNUNET_SERVER_Client *client,
             const struct GNUNET_MessageHeader *message)
 {
+  /**
+   * Counter used to generate unique values.
+   */
+  static uint32_t uidgen;
+
   char *service;
   char *name;
   uint16_t msize;
@@ -419,14 +420,17 @@ handle_set (void *cls,
 
 
 /**
- * List of handlers for the messages understood by this
- * service.
+ * Task run during shutdown.
+ *
+ * @param cls unused
+ * @param tc unused
  */
-static struct GNUNET_SERVER_MessageHandler handlers[] = {
-  {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
-  {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
-  {NULL, NULL, 0, 0}
-};
+static void
+shutdown_task (void *cls,
+              const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  save ();
+}
 
 
 /**
@@ -435,16 +439,26 @@ static struct GNUNET_SERVER_MessageHandler handlers[] = {
  * @param cls closure
  * @param sched scheduler to use
  * @param server the initialized server
- * @param cfg configuration to use
+ * @param c configuration to use
  */
 static void
 run (void *cls,
      struct GNUNET_SCHEDULER_Handle *sched,
      struct GNUNET_SERVER_Handle *server,
-     struct GNUNET_CONFIGURATION_Handle *cfg)
+     const struct GNUNET_CONFIGURATION_Handle *c)
 {
+  static const struct GNUNET_SERVER_MessageHandler handlers[] = {
+    {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
+    {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
+    {NULL, NULL, 0, 0}
+  };
+  cfg = c;
   GNUNET_SERVER_add_handlers (server, handlers);
-  load (server, cfg);
+  load (server);
+  GNUNET_SCHEDULER_add_delayed (sched,
+                               GNUNET_TIME_UNIT_FOREVER_REL,
+                               &shutdown_task,
+                               NULL);
 }
 
 
@@ -461,7 +475,9 @@ main (int argc, char *const *argv)
   return (GNUNET_OK ==
           GNUNET_SERVICE_run (argc,
                               argv,
-                              "statistics", &run, NULL, &save, NULL)) ? 0 : 1;
+                              "statistics",
+                             GNUNET_SERVICE_OPTION_NONE,
+                             &run, NULL)) ? 0 : 1;
 }
 
 /* end of gnunet-service-statistics.c */