-LRN: calculate file size for single files when needed and use GNUNET_DISK_file_size...
[oweals/gnunet.git] / src / statistics / statistics_api.c
index c30279e718d6c228f6aea2a0e9205b6219c6b35e..c0b978f70e59184e87dfb7492c67aadd55ea8927 100644 (file)
@@ -385,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;
 }
 
@@ -418,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);
@@ -444,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);
@@ -464,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;
 }
 
@@ -541,10 +543,8 @@ receive_stats (void *cls, const struct GNUNET_MessageHeader *msg)
 
   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;
@@ -552,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);
@@ -586,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));
@@ -641,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;
@@ -662,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));
@@ -694,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);
@@ -856,12 +844,12 @@ 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);
@@ -879,36 +867,23 @@ 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))
     {
@@ -928,7 +903,7 @@ GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h, int sync_first)
       }
     }
     if (h->th != NULL)
-      return;
+      return; /* do not finish destruction just yet */
   }
   while (NULL != (pos = h->action_head))
   {
@@ -991,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);
   }