2 This file is part of GNUnet.
3 (C) 2009 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_container_lib.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_server_lib.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet_strings_lib.h"
33 #include "statistics.h"
36 * How long do we wait until a statistics request for setting
37 * a value times out? (The update will be lost if the
38 * service does not react within this timeframe).
40 #define SET_TRANSMIT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
55 * Linked list of things we still need to do.
57 struct GNUNET_STATISTICS_GetHandle
61 * This is a doubly linked list.
63 struct GNUNET_STATISTICS_GetHandle *next;
66 * This is a doubly linked list.
68 struct GNUNET_STATISTICS_GetHandle *prev;
71 * Main statistics handle.
73 struct GNUNET_STATISTICS_Handle *sh;
76 * What subsystem is this action about? (can be NULL)
81 * What value is this action about? (can be NULL)
86 * Continuation to call once action is complete.
88 GNUNET_STATISTICS_Callback cont;
91 * Function to call (for GET actions only).
93 GNUNET_STATISTICS_Iterator proc;
96 * Closure for proc and cont.
101 * Timeout for this action.
103 struct GNUNET_TIME_Absolute timeout;
111 * Flag for SET/UPDATE actions.
116 * Has the current iteration been aborted; for GET actions.
121 * Is this a GET, SET or UPDATE?
123 enum ActionType type;
126 * Size of the message that we will be transmitting.
134 * Handle for the service.
136 struct GNUNET_STATISTICS_Handle
141 struct GNUNET_SCHEDULER_Handle *sched;
144 * Name of our subsystem.
149 * Configuration to use.
151 const struct GNUNET_CONFIGURATION_Handle *cfg;
154 * Socket (if available).
156 struct GNUNET_CLIENT_Connection *client;
159 * Currently pending transmission request.
161 struct GNUNET_CLIENT_TransmitHandle *th;
164 * Head of the linked list of pending actions (first action
167 struct GNUNET_STATISTICS_GetHandle *action_head;
170 * Tail of the linked list of actions (for fast append).
172 struct GNUNET_STATISTICS_GetHandle *action_tail;
175 * Action we are currently busy with (action request has been
176 * transmitted, we're now receiving the response from the
179 struct GNUNET_STATISTICS_GetHandle *current;
182 * Should this handle auto-destruct once all actions have
191 * Try to (re)connect to the statistics service.
193 * @return GNUNET_YES on success, GNUNET_NO on failure.
196 try_connect (struct GNUNET_STATISTICS_Handle *ret)
198 if (ret->client != NULL)
200 ret->client = GNUNET_CLIENT_connect (ret->sched, "statistics", ret->cfg);
201 if (ret->client != NULL)
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205 _("Failed to connect to statistics service!\n"));
212 * Free memory associated with the given action item.
215 free_action_item (struct GNUNET_STATISTICS_GetHandle *ai)
217 GNUNET_free_non_null (ai->subsystem);
218 GNUNET_free_non_null (ai->name);
224 * Schedule the next action to be performed.
226 static void schedule_action (struct GNUNET_STATISTICS_Handle *h);
230 * GET processing is complete, tell client about it.
233 finish (struct GNUNET_STATISTICS_Handle *h, int code)
235 struct GNUNET_STATISTICS_GetHandle *pos = h->current;
238 if (pos->cont != NULL)
239 pos->cont (pos->cls, code);
240 free_action_item (pos);
245 * Process the message.
247 * @return GNUNET_OK if the message was well-formed
250 process_message (struct GNUNET_STATISTICS_Handle *h,
251 const struct GNUNET_MessageHeader *msg)
255 const struct GNUNET_STATISTICS_ReplyMessage *smsg;
258 if (h->current->aborted)
259 return GNUNET_OK; /* don't bother */
260 size = ntohs (msg->size);
261 if (size < sizeof (struct GNUNET_STATISTICS_ReplyMessage))
264 return GNUNET_SYSERR;
266 smsg = (const struct GNUNET_STATISTICS_ReplyMessage *) msg;
267 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
268 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &smsg[1],
269 size, 2, &service, &name))
272 return GNUNET_SYSERR;
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
276 "Received valid statistic on `%s:%s': %llu\n",
277 service, name, GNUNET_ntohll (smsg->value));
280 h->current->proc (h->current->cls,
283 GNUNET_ntohll (smsg->value),
285 (ntohl (smsg->uid) & GNUNET_STATISTICS_PERSIST_BIT)))
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289 "Processing of remaining statistics aborted by client.\n");
291 h->current->aborted = GNUNET_YES;
298 * Function called with messages from stats service.
301 * @param msg message received, NULL on timeout or fatal error
304 receive_stats (void *cls, const struct GNUNET_MessageHeader *msg)
306 struct GNUNET_STATISTICS_Handle *h = cls;
310 if (NULL != h->client)
312 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
317 "Error receiving statistics from service, is the service running?\n" );
319 finish (h, GNUNET_SYSERR);
322 switch (ntohs (msg->type))
324 case GNUNET_MESSAGE_TYPE_STATISTICS_END:
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327 "Received end of statistics marker\n");
329 finish (h, GNUNET_OK);
331 case GNUNET_MESSAGE_TYPE_STATISTICS_VALUE:
332 if (GNUNET_OK == process_message (h, msg))
334 /* finally, look for more! */
335 GNUNET_CLIENT_receive (h->client,
338 GNUNET_TIME_absolute_get_remaining
339 (h->current->timeout));
348 if (NULL != h->client)
350 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
353 finish (h, GNUNET_SYSERR);
358 * Transmit a GET request (and if successful, start to receive
362 transmit_get (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
364 struct GNUNET_MessageHeader *hdr;
371 /* timeout / error */
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
374 "Transmission of request for statistics failed!\n");
376 finish (handle, GNUNET_SYSERR);
379 slen1 = strlen (handle->current->subsystem) + 1;
380 slen2 = strlen (handle->current->name) + 1;
381 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
382 GNUNET_assert (msize <= size);
383 hdr = (struct GNUNET_MessageHeader *) buf;
384 hdr->size = htons (msize);
385 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_GET);
386 GNUNET_assert (slen1 + slen2 ==
387 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1],
390 handle->current->subsystem,
391 handle->current->name));
392 GNUNET_CLIENT_receive (handle->client,
395 GNUNET_TIME_absolute_get_remaining (handle->
403 * Transmit a SET/UPDATE request.
406 transmit_set (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
408 struct GNUNET_STATISTICS_SetMessage *r;
415 finish (handle, GNUNET_SYSERR);
419 slen = strlen (handle->current->subsystem) + 1;
420 nlen = strlen (handle->current->name) + 1;
421 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
425 finish (handle, GNUNET_SYSERR);
429 r->header.size = htons (nsize);
430 r->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
432 r->value = GNUNET_htonll (handle->current->value);
433 if (handle->current->make_persistent)
434 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_PERSISTENT);
435 if (handle->current->type == ACTION_UPDATE)
436 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_RELATIVE);
437 GNUNET_assert (slen + nlen ==
438 GNUNET_STRINGS_buffer_fill ((char *) &r[1],
441 handle->current->subsystem,
442 handle->current->name));
443 finish (handle, GNUNET_OK);
449 transmit_action (void *cls, size_t size, void *buf)
451 struct GNUNET_STATISTICS_Handle *handle = cls;
455 switch (handle->current->type)
458 ret = transmit_get (handle, size, buf);
462 ret = transmit_set (handle, size, buf);
474 * Get handle for the statistics service.
476 * @param sched scheduler to use
477 * @param subsystem name of subsystem using the service
478 * @param cfg services configuration in use
479 * @return handle to use
481 struct GNUNET_STATISTICS_Handle *
482 GNUNET_STATISTICS_create (struct GNUNET_SCHEDULER_Handle *sched,
483 const char *subsystem,
484 const struct GNUNET_CONFIGURATION_Handle *cfg)
486 struct GNUNET_STATISTICS_Handle *ret;
488 GNUNET_assert (subsystem != NULL);
489 GNUNET_assert (sched != NULL);
490 GNUNET_assert (cfg != NULL);
491 ret = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_Handle));
494 ret->subsystem = GNUNET_strdup (subsystem);
501 * Destroy a handle (free all state associated with
504 * @param h statistics handle to destroy
505 * @param sync_first set to GNUNET_YES if pending SET requests should
509 GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h,
512 struct GNUNET_STATISTICS_GetHandle *pos;
513 struct GNUNET_STATISTICS_GetHandle *next;
514 struct GNUNET_STATISTICS_GetHandle *prev;
515 struct GNUNET_TIME_Relative timeout;
519 if (h->current != NULL)
521 if (h->current->type == ACTION_GET)
523 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
525 free_action_item (h->current);
529 pos = h->action_head;
534 if (pos->type == ACTION_GET)
537 h->action_head = next;
540 free_action_item (pos);
548 h->action_tail = prev;
549 if (h->current == NULL)
551 h->current = h->action_head;
552 if (h->action_head != NULL)
554 h->action_head = h->action_head->next;
555 if (h->action_head == NULL)
556 h->action_tail = NULL;
559 if ( (h->current != NULL) &&
562 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
563 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
567 &transmit_action, h);
568 GNUNET_assert (NULL != h->th);
570 h->do_destroy = GNUNET_YES;
575 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
578 if (h->current != NULL)
579 free_action_item (h->current);
580 while (NULL != (pos = h->action_head))
582 h->action_head = pos->next;
583 free_action_item (pos);
585 if (h->client != NULL)
587 GNUNET_CLIENT_disconnect (h->client, GNUNET_YES);
590 GNUNET_free (h->subsystem);
597 * Schedule the next action to be performed.
600 schedule_action (struct GNUNET_STATISTICS_Handle *h)
602 struct GNUNET_TIME_Relative timeout;
604 if (h->current != NULL)
605 return; /* action already pending */
606 if (GNUNET_YES != try_connect (h))
608 finish (h, GNUNET_SYSERR);
612 /* schedule next action */
613 h->current = h->action_head;
614 if (NULL == h->current)
618 h->do_destroy = GNUNET_NO;
619 GNUNET_STATISTICS_destroy (h, GNUNET_YES);
623 GNUNET_CONTAINER_DLL_remove (h->action_head,
626 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
628 (h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
632 &transmit_action, h)))
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 "Failed to transmit request to statistics service.\n");
638 finish (h, GNUNET_SYSERR);
644 insert_ai (struct GNUNET_STATISTICS_Handle *h, struct GNUNET_STATISTICS_GetHandle *ai)
646 GNUNET_CONTAINER_DLL_insert_after (h->action_head,
650 if (h->action_head == ai)
656 * Get statistic from the peer.
658 * @param handle identification of the statistics service
659 * @param subsystem limit to the specified subsystem, NULL for our subsystem
660 * @param name name of the statistic value, NULL for all values
661 * @param timeout after how long should we give up (and call
662 * cont with an error code)?
663 * @param cont continuation to call when done (can be NULL)
664 * @param proc function to call on each value
665 * @param cls closure for cont and proc
666 * @return NULL on error
668 struct GNUNET_STATISTICS_GetHandle *
669 GNUNET_STATISTICS_get (struct GNUNET_STATISTICS_Handle *handle,
670 const char *subsystem,
672 struct GNUNET_TIME_Relative timeout,
673 GNUNET_STATISTICS_Callback cont,
674 GNUNET_STATISTICS_Iterator proc, void *cls)
678 struct GNUNET_STATISTICS_GetHandle *ai;
680 GNUNET_assert (handle != NULL);
681 GNUNET_assert (proc != NULL);
682 GNUNET_assert (GNUNET_NO == handle->do_destroy);
683 if (GNUNET_YES != try_connect (handle))
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
687 "Failed to connect to statistics service, can not get value `%s:%s'.\n",
688 strlen (subsystem) ? subsystem : "*",
689 strlen (name) ? name : "*");
693 if (subsystem == NULL)
697 slen1 = strlen (subsystem);
698 slen2 = strlen (name);
699 GNUNET_assert (slen1 + slen2 + sizeof (struct GNUNET_MessageHeader) <
700 GNUNET_SERVER_MAX_MESSAGE_SIZE);
701 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
703 ai->subsystem = GNUNET_strdup (subsystem);
704 ai->name = GNUNET_strdup (name);
708 ai->timeout = GNUNET_TIME_relative_to_absolute (timeout);
709 ai->type = ACTION_GET;
710 ai->msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
711 insert_ai (handle, ai);
717 * Cancel a 'get' request. Must be called before the 'cont'
718 * function is called.
720 * @param gh handle of the request to cancel
723 GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh)
725 if (gh->sh->current == gh)
727 gh->aborted = GNUNET_YES;
731 GNUNET_CONTAINER_DLL_remove (gh->sh->action_head,
734 GNUNET_free (gh->name);
735 GNUNET_free (gh->subsystem);
742 add_setter_action (struct GNUNET_STATISTICS_Handle *h,
745 uint64_t value, enum ActionType type)
747 struct GNUNET_STATISTICS_GetHandle *ai;
752 GNUNET_assert (h != NULL);
753 GNUNET_assert (name != NULL);
754 if (GNUNET_YES != try_connect (h))
756 slen = strlen (h->subsystem) + 1;
757 nlen = strlen (name) + 1;
758 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
759 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
764 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
766 ai->subsystem = GNUNET_strdup (h->subsystem);
767 ai->name = GNUNET_strdup (name);
768 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
769 ai->make_persistent = make_persistent;
779 * Set statistic value for the peer. Will always use our
780 * subsystem (the argument used when "handle" was created).
782 * @param handle identification of the statistics service
783 * @param name name of the statistic value
784 * @param value new value to set
785 * @param make_persistent should the value be kept across restarts?
788 GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
790 uint64_t value, int make_persistent)
792 GNUNET_assert (GNUNET_NO == handle->do_destroy);
793 add_setter_action (handle, name, make_persistent, value, ACTION_SET);
798 * Set statistic value for the peer. Will always use our
799 * subsystem (the argument used when "handle" was created).
801 * @param handle identification of the statistics service
802 * @param name name of the statistic value
803 * @param delta change in value (added to existing value)
804 * @param make_persistent should the value be kept across restarts?
807 GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle,
809 int64_t delta, int make_persistent)
811 GNUNET_assert (GNUNET_NO == handle->do_destroy);
812 add_setter_action (handle, name, make_persistent,
813 (unsigned long long) delta, ACTION_UPDATE);
817 /* end of statistics_api.c */