1ec07ad5dc40eb0ee649ead935ea5df36b155f6e
[oweals/gnunet.git] / src / statistics / gnunet-service-statistics.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2012 Christian Grothoff (and other contributing authors)
4
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 3, or (at your
8      option) any later version.
9
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.
14
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.
19 */
20
21 /**
22  * @file statistics/gnunet-service-statistics.c
23  * @brief program that tracks statistics
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_bio_lib.h"
28 #include "gnunet_container_lib.h"
29 #include "gnunet_disk_lib.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_service_lib.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_strings_lib.h"
35 #include "gnunet_time_lib.h"
36 #include "statistics.h"
37
38 /**
39  * Watch entry.
40  */
41 struct WatchEntry
42 {
43
44   /**
45    * Watch entries are kept in a linked list.
46    */
47   struct WatchEntry *next;
48
49   /**
50    * Watch entries are kept in a linked list.
51    */
52   struct WatchEntry *prev;
53
54   /**
55    * For which client is this watch entry?
56    */
57   struct GNUNET_SERVER_Client *client;
58
59   /**
60    * Last value we communicated to the client for this watch entry.
61    */
62   uint64_t last_value;
63
64   /**
65    * Unique watch number for this client and this watched value.
66    */
67   uint32_t wid;
68
69 };
70
71
72 /**
73  * Client entry.
74  */
75 struct ClientEntry
76 {
77   /**
78    * Clients are kept in a linked list.
79    */
80   struct ClientEntry *next;
81
82   /**
83    * Clients are kept in a linked list.
84    */
85   struct ClientEntry *prev;
86
87   /**
88    * Corresponding server handle.
89    */
90   struct GNUNET_SERVER_Client *client;
91
92   /**
93    * Maximum watch ID used by this client so far.
94    */
95   uint32_t max_wid;
96
97 };
98
99 /**
100  * Entry in the statistics list.
101  */
102 struct StatsEntry
103 {
104   /**
105    * This is a linked list.
106    */
107   struct StatsEntry *next;
108
109   /**
110    * Name of the service, points into the
111    * middle of msg.
112    */
113   const char *service;
114
115   /**
116    * Name for the value, points into
117    * the middle of msg.
118    */
119   const char *name;
120
121   /**
122    * Message that can be used to set this value,
123    * stored at the end of the memory used by
124    * this struct.
125    */
126   struct GNUNET_STATISTICS_SetMessage *msg;
127
128   /**
129    * Watch context for changes to this
130    * value, or NULL for none.
131    */
132   struct WatchEntry *we_head;
133
134   /**
135    * Watch context for changes to this
136    * value, or NULL for none.
137    */
138   struct WatchEntry *we_tail;
139
140   /**
141    * Our value.
142    */
143   uint64_t value;
144
145   /**
146    * Unique ID.
147    */
148   uint32_t uid;
149
150   /**
151    * Is this value persistent?
152    */
153   int persistent;
154
155 };
156
157 /**
158  * Our configuration.
159  */
160 static const struct GNUNET_CONFIGURATION_Handle *cfg;
161
162 /**
163  * Linked list of our active statistics.
164  */
165 static struct StatsEntry *start;
166
167 /**
168  * Head of linked list of connected clients.
169  */
170 static struct ClientEntry *client_head;
171
172 /**
173  * Tail of linked list of connected clients.
174  */
175 static struct ClientEntry *client_tail;
176
177 /**
178  * Handle to our server.
179  */
180 static struct GNUNET_SERVER_Handle *srv;
181
182 /**
183  * Our notification context.
184  */
185 static struct GNUNET_SERVER_NotificationContext *nc;
186
187 /**
188  * Counter used to generate unique values.
189  */
190 static uint32_t uidgen;
191
192 /**
193  * Set to YES if we are shutting down as soon as possible.
194  */
195 static int in_shutdown;
196
197
198 /**
199  * Inject a message to our server with a client of 'NULL'.
200  *
201  * @param cls the 'struct GNUNET_SERVER_Handle'
202  * @param client unused
203  * @param msg message to inject
204  */
205 static void
206 inject_message (void *cls, void *client, const struct GNUNET_MessageHeader *msg)
207 {
208   struct GNUNET_SERVER_Handle *server = cls;
209
210   GNUNET_break (GNUNET_OK == GNUNET_SERVER_inject (server, NULL, msg));
211 }
212
213
214 /**
215  * Load persistent values from disk.  Disk format is
216  * exactly the same format that we also use for
217  * setting the values over the network.
218  *
219  * @param server handle to the server context
220  */
221 static void
222 load (struct GNUNET_SERVER_Handle *server)
223 {
224   char *fn;
225   struct GNUNET_BIO_ReadHandle *rh;
226   struct stat sb;
227   uint64_t fsize;
228   char *buf;
229   struct GNUNET_SERVER_MessageStreamTokenizer *mst;
230   char *emsg;
231
232   fn = GNUNET_DISK_get_home_filename (cfg, "statistics", "statistics.data",
233                                       NULL);
234   if (fn == NULL)
235     return;
236   if ((GNUNET_OK != GNUNET_DISK_file_size (fn, &fsize, GNUNET_NO, GNUNET_YES)) || (fsize == 0))
237   {
238     GNUNET_free (fn);
239     return;
240   }
241   buf = GNUNET_malloc (fsize);
242   rh = GNUNET_BIO_read_open (fn);
243   if (!rh)
244   {
245     GNUNET_free (buf);
246     GNUNET_free (fn);
247     return;
248   }
249   if (GNUNET_OK != GNUNET_BIO_read (rh, fn, buf, fsize))
250   {
251     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", fn);
252     GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
253     GNUNET_free (buf);
254     GNUNET_free_non_null (emsg);
255     GNUNET_free (fn);
256     return;
257   }
258   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
259               _("Loading %llu bytes of statistics from `%s'\n"),
260               fsize, fn);
261   mst = GNUNET_SERVER_mst_create (&inject_message, server);
262   GNUNET_break (GNUNET_OK ==
263                 GNUNET_SERVER_mst_receive (mst, NULL, buf, fsize,
264                                            GNUNET_YES, GNUNET_NO));
265   GNUNET_SERVER_mst_destroy (mst);
266   GNUNET_free (buf);
267   GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
268   GNUNET_free_non_null (emsg);
269   GNUNET_free (fn);
270 }
271
272
273 /**
274  * Write persistent statistics to disk.
275  */
276 static void
277 save ()
278 {
279   struct StatsEntry *pos;
280   char *fn;
281   struct GNUNET_BIO_WriteHandle *wh;
282   
283   uint16_t size;
284   unsigned long long total;
285
286   wh = NULL;
287   fn = GNUNET_DISK_get_home_filename (cfg, "statistics", "statistics.data",
288                                       NULL);
289   if (fn != NULL)
290     wh = GNUNET_BIO_write_open (fn);
291   total = 0;
292   while (NULL != (pos = start))
293   {
294     start = pos->next;
295     if ((pos->persistent) && (NULL != wh))
296     {
297       size = htons (pos->msg->header.size);
298       if (GNUNET_OK != GNUNET_BIO_write (wh, pos->msg, size))
299       {
300         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
301         if (GNUNET_OK != GNUNET_BIO_write_close (wh))
302           GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
303         wh = NULL;
304       }
305       else
306         total += size;
307     }
308     GNUNET_free (pos);
309   }
310   if (NULL != wh)
311   {
312     if (GNUNET_OK != GNUNET_BIO_write_close (wh))
313       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
314     if (total == 0)
315       GNUNET_break (0 == UNLINK (fn));
316     else
317       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
318                   _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
319   }
320   GNUNET_free_non_null (fn);
321 }
322
323
324 /**
325  * Transmit the given stats value.
326  *
327  * @param client receiver of the value
328  * @param e value to transmit
329  */
330 static void
331 transmit (struct GNUNET_SERVER_Client *client, const struct StatsEntry *e)
332 {
333   struct GNUNET_STATISTICS_ReplyMessage *m;
334   size_t size;
335
336   size =
337       sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
338       strlen (e->name) + 1;
339   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
340   m = GNUNET_malloc (size);
341   m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
342   m->header.size = htons (size);
343   m->uid = htonl (e->uid);
344   if (e->persistent)
345     m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
346   m->value = GNUNET_htonll (e->value);
347   size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
348   GNUNET_assert (size ==
349                  GNUNET_STRINGS_buffer_fill ((char *) &m[1], size, 2,
350                                              e->service, e->name));
351   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352               "Transmitting value for `%s:%s' (%d): %llu\n", e->service,
353               e->name, e->persistent, e->value);
354   GNUNET_SERVER_notification_context_unicast (nc, client, &m->header,
355                                               GNUNET_NO);
356   GNUNET_free (m);
357 }
358
359
360 /**
361  * Does this entry match the request?
362  *
363  * @param e an entry
364  * @param service name of service to match
365  * @param name value to match
366  * @return 1 if they match, 0 if not
367  */
368 static int
369 matches (const struct StatsEntry *e, const char *service, const char *name)
370 {
371   return ((0 == strlen (service)) || (0 == strcmp (service, e->service))) &&
372       ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
373 }
374
375
376 /**
377  * Find a client entry for the given client handle, or create one.
378  *
379  * @param client handle to match
380  * @return corresponding client entry struct
381  */
382 static struct ClientEntry *
383 make_client_entry (struct GNUNET_SERVER_Client *client)
384 {
385   struct ClientEntry *ce;
386
387   GNUNET_assert (client != NULL);
388   ce = client_head;
389   while (ce != NULL)
390   {
391     if (ce->client == client)
392       return ce;
393     ce = ce->next;
394   }
395   if (NULL == nc)
396   {
397     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
398     return NULL;
399   }
400   ce = GNUNET_malloc (sizeof (struct ClientEntry));
401   ce->client = client;
402   GNUNET_SERVER_client_keep (client);
403   GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ce);
404   GNUNET_SERVER_notification_context_add (nc, client);
405   return ce;
406 }
407
408
409 /**
410  * Handle GET-message.
411  *
412  * @param cls closure
413  * @param client identification of the client
414  * @param message the actual message
415  * @return GNUNET_OK to keep the connection open,
416  *         GNUNET_SYSERR to close it (signal serious error)
417  */
418 static void
419 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
420             const struct GNUNET_MessageHeader *message)
421 {
422   struct GNUNET_MessageHeader end;
423   char *service;
424   char *name;
425   struct StatsEntry *pos;
426   size_t size;
427
428   if ( (NULL != client) &&
429        (NULL == make_client_entry (client)) )
430     return; /* new client during shutdown */
431   size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
432   if (size !=
433       GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
434                                       &service, &name))
435   {
436     GNUNET_break (0);
437     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
438     return;
439   }
440   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441               "Received request for statistics on `%s:%s'\n",
442               strlen (service) ? service : "*", strlen (name) ? name : "*");
443   for (pos = start; NULL != pos; pos = pos->next)
444     if (matches (pos, service, name))
445       transmit (client, pos);
446   end.size = htons (sizeof (struct GNUNET_MessageHeader));
447   end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
448   GNUNET_SERVER_notification_context_unicast (nc, client, &end, GNUNET_NO);
449   GNUNET_SERVER_receive_done (client, GNUNET_OK);
450 }
451
452
453 /**
454  * Notify all clients listening about a change to a value.
455  *
456  * @param se value that changed
457  */
458 static void
459 notify_change (struct StatsEntry *se)
460 {
461   struct GNUNET_STATISTICS_WatchValueMessage wvm;
462   struct WatchEntry *pos;
463
464   for (pos = se->we_head; NULL != pos; pos = pos->next)
465   {
466     if (pos->last_value == se->value)
467       continue;
468     wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
469     wvm.header.size =
470       htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
471     wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
472     wvm.wid = htonl (pos->wid);
473     wvm.reserved = htonl (0);
474     wvm.value = GNUNET_htonll (se->value);
475     GNUNET_SERVER_notification_context_unicast (nc, pos->client, &wvm.header,
476                                                 GNUNET_NO);
477     pos->last_value = se->value;
478   }
479 }
480
481 /**
482  * Handle SET-message.
483  *
484  * @param cls closure
485  * @param client identification of the client
486  * @param message the actual message
487  */
488 static void
489 handle_set (void *cls, struct GNUNET_SERVER_Client *client,
490             const struct GNUNET_MessageHeader *message)
491 {
492   char *service;
493   char *name;
494   uint16_t msize;
495   uint16_t size;
496   const struct GNUNET_STATISTICS_SetMessage *msg;
497   struct StatsEntry *pos;
498   struct StatsEntry *prev;
499   uint32_t flags;
500   uint64_t value;
501   int64_t delta;
502   int changed;
503
504   if ( (NULL != client) &&
505        (NULL == make_client_entry (client)) )
506     return; /* new client during shutdown */
507   msize = ntohs (message->size);
508   if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
509   {
510     GNUNET_break (0);
511     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
512     return;
513   }
514   size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
515   msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
516
517   if (size !=
518       GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1], size, 2, &service,
519                                       &name))
520   {
521     GNUNET_break (0);
522     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
523     return;
524   }
525   flags = ntohl (msg->flags);
526   value = GNUNET_ntohll (msg->value);
527   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
528               "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
529               service, name, (unsigned int) flags, (unsigned long long) value);
530   pos = start;
531   prev = NULL;
532   while (pos != NULL)
533   {
534     if (matches (pos, service, name))
535     {
536       if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
537       {
538         changed = (pos->value != value);
539         pos->value = value;
540       }
541       else
542       {
543         delta = (int64_t) value;
544         if ((delta < 0) && (pos->value < -delta))
545         {
546           changed = (pos->value != 0);
547           pos->value = 0;
548         }
549         else
550         {
551           changed = (delta != 0);
552           GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value));
553           pos->value += delta;
554         }
555       }
556       pos->msg->value = GNUNET_htonll (pos->value);
557       pos->msg->flags = msg->flags;
558       pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
559       if (prev != NULL)
560       {
561         /* move to front for faster setting next time! */
562         prev->next = pos->next;
563         pos->next = start;
564         start = pos;
565       }
566       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
567                   "Statistic `%s:%s' updated to value %llu.\n", service, name,
568                   pos->value);
569       if (changed)
570         notify_change (pos);
571       GNUNET_SERVER_receive_done (client, GNUNET_OK);
572       return;
573     }
574     prev = pos;
575     pos = pos->next;
576   }
577   pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
578   pos->next = start;
579   if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
580       (0 < (int64_t) GNUNET_ntohll (msg->value)))
581     pos->value = GNUNET_ntohll (msg->value);
582   pos->uid = uidgen++;
583   pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
584   pos->msg = (void *) &pos[1];
585   memcpy (pos->msg, message, ntohs (message->size));
586   pos->service = (const char *) &pos->msg[1];
587   pos->name = &pos->service[strlen (pos->service) + 1];
588
589   start = pos;
590   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
591               "New statistic on `%s:%s' with value %llu created.\n", service,
592               name, pos->value);
593   GNUNET_SERVER_receive_done (client, GNUNET_OK);
594 }
595
596
597 /**
598  * Handle WATCH-message.
599  *
600  * @param cls closure
601  * @param client identification of the client
602  * @param message the actual message
603  */
604 static void
605 handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
606               const struct GNUNET_MessageHeader *message)
607 {
608   char *service;
609   char *name;
610   uint16_t msize;
611   uint16_t size;
612   struct StatsEntry *pos;
613   struct ClientEntry *ce;
614   struct WatchEntry *we;
615   size_t slen;
616
617   if (NULL == nc)
618   {
619     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
620     return;
621   }
622   ce = make_client_entry (client);
623   msize = ntohs (message->size);
624   if (msize < sizeof (struct GNUNET_MessageHeader))
625   {
626     GNUNET_break (0);
627     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
628     return;
629   }
630   size = msize - sizeof (struct GNUNET_MessageHeader);
631   if (size !=
632       GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
633                                       &service, &name))
634   {
635     GNUNET_break (0);
636     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
637     return;
638   }
639   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
640               "Received request to watch statistic on `%s:%s'\n", service,
641               name);
642   pos = start;
643   while (pos != NULL)
644   {
645     if (matches (pos, service, name))
646       break;
647     pos = pos->next;
648   }
649   if (pos == NULL)
650   {
651     pos =
652         GNUNET_malloc (sizeof (struct StatsEntry) +
653                        sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
654     pos->next = start;
655     pos->uid = uidgen++;
656     pos->msg = (void *) &pos[1];
657     pos->msg->header.size =
658         htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
659     pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
660     pos->service = (const char *) &pos->msg[1];
661     slen = strlen (service) + 1;
662     memcpy ((void *) pos->service, service, slen);
663     pos->name = &pos->service[slen];
664     memcpy ((void *) pos->name, name, strlen (name) + 1);
665     start = pos;
666   }
667   we = GNUNET_malloc (sizeof (struct WatchEntry));
668   we->client = client;
669   GNUNET_SERVER_client_keep (client);
670   we->wid = ce->max_wid++;
671   GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we);
672   if (pos->value != 0)
673     notify_change (pos);
674   GNUNET_SERVER_receive_done (client, GNUNET_OK);
675 }
676
677
678 /**
679  * Actually perform the shutdown.
680  */
681 static void 
682 do_shutdown ()
683 {
684   struct WatchEntry *we;
685   struct StatsEntry *se;
686
687   if (NULL == nc)
688     return;
689   save ();
690   GNUNET_SERVER_notification_context_destroy (nc);
691   nc = NULL;  
692   GNUNET_assert (NULL == client_head);
693   while (NULL != (se = start))
694   {
695     start = se->next;
696     while (NULL != (we = se->we_head))
697     {
698       GNUNET_SERVER_client_drop (we->client);
699       GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
700       GNUNET_free (we);
701     }
702     GNUNET_free (se);
703   }
704 }
705
706
707 /**
708  * Task run during shutdown.
709  *
710  * @param cls unused
711  * @param tc unused
712  */
713 static void
714 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
715 {
716   in_shutdown = GNUNET_YES;
717   if (NULL != client_head)
718     return;
719   do_shutdown ();
720 }
721
722
723 /**
724  * A client disconnected.  Remove all of its data structure entries.
725  *
726  * @param cls closure, NULL
727  * @param client identification of the client
728  */
729 static void
730 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
731 {
732   struct ClientEntry *ce;
733   struct WatchEntry *we;
734   struct WatchEntry *wen;
735   struct StatsEntry *se;
736
737   ce = client_head;
738   while (NULL != ce)
739   {
740     if (ce->client == client)
741     {
742       GNUNET_SERVER_client_drop (ce->client);
743       GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
744       GNUNET_free (ce);
745       break;
746     }
747     ce = ce->next;
748   }
749   se = start;
750   while (NULL != se)
751   {
752     wen = se->we_head;
753     while (NULL != (we = wen))
754     {
755       wen = we->next;
756       if (we->client != client)
757         continue;
758       GNUNET_SERVER_client_drop (we->client);
759       GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
760       GNUNET_free (we);
761     }
762     se = se->next;
763   }
764   if ( (NULL == client_head) &&
765        (GNUNET_YES == in_shutdown) )
766     do_shutdown ();
767 }
768
769
770 /**
771  * Process statistics requests.
772  *
773  * @param cls closure
774  * @param server the initialized server
775  * @param c configuration to use
776  */
777 static void
778 run (void *cls, struct GNUNET_SERVER_Handle *server,
779      const struct GNUNET_CONFIGURATION_Handle *c)
780 {
781   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
782     {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
783     {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
784     {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
785     {NULL, NULL, 0, 0}
786   };
787   cfg = c;
788   srv = server;
789   GNUNET_SERVER_add_handlers (server, handlers);
790   GNUNET_SERVER_ignore_shutdown (server, GNUNET_YES);
791   nc = GNUNET_SERVER_notification_context_create (server, 16);
792   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
793   load (server);
794   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
795                                 NULL);
796 }
797
798
799 /**
800  * The main function for the statistics service.
801  *
802  * @param argc number of arguments from the command line
803  * @param argv command line arguments
804  * @return 0 ok, 1 on error
805  */
806 int
807 main (int argc, char *const *argv)
808 {
809   return (GNUNET_OK ==
810           GNUNET_SERVICE_run (argc, argv, "statistics",
811                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
812 }
813
814 /* end of gnunet-service-statistics.c */