2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file statistics/statistics_api.c
23 * @brief API of the statistics service
24 * @author Christian Grothoff
27 #include "gnunet_client_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_server_lib.h"
32 #include "gnunet_statistics_service.h"
33 #include "gnunet_strings_lib.h"
34 #include "statistics.h"
37 * How long do we wait until a statistics request for setting
38 * a value times out? (The update will be lost if the
39 * service does not react within this timeframe).
41 #define SET_TRANSMIT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
43 #define LOG(kind,...) GNUNET_log_from (kind, "statistics-api",__VA_ARGS__)
73 * Entry kept for each value we are watching.
75 struct GNUNET_STATISTICS_WatchEntry
79 * What subsystem is this action about? (never NULL)
84 * What value is this action about? (never NULL)
91 GNUNET_STATISTICS_Iterator proc;
102 * Linked list of things we still need to do.
104 struct GNUNET_STATISTICS_GetHandle
108 * This is a doubly linked list.
110 struct GNUNET_STATISTICS_GetHandle *next;
113 * This is a doubly linked list.
115 struct GNUNET_STATISTICS_GetHandle *prev;
118 * Main statistics handle.
120 struct GNUNET_STATISTICS_Handle *sh;
123 * What subsystem is this action about? (can be NULL)
128 * What value is this action about? (can be NULL)
133 * Continuation to call once action is complete.
135 GNUNET_STATISTICS_Callback cont;
138 * Function to call (for GET actions only).
140 GNUNET_STATISTICS_Iterator proc;
143 * Closure for proc and cont.
148 * Timeout for this action.
150 struct GNUNET_TIME_Absolute timeout;
158 * Flag for SET/UPDATE actions.
163 * Has the current iteration been aborted; for GET actions.
168 * Is this a GET, SET, UPDATE or WATCH?
170 enum ActionType type;
173 * Size of the message that we will be transmitting.
181 * Handle for the service.
183 struct GNUNET_STATISTICS_Handle
186 * Name of our subsystem.
191 * Configuration to use.
193 const struct GNUNET_CONFIGURATION_Handle *cfg;
196 * Socket (if available).
198 struct GNUNET_CLIENT_Connection *client;
201 * Currently pending transmission request.
203 struct GNUNET_CLIENT_TransmitHandle *th;
206 * Head of the linked list of pending actions (first action
209 struct GNUNET_STATISTICS_GetHandle *action_head;
212 * Tail of the linked list of actions (for fast append).
214 struct GNUNET_STATISTICS_GetHandle *action_tail;
217 * Action we are currently busy with (action request has been
218 * transmitted, we're now receiving the response from the
221 struct GNUNET_STATISTICS_GetHandle *current;
224 * Array of watch entries.
226 struct GNUNET_STATISTICS_WatchEntry **watches;
229 * Task doing exponential back-off trying to reconnect.
231 GNUNET_SCHEDULER_TaskIdentifier backoff_task;
234 * Time for next connect retry.
236 struct GNUNET_TIME_Relative backoff;
239 * Size of the 'watches' array.
241 unsigned int watches_size;
244 * Should this handle auto-destruct once all actions have
250 * Are we currently receiving from the service?
258 * Schedule the next action to be performed.
260 * @param h statistics handle to reconnect
263 schedule_action (struct GNUNET_STATISTICS_Handle *h);
267 * Transmit request to service that we want to watch
268 * the development of a particular value.
270 * @param h statistics handle
271 * @param watch watch entry of the value to watch
274 schedule_watch_request (struct GNUNET_STATISTICS_Handle *h,
275 struct GNUNET_STATISTICS_WatchEntry *watch)
278 struct GNUNET_STATISTICS_GetHandle *ai;
283 GNUNET_assert (h != NULL);
284 GNUNET_assert (watch != NULL);
286 slen = strlen (watch->subsystem) + 1;
287 nlen = strlen (watch->name) + 1;
288 nsize = sizeof (struct GNUNET_MessageHeader) + slen + nlen;
289 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
294 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
296 ai->subsystem = GNUNET_strdup (watch->subsystem);
297 ai->name = GNUNET_strdup (watch->name);
298 ai->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
300 ai->type = ACTION_WATCH;
301 ai->proc = watch->proc;
302 ai->cls = watch->proc_cls;
303 GNUNET_CONTAINER_DLL_insert_tail (h->action_head, h->action_tail,
310 * Free memory associated with the given action item.
312 * @param gh action item to free
315 free_action_item (struct GNUNET_STATISTICS_GetHandle *gh)
317 GNUNET_free_non_null (gh->subsystem);
318 GNUNET_free_non_null (gh->name);
324 * Disconnect from the statistics service.
326 * @param h statistics handle to disconnect from
329 do_disconnect (struct GNUNET_STATISTICS_Handle *h)
331 struct GNUNET_STATISTICS_GetHandle *c;
335 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
338 if (NULL != h->client)
340 GNUNET_CLIENT_disconnect (h->client);
343 h->receiving = GNUNET_NO;
344 if (NULL != (c = h->current))
348 c->cont (c->cls, GNUNET_SYSERR);
349 free_action_item (c);
355 * Try to (re)connect to the statistics service.
357 * @param h statistics handle to reconnect
358 * @return GNUNET_YES on success, GNUNET_NO on failure.
361 try_connect (struct GNUNET_STATISTICS_Handle *h)
363 struct GNUNET_STATISTICS_GetHandle *gh;
364 struct GNUNET_STATISTICS_GetHandle *gn;
367 if (h->backoff_task != GNUNET_SCHEDULER_NO_TASK)
369 if (h->client != NULL)
371 h->client = GNUNET_CLIENT_connect ("statistics", h->cfg);
372 if (h->client != NULL)
375 while (NULL != (gh = gn))
378 if (gh->type == ACTION_WATCH)
380 GNUNET_CONTAINER_DLL_remove (h->action_head,
383 free_action_item (gh);
386 for (i = 0; i < h->watches_size; i++)
388 if (NULL != h->watches[i])
389 schedule_watch_request (h, h->watches[i]);
393 LOG (GNUNET_ERROR_TYPE_DEBUG,
394 "Failed to connect to statistics service!\n");
400 * We've waited long enough, reconnect now.
402 * @param cls the 'struct GNUNET_STATISTICS_Handle' to reconnect
403 * @param tc scheduler context (unused)
406 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
408 struct GNUNET_STATISTICS_Handle *h = cls;
410 h->backoff_task = GNUNET_SCHEDULER_NO_TASK;
417 * Task used by 'reconnect_later' to shutdown the handle
419 * @param cls the statistics handle
420 * @param tc scheduler context
423 do_destroy (void *cls,
424 const struct GNUNET_SCHEDULER_TaskContext *tc)
426 struct GNUNET_STATISTICS_Handle *h = cls;
428 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
432 * Reconnect at a later time, respecting back-off.
434 * @param h statistics handle
437 reconnect_later (struct GNUNET_STATISTICS_Handle *h)
440 struct GNUNET_STATISTICS_GetHandle *gh;
442 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->backoff_task);
445 /* So we are shutting down and the service is not reachable.
446 * Chances are that it's down for good and we are not going to connect to
448 * Give up and don't sync the rest of the data.
451 for (gh = h->action_head; NULL != gh; gh = gh->next)
452 if ( (gh->make_persistent) && (gh->type == ACTION_SET) )
454 if (GNUNET_YES == loss)
455 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
456 _("Could not save some persistent statistics\n"));
457 h->do_destroy = GNUNET_NO;
458 GNUNET_SCHEDULER_add_continuation (&do_destroy, h,
459 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
463 GNUNET_SCHEDULER_add_delayed (h->backoff, &reconnect_task, h);
464 h->backoff = GNUNET_TIME_relative_multiply (h->backoff, 2);
466 GNUNET_TIME_relative_min (h->backoff, GNUNET_CONSTANTS_SERVICE_TIMEOUT);
471 * Process a 'GNUNET_MESSAGE_TYPE_STATISTICS_VALUE' message.
473 * @param h statistics handle
474 * @param msg message received from the service, never NULL
475 * @return GNUNET_OK if the message was well-formed
478 process_statistics_value_message (struct GNUNET_STATISTICS_Handle *h,
479 const struct GNUNET_MessageHeader *msg)
483 const struct GNUNET_STATISTICS_ReplyMessage *smsg;
486 if (h->current->aborted)
488 LOG (GNUNET_ERROR_TYPE_DEBUG, "Iteration was aborted, ignoring VALUE\n");
489 return GNUNET_OK; /* don't bother */
491 size = ntohs (msg->size);
492 if (size < sizeof (struct GNUNET_STATISTICS_ReplyMessage))
495 return GNUNET_SYSERR;
497 smsg = (const struct GNUNET_STATISTICS_ReplyMessage *) msg;
498 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
500 GNUNET_STRINGS_buffer_tokenize ((const char *) &smsg[1], size, 2,
504 return GNUNET_SYSERR;
506 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received valid statistic on `%s:%s': %llu\n",
507 service, name, GNUNET_ntohll (smsg->value));
509 h->current->proc (h->current->cls, service, name,
510 GNUNET_ntohll (smsg->value),
512 (ntohl (smsg->uid) & GNUNET_STATISTICS_PERSIST_BIT)))
514 LOG (GNUNET_ERROR_TYPE_DEBUG,
515 "Processing of remaining statistics aborted by client.\n");
516 h->current->aborted = GNUNET_YES;
518 LOG (GNUNET_ERROR_TYPE_DEBUG, "VALUE processed successfully\n");
524 * We have received a watch value from the service. Process it.
526 * @param h statistics handle
527 * @param msg the watch value message
528 * @return GNUNET_OK if the message was well-formed, GNUNET_SYSERR if not,
529 * GNUNET_NO if this watch has been cancelled
532 process_watch_value (struct GNUNET_STATISTICS_Handle *h,
533 const struct GNUNET_MessageHeader *msg)
535 const struct GNUNET_STATISTICS_WatchValueMessage *wvm;
536 struct GNUNET_STATISTICS_WatchEntry *w;
539 if (sizeof (struct GNUNET_STATISTICS_WatchValueMessage) != ntohs (msg->size))
542 return GNUNET_SYSERR;
544 wvm = (const struct GNUNET_STATISTICS_WatchValueMessage *) msg;
545 GNUNET_break (0 == ntohl (wvm->reserved));
546 wid = ntohl (wvm->wid);
547 if (wid >= h->watches_size)
550 return GNUNET_SYSERR;
555 (void) w->proc (w->proc_cls, w->subsystem, w->name,
556 GNUNET_ntohll (wvm->value),
557 0 != (ntohl (wvm->flags) & GNUNET_STATISTICS_PERSIST_BIT));
563 destroy_task (void *cls,
564 const struct GNUNET_SCHEDULER_TaskContext *tc)
566 struct GNUNET_STATISTICS_Handle *h = cls;
568 GNUNET_STATISTICS_destroy (h, GNUNET_YES);
573 * Function called with messages from stats service.
576 * @param msg message received, NULL on timeout or fatal error
579 receive_stats (void *cls, const struct GNUNET_MessageHeader *msg)
581 struct GNUNET_STATISTICS_Handle *h = cls;
582 struct GNUNET_STATISTICS_GetHandle *c;
587 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
588 "Error receiving statistics from service, is the service running?\n");
593 switch (ntohs (msg->type))
595 case GNUNET_MESSAGE_TYPE_TEST:
596 if (GNUNET_SYSERR != h->do_destroy)
598 /* not in shutdown, why do we get 'TEST'? */
604 h->do_destroy = GNUNET_NO;
605 GNUNET_SCHEDULER_add_continuation (&destroy_task, h,
606 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
608 case GNUNET_MESSAGE_TYPE_STATISTICS_END:
609 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received end of statistics marker\n");
610 if (NULL == (c = h->current))
617 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
618 if (h->watches_size > 0)
620 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
621 GNUNET_TIME_UNIT_FOREVER_REL);
625 h->receiving = GNUNET_NO;
630 c->cont (c->cls, GNUNET_OK);
631 free_action_item (c);
633 case GNUNET_MESSAGE_TYPE_STATISTICS_VALUE:
634 if (GNUNET_OK != process_statistics_value_message (h, msg))
640 /* finally, look for more! */
641 LOG (GNUNET_ERROR_TYPE_DEBUG,
642 "Processing VALUE done, now reading more\n");
643 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
644 GNUNET_TIME_absolute_get_remaining (h->
646 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
648 case GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE:
650 (ret = process_watch_value (h, msg)))
653 if (GNUNET_NO == ret)
654 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
658 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
659 GNUNET_assert (h->watches_size > 0);
660 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
661 GNUNET_TIME_UNIT_FOREVER_REL);
673 * Transmit a GET 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_get (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
684 struct GNUNET_STATISTICS_GetHandle *c;
685 struct GNUNET_MessageHeader *hdr;
690 GNUNET_assert (NULL != (c = handle->current));
693 /* timeout / error */
694 LOG (GNUNET_ERROR_TYPE_DEBUG,
695 "Transmission of request for statistics failed!\n");
696 do_disconnect (handle);
697 reconnect_later (handle);
700 slen1 = strlen (c->subsystem) + 1;
701 slen2 = strlen (c->name) + 1;
702 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
703 GNUNET_assert (msize <= size);
704 hdr = (struct GNUNET_MessageHeader *) buf;
705 hdr->size = htons (msize);
706 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_GET);
707 GNUNET_assert (slen1 + slen2 ==
708 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
711 if (GNUNET_YES != handle->receiving)
713 LOG (GNUNET_ERROR_TYPE_DEBUG,
714 "Transmission of GET done, now reading response\n");
715 handle->receiving = GNUNET_YES;
716 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
717 GNUNET_TIME_absolute_get_remaining (c->timeout));
724 * Transmit a WATCH request (and if successful, start to receive
727 * @param handle statistics handle
728 * @param size how many bytes can we write to buf
729 * @param buf where to write requests to the service
730 * @return number of bytes written to buf
733 transmit_watch (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
735 struct GNUNET_MessageHeader *hdr;
742 /* timeout / error */
743 LOG (GNUNET_ERROR_TYPE_DEBUG,
744 "Transmission of request for statistics failed!\n");
745 do_disconnect (handle);
746 reconnect_later (handle);
749 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting watch request for `%s'\n",
750 handle->current->name);
751 slen1 = strlen (handle->current->subsystem) + 1;
752 slen2 = strlen (handle->current->name) + 1;
753 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
754 GNUNET_assert (msize <= size);
755 hdr = (struct GNUNET_MessageHeader *) buf;
756 hdr->size = htons (msize);
757 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH);
758 GNUNET_assert (slen1 + slen2 ==
759 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
760 handle->current->subsystem,
761 handle->current->name));
762 if (GNUNET_YES != handle->receiving)
764 handle->receiving = GNUNET_YES;
765 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
766 GNUNET_TIME_UNIT_FOREVER_REL);
768 GNUNET_assert (NULL == handle->current->cont);
769 free_action_item (handle->current);
770 handle->current = NULL;
776 * Transmit a SET/UPDATE request.
778 * @param handle statistics handle
779 * @param size how many bytes can we write to buf
780 * @param buf where to write requests to the service
781 * @return number of bytes written to buf
784 transmit_set (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
786 struct GNUNET_STATISTICS_SetMessage *r;
793 do_disconnect (handle);
794 reconnect_later (handle);
797 slen = strlen (handle->current->subsystem) + 1;
798 nlen = strlen (handle->current->name) + 1;
799 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
803 do_disconnect (handle);
804 reconnect_later (handle);
808 r->header.size = htons (nsize);
809 r->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
811 r->value = GNUNET_htonll (handle->current->value);
812 if (handle->current->make_persistent)
813 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_PERSISTENT);
814 if (handle->current->type == ACTION_UPDATE)
815 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_RELATIVE);
816 GNUNET_assert (slen + nlen ==
817 GNUNET_STRINGS_buffer_fill ((char *) &r[1], slen + nlen, 2,
818 handle->current->subsystem,
819 handle->current->name));
820 GNUNET_assert (NULL == handle->current->cont);
821 free_action_item (handle->current);
822 handle->current = NULL;
828 * Function called when we are ready to transmit a request to the service.
830 * @param cls the 'struct GNUNET_STATISTICS_Handle'
831 * @param size how many bytes can we write to buf
832 * @param buf where to write requests to the service
833 * @return number of bytes written to buf
836 transmit_action (void *cls, size_t size, void *buf)
838 struct GNUNET_STATISTICS_Handle *h = cls;
843 if (NULL != h->current)
844 switch (h->current->type)
847 ret = transmit_get (h, size, buf);
851 ret = transmit_set (h, size, buf);
854 ret = transmit_watch (h, size, buf);
866 * Get handle for the statistics service.
868 * @param subsystem name of subsystem using the service
869 * @param cfg services configuration in use
870 * @return handle to use
872 struct GNUNET_STATISTICS_Handle *
873 GNUNET_STATISTICS_create (const char *subsystem,
874 const struct GNUNET_CONFIGURATION_Handle *cfg)
876 struct GNUNET_STATISTICS_Handle *ret;
878 GNUNET_assert (subsystem != NULL);
879 GNUNET_assert (cfg != NULL);
880 ret = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_Handle));
882 ret->subsystem = GNUNET_strdup (subsystem);
883 ret->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
889 * Destroy a handle (free all state associated with
892 * @param h statistics handle to destroy
893 * @param sync_first set to GNUNET_YES if pending SET requests should
897 GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h, int sync_first)
899 struct GNUNET_STATISTICS_GetHandle *pos;
900 struct GNUNET_STATISTICS_GetHandle *next;
901 struct GNUNET_TIME_Relative timeout;
906 GNUNET_assert (GNUNET_NO == h->do_destroy); // Don't call twice.
907 if (GNUNET_SCHEDULER_NO_TASK != h->backoff_task)
909 GNUNET_SCHEDULER_cancel (h->backoff_task);
910 h->backoff_task = GNUNET_SCHEDULER_NO_TASK;
914 if (h->current != NULL)
916 if (h->current->type == ACTION_GET)
918 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
920 free_action_item (h->current);
924 next = h->action_head;
925 while (NULL != (pos = next))
928 if (pos->type == ACTION_GET)
930 GNUNET_CONTAINER_DLL_remove (h->action_head,
933 free_action_item (pos);
936 if ( (NULL == h->current) &&
937 (NULL != (h->current = h->action_head)) )
938 GNUNET_CONTAINER_DLL_remove (h->action_head,
941 h->do_destroy = GNUNET_YES;
942 if ((h->current != NULL) && (h->th == NULL) &&
945 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
947 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
949 &transmit_action, h);
950 GNUNET_assert (NULL != h->th);
953 return; /* do not finish destruction just yet */
955 while (NULL != (pos = h->action_head))
957 GNUNET_CONTAINER_DLL_remove (h->action_head,
960 free_action_item (pos);
963 for (i = 0; i < h->watches_size; i++)
965 if (NULL == h->watches[i])
967 GNUNET_free (h->watches[i]->subsystem);
968 GNUNET_free (h->watches[i]->name);
969 GNUNET_free (h->watches[i]);
971 GNUNET_array_grow (h->watches, h->watches_size, 0);
972 GNUNET_free (h->subsystem);
978 * Function called to transmit TEST message to service to
979 * confirm that the service has received all of our 'SET'
980 * messages (during statistics disconnect/shutdown).
982 * @param cls the 'struct GNUNET_STATISTICS_Handle'
983 * @param size how many bytes can we write to buf
984 * @param buf where to write requests to the service
985 * @return number of bytes written to buf
988 transmit_test_on_shutdown (void *cls,
992 struct GNUNET_STATISTICS_Handle *h = cls;
993 struct GNUNET_MessageHeader hdr;
997 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
998 _("Failed to receive acknowledgement from statistics service, some statistics might have been lost!\n"));
999 h->do_destroy = GNUNET_NO;
1000 GNUNET_SCHEDULER_add_continuation (&destroy_task, h,
1001 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1004 hdr.type = htons (GNUNET_MESSAGE_TYPE_TEST);
1005 hdr.size = htons (sizeof (struct GNUNET_MessageHeader));
1006 memcpy (buf, &hdr, sizeof (hdr));
1007 return sizeof (struct GNUNET_MessageHeader);
1012 * Schedule the next action to be performed.
1014 * @param h statistics handle
1017 schedule_action (struct GNUNET_STATISTICS_Handle *h)
1019 struct GNUNET_TIME_Relative timeout;
1021 if ( (h->th != NULL) ||
1022 (h->backoff_task != GNUNET_SCHEDULER_NO_TASK) )
1023 return; /* action already pending */
1024 if (GNUNET_YES != try_connect (h))
1026 reconnect_later (h);
1029 if (NULL != h->current)
1030 return; /* action already pending */
1031 /* schedule next action */
1032 h->current = h->action_head;
1033 if (NULL == h->current)
1037 h->do_destroy = GNUNET_SYSERR; /* in 'TEST' mode */
1038 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
1039 sizeof (struct GNUNET_MessageHeader),
1040 SET_TRANSMIT_TIMEOUT,
1042 &transmit_test_on_shutdown, h);
1046 GNUNET_CONTAINER_DLL_remove (h->action_head, h->action_tail, h->current);
1047 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
1050 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
1051 timeout, GNUNET_YES,
1052 &transmit_action, h)))
1054 LOG (GNUNET_ERROR_TYPE_DEBUG,
1055 "Failed to transmit request to statistics service.\n");
1057 reconnect_later (h);
1063 * Get statistic from the peer.
1065 * @param handle identification of the statistics service
1066 * @param subsystem limit to the specified subsystem, NULL for our subsystem
1067 * @param name name of the statistic value, NULL for all values
1068 * @param timeout after how long should we give up (and call
1069 * cont with an error code)?
1070 * @param cont continuation to call when done (can be NULL)
1071 * @param proc function to call on each value
1072 * @param cls closure for cont and proc
1073 * @return NULL on error
1075 struct GNUNET_STATISTICS_GetHandle *
1076 GNUNET_STATISTICS_get (struct GNUNET_STATISTICS_Handle *handle,
1077 const char *subsystem, const char *name,
1078 struct GNUNET_TIME_Relative timeout,
1079 GNUNET_STATISTICS_Callback cont,
1080 GNUNET_STATISTICS_Iterator proc, void *cls)
1084 struct GNUNET_STATISTICS_GetHandle *ai;
1088 GNUNET_assert (proc != NULL);
1089 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1090 if (subsystem == NULL)
1094 slen1 = strlen (subsystem) + 1;
1095 slen2 = strlen (name) + 1;
1096 GNUNET_assert (slen1 + slen2 + sizeof (struct GNUNET_MessageHeader) <
1097 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1098 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
1100 ai->subsystem = GNUNET_strdup (subsystem);
1101 ai->name = GNUNET_strdup (name);
1105 ai->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1106 ai->type = ACTION_GET;
1107 ai->msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
1108 GNUNET_CONTAINER_DLL_insert_tail (handle->action_head, handle->action_tail,
1110 schedule_action (handle);
1116 * Cancel a 'get' request. Must be called before the 'cont'
1117 * function is called.
1119 * @param gh handle of the request to cancel
1122 GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh)
1126 if (gh->sh->current == gh)
1128 gh->aborted = GNUNET_YES;
1132 GNUNET_CONTAINER_DLL_remove (gh->sh->action_head, gh->sh->action_tail, gh);
1133 GNUNET_free (gh->name);
1134 GNUNET_free (gh->subsystem);
1141 * Watch statistics from the peer (be notified whenever they change).
1143 * @param handle identification of the statistics service
1144 * @param subsystem limit to the specified subsystem, never NULL
1145 * @param name name of the statistic value, never NULL
1146 * @param proc function to call on each value
1147 * @param proc_cls closure for proc
1148 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1151 GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
1152 const char *subsystem, const char *name,
1153 GNUNET_STATISTICS_Iterator proc, void *proc_cls)
1155 struct GNUNET_STATISTICS_WatchEntry *w;
1158 return GNUNET_SYSERR;
1159 w = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_WatchEntry));
1160 w->subsystem = GNUNET_strdup (subsystem);
1161 w->name = GNUNET_strdup (name);
1163 w->proc_cls = proc_cls;
1164 GNUNET_array_append (handle->watches, handle->watches_size, w);
1165 schedule_watch_request (handle, w);
1171 * Stop watching statistics from the peer.
1173 * @param handle identification of the statistics service
1174 * @param subsystem limit to the specified subsystem, never NULL
1175 * @param name name of the statistic value, never NULL
1176 * @param proc function to call on each value
1177 * @param proc_cls closure for proc
1178 * @return GNUNET_OK on success, GNUNET_SYSERR on error (no such watch)
1181 GNUNET_STATISTICS_watch_cancel (struct GNUNET_STATISTICS_Handle *handle,
1182 const char *subsystem, const char *name,
1183 GNUNET_STATISTICS_Iterator proc, void *proc_cls)
1185 struct GNUNET_STATISTICS_WatchEntry *w;
1189 return GNUNET_SYSERR;
1190 for (i=0;i<handle->watches_size;i++)
1192 w = handle->watches[i];
1195 if ( (w->proc == proc) &&
1196 (w->proc_cls == proc_cls) &&
1197 (0 == strcmp (w->name, name)) &&
1198 (0 == strcmp (w->subsystem, subsystem)) )
1200 GNUNET_free (w->name);
1201 GNUNET_free (w->subsystem);
1203 handle->watches[i] = NULL;
1207 return GNUNET_SYSERR;
1213 * Queue a request to change a statistic.
1215 * @param h statistics handle
1216 * @param name name of the value
1217 * @param make_persistent should the value be kept across restarts?
1218 * @param value new value or change
1219 * @param type type of the action (ACTION_SET or ACTION_UPDATE)
1222 add_setter_action (struct GNUNET_STATISTICS_Handle *h, const char *name,
1223 int make_persistent, uint64_t value, enum ActionType type)
1225 struct GNUNET_STATISTICS_GetHandle *ai;
1231 GNUNET_assert (h != NULL);
1232 GNUNET_assert (name != NULL);
1233 slen = strlen (h->subsystem) + 1;
1234 nlen = strlen (name) + 1;
1235 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
1236 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1241 for (ai = h->action_head; ai != NULL; ai = ai->next)
1243 if (! ( (0 == strcmp (ai->subsystem, h->subsystem)) &&
1244 (0 == strcmp (ai->name, name)) &&
1245 ( (ai->type == ACTION_UPDATE) ||
1246 (ai->type == ACTION_SET) ) ) )
1248 if (ai->type == ACTION_SET)
1250 if (type == ACTION_UPDATE)
1252 delta = (int64_t) value;
1255 /* update old set by new delta */
1260 /* update old set by new delta, but never go negative */
1261 if (ai->value < -delta)
1269 /* new set overrides old set */
1275 if (type == ACTION_UPDATE)
1277 /* make delta cummulative */
1278 delta = (int64_t) value;
1283 /* drop old 'update', use new 'set' instead */
1288 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1289 ai->make_persistent = make_persistent;
1292 /* no existing entry matches, create a fresh one */
1293 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
1295 ai->subsystem = GNUNET_strdup (h->subsystem);
1296 ai->name = GNUNET_strdup (name);
1297 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1298 ai->make_persistent = make_persistent;
1302 GNUNET_CONTAINER_DLL_insert_tail (h->action_head, h->action_tail,
1304 schedule_action (h);
1309 * Set statistic value for the peer. Will always use our
1310 * subsystem (the argument used when "handle" was created).
1312 * @param handle identification of the statistics service
1313 * @param name name of the statistic value
1314 * @param value new value to set
1315 * @param make_persistent should the value be kept across restarts?
1318 GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
1319 const char *name, uint64_t value, int make_persistent)
1323 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1324 add_setter_action (handle, name, make_persistent, value, ACTION_SET);
1329 * Set statistic value for the peer. Will always use our
1330 * subsystem (the argument used when "handle" was created).
1332 * @param handle identification of the statistics service
1333 * @param name name of the statistic value
1334 * @param delta change in value (added to existing value)
1335 * @param make_persistent should the value be kept across restarts?
1338 GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle,
1339 const char *name, int64_t delta, int make_persistent)
1345 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1346 add_setter_action (handle, name, make_persistent, (uint64_t) delta,
1351 /* end of statistics_api.c */