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