aboutsummaryrefslogtreecommitdiff
path: root/src/statistics
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-08-15 21:46:35 +0000
committerChristian Grothoff <christian@grothoff.org>2011-08-15 21:46:35 +0000
commit502af2167f7c218366666ca4944bd7cc54b5b19a (patch)
treea91fec5cc9769d260640bd91c6633cb9cf395524 /src/statistics
parent03af5a603b7cc53432249d5854cd412aa90dde0d (diff)
downloadgnunet-502af2167f7c218366666ca4944bd7cc54b5b19a.tar.gz
gnunet-502af2167f7c218366666ca4944bd7cc54b5b19a.zip
indentation
Diffstat (limited to 'src/statistics')
-rw-r--r--src/statistics/gnunet-service-statistics.c468
-rw-r--r--src/statistics/gnunet-statistics.c57
-rw-r--r--src/statistics/statistics.h2
-rw-r--r--src/statistics/statistics_api.c747
-rw-r--r--src/statistics/test_statistics_api.c89
-rw-r--r--src/statistics/test_statistics_api_loop.c40
-rw-r--r--src/statistics/test_statistics_api_watch.c74
7 files changed, 698 insertions, 779 deletions
diff --git a/src/statistics/gnunet-service-statistics.c b/src/statistics/gnunet-service-statistics.c
index c506dee7b..5e0be1183 100644
--- a/src/statistics/gnunet-service-statistics.c
+++ b/src/statistics/gnunet-service-statistics.c
@@ -50,7 +50,7 @@ struct WatchEntry
50 struct GNUNET_SERVER_Client *client; 50 struct GNUNET_SERVER_Client *client;
51 51
52 uint64_t last_value; 52 uint64_t last_value;
53 53
54 uint32_t wid; 54 uint32_t wid;
55 55
56}; 56};
@@ -67,7 +67,7 @@ struct ClientEntry
67 struct ClientEntry *prev; 67 struct ClientEntry *prev;
68 68
69 struct GNUNET_SERVER_Client *client; 69 struct GNUNET_SERVER_Client *client;
70 70
71 uint32_t max_wid; 71 uint32_t max_wid;
72 72
73}; 73};
@@ -156,9 +156,7 @@ static uint32_t uidgen;
156 156
157 157
158static void 158static void
159inject_message (void *cls, 159inject_message (void *cls, void *client, const struct GNUNET_MessageHeader *msg)
160 void *client,
161 const struct GNUNET_MessageHeader *msg)
162{ 160{
163 struct GNUNET_SERVER_Handle *server = cls; 161 struct GNUNET_SERVER_Handle *server = cls;
164 162
@@ -188,37 +186,33 @@ load (struct GNUNET_SERVER_Handle *server)
188 if (fn == NULL) 186 if (fn == NULL)
189 return; 187 return;
190 if ((0 != stat (fn, &sb)) || (sb.st_size == 0)) 188 if ((0 != stat (fn, &sb)) || (sb.st_size == 0))
191 { 189 {
192 GNUNET_free (fn); 190 GNUNET_free (fn);
193 return; 191 return;
194 } 192 }
195 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, 193 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
196 GNUNET_DISK_PERM_NONE);
197 if (!fh) 194 if (!fh)
198 { 195 {
199 GNUNET_free (fn); 196 GNUNET_free (fn);
200 return; 197 return;
201 } 198 }
202 buf = GNUNET_DISK_file_map (fh, &mh, GNUNET_DISK_MAP_TYPE_READ, sb.st_size); 199 buf = GNUNET_DISK_file_map (fh, &mh, GNUNET_DISK_MAP_TYPE_READ, sb.st_size);
203 if (NULL == buf) 200 if (NULL == buf)
204 { 201 {
205 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "mmap", fn); 202 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "mmap", fn);
206 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh)); 203 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
207 GNUNET_free (fn); 204 GNUNET_free (fn);
208 return; 205 return;
209 } 206 }
210 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 207 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
211 _("Loading %llu bytes of statistics from `%s'\n"), 208 _("Loading %llu bytes of statistics from `%s'\n"),
212 (unsigned long long) sb.st_size, fn); 209 (unsigned long long) sb.st_size, fn);
213 mst = GNUNET_SERVER_mst_create (&inject_message, 210 mst = GNUNET_SERVER_mst_create (&inject_message, server);
214 server);
215 GNUNET_break (GNUNET_OK == 211 GNUNET_break (GNUNET_OK ==
216 GNUNET_SERVER_mst_receive (mst, 212 GNUNET_SERVER_mst_receive (mst,
217 NULL, 213 NULL,
218 buf, 214 buf,
219 sb.st_size, 215 sb.st_size, GNUNET_YES, GNUNET_NO));
220 GNUNET_YES,
221 GNUNET_NO));
222 GNUNET_SERVER_mst_destroy (mst); 216 GNUNET_SERVER_mst_destroy (mst);
223 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_unmap (mh)); 217 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_unmap (mh));
224 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh)); 218 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
@@ -229,7 +223,7 @@ load (struct GNUNET_SERVER_Handle *server)
229 * Write persistent statistics to disk. 223 * Write persistent statistics to disk.
230 */ 224 */
231static void 225static void
232save () 226save ()
233{ 227{
234 struct StatsEntry *pos; 228 struct StatsEntry *pos;
235 char *fn; 229 char *fn;
@@ -242,36 +236,37 @@ save ()
242 "statistics", "statistics.data", NULL); 236 "statistics", "statistics.data", NULL);
243 if (fn != NULL) 237 if (fn != NULL)
244 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_WRITE 238 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_WRITE
245 | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_TRUNCATE, 239 | GNUNET_DISK_OPEN_CREATE |
246 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE); 240 GNUNET_DISK_OPEN_TRUNCATE,
241 GNUNET_DISK_PERM_USER_READ |
242 GNUNET_DISK_PERM_USER_WRITE);
247 total = 0; 243 total = 0;
248 while (NULL != (pos = start)) 244 while (NULL != (pos = start))
245 {
246 start = pos->next;
247 if ((pos->persistent) && (NULL != fh))
249 { 248 {
250 start = pos->next; 249 size = htons (pos->msg->header.size);
251 if ((pos->persistent) && (NULL != fh)) 250 if (size != GNUNET_DISK_file_write (fh, pos->msg, size))
252 { 251 {
253 size = htons (pos->msg->header.size); 252 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
254 if (size != GNUNET_DISK_file_write (fh, pos->msg, size)) 253 GNUNET_DISK_file_close (fh);
255 { 254 fh = NULL;
256 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 255 }
257 "write", fn);
258 GNUNET_DISK_file_close (fh);
259 fh = NULL;
260 }
261 else
262 total += size;
263 }
264 GNUNET_free (pos);
265 }
266 if (NULL != fh)
267 {
268 GNUNET_DISK_file_close (fh);
269 if (total == 0)
270 GNUNET_break (0 == UNLINK (fn));
271 else 256 else
272 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 257 total += size;
273 _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
274 } 258 }
259 GNUNET_free (pos);
260 }
261 if (NULL != fh)
262 {
263 GNUNET_DISK_file_close (fh);
264 if (total == 0)
265 GNUNET_break (0 == UNLINK (fn));
266 else
267 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
268 _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
269 }
275 GNUNET_free_non_null (fn); 270 GNUNET_free_non_null (fn);
276} 271}
277 272
@@ -280,15 +275,14 @@ save ()
280 * Transmit the given stats value. 275 * Transmit the given stats value.
281 */ 276 */
282static void 277static void
283transmit (struct GNUNET_SERVER_Client *client, 278transmit (struct GNUNET_SERVER_Client *client, const struct StatsEntry *e)
284 const struct StatsEntry *e)
285{ 279{
286 struct GNUNET_STATISTICS_ReplyMessage *m; 280 struct GNUNET_STATISTICS_ReplyMessage *m;
287 size_t size; 281 size_t size;
288 282
289 size = 283 size =
290 sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 + 284 sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
291 strlen (e->name) + 1; 285 strlen (e->name) + 1;
292 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE); 286 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
293 m = GNUNET_malloc (size); 287 m = GNUNET_malloc (size);
294 m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE); 288 m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
@@ -304,10 +298,10 @@ transmit (struct GNUNET_SERVER_Client *client,
304#if DEBUG_STATISTICS 298#if DEBUG_STATISTICS
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
306 "Transmitting value for `%s:%s' (%d): %llu\n", 300 "Transmitting value for `%s:%s' (%d): %llu\n",
307 e->service, e->name, 301 e->service, e->name, e->persistent, e->value);
308 e->persistent, e->value);
309#endif 302#endif
310 GNUNET_SERVER_notification_context_unicast (nc, client, &m->header, GNUNET_NO); 303 GNUNET_SERVER_notification_context_unicast (nc, client, &m->header,
304 GNUNET_NO);
311 GNUNET_free (m); 305 GNUNET_free (m);
312} 306}
313 307
@@ -320,7 +314,7 @@ matches (const struct StatsEntry *e, const char *service, const char *name)
320{ 314{
321 return ((0 == strlen (service)) || 315 return ((0 == strlen (service)) ||
322 (0 == strcmp (service, e->service))) 316 (0 == strcmp (service, e->service)))
323 && ((0 == strlen (name)) || (0 == strcmp (name, e->name))); 317 && ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
324} 318}
325 319
326 320
@@ -332,19 +326,16 @@ make_client_entry (struct GNUNET_SERVER_Client *client)
332 GNUNET_assert (client != NULL); 326 GNUNET_assert (client != NULL);
333 ce = client_head; 327 ce = client_head;
334 while (ce != NULL) 328 while (ce != NULL)
335 { 329 {
336 if (ce->client == client) 330 if (ce->client == client)
337 return ce; 331 return ce;
338 ce = ce->next; 332 ce = ce->next;
339 } 333 }
340 ce = GNUNET_malloc (sizeof (struct ClientEntry)); 334 ce = GNUNET_malloc (sizeof (struct ClientEntry));
341 ce->client = client; 335 ce->client = client;
342 GNUNET_SERVER_client_keep (client); 336 GNUNET_SERVER_client_keep (client);
343 GNUNET_CONTAINER_DLL_insert (client_head, 337 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ce);
344 client_tail, 338 GNUNET_SERVER_notification_context_add (nc, client);
345 ce);
346 GNUNET_SERVER_notification_context_add (nc,
347 client);
348 return ce; 339 return ce;
349} 340}
350 341
@@ -374,11 +365,11 @@ handle_get (void *cls,
374 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader); 365 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
375 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], 366 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
376 size, 2, &service, &name)) 367 size, 2, &service, &name))
377 { 368 {
378 GNUNET_break (0); 369 GNUNET_break (0);
379 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 370 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
380 return; 371 return;
381 } 372 }
382#if DEBUG_STATISTICS 373#if DEBUG_STATISTICS
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Received request for statistics on `%s:%s'\n", 375 "Received request for statistics on `%s:%s'\n",
@@ -386,19 +377,15 @@ handle_get (void *cls,
386#endif 377#endif
387 pos = start; 378 pos = start;
388 while (pos != NULL) 379 while (pos != NULL)
389 { 380 {
390 if (matches (pos, service, name)) 381 if (matches (pos, service, name))
391 transmit (client, pos); 382 transmit (client, pos);
392 pos = pos->next; 383 pos = pos->next;
393 } 384 }
394 end.size = htons (sizeof (struct GNUNET_MessageHeader)); 385 end.size = htons (sizeof (struct GNUNET_MessageHeader));
395 end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END); 386 end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
396 GNUNET_SERVER_notification_context_unicast (nc, 387 GNUNET_SERVER_notification_context_unicast (nc, client, &end, GNUNET_NO);
397 client, 388 GNUNET_SERVER_receive_done (client, GNUNET_OK);
398 &end,
399 GNUNET_NO);
400 GNUNET_SERVER_receive_done (client,
401 GNUNET_OK);
402} 389}
403 390
404 391
@@ -410,23 +397,23 @@ notify_change (struct StatsEntry *se)
410 397
411 pos = se->we_head; 398 pos = se->we_head;
412 while (pos != NULL) 399 while (pos != NULL)
400 {
401 if (pos->last_value != se->value)
413 { 402 {
414 if (pos->last_value != se->value) 403 wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
415 { 404 wvm.header.size =
416 wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE); 405 htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
417 wvm.header.size = htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage)); 406 wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
418 wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0); 407 wvm.wid = htonl (pos->wid);
419 wvm.wid = htonl (pos->wid); 408 wvm.reserved = htonl (0);
420 wvm.reserved = htonl (0); 409 wvm.value = GNUNET_htonll (se->value);
421 wvm.value = GNUNET_htonll (se->value); 410 GNUNET_SERVER_notification_context_unicast (nc,
422 GNUNET_SERVER_notification_context_unicast (nc, 411 pos->client,
423 pos->client, 412 &wvm.header, GNUNET_NO);
424 &wvm.header, 413 pos->last_value = se->value;
425 GNUNET_NO);
426 pos->last_value = se->value;
427 }
428 pos = pos->next;
429 } 414 }
415 pos = pos->next;
416 }
430} 417}
431 418
432/** 419/**
@@ -457,81 +444,77 @@ handle_set (void *cls,
457 make_client_entry (client); 444 make_client_entry (client);
458 msize = ntohs (message->size); 445 msize = ntohs (message->size);
459 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage)) 446 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
460 { 447 {
461 GNUNET_break (0); 448 GNUNET_break (0);
462 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 449 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
463 return; 450 return;
464 } 451 }
465 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage); 452 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
466 msg = (const struct GNUNET_STATISTICS_SetMessage *) message; 453 msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
467 454
468 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1], 455 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
469 size, 2, &service, &name)) 456 size, 2, &service, &name))
470 { 457 {
471 GNUNET_break (0); 458 GNUNET_break (0);
472 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 459 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
473 return; 460 return;
474 } 461 }
475 flags = ntohl (msg->flags); 462 flags = ntohl (msg->flags);
476 value = GNUNET_ntohll (msg->value); 463 value = GNUNET_ntohll (msg->value);
477#if DEBUG_STATISTICS 464#if DEBUG_STATISTICS
478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479 "Received request to update statistic on `%s:%s' (%u) to/by %llu\n", 466 "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
480 service, name, 467 service, name, (unsigned int) flags, (unsigned long long) value);
481 (unsigned int) flags,
482 (unsigned long long) value);
483#endif 468#endif
484 pos = start; 469 pos = start;
485 prev = NULL; 470 prev = NULL;
486 while (pos != NULL) 471 while (pos != NULL)
472 {
473 if (matches (pos, service, name))
487 { 474 {
488 if (matches (pos, service, name)) 475 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
476 {
477 changed = (pos->value != value);
478 pos->value = value;
479 }
480 else
481 {
482 delta = (int64_t) value;
483 if ((delta < 0) && (pos->value < -delta))
484 {
485 changed = (pos->value != 0);
486 pos->value = 0;
487 }
488 else
489 { 489 {
490 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) 490 changed = (delta != 0);
491 { 491 GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value));
492 changed = (pos->value != value); 492 pos->value += delta;
493 pos->value = value; 493 }
494 } 494 }
495 else 495 pos->msg->value = GNUNET_htonll (pos->value);
496 { 496 pos->msg->flags = msg->flags;
497 delta = (int64_t) value; 497 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
498 if ((delta < 0) && (pos->value < -delta)) 498 if (prev != NULL)
499 { 499 {
500 changed = (pos->value != 0); 500 /* move to front for faster setting next time! */
501 pos->value = 0; 501 prev->next = pos->next;
502 } 502 pos->next = start;
503 else 503 start = pos;
504 { 504 }
505 changed = (delta != 0);
506 GNUNET_break ((delta <= 0) ||
507 (pos->value + delta > pos->value));
508 pos->value += delta;
509 }
510 }
511 pos->msg->value = GNUNET_htonll (pos->value);
512 pos->msg->flags = msg->flags;
513 pos->persistent =
514 (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
515 if (prev != NULL)
516 {
517 /* move to front for faster setting next time! */
518 prev->next = pos->next;
519 pos->next = start;
520 start = pos;
521 }
522#if DEBUG_STATISTICS 505#if DEBUG_STATISTICS
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
524 "Statistic `%s:%s' updated to value %llu.\n", 507 "Statistic `%s:%s' updated to value %llu.\n",
525 service, name, pos->value); 508 service, name, pos->value);
526#endif 509#endif
527 if (changed) 510 if (changed)
528 notify_change (pos); 511 notify_change (pos);
529 GNUNET_SERVER_receive_done (client, GNUNET_OK); 512 GNUNET_SERVER_receive_done (client, GNUNET_OK);
530 return; 513 return;
531 }
532 prev = pos;
533 pos = pos->next;
534 } 514 }
515 prev = pos;
516 pos = pos->next;
517 }
535 pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize); 518 pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
536 pos->next = start; 519 pos->next = start;
537 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) || 520 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
@@ -563,8 +546,8 @@ handle_set (void *cls,
563 */ 546 */
564static void 547static void
565handle_watch (void *cls, 548handle_watch (void *cls,
566 struct GNUNET_SERVER_Client *client, 549 struct GNUNET_SERVER_Client *client,
567 const struct GNUNET_MessageHeader *message) 550 const struct GNUNET_MessageHeader *message)
568{ 551{
569 char *service; 552 char *service;
570 char *name; 553 char *name;
@@ -578,19 +561,19 @@ handle_watch (void *cls,
578 ce = make_client_entry (client); 561 ce = make_client_entry (client);
579 msize = ntohs (message->size); 562 msize = ntohs (message->size);
580 if (msize < sizeof (struct GNUNET_MessageHeader)) 563 if (msize < sizeof (struct GNUNET_MessageHeader))
581 { 564 {
582 GNUNET_break (0); 565 GNUNET_break (0);
583 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 566 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
584 return; 567 return;
585 } 568 }
586 size = msize - sizeof (struct GNUNET_MessageHeader); 569 size = msize - sizeof (struct GNUNET_MessageHeader);
587 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], 570 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
588 size, 2, &service, &name)) 571 size, 2, &service, &name))
589 { 572 {
590 GNUNET_break (0); 573 GNUNET_break (0);
591 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 574 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
592 return; 575 return;
593 } 576 }
594#if DEBUG_STATISTICS 577#if DEBUG_STATISTICS
595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
596 "Received request to watch statistic on `%s:%s'\n", 579 "Received request to watch statistic on `%s:%s'\n",
@@ -598,40 +581,36 @@ handle_watch (void *cls,
598#endif 581#endif
599 pos = start; 582 pos = start;
600 while (pos != NULL) 583 while (pos != NULL)
601 { 584 {
602 if (matches (pos, service, name)) 585 if (matches (pos, service, name))
603 break; 586 break;
604 pos = pos->next; 587 pos = pos->next;
605 } 588 }
606 if (pos == NULL) 589 if (pos == NULL)
607 { 590 {
608 pos = GNUNET_malloc (sizeof (struct StatsEntry) + 591 pos = GNUNET_malloc (sizeof (struct StatsEntry) +
609 sizeof (struct GNUNET_STATISTICS_SetMessage) + 592 sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
610 size); 593 pos->next = start;
611 pos->next = start; 594 pos->uid = uidgen++;
612 pos->uid = uidgen++; 595 pos->msg = (void *) &pos[1];
613 pos->msg = (void *) &pos[1]; 596 pos->msg->header.size =
614 pos->msg->header.size = htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + 597 htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
615 size); 598 pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
616 pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET); 599 pos->service = (const char *) &pos->msg[1];
617 pos->service = (const char *) &pos->msg[1]; 600 slen = strlen (service) + 1;
618 slen = strlen (service) + 1; 601 memcpy ((void *) pos->service, service, slen);
619 memcpy ((void*) pos->service, service, slen); 602 pos->name = &pos->service[slen];
620 pos->name = &pos->service[slen]; 603 memcpy ((void *) pos->name, name, strlen (name) + 1);
621 memcpy ((void*) pos->name, name, strlen (name)+1); 604 start = pos;
622 start = pos; 605 }
623 }
624 we = GNUNET_malloc (sizeof (struct WatchEntry)); 606 we = GNUNET_malloc (sizeof (struct WatchEntry));
625 we->client = client; 607 we->client = client;
626 GNUNET_SERVER_client_keep (client); 608 GNUNET_SERVER_client_keep (client);
627 we->wid = ce->max_wid++; 609 we->wid = ce->max_wid++;
628 GNUNET_CONTAINER_DLL_insert (pos->we_head, 610 GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we);
629 pos->we_tail,
630 we);
631 if (pos->value != 0) 611 if (pos->value != 0)
632 notify_change (pos); 612 notify_change (pos);
633 GNUNET_SERVER_receive_done (client, 613 GNUNET_SERVER_receive_done (client, GNUNET_OK);
634 GNUNET_OK);
635} 614}
636 615
637 616
@@ -642,8 +621,7 @@ handle_watch (void *cls,
642 * @param tc unused 621 * @param tc unused
643 */ 622 */
644static void 623static void
645shutdown_task (void *cls, 624shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
646 const struct GNUNET_SCHEDULER_TaskContext *tc)
647{ 625{
648 struct ClientEntry *ce; 626 struct ClientEntry *ce;
649 struct WatchEntry *we; 627 struct WatchEntry *we;
@@ -653,26 +631,22 @@ shutdown_task (void *cls,
653 GNUNET_SERVER_notification_context_destroy (nc); 631 GNUNET_SERVER_notification_context_destroy (nc);
654 nc = NULL; 632 nc = NULL;
655 while (NULL != (ce = client_head)) 633 while (NULL != (ce = client_head))
656 { 634 {
657 GNUNET_SERVER_client_drop (ce->client); 635 GNUNET_SERVER_client_drop (ce->client);
658 GNUNET_CONTAINER_DLL_remove (client_head, 636 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
659 client_tail, 637 GNUNET_free (ce);
660 ce); 638 }
661 GNUNET_free (ce);
662 }
663 while (NULL != (se = start)) 639 while (NULL != (se = start))
640 {
641 start = se->next;
642 while (NULL != (we = se->we_head))
664 { 643 {
665 start = se->next; 644 GNUNET_SERVER_client_drop (we->client);
666 while (NULL != (we = se->we_head)) 645 GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
667 { 646 GNUNET_free (we);
668 GNUNET_SERVER_client_drop (we->client);
669 GNUNET_CONTAINER_DLL_remove (se->we_head,
670 se->we_tail,
671 we);
672 GNUNET_free (we);
673 }
674 GNUNET_free (se);
675 } 647 }
648 GNUNET_free (se);
649 }
676} 650}
677 651
678 652
@@ -683,46 +657,40 @@ shutdown_task (void *cls,
683 * @param client identification of the client 657 * @param client identification of the client
684 */ 658 */
685static void 659static void
686handle_client_disconnect (void *cls, 660handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
687 struct GNUNET_SERVER_Client
688 * client)
689{ 661{
690 struct ClientEntry *ce; 662 struct ClientEntry *ce;
691 struct WatchEntry *we; 663 struct WatchEntry *we;
692 struct WatchEntry *wen; 664 struct WatchEntry *wen;
693 struct StatsEntry *se; 665 struct StatsEntry *se;
694 666
695 ce = client_head; 667 ce = client_head;
696 while (NULL != ce) 668 while (NULL != ce)
669 {
670 if (ce->client == client)
697 { 671 {
698 if (ce->client == client) 672 GNUNET_SERVER_client_drop (ce->client);
699 { 673 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
700 GNUNET_SERVER_client_drop (ce->client); 674 GNUNET_free (ce);
701 GNUNET_CONTAINER_DLL_remove (client_head, 675 break;
702 client_tail,
703 ce);
704 GNUNET_free (ce);
705 break;
706 }
707 ce = ce->next;
708 } 676 }
677 ce = ce->next;
678 }
709 se = start; 679 se = start;
710 while (NULL != se) 680 while (NULL != se)
681 {
682 wen = se->we_head;
683 while (NULL != (we = wen))
711 { 684 {
712 wen = se->we_head; 685 wen = we->next;
713 while (NULL != (we = wen)) 686 if (we->client != client)
714 { 687 continue;
715 wen = we->next; 688 GNUNET_SERVER_client_drop (we->client);
716 if (we->client != client) 689 GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
717 continue; 690 GNUNET_free (we);
718 GNUNET_SERVER_client_drop (we->client);
719 GNUNET_CONTAINER_DLL_remove (se->we_head,
720 se->we_tail,
721 we);
722 GNUNET_free (we);
723 }
724 se = se->next;
725 } 691 }
692 se = se->next;
693 }
726} 694}
727 695
728 696
@@ -747,13 +715,10 @@ run (void *cls,
747 cfg = c; 715 cfg = c;
748 GNUNET_SERVER_add_handlers (server, handlers); 716 GNUNET_SERVER_add_handlers (server, handlers);
749 nc = GNUNET_SERVER_notification_context_create (server, 16); 717 nc = GNUNET_SERVER_notification_context_create (server, 16);
750 GNUNET_SERVER_disconnect_notify (server, 718 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
751 &handle_client_disconnect,
752 NULL);
753 load (server); 719 load (server);
754 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, 720 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
755 &shutdown_task, 721 &shutdown_task, NULL);
756 NULL);
757} 722}
758 723
759 724
@@ -771,8 +736,7 @@ main (int argc, char *const *argv)
771 GNUNET_SERVICE_run (argc, 736 GNUNET_SERVICE_run (argc,
772 argv, 737 argv,
773 "statistics", 738 "statistics",
774 GNUNET_SERVICE_OPTION_NONE, 739 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
775 &run, NULL)) ? 0 : 1;
776} 740}
777 741
778/* end of gnunet-service-statistics.c */ 742/* end of gnunet-service-statistics.c */
diff --git a/src/statistics/gnunet-statistics.c b/src/statistics/gnunet-statistics.c
index 0cbafa68f..5267e38cf 100644
--- a/src/statistics/gnunet-statistics.c
+++ b/src/statistics/gnunet-statistics.c
@@ -65,13 +65,12 @@ static int persistent;
65static int 65static int
66printer (void *cls, 66printer (void *cls,
67 const char *subsystem, 67 const char *subsystem,
68 const char *name, 68 const char *name, uint64_t value, int is_persistent)
69 uint64_t value, int is_persistent)
70{ 69{
71 FPRINTF (stdout, 70 FPRINTF (stdout,
72 "%s%-12s %-50s: %16llu\n", 71 "%s%-12s %-50s: %16llu\n",
73 is_persistent ? "!" : " ", subsystem, _(name), 72 is_persistent ? "!" : " ", subsystem, _(name),
74 (unsigned long long) value); 73 (unsigned long long) value);
75 return GNUNET_OK; 74 return GNUNET_OK;
76} 75}
77 76
@@ -89,14 +88,12 @@ cleanup (void *cls, int success)
89 struct GNUNET_STATISTICS_Handle *h = cls; 88 struct GNUNET_STATISTICS_Handle *h = cls;
90 89
91 if (success != GNUNET_OK) 90 if (success != GNUNET_OK)
92 { 91 {
93 fprintf (stderr, 92 fprintf (stderr, _("Failed to obtain statistics.\n"));
94 _("Failed to obtain statistics.\n")); 93 ret = 1;
95 ret = 1; 94 }
96 }
97 if (h != NULL) 95 if (h != NULL)
98 GNUNET_STATISTICS_destroy (h, 96 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
99 GNUNET_NO);
100} 97}
101 98
102 99
@@ -111,39 +108,39 @@ cleanup (void *cls, int success)
111static void 108static void
112run (void *cls, 109run (void *cls,
113 char *const *args, 110 char *const *args,
114 const char *cfgfile, 111 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
115 const struct GNUNET_CONFIGURATION_Handle *cfg)
116{ 112{
117 struct GNUNET_STATISTICS_Handle *h; 113 struct GNUNET_STATISTICS_Handle *h;
118 unsigned long long val; 114 unsigned long long val;
119 115
120 if (args[0] != NULL) 116 if (args[0] != NULL)
117 {
118 if ((1 != SSCANF (args[0], "%llu", &val)) ||
119 (subsystem == NULL) || (name == NULL))
121 { 120 {
122 if ((1 != SSCANF (args[0], "%llu", &val)) || 121 FPRINTF (stderr, _("Invalid argument `%s'\n"), args[0]);
123 (subsystem == NULL) || (name == NULL)) 122 ret = 1;
124 {
125 FPRINTF (stderr, _("Invalid argument `%s'\n"), args[0]);
126 ret = 1;
127 return;
128 }
129 h = GNUNET_STATISTICS_create (subsystem, cfg);
130 if (h == NULL)
131 {
132 ret = 1;
133 return;
134 }
135 GNUNET_STATISTICS_set (h, name, (uint64_t) val, persistent);
136 GNUNET_STATISTICS_destroy (h, GNUNET_YES);
137 return; 123 return;
138 } 124 }
139 h = GNUNET_STATISTICS_create ("gnunet-statistics", cfg); 125 h = GNUNET_STATISTICS_create (subsystem, cfg);
140 if (h == NULL) 126 if (h == NULL)
141 { 127 {
142 ret = 1; 128 ret = 1;
143 return; 129 return;
144 } 130 }
131 GNUNET_STATISTICS_set (h, name, (uint64_t) val, persistent);
132 GNUNET_STATISTICS_destroy (h, GNUNET_YES);
133 return;
134 }
135 h = GNUNET_STATISTICS_create ("gnunet-statistics", cfg);
136 if (h == NULL)
137 {
138 ret = 1;
139 return;
140 }
145 if (NULL == GNUNET_STATISTICS_get (h, 141 if (NULL == GNUNET_STATISTICS_get (h,
146 subsystem, name, GET_TIMEOUT, &cleanup, &printer, h)) 142 subsystem, name, GET_TIMEOUT, &cleanup,
143 &printer, h))
147 cleanup (h, GNUNET_SYSERR); 144 cleanup (h, GNUNET_SYSERR);
148} 145}
149 146
diff --git a/src/statistics/statistics.h b/src/statistics/statistics.h
index 070a0aba5..757550922 100644
--- a/src/statistics/statistics.h
+++ b/src/statistics/statistics.h
@@ -126,7 +126,7 @@ struct GNUNET_STATISTICS_WatchValueMessage
126 * be signed even though the type given here is unsigned. 126 * be signed even though the type given here is unsigned.
127 */ 127 */
128 uint64_t value GNUNET_PACKED; 128 uint64_t value GNUNET_PACKED;
129 129
130}; 130};
131 131
132 132
diff --git a/src/statistics/statistics_api.c b/src/statistics/statistics_api.c
index e7cc73832..a76f83db1 100644
--- a/src/statistics/statistics_api.c
+++ b/src/statistics/statistics_api.c
@@ -58,7 +58,7 @@ enum ActionType
58 */ 58 */
59struct GNUNET_STATISTICS_WatchEntry 59struct GNUNET_STATISTICS_WatchEntry
60{ 60{
61 61
62 /** 62 /**
63 * What subsystem is this action about? (never NULL) 63 * What subsystem is this action about? (never NULL)
64 */ 64 */
@@ -102,7 +102,7 @@ struct GNUNET_STATISTICS_GetHandle
102 * Main statistics handle. 102 * Main statistics handle.
103 */ 103 */
104 struct GNUNET_STATISTICS_Handle *sh; 104 struct GNUNET_STATISTICS_Handle *sh;
105 105
106 /** 106 /**
107 * What subsystem is this action about? (can be NULL) 107 * What subsystem is this action about? (can be NULL)
108 */ 108 */
@@ -249,17 +249,15 @@ static void schedule_action (struct GNUNET_STATISTICS_Handle *h);
249 * 249 *
250 * @return GNUNET_YES on success, GNUNET_NO on failure. 250 * @return GNUNET_YES on success, GNUNET_NO on failure.
251 */ 251 */
252static int 252static int try_connect (struct GNUNET_STATISTICS_Handle *ret);
253try_connect (struct GNUNET_STATISTICS_Handle *ret);
254 253
255 254
256static void 255static void
257insert_ai (struct GNUNET_STATISTICS_Handle *h, struct GNUNET_STATISTICS_GetHandle *ai) 256insert_ai (struct GNUNET_STATISTICS_Handle *h,
257 struct GNUNET_STATISTICS_GetHandle *ai)
258{ 258{
259 GNUNET_CONTAINER_DLL_insert_after (h->action_head, 259 GNUNET_CONTAINER_DLL_insert_after (h->action_head,
260 h->action_tail, 260 h->action_tail, h->action_tail, ai);
261 h->action_tail,
262 ai);
263 if (h->action_head == ai) 261 if (h->action_head == ai)
264 schedule_action (h); 262 schedule_action (h);
265} 263}
@@ -267,28 +265,28 @@ insert_ai (struct GNUNET_STATISTICS_Handle *h, struct GNUNET_STATISTICS_GetHandl
267 265
268static void 266static void
269schedule_watch_request (struct GNUNET_STATISTICS_Handle *h, 267schedule_watch_request (struct GNUNET_STATISTICS_Handle *h,
270 struct GNUNET_STATISTICS_WatchEntry *watch) 268 struct GNUNET_STATISTICS_WatchEntry *watch)
271{ 269{
272 270
273 struct GNUNET_STATISTICS_GetHandle *ai; 271 struct GNUNET_STATISTICS_GetHandle *ai;
274 size_t slen; 272 size_t slen;
275 size_t nlen; 273 size_t nlen;
276 size_t nsize; 274 size_t nsize;
277 275
278 GNUNET_assert (h != NULL); 276 GNUNET_assert (h != NULL);
279 if (GNUNET_YES != try_connect (h)) 277 if (GNUNET_YES != try_connect (h))
280 { 278 {
281 schedule_action (h); 279 schedule_action (h);
282 return; 280 return;
283 } 281 }
284 slen = strlen (watch->subsystem) + 1; 282 slen = strlen (watch->subsystem) + 1;
285 nlen = strlen (watch->name) + 1; 283 nlen = strlen (watch->name) + 1;
286 nsize = sizeof (struct GNUNET_MessageHeader) + slen + nlen; 284 nsize = sizeof (struct GNUNET_MessageHeader) + slen + nlen;
287 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) 285 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
288 { 286 {
289 GNUNET_break (0); 287 GNUNET_break (0);
290 return; 288 return;
291 } 289 }
292 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle)); 290 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
293 ai->sh = h; 291 ai->sh = h;
294 ai->subsystem = GNUNET_strdup (watch->subsystem); 292 ai->subsystem = GNUNET_strdup (watch->subsystem);
@@ -311,15 +309,16 @@ static int
311try_connect (struct GNUNET_STATISTICS_Handle *ret) 309try_connect (struct GNUNET_STATISTICS_Handle *ret)
312{ 310{
313 unsigned int i; 311 unsigned int i;
312
314 if (ret->client != NULL) 313 if (ret->client != NULL)
315 return GNUNET_YES; 314 return GNUNET_YES;
316 ret->client = GNUNET_CLIENT_connect ("statistics", ret->cfg); 315 ret->client = GNUNET_CLIENT_connect ("statistics", ret->cfg);
317 if (ret->client != NULL) 316 if (ret->client != NULL)
318 { 317 {
319 for (i=0;i<ret->watches_size;i++) 318 for (i = 0; i < ret->watches_size; i++)
320 schedule_watch_request (ret, ret->watches[i]); 319 schedule_watch_request (ret, ret->watches[i]);
321 return GNUNET_YES; 320 return GNUNET_YES;
322 } 321 }
323#if DEBUG_STATISTICS 322#if DEBUG_STATISTICS
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
325 _("Failed to connect to statistics service!\n")); 324 _("Failed to connect to statistics service!\n"));
@@ -347,14 +346,15 @@ static void
347finish (struct GNUNET_STATISTICS_Handle *h, int code) 346finish (struct GNUNET_STATISTICS_Handle *h, int code)
348{ 347{
349 struct GNUNET_STATISTICS_GetHandle *pos = h->current; 348 struct GNUNET_STATISTICS_GetHandle *pos = h->current;
349
350 h->current = NULL; 350 h->current = NULL;
351 schedule_action (h); 351 schedule_action (h);
352 if (pos != NULL) 352 if (pos != NULL)
353 { 353 {
354 if (pos->cont != NULL) 354 if (pos->cont != NULL)
355 pos->cont (pos->cls, code); 355 pos->cont (pos->cls, code);
356 free_action_item (pos); 356 free_action_item (pos);
357 } 357 }
358} 358}
359 359
360 360
@@ -373,27 +373,27 @@ process_message (struct GNUNET_STATISTICS_Handle *h,
373 uint16_t size; 373 uint16_t size;
374 374
375 if (h->current->aborted) 375 if (h->current->aborted)
376 { 376 {
377#if DEBUG_STATISTICS 377#if DEBUG_STATISTICS
378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
379 "Iteration was aborted, ignoring VALUE\n"); 379 "Iteration was aborted, ignoring VALUE\n");
380#endif 380#endif
381 return GNUNET_OK; /* don't bother */ 381 return GNUNET_OK; /* don't bother */
382 } 382 }
383 size = ntohs (msg->size); 383 size = ntohs (msg->size);
384 if (size < sizeof (struct GNUNET_STATISTICS_ReplyMessage)) 384 if (size < sizeof (struct GNUNET_STATISTICS_ReplyMessage))
385 { 385 {
386 GNUNET_break (0); 386 GNUNET_break (0);
387 return GNUNET_SYSERR; 387 return GNUNET_SYSERR;
388 } 388 }
389 smsg = (const struct GNUNET_STATISTICS_ReplyMessage *) msg; 389 smsg = (const struct GNUNET_STATISTICS_ReplyMessage *) msg;
390 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage); 390 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
391 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &smsg[1], 391 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &smsg[1],
392 size, 2, &service, &name)) 392 size, 2, &service, &name))
393 { 393 {
394 GNUNET_break (0); 394 GNUNET_break (0);
395 return GNUNET_SYSERR; 395 return GNUNET_SYSERR;
396 } 396 }
397#if DEBUG_STATISTICS 397#if DEBUG_STATISTICS
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399 "Received valid statistic on `%s:%s': %llu\n", 399 "Received valid statistic on `%s:%s': %llu\n",
@@ -401,55 +401,52 @@ process_message (struct GNUNET_STATISTICS_Handle *h,
401#endif 401#endif
402 if (GNUNET_OK != 402 if (GNUNET_OK !=
403 h->current->proc (h->current->cls, 403 h->current->proc (h->current->cls,
404 service, 404 service,
405 name, 405 name,
406 GNUNET_ntohll (smsg->value), 406 GNUNET_ntohll (smsg->value),
407 0 != 407 0 !=
408 (ntohl (smsg->uid) & GNUNET_STATISTICS_PERSIST_BIT))) 408 (ntohl (smsg->uid) & GNUNET_STATISTICS_PERSIST_BIT)))
409 { 409 {
410#if DEBUG_STATISTICS 410#if DEBUG_STATISTICS
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412 "Processing of remaining statistics aborted by client.\n"); 412 "Processing of remaining statistics aborted by client.\n");
413#endif 413#endif
414 h->current->aborted = GNUNET_YES; 414 h->current->aborted = GNUNET_YES;
415 } 415 }
416#if DEBUG_STATISTICS 416#if DEBUG_STATISTICS
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "VALUE processed successfully\n");
418 "VALUE processed successfully\n"); 418#endif
419#endif
420 return GNUNET_OK; 419 return GNUNET_OK;
421} 420}
422 421
423 422
424static int 423static int
425process_watch_value (struct GNUNET_STATISTICS_Handle *h, 424process_watch_value (struct GNUNET_STATISTICS_Handle *h,
426 const struct GNUNET_MessageHeader *msg) 425 const struct GNUNET_MessageHeader *msg)
427{ 426{
428 const struct GNUNET_STATISTICS_WatchValueMessage *wvm; 427 const struct GNUNET_STATISTICS_WatchValueMessage *wvm;
429 struct GNUNET_STATISTICS_WatchEntry *w; 428 struct GNUNET_STATISTICS_WatchEntry *w;
430 uint32_t wid; 429 uint32_t wid;
431 430
432 if (sizeof(struct GNUNET_STATISTICS_WatchValueMessage) != 431 if (sizeof (struct GNUNET_STATISTICS_WatchValueMessage) != ntohs (msg->size))
433 ntohs (msg->size)) 432 {
434 { 433 GNUNET_break (0);
435 GNUNET_break (0); 434 return GNUNET_SYSERR;
436 return GNUNET_SYSERR; 435 }
437 } 436 wvm = (const struct GNUNET_STATISTICS_WatchValueMessage *) msg;
438 wvm = (const struct GNUNET_STATISTICS_WatchValueMessage *)msg;
439 GNUNET_break (0 == ntohl (wvm->reserved)); 437 GNUNET_break (0 == ntohl (wvm->reserved));
440 wid = ntohl (wvm->wid); 438 wid = ntohl (wvm->wid);
441 if (wid >= h->watches_size) 439 if (wid >= h->watches_size)
442 { 440 {
443 GNUNET_break (0); 441 GNUNET_break (0);
444 return GNUNET_SYSERR; 442 return GNUNET_SYSERR;
445 } 443 }
446 w = h->watches[wid]; 444 w = h->watches[wid];
447 (void) w->proc (w->proc_cls, 445 (void) w->proc (w->proc_cls,
448 w->subsystem, 446 w->subsystem,
449 w->name, 447 w->name,
450 GNUNET_ntohll (wvm->value), 448 GNUNET_ntohll (wvm->value),
451 0 != 449 0 != (ntohl (wvm->flags) & GNUNET_STATISTICS_PERSIST_BIT));
452 (ntohl (wvm->flags) & GNUNET_STATISTICS_PERSIST_BIT));
453 return GNUNET_OK; 450 return GNUNET_OK;
454} 451}
455 452
@@ -466,82 +463,75 @@ receive_stats (void *cls, const struct GNUNET_MessageHeader *msg)
466 struct GNUNET_STATISTICS_Handle *h = cls; 463 struct GNUNET_STATISTICS_Handle *h = cls;
467 464
468 if (msg == NULL) 465 if (msg == NULL)
466 {
467 if (NULL != h->client)
469 { 468 {
470 if (NULL != h->client) 469 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
471 { 470 h->client = NULL;
472 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO); 471 }
473 h->client = NULL;
474 }
475#if DEBUG_STATISTICS 472#if DEBUG_STATISTICS
476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, 473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
477 "Error receiving statistics from service, is the service running?\n" ); 474 "Error receiving statistics from service, is the service running?\n");
478#endif 475#endif
479 finish (h, GNUNET_SYSERR); 476 finish (h, GNUNET_SYSERR);
480 return; 477 return;
481 } 478 }
482 switch (ntohs (msg->type)) 479 switch (ntohs (msg->type))
480 {
481 case GNUNET_MESSAGE_TYPE_STATISTICS_END:
482#if DEBUG_STATISTICS
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received end of statistics marker\n");
484#endif
485 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
486 if (h->watches_size > 0)
483 { 487 {
484 case GNUNET_MESSAGE_TYPE_STATISTICS_END: 488 GNUNET_CLIENT_receive (h->client,
489 &receive_stats, h, GNUNET_TIME_UNIT_FOREVER_REL);
490 }
491 else
492 {
493 h->receiving = GNUNET_NO;
494 }
495 finish (h, GNUNET_OK);
496 return;
497 case GNUNET_MESSAGE_TYPE_STATISTICS_VALUE:
498 if (GNUNET_OK == process_message (h, msg))
499 {
500 /* finally, look for more! */
485#if DEBUG_STATISTICS 501#if DEBUG_STATISTICS
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "Received end of statistics marker\n"); 503 "Processing VALUE done, now reading more\n");
488#endif 504#endif
505 GNUNET_CLIENT_receive (h->client,
506 &receive_stats,
507 h,
508 GNUNET_TIME_absolute_get_remaining
509 (h->current->timeout));
489 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS; 510 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
490 if (h->watches_size > 0)
491 {
492 GNUNET_CLIENT_receive (h->client,
493 &receive_stats,
494 h,
495 GNUNET_TIME_UNIT_FOREVER_REL);
496 }
497 else
498 {
499 h->receiving = GNUNET_NO;
500 }
501 finish (h, GNUNET_OK);
502 return; 511 return;
503 case GNUNET_MESSAGE_TYPE_STATISTICS_VALUE:
504 if (GNUNET_OK == process_message (h, msg))
505 {
506 /* finally, look for more! */
507#if DEBUG_STATISTICS
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509 "Processing VALUE done, now reading more\n");
510#endif
511 GNUNET_CLIENT_receive (h->client,
512 &receive_stats,
513 h,
514 GNUNET_TIME_absolute_get_remaining
515 (h->current->timeout));
516 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
517 return;
518 }
519 GNUNET_break (0);
520 break;
521 case GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE:
522 if (GNUNET_OK ==
523 process_watch_value (h,
524 msg))
525 {
526 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
527 GNUNET_assert (h->watches_size > 0);
528 GNUNET_CLIENT_receive (h->client,
529 &receive_stats,
530 h,
531 GNUNET_TIME_UNIT_FOREVER_REL);
532 return;
533 }
534 GNUNET_break (0);
535 break;
536 default:
537 GNUNET_break (0);
538 break;
539 } 512 }
540 if (NULL != h->client) 513 GNUNET_break (0);
514 break;
515 case GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE:
516 if (GNUNET_OK == process_watch_value (h, msg))
541 { 517 {
542 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO); 518 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
543 h->client = NULL; 519 GNUNET_assert (h->watches_size > 0);
520 GNUNET_CLIENT_receive (h->client,
521 &receive_stats, h, GNUNET_TIME_UNIT_FOREVER_REL);
522 return;
544 } 523 }
524 GNUNET_break (0);
525 break;
526 default:
527 GNUNET_break (0);
528 break;
529 }
530 if (NULL != h->client)
531 {
532 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
533 h->client = NULL;
534 }
545 finish (h, GNUNET_SYSERR); 535 finish (h, GNUNET_SYSERR);
546} 536}
547 537
@@ -559,15 +549,15 @@ transmit_get (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
559 uint16_t msize; 549 uint16_t msize;
560 550
561 if (buf == NULL) 551 if (buf == NULL)
562 { 552 {
563 /* timeout / error */ 553 /* timeout / error */
564#if DEBUG_STATISTICS 554#if DEBUG_STATISTICS
565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
566 "Transmission of request for statistics failed!\n"); 556 "Transmission of request for statistics failed!\n");
567#endif 557#endif
568 finish (handle, GNUNET_SYSERR); 558 finish (handle, GNUNET_SYSERR);
569 return 0; 559 return 0;
570 } 560 }
571 slen1 = strlen (handle->current->subsystem) + 1; 561 slen1 = strlen (handle->current->subsystem) + 1;
572 slen2 = strlen (handle->current->name) + 1; 562 slen2 = strlen (handle->current->name) + 1;
573 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader); 563 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
@@ -581,19 +571,19 @@ transmit_get (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
581 2, 571 2,
582 handle->current->subsystem, 572 handle->current->subsystem,
583 handle->current->name)); 573 handle->current->name));
584 if (! handle->receiving) 574 if (!handle->receiving)
585 { 575 {
586#if DEBUG_STATISTICS 576#if DEBUG_STATISTICS
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588 "Transmission of GET done, now reading response\n"); 578 "Transmission of GET done, now reading response\n");
589#endif 579#endif
590 handle->receiving = GNUNET_YES; 580 handle->receiving = GNUNET_YES;
591 GNUNET_CLIENT_receive (handle->client, 581 GNUNET_CLIENT_receive (handle->client,
592 &receive_stats, 582 &receive_stats,
593 handle, 583 handle,
594 GNUNET_TIME_absolute_get_remaining (handle-> 584 GNUNET_TIME_absolute_get_remaining (handle->
595 current->timeout)); 585 current->timeout));
596 } 586 }
597 return msize; 587 return msize;
598} 588}
599 589
@@ -611,19 +601,18 @@ transmit_watch (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
611 uint16_t msize; 601 uint16_t msize;
612 602
613 if (buf == NULL) 603 if (buf == NULL)
614 { 604 {
615 /* timeout / error */ 605 /* timeout / error */
616#if DEBUG_STATISTICS 606#if DEBUG_STATISTICS
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
618 "Transmission of request for statistics failed!\n"); 608 "Transmission of request for statistics failed!\n");
619#endif 609#endif
620 finish (handle, GNUNET_SYSERR); 610 finish (handle, GNUNET_SYSERR);
621 return 0; 611 return 0;
622 } 612 }
623#if DEBUG_STATISTICS 613#if DEBUG_STATISTICS
624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
625 "Transmitting watch request for `%s'\n", 615 "Transmitting watch request for `%s'\n", handle->current->name);
626 handle->current->name);
627#endif 616#endif
628 slen1 = strlen (handle->current->subsystem) + 1; 617 slen1 = strlen (handle->current->subsystem) + 1;
629 slen2 = strlen (handle->current->name) + 1; 618 slen2 = strlen (handle->current->name) + 1;
@@ -639,13 +628,12 @@ transmit_watch (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
639 handle->current->subsystem, 628 handle->current->subsystem,
640 handle->current->name)); 629 handle->current->name));
641 if (GNUNET_YES != handle->receiving) 630 if (GNUNET_YES != handle->receiving)
642 { 631 {
643 handle->receiving = GNUNET_YES; 632 handle->receiving = GNUNET_YES;
644 GNUNET_CLIENT_receive (handle->client, 633 GNUNET_CLIENT_receive (handle->client,
645 &receive_stats, 634 &receive_stats,
646 handle, 635 handle, GNUNET_TIME_UNIT_FOREVER_REL);
647 GNUNET_TIME_UNIT_FOREVER_REL); 636 }
648 }
649 finish (handle, GNUNET_OK); 637 finish (handle, GNUNET_OK);
650 return msize; 638 return msize;
651} 639}
@@ -663,20 +651,20 @@ transmit_set (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
663 size_t nsize; 651 size_t nsize;
664 652
665 if (NULL == buf) 653 if (NULL == buf)
666 { 654 {
667 finish (handle, GNUNET_SYSERR); 655 finish (handle, GNUNET_SYSERR);
668 return 0; 656 return 0;
669 } 657 }
670 658
671 slen = strlen (handle->current->subsystem) + 1; 659 slen = strlen (handle->current->subsystem) + 1;
672 nlen = strlen (handle->current->name) + 1; 660 nlen = strlen (handle->current->name) + 1;
673 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen; 661 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
674 if (size < nsize) 662 if (size < nsize)
675 { 663 {
676 GNUNET_break (0); 664 GNUNET_break (0);
677 finish (handle, GNUNET_SYSERR); 665 finish (handle, GNUNET_SYSERR);
678 return 0; 666 return 0;
679 } 667 }
680 r = buf; 668 r = buf;
681 r->header.size = htons (nsize); 669 r->header.size = htons (nsize);
682 r->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET); 670 r->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
@@ -705,22 +693,22 @@ transmit_action (void *cls, size_t size, void *buf)
705 693
706 handle->th = NULL; 694 handle->th = NULL;
707 switch (handle->current->type) 695 switch (handle->current->type)
708 { 696 {
709 case ACTION_GET: 697 case ACTION_GET:
710 ret = transmit_get (handle, size, buf); 698 ret = transmit_get (handle, size, buf);
711 break; 699 break;
712 case ACTION_SET: 700 case ACTION_SET:
713 case ACTION_UPDATE: 701 case ACTION_UPDATE:
714 ret = transmit_set (handle, size, buf); 702 ret = transmit_set (handle, size, buf);
715 break; 703 break;
716 case ACTION_WATCH: 704 case ACTION_WATCH:
717 ret = transmit_watch (handle, size, buf); 705 ret = transmit_watch (handle, size, buf);
718 break; 706 break;
719 default: 707 default:
720 ret = 0; 708 ret = 0;
721 GNUNET_break (0); 709 GNUNET_break (0);
722 break; 710 break;
723 } 711 }
724 return ret; 712 return ret;
725} 713}
726 714
@@ -745,11 +733,11 @@ GNUNET_STATISTICS_create (const char *subsystem,
745 ret->subsystem = GNUNET_strdup (subsystem); 733 ret->subsystem = GNUNET_strdup (subsystem);
746 ret->backoff = GNUNET_TIME_UNIT_MILLISECONDS; 734 ret->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
747 if (GNUNET_YES != try_connect (ret)) 735 if (GNUNET_YES != try_connect (ret))
748 { 736 {
749 GNUNET_free (ret->subsystem); 737 GNUNET_free (ret->subsystem);
750 GNUNET_free (ret); 738 GNUNET_free (ret);
751 return NULL; 739 return NULL;
752 } 740 }
753 return ret; 741 return ret;
754} 742}
755 743
@@ -763,8 +751,7 @@ GNUNET_STATISTICS_create (const char *subsystem,
763 * be completed 751 * be completed
764 */ 752 */
765void 753void
766GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h, 754GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h, int sync_first)
767 int sync_first)
768{ 755{
769 struct GNUNET_STATISTICS_GetHandle *pos; 756 struct GNUNET_STATISTICS_GetHandle *pos;
770 struct GNUNET_STATISTICS_GetHandle *next; 757 struct GNUNET_STATISTICS_GetHandle *next;
@@ -772,101 +759,97 @@ GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h,
772 struct GNUNET_TIME_Relative timeout; 759 struct GNUNET_TIME_Relative timeout;
773 int i; 760 int i;
774 761
775 if (h == NULL) 762 if (h == NULL)
776 return; 763 return;
777 if (GNUNET_SCHEDULER_NO_TASK != h->backoff_task) 764 if (GNUNET_SCHEDULER_NO_TASK != h->backoff_task)
778 GNUNET_SCHEDULER_cancel (h->backoff_task); 765 GNUNET_SCHEDULER_cancel (h->backoff_task);
779 if (sync_first) 766 if (sync_first)
767 {
768 if (h->current != NULL)
780 { 769 {
781 if (h->current != NULL) 770 if (h->current->type == ACTION_GET)
782 { 771 {
783 if (h->current->type == ACTION_GET) 772 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
784 { 773 h->th = NULL;
785 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th); 774 free_action_item (h->current);
786 h->th = NULL; 775 h->current = NULL;
787 free_action_item (h->current); 776 }
788 h->current = NULL;
789 }
790 }
791 pos = h->action_head;
792 prev = NULL;
793 while (pos != NULL)
794 {
795 next = pos->next;
796 if (pos->type == ACTION_GET)
797 {
798 if (prev == NULL)
799 h->action_head = next;
800 else
801 prev->next = next;
802 free_action_item (pos);
803 }
804 else
805 {
806 prev = pos;
807 }
808 pos = next;
809 }
810 h->action_tail = prev;
811 if (h->current == NULL)
812 {
813 h->current = h->action_head;
814 if (h->action_head != NULL)
815 {
816 h->action_head = h->action_head->next;
817 if (h->action_head == NULL)
818 h->action_tail = NULL;
819 }
820 }
821 h->do_destroy = GNUNET_YES;
822 if ( (h->current != NULL) &&
823 (h->th == NULL) )
824 {
825 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
826 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
827 h->current->msize,
828 timeout,
829 GNUNET_YES,
830 &transmit_action, h);
831 GNUNET_assert (NULL != h->th);
832 }
833 if (h->th != NULL)
834 return;
835 }
836 if (NULL != h->th)
837 {
838 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
839 h->th = NULL;
840 } 777 }
841 if (h->current != NULL) 778 pos = h->action_head;
842 free_action_item (h->current); 779 prev = NULL;
843 while (NULL != (pos = h->action_head)) 780 while (pos != NULL)
844 { 781 {
845 h->action_head = pos->next; 782 next = pos->next;
846 free_action_item (pos); 783 if (pos->type == ACTION_GET)
784 {
785 if (prev == NULL)
786 h->action_head = next;
787 else
788 prev->next = next;
789 free_action_item (pos);
790 }
791 else
792 {
793 prev = pos;
794 }
795 pos = next;
847 } 796 }
848 if (h->client != NULL) 797 h->action_tail = prev;
798 if (h->current == NULL)
849 { 799 {
850 GNUNET_CLIENT_disconnect (h->client, GNUNET_YES); 800 h->current = h->action_head;
851 h->client = NULL; 801 if (h->action_head != NULL)
802 {
803 h->action_head = h->action_head->next;
804 if (h->action_head == NULL)
805 h->action_tail = NULL;
806 }
852 } 807 }
853 for (i=0;i<h->watches_size;i++) 808 h->do_destroy = GNUNET_YES;
809 if ((h->current != NULL) && (h->th == NULL))
854 { 810 {
855 GNUNET_free (h->watches[i]->subsystem); 811 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
856 GNUNET_free (h->watches[i]->name); 812 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
857 GNUNET_free (h->watches[i]); 813 h->current->msize,
814 timeout,
815 GNUNET_YES,
816 &transmit_action, h);
817 GNUNET_assert (NULL != h->th);
858 } 818 }
859 GNUNET_array_grow (h->watches, 819 if (h->th != NULL)
860 h->watches_size, 820 return;
861 0); 821 }
822 if (NULL != h->th)
823 {
824 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
825 h->th = NULL;
826 }
827 if (h->current != NULL)
828 free_action_item (h->current);
829 while (NULL != (pos = h->action_head))
830 {
831 h->action_head = pos->next;
832 free_action_item (pos);
833 }
834 if (h->client != NULL)
835 {
836 GNUNET_CLIENT_disconnect (h->client, GNUNET_YES);
837 h->client = NULL;
838 }
839 for (i = 0; i < h->watches_size; i++)
840 {
841 GNUNET_free (h->watches[i]->subsystem);
842 GNUNET_free (h->watches[i]->name);
843 GNUNET_free (h->watches[i]);
844 }
845 GNUNET_array_grow (h->watches, h->watches_size, 0);
862 GNUNET_free (h->subsystem); 846 GNUNET_free (h->subsystem);
863 GNUNET_free (h); 847 GNUNET_free (h);
864} 848}
865 849
866 850
867static void 851static void
868finish_task (void *cls, 852finish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
869 const struct GNUNET_SCHEDULER_TaskContext *tc)
870{ 853{
871 struct GNUNET_STATISTICS_Handle *h = cls; 854 struct GNUNET_STATISTICS_Handle *h = cls;
872 855
@@ -886,44 +869,41 @@ schedule_action (struct GNUNET_STATISTICS_Handle *h)
886 if (h->current != NULL) 869 if (h->current != NULL)
887 return; /* action already pending */ 870 return; /* action already pending */
888 if (GNUNET_YES != try_connect (h)) 871 if (GNUNET_YES != try_connect (h))
889 { 872 {
890 h->backoff_task = GNUNET_SCHEDULER_add_delayed (h->backoff, 873 h->backoff_task = GNUNET_SCHEDULER_add_delayed (h->backoff,
891 &finish_task, 874 &finish_task, h);
892 h); 875 h->backoff = GNUNET_TIME_relative_multiply (h->backoff, 2);
893 h->backoff = GNUNET_TIME_relative_multiply (h->backoff, 2); 876 h->backoff = GNUNET_TIME_relative_min (h->backoff,
894 h->backoff = GNUNET_TIME_relative_min (h->backoff, 877 GNUNET_CONSTANTS_SERVICE_TIMEOUT);
895 GNUNET_CONSTANTS_SERVICE_TIMEOUT); 878 return;
896 return; 879 }
897 }
898 880
899 /* schedule next action */ 881 /* schedule next action */
900 h->current = h->action_head; 882 h->current = h->action_head;
901 if (NULL == h->current) 883 if (NULL == h->current)
884 {
885 if (h->do_destroy)
902 { 886 {
903 if (h->do_destroy) 887 h->do_destroy = GNUNET_NO;
904 { 888 GNUNET_STATISTICS_destroy (h, GNUNET_YES);
905 h->do_destroy = GNUNET_NO;
906 GNUNET_STATISTICS_destroy (h, GNUNET_YES);
907 }
908 return;
909 } 889 }
910 GNUNET_CONTAINER_DLL_remove (h->action_head, 890 return;
911 h->action_tail, 891 }
912 h->current); 892 GNUNET_CONTAINER_DLL_remove (h->action_head, h->action_tail, h->current);
913 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout); 893 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
914 if (NULL == 894 if (NULL ==
915 (h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, 895 (h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
916 h->current->msize, 896 h->current->msize,
917 timeout, 897 timeout,
918 GNUNET_YES, 898 GNUNET_YES,
919 &transmit_action, h))) 899 &transmit_action, h)))
920 { 900 {
921#if DEBUG_STATISTICS 901#if DEBUG_STATISTICS
922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
923 "Failed to transmit request to statistics service.\n"); 903 "Failed to transmit request to statistics service.\n");
924#endif 904#endif
925 finish (h, GNUNET_SYSERR); 905 finish (h, GNUNET_SYSERR);
926 } 906 }
927} 907}
928 908
929 909
@@ -956,15 +936,15 @@ GNUNET_STATISTICS_get (struct GNUNET_STATISTICS_Handle *handle,
956 GNUNET_assert (proc != NULL); 936 GNUNET_assert (proc != NULL);
957 GNUNET_assert (GNUNET_NO == handle->do_destroy); 937 GNUNET_assert (GNUNET_NO == handle->do_destroy);
958 if (GNUNET_YES != try_connect (handle)) 938 if (GNUNET_YES != try_connect (handle))
959 { 939 {
960#if DEBUG_STATISTICS 940#if DEBUG_STATISTICS
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962 "Failed to connect to statistics service, can not get value `%s:%s'.\n", 942 "Failed to connect to statistics service, can not get value `%s:%s'.\n",
963 strlen (subsystem) ? subsystem : "*", 943 strlen (subsystem) ? subsystem : "*",
964 strlen (name) ? name : "*"); 944 strlen (name) ? name : "*");
965#endif 945#endif
966 return NULL; 946 return NULL;
967 } 947 }
968 if (subsystem == NULL) 948 if (subsystem == NULL)
969 subsystem = ""; 949 subsystem = "";
970 if (name == NULL) 950 if (name == NULL)
@@ -998,18 +978,16 @@ void
998GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh) 978GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh)
999{ 979{
1000 if (gh->sh->current == gh) 980 if (gh->sh->current == gh)
1001 { 981 {
1002 gh->aborted = GNUNET_YES; 982 gh->aborted = GNUNET_YES;
1003 } 983 }
1004 else 984 else
1005 { 985 {
1006 GNUNET_CONTAINER_DLL_remove (gh->sh->action_head, 986 GNUNET_CONTAINER_DLL_remove (gh->sh->action_head, gh->sh->action_tail, gh);
1007 gh->sh->action_tail, 987 GNUNET_free (gh->name);
1008 gh); 988 GNUNET_free (gh->subsystem);
1009 GNUNET_free (gh->name); 989 GNUNET_free (gh);
1010 GNUNET_free (gh->subsystem); 990 }
1011 GNUNET_free (gh);
1012 }
1013} 991}
1014 992
1015 993
@@ -1027,23 +1005,20 @@ GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh)
1027 */ 1005 */
1028int 1006int
1029GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle, 1007GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
1030 const char *subsystem, 1008 const char *subsystem,
1031 const char *name, 1009 const char *name,
1032 GNUNET_STATISTICS_Iterator proc, 1010 GNUNET_STATISTICS_Iterator proc, void *proc_cls)
1033 void *proc_cls)
1034{ 1011{
1035 struct GNUNET_STATISTICS_WatchEntry *w; 1012 struct GNUNET_STATISTICS_WatchEntry *w;
1036 1013
1037 if (handle == NULL) 1014 if (handle == NULL)
1038 return GNUNET_SYSERR; 1015 return GNUNET_SYSERR;
1039 w = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_WatchEntry)); 1016 w = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_WatchEntry));
1040 w->subsystem = GNUNET_strdup (subsystem); 1017 w->subsystem = GNUNET_strdup (subsystem);
1041 w->name = GNUNET_strdup (name); 1018 w->name = GNUNET_strdup (name);
1042 w->proc = proc; 1019 w->proc = proc;
1043 w->proc_cls = proc_cls; 1020 w->proc_cls = proc_cls;
1044 GNUNET_array_append (handle->watches, 1021 GNUNET_array_append (handle->watches, handle->watches_size, w);
1045 handle->watches_size,
1046 w);
1047 schedule_watch_request (handle, w); 1022 schedule_watch_request (handle, w);
1048 return GNUNET_OK; 1023 return GNUNET_OK;
1049} 1024}
@@ -1052,15 +1027,14 @@ GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
1052static void 1027static void
1053add_setter_action (struct GNUNET_STATISTICS_Handle *h, 1028add_setter_action (struct GNUNET_STATISTICS_Handle *h,
1054 const char *name, 1029 const char *name,
1055 int make_persistent, 1030 int make_persistent, uint64_t value, enum ActionType type)
1056 uint64_t value, enum ActionType type)
1057{ 1031{
1058 struct GNUNET_STATISTICS_GetHandle *ai; 1032 struct GNUNET_STATISTICS_GetHandle *ai;
1059 size_t slen; 1033 size_t slen;
1060 size_t nlen; 1034 size_t nlen;
1061 size_t nsize; 1035 size_t nsize;
1062 int64_t delta; 1036 int64_t delta;
1063 1037
1064 GNUNET_assert (h != NULL); 1038 GNUNET_assert (h != NULL);
1065 GNUNET_assert (name != NULL); 1039 GNUNET_assert (name != NULL);
1066 if (GNUNET_YES != try_connect (h)) 1040 if (GNUNET_YES != try_connect (h))
@@ -1069,59 +1043,58 @@ add_setter_action (struct GNUNET_STATISTICS_Handle *h,
1069 nlen = strlen (name) + 1; 1043 nlen = strlen (name) + 1;
1070 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen; 1044 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
1071 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) 1045 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1072 { 1046 {
1073 GNUNET_break (0); 1047 GNUNET_break (0);
1074 return; 1048 return;
1075 } 1049 }
1076 ai = h->action_head; 1050 ai = h->action_head;
1077 while (ai != NULL) 1051 while (ai != NULL)
1052 {
1053 if ((0 == strcmp (ai->subsystem, h->subsystem)) &&
1054 (0 == strcmp (ai->name, name)) &&
1055 ((ai->type == ACTION_UPDATE) || (ai->type == ACTION_SET)))
1078 { 1056 {
1079 if ( (0 == strcmp (ai->subsystem, h->subsystem)) && 1057 if (ai->type == ACTION_SET)
1080 (0 == strcmp (ai->name, name)) && 1058 {
1081 ( (ai->type == ACTION_UPDATE) || 1059 if (type == ACTION_UPDATE)
1082 (ai->type == ACTION_SET) ) ) 1060 {
1083 { 1061 delta = (int64_t) value;
1084 if (ai->type == ACTION_SET) 1062 if (delta > 0)
1085 { 1063 {
1086 if (type == ACTION_UPDATE) 1064 ai->value += delta;
1087 { 1065 }
1088 delta = (int64_t) value; 1066 else
1089 if (delta > 0) 1067 {
1090 { 1068 if (ai->value < -delta)
1091 ai->value += delta; 1069 ai->value = 0;
1092 } 1070 else
1093 else 1071 ai->value += delta;
1094 { 1072 }
1095 if (ai->value < -delta) 1073 }
1096 ai->value = 0; 1074 else
1097 else 1075 {
1098 ai->value += delta; 1076 ai->value = value;
1099 } 1077 }
1100 } 1078 }
1101 else 1079 else
1102 { 1080 {
1103 ai->value = value; 1081 if (type == ACTION_UPDATE)
1104 } 1082 {
1105 } 1083 delta = (int64_t) value;
1106 else 1084 ai->value += delta;
1107 { 1085 }
1108 if (type == ACTION_UPDATE) 1086 else
1109 { 1087 {
1110 delta = (int64_t) value; 1088 ai->value = value;
1111 ai->value += delta; 1089 ai->type = type;
1112 } 1090 }
1113 else 1091 }
1114 { 1092 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1115 ai->value = value; 1093 ai->make_persistent = make_persistent;
1116 ai->type = type; 1094 return;
1117 }
1118 }
1119 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1120 ai->make_persistent = make_persistent;
1121 return;
1122 }
1123 ai = ai->next;
1124 } 1095 }
1096 ai = ai->next;
1097 }
1125 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle)); 1098 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
1126 ai->sh = h; 1099 ai->sh = h;
1127 ai->subsystem = GNUNET_strdup (h->subsystem); 1100 ai->subsystem = GNUNET_strdup (h->subsystem);
@@ -1146,8 +1119,7 @@ add_setter_action (struct GNUNET_STATISTICS_Handle *h,
1146 */ 1119 */
1147void 1120void
1148GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle, 1121GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
1149 const char *name, 1122 const char *name, uint64_t value, int make_persistent)
1150 uint64_t value, int make_persistent)
1151{ 1123{
1152 if (handle == NULL) 1124 if (handle == NULL)
1153 return; 1125 return;
@@ -1167,8 +1139,7 @@ GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
1167 */ 1139 */
1168void 1140void
1169GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle, 1141GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle,
1170 const char *name, 1142 const char *name, int64_t delta, int make_persistent)
1171 int64_t delta, int make_persistent)
1172{ 1143{
1173 if (handle == NULL) 1144 if (handle == NULL)
1174 return; 1145 return;
diff --git a/src/statistics/test_statistics_api.c b/src/statistics/test_statistics_api.c
index fff2b78b7..de05d6c69 100644
--- a/src/statistics/test_statistics_api.c
+++ b/src/statistics/test_statistics_api.c
@@ -39,10 +39,8 @@ check_1 (void *cls,
39 const char *name, uint64_t value, int is_persistent) 39 const char *name, uint64_t value, int is_persistent)
40{ 40{
41 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 41 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
42 "Received value %llu for `%s:%s\n", 42 "Received value %llu for `%s:%s\n",
43 (unsigned long long) value, 43 (unsigned long long) value, subsystem, name);
44 subsystem,
45 name);
46 GNUNET_assert (0 == strcmp (name, "test-1")); 44 GNUNET_assert (0 == strcmp (name, "test-1"));
47 GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api")); 45 GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
48 GNUNET_assert (value == 1); 46 GNUNET_assert (value == 1);
@@ -56,10 +54,8 @@ check_2 (void *cls,
56 const char *name, uint64_t value, int is_persistent) 54 const char *name, uint64_t value, int is_persistent)
57{ 55{
58 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 56 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
59 "Received value %llu for `%s:%s\n", 57 "Received value %llu for `%s:%s\n",
60 (unsigned long long) value, 58 (unsigned long long) value, subsystem, name);
61 subsystem,
62 name);
63 GNUNET_assert (0 == strcmp (name, "test-2")); 59 GNUNET_assert (0 == strcmp (name, "test-2"));
64 GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api")); 60 GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
65 GNUNET_assert (value == 2); 61 GNUNET_assert (value == 2);
@@ -73,10 +69,8 @@ check_3 (void *cls,
73 const char *name, uint64_t value, int is_persistent) 69 const char *name, uint64_t value, int is_persistent)
74{ 70{
75 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 71 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
76 "Received value %llu for `%s:%s\n", 72 "Received value %llu for `%s:%s\n",
77 (unsigned long long) value, 73 (unsigned long long) value, subsystem, name);
78 subsystem,
79 name);
80 GNUNET_assert (0 == strcmp (name, "test-3")); 74 GNUNET_assert (0 == strcmp (name, "test-3"));
81 GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api")); 75 GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
82 GNUNET_assert (value == 3); 76 GNUNET_assert (value == 3);
@@ -100,54 +94,54 @@ static void
100next (void *cls, int success) 94next (void *cls, int success)
101{ 95{
102 GNUNET_assert (success == GNUNET_OK); 96 GNUNET_assert (success == GNUNET_OK);
103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 97 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Issuing GET request\n");
104 "Issuing GET request\n");
105 GNUNET_break (NULL != 98 GNUNET_break (NULL !=
106 GNUNET_STATISTICS_get (h, NULL, "test-2", 99 GNUNET_STATISTICS_get (h, NULL, "test-2",
107 GNUNET_TIME_UNIT_SECONDS, &next_fin, &check_2, cls)); 100 GNUNET_TIME_UNIT_SECONDS, &next_fin,
101 &check_2, cls));
108} 102}
109 103
110static void 104static void
111run (void *cls, 105run (void *cls,
112 char *const *args, 106 char *const *args,
113 const char *cfgfile, 107 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
114 const struct GNUNET_CONFIGURATION_Handle *cfg)
115{ 108{
116 h = GNUNET_STATISTICS_create ("test-statistics-api", cfg); 109 h = GNUNET_STATISTICS_create ("test-statistics-api", cfg);
117 GNUNET_STATISTICS_set (h, "test-1", 1, GNUNET_NO); 110 GNUNET_STATISTICS_set (h, "test-1", 1, GNUNET_NO);
118 GNUNET_STATISTICS_set (h, "test-2", 2, GNUNET_NO); 111 GNUNET_STATISTICS_set (h, "test-2", 2, GNUNET_NO);
119 GNUNET_STATISTICS_set (h, "test-3", 2, GNUNET_NO); 112 GNUNET_STATISTICS_set (h, "test-3", 2, GNUNET_NO);
120 GNUNET_STATISTICS_update (h, "test-3", 1, GNUNET_YES); 113 GNUNET_STATISTICS_update (h, "test-3", 1, GNUNET_YES);
121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Issuing GET request\n");
122 "Issuing GET request\n"); 115 GNUNET_break (NULL !=
123 GNUNET_break (NULL != 116 GNUNET_STATISTICS_get (h, NULL, "test-1",
124 GNUNET_STATISTICS_get (h, NULL, "test-1", 117 GNUNET_TIME_UNIT_SECONDS, &next,
125 GNUNET_TIME_UNIT_SECONDS, &next, &check_1, cls)); 118 &check_1, cls));
126} 119}
127 120
128static void 121static void
129run_more (void *cls, 122run_more (void *cls,
130 char *const *args, 123 char *const *args,
131 const char *cfgfile, 124 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
132 const struct GNUNET_CONFIGURATION_Handle *cfg)
133{ 125{
134 h = GNUNET_STATISTICS_create ("test-statistics-api", cfg); 126 h = GNUNET_STATISTICS_create ("test-statistics-api", cfg);
135 GNUNET_break (NULL != 127 GNUNET_break (NULL !=
136 GNUNET_STATISTICS_get (h, NULL, "test-3", 128 GNUNET_STATISTICS_get (h, NULL, "test-3",
137 GNUNET_TIME_UNIT_SECONDS, &next_fin, &check_3, cls)); 129 GNUNET_TIME_UNIT_SECONDS, &next_fin,
130 &check_3, cls));
138} 131}
139 132
140static int 133static int
141check () 134check ()
142{ 135{
143 int ok = 1; 136 int ok = 1;
137
144 char *const argv[] = { "test-statistics-api", 138 char *const argv[] = { "test-statistics-api",
145 "-c", 139 "-c",
146 "test_statistics_api_data.conf", 140 "test_statistics_api_data.conf",
147#if DEBUG_STATISTICS 141#if DEBUG_STATISTICS
148 "-L", "DEBUG", 142 "-L", "DEBUG",
149#else 143#else
150 "-L", "WARNING", 144 "-L", "WARNING",
151#endif 145#endif
152 NULL 146 NULL
153 }; 147 };
@@ -156,22 +150,23 @@ check ()
156 }; 150 };
157#if START_SERVICE 151#if START_SERVICE
158 struct GNUNET_OS_Process *proc; 152 struct GNUNET_OS_Process *proc;
153
159 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics", 154 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
160 "gnunet-service-statistics", 155 "gnunet-service-statistics",
161#if DEBUG_STATISTICS 156#if DEBUG_STATISTICS
162 "-L", "DEBUG", 157 "-L", "DEBUG",
163#endif 158#endif
164 "-c", "test_statistics_api_data.conf", NULL); 159 "-c", "test_statistics_api_data.conf", NULL);
165#endif 160#endif
166 GNUNET_assert (NULL != proc); 161 GNUNET_assert (NULL != proc);
167 GNUNET_PROGRAM_run (5, argv, "test-statistics-api", "nohelp", 162 GNUNET_PROGRAM_run (5, argv, "test-statistics-api", "nohelp",
168 options, &run, &ok); 163 options, &run, &ok);
169#if START_SERVICE 164#if START_SERVICE
170 if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) 165 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
171 { 166 {
172 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 167 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
173 ok = 1; 168 ok = 1;
174 } 169 }
175 GNUNET_OS_process_wait (proc); 170 GNUNET_OS_process_wait (proc);
176 GNUNET_OS_process_close (proc); 171 GNUNET_OS_process_close (proc);
177 proc = NULL; 172 proc = NULL;
@@ -182,20 +177,20 @@ check ()
182#if START_SERVICE 177#if START_SERVICE
183 /* restart to check persistence! */ 178 /* restart to check persistence! */
184 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics", 179 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
185 "gnunet-service-statistics", 180 "gnunet-service-statistics",
186#if DEBUG_STATISTICS 181#if DEBUG_STATISTICS
187 "-L", "DEBUG", 182 "-L", "DEBUG",
188#endif 183#endif
189 "-c", "test_statistics_api_data.conf", NULL); 184 "-c", "test_statistics_api_data.conf", NULL);
190#endif 185#endif
191 GNUNET_PROGRAM_run (5, argv, "test-statistics-api", "nohelp", 186 GNUNET_PROGRAM_run (5, argv, "test-statistics-api", "nohelp",
192 options, &run_more, &ok); 187 options, &run_more, &ok);
193#if START_SERVICE 188#if START_SERVICE
194 if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) 189 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
195 { 190 {
196 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 191 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
197 ok = 1; 192 ok = 1;
198 } 193 }
199 GNUNET_OS_process_wait (proc); 194 GNUNET_OS_process_wait (proc);
200 GNUNET_OS_process_close (proc); 195 GNUNET_OS_process_close (proc);
201 proc = NULL; 196 proc = NULL;
@@ -208,13 +203,13 @@ main (int argc, char *argv[])
208{ 203{
209 int ret; 204 int ret;
210 205
211 GNUNET_log_setup ("test_statistics_api", 206 GNUNET_log_setup ("test_statistics_api",
212#if DEBUG_STATISTICS 207#if DEBUG_STATISTICS
213 "DEBUG", 208 "DEBUG",
214#else 209#else
215 "WARNING", 210 "WARNING",
216#endif 211#endif
217 NULL); 212 NULL);
218 ret = check (); 213 ret = check ();
219 214
220 return ret; 215 return ret;
diff --git a/src/statistics/test_statistics_api_loop.c b/src/statistics/test_statistics_api_loop.c
index d969e65ce..87190a99a 100644
--- a/src/statistics/test_statistics_api_loop.c
+++ b/src/statistics/test_statistics_api_loop.c
@@ -61,24 +61,24 @@ next (void *cls, int success)
61static void 61static void
62run (void *cls, 62run (void *cls,
63 char *const *args, 63 char *const *args,
64 const char *cfgfile, 64 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
65 const struct GNUNET_CONFIGURATION_Handle *cfg)
66{ 65{
67 int i; 66 int i;
68 char name[128]; 67 char name[128];
69 68
70 h = GNUNET_STATISTICS_create ("test-statistics-api-loop", cfg); 69 h = GNUNET_STATISTICS_create ("test-statistics-api-loop", cfg);
71 for (i=0;i<ROUNDS;i++) 70 for (i = 0; i < ROUNDS; i++)
72 { 71 {
73 GNUNET_snprintf (name, sizeof (name), "test-%d", i % 256); 72 GNUNET_snprintf (name, sizeof (name), "test-%d", i % 256);
74 GNUNET_STATISTICS_set (h, name, i, GNUNET_NO); 73 GNUNET_STATISTICS_set (h, name, i, GNUNET_NO);
75 GNUNET_snprintf (name, sizeof (name), "test-%d", i % 128); 74 GNUNET_snprintf (name, sizeof (name), "test-%d", i % 128);
76 GNUNET_STATISTICS_update (h, name, 1, GNUNET_NO); 75 GNUNET_STATISTICS_update (h, name, 1, GNUNET_NO);
77 } 76 }
78 i = 0; 77 i = 0;
79 GNUNET_break (NULL != 78 GNUNET_break (NULL !=
80 GNUNET_STATISTICS_get (h, NULL, "test-0", 79 GNUNET_STATISTICS_get (h, NULL, "test-0",
81 GNUNET_TIME_UNIT_MINUTES, &next, &check_1, cls)); 80 GNUNET_TIME_UNIT_MINUTES, &next,
81 &check_1, cls));
82} 82}
83 83
84 84
@@ -86,6 +86,7 @@ static int
86check () 86check ()
87{ 87{
88 int ok = 1; 88 int ok = 1;
89
89 char *const argv[] = { "test-statistics-api", 90 char *const argv[] = { "test-statistics-api",
90 "-c", 91 "-c",
91 "test_statistics_api_data.conf", 92 "test_statistics_api_data.conf",
@@ -96,22 +97,23 @@ check ()
96 }; 97 };
97#if START_SERVICE 98#if START_SERVICE
98 struct GNUNET_OS_Process *proc; 99 struct GNUNET_OS_Process *proc;
100
99 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics", 101 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
100 "gnunet-service-statistics", 102 "gnunet-service-statistics",
101#if DEBUG_STATISTICS 103#if DEBUG_STATISTICS
102 "-L", "DEBUG", 104 "-L", "DEBUG",
103#endif 105#endif
104 "-c", "test_statistics_api_data.conf", NULL); 106 "-c", "test_statistics_api_data.conf", NULL);
105#endif 107#endif
106 GNUNET_assert (NULL != proc); 108 GNUNET_assert (NULL != proc);
107 GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp", 109 GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp",
108 options, &run, &ok); 110 options, &run, &ok);
109#if START_SERVICE 111#if START_SERVICE
110 if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) 112 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
111 { 113 {
112 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 114 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
113 ok = 1; 115 ok = 1;
114 } 116 }
115 GNUNET_OS_process_wait (proc); 117 GNUNET_OS_process_wait (proc);
116 GNUNET_OS_process_close (proc); 118 GNUNET_OS_process_close (proc);
117 proc = NULL; 119 proc = NULL;
diff --git a/src/statistics/test_statistics_api_watch.c b/src/statistics/test_statistics_api_watch.c
index 4c44bd0ba..72cb27ecf 100644
--- a/src/statistics/test_statistics_api_watch.c
+++ b/src/statistics/test_statistics_api_watch.c
@@ -45,8 +45,7 @@ static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
45 45
46 46
47static void 47static void
48force_shutdown (void *cls, 48force_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
49 const struct GNUNET_SCHEDULER_TaskContext *tc)
50{ 49{
51 fprintf (stderr, "Timeout, failed to receive notifications: %d\n", ok); 50 fprintf (stderr, "Timeout, failed to receive notifications: %d\n", ok);
52 GNUNET_STATISTICS_destroy (h, GNUNET_NO); 51 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
@@ -56,8 +55,7 @@ force_shutdown (void *cls,
56 55
57 56
58static void 57static void
59normal_shutdown (void *cls, 58normal_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
60 const struct GNUNET_SCHEDULER_TaskContext *tc)
61{ 59{
62 GNUNET_STATISTICS_destroy (h, GNUNET_NO); 60 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
63 GNUNET_STATISTICS_destroy (h2, GNUNET_NO); 61 GNUNET_STATISTICS_destroy (h2, GNUNET_NO);
@@ -65,39 +63,35 @@ normal_shutdown (void *cls,
65 63
66 64
67static int 65static int
68watch_1 (void *cls, 66watch_1 (void *cls,
69 const char *subsystem, 67 const char *subsystem,
70 const char *name, 68 const char *name, uint64_t value, int is_persistent)
71 uint64_t value,
72 int is_persistent)
73{ 69{
74 GNUNET_assert (value == 42); 70 GNUNET_assert (value == 42);
75 GNUNET_assert (0 == strcmp (name, "test-1")); 71 GNUNET_assert (0 == strcmp (name, "test-1"));
76 ok &= ~1; 72 ok &= ~1;
77 if (0 == ok) 73 if (0 == ok)
78 { 74 {
79 GNUNET_SCHEDULER_cancel (shutdown_task); 75 GNUNET_SCHEDULER_cancel (shutdown_task);
80 GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL); 76 GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
81 } 77 }
82 return GNUNET_OK; 78 return GNUNET_OK;
83} 79}
84 80
85 81
86static int 82static int
87watch_2 (void *cls, 83watch_2 (void *cls,
88 const char *subsystem, 84 const char *subsystem,
89 const char *name, 85 const char *name, uint64_t value, int is_persistent)
90 uint64_t value,
91 int is_persistent)
92{ 86{
93 GNUNET_assert (value == 43); 87 GNUNET_assert (value == 43);
94 GNUNET_assert (0 == strcmp (name, "test-2")); 88 GNUNET_assert (0 == strcmp (name, "test-2"));
95 ok &= ~2; 89 ok &= ~2;
96 if (0 == ok) 90 if (0 == ok)
97 { 91 {
98 GNUNET_SCHEDULER_cancel (shutdown_task); 92 GNUNET_SCHEDULER_cancel (shutdown_task);
99 GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL); 93 GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
100 } 94 }
101 return GNUNET_OK; 95 return GNUNET_OK;
102} 96}
103 97
@@ -105,27 +99,22 @@ watch_2 (void *cls,
105static void 99static void
106run (void *cls, 100run (void *cls,
107 char *const *args, 101 char *const *args,
108 const char *cfgfile, 102 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
109 const struct GNUNET_CONFIGURATION_Handle *cfg)
110{ 103{
111 h = GNUNET_STATISTICS_create ("dummy", cfg); 104 h = GNUNET_STATISTICS_create ("dummy", cfg);
112 GNUNET_assert (GNUNET_OK == 105 GNUNET_assert (GNUNET_OK ==
113 GNUNET_STATISTICS_watch (h, 106 GNUNET_STATISTICS_watch (h,
114 "test-statistics-api-watch", 107 "test-statistics-api-watch",
115 "test-1", 108 "test-1", &watch_1, NULL));
116 &watch_1,
117 NULL));
118 GNUNET_assert (GNUNET_OK == 109 GNUNET_assert (GNUNET_OK ==
119 GNUNET_STATISTICS_watch (h, 110 GNUNET_STATISTICS_watch (h,
120 "test-statistics-api-watch", 111 "test-statistics-api-watch",
121 "test-2", 112 "test-2", &watch_2, NULL));
122 &watch_2,
123 NULL));
124 h2 = GNUNET_STATISTICS_create ("test-statistics-api-watch", cfg); 113 h2 = GNUNET_STATISTICS_create ("test-statistics-api-watch", cfg);
125 GNUNET_STATISTICS_set (h2, "test-1", 42, GNUNET_NO); 114 GNUNET_STATISTICS_set (h2, "test-1", 42, GNUNET_NO);
126 GNUNET_STATISTICS_set (h2, "test-2", 43, GNUNET_NO); 115 GNUNET_STATISTICS_set (h2, "test-2", 43, GNUNET_NO);
127 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, 116 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
128 &force_shutdown, NULL); 117 &force_shutdown, NULL);
129} 118}
130 119
131 120
@@ -142,12 +131,13 @@ check ()
142 }; 131 };
143#if START_SERVICE 132#if START_SERVICE
144 struct GNUNET_OS_Process *proc; 133 struct GNUNET_OS_Process *proc;
134
145 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics", 135 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
146 "gnunet-service-statistics", 136 "gnunet-service-statistics",
147#if VERBOSE 137#if VERBOSE
148 "-L", "DEBUG", 138 "-L", "DEBUG",
149#endif 139#endif
150 "-c", "test_statistics_api_data.conf", NULL); 140 "-c", "test_statistics_api_data.conf", NULL);
151#endif 141#endif
152 GNUNET_assert (NULL != proc); 142 GNUNET_assert (NULL != proc);
153 ok = 3; 143 ok = 3;
@@ -155,10 +145,10 @@ check ()
155 options, &run, NULL); 145 options, &run, NULL);
156#if START_SERVICE 146#if START_SERVICE
157 if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) 147 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
158 { 148 {
159 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 149 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
160 ok = 1; 150 ok = 1;
161 } 151 }
162 GNUNET_OS_process_wait (proc); 152 GNUNET_OS_process_wait (proc);
163 GNUNET_OS_process_close (proc); 153 GNUNET_OS_process_close (proc);
164 proc = NULL; 154 proc = NULL;