spacing
[oweals/gnunet.git] / src / statistics / statistics_api.c
index 29b1cbbfdcba2bf557d9ebd401b3d1b98a634465..290a7b93fe3afdcd96b4397f8b663fc6816a04d5 100644 (file)
@@ -166,11 +166,6 @@ struct GNUNET_STATISTICS_GetHandle
  */
 struct GNUNET_STATISTICS_Handle
 {
-  /**
-   * Our scheduler.
-   */
-  struct GNUNET_SCHEDULER_Handle *sched;
-
   /**
    * Name of our subsystem.
    */
@@ -318,7 +313,7 @@ try_connect (struct GNUNET_STATISTICS_Handle *ret)
   unsigned int i;
   if (ret->client != NULL)
     return GNUNET_YES;
-  ret->client = GNUNET_CLIENT_connect (ret->sched, "statistics", ret->cfg);
+  ret->client = GNUNET_CLIENT_connect ("statistics", ret->cfg);
   if (ret->client != NULL)
     {
       for (i=0;i<ret->watches_size;i++)
@@ -441,6 +436,7 @@ process_watch_value (struct GNUNET_STATISTICS_Handle *h,
       return GNUNET_SYSERR;
     }
   wvm = (const struct GNUNET_STATISTICS_WatchValueMessage *)msg;
+  GNUNET_break (0 == ntohl (wvm->reserved));
   wid = ntohl (wvm->wid);
   if (wid >= h->watches_size)
     {
@@ -624,6 +620,11 @@ transmit_watch (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
       finish (handle, GNUNET_SYSERR);
       return 0;
     }
+#if DEBUG_STATISTICS
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Transmitting watch request for `%s'\n",
+             handle->current->name);
+#endif
   slen1 = strlen (handle->current->subsystem) + 1;
   slen2 = strlen (handle->current->name) + 1;
   msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
@@ -637,7 +638,7 @@ transmit_watch (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
                                              2,
                                              handle->current->subsystem,
                                              handle->current->name));
-  if (! handle->receiving)
+  if (GNUNET_YES != handle->receiving)
     {
       handle->receiving = GNUNET_YES;
       GNUNET_CLIENT_receive (handle->client,
@@ -645,6 +646,7 @@ transmit_watch (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
                             handle,
                             GNUNET_TIME_UNIT_FOREVER_REL);
     }
+  finish (handle, GNUNET_OK);
   return msize;
 }
 
@@ -726,27 +728,28 @@ transmit_action (void *cls, size_t size, void *buf)
 /**
  * Get handle for the statistics service.
  *
- * @param sched scheduler to use
  * @param subsystem name of subsystem using the service
  * @param cfg services configuration in use
  * @return handle to use
  */
 struct GNUNET_STATISTICS_Handle *
-GNUNET_STATISTICS_create (struct GNUNET_SCHEDULER_Handle *sched,
-                          const char *subsystem,
+GNUNET_STATISTICS_create (const char *subsystem,
                           const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   struct GNUNET_STATISTICS_Handle *ret;
 
   GNUNET_assert (subsystem != NULL);
-  GNUNET_assert (sched != NULL);
   GNUNET_assert (cfg != NULL);
   ret = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_Handle));
-  ret->sched = sched;
   ret->cfg = cfg;
   ret->subsystem = GNUNET_strdup (subsystem);
   ret->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
-  try_connect (ret);
+  if (GNUNET_YES != try_connect (ret))
+    {
+      GNUNET_free (ret->subsystem);
+      GNUNET_free (ret);
+      return NULL;
+    }
   return ret;
 }
 
@@ -769,9 +772,9 @@ GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h,
   struct GNUNET_TIME_Relative timeout;
   int i;
 
+  if (h == NULL) return;
   if (GNUNET_SCHEDULER_NO_TASK != h->backoff_task)
-    GNUNET_SCHEDULER_cancel (h->sched,
-                            h->backoff_task);
+    GNUNET_SCHEDULER_cancel (h->backoff_task);
   if (sync_first)
     {
       if (h->current != NULL)
@@ -883,8 +886,7 @@ schedule_action (struct GNUNET_STATISTICS_Handle *h)
     return;                     /* action already pending */
   if (GNUNET_YES != try_connect (h))
     {
-      h->backoff_task = GNUNET_SCHEDULER_add_delayed (h->sched,
-                                                     h->backoff,
+      h->backoff_task = GNUNET_SCHEDULER_add_delayed (h->backoff,
                                                      &finish_task,
                                                      h);
       h->backoff = GNUNET_TIME_relative_multiply (h->backoff, 2);
@@ -923,6 +925,7 @@ schedule_action (struct GNUNET_STATISTICS_Handle *h)
     }
 }
 
+
 /**
  * Get statistic from the peer.
  *
@@ -1030,6 +1033,8 @@ GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
 {
   struct GNUNET_STATISTICS_WatchEntry *w;
 
+  if (handle == NULL) 
+    return GNUNET_SYSERR;
   w = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_WatchEntry));
   w->subsystem = GNUNET_strdup (subsystem);
   w->name = GNUNET_strdup (name);