-LRN: calculate file size for single files when needed and use GNUNET_DISK_file_size...
[oweals/gnunet.git] / src / statistics / statistics_api.c
index 557694cfb5cdd056b7593f47d914336d7d5bd60e..c0b978f70e59184e87dfb7492c67aadd55ea8927 100644 (file)
@@ -333,8 +333,11 @@ do_disconnect (struct GNUNET_STATISTICS_Handle *h)
     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
     h->th = NULL;
   } 
-  GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
-  h->client = NULL;
+  if (NULL != h->client)
+  {
+    GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
+    h->client = NULL;
+  }
   h->receiving = GNUNET_NO;
   if (NULL != (c = h->current))
   {
@@ -382,10 +385,8 @@ try_connect (struct GNUNET_STATISTICS_Handle *h)
       schedule_watch_request (h, h->watches[i]);
     return GNUNET_YES;
   }
-#if DEBUG_STATISTICS
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       _("Failed to connect to statistics service!\n"));
-#endif
+       "Failed to connect to statistics service!\n");
   return GNUNET_NO;
 }
 
@@ -415,6 +416,18 @@ static void
 reconnect_later (struct GNUNET_STATISTICS_Handle *h)
 {
   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->backoff_task);
+  if (h->do_destroy)
+  {
+    /* So we are shutting down and the service is not reachable.
+     * Chances are that it's down for good and we are not going to connect to
+     * it anymore.
+     * Give up and don't sync the rest of the data.
+     */
+    GNUNET_break (0);
+    h->do_destroy = GNUNET_NO;
+    GNUNET_STATISTICS_destroy (h, GNUNET_NO);
+    return;
+  }
   h->backoff_task =
     GNUNET_SCHEDULER_add_delayed (h->backoff, &reconnect_task, h);
   h->backoff = GNUNET_TIME_relative_multiply (h->backoff, 2);
@@ -441,9 +454,7 @@ process_statistics_value_message (struct GNUNET_STATISTICS_Handle *h,
 
   if (h->current->aborted)
   {
-#if DEBUG_STATISTICS
     LOG (GNUNET_ERROR_TYPE_DEBUG, "Iteration was aborted, ignoring VALUE\n");
-#endif
     return GNUNET_OK;           /* don't bother */
   }
   size = ntohs (msg->size);
@@ -461,25 +472,19 @@ process_statistics_value_message (struct GNUNET_STATISTICS_Handle *h,
     GNUNET_break (0);
     return GNUNET_SYSERR;
   }
-#if DEBUG_STATISTICS
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Received valid statistic on `%s:%s': %llu\n",
        service, name, GNUNET_ntohll (smsg->value));
-#endif
   if (GNUNET_OK !=
       h->current->proc (h->current->cls, service, name,
                         GNUNET_ntohll (smsg->value),
                         0 !=
                         (ntohl (smsg->uid) & GNUNET_STATISTICS_PERSIST_BIT)))
   {
-#if DEBUG_STATISTICS
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Processing of remaining statistics aborted by client.\n");
-#endif
     h->current->aborted = GNUNET_YES;
   }
-#if DEBUG_STATISTICS
   LOG (GNUNET_ERROR_TYPE_DEBUG, "VALUE processed successfully\n");
-#endif
   return GNUNET_OK;
 }
 
@@ -489,7 +494,8 @@ process_statistics_value_message (struct GNUNET_STATISTICS_Handle *h,
  *
  * @param h statistics handle
  * @param msg the watch value message
- * @return GNUNET_OK if the message was well-formed, GNUNET_SYSERR if not
+ * @return GNUNET_OK if the message was well-formed, GNUNET_SYSERR if not,
+ *         GNUNET_NO if this watch has been cancelled
  */
 static int
 process_watch_value (struct GNUNET_STATISTICS_Handle *h,
@@ -513,6 +519,8 @@ process_watch_value (struct GNUNET_STATISTICS_Handle *h,
     return GNUNET_SYSERR;
   }
   w = h->watches[wid];
+  if (NULL == w)  
+    return GNUNET_NO;  
   (void) w->proc (w->proc_cls, w->subsystem, w->name,
                   GNUNET_ntohll (wvm->value),
                   0 != (ntohl (wvm->flags) & GNUNET_STATISTICS_PERSIST_BIT));
@@ -531,14 +539,12 @@ receive_stats (void *cls, const struct GNUNET_MessageHeader *msg)
 {
   struct GNUNET_STATISTICS_Handle *h = cls;
   struct GNUNET_STATISTICS_GetHandle *c;
+  int ret;
 
   if (msg == NULL)
   {
-#if DEBUG_STATISTICS
     LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
          "Error receiving statistics from service, is the service running?\n");
-#endif
     do_disconnect (h);
     reconnect_later (h);
     return;
@@ -546,9 +552,7 @@ receive_stats (void *cls, const struct GNUNET_MessageHeader *msg)
   switch (ntohs (msg->type))
   {
   case GNUNET_MESSAGE_TYPE_STATISTICS_END:
-#if DEBUG_STATISTICS
     LOG (GNUNET_ERROR_TYPE_DEBUG, "Received end of statistics marker\n");
-#endif
     if (NULL == (c = h->current))
     {
       GNUNET_break (0);
@@ -580,10 +584,8 @@ receive_stats (void *cls, const struct GNUNET_MessageHeader *msg)
       return;     
     }
     /* finally, look for more! */
-#if DEBUG_STATISTICS
     LOG (GNUNET_ERROR_TYPE_DEBUG,
         "Processing VALUE done, now reading more\n");
-#endif
     GNUNET_CLIENT_receive (h->client, &receive_stats, h,
                           GNUNET_TIME_absolute_get_remaining (h->
                                                               current->timeout));
@@ -591,9 +593,11 @@ receive_stats (void *cls, const struct GNUNET_MessageHeader *msg)
     return;
   case GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE:
     if (GNUNET_OK != 
-       process_watch_value (h, msg))
+       (ret = process_watch_value (h, msg)))
     {
       do_disconnect (h);
+      if (GNUNET_NO == ret)
+       h->backoff = GNUNET_TIME_UNIT_MILLISECONDS; 
       reconnect_later (h);
       return;
     }
@@ -633,10 +637,8 @@ transmit_get (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
   if (buf == NULL)
   {
     /* timeout / error */
-#if DEBUG_STATISTICS
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Transmission of request for statistics failed!\n");
-#endif  
     do_disconnect (handle);
     reconnect_later (handle);
     return 0;
@@ -654,10 +656,8 @@ transmit_get (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
                                              c->name));
   if (GNUNET_YES != handle->receiving)
   {
-#if DEBUG_STATISTICS
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Transmission of GET done, now reading response\n");
-#endif
     handle->receiving = GNUNET_YES;
     GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
                            GNUNET_TIME_absolute_get_remaining (c->timeout));
@@ -686,18 +686,14 @@ transmit_watch (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
   if (buf == NULL)
   {
     /* timeout / error */
-#if DEBUG_STATISTICS
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Transmission of request for statistics failed!\n");
-#endif
     do_disconnect (handle);
     reconnect_later (handle);
     return 0;
   }
-#if DEBUG_STATISTICS
   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);
@@ -848,14 +844,17 @@ GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h, int sync_first)
 {
   struct GNUNET_STATISTICS_GetHandle *pos;
   struct GNUNET_STATISTICS_GetHandle *next;
-  struct GNUNET_STATISTICS_GetHandle *prev;
   struct GNUNET_TIME_Relative timeout;
   int i;
 
   if (h == NULL)
     return;
+  GNUNET_assert (GNUNET_NO == h->do_destroy); // Don't call twice.
   if (GNUNET_SCHEDULER_NO_TASK != h->backoff_task)
+  {
     GNUNET_SCHEDULER_cancel (h->backoff_task);
+    h->backoff_task = GNUNET_SCHEDULER_NO_TASK;
+  }
   if (sync_first)
   {
     if (h->current != NULL)
@@ -868,48 +867,43 @@ GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h, int sync_first)
         h->current = NULL;
       }
     }
-    pos = h->action_head;
-    prev = NULL;
-    while (pos != NULL)
+    next = h->action_head; 
+    while (NULL != (pos = next))
     {
       next = pos->next;
       if (pos->type == ACTION_GET)
       {
-        if (prev == NULL)
-          h->action_head = next;
-        else
-          prev->next = next;
+       GNUNET_CONTAINER_DLL_remove (h->action_head,
+                                    h->action_tail,
+                                    pos);
         free_action_item (pos);
       }
-      else
-      {
-        prev = pos;
-      }
-      pos = next;
-    }
-    h->action_tail = prev;
-    if (h->current == NULL)
-    {
-      h->current = h->action_head;
-      if (h->action_head != NULL)
-      {
-        h->action_head = h->action_head->next;
-        if (h->action_head == NULL)
-          h->action_tail = NULL;
-      }
     }
+    if ( (NULL == h->current) &&
+        (NULL != (h->current = h->action_head)) )
+      GNUNET_CONTAINER_DLL_remove (h->action_head,
+                                  h->action_tail,
+                                  h->current);
     h->do_destroy = GNUNET_YES;
     if ((h->current != NULL) && (h->th == NULL))
     {
-      timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
-      h->th =
+      if (NULL == h->client)
+      {
+       /* instant-connect (regardless of back-off) to submit final value */
+       h->client = GNUNET_CLIENT_connect ("statistics", h->cfg);
+      }
+      if (NULL != h->client)
+      {
+       timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
+       h->th =
           GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
                                                timeout, GNUNET_YES,
                                                &transmit_action, h);
-      GNUNET_assert (NULL != h->th);
+       GNUNET_assert (NULL != h->th);
+      }
     }
     if (h->th != NULL)
-      return;
+      return; /* do not finish destruction just yet */
   }
   while (NULL != (pos = h->action_head))
   {
@@ -972,10 +966,8 @@ schedule_action (struct GNUNET_STATISTICS_Handle *h)
                                             timeout, GNUNET_YES,
                                             &transmit_action, h)))
   {
-#if DEBUG_STATISTICS
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Failed to transmit request to statistics service.\n");
-#endif
     do_disconnect (h);
     reconnect_later (h);
   }
@@ -1062,8 +1054,6 @@ GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh)
 
 /**
  * Watch statistics from the peer (be notified whenever they change).
- * Note that the only way to cancel a "watch" request is to destroy
- * the statistics handle given as the first argument to this call.
  *
  * @param handle identification of the statistics service
  * @param subsystem limit to the specified subsystem, never NULL
@@ -1092,6 +1082,46 @@ GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
 }
 
 
+/**
+ * Stop watching statistics from the peer.  
+ *
+ * @param handle identification of the statistics service
+ * @param subsystem limit to the specified subsystem, never NULL
+ * @param name name of the statistic value, never NULL
+ * @param proc function to call on each value
+ * @param proc_cls closure for proc
+ * @return GNUNET_OK on success, GNUNET_SYSERR on error (no such watch)
+ */
+int
+GNUNET_STATISTICS_watch_cancel (struct GNUNET_STATISTICS_Handle *handle,
+                               const char *subsystem, const char *name,
+                               GNUNET_STATISTICS_Iterator proc, void *proc_cls)
+{
+  struct GNUNET_STATISTICS_WatchEntry *w;
+  unsigned int i;
+
+  if (handle == NULL)
+    return GNUNET_SYSERR;
+  for (i=0;i<handle->watches_size;i++)
+  {
+    w = handle->watches[i];
+    if ( (w->proc == proc) &&
+        (w->proc_cls == proc_cls) &&
+        (0 == strcmp (w->name, name)) &&
+        (0 == strcmp (w->subsystem, subsystem)) )
+    {
+      GNUNET_free (w->name);
+      GNUNET_free (w->subsystem);
+      GNUNET_free (w);
+      handle->watches[i] = NULL;      
+      return GNUNET_OK;
+    }   
+  }
+  return GNUNET_SYSERR;
+}
+
+
+
 /**
  * Queue a request to change a statistic.
  *