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_protocols.h"
29 #include "gnunet_server_lib.h"
30 #include "gnunet_statistics_service.h"
31 #include "gnunet_strings_lib.h"
32 #include "statistics.h"
34 #define SET_TRANSMIT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
49 * Linked list of things we still need to do.
54 * This is a linked list.
56 struct ActionItem *next;
59 * What subsystem is this action about? (can be NULL)
64 * What value is this action about? (can be NULL)
69 * Continuation to call once action is complete.
71 GNUNET_STATISTICS_Callback cont;
74 * Function to call (for GET actions only).
76 GNUNET_STATISTICS_Iterator proc;
79 * Closure for proc and cont.
84 * Timeout for this action.
86 struct GNUNET_TIME_Absolute timeout;
91 unsigned long long value;
94 * Flag for SET/UPDATE actions.
99 * Has the current iteration been aborted; for GET actions.
104 * Is this a GET, SET or UPDATE?
106 enum ActionType type;
109 * Size of the message that we will be transmitting.
117 * Handle for the service.
119 struct GNUNET_STATISTICS_Handle
124 struct GNUNET_SCHEDULER_Handle *sched;
127 * Name of our subsystem.
132 * Configuration to use.
134 struct GNUNET_CONFIGURATION_Handle *cfg;
137 * Socket (if available).
139 struct GNUNET_CLIENT_Connection *client;
142 * Head of the linked list of pending actions (first action
145 struct ActionItem *action_head;
148 * Tail of the linked list of actions (for fast append).
150 struct ActionItem *action_tail;
153 * Action we are currently busy with (action request has been
154 * transmitted, we're now receiving the response from the
157 struct ActionItem *current;
160 * Should this handle be destroyed once we've processed
169 * Try to (re)connect to the statistics service.
171 * @return GNUNET_YES on success, GNUNET_NO on failure.
174 try_connect (struct GNUNET_STATISTICS_Handle *ret)
176 if (ret->client != NULL)
178 ret->client = GNUNET_CLIENT_connect (ret->sched, "statistics", ret->cfg);
179 if (ret->client != NULL)
181 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
182 _("Failed to connect to statistics service!\n"));
188 * Free memory associated with the given action item.
191 free_action_item (struct ActionItem *ai)
193 GNUNET_free_non_null (ai->subsystem);
194 GNUNET_free_non_null (ai->name);
200 * Get handle for the statistics service.
202 * @param subsystem name of subsystem using the service
203 * @param cfg services configuration in use
204 * @return handle to use
206 struct GNUNET_STATISTICS_Handle *
207 GNUNET_STATISTICS_create (struct GNUNET_SCHEDULER_Handle *sched,
208 const char *subsystem,
209 struct GNUNET_CONFIGURATION_Handle *cfg)
211 struct GNUNET_STATISTICS_Handle *ret;
213 GNUNET_assert (subsystem != NULL);
214 GNUNET_assert (sched != NULL);
215 GNUNET_assert (cfg != NULL);
216 ret = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_Handle));
219 ret->subsystem = GNUNET_strdup (subsystem);
226 * Actually free the handle.
229 do_destroy (struct GNUNET_STATISTICS_Handle *h)
231 GNUNET_assert (h->action_head == NULL);
232 GNUNET_assert (h->current == NULL);
233 if (h->client != NULL)
235 GNUNET_CLIENT_disconnect (h->client);
238 GNUNET_free (h->subsystem);
244 * Destroy a handle (free all state associated with
248 GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *handle)
250 GNUNET_assert (handle->do_destroy == GNUNET_NO);
251 if ((handle->action_head != NULL) || (handle->current != NULL))
253 handle->do_destroy = GNUNET_YES;
261 * Process the message.
263 * @return GNUNET_OK if the message was well-formed
266 process_message (struct GNUNET_STATISTICS_Handle *h,
267 const struct GNUNET_MessageHeader *msg)
271 const struct GNUNET_STATISTICS_ReplyMessage *smsg;
274 if (h->current->aborted)
275 return GNUNET_OK; /* don't bother */
276 size = ntohs (msg->size);
277 if (size < sizeof (struct GNUNET_STATISTICS_ReplyMessage))
280 return GNUNET_SYSERR;
282 smsg = (const struct GNUNET_STATISTICS_ReplyMessage *) msg;
283 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
284 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &smsg[1],
285 size, 2, &service, &name))
288 return GNUNET_SYSERR;
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "Received valid statistic on `%s:%s': %llu\n",
293 service, name, GNUNET_ntohll (smsg->value));
296 h->current->proc (h->current->cls,
299 GNUNET_ntohll (smsg->value),
301 (ntohl (smsg->uid) & GNUNET_STATISTICS_PERSIST_BIT)))
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
305 "Processing of remaining statistics aborted by client.\n");
307 h->current->aborted = GNUNET_YES;
315 * Schedule the next action to be performed.
317 static void schedule_action (struct GNUNET_STATISTICS_Handle *h);
321 * GET processing is complete, tell client about it.
324 finish (struct GNUNET_STATISTICS_Handle *h, int code)
326 struct ActionItem *pos = h->current;
329 if (pos->cont != NULL)
330 pos->cont (pos->cls, code);
331 free_action_item (pos);
336 * Function called with messages from stats service.
339 * @param msg message received, NULL on timeout or fatal error
342 receive_stats (void *cls, const struct GNUNET_MessageHeader *msg)
344 struct GNUNET_STATISTICS_Handle *h = cls;
348 GNUNET_CLIENT_disconnect (h->client);
350 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
352 ("Error receiving statistics from service, is the service running?\n"));
353 finish (h, GNUNET_SYSERR);
356 switch (ntohs (msg->type))
358 case GNUNET_MESSAGE_TYPE_STATISTICS_END:
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361 "Received end of statistics marker\n");
363 finish (h, GNUNET_OK);
365 case GNUNET_MESSAGE_TYPE_STATISTICS_VALUE:
366 if (GNUNET_OK == process_message (h, msg))
368 /* finally, look for more! */
369 GNUNET_CLIENT_receive (h->client,
372 GNUNET_TIME_absolute_get_remaining
373 (h->current->timeout));
382 GNUNET_CLIENT_disconnect (h->client);
384 finish (h, GNUNET_SYSERR);
389 * Transmit a GET request (and if successful, start to receive
393 transmit_get (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
395 struct GNUNET_MessageHeader *hdr;
402 /* timeout / error */
403 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
404 _("Transmission of request for statistics failed!\n"));
405 finish (handle, GNUNET_SYSERR);
408 slen1 = strlen (handle->current->subsystem) + 1;
409 slen2 = strlen (handle->current->name) + 1;
410 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
411 GNUNET_assert (msize <= size);
412 hdr = (struct GNUNET_MessageHeader *) buf;
413 hdr->size = htons (msize);
414 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_GET);
415 GNUNET_assert (slen1 + slen2 ==
416 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1],
419 handle->current->subsystem,
420 handle->current->name));
421 GNUNET_CLIENT_receive (handle->client,
424 GNUNET_TIME_absolute_get_remaining (handle->
432 * Transmit a SET/UPDATE request.
435 transmit_set (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
437 struct GNUNET_STATISTICS_SetMessage *r;
444 finish (handle, GNUNET_SYSERR);
448 slen = strlen (handle->current->subsystem) + 1;
449 nlen = strlen (handle->current->name) + 1;
450 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
454 finish (handle, GNUNET_SYSERR);
458 r->header.size = htons (nsize);
459 r->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
461 r->value = GNUNET_htonll (handle->current->value);
462 if (handle->current->make_persistent)
463 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_PERSISTENT);
464 if (handle->current->type == ACTION_UPDATE)
465 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_RELATIVE);
466 GNUNET_assert (slen + nlen ==
467 GNUNET_STRINGS_buffer_fill ((char *) &r[1],
470 handle->current->subsystem,
471 handle->current->name));
472 finish (handle, GNUNET_OK);
478 transmit_action (void *cls, size_t size, void *buf)
480 struct GNUNET_STATISTICS_Handle *handle = cls;
483 switch (handle->current->type)
486 ret = transmit_get (handle, size, buf);
490 ret = transmit_set (handle, size, buf);
502 * Schedule the next action to be performed.
505 schedule_action (struct GNUNET_STATISTICS_Handle *h)
507 struct GNUNET_TIME_Relative timeout;
509 if (h->current != NULL)
510 return; /* action already pending */
511 if (GNUNET_YES != try_connect (h))
513 finish (h, GNUNET_SYSERR);
517 /* schedule next action */
518 h->current = h->action_head;
519 if (NULL == h->current)
521 /* no pending network action, check destroy! */
522 if (h->do_destroy != GNUNET_YES)
527 h->action_head = h->action_head->next;
528 if (NULL == h->action_head)
529 h->action_tail = NULL;
530 h->current->next = NULL;
532 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
534 GNUNET_CLIENT_notify_transmit_ready (h->client,
536 timeout, &transmit_action, h))
538 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
539 "Failed to transmit request to statistics service.\n");
540 finish (h, GNUNET_SYSERR);
546 insert_ai (struct GNUNET_STATISTICS_Handle *h, struct ActionItem *ai)
548 if (h->action_tail == NULL)
556 h->action_tail->next = ai;
563 * Get statistic from the peer.
565 * @param handle identification of the statistics service
566 * @param subsystem limit to the specified subsystem, NULL for our subsystem
567 * @param name name of the statistic value, NULL for all values
568 * @param timeout after how long should we give up (and call
569 * cont with an error code)?
570 * @param cont continuation to call when done (can be NULL)
571 * @param proc function to call on each value
572 * @param cls closure for cont and proc
575 GNUNET_STATISTICS_get (struct GNUNET_STATISTICS_Handle *handle,
576 const char *subsystem,
578 struct GNUNET_TIME_Relative timeout,
579 GNUNET_STATISTICS_Callback cont,
580 GNUNET_STATISTICS_Iterator proc, void *cls)
584 struct ActionItem *ai;
586 GNUNET_assert (handle != NULL);
587 GNUNET_assert (proc != NULL);
588 if (GNUNET_YES != try_connect (handle))
590 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
591 "Failed to connect to statistics service, can not get value `%s:%s'.\n",
592 strlen (subsystem) ? subsystem : "*",
593 strlen (name) ? name : "*");
595 cont (cls, GNUNET_SYSERR);
598 if (subsystem == NULL)
602 slen1 = strlen (subsystem);
603 slen2 = strlen (name);
604 GNUNET_assert (slen1 + slen2 + sizeof (struct GNUNET_MessageHeader) <
605 GNUNET_SERVER_MAX_MESSAGE_SIZE);
606 ai = GNUNET_malloc (sizeof (struct ActionItem));
607 ai->subsystem = GNUNET_strdup (subsystem);
608 ai->name = GNUNET_strdup (name);
612 ai->timeout = GNUNET_TIME_relative_to_absolute (timeout);
613 ai->type = ACTION_GET;
614 ai->msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
615 insert_ai (handle, ai);
620 add_setter_action (struct GNUNET_STATISTICS_Handle *h,
623 unsigned long long value, enum ActionType type)
625 struct ActionItem *ai;
630 GNUNET_assert (h != NULL);
631 GNUNET_assert (name != NULL);
632 if (GNUNET_YES != try_connect (h))
634 slen = strlen (h->subsystem) + 1;
635 nlen = strlen (name) + 1;
636 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
637 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
642 ai = GNUNET_malloc (sizeof (struct ActionItem));
643 ai->subsystem = GNUNET_strdup (h->subsystem);
644 ai->name = GNUNET_strdup (name);
645 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
646 ai->make_persistent = make_persistent;
656 * Set statistic value for the peer. Will always use our
657 * subsystem (the argument used when "handle" was created).
659 * @param handle identification of the statistics service
660 * @param name name of the statistic value
661 * @param value new value to set
662 * @param make_persistent should the value be kept across restarts?
665 GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
667 unsigned long long value, int make_persistent)
669 add_setter_action (handle, name, make_persistent, value, ACTION_SET);
674 * Set statistic value for the peer. Will always use our
675 * subsystem (the argument used when "handle" was created).
677 * @param handle identification of the statistics service
678 * @param name name of the statistic value
679 * @param delta change in value (added to existing value)
680 * @param make_persistent should the value be kept across restarts?
683 GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle,
685 long long delta, int make_persistent)
687 add_setter_action (handle, name, make_persistent,
688 (unsigned long long) delta, ACTION_UPDATE);
692 /* end of statistics_api.c */