2 This file is part of GNUnet.
3 (C) 2009, 2010 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)
57 * Entry kept for each value we are watching.
59 struct GNUNET_STATISTICS_WatchEntry
63 * What subsystem is this action about? (never NULL)
68 * What value is this action about? (never NULL)
75 GNUNET_STATISTICS_Iterator proc;
86 * Linked list of things we still need to do.
88 struct GNUNET_STATISTICS_GetHandle
92 * This is a doubly linked list.
94 struct GNUNET_STATISTICS_GetHandle *next;
97 * This is a doubly linked list.
99 struct GNUNET_STATISTICS_GetHandle *prev;
102 * Main statistics handle.
104 struct GNUNET_STATISTICS_Handle *sh;
107 * What subsystem is this action about? (can be NULL)
112 * What value is this action about? (can be NULL)
117 * Continuation to call once action is complete.
119 GNUNET_STATISTICS_Callback cont;
122 * Function to call (for GET actions only).
124 GNUNET_STATISTICS_Iterator proc;
127 * Closure for proc and cont.
132 * Timeout for this action.
134 struct GNUNET_TIME_Absolute timeout;
142 * Flag for SET/UPDATE actions.
147 * Has the current iteration been aborted; for GET actions.
152 * Is this a GET, SET, UPDATE or WATCH?
154 enum ActionType type;
157 * Size of the message that we will be transmitting.
165 * Handle for the service.
167 struct GNUNET_STATISTICS_Handle
170 * Name of our subsystem.
175 * Configuration to use.
177 const struct GNUNET_CONFIGURATION_Handle *cfg;
180 * Socket (if available).
182 struct GNUNET_CLIENT_Connection *client;
185 * Currently pending transmission request.
187 struct GNUNET_CLIENT_TransmitHandle *th;
190 * Head of the linked list of pending actions (first action
193 struct GNUNET_STATISTICS_GetHandle *action_head;
196 * Tail of the linked list of actions (for fast append).
198 struct GNUNET_STATISTICS_GetHandle *action_tail;
201 * Action we are currently busy with (action request has been
202 * transmitted, we're now receiving the response from the
205 struct GNUNET_STATISTICS_GetHandle *current;
208 * Array of watch entries.
210 struct GNUNET_STATISTICS_WatchEntry **watches;
213 * Task doing exponential back-off trying to reconnect.
215 GNUNET_SCHEDULER_TaskIdentifier backoff_task;
218 * Time for next connect retry.
220 struct GNUNET_TIME_Relative backoff;
223 * Size of the 'watches' array.
225 unsigned int watches_size;
228 * Should this handle auto-destruct once all actions have
234 * Are we currently receiving from the service?
243 * Schedule the next action to be performed.
246 schedule_action (struct GNUNET_STATISTICS_Handle *h);
249 * Try to (re)connect to the statistics service.
251 * @return GNUNET_YES on success, GNUNET_NO on failure.
254 try_connect (struct GNUNET_STATISTICS_Handle *ret);
258 insert_ai (struct GNUNET_STATISTICS_Handle *h,
259 struct GNUNET_STATISTICS_GetHandle *ai)
261 GNUNET_CONTAINER_DLL_insert_after (h->action_head, h->action_tail,
263 if (h->action_head == ai)
269 schedule_watch_request (struct GNUNET_STATISTICS_Handle *h,
270 struct GNUNET_STATISTICS_WatchEntry *watch)
273 struct GNUNET_STATISTICS_GetHandle *ai;
278 GNUNET_assert (h != NULL);
279 if (GNUNET_YES != try_connect (h))
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;
306 * Try to (re)connect to the statistics service.
308 * @return GNUNET_YES on success, GNUNET_NO on failure.
311 try_connect (struct GNUNET_STATISTICS_Handle *ret)
315 if (ret->client != NULL)
317 ret->client = GNUNET_CLIENT_connect ("statistics", ret->cfg);
318 if (ret->client != NULL)
320 for (i = 0; i < ret->watches_size; i++)
321 schedule_watch_request (ret, ret->watches[i]);
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326 _("Failed to connect to statistics service!\n"));
333 * Free memory associated with the given action item.
336 free_action_item (struct GNUNET_STATISTICS_GetHandle *ai)
338 GNUNET_free_non_null (ai->subsystem);
339 GNUNET_free_non_null (ai->name);
345 * GET processing is complete, tell client about it.
348 finish (struct GNUNET_STATISTICS_Handle *h, int code)
350 struct GNUNET_STATISTICS_GetHandle *pos = h->current;
356 if (pos->cont != NULL)
357 pos->cont (pos->cls, code);
358 free_action_item (pos);
364 * Process the message.
366 * @return GNUNET_OK if the message was well-formed
369 process_message (struct GNUNET_STATISTICS_Handle *h,
370 const struct GNUNET_MessageHeader *msg)
374 const struct GNUNET_STATISTICS_ReplyMessage *smsg;
377 if (h->current->aborted)
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
381 "Iteration was aborted, ignoring VALUE\n");
383 return GNUNET_OK; /* don't bother */
385 size = ntohs (msg->size);
386 if (size < sizeof (struct GNUNET_STATISTICS_ReplyMessage))
389 return GNUNET_SYSERR;
391 smsg = (const struct GNUNET_STATISTICS_ReplyMessage *) msg;
392 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
394 GNUNET_STRINGS_buffer_tokenize ((const char *) &smsg[1], size, 2,
398 return GNUNET_SYSERR;
401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
402 "Received valid statistic on `%s:%s': %llu\n", service, name,
403 GNUNET_ntohll (smsg->value));
406 h->current->proc (h->current->cls, service, name,
407 GNUNET_ntohll (smsg->value),
409 (ntohl (smsg->uid) & GNUNET_STATISTICS_PERSIST_BIT)))
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Processing of remaining statistics aborted by client.\n");
415 h->current->aborted = GNUNET_YES;
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "VALUE processed successfully\n");
425 process_watch_value (struct GNUNET_STATISTICS_Handle *h,
426 const struct GNUNET_MessageHeader *msg)
428 const struct GNUNET_STATISTICS_WatchValueMessage *wvm;
429 struct GNUNET_STATISTICS_WatchEntry *w;
432 if (sizeof (struct GNUNET_STATISTICS_WatchValueMessage) != ntohs (msg->size))
435 return GNUNET_SYSERR;
437 wvm = (const struct GNUNET_STATISTICS_WatchValueMessage *) msg;
438 GNUNET_break (0 == ntohl (wvm->reserved));
439 wid = ntohl (wvm->wid);
440 if (wid >= h->watches_size)
443 return GNUNET_SYSERR;
446 (void) w->proc (w->proc_cls, w->subsystem, w->name,
447 GNUNET_ntohll (wvm->value),
448 0 != (ntohl (wvm->flags) & GNUNET_STATISTICS_PERSIST_BIT));
454 * Function called with messages from stats service.
457 * @param msg message received, NULL on timeout or fatal error
460 receive_stats (void *cls, const struct GNUNET_MessageHeader *msg)
462 struct GNUNET_STATISTICS_Handle *h = cls;
466 if (NULL != h->client)
468 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
473 "Error receiving statistics from service, is the service running?\n");
475 finish (h, GNUNET_SYSERR);
478 switch (ntohs (msg->type))
480 case GNUNET_MESSAGE_TYPE_STATISTICS_END:
482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received end of statistics marker\n");
484 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
485 if (h->watches_size > 0)
487 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
488 GNUNET_TIME_UNIT_FOREVER_REL);
492 h->receiving = GNUNET_NO;
494 finish (h, GNUNET_OK);
496 case GNUNET_MESSAGE_TYPE_STATISTICS_VALUE:
497 if (GNUNET_OK == process_message (h, msg))
499 /* finally, look for more! */
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
502 "Processing VALUE done, now reading more\n");
504 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
505 GNUNET_TIME_absolute_get_remaining (h->
507 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
512 case GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE:
513 if (GNUNET_OK == process_watch_value (h, msg))
515 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
516 GNUNET_assert (h->watches_size > 0);
517 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
518 GNUNET_TIME_UNIT_FOREVER_REL);
527 if (NULL != h->client)
529 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
532 finish (h, GNUNET_SYSERR);
537 * Transmit a GET request (and if successful, start to receive
541 transmit_get (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
543 struct GNUNET_MessageHeader *hdr;
550 /* timeout / error */
552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
553 "Transmission of request for statistics failed!\n");
555 finish (handle, GNUNET_SYSERR);
558 slen1 = strlen (handle->current->subsystem) + 1;
559 slen2 = strlen (handle->current->name) + 1;
560 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
561 GNUNET_assert (msize <= size);
562 hdr = (struct GNUNET_MessageHeader *) buf;
563 hdr->size = htons (msize);
564 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_GET);
565 GNUNET_assert (slen1 + slen2 ==
566 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
567 handle->current->subsystem,
568 handle->current->name));
569 if (!handle->receiving)
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
573 "Transmission of GET done, now reading response\n");
575 handle->receiving = GNUNET_YES;
576 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
577 GNUNET_TIME_absolute_get_remaining (handle->
585 * Transmit a WATCH request (and if successful, start to receive
589 transmit_watch (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
591 struct GNUNET_MessageHeader *hdr;
598 /* timeout / error */
600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
601 "Transmission of request for statistics failed!\n");
603 finish (handle, GNUNET_SYSERR);
607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting watch request for `%s'\n",
608 handle->current->name);
610 slen1 = strlen (handle->current->subsystem) + 1;
611 slen2 = strlen (handle->current->name) + 1;
612 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
613 GNUNET_assert (msize <= size);
614 hdr = (struct GNUNET_MessageHeader *) buf;
615 hdr->size = htons (msize);
616 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH);
617 GNUNET_assert (slen1 + slen2 ==
618 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
619 handle->current->subsystem,
620 handle->current->name));
621 if (GNUNET_YES != handle->receiving)
623 handle->receiving = GNUNET_YES;
624 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
625 GNUNET_TIME_UNIT_FOREVER_REL);
627 finish (handle, GNUNET_OK);
633 * Transmit a SET/UPDATE request.
636 transmit_set (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
638 struct GNUNET_STATISTICS_SetMessage *r;
645 finish (handle, GNUNET_SYSERR);
649 slen = strlen (handle->current->subsystem) + 1;
650 nlen = strlen (handle->current->name) + 1;
651 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
655 finish (handle, GNUNET_SYSERR);
659 r->header.size = htons (nsize);
660 r->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
662 r->value = GNUNET_htonll (handle->current->value);
663 if (handle->current->make_persistent)
664 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_PERSISTENT);
665 if (handle->current->type == ACTION_UPDATE)
666 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_RELATIVE);
667 GNUNET_assert (slen + nlen ==
668 GNUNET_STRINGS_buffer_fill ((char *) &r[1], slen + nlen, 2,
669 handle->current->subsystem,
670 handle->current->name));
671 finish (handle, GNUNET_OK);
677 transmit_action (void *cls, size_t size, void *buf)
679 struct GNUNET_STATISTICS_Handle *handle = cls;
683 switch (handle->current->type)
686 ret = transmit_get (handle, size, buf);
690 ret = transmit_set (handle, size, buf);
693 ret = transmit_watch (handle, size, buf);
705 * Get handle for the statistics service.
707 * @param subsystem name of subsystem using the service
708 * @param cfg services configuration in use
709 * @return handle to use
711 struct GNUNET_STATISTICS_Handle *
712 GNUNET_STATISTICS_create (const char *subsystem,
713 const struct GNUNET_CONFIGURATION_Handle *cfg)
715 struct GNUNET_STATISTICS_Handle *ret;
717 GNUNET_assert (subsystem != NULL);
718 GNUNET_assert (cfg != NULL);
719 ret = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_Handle));
721 ret->subsystem = GNUNET_strdup (subsystem);
722 ret->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
723 if (GNUNET_YES != try_connect (ret))
725 GNUNET_free (ret->subsystem);
734 * Destroy a handle (free all state associated with
737 * @param h statistics handle to destroy
738 * @param sync_first set to GNUNET_YES if pending SET requests should
742 GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h, int sync_first)
744 struct GNUNET_STATISTICS_GetHandle *pos;
745 struct GNUNET_STATISTICS_GetHandle *next;
746 struct GNUNET_STATISTICS_GetHandle *prev;
747 struct GNUNET_TIME_Relative timeout;
752 if (GNUNET_SCHEDULER_NO_TASK != h->backoff_task)
753 GNUNET_SCHEDULER_cancel (h->backoff_task);
756 if (h->current != NULL)
758 if (h->current->type == ACTION_GET)
760 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
762 free_action_item (h->current);
766 pos = h->action_head;
771 if (pos->type == ACTION_GET)
774 h->action_head = next;
777 free_action_item (pos);
785 h->action_tail = prev;
786 if (h->current == NULL)
788 h->current = h->action_head;
789 if (h->action_head != NULL)
791 h->action_head = h->action_head->next;
792 if (h->action_head == NULL)
793 h->action_tail = NULL;
796 h->do_destroy = GNUNET_YES;
797 if ((h->current != NULL) && (h->th == NULL))
799 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
801 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
803 &transmit_action, h);
804 GNUNET_assert (NULL != h->th);
811 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
814 if (h->current != NULL)
815 free_action_item (h->current);
816 while (NULL != (pos = h->action_head))
818 h->action_head = pos->next;
819 free_action_item (pos);
821 if (h->client != NULL)
823 GNUNET_CLIENT_disconnect (h->client, GNUNET_YES);
826 for (i = 0; i < h->watches_size; i++)
828 GNUNET_free (h->watches[i]->subsystem);
829 GNUNET_free (h->watches[i]->name);
830 GNUNET_free (h->watches[i]);
832 GNUNET_array_grow (h->watches, h->watches_size, 0);
833 GNUNET_free (h->subsystem);
839 finish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
841 struct GNUNET_STATISTICS_Handle *h = cls;
843 h->backoff_task = GNUNET_SCHEDULER_NO_TASK;
844 finish (h, GNUNET_SYSERR);
849 * Schedule the next action to be performed.
852 schedule_action (struct GNUNET_STATISTICS_Handle *h)
854 struct GNUNET_TIME_Relative timeout;
856 if (h->current != NULL)
857 return; /* action already pending */
858 if (GNUNET_YES != try_connect (h))
861 GNUNET_SCHEDULER_add_delayed (h->backoff, &finish_task, h);
862 h->backoff = GNUNET_TIME_relative_multiply (h->backoff, 2);
864 GNUNET_TIME_relative_min (h->backoff, GNUNET_CONSTANTS_SERVICE_TIMEOUT);
868 /* schedule next action */
869 h->current = h->action_head;
870 if (NULL == h->current)
874 h->do_destroy = GNUNET_NO;
875 GNUNET_STATISTICS_destroy (h, GNUNET_YES);
879 GNUNET_CONTAINER_DLL_remove (h->action_head, h->action_tail, h->current);
880 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
883 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
885 &transmit_action, h)))
888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
889 "Failed to transmit request to statistics service.\n");
891 finish (h, GNUNET_SYSERR);
897 * Get statistic from the peer.
899 * @param handle identification of the statistics service
900 * @param subsystem limit to the specified subsystem, NULL for our subsystem
901 * @param name name of the statistic value, NULL for all values
902 * @param timeout after how long should we give up (and call
903 * cont with an error code)?
904 * @param cont continuation to call when done (can be NULL)
905 * @param proc function to call on each value
906 * @param cls closure for cont and proc
907 * @return NULL on error
909 struct GNUNET_STATISTICS_GetHandle *
910 GNUNET_STATISTICS_get (struct GNUNET_STATISTICS_Handle *handle,
911 const char *subsystem, const char *name,
912 struct GNUNET_TIME_Relative timeout,
913 GNUNET_STATISTICS_Callback cont,
914 GNUNET_STATISTICS_Iterator proc, void *cls)
918 struct GNUNET_STATISTICS_GetHandle *ai;
920 GNUNET_assert (handle != NULL);
921 GNUNET_assert (proc != NULL);
922 GNUNET_assert (GNUNET_NO == handle->do_destroy);
923 if (GNUNET_YES != try_connect (handle))
926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
927 "Failed to connect to statistics service, can not get value `%s:%s'.\n",
928 strlen (subsystem) ? subsystem : "*",
929 strlen (name) ? name : "*");
933 if (subsystem == NULL)
937 slen1 = strlen (subsystem) + 1;
938 slen2 = strlen (name) + 1;
939 GNUNET_assert (slen1 + slen2 + sizeof (struct GNUNET_MessageHeader) <
940 GNUNET_SERVER_MAX_MESSAGE_SIZE);
941 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
943 ai->subsystem = GNUNET_strdup (subsystem);
944 ai->name = GNUNET_strdup (name);
948 ai->timeout = GNUNET_TIME_relative_to_absolute (timeout);
949 ai->type = ACTION_GET;
950 ai->msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
951 insert_ai (handle, ai);
957 * Cancel a 'get' request. Must be called before the 'cont'
958 * function is called.
960 * @param gh handle of the request to cancel
963 GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh)
965 if (gh->sh->current == gh)
967 gh->aborted = GNUNET_YES;
971 GNUNET_CONTAINER_DLL_remove (gh->sh->action_head, gh->sh->action_tail, gh);
972 GNUNET_free (gh->name);
973 GNUNET_free (gh->subsystem);
980 * Watch statistics from the peer (be notified whenever they change).
981 * Note that the only way to cancel a "watch" request is to destroy
982 * the statistics handle given as the first argument to this call.
984 * @param handle identification of the statistics service
985 * @param subsystem limit to the specified subsystem, never NULL
986 * @param name name of the statistic value, never NULL
987 * @param proc function to call on each value
988 * @param proc_cls closure for proc
989 * @return GNUNET_OK on success, GNUNET_SYSERR on error
992 GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
993 const char *subsystem, const char *name,
994 GNUNET_STATISTICS_Iterator proc, void *proc_cls)
996 struct GNUNET_STATISTICS_WatchEntry *w;
999 return GNUNET_SYSERR;
1000 w = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_WatchEntry));
1001 w->subsystem = GNUNET_strdup (subsystem);
1002 w->name = GNUNET_strdup (name);
1004 w->proc_cls = proc_cls;
1005 GNUNET_array_append (handle->watches, handle->watches_size, w);
1006 schedule_watch_request (handle, w);
1012 add_setter_action (struct GNUNET_STATISTICS_Handle *h, const char *name,
1013 int make_persistent, uint64_t value, enum ActionType type)
1015 struct GNUNET_STATISTICS_GetHandle *ai;
1021 GNUNET_assert (h != NULL);
1022 GNUNET_assert (name != NULL);
1023 if (GNUNET_YES != try_connect (h))
1025 slen = strlen (h->subsystem) + 1;
1026 nlen = strlen (name) + 1;
1027 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
1028 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1033 ai = h->action_head;
1036 if ((0 == strcmp (ai->subsystem, h->subsystem)) &&
1037 (0 == strcmp (ai->name, name)) && ((ai->type == ACTION_UPDATE) ||
1038 (ai->type == ACTION_SET)))
1040 if (ai->type == ACTION_SET)
1042 if (type == ACTION_UPDATE)
1044 delta = (int64_t) value;
1051 if (ai->value < -delta)
1064 if (type == ACTION_UPDATE)
1066 delta = (int64_t) value;
1075 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1076 ai->make_persistent = make_persistent;
1081 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
1083 ai->subsystem = GNUNET_strdup (h->subsystem);
1084 ai->name = GNUNET_strdup (name);
1085 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1086 ai->make_persistent = make_persistent;
1095 * Set statistic value for the peer. Will always use our
1096 * subsystem (the argument used when "handle" was created).
1098 * @param handle identification of the statistics service
1099 * @param name name of the statistic value
1100 * @param value new value to set
1101 * @param make_persistent should the value be kept across restarts?
1104 GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
1105 const char *name, uint64_t value, int make_persistent)
1109 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1110 add_setter_action (handle, name, make_persistent, value, ACTION_SET);
1115 * Set statistic value for the peer. Will always use our
1116 * subsystem (the argument used when "handle" was created).
1118 * @param handle identification of the statistics service
1119 * @param name name of the statistic value
1120 * @param delta change in value (added to existing value)
1121 * @param make_persistent should the value be kept across restarts?
1124 GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle,
1125 const char *name, int64_t delta, int make_persistent)
1131 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1132 add_setter_action (handle, name, make_persistent, (uint64_t) delta,
1137 /* end of statistics_api.c */