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)
43 #define LOG(kind,...) GNUNET_log_from (kind, "statistics-api",__VA_ARGS__)
58 * Entry kept for each value we are watching.
60 struct GNUNET_STATISTICS_WatchEntry
64 * What subsystem is this action about? (never NULL)
69 * What value is this action about? (never NULL)
76 GNUNET_STATISTICS_Iterator proc;
87 * Linked list of things we still need to do.
89 struct GNUNET_STATISTICS_GetHandle
93 * This is a doubly linked list.
95 struct GNUNET_STATISTICS_GetHandle *next;
98 * This is a doubly linked list.
100 struct GNUNET_STATISTICS_GetHandle *prev;
103 * Main statistics handle.
105 struct GNUNET_STATISTICS_Handle *sh;
108 * What subsystem is this action about? (can be NULL)
113 * What value is this action about? (can be NULL)
118 * Continuation to call once action is complete.
120 GNUNET_STATISTICS_Callback cont;
123 * Function to call (for GET actions only).
125 GNUNET_STATISTICS_Iterator proc;
128 * Closure for proc and cont.
133 * Timeout for this action.
135 struct GNUNET_TIME_Absolute timeout;
143 * Flag for SET/UPDATE actions.
148 * Has the current iteration been aborted; for GET actions.
153 * Is this a GET, SET, UPDATE or WATCH?
155 enum ActionType type;
158 * Size of the message that we will be transmitting.
166 * Handle for the service.
168 struct GNUNET_STATISTICS_Handle
171 * Name of our subsystem.
176 * Configuration to use.
178 const struct GNUNET_CONFIGURATION_Handle *cfg;
181 * Socket (if available).
183 struct GNUNET_CLIENT_Connection *client;
186 * Currently pending transmission request.
188 struct GNUNET_CLIENT_TransmitHandle *th;
191 * Head of the linked list of pending actions (first action
194 struct GNUNET_STATISTICS_GetHandle *action_head;
197 * Tail of the linked list of actions (for fast append).
199 struct GNUNET_STATISTICS_GetHandle *action_tail;
202 * Action we are currently busy with (action request has been
203 * transmitted, we're now receiving the response from the
206 struct GNUNET_STATISTICS_GetHandle *current;
209 * Array of watch entries.
211 struct GNUNET_STATISTICS_WatchEntry **watches;
214 * Task doing exponential back-off trying to reconnect.
216 GNUNET_SCHEDULER_TaskIdentifier backoff_task;
219 * Time for next connect retry.
221 struct GNUNET_TIME_Relative backoff;
224 * Size of the 'watches' array.
226 unsigned int watches_size;
229 * Should this handle auto-destruct once all actions have
235 * Are we currently receiving from the service?
244 * Schedule the next action to be performed.
246 static void 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.
253 static int try_connect (struct GNUNET_STATISTICS_Handle *ret);
257 insert_ai (struct GNUNET_STATISTICS_Handle *h,
258 struct GNUNET_STATISTICS_GetHandle *ai)
260 GNUNET_CONTAINER_DLL_insert_after (h->action_head, h->action_tail,
262 if (h->action_head == ai)
268 schedule_watch_request (struct GNUNET_STATISTICS_Handle *h,
269 struct GNUNET_STATISTICS_WatchEntry *watch)
272 struct GNUNET_STATISTICS_GetHandle *ai;
277 GNUNET_assert (h != NULL);
278 if (GNUNET_YES != try_connect (h))
283 slen = strlen (watch->subsystem) + 1;
284 nlen = strlen (watch->name) + 1;
285 nsize = sizeof (struct GNUNET_MessageHeader) + slen + nlen;
286 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
291 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
293 ai->subsystem = GNUNET_strdup (watch->subsystem);
294 ai->name = GNUNET_strdup (watch->name);
295 ai->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
297 ai->type = ACTION_WATCH;
298 ai->proc = watch->proc;
299 ai->cls = watch->proc_cls;
305 * Try to (re)connect to the statistics service.
307 * @return GNUNET_YES on success, GNUNET_NO on failure.
310 try_connect (struct GNUNET_STATISTICS_Handle *ret)
314 if (ret->client != NULL)
316 ret->client = GNUNET_CLIENT_connect ("statistics", ret->cfg);
317 if (ret->client != NULL)
319 for (i = 0; i < ret->watches_size; i++)
320 schedule_watch_request (ret, ret->watches[i]);
324 LOG (GNUNET_ERROR_TYPE_DEBUG,
325 _("Failed to connect to statistics service!\n"));
332 * Free memory associated with the given action item.
335 free_action_item (struct GNUNET_STATISTICS_GetHandle *ai)
337 GNUNET_free_non_null (ai->subsystem);
338 GNUNET_free_non_null (ai->name);
344 * GET processing is complete, tell client about it.
347 finish (struct GNUNET_STATISTICS_Handle *h, int code)
349 struct GNUNET_STATISTICS_GetHandle *pos = h->current;
355 if (pos->cont != NULL)
356 pos->cont (pos->cls, code);
357 free_action_item (pos);
363 * Process the message.
365 * @return GNUNET_OK if the message was well-formed
368 process_message (struct GNUNET_STATISTICS_Handle *h,
369 const struct GNUNET_MessageHeader *msg)
373 const struct GNUNET_STATISTICS_ReplyMessage *smsg;
376 if (h->current->aborted)
379 LOG (GNUNET_ERROR_TYPE_DEBUG,
380 "Iteration was aborted, ignoring VALUE\n");
382 return GNUNET_OK; /* don't bother */
384 size = ntohs (msg->size);
385 if (size < sizeof (struct GNUNET_STATISTICS_ReplyMessage))
388 return GNUNET_SYSERR;
390 smsg = (const struct GNUNET_STATISTICS_ReplyMessage *) msg;
391 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
393 GNUNET_STRINGS_buffer_tokenize ((const char *) &smsg[1], size, 2,
397 return GNUNET_SYSERR;
400 LOG (GNUNET_ERROR_TYPE_DEBUG,
401 "Received valid statistic on `%s:%s': %llu\n", service, name,
402 GNUNET_ntohll (smsg->value));
405 h->current->proc (h->current->cls, service, name,
406 GNUNET_ntohll (smsg->value),
408 (ntohl (smsg->uid) & GNUNET_STATISTICS_PERSIST_BIT)))
411 LOG (GNUNET_ERROR_TYPE_DEBUG,
412 "Processing of remaining statistics aborted by client.\n");
414 h->current->aborted = GNUNET_YES;
417 LOG (GNUNET_ERROR_TYPE_DEBUG, "VALUE processed successfully\n");
424 process_watch_value (struct GNUNET_STATISTICS_Handle *h,
425 const struct GNUNET_MessageHeader *msg)
427 const struct GNUNET_STATISTICS_WatchValueMessage *wvm;
428 struct GNUNET_STATISTICS_WatchEntry *w;
431 if (sizeof (struct GNUNET_STATISTICS_WatchValueMessage) !=
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 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 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 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
506 (h->current->timeout));
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 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,
567 2, handle->current->subsystem,
568 handle->current->name));
569 if (!handle->receiving)
572 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
578 (handle->current->timeout));
585 * Transmit a WATCH request (and if successful, start to receive
589 transmit_watch (struct GNUNET_STATISTICS_Handle *handle, size_t size,
592 struct GNUNET_MessageHeader *hdr;
599 /* timeout / error */
601 LOG (GNUNET_ERROR_TYPE_DEBUG,
602 "Transmission of request for statistics failed!\n");
604 finish (handle, GNUNET_SYSERR);
608 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting watch request for `%s'\n",
609 handle->current->name);
611 slen1 = strlen (handle->current->subsystem) + 1;
612 slen2 = strlen (handle->current->name) + 1;
613 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
614 GNUNET_assert (msize <= size);
615 hdr = (struct GNUNET_MessageHeader *) buf;
616 hdr->size = htons (msize);
617 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH);
618 GNUNET_assert (slen1 + slen2 ==
619 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2,
620 2, handle->current->subsystem,
621 handle->current->name));
622 if (GNUNET_YES != handle->receiving)
624 handle->receiving = GNUNET_YES;
625 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
626 GNUNET_TIME_UNIT_FOREVER_REL);
628 finish (handle, GNUNET_OK);
634 * Transmit a SET/UPDATE request.
637 transmit_set (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
639 struct GNUNET_STATISTICS_SetMessage *r;
646 finish (handle, GNUNET_SYSERR);
650 slen = strlen (handle->current->subsystem) + 1;
651 nlen = strlen (handle->current->name) + 1;
652 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
656 finish (handle, GNUNET_SYSERR);
660 r->header.size = htons (nsize);
661 r->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
663 r->value = GNUNET_htonll (handle->current->value);
664 if (handle->current->make_persistent)
665 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_PERSISTENT);
666 if (handle->current->type == ACTION_UPDATE)
667 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_RELATIVE);
668 GNUNET_assert (slen + nlen ==
669 GNUNET_STRINGS_buffer_fill ((char *) &r[1], slen + nlen, 2,
670 handle->current->subsystem,
671 handle->current->name));
672 finish (handle, GNUNET_OK);
678 transmit_action (void *cls, size_t size, void *buf)
680 struct GNUNET_STATISTICS_Handle *handle = cls;
684 switch (handle->current->type)
687 ret = transmit_get (handle, size, buf);
691 ret = transmit_set (handle, size, buf);
694 ret = transmit_watch (handle, size, buf);
706 * Get handle for the statistics service.
708 * @param subsystem name of subsystem using the service
709 * @param cfg services configuration in use
710 * @return handle to use
712 struct GNUNET_STATISTICS_Handle *
713 GNUNET_STATISTICS_create (const char *subsystem,
714 const struct GNUNET_CONFIGURATION_Handle *cfg)
716 struct GNUNET_STATISTICS_Handle *ret;
718 GNUNET_assert (subsystem != NULL);
719 GNUNET_assert (cfg != NULL);
720 ret = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_Handle));
722 ret->subsystem = GNUNET_strdup (subsystem);
723 ret->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
724 if (GNUNET_YES != try_connect (ret))
726 GNUNET_free (ret->subsystem);
735 * Destroy a handle (free all state associated with
738 * @param h statistics handle to destroy
739 * @param sync_first set to GNUNET_YES if pending SET requests should
743 GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h, int sync_first)
745 struct GNUNET_STATISTICS_GetHandle *pos;
746 struct GNUNET_STATISTICS_GetHandle *next;
747 struct GNUNET_STATISTICS_GetHandle *prev;
748 struct GNUNET_TIME_Relative timeout;
753 if (GNUNET_SCHEDULER_NO_TASK != h->backoff_task)
754 GNUNET_SCHEDULER_cancel (h->backoff_task);
757 if (h->current != NULL)
759 if (h->current->type == ACTION_GET)
761 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
763 free_action_item (h->current);
767 pos = h->action_head;
772 if (pos->type == ACTION_GET)
775 h->action_head = next;
778 free_action_item (pos);
786 h->action_tail = prev;
787 if (h->current == NULL)
789 h->current = h->action_head;
790 if (h->action_head != NULL)
792 h->action_head = h->action_head->next;
793 if (h->action_head == NULL)
794 h->action_tail = NULL;
797 h->do_destroy = GNUNET_YES;
798 if ((h->current != NULL) && (h->th == NULL))
800 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
802 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
804 &transmit_action, h);
805 GNUNET_assert (NULL != h->th);
812 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
815 if (h->current != NULL)
816 free_action_item (h->current);
817 while (NULL != (pos = h->action_head))
819 h->action_head = pos->next;
820 free_action_item (pos);
822 if (h->client != NULL)
824 GNUNET_CLIENT_disconnect (h->client, GNUNET_YES);
827 for (i = 0; i < h->watches_size; i++)
829 GNUNET_free (h->watches[i]->subsystem);
830 GNUNET_free (h->watches[i]->name);
831 GNUNET_free (h->watches[i]);
833 GNUNET_array_grow (h->watches, h->watches_size, 0);
834 GNUNET_free (h->subsystem);
840 finish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
842 struct GNUNET_STATISTICS_Handle *h = cls;
844 h->backoff_task = GNUNET_SCHEDULER_NO_TASK;
845 finish (h, GNUNET_SYSERR);
850 * Schedule the next action to be performed.
853 schedule_action (struct GNUNET_STATISTICS_Handle *h)
855 struct GNUNET_TIME_Relative timeout;
857 if (h->current != NULL)
858 return; /* action already pending */
859 if (GNUNET_YES != try_connect (h))
862 GNUNET_SCHEDULER_add_delayed (h->backoff, &finish_task, h);
863 h->backoff = GNUNET_TIME_relative_multiply (h->backoff, 2);
865 GNUNET_TIME_relative_min (h->backoff,
866 GNUNET_CONSTANTS_SERVICE_TIMEOUT);
870 /* schedule next action */
871 h->current = h->action_head;
872 if (NULL == h->current)
876 h->do_destroy = GNUNET_NO;
877 GNUNET_STATISTICS_destroy (h, GNUNET_YES);
881 GNUNET_CONTAINER_DLL_remove (h->action_head, h->action_tail, h->current);
882 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
885 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
887 &transmit_action, h)))
890 LOG (GNUNET_ERROR_TYPE_DEBUG,
891 "Failed to transmit request to statistics service.\n");
893 finish (h, GNUNET_SYSERR);
899 * Get statistic from the peer.
901 * @param handle identification of the statistics service
902 * @param subsystem limit to the specified subsystem, NULL for our subsystem
903 * @param name name of the statistic value, NULL for all values
904 * @param timeout after how long should we give up (and call
905 * cont with an error code)?
906 * @param cont continuation to call when done (can be NULL)
907 * @param proc function to call on each value
908 * @param cls closure for cont and proc
909 * @return NULL on error
911 struct GNUNET_STATISTICS_GetHandle *
912 GNUNET_STATISTICS_get (struct GNUNET_STATISTICS_Handle *handle,
913 const char *subsystem, const char *name,
914 struct GNUNET_TIME_Relative timeout,
915 GNUNET_STATISTICS_Callback cont,
916 GNUNET_STATISTICS_Iterator proc, void *cls)
920 struct GNUNET_STATISTICS_GetHandle *ai;
922 GNUNET_assert (handle != NULL);
923 GNUNET_assert (proc != NULL);
924 GNUNET_assert (GNUNET_NO == handle->do_destroy);
925 if (GNUNET_YES != try_connect (handle))
928 LOG (GNUNET_ERROR_TYPE_DEBUG,
929 "Failed to connect to statistics service, can not get value `%s:%s'.\n",
930 strlen (subsystem) ? subsystem : "*", strlen (name) ? name : "*");
934 if (subsystem == NULL)
938 slen1 = strlen (subsystem) + 1;
939 slen2 = strlen (name) + 1;
940 GNUNET_assert (slen1 + slen2 + sizeof (struct GNUNET_MessageHeader) <
941 GNUNET_SERVER_MAX_MESSAGE_SIZE);
942 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
944 ai->subsystem = GNUNET_strdup (subsystem);
945 ai->name = GNUNET_strdup (name);
949 ai->timeout = GNUNET_TIME_relative_to_absolute (timeout);
950 ai->type = ACTION_GET;
951 ai->msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
952 insert_ai (handle, ai);
958 * Cancel a 'get' request. Must be called before the 'cont'
959 * function is called.
961 * @param gh handle of the request to cancel
964 GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh)
966 if (gh->sh->current == gh)
968 gh->aborted = GNUNET_YES;
972 GNUNET_CONTAINER_DLL_remove (gh->sh->action_head, gh->sh->action_tail,
974 GNUNET_free (gh->name);
975 GNUNET_free (gh->subsystem);
982 * Watch statistics from the peer (be notified whenever they change).
983 * Note that the only way to cancel a "watch" request is to destroy
984 * the statistics handle given as the first argument to this call.
986 * @param handle identification of the statistics service
987 * @param subsystem limit to the specified subsystem, never NULL
988 * @param name name of the statistic value, never NULL
989 * @param proc function to call on each value
990 * @param proc_cls closure for proc
991 * @return GNUNET_OK on success, GNUNET_SYSERR on error
994 GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
995 const char *subsystem, const char *name,
996 GNUNET_STATISTICS_Iterator proc, void *proc_cls)
998 struct GNUNET_STATISTICS_WatchEntry *w;
1001 return GNUNET_SYSERR;
1002 w = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_WatchEntry));
1003 w->subsystem = GNUNET_strdup (subsystem);
1004 w->name = GNUNET_strdup (name);
1006 w->proc_cls = proc_cls;
1007 GNUNET_array_append (handle->watches, handle->watches_size, w);
1008 schedule_watch_request (handle, w);
1014 add_setter_action (struct GNUNET_STATISTICS_Handle *h, const char *name,
1015 int make_persistent, uint64_t value, enum ActionType type)
1017 struct GNUNET_STATISTICS_GetHandle *ai;
1023 GNUNET_assert (h != NULL);
1024 GNUNET_assert (name != NULL);
1025 if (GNUNET_YES != try_connect (h))
1027 slen = strlen (h->subsystem) + 1;
1028 nlen = strlen (name) + 1;
1029 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
1030 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1035 ai = h->action_head;
1038 if ((0 == strcmp (ai->subsystem, h->subsystem)) &&
1039 (0 == strcmp (ai->name, name)) && ((ai->type == ACTION_UPDATE) ||
1040 (ai->type == ACTION_SET)))
1042 if (ai->type == ACTION_SET)
1044 if (type == ACTION_UPDATE)
1046 delta = (int64_t) value;
1053 if (ai->value < -delta)
1066 if (type == ACTION_UPDATE)
1068 delta = (int64_t) value;
1078 GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1079 ai->make_persistent = make_persistent;
1084 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
1086 ai->subsystem = GNUNET_strdup (h->subsystem);
1087 ai->name = GNUNET_strdup (name);
1088 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1089 ai->make_persistent = make_persistent;
1098 * Set statistic value for the peer. Will always use our
1099 * subsystem (the argument used when "handle" was created).
1101 * @param handle identification of the statistics service
1102 * @param name name of the statistic value
1103 * @param value new value to set
1104 * @param make_persistent should the value be kept across restarts?
1107 GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
1108 const char *name, uint64_t value, int make_persistent)
1112 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1113 add_setter_action (handle, name, make_persistent, value, ACTION_SET);
1118 * Set statistic value for the peer. Will always use our
1119 * subsystem (the argument used when "handle" was created).
1121 * @param handle identification of the statistics service
1122 * @param name name of the statistic value
1123 * @param delta change in value (added to existing value)
1124 * @param make_persistent should the value be kept across restarts?
1127 GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle,
1128 const char *name, int64_t delta,
1129 int make_persistent)
1135 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1136 add_setter_action (handle, name, make_persistent, (uint64_t) delta,
1141 /* end of statistics_api.c */