if (NULL != h->th)
{
- h->th = NULL;
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))
{
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;
}
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);
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);
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;
}
*
* @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,
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));
{
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;
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);
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));
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;
}
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;
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));
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);
{
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)
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))
{
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);
}
/**
* 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
}
+/**
+ * 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.
*