- cmd line arg
[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   uint64_t fsize;
227   char *buf;
228   struct GNUNET_SERVER_MessageStreamTokenizer *mst;
229   char *emsg;
230
231   fn = GNUNET_DISK_get_home_filename (cfg, "statistics", "statistics.data",
232                                       NULL);
233   if (fn == NULL)
234     return;
235   if ((GNUNET_OK != GNUNET_DISK_file_size (fn, &fsize, GNUNET_NO, GNUNET_YES)) || (fsize == 0))
236   {
237     GNUNET_free (fn);
238     return;
239   }
240   buf = GNUNET_malloc (fsize);
241   rh = GNUNET_BIO_read_open (fn);
242   if (!rh)
243   {
244     GNUNET_free (buf);
245     GNUNET_free (fn);
246     return;
247   }
248   if (GNUNET_OK != GNUNET_BIO_read (rh, fn, buf, fsize))
249   {
250     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", fn);
251     GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
252     GNUNET_free (buf);
253     GNUNET_free_non_null (emsg);
254     GNUNET_free (fn);
255     return;
256   }
257   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
258               _("Loading %llu bytes of statistics from `%s'\n"),
259               fsize, fn);
260   mst = GNUNET_SERVER_mst_create (&inject_message, server);
261   GNUNET_break (GNUNET_OK ==
262                 GNUNET_SERVER_mst_receive (mst, NULL, buf, fsize,
263                                            GNUNET_YES, GNUNET_NO));
264   GNUNET_SERVER_mst_destroy (mst);
265   GNUNET_free (buf);
266   GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
267   GNUNET_free_non_null (emsg);
268   GNUNET_free (fn);
269 }
270
271
272 /**
273  * Write persistent statistics to disk.
274  */
275 static void
276 save ()
277 {
278   struct StatsEntry *pos;
279   char *fn;
280   struct GNUNET_BIO_WriteHandle *wh;
281   
282   uint16_t size;
283   unsigned long long total;
284
285   wh = NULL;
286   fn = GNUNET_DISK_get_home_filename (cfg, "statistics", "statistics.data",
287                                       NULL);
288   if (fn != NULL)
289     wh = GNUNET_BIO_write_open (fn);
290   total = 0;
291   while (NULL != (pos = start))
292   {
293     start = pos->next;
294     if ((pos->persistent) && (NULL != wh))
295     {
296       size = htons (pos->msg->header.size);
297       if (GNUNET_OK != GNUNET_BIO_write (wh, pos->msg, size))
298       {
299         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
300         if (GNUNET_OK != GNUNET_BIO_write_close (wh))
301           GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
302         wh = NULL;
303       }
304       else
305         total += size;
306     }
307     GNUNET_free (pos);
308   }
309   if (NULL != wh)
310   {
311     if (GNUNET_OK != GNUNET_BIO_write_close (wh))
312       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
313     if (total == 0)
314       GNUNET_break (0 == UNLINK (fn));
315     else
316       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
317                   _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
318   }
319   GNUNET_free_non_null (fn);
320 }
321
322
323 /**
324  * Transmit the given stats value.
325  *
326  * @param client receiver of the value
327  * @param e value to transmit
328  */
329 static void
330 transmit (struct GNUNET_SERVER_Client *client, const struct StatsEntry *e)
331 {
332   struct GNUNET_STATISTICS_ReplyMessage *m;
333   size_t size;
334
335   size =
336       sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
337       strlen (e->name) + 1;
338   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
339   m = GNUNET_malloc (size);
340   m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
341   m->header.size = htons (size);
342   m->uid = htonl (e->uid);
343   if (e->persistent)
344     m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
345   m->value = GNUNET_htonll (e->value);
346   size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
347   GNUNET_assert (size ==
348                  GNUNET_STRINGS_buffer_fill ((char *) &m[1], size, 2,
349                                              e->service, e->name));
350   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
351               "Transmitting value for `%s:%s' (%d): %llu\n", e->service,
352               e->name, e->persistent, e->value);
353   GNUNET_SERVER_notification_context_unicast (nc, client, &m->header,
354                                               GNUNET_NO);
355   GNUNET_free (m);
356 }
357
358
359 /**
360  * Does this entry match the request?
361  *
362  * @param e an entry
363  * @param service name of service to match
364  * @param name value to match
365  * @return 1 if they match, 0 if not
366  */
367 static int
368 matches (const struct StatsEntry *e, const char *service, const char *name)
369 {
370   return ((0 == strlen (service)) || (0 == strcmp (service, e->service))) &&
371       ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
372 }
373
374
375 /**
376  * Find a client entry for the given client handle, or create one.
377  *
378  * @param client handle to match
379  * @return corresponding client entry struct
380  */
381 static struct ClientEntry *
382 make_client_entry (struct GNUNET_SERVER_Client *client)
383 {
384   struct ClientEntry *ce;
385
386   GNUNET_assert (client != NULL);
387   ce = client_head;
388   while (ce != NULL)
389   {
390     if (ce->client == client)
391       return ce;
392     ce = ce->next;
393   }
394   if (NULL == nc)
395   {
396     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
397     return NULL;
398   }
399   ce = GNUNET_malloc (sizeof (struct ClientEntry));
400   ce->client = client;
401   GNUNET_SERVER_client_keep (client);
402   GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ce);
403   GNUNET_SERVER_notification_context_add (nc, client);
404   return ce;
405 }
406
407
408 /**
409  * Handle GET-message.
410  *
411  * @param cls closure
412  * @param client identification of the client
413  * @param message the actual message
414  * @return GNUNET_OK to keep the connection open,
415  *         GNUNET_SYSERR to close it (signal serious error)
416  */
417 static void
418 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
419             const struct GNUNET_MessageHeader *message)
420 {
421   struct GNUNET_MessageHeader end;
422   char *service;
423   char *name;
424   struct StatsEntry *pos;
425   size_t size;
426
427   if ( (NULL != client) &&
428        (NULL == make_client_entry (client)) )
429     return; /* new client during shutdown */
430   size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
431   if (size !=
432       GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
433                                       &service, &name))
434   {
435     GNUNET_break (0);
436     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
437     return;
438   }
439   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440               "Received request for statistics on `%s:%s'\n",
441               strlen (service) ? service : "*", strlen (name) ? name : "*");
442   for (pos = start; NULL != pos; pos = pos->next)
443     if (matches (pos, service, name))
444       transmit (client, pos);
445   end.size = htons (sizeof (struct GNUNET_MessageHeader));
446   end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
447   GNUNET_SERVER_notification_context_unicast (nc, client, &end, GNUNET_NO);
448   GNUNET_SERVER_receive_done (client, GNUNET_OK);
449 }
450
451
452 /**
453  * Notify all clients listening about a change to a value.
454  *
455  * @param se value that changed
456  */
457 static void
458 notify_change (struct StatsEntry *se)
459 {
460   struct GNUNET_STATISTICS_WatchValueMessage wvm;
461   struct WatchEntry *pos;
462
463   for (pos = se->we_head; NULL != pos; pos = pos->next)
464   {
465     if (pos->last_value == se->value)
466       continue;
467     wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
468     wvm.header.size =
469       htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
470     wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
471     wvm.wid = htonl (pos->wid);
472     wvm.reserved = htonl (0);
473     wvm.value = GNUNET_htonll (se->value);
474     GNUNET_SERVER_notification_context_unicast (nc, pos->client, &wvm.header,
475                                                 GNUNET_NO);
476     pos->last_value = se->value;
477   }
478 }
479
480 /**
481  * Handle SET-message.
482  *
483  * @param cls closure
484  * @param client identification of the client
485  * @param message the actual message
486  */
487 static void
488 handle_set (void *cls, struct GNUNET_SERVER_Client *client,
489             const struct GNUNET_MessageHeader *message)
490 {
491   char *service;
492   char *name;
493   uint16_t msize;
494   uint16_t size;
495   const struct GNUNET_STATISTICS_SetMessage *msg;
496   struct StatsEntry *pos;
497   struct StatsEntry *prev;
498   uint32_t flags;
499   uint64_t value;
500   int64_t delta;
501   int changed;
502
503   if ( (NULL != client) &&
504        (NULL == make_client_entry (client)) )
505     return; /* new client during shutdown */
506   msize = ntohs (message->size);
507   if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
508   {
509     GNUNET_break (0);
510     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
511     return;
512   }
513   size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
514   msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
515
516   if (size !=
517       GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1], size, 2, &service,
518                                       &name))
519   {
520     GNUNET_break (0);
521     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
522     return;
523   }
524   flags = ntohl (msg->flags);
525   value = GNUNET_ntohll (msg->value);
526   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
527               "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
528               service, name, (unsigned int) flags, (unsigned long long) value);
529   pos = start;
530   prev = NULL;
531   while (pos != NULL)
532   {
533     if (matches (pos, service, name))
534     {
535       if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
536       {
537         changed = (pos->value != value);
538         pos->value = value;
539       }
540       else
541       {
542         delta = (int64_t) value;
543         if ((delta < 0) && (pos->value < -delta))
544         {
545           changed = (pos->value != 0);
546           pos->value = 0;
547         }
548         else
549         {
550           changed = (delta != 0);
551           GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value));
552           pos->value += delta;
553         }
554       }
555       pos->msg->value = GNUNET_htonll (pos->value);
556       pos->msg->flags = msg->flags;
557       pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
558       if (prev != NULL)
559       {
560         /* move to front for faster setting next time! */
561         prev->next = pos->next;
562         pos->next = start;
563         start = pos;
564       }
565       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
566                   "Statistic `%s:%s' updated to value %llu.\n", service, name,
567                   pos->value);
568       if (changed)
569         notify_change (pos);
570       GNUNET_SERVER_receive_done (client, GNUNET_OK);
571       return;
572     }
573     prev = pos;
574     pos = pos->next;
575   }
576   pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
577   pos->next = start;
578   if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
579       (0 < (int64_t) GNUNET_ntohll (msg->value)))
580     pos->value = GNUNET_ntohll (msg->value);
581   pos->uid = uidgen++;
582   pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
583   pos->msg = (void *) &pos[1];
584   memcpy (pos->msg, message, ntohs (message->size));
585   pos->service = (const char *) &pos->msg[1];
586   pos->name = &pos->service[strlen (pos->service) + 1];
587
588   start = pos;
589   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
590               "New statistic on `%s:%s' with value %llu created.\n", service,
591               name, pos->value);
592   GNUNET_SERVER_receive_done (client, GNUNET_OK);
593 }
594
595
596 /**
597  * Handle WATCH-message.
598  *
599  * @param cls closure
600  * @param client identification of the client
601  * @param message the actual message
602  */
603 static void
604 handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
605               const struct GNUNET_MessageHeader *message)
606 {
607   char *service;
608   char *name;
609   uint16_t msize;
610   uint16_t size;
611   struct StatsEntry *pos;
612   struct ClientEntry *ce;
613   struct WatchEntry *we;
614   size_t slen;
615
616   if (NULL == nc)
617   {
618     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
619     return;
620   }
621   ce = make_client_entry (client);
622   msize = ntohs (message->size);
623   if (msize < sizeof (struct GNUNET_MessageHeader))
624   {
625     GNUNET_break (0);
626     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
627     return;
628   }
629   size = msize - sizeof (struct GNUNET_MessageHeader);
630   if (size !=
631       GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
632                                       &service, &name))
633   {
634     GNUNET_break (0);
635     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
636     return;
637   }
638   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
639               "Received request to watch statistic on `%s:%s'\n", service,
640               name);
641   pos = start;
642   while (pos != NULL)
643   {
644     if (matches (pos, service, name))
645       break;
646     pos = pos->next;
647   }
648   if (pos == NULL)
649   {
650     pos =
651         GNUNET_malloc (sizeof (struct StatsEntry) +
652                        sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
653     pos->next = start;
654     pos->uid = uidgen++;
655     pos->msg = (void *) &pos[1];
656     pos->msg->header.size =
657         htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
658     pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
659     pos->service = (const char *) &pos->msg[1];
660     slen = strlen (service) + 1;
661     memcpy ((void *) pos->service, service, slen);
662     pos->name = &pos->service[slen];
663     memcpy ((void *) pos->name, name, strlen (name) + 1);
664     start = pos;
665   }
666   we = GNUNET_malloc (sizeof (struct WatchEntry));
667   we->client = client;
668   GNUNET_SERVER_client_keep (client);
669   we->wid = ce->max_wid++;
670   GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we);
671   if (pos->value != 0)
672     notify_change (pos);
673   GNUNET_SERVER_receive_done (client, GNUNET_OK);
674 }
675
676
677 /**
678  * Actually perform the shutdown.
679  */
680 static void 
681 do_shutdown ()
682 {
683   struct WatchEntry *we;
684   struct StatsEntry *se;
685
686   if (NULL == nc)
687     return;
688   save ();
689   GNUNET_SERVER_notification_context_destroy (nc);
690   nc = NULL;  
691   GNUNET_assert (NULL == client_head);
692   while (NULL != (se = start))
693   {
694     start = se->next;
695     while (NULL != (we = se->we_head))
696     {
697       GNUNET_SERVER_client_drop (we->client);
698       GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
699       GNUNET_free (we);
700     }
701     GNUNET_free (se);
702   }
703 }
704
705
706 /**
707  * Task run during shutdown.
708  *
709  * @param cls unused
710  * @param tc unused
711  */
712 static void
713 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
714 {
715   in_shutdown = GNUNET_YES;
716   if (NULL != client_head)
717     return;
718   do_shutdown ();
719 }
720
721
722 /**
723  * A client disconnected.  Remove all of its data structure entries.
724  *
725  * @param cls closure, NULL
726  * @param client identification of the client
727  */
728 static void
729 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
730 {
731   struct ClientEntry *ce;
732   struct WatchEntry *we;
733   struct WatchEntry *wen;
734   struct StatsEntry *se;
735
736   ce = client_head;
737   while (NULL != ce)
738   {
739     if (ce->client == client)
740     {
741       GNUNET_SERVER_client_drop (ce->client);
742       GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
743       GNUNET_free (ce);
744       break;
745     }
746     ce = ce->next;
747   }
748   se = start;
749   while (NULL != se)
750   {
751     wen = se->we_head;
752     while (NULL != (we = wen))
753     {
754       wen = we->next;
755       if (we->client != client)
756         continue;
757       GNUNET_SERVER_client_drop (we->client);
758       GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
759       GNUNET_free (we);
760     }
761     se = se->next;
762   }
763   if ( (NULL == client_head) &&
764        (GNUNET_YES == in_shutdown) )
765     do_shutdown ();
766 }
767
768
769 /**
770  * Process statistics requests.
771  *
772  * @param cls closure
773  * @param server the initialized server
774  * @param c configuration to use
775  */
776 static void
777 run (void *cls, struct GNUNET_SERVER_Handle *server,
778      const struct GNUNET_CONFIGURATION_Handle *c)
779 {
780   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
781     {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
782     {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
783     {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
784     {NULL, NULL, 0, 0}
785   };
786   cfg = c;
787   srv = server;
788   GNUNET_SERVER_add_handlers (server, handlers);
789   GNUNET_SERVER_ignore_shutdown (server, GNUNET_YES);
790   nc = GNUNET_SERVER_notification_context_create (server, 16);
791   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
792   load (server);
793   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
794                                 NULL);
795 }
796
797
798 /**
799  * The main function for the statistics service.
800  *
801  * @param argc number of arguments from the command line
802  * @param argv command line arguments
803  * @return 0 ok, 1 on error
804  */
805 int
806 main (int argc, char *const *argv)
807 {
808   return (GNUNET_OK ==
809           GNUNET_SERVICE_run (argc, argv, "statistics",
810                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
811 }
812
813 /* end of gnunet-service-statistics.c */