/*
This file is part of GNUnet.
- (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2009, 2010, 2011 GNUnet e.V.
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
*/
/**
*/
struct GNUNET_TIME_Absolute timeout;
+ /**
+ * Task run on timeout.
+ */
+ struct GNUNET_SCHEDULER_Task * timeout_task;
+
/**
* Associated value.
*/
/**
* Task doing exponential back-off trying to reconnect.
*/
- GNUNET_SCHEDULER_TaskIdentifier backoff_task;
+ struct GNUNET_SCHEDULER_Task * backoff_task;
/**
* Time for next connect retry.
schedule_watch_request (struct GNUNET_STATISTICS_Handle *h,
struct GNUNET_STATISTICS_WatchEntry *watch)
{
-
struct GNUNET_STATISTICS_GetHandle *ai;
size_t slen;
size_t nlen;
size_t nsize;
- GNUNET_assert (NULL != h);
- GNUNET_assert (NULL != watch);
-
slen = strlen (watch->subsystem) + 1;
nlen = strlen (watch->name) + 1;
nsize = sizeof (struct GNUNET_MessageHeader) + slen + nlen;
static void
free_action_item (struct GNUNET_STATISTICS_GetHandle *gh)
{
+ if (NULL != gh->timeout_task)
+ {
+ GNUNET_SCHEDULER_cancel (gh->timeout_task);
+ gh->timeout_task = NULL;
+ }
GNUNET_free_non_null (gh->subsystem);
GNUNET_free_non_null (gh->name);
GNUNET_free (gh);
h->current = NULL;
if ( (NULL != c->cont) &&
(GNUNET_YES != c->aborted) )
+ {
c->cont (c->cls, GNUNET_SYSERR);
+ c->cont = NULL;
+ }
free_action_item (c);
}
if (NULL != h->client)
struct GNUNET_STATISTICS_GetHandle *gn;
unsigned int i;
- if (GNUNET_SCHEDULER_NO_TASK != h->backoff_task)
+ if (NULL != h->backoff_task)
return GNUNET_NO;
if (NULL != h->client)
return GNUNET_YES;
/**
* We've waited long enough, reconnect now.
*
- * @param cls the 'struct GNUNET_STATISTICS_Handle' to reconnect
- * @param tc scheduler context (unused)
+ * @param cls the `struct GNUNET_STATISTICS_Handle` to reconnect
*/
static void
-reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+reconnect_task (void *cls)
{
struct GNUNET_STATISTICS_Handle *h = cls;
- h->backoff_task = GNUNET_SCHEDULER_NO_TASK;
+ h->backoff_task = NULL;
schedule_action (h);
}
* Task used by 'reconnect_later' to shutdown the handle
*
* @param cls the statistics handle
- * @param tc scheduler context
*/
static void
-do_destroy (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_destroy (void *cls)
{
struct GNUNET_STATISTICS_Handle *h = cls;
int loss;
struct GNUNET_STATISTICS_GetHandle *gh;
- GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->backoff_task);
+ GNUNET_assert (NULL == h->backoff_task);
if (GNUNET_YES == h->do_destroy)
{
/* So we are shutting down and the service is not reachable.
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
_("Could not save some persistent statistics\n"));
h->do_destroy = GNUNET_NO;
- GNUNET_SCHEDULER_add_continuation (&do_destroy, h,
- GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+ GNUNET_SCHEDULER_add_now (&do_destroy, h);
return;
}
h->backoff_task =
/**
- * Process a 'GNUNET_MESSAGE_TYPE_STATISTICS_VALUE' message.
+ * Process a #GNUNET_MESSAGE_TYPE_STATISTICS_VALUE message.
*
* @param h statistics handle
* @param msg message received from the service, never NULL
- * @return GNUNET_OK if the message was well-formed
+ * @return #GNUNET_OK if the message was well-formed
*/
static int
process_statistics_value_message (struct GNUNET_STATISTICS_Handle *h,
if (h->current->aborted)
{
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Iteration was aborted, ignoring VALUE\n");
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Iteration was aborted, ignoring VALUE\n");
return GNUNET_OK; /* don't bother */
}
size = ntohs (msg->size);
GNUNET_break (0);
return GNUNET_SYSERR;
}
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Received valid statistic on `%s:%s': %llu\n",
- service, name, GNUNET_ntohll (smsg->value));
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Received valid statistic on `%s:%s': %llu\n",
+ service, name,
+ GNUNET_ntohll (smsg->value));
if (GNUNET_OK !=
h->current->proc (h->current->cls, service, name,
GNUNET_ntohll (smsg->value),
"Processing of remaining statistics aborted by client.\n");
h->current->aborted = GNUNET_YES;
}
- LOG (GNUNET_ERROR_TYPE_DEBUG, "VALUE processed successfully\n");
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "VALUE processed successfully\n");
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,
- * GNUNET_NO if this watch has been cancelled
+ * @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,
}
+/**
+ * Task used to destroy the statistics handle.
+ *
+ * @param cls the `struct GNUNET_STATISTICS_Handle`
+ */
static void
-destroy_task (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc)
+destroy_task (void *cls)
{
struct GNUNET_STATISTICS_Handle *h = cls;
* @param msg message received, NULL on timeout or fatal error
*/
static void
-receive_stats (void *cls, const struct GNUNET_MessageHeader *msg)
+receive_stats (void *cls,
+ const struct GNUNET_MessageHeader *msg)
{
struct GNUNET_STATISTICS_Handle *h = cls;
struct GNUNET_STATISTICS_GetHandle *c;
return;
}
h->do_destroy = GNUNET_NO;
- GNUNET_SCHEDULER_add_continuation (&destroy_task, h,
- GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+ GNUNET_SCHEDULER_add_now (&destroy_task, h);
break;
case GNUNET_MESSAGE_TYPE_STATISTICS_END:
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Received end of statistics marker\n");
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Received end of statistics marker\n");
if (NULL == (c = h->current))
{
GNUNET_break (0);
h->current = NULL;
schedule_action (h);
if (NULL != c->cont)
+ {
c->cont (c->cls, GNUNET_OK);
+ c->cont = NULL;
+ }
free_action_item (c);
return;
case GNUNET_MESSAGE_TYPE_STATISTICS_VALUE:
* the response).
*
* @param handle statistics handle
- * @param size how many bytes can we write to buf
+ * @param size how many bytes can we write to @a buf
* @param buf where to write requests to the service
- * @return number of bytes written to buf
+ * @return number of bytes written to @a buf
*/
static size_t
-transmit_get (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
+transmit_get (struct GNUNET_STATISTICS_Handle *handle,
+ size_t size,
+ void *buf)
{
struct GNUNET_STATISTICS_GetHandle *c;
struct GNUNET_MessageHeader *hdr;
* the response).
*
* @param handle statistics handle
- * @param size how many bytes can we write to buf
+ * @param size how many bytes can we write to @a buf
* @param buf where to write requests to the service
- * @return number of bytes written to buf
+ * @return number of bytes written to @a buf
*/
static size_t
-transmit_watch (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
+transmit_watch (struct GNUNET_STATISTICS_Handle *handle,
+ size_t size,
+ void *buf)
{
struct GNUNET_MessageHeader *hdr;
size_t slen1;
reconnect_later (handle);
return 0;
}
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting watch request for `%s'\n",
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Transmitting watch request for `%s'\n",
handle->current->name);
slen1 = strlen (handle->current->subsystem) + 1;
slen2 = strlen (handle->current->name) + 1;
* Transmit a SET/UPDATE request.
*
* @param handle statistics handle
- * @param size how many bytes can we write to buf
+ * @param size how many bytes can we write to @a buf
* @param buf where to write requests to the service
- * @return number of bytes written to buf
+ * @return number of bytes written to @a buf
*/
static size_t
-transmit_set (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
+transmit_set (struct GNUNET_STATISTICS_Handle *handle,
+ size_t size,
+ void *buf)
{
struct GNUNET_STATISTICS_SetMessage *r;
size_t slen;
/**
* Function called when we are ready to transmit a request to the service.
*
- * @param cls the 'struct GNUNET_STATISTICS_Handle'
- * @param size how many bytes can we write to buf
+ * @param cls the `struct GNUNET_STATISTICS_Handle`
+ * @param size how many bytes can we write to @a buf
* @param buf where to write requests to the service
- * @return number of bytes written to buf
+ * @return number of bytes written to @a buf
*/
static size_t
transmit_action (void *cls, size_t size, void *buf)
* it).
*
* @param h statistics handle to destroy
- * @param sync_first set to GNUNET_YES if pending SET requests should
+ * @param sync_first set to #GNUNET_YES if pending SET requests should
* be completed
*/
void
-GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h, int sync_first)
+GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h,
+ int sync_first)
{
struct GNUNET_STATISTICS_GetHandle *pos;
struct GNUNET_STATISTICS_GetHandle *next;
if (NULL == h)
return;
GNUNET_assert (GNUNET_NO == h->do_destroy); // Don't call twice.
- if (GNUNET_SCHEDULER_NO_TASK != h->backoff_task)
+ if (NULL != h->backoff_task)
{
GNUNET_SCHEDULER_cancel (h->backoff_task);
- h->backoff_task = GNUNET_SCHEDULER_NO_TASK;
+ h->backoff_task = NULL;
}
if (sync_first)
{
{
if (ACTION_GET == h->current->type)
{
- GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
- h->th = NULL;
+ if (NULL != h->th)
+ {
+ GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
+ h->th = NULL;
+ }
free_action_item (h->current);
h->current = NULL;
}
* confirm that the service has received all of our 'SET'
* messages (during statistics disconnect/shutdown).
*
- * @param cls the 'struct GNUNET_STATISTICS_Handle'
- * @param size how many bytes can we write to buf
+ * @param cls the `struct GNUNET_STATISTICS_Handle`
+ * @param size how many bytes can we write to @a buf
* @param buf where to write requests to the service
- * @return number of bytes written to buf
+ * @return number of bytes written to @a buf
*/
static size_t
transmit_test_on_shutdown (void *cls,
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
_("Failed to receive acknowledgement from statistics service, some statistics might have been lost!\n"));
h->do_destroy = GNUNET_NO;
- GNUNET_SCHEDULER_add_continuation (&destroy_task, h,
- GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+ GNUNET_SCHEDULER_add_now (&destroy_task, h);
return 0;
}
hdr.type = htons (GNUNET_MESSAGE_TYPE_TEST);
struct GNUNET_TIME_Relative timeout;
if ( (NULL != h->th) ||
- (GNUNET_SCHEDULER_NO_TASK != h->backoff_task) )
+ (NULL != h->backoff_task) )
return; /* action already pending */
if (GNUNET_YES != try_connect (h))
{
}
+/**
+ * We have run into a timeout on a #GNUNET_STATISTICS_get() operation,
+ * call the continuation.
+ *
+ * @param cls the `struct GNUNET_STATISTICS_GetHandle`
+ */
+static void
+run_get_timeout (void *cls)
+{
+ struct GNUNET_STATISTICS_GetHandle *gh = cls;
+ GNUNET_STATISTICS_Callback cont = gh->cont;
+ void *cont_cls = gh->cls;
+
+ gh->timeout_task = NULL;
+ GNUNET_STATISTICS_get_cancel (gh);
+ cont (cont_cls, GNUNET_SYSERR);
+}
+
+
/**
* Get statistic from the peer.
*
* @param cont continuation to call when done (can be NULL)
* This callback CANNOT destroy the statistics handle in the same call.
* @param proc function to call on each value
- * @param cls closure for cont and proc
+ * @param cls closure for @a cont and @a proc
* @return NULL on error
*/
struct GNUNET_STATISTICS_GetHandle *
ai->timeout = GNUNET_TIME_relative_to_absolute (timeout);
ai->type = ACTION_GET;
ai->msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
- GNUNET_CONTAINER_DLL_insert_tail (handle->action_head, handle->action_tail,
+ ai->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
+ &run_get_timeout,
+ ai);
+ GNUNET_CONTAINER_DLL_insert_tail (handle->action_head,
+ handle->action_tail,
ai);
schedule_action (handle);
return ai;
{
if (NULL == gh)
return;
+ if (NULL != gh->timeout_task)
+ {
+ GNUNET_SCHEDULER_cancel (gh->timeout_task);
+ gh->timeout_task = NULL;
+ }
gh->cont = NULL;
if (gh->sh->current == gh)
{
* @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
+ * @param proc_cls closure for @a proc
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
*/
int
GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
* @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)
+ * @param proc_cls closure for @a 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)
+ const char *subsystem,
+ const char *name,
+ GNUNET_STATISTICS_Iterator proc,
+ void *proc_cls)
{
struct GNUNET_STATISTICS_WatchEntry *w;
unsigned int i;
* @param name name of the value
* @param make_persistent should the value be kept across restarts?
* @param value new value or change
- * @param type type of the action (ACTION_SET or ACTION_UPDATE)
+ * @param type type of the action (#ACTION_SET or #ACTION_UPDATE)
*/
static void
-add_setter_action (struct GNUNET_STATISTICS_Handle *h, const char *name,
- int make_persistent, uint64_t value, enum ActionType type)
+add_setter_action (struct GNUNET_STATISTICS_Handle *h,
+ const char *name,
+ int make_persistent,
+ uint64_t value,
+ enum ActionType type)
{
struct GNUNET_STATISTICS_GetHandle *ai;
size_t slen;
size_t nsize;
int64_t delta;
- GNUNET_assert (NULL != h);
- GNUNET_assert (NULL != name);
slen = strlen (h->subsystem) + 1;
nlen = strlen (name) + 1;
nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
*/
void
GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
- const char *name, uint64_t value, int make_persistent)
+ const char *name,
+ uint64_t value,
+ int make_persistent)
{
if (NULL == handle)
return;
*/
void
GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle,
- const char *name, int64_t delta, int make_persistent)
+ const char *name,
+ int64_t delta,
+ int make_persistent)
{
if (NULL == handle)
return;
if (0 == delta)
return;
GNUNET_assert (GNUNET_NO == handle->do_destroy);
- add_setter_action (handle, name, make_persistent, (uint64_t) delta,
+ add_setter_action (handle,
+ name,
+ make_persistent,
+ (uint64_t) delta,
ACTION_UPDATE);
}