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.
247 schedule_action (struct GNUNET_STATISTICS_Handle *h);
250 * Try to (re)connect to the statistics service.
252 * @return GNUNET_YES on success, GNUNET_NO on failure.
255 try_connect (struct GNUNET_STATISTICS_Handle *ret);
259 insert_ai (struct GNUNET_STATISTICS_Handle *h,
260 struct GNUNET_STATISTICS_GetHandle *ai)
262 GNUNET_CONTAINER_DLL_insert_after (h->action_head, h->action_tail,
264 if (h->action_head == ai)
270 schedule_watch_request (struct GNUNET_STATISTICS_Handle *h,
271 struct GNUNET_STATISTICS_WatchEntry *watch)
274 struct GNUNET_STATISTICS_GetHandle *ai;
279 GNUNET_assert (h != NULL);
280 if (GNUNET_YES != try_connect (h))
285 slen = strlen (watch->subsystem) + 1;
286 nlen = strlen (watch->name) + 1;
287 nsize = sizeof (struct GNUNET_MessageHeader) + slen + nlen;
288 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
293 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
295 ai->subsystem = GNUNET_strdup (watch->subsystem);
296 ai->name = GNUNET_strdup (watch->name);
297 ai->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
299 ai->type = ACTION_WATCH;
300 ai->proc = watch->proc;
301 ai->cls = watch->proc_cls;
307 * Try to (re)connect to the statistics service.
309 * @return GNUNET_YES on success, GNUNET_NO on failure.
312 try_connect (struct GNUNET_STATISTICS_Handle *ret)
316 if (ret->client != NULL)
318 ret->client = GNUNET_CLIENT_connect ("statistics", ret->cfg);
319 if (ret->client != NULL)
321 for (i = 0; i < ret->watches_size; i++)
322 schedule_watch_request (ret, ret->watches[i]);
326 LOG (GNUNET_ERROR_TYPE_DEBUG,
327 _("Failed to connect to statistics service!\n"));
334 * Free memory associated with the given action item.
337 free_action_item (struct GNUNET_STATISTICS_GetHandle *ai)
339 GNUNET_free_non_null (ai->subsystem);
340 GNUNET_free_non_null (ai->name);
346 * GET processing is complete, tell client about it.
349 finish (struct GNUNET_STATISTICS_Handle *h, int code)
351 struct GNUNET_STATISTICS_GetHandle *pos = h->current;
357 if (pos->cont != NULL)
358 pos->cont (pos->cls, code);
359 free_action_item (pos);
365 * Process the message.
367 * @return GNUNET_OK if the message was well-formed
370 process_message (struct GNUNET_STATISTICS_Handle *h,
371 const struct GNUNET_MessageHeader *msg)
375 const struct GNUNET_STATISTICS_ReplyMessage *smsg;
378 if (h->current->aborted)
381 LOG (GNUNET_ERROR_TYPE_DEBUG, "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 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received valid statistic on `%s:%s': %llu\n",
402 service, name, 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) != ntohs (msg->size))
434 return GNUNET_SYSERR;
436 wvm = (const struct GNUNET_STATISTICS_WatchValueMessage *) msg;
437 GNUNET_break (0 == ntohl (wvm->reserved));
438 wid = ntohl (wvm->wid);
439 if (wid >= h->watches_size)
442 return GNUNET_SYSERR;
445 (void) w->proc (w->proc_cls, w->subsystem, w->name,
446 GNUNET_ntohll (wvm->value),
447 0 != (ntohl (wvm->flags) & GNUNET_STATISTICS_PERSIST_BIT));
453 * Function called with messages from stats service.
456 * @param msg message received, NULL on timeout or fatal error
459 receive_stats (void *cls, const struct GNUNET_MessageHeader *msg)
461 struct GNUNET_STATISTICS_Handle *h = cls;
465 if (NULL != h->client)
467 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
471 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
472 "Error receiving statistics from service, is the service running?\n");
474 finish (h, GNUNET_SYSERR);
477 switch (ntohs (msg->type))
479 case GNUNET_MESSAGE_TYPE_STATISTICS_END:
481 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received end of statistics marker\n");
483 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
484 if (h->watches_size > 0)
486 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
487 GNUNET_TIME_UNIT_FOREVER_REL);
491 h->receiving = GNUNET_NO;
493 finish (h, GNUNET_OK);
495 case GNUNET_MESSAGE_TYPE_STATISTICS_VALUE:
496 if (GNUNET_OK == process_message (h, msg))
498 /* finally, look for more! */
500 LOG (GNUNET_ERROR_TYPE_DEBUG,
501 "Processing VALUE done, now reading more\n");
503 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
504 GNUNET_TIME_absolute_get_remaining (h->
506 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
511 case GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE:
512 if (GNUNET_OK == process_watch_value (h, msg))
514 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
515 GNUNET_assert (h->watches_size > 0);
516 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
517 GNUNET_TIME_UNIT_FOREVER_REL);
526 if (NULL != h->client)
528 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
531 finish (h, GNUNET_SYSERR);
536 * Transmit a GET request (and if successful, start to receive
540 transmit_get (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
542 struct GNUNET_MessageHeader *hdr;
549 /* timeout / error */
551 LOG (GNUNET_ERROR_TYPE_DEBUG,
552 "Transmission of request for statistics failed!\n");
554 finish (handle, GNUNET_SYSERR);
557 slen1 = strlen (handle->current->subsystem) + 1;
558 slen2 = strlen (handle->current->name) + 1;
559 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
560 GNUNET_assert (msize <= size);
561 hdr = (struct GNUNET_MessageHeader *) buf;
562 hdr->size = htons (msize);
563 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_GET);
564 GNUNET_assert (slen1 + slen2 ==
565 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
566 handle->current->subsystem,
567 handle->current->name));
568 if (!handle->receiving)
571 LOG (GNUNET_ERROR_TYPE_DEBUG,
572 "Transmission of GET done, now reading response\n");
574 handle->receiving = GNUNET_YES;
575 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
576 GNUNET_TIME_absolute_get_remaining (handle->
584 * Transmit a WATCH request (and if successful, start to receive
588 transmit_watch (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
590 struct GNUNET_MessageHeader *hdr;
597 /* timeout / error */
599 LOG (GNUNET_ERROR_TYPE_DEBUG,
600 "Transmission of request for statistics failed!\n");
602 finish (handle, GNUNET_SYSERR);
606 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting watch request for `%s'\n",
607 handle->current->name);
609 slen1 = strlen (handle->current->subsystem) + 1;
610 slen2 = strlen (handle->current->name) + 1;
611 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
612 GNUNET_assert (msize <= size);
613 hdr = (struct GNUNET_MessageHeader *) buf;
614 hdr->size = htons (msize);
615 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH);
616 GNUNET_assert (slen1 + slen2 ==
617 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
618 handle->current->subsystem,
619 handle->current->name));
620 if (GNUNET_YES != handle->receiving)
622 handle->receiving = GNUNET_YES;
623 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
624 GNUNET_TIME_UNIT_FOREVER_REL);
626 finish (handle, GNUNET_OK);
632 * Transmit a SET/UPDATE request.
635 transmit_set (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
637 struct GNUNET_STATISTICS_SetMessage *r;
644 finish (handle, GNUNET_SYSERR);
648 slen = strlen (handle->current->subsystem) + 1;
649 nlen = strlen (handle->current->name) + 1;
650 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
654 finish (handle, GNUNET_SYSERR);
658 r->header.size = htons (nsize);
659 r->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
661 r->value = GNUNET_htonll (handle->current->value);
662 if (handle->current->make_persistent)
663 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_PERSISTENT);
664 if (handle->current->type == ACTION_UPDATE)
665 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_RELATIVE);
666 GNUNET_assert (slen + nlen ==
667 GNUNET_STRINGS_buffer_fill ((char *) &r[1], slen + nlen, 2,
668 handle->current->subsystem,
669 handle->current->name));
670 finish (handle, GNUNET_OK);
676 transmit_action (void *cls, size_t size, void *buf)
678 struct GNUNET_STATISTICS_Handle *handle = cls;
682 if (NULL == handle->current)
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, GNUNET_CONSTANTS_SERVICE_TIMEOUT);
869 /* schedule next action */
870 h->current = h->action_head;
871 if (NULL == h->current)
875 h->do_destroy = GNUNET_NO;
876 GNUNET_STATISTICS_destroy (h, GNUNET_YES);
880 GNUNET_CONTAINER_DLL_remove (h->action_head, h->action_tail, h->current);
881 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
882 GNUNET_assert (NULL == h->th);
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, gh);
973 GNUNET_free (gh->name);
974 GNUNET_free (gh->subsystem);
981 * Watch statistics from the peer (be notified whenever they change).
982 * Note that the only way to cancel a "watch" request is to destroy
983 * the statistics handle given as the first argument to this call.
985 * @param handle identification of the statistics service
986 * @param subsystem limit to the specified subsystem, never NULL
987 * @param name name of the statistic value, never NULL
988 * @param proc function to call on each value
989 * @param proc_cls closure for proc
990 * @return GNUNET_OK on success, GNUNET_SYSERR on error
993 GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
994 const char *subsystem, const char *name,
995 GNUNET_STATISTICS_Iterator proc, void *proc_cls)
997 struct GNUNET_STATISTICS_WatchEntry *w;
1000 return GNUNET_SYSERR;
1001 w = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_WatchEntry));
1002 w->subsystem = GNUNET_strdup (subsystem);
1003 w->name = GNUNET_strdup (name);
1005 w->proc_cls = proc_cls;
1006 GNUNET_array_append (handle->watches, handle->watches_size, w);
1007 schedule_watch_request (handle, w);
1013 add_setter_action (struct GNUNET_STATISTICS_Handle *h, const char *name,
1014 int make_persistent, uint64_t value, enum ActionType type)
1016 struct GNUNET_STATISTICS_GetHandle *ai;
1022 GNUNET_assert (h != NULL);
1023 GNUNET_assert (name != NULL);
1024 if (GNUNET_YES != try_connect (h))
1026 slen = strlen (h->subsystem) + 1;
1027 nlen = strlen (name) + 1;
1028 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
1029 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1034 ai = h->action_head;
1037 if ((0 == strcmp (ai->subsystem, h->subsystem)) &&
1038 (0 == strcmp (ai->name, name)) && ((ai->type == ACTION_UPDATE) ||
1039 (ai->type == ACTION_SET)))
1041 if (ai->type == ACTION_SET)
1043 if (type == ACTION_UPDATE)
1045 delta = (int64_t) value;
1052 if (ai->value < -delta)
1065 if (type == ACTION_UPDATE)
1067 delta = (int64_t) value;
1076 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1077 ai->make_persistent = make_persistent;
1082 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
1084 ai->subsystem = GNUNET_strdup (h->subsystem);
1085 ai->name = GNUNET_strdup (name);
1086 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1087 ai->make_persistent = make_persistent;
1096 * Set statistic value for the peer. Will always use our
1097 * subsystem (the argument used when "handle" was created).
1099 * @param handle identification of the statistics service
1100 * @param name name of the statistic value
1101 * @param value new value to set
1102 * @param make_persistent should the value be kept across restarts?
1105 GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
1106 const char *name, uint64_t value, int make_persistent)
1110 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1111 add_setter_action (handle, name, make_persistent, value, ACTION_SET);
1116 * Set statistic value for the peer. Will always use our
1117 * subsystem (the argument used when "handle" was created).
1119 * @param handle identification of the statistics service
1120 * @param name name of the statistic value
1121 * @param delta change in value (added to existing value)
1122 * @param make_persistent should the value be kept across restarts?
1125 GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle,
1126 const char *name, int64_t delta, int make_persistent)
1132 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1133 add_setter_action (handle, name, make_persistent, (uint64_t) delta,
1138 /* end of statistics_api.c */