line
[oweals/gnunet.git] / src / statistics / gnunet-service-statistics.c
index 251302a660849fca3b9bd181f3c5caec515a5e0d..c506dee7b9aa44408be58b387a12f0a617da195a 100644 (file)
@@ -155,6 +155,17 @@ static struct GNUNET_SERVER_NotificationContext *nc;
 static uint32_t uidgen;
 
 
+static void
+inject_message (void *cls,
+               void *client,
+               const struct GNUNET_MessageHeader *msg)
+{
+  struct GNUNET_SERVER_Handle *server = cls;
+
+  GNUNET_break (GNUNET_OK == GNUNET_SERVER_inject (server, NULL, msg));
+}
+
+
 /**
  * Load persistent values from disk.  Disk format is
  * exactly the same format that we also use for
@@ -170,8 +181,7 @@ load (struct GNUNET_SERVER_Handle *server)
   struct GNUNET_DISK_MapHandle *mh;
   struct stat sb;
   char *buf;
-  size_t off;
-  const struct GNUNET_MessageHeader *msg;
+  struct GNUNET_SERVER_MessageStreamTokenizer *mst;
 
   fn = GNUNET_DISK_get_home_filename (cfg,
                                       "statistics", "statistics.data", NULL);
@@ -200,18 +210,16 @@ load (struct GNUNET_SERVER_Handle *server)
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               _("Loading %llu bytes of statistics from `%s'\n"),
               (unsigned long long) sb.st_size, fn);
-  off = 0;
-  while (off + sizeof (struct GNUNET_MessageHeader) < sb.st_size)
-    {
-      msg = (const struct GNUNET_MessageHeader *) &buf[off];
-      if ((ntohs (msg->size) + off > sb.st_size) ||
-          (GNUNET_OK != GNUNET_SERVER_inject (server, NULL, msg)))
-        {
-          GNUNET_break (0);
-          break;
-        }
-      off += ntohs (msg->size);
-    }
+  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_destroy (mst);
   GNUNET_break (GNUNET_OK == GNUNET_DISK_file_unmap (mh));
   GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
   GNUNET_free (fn);
@@ -321,8 +329,7 @@ make_client_entry (struct GNUNET_SERVER_Client *client)
 {
   struct ClientEntry *ce;
 
-  if (client == NULL)
-    return NULL;
+  GNUNET_assert (client != NULL);
   ce = client_head;
   while (ce != NULL)
     {
@@ -362,7 +369,8 @@ handle_get (void *cls,
   struct StatsEntry *pos;
   size_t size;
 
-  make_client_entry (client);
+  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))
@@ -445,7 +453,8 @@ handle_set (void *cls,
   int64_t delta;
   int changed;
 
-  make_client_entry (client);
+  if (client != NULL)
+    make_client_entry (client);
   msize = ntohs (message->size);
   if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
     {
@@ -564,6 +573,7 @@ handle_watch (void *cls,
   struct StatsEntry *pos;
   struct ClientEntry *ce;
   struct WatchEntry *we;
+  size_t slen;
 
   ce = make_client_entry (client);
   msize = ntohs (message->size);
@@ -604,10 +614,10 @@ handle_watch (void *cls,
       pos->msg->header.size = htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + 
                                     size);
       pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
-      memcpy (pos->msg, message, ntohs (message->size));
       pos->service = (const char *) &pos->msg[1];
-      memcpy (&pos->msg[1], service, strlen (service)+1);
-      pos->name = &pos->service[strlen (pos->service) + 1];
+      slen = strlen (service) + 1;
+      memcpy ((void*) pos->service, service, slen);
+      pos->name = &pos->service[slen];
       memcpy ((void*) pos->name, name, strlen (name)+1);
       start = pos;
     }
@@ -720,13 +730,11 @@ handle_client_disconnect (void *cls,
  * Process statistics requests.
  *
  * @param cls closure
- * @param sched scheduler to use
  * @param server the initialized server
  * @param c configuration to use
  */
 static void
 run (void *cls,
-     struct GNUNET_SCHEDULER_Handle *sched,
      struct GNUNET_SERVER_Handle *server,
      const struct GNUNET_CONFIGURATION_Handle *c)
 {
@@ -743,8 +751,7 @@ run (void *cls,
                                   &handle_client_disconnect,
                                   NULL);
   load (server);
-  GNUNET_SCHEDULER_add_delayed (sched,
-                               GNUNET_TIME_UNIT_FOREVER_REL,
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
                                &shutdown_task,
                                NULL);
 }