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 slen = strlen (watch->subsystem) + 1;
285 nlen = strlen (watch->name) + 1;
286 nsize = sizeof (struct GNUNET_MessageHeader) + slen + nlen;
287 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
292 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
294 ai->subsystem = GNUNET_strdup (watch->subsystem);
295 ai->name = GNUNET_strdup (watch->name);
296 ai->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
298 ai->type = ACTION_WATCH;
299 ai->proc = watch->proc;
300 ai->cls = watch->proc_cls;
301 GNUNET_CONTAINER_DLL_insert_tail (h->action_head, h->action_tail,
308 * Free memory associated with the given action item.
310 * @param gh action item to free
313 free_action_item (struct GNUNET_STATISTICS_GetHandle *gh)
315 GNUNET_free_non_null (gh->subsystem);
316 GNUNET_free_non_null (gh->name);
322 * Disconnect from the statistics service.
324 * @param h statistics handle to disconnect from
327 do_disconnect (struct GNUNET_STATISTICS_Handle *h)
329 struct GNUNET_STATISTICS_GetHandle *c;
333 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
336 if (NULL != h->client)
338 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
341 h->receiving = GNUNET_NO;
342 if (NULL != (c = h->current))
346 c->cont (c->cls, GNUNET_SYSERR);
347 free_action_item (c);
353 * Try to (re)connect to the statistics service.
355 * @param h statistics handle to reconnect
356 * @return GNUNET_YES on success, GNUNET_NO on failure.
359 try_connect (struct GNUNET_STATISTICS_Handle *h)
361 struct GNUNET_STATISTICS_GetHandle *gh;
362 struct GNUNET_STATISTICS_GetHandle *gn;
365 if (h->backoff_task != GNUNET_SCHEDULER_NO_TASK)
367 if (h->client != NULL)
369 h->client = GNUNET_CLIENT_connect ("statistics", h->cfg);
370 if (h->client != NULL)
373 while (NULL != (gh = gn))
376 if (gh->type == ACTION_WATCH)
378 GNUNET_CONTAINER_DLL_remove (h->action_head,
381 free_action_item (gh);
384 for (i = 0; i < h->watches_size; i++)
385 schedule_watch_request (h, h->watches[i]);
389 LOG (GNUNET_ERROR_TYPE_DEBUG,
390 _("Failed to connect to statistics service!\n"));
397 * We've waited long enough, reconnect now.
399 * @param cls the 'struct GNUNET_STATISTICS_Handle' to reconnect
400 * @param tc scheduler context (unused)
403 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
405 struct GNUNET_STATISTICS_Handle *h = cls;
407 h->backoff_task = GNUNET_SCHEDULER_NO_TASK;
413 * Reconnect at a later time, respecting back-off.
415 * @param h statistics handle
418 reconnect_later (struct GNUNET_STATISTICS_Handle *h)
420 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->backoff_task);
422 GNUNET_SCHEDULER_add_delayed (h->backoff, &reconnect_task, h);
423 h->backoff = GNUNET_TIME_relative_multiply (h->backoff, 2);
425 GNUNET_TIME_relative_min (h->backoff, GNUNET_CONSTANTS_SERVICE_TIMEOUT);
430 * Process a 'GNUNET_MESSAGE_TYPE_STATISTICS_VALUE' message.
432 * @param h statistics handle
433 * @param msg message received from the service, never NULL
434 * @return GNUNET_OK if the message was well-formed
437 process_statistics_value_message (struct GNUNET_STATISTICS_Handle *h,
438 const struct GNUNET_MessageHeader *msg)
442 const struct GNUNET_STATISTICS_ReplyMessage *smsg;
445 if (h->current->aborted)
448 LOG (GNUNET_ERROR_TYPE_DEBUG, "Iteration was aborted, ignoring VALUE\n");
450 return GNUNET_OK; /* don't bother */
452 size = ntohs (msg->size);
453 if (size < sizeof (struct GNUNET_STATISTICS_ReplyMessage))
456 return GNUNET_SYSERR;
458 smsg = (const struct GNUNET_STATISTICS_ReplyMessage *) msg;
459 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
461 GNUNET_STRINGS_buffer_tokenize ((const char *) &smsg[1], size, 2,
465 return GNUNET_SYSERR;
468 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received valid statistic on `%s:%s': %llu\n",
469 service, name, GNUNET_ntohll (smsg->value));
472 h->current->proc (h->current->cls, service, name,
473 GNUNET_ntohll (smsg->value),
475 (ntohl (smsg->uid) & GNUNET_STATISTICS_PERSIST_BIT)))
478 LOG (GNUNET_ERROR_TYPE_DEBUG,
479 "Processing of remaining statistics aborted by client.\n");
481 h->current->aborted = GNUNET_YES;
484 LOG (GNUNET_ERROR_TYPE_DEBUG, "VALUE processed successfully\n");
491 * We have received a watch value from the service. Process it.
493 * @param h statistics handle
494 * @param msg the watch value message
495 * @return GNUNET_OK if the message was well-formed, GNUNET_SYSERR if not
498 process_watch_value (struct GNUNET_STATISTICS_Handle *h,
499 const struct GNUNET_MessageHeader *msg)
501 const struct GNUNET_STATISTICS_WatchValueMessage *wvm;
502 struct GNUNET_STATISTICS_WatchEntry *w;
505 if (sizeof (struct GNUNET_STATISTICS_WatchValueMessage) != ntohs (msg->size))
508 return GNUNET_SYSERR;
510 wvm = (const struct GNUNET_STATISTICS_WatchValueMessage *) msg;
511 GNUNET_break (0 == ntohl (wvm->reserved));
512 wid = ntohl (wvm->wid);
513 if (wid >= h->watches_size)
516 return GNUNET_SYSERR;
519 (void) w->proc (w->proc_cls, w->subsystem, w->name,
520 GNUNET_ntohll (wvm->value),
521 0 != (ntohl (wvm->flags) & GNUNET_STATISTICS_PERSIST_BIT));
527 * Function called with messages from stats service.
530 * @param msg message received, NULL on timeout or fatal error
533 receive_stats (void *cls, const struct GNUNET_MessageHeader *msg)
535 struct GNUNET_STATISTICS_Handle *h = cls;
536 struct GNUNET_STATISTICS_GetHandle *c;
542 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
543 "Error receiving statistics from service, is the service running?\n");
549 switch (ntohs (msg->type))
551 case GNUNET_MESSAGE_TYPE_STATISTICS_END:
553 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received end of statistics marker\n");
555 if (NULL == (c = h->current))
562 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
563 if (h->watches_size > 0)
565 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
566 GNUNET_TIME_UNIT_FOREVER_REL);
570 h->receiving = GNUNET_NO;
575 c->cont (c->cls, GNUNET_OK);
576 free_action_item (c);
578 case GNUNET_MESSAGE_TYPE_STATISTICS_VALUE:
579 if (GNUNET_OK != process_statistics_value_message (h, msg))
585 /* finally, look for more! */
587 LOG (GNUNET_ERROR_TYPE_DEBUG,
588 "Processing VALUE done, now reading more\n");
590 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
591 GNUNET_TIME_absolute_get_remaining (h->
593 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
595 case GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE:
597 process_watch_value (h, msg))
603 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
604 GNUNET_assert (h->watches_size > 0);
605 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
606 GNUNET_TIME_UNIT_FOREVER_REL);
618 * Transmit a GET request (and if successful, start to receive
621 * @param handle statistics handle
622 * @param size how many bytes can we write to buf
623 * @param buf where to write requests to the service
624 * @return number of bytes written to buf
627 transmit_get (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
629 struct GNUNET_STATISTICS_GetHandle *c;
630 struct GNUNET_MessageHeader *hdr;
635 GNUNET_assert (NULL != (c = handle->current));
638 /* timeout / error */
640 LOG (GNUNET_ERROR_TYPE_DEBUG,
641 "Transmission of request for statistics failed!\n");
643 do_disconnect (handle);
644 reconnect_later (handle);
647 slen1 = strlen (c->subsystem) + 1;
648 slen2 = strlen (c->name) + 1;
649 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
650 GNUNET_assert (msize <= size);
651 hdr = (struct GNUNET_MessageHeader *) buf;
652 hdr->size = htons (msize);
653 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_GET);
654 GNUNET_assert (slen1 + slen2 ==
655 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
658 if (GNUNET_YES != handle->receiving)
661 LOG (GNUNET_ERROR_TYPE_DEBUG,
662 "Transmission of GET done, now reading response\n");
664 handle->receiving = GNUNET_YES;
665 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
666 GNUNET_TIME_absolute_get_remaining (c->timeout));
673 * Transmit a WATCH request (and if successful, start to receive
676 * @param handle statistics handle
677 * @param size how many bytes can we write to buf
678 * @param buf where to write requests to the service
679 * @return number of bytes written to buf
682 transmit_watch (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
684 struct GNUNET_MessageHeader *hdr;
691 /* timeout / error */
693 LOG (GNUNET_ERROR_TYPE_DEBUG,
694 "Transmission of request for statistics failed!\n");
696 do_disconnect (handle);
697 reconnect_later (handle);
701 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting watch request for `%s'\n",
702 handle->current->name);
704 slen1 = strlen (handle->current->subsystem) + 1;
705 slen2 = strlen (handle->current->name) + 1;
706 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
707 GNUNET_assert (msize <= size);
708 hdr = (struct GNUNET_MessageHeader *) buf;
709 hdr->size = htons (msize);
710 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH);
711 GNUNET_assert (slen1 + slen2 ==
712 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
713 handle->current->subsystem,
714 handle->current->name));
715 if (GNUNET_YES != handle->receiving)
717 handle->receiving = GNUNET_YES;
718 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
719 GNUNET_TIME_UNIT_FOREVER_REL);
721 GNUNET_assert (NULL == handle->current->cont);
722 free_action_item (handle->current);
723 handle->current = NULL;
729 * Transmit a SET/UPDATE request.
731 * @param handle statistics handle
732 * @param size how many bytes can we write to buf
733 * @param buf where to write requests to the service
734 * @return number of bytes written to buf
737 transmit_set (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
739 struct GNUNET_STATISTICS_SetMessage *r;
746 do_disconnect (handle);
747 reconnect_later (handle);
750 slen = strlen (handle->current->subsystem) + 1;
751 nlen = strlen (handle->current->name) + 1;
752 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
756 do_disconnect (handle);
757 reconnect_later (handle);
761 r->header.size = htons (nsize);
762 r->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
764 r->value = GNUNET_htonll (handle->current->value);
765 if (handle->current->make_persistent)
766 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_PERSISTENT);
767 if (handle->current->type == ACTION_UPDATE)
768 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_RELATIVE);
769 GNUNET_assert (slen + nlen ==
770 GNUNET_STRINGS_buffer_fill ((char *) &r[1], slen + nlen, 2,
771 handle->current->subsystem,
772 handle->current->name));
773 GNUNET_assert (NULL == handle->current->cont);
774 free_action_item (handle->current);
775 handle->current = NULL;
781 * Function called when we are ready to transmit a request to the service.
783 * @param cls the 'struct GNUNET_STATISTICS_Handle'
784 * @param size how many bytes can we write to buf
785 * @param buf where to write requests to the service
786 * @return number of bytes written to buf
789 transmit_action (void *cls, size_t size, void *buf)
791 struct GNUNET_STATISTICS_Handle *h = cls;
796 if (NULL != h->current)
797 switch (h->current->type)
800 ret = transmit_get (h, size, buf);
804 ret = transmit_set (h, size, buf);
807 ret = transmit_watch (h, size, buf);
819 * Get handle for the statistics service.
821 * @param subsystem name of subsystem using the service
822 * @param cfg services configuration in use
823 * @return handle to use
825 struct GNUNET_STATISTICS_Handle *
826 GNUNET_STATISTICS_create (const char *subsystem,
827 const struct GNUNET_CONFIGURATION_Handle *cfg)
829 struct GNUNET_STATISTICS_Handle *ret;
831 GNUNET_assert (subsystem != NULL);
832 GNUNET_assert (cfg != NULL);
833 ret = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_Handle));
835 ret->subsystem = GNUNET_strdup (subsystem);
836 ret->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
842 * Destroy a handle (free all state associated with
845 * @param h statistics handle to destroy
846 * @param sync_first set to GNUNET_YES if pending SET requests should
850 GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h, int sync_first)
852 struct GNUNET_STATISTICS_GetHandle *pos;
853 struct GNUNET_STATISTICS_GetHandle *next;
854 struct GNUNET_STATISTICS_GetHandle *prev;
855 struct GNUNET_TIME_Relative timeout;
860 if (GNUNET_SCHEDULER_NO_TASK != h->backoff_task)
861 GNUNET_SCHEDULER_cancel (h->backoff_task);
864 if (h->current != NULL)
866 if (h->current->type == ACTION_GET)
868 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
870 free_action_item (h->current);
874 pos = h->action_head;
879 if (pos->type == ACTION_GET)
882 h->action_head = next;
885 free_action_item (pos);
893 h->action_tail = prev;
894 if (h->current == NULL)
896 h->current = h->action_head;
897 if (h->action_head != NULL)
899 h->action_head = h->action_head->next;
900 if (h->action_head == NULL)
901 h->action_tail = NULL;
904 h->do_destroy = GNUNET_YES;
905 if ((h->current != NULL) && (h->th == NULL))
907 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
909 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
911 &transmit_action, h);
912 GNUNET_assert (NULL != h->th);
917 while (NULL != (pos = h->action_head))
919 GNUNET_CONTAINER_DLL_remove (h->action_head,
922 free_action_item (pos);
925 for (i = 0; i < h->watches_size; i++)
927 if (NULL == h->watches[i])
929 GNUNET_free (h->watches[i]->subsystem);
930 GNUNET_free (h->watches[i]->name);
931 GNUNET_free (h->watches[i]);
933 GNUNET_array_grow (h->watches, h->watches_size, 0);
934 GNUNET_free (h->subsystem);
940 * Schedule the next action to be performed.
942 * @param h statistics handle
945 schedule_action (struct GNUNET_STATISTICS_Handle *h)
947 struct GNUNET_TIME_Relative timeout;
949 if ( (h->th != NULL) ||
950 (h->backoff_task != GNUNET_SCHEDULER_NO_TASK) )
951 return; /* action already pending */
952 if (GNUNET_YES != try_connect (h))
957 if (NULL != h->current)
958 return; /* action already pending */
959 /* schedule next action */
960 h->current = h->action_head;
961 if (NULL == h->current)
965 h->do_destroy = GNUNET_NO;
966 GNUNET_STATISTICS_destroy (h, GNUNET_YES);
970 GNUNET_CONTAINER_DLL_remove (h->action_head, h->action_tail, h->current);
971 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
974 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
976 &transmit_action, h)))
979 LOG (GNUNET_ERROR_TYPE_DEBUG,
980 "Failed to transmit request to statistics service.\n");
989 * Get statistic from the peer.
991 * @param handle identification of the statistics service
992 * @param subsystem limit to the specified subsystem, NULL for our subsystem
993 * @param name name of the statistic value, NULL for all values
994 * @param timeout after how long should we give up (and call
995 * cont with an error code)?
996 * @param cont continuation to call when done (can be NULL)
997 * @param proc function to call on each value
998 * @param cls closure for cont and proc
999 * @return NULL on error
1001 struct GNUNET_STATISTICS_GetHandle *
1002 GNUNET_STATISTICS_get (struct GNUNET_STATISTICS_Handle *handle,
1003 const char *subsystem, const char *name,
1004 struct GNUNET_TIME_Relative timeout,
1005 GNUNET_STATISTICS_Callback cont,
1006 GNUNET_STATISTICS_Iterator proc, void *cls)
1010 struct GNUNET_STATISTICS_GetHandle *ai;
1014 GNUNET_assert (proc != NULL);
1015 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1016 if (subsystem == NULL)
1020 slen1 = strlen (subsystem) + 1;
1021 slen2 = strlen (name) + 1;
1022 GNUNET_assert (slen1 + slen2 + sizeof (struct GNUNET_MessageHeader) <
1023 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1024 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
1026 ai->subsystem = GNUNET_strdup (subsystem);
1027 ai->name = GNUNET_strdup (name);
1031 ai->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1032 ai->type = ACTION_GET;
1033 ai->msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
1034 GNUNET_CONTAINER_DLL_insert_tail (handle->action_head, handle->action_tail,
1036 schedule_action (handle);
1042 * Cancel a 'get' request. Must be called before the 'cont'
1043 * function is called.
1045 * @param gh handle of the request to cancel
1048 GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh)
1052 if (gh->sh->current == gh)
1054 gh->aborted = GNUNET_YES;
1058 GNUNET_CONTAINER_DLL_remove (gh->sh->action_head, gh->sh->action_tail, gh);
1059 GNUNET_free (gh->name);
1060 GNUNET_free (gh->subsystem);
1067 * Watch statistics from the peer (be notified whenever they change).
1068 * Note that the only way to cancel a "watch" request is to destroy
1069 * the statistics handle given as the first argument to this call.
1071 * @param handle identification of the statistics service
1072 * @param subsystem limit to the specified subsystem, never NULL
1073 * @param name name of the statistic value, never NULL
1074 * @param proc function to call on each value
1075 * @param proc_cls closure for proc
1076 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1079 GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
1080 const char *subsystem, const char *name,
1081 GNUNET_STATISTICS_Iterator proc, void *proc_cls)
1083 struct GNUNET_STATISTICS_WatchEntry *w;
1086 return GNUNET_SYSERR;
1087 w = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_WatchEntry));
1088 w->subsystem = GNUNET_strdup (subsystem);
1089 w->name = GNUNET_strdup (name);
1091 w->proc_cls = proc_cls;
1092 GNUNET_array_append (handle->watches, handle->watches_size, w);
1093 schedule_watch_request (handle, w);
1099 * Queue a request to change a statistic.
1101 * @param h statistics handle
1102 * @param name name of the value
1103 * @param make_persistent should the value be kept across restarts?
1104 * @param value new value or change
1105 * @param type type of the action (ACTION_SET or ACTION_UPDATE)
1108 add_setter_action (struct GNUNET_STATISTICS_Handle *h, const char *name,
1109 int make_persistent, uint64_t value, enum ActionType type)
1111 struct GNUNET_STATISTICS_GetHandle *ai;
1117 GNUNET_assert (h != NULL);
1118 GNUNET_assert (name != NULL);
1119 slen = strlen (h->subsystem) + 1;
1120 nlen = strlen (name) + 1;
1121 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
1122 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1127 for (ai = h->action_head; ai != NULL; ai = ai->next)
1129 if (! ( (0 == strcmp (ai->subsystem, h->subsystem)) &&
1130 (0 == strcmp (ai->name, name)) &&
1131 ( (ai->type == ACTION_UPDATE) ||
1132 (ai->type == ACTION_SET) ) ) )
1134 if (ai->type == ACTION_SET)
1136 if (type == ACTION_UPDATE)
1138 delta = (int64_t) value;
1141 /* update old set by new delta */
1146 /* update old set by new delta, but never go negative */
1147 if (ai->value < -delta)
1155 /* new set overrides old set */
1161 if (type == ACTION_UPDATE)
1163 /* make delta cummulative */
1164 delta = (int64_t) value;
1169 /* drop old 'update', use new 'set' instead */
1174 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1175 ai->make_persistent = make_persistent;
1178 /* no existing entry matches, create a fresh one */
1179 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
1181 ai->subsystem = GNUNET_strdup (h->subsystem);
1182 ai->name = GNUNET_strdup (name);
1183 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1184 ai->make_persistent = make_persistent;
1188 GNUNET_CONTAINER_DLL_insert_tail (h->action_head, h->action_tail,
1190 schedule_action (h);
1195 * Set statistic value for the peer. Will always use our
1196 * subsystem (the argument used when "handle" was created).
1198 * @param handle identification of the statistics service
1199 * @param name name of the statistic value
1200 * @param value new value to set
1201 * @param make_persistent should the value be kept across restarts?
1204 GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
1205 const char *name, uint64_t value, int make_persistent)
1209 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1210 add_setter_action (handle, name, make_persistent, value, ACTION_SET);
1215 * Set statistic value for the peer. Will always use our
1216 * subsystem (the argument used when "handle" was created).
1218 * @param handle identification of the statistics service
1219 * @param name name of the statistic value
1220 * @param delta change in value (added to existing value)
1221 * @param make_persistent should the value be kept across restarts?
1224 GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle,
1225 const char *name, int64_t delta, int make_persistent)
1231 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1232 add_setter_action (handle, name, make_persistent, (uint64_t) delta,
1237 /* end of statistics_api.c */