2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file statistics/statistics_api.c
23 * @brief API of the statistics service
24 * @author Christian Grothoff
27 #include "gnunet_client_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_server_lib.h"
32 #include "gnunet_statistics_service.h"
33 #include "gnunet_strings_lib.h"
34 #include "statistics.h"
37 * How long do we wait until a statistics request for setting
38 * a value times out? (The update will be lost if the
39 * service does not react within this timeframe).
41 #define SET_TRANSMIT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
43 #define LOG(kind,...) GNUNET_log_from (kind, "statistics-api",__VA_ARGS__)
73 * Entry kept for each value we are watching.
75 struct GNUNET_STATISTICS_WatchEntry
79 * What subsystem is this action about? (never NULL)
84 * What value is this action about? (never NULL)
91 GNUNET_STATISTICS_Iterator proc;
102 * Linked list of things we still need to do.
104 struct GNUNET_STATISTICS_GetHandle
108 * This is a doubly linked list.
110 struct GNUNET_STATISTICS_GetHandle *next;
113 * This is a doubly linked list.
115 struct GNUNET_STATISTICS_GetHandle *prev;
118 * Main statistics handle.
120 struct GNUNET_STATISTICS_Handle *sh;
123 * What subsystem is this action about? (can be NULL)
128 * What value is this action about? (can be NULL)
133 * Continuation to call once action is complete.
135 GNUNET_STATISTICS_Callback cont;
138 * Function to call (for GET actions only).
140 GNUNET_STATISTICS_Iterator proc;
143 * Closure for proc and cont.
148 * Timeout for this action.
150 struct GNUNET_TIME_Absolute timeout;
158 * Flag for SET/UPDATE actions.
163 * Has the current iteration been aborted; for GET actions.
168 * Is this a GET, SET, UPDATE or WATCH?
170 enum ActionType type;
173 * Size of the message that we will be transmitting.
181 * Handle for the service.
183 struct GNUNET_STATISTICS_Handle
186 * Name of our subsystem.
191 * Configuration to use.
193 const struct GNUNET_CONFIGURATION_Handle *cfg;
196 * Socket (if available).
198 struct GNUNET_CLIENT_Connection *client;
201 * Currently pending transmission request.
203 struct GNUNET_CLIENT_TransmitHandle *th;
206 * Head of the linked list of pending actions (first action
209 struct GNUNET_STATISTICS_GetHandle *action_head;
212 * Tail of the linked list of actions (for fast append).
214 struct GNUNET_STATISTICS_GetHandle *action_tail;
217 * Action we are currently busy with (action request has been
218 * transmitted, we're now receiving the response from the
221 struct GNUNET_STATISTICS_GetHandle *current;
224 * Array of watch entries.
226 struct GNUNET_STATISTICS_WatchEntry **watches;
229 * Task doing exponential back-off trying to reconnect.
231 GNUNET_SCHEDULER_TaskIdentifier backoff_task;
234 * Time for next connect retry.
236 struct GNUNET_TIME_Relative backoff;
239 * Size of the 'watches' array.
241 unsigned int watches_size;
244 * Should this handle auto-destruct once all actions have
250 * Are we currently receiving from the service?
258 * Schedule the next action to be performed.
260 * @param h statistics handle to reconnect
263 schedule_action (struct GNUNET_STATISTICS_Handle *h);
267 * Transmit request to service that we want to watch
268 * the development of a particular value.
270 * @param h statistics handle
271 * @param watch watch entry of the value to watch
274 schedule_watch_request (struct GNUNET_STATISTICS_Handle *h,
275 struct GNUNET_STATISTICS_WatchEntry *watch)
278 struct GNUNET_STATISTICS_GetHandle *ai;
283 GNUNET_assert (h != NULL);
284 GNUNET_assert (watch != NULL);
286 slen = strlen (watch->subsystem) + 1;
287 nlen = strlen (watch->name) + 1;
288 nsize = sizeof (struct GNUNET_MessageHeader) + slen + nlen;
289 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
294 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
296 ai->subsystem = GNUNET_strdup (watch->subsystem);
297 ai->name = GNUNET_strdup (watch->name);
298 ai->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
300 ai->type = ACTION_WATCH;
301 ai->proc = watch->proc;
302 ai->cls = watch->proc_cls;
303 GNUNET_CONTAINER_DLL_insert_tail (h->action_head, h->action_tail,
310 * Free memory associated with the given action item.
312 * @param gh action item to free
315 free_action_item (struct GNUNET_STATISTICS_GetHandle *gh)
317 GNUNET_free_non_null (gh->subsystem);
318 GNUNET_free_non_null (gh->name);
324 * Disconnect from the statistics service.
326 * @param h statistics handle to disconnect from
329 do_disconnect (struct GNUNET_STATISTICS_Handle *h)
331 struct GNUNET_STATISTICS_GetHandle *c;
335 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
338 if (NULL != h->client)
340 GNUNET_CLIENT_disconnect (h->client);
343 h->receiving = GNUNET_NO;
344 if (NULL != (c = h->current))
348 c->cont (c->cls, GNUNET_SYSERR);
349 free_action_item (c);
355 * Try to (re)connect to the statistics service.
357 * @param h statistics handle to reconnect
358 * @return GNUNET_YES on success, GNUNET_NO on failure.
361 try_connect (struct GNUNET_STATISTICS_Handle *h)
363 struct GNUNET_STATISTICS_GetHandle *gh;
364 struct GNUNET_STATISTICS_GetHandle *gn;
367 if (h->backoff_task != GNUNET_SCHEDULER_NO_TASK)
369 if (h->client != NULL)
371 h->client = GNUNET_CLIENT_connect ("statistics", h->cfg);
372 if (h->client != NULL)
375 while (NULL != (gh = gn))
378 if (gh->type == ACTION_WATCH)
380 GNUNET_CONTAINER_DLL_remove (h->action_head,
383 free_action_item (gh);
386 for (i = 0; i < h->watches_size; i++)
388 if (NULL != h->watches[i])
389 schedule_watch_request (h, h->watches[i]);
393 LOG (GNUNET_ERROR_TYPE_DEBUG,
394 "Failed to connect to statistics service!\n");
400 * We've waited long enough, reconnect now.
402 * @param cls the 'struct GNUNET_STATISTICS_Handle' to reconnect
403 * @param tc scheduler context (unused)
406 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
408 struct GNUNET_STATISTICS_Handle *h = cls;
410 h->backoff_task = GNUNET_SCHEDULER_NO_TASK;
417 * Task used by 'reconnect_later' to shutdown the handle
419 * @param cls the statistics handle
420 * @param tc scheduler context
423 do_destroy (void *cls,
424 const struct GNUNET_SCHEDULER_TaskContext *tc)
426 struct GNUNET_STATISTICS_Handle *h = cls;
428 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
432 * Reconnect at a later time, respecting back-off.
434 * @param h statistics handle
437 reconnect_later (struct GNUNET_STATISTICS_Handle *h)
439 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->backoff_task);
442 /* So we are shutting down and the service is not reachable.
443 * Chances are that it's down for good and we are not going to connect to
445 * Give up and don't sync the rest of the data.
448 h->do_destroy = GNUNET_NO;
449 GNUNET_SCHEDULER_add_continuation (&do_destroy, h,
450 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
454 GNUNET_SCHEDULER_add_delayed (h->backoff, &reconnect_task, h);
455 h->backoff = GNUNET_TIME_relative_multiply (h->backoff, 2);
457 GNUNET_TIME_relative_min (h->backoff, GNUNET_CONSTANTS_SERVICE_TIMEOUT);
462 * Process a 'GNUNET_MESSAGE_TYPE_STATISTICS_VALUE' message.
464 * @param h statistics handle
465 * @param msg message received from the service, never NULL
466 * @return GNUNET_OK if the message was well-formed
469 process_statistics_value_message (struct GNUNET_STATISTICS_Handle *h,
470 const struct GNUNET_MessageHeader *msg)
474 const struct GNUNET_STATISTICS_ReplyMessage *smsg;
477 if (h->current->aborted)
479 LOG (GNUNET_ERROR_TYPE_DEBUG, "Iteration was aborted, ignoring VALUE\n");
480 return GNUNET_OK; /* don't bother */
482 size = ntohs (msg->size);
483 if (size < sizeof (struct GNUNET_STATISTICS_ReplyMessage))
486 return GNUNET_SYSERR;
488 smsg = (const struct GNUNET_STATISTICS_ReplyMessage *) msg;
489 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
491 GNUNET_STRINGS_buffer_tokenize ((const char *) &smsg[1], size, 2,
495 return GNUNET_SYSERR;
497 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received valid statistic on `%s:%s': %llu\n",
498 service, name, GNUNET_ntohll (smsg->value));
500 h->current->proc (h->current->cls, service, name,
501 GNUNET_ntohll (smsg->value),
503 (ntohl (smsg->uid) & GNUNET_STATISTICS_PERSIST_BIT)))
505 LOG (GNUNET_ERROR_TYPE_DEBUG,
506 "Processing of remaining statistics aborted by client.\n");
507 h->current->aborted = GNUNET_YES;
509 LOG (GNUNET_ERROR_TYPE_DEBUG, "VALUE processed successfully\n");
515 * We have received a watch value from the service. Process it.
517 * @param h statistics handle
518 * @param msg the watch value message
519 * @return GNUNET_OK if the message was well-formed, GNUNET_SYSERR if not,
520 * GNUNET_NO if this watch has been cancelled
523 process_watch_value (struct GNUNET_STATISTICS_Handle *h,
524 const struct GNUNET_MessageHeader *msg)
526 const struct GNUNET_STATISTICS_WatchValueMessage *wvm;
527 struct GNUNET_STATISTICS_WatchEntry *w;
530 if (sizeof (struct GNUNET_STATISTICS_WatchValueMessage) != ntohs (msg->size))
533 return GNUNET_SYSERR;
535 wvm = (const struct GNUNET_STATISTICS_WatchValueMessage *) msg;
536 GNUNET_break (0 == ntohl (wvm->reserved));
537 wid = ntohl (wvm->wid);
538 if (wid >= h->watches_size)
541 return GNUNET_SYSERR;
546 (void) w->proc (w->proc_cls, w->subsystem, w->name,
547 GNUNET_ntohll (wvm->value),
548 0 != (ntohl (wvm->flags) & GNUNET_STATISTICS_PERSIST_BIT));
554 * Function called with messages from stats service.
557 * @param msg message received, NULL on timeout or fatal error
560 receive_stats (void *cls, const struct GNUNET_MessageHeader *msg)
562 struct GNUNET_STATISTICS_Handle *h = cls;
563 struct GNUNET_STATISTICS_GetHandle *c;
568 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
569 "Error receiving statistics from service, is the service running?\n");
574 switch (ntohs (msg->type))
576 case GNUNET_MESSAGE_TYPE_STATISTICS_END:
577 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received end of statistics marker\n");
578 if (NULL == (c = h->current))
585 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
586 if (h->watches_size > 0)
588 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
589 GNUNET_TIME_UNIT_FOREVER_REL);
593 h->receiving = GNUNET_NO;
598 c->cont (c->cls, GNUNET_OK);
599 free_action_item (c);
601 case GNUNET_MESSAGE_TYPE_STATISTICS_VALUE:
602 if (GNUNET_OK != process_statistics_value_message (h, msg))
608 /* finally, look for more! */
609 LOG (GNUNET_ERROR_TYPE_DEBUG,
610 "Processing VALUE done, now reading more\n");
611 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
612 GNUNET_TIME_absolute_get_remaining (h->
614 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
616 case GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE:
618 (ret = process_watch_value (h, msg)))
621 if (GNUNET_NO == ret)
622 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
626 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
627 GNUNET_assert (h->watches_size > 0);
628 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
629 GNUNET_TIME_UNIT_FOREVER_REL);
641 * Transmit a GET request (and if successful, start to receive
644 * @param handle statistics handle
645 * @param size how many bytes can we write to buf
646 * @param buf where to write requests to the service
647 * @return number of bytes written to buf
650 transmit_get (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
652 struct GNUNET_STATISTICS_GetHandle *c;
653 struct GNUNET_MessageHeader *hdr;
658 GNUNET_assert (NULL != (c = handle->current));
661 /* timeout / error */
662 LOG (GNUNET_ERROR_TYPE_DEBUG,
663 "Transmission of request for statistics failed!\n");
664 do_disconnect (handle);
665 reconnect_later (handle);
668 slen1 = strlen (c->subsystem) + 1;
669 slen2 = strlen (c->name) + 1;
670 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
671 GNUNET_assert (msize <= size);
672 hdr = (struct GNUNET_MessageHeader *) buf;
673 hdr->size = htons (msize);
674 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_GET);
675 GNUNET_assert (slen1 + slen2 ==
676 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
679 if (GNUNET_YES != handle->receiving)
681 LOG (GNUNET_ERROR_TYPE_DEBUG,
682 "Transmission of GET done, now reading response\n");
683 handle->receiving = GNUNET_YES;
684 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
685 GNUNET_TIME_absolute_get_remaining (c->timeout));
692 * Transmit a WATCH request (and if successful, start to receive
695 * @param handle statistics handle
696 * @param size how many bytes can we write to buf
697 * @param buf where to write requests to the service
698 * @return number of bytes written to buf
701 transmit_watch (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
703 struct GNUNET_MessageHeader *hdr;
710 /* timeout / error */
711 LOG (GNUNET_ERROR_TYPE_DEBUG,
712 "Transmission of request for statistics failed!\n");
713 do_disconnect (handle);
714 reconnect_later (handle);
717 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting watch request for `%s'\n",
718 handle->current->name);
719 slen1 = strlen (handle->current->subsystem) + 1;
720 slen2 = strlen (handle->current->name) + 1;
721 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
722 GNUNET_assert (msize <= size);
723 hdr = (struct GNUNET_MessageHeader *) buf;
724 hdr->size = htons (msize);
725 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH);
726 GNUNET_assert (slen1 + slen2 ==
727 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
728 handle->current->subsystem,
729 handle->current->name));
730 if (GNUNET_YES != handle->receiving)
732 handle->receiving = GNUNET_YES;
733 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
734 GNUNET_TIME_UNIT_FOREVER_REL);
736 GNUNET_assert (NULL == handle->current->cont);
737 free_action_item (handle->current);
738 handle->current = NULL;
744 * Transmit a SET/UPDATE request.
746 * @param handle statistics handle
747 * @param size how many bytes can we write to buf
748 * @param buf where to write requests to the service
749 * @return number of bytes written to buf
752 transmit_set (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
754 struct GNUNET_STATISTICS_SetMessage *r;
761 do_disconnect (handle);
762 reconnect_later (handle);
765 slen = strlen (handle->current->subsystem) + 1;
766 nlen = strlen (handle->current->name) + 1;
767 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
771 do_disconnect (handle);
772 reconnect_later (handle);
776 r->header.size = htons (nsize);
777 r->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
779 r->value = GNUNET_htonll (handle->current->value);
780 if (handle->current->make_persistent)
781 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_PERSISTENT);
782 if (handle->current->type == ACTION_UPDATE)
783 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_RELATIVE);
784 GNUNET_assert (slen + nlen ==
785 GNUNET_STRINGS_buffer_fill ((char *) &r[1], slen + nlen, 2,
786 handle->current->subsystem,
787 handle->current->name));
788 GNUNET_assert (NULL == handle->current->cont);
789 free_action_item (handle->current);
790 handle->current = NULL;
796 * Function called when we are ready to transmit a request to the service.
798 * @param cls the 'struct GNUNET_STATISTICS_Handle'
799 * @param size how many bytes can we write to buf
800 * @param buf where to write requests to the service
801 * @return number of bytes written to buf
804 transmit_action (void *cls, size_t size, void *buf)
806 struct GNUNET_STATISTICS_Handle *h = cls;
811 if (NULL != h->current)
812 switch (h->current->type)
815 ret = transmit_get (h, size, buf);
819 ret = transmit_set (h, size, buf);
822 ret = transmit_watch (h, size, buf);
834 * Get handle for the statistics service.
836 * @param subsystem name of subsystem using the service
837 * @param cfg services configuration in use
838 * @return handle to use
840 struct GNUNET_STATISTICS_Handle *
841 GNUNET_STATISTICS_create (const char *subsystem,
842 const struct GNUNET_CONFIGURATION_Handle *cfg)
844 struct GNUNET_STATISTICS_Handle *ret;
846 GNUNET_assert (subsystem != NULL);
847 GNUNET_assert (cfg != NULL);
848 ret = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_Handle));
850 ret->subsystem = GNUNET_strdup (subsystem);
851 ret->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
857 * Destroy a handle (free all state associated with
860 * @param h statistics handle to destroy
861 * @param sync_first set to GNUNET_YES if pending SET requests should
865 GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h, int sync_first)
867 struct GNUNET_STATISTICS_GetHandle *pos;
868 struct GNUNET_STATISTICS_GetHandle *next;
869 struct GNUNET_TIME_Relative timeout;
874 GNUNET_assert (GNUNET_NO == h->do_destroy); // Don't call twice.
875 if (GNUNET_SCHEDULER_NO_TASK != h->backoff_task)
877 GNUNET_SCHEDULER_cancel (h->backoff_task);
878 h->backoff_task = GNUNET_SCHEDULER_NO_TASK;
882 if (h->current != NULL)
884 if (h->current->type == ACTION_GET)
886 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
888 free_action_item (h->current);
892 next = h->action_head;
893 while (NULL != (pos = next))
896 if (pos->type == ACTION_GET)
898 GNUNET_CONTAINER_DLL_remove (h->action_head,
901 free_action_item (pos);
904 if ( (NULL == h->current) &&
905 (NULL != (h->current = h->action_head)) )
906 GNUNET_CONTAINER_DLL_remove (h->action_head,
909 h->do_destroy = GNUNET_YES;
910 if ((h->current != NULL) && (h->th == NULL))
912 if (NULL == h->client)
914 /* instant-connect (regardless of back-off) to submit final value */
915 h->client = GNUNET_CLIENT_connect ("statistics", h->cfg);
917 if (NULL != h->client)
919 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
921 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
923 &transmit_action, h);
924 GNUNET_assert (NULL != h->th);
928 return; /* do not finish destruction just yet */
930 while (NULL != (pos = h->action_head))
932 GNUNET_CONTAINER_DLL_remove (h->action_head,
935 free_action_item (pos);
938 for (i = 0; i < h->watches_size; i++)
940 if (NULL == h->watches[i])
942 GNUNET_free (h->watches[i]->subsystem);
943 GNUNET_free (h->watches[i]->name);
944 GNUNET_free (h->watches[i]);
946 GNUNET_array_grow (h->watches, h->watches_size, 0);
947 GNUNET_free (h->subsystem);
953 destroy_task (void *cls,
954 const struct GNUNET_SCHEDULER_TaskContext *tc)
956 struct GNUNET_STATISTICS_Handle *h = cls;
958 GNUNET_STATISTICS_destroy (h, GNUNET_YES);
963 * Schedule the next action to be performed.
965 * @param h statistics handle
968 schedule_action (struct GNUNET_STATISTICS_Handle *h)
970 struct GNUNET_TIME_Relative timeout;
972 if ( (h->th != NULL) ||
973 (h->backoff_task != GNUNET_SCHEDULER_NO_TASK) )
974 return; /* action already pending */
975 if (GNUNET_YES != try_connect (h))
980 if (NULL != h->current)
981 return; /* action already pending */
982 /* schedule next action */
983 h->current = h->action_head;
984 if (NULL == h->current)
988 h->do_destroy = GNUNET_NO;
989 GNUNET_SCHEDULER_add_continuation (&destroy_task, h,
990 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
994 GNUNET_CONTAINER_DLL_remove (h->action_head, h->action_tail, h->current);
995 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
998 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
1000 &transmit_action, h)))
1002 LOG (GNUNET_ERROR_TYPE_DEBUG,
1003 "Failed to transmit request to statistics service.\n");
1005 reconnect_later (h);
1011 * Get statistic from the peer.
1013 * @param handle identification of the statistics service
1014 * @param subsystem limit to the specified subsystem, NULL for our subsystem
1015 * @param name name of the statistic value, NULL for all values
1016 * @param timeout after how long should we give up (and call
1017 * cont with an error code)?
1018 * @param cont continuation to call when done (can be NULL)
1019 * @param proc function to call on each value
1020 * @param cls closure for cont and proc
1021 * @return NULL on error
1023 struct GNUNET_STATISTICS_GetHandle *
1024 GNUNET_STATISTICS_get (struct GNUNET_STATISTICS_Handle *handle,
1025 const char *subsystem, const char *name,
1026 struct GNUNET_TIME_Relative timeout,
1027 GNUNET_STATISTICS_Callback cont,
1028 GNUNET_STATISTICS_Iterator proc, void *cls)
1032 struct GNUNET_STATISTICS_GetHandle *ai;
1036 GNUNET_assert (proc != NULL);
1037 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1038 if (subsystem == NULL)
1042 slen1 = strlen (subsystem) + 1;
1043 slen2 = strlen (name) + 1;
1044 GNUNET_assert (slen1 + slen2 + sizeof (struct GNUNET_MessageHeader) <
1045 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1046 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
1048 ai->subsystem = GNUNET_strdup (subsystem);
1049 ai->name = GNUNET_strdup (name);
1053 ai->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1054 ai->type = ACTION_GET;
1055 ai->msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
1056 GNUNET_CONTAINER_DLL_insert_tail (handle->action_head, handle->action_tail,
1058 schedule_action (handle);
1064 * Cancel a 'get' request. Must be called before the 'cont'
1065 * function is called.
1067 * @param gh handle of the request to cancel
1070 GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh)
1074 if (gh->sh->current == gh)
1076 gh->aborted = GNUNET_YES;
1080 GNUNET_CONTAINER_DLL_remove (gh->sh->action_head, gh->sh->action_tail, gh);
1081 GNUNET_free (gh->name);
1082 GNUNET_free (gh->subsystem);
1089 * Watch statistics from the peer (be notified whenever they change).
1091 * @param handle identification of the statistics service
1092 * @param subsystem limit to the specified subsystem, never NULL
1093 * @param name name of the statistic value, never NULL
1094 * @param proc function to call on each value
1095 * @param proc_cls closure for proc
1096 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1099 GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
1100 const char *subsystem, const char *name,
1101 GNUNET_STATISTICS_Iterator proc, void *proc_cls)
1103 struct GNUNET_STATISTICS_WatchEntry *w;
1106 return GNUNET_SYSERR;
1107 w = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_WatchEntry));
1108 w->subsystem = GNUNET_strdup (subsystem);
1109 w->name = GNUNET_strdup (name);
1111 w->proc_cls = proc_cls;
1112 GNUNET_array_append (handle->watches, handle->watches_size, w);
1113 schedule_watch_request (handle, w);
1119 * Stop watching statistics from the peer.
1121 * @param handle identification of the statistics service
1122 * @param subsystem limit to the specified subsystem, never NULL
1123 * @param name name of the statistic value, never NULL
1124 * @param proc function to call on each value
1125 * @param proc_cls closure for proc
1126 * @return GNUNET_OK on success, GNUNET_SYSERR on error (no such watch)
1129 GNUNET_STATISTICS_watch_cancel (struct GNUNET_STATISTICS_Handle *handle,
1130 const char *subsystem, const char *name,
1131 GNUNET_STATISTICS_Iterator proc, void *proc_cls)
1133 struct GNUNET_STATISTICS_WatchEntry *w;
1137 return GNUNET_SYSERR;
1138 for (i=0;i<handle->watches_size;i++)
1140 w = handle->watches[i];
1141 if ( (w->proc == proc) &&
1142 (w->proc_cls == proc_cls) &&
1143 (0 == strcmp (w->name, name)) &&
1144 (0 == strcmp (w->subsystem, subsystem)) )
1146 GNUNET_free (w->name);
1147 GNUNET_free (w->subsystem);
1149 handle->watches[i] = NULL;
1153 return GNUNET_SYSERR;
1159 * Queue a request to change a statistic.
1161 * @param h statistics handle
1162 * @param name name of the value
1163 * @param make_persistent should the value be kept across restarts?
1164 * @param value new value or change
1165 * @param type type of the action (ACTION_SET or ACTION_UPDATE)
1168 add_setter_action (struct GNUNET_STATISTICS_Handle *h, const char *name,
1169 int make_persistent, uint64_t value, enum ActionType type)
1171 struct GNUNET_STATISTICS_GetHandle *ai;
1177 GNUNET_assert (h != NULL);
1178 GNUNET_assert (name != NULL);
1179 slen = strlen (h->subsystem) + 1;
1180 nlen = strlen (name) + 1;
1181 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
1182 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1187 for (ai = h->action_head; ai != NULL; ai = ai->next)
1189 if (! ( (0 == strcmp (ai->subsystem, h->subsystem)) &&
1190 (0 == strcmp (ai->name, name)) &&
1191 ( (ai->type == ACTION_UPDATE) ||
1192 (ai->type == ACTION_SET) ) ) )
1194 if (ai->type == ACTION_SET)
1196 if (type == ACTION_UPDATE)
1198 delta = (int64_t) value;
1201 /* update old set by new delta */
1206 /* update old set by new delta, but never go negative */
1207 if (ai->value < -delta)
1215 /* new set overrides old set */
1221 if (type == ACTION_UPDATE)
1223 /* make delta cummulative */
1224 delta = (int64_t) value;
1229 /* drop old 'update', use new 'set' instead */
1234 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1235 ai->make_persistent = make_persistent;
1238 /* no existing entry matches, create a fresh one */
1239 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
1241 ai->subsystem = GNUNET_strdup (h->subsystem);
1242 ai->name = GNUNET_strdup (name);
1243 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1244 ai->make_persistent = make_persistent;
1248 GNUNET_CONTAINER_DLL_insert_tail (h->action_head, h->action_tail,
1250 schedule_action (h);
1255 * Set statistic value for the peer. Will always use our
1256 * subsystem (the argument used when "handle" was created).
1258 * @param handle identification of the statistics service
1259 * @param name name of the statistic value
1260 * @param value new value to set
1261 * @param make_persistent should the value be kept across restarts?
1264 GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
1265 const char *name, uint64_t value, int make_persistent)
1269 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1270 add_setter_action (handle, name, make_persistent, value, ACTION_SET);
1275 * Set statistic value for the peer. Will always use our
1276 * subsystem (the argument used when "handle" was created).
1278 * @param handle identification of the statistics service
1279 * @param name name of the statistic value
1280 * @param delta change in value (added to existing value)
1281 * @param make_persistent should the value be kept across restarts?
1284 GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle,
1285 const char *name, int64_t delta, int make_persistent)
1291 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1292 add_setter_action (handle, name, make_persistent, (uint64_t) delta,
1297 /* end of statistics_api.c */