aboutsummaryrefslogtreecommitdiff
path: root/src/peerstore/peerstore_api.c
diff options
context:
space:
mode:
authorOmar Tarabai <tarabai@devegypt.com>2014-08-04 11:06:52 +0000
committerOmar Tarabai <tarabai@devegypt.com>2014-08-04 11:06:52 +0000
commit4371b0fcd9b50f78ca27ac48adf2f4663b2dfc9b (patch)
tree05c40696267088fa94cdb0a94b3c079b38ea8ed2 /src/peerstore/peerstore_api.c
parentef494e57d189e2b7c038104e44398d11026ff6a6 (diff)
downloadgnunet-4371b0fcd9b50f78ca27ac48adf2f4663b2dfc9b.tar.gz
gnunet-4371b0fcd9b50f78ca27ac48adf2f4663b2dfc9b.zip
peerstore: indentation
Diffstat (limited to 'src/peerstore/peerstore_api.c')
-rw-r--r--src/peerstore/peerstore_api.c369
1 files changed, 192 insertions, 177 deletions
diff --git a/src/peerstore/peerstore_api.c b/src/peerstore/peerstore_api.c
index 3c67846e6..bebf9678e 100644
--- a/src/peerstore/peerstore_api.c
+++ b/src/peerstore/peerstore_api.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 3 (C)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published
@@ -256,30 +256,34 @@ reconnect (struct GNUNET_PEERSTORE_Handle *h);
256 * 256 *
257 * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *' 257 * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *'
258 */ 258 */
259static void watch_request_sent (void *cls); 259static void
260watch_request_sent (void *cls);
260 261
261/** 262/**
262 * Callback after MQ envelope is sent 263 * Callback after MQ envelope is sent
263 * 264 *
264 * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *' 265 * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *'
265 */ 266 */
266static void iterate_request_sent (void *cls); 267static void
268iterate_request_sent (void *cls);
267 269
268/** 270/**
269 * Callback after MQ envelope is sent 271 * Callback after MQ envelope is sent
270 * 272 *
271 * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *' 273 * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *'
272 */ 274 */
273static void store_request_sent (void *cls); 275static void
276store_request_sent (void *cls);
274 277
275/** 278/**
276 * MQ message handlers 279 * MQ message handlers
277 */ 280 */
278static const struct GNUNET_MQ_MessageHandler mq_handlers[] = { 281static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
279 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD, 0}, 282 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD, 0},
280 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END, sizeof(struct GNUNET_MessageHeader)}, 283 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END,
281 {&handle_watch_result, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD, 0}, 284 sizeof (struct GNUNET_MessageHeader)},
282 GNUNET_MQ_HANDLERS_END 285 {&handle_watch_result, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD, 0},
286 GNUNET_MQ_HANDLERS_END
283}; 287};
284 288
285/******************************************************************************/ 289/******************************************************************************/
@@ -291,32 +295,34 @@ handle_client_error (void *cls, enum GNUNET_MQ_Error error)
291{ 295{
292 struct GNUNET_PEERSTORE_Handle *h = cls; 296 struct GNUNET_PEERSTORE_Handle *h = cls;
293 297
294 LOG(GNUNET_ERROR_TYPE_ERROR, _("Received an error notification from MQ of type: %d\n"), error); 298 LOG (GNUNET_ERROR_TYPE_ERROR,
295 reconnect(h); 299 _("Received an error notification from MQ of type: %d\n"), error);
300 reconnect (h);
296} 301}
297 302
303
298/** 304/**
299 * Iterator over previous watches to resend them 305 * Iterator over previous watches to resend them
300 */ 306 */
301static int rewatch_it(void *cls, 307static int
302 const struct GNUNET_HashCode *key, 308rewatch_it (void *cls, const struct GNUNET_HashCode *key, void *value)
303 void *value)
304{ 309{
305 struct GNUNET_PEERSTORE_Handle *h = cls; 310 struct GNUNET_PEERSTORE_Handle *h = cls;
306 struct GNUNET_PEERSTORE_WatchContext *wc = value; 311 struct GNUNET_PEERSTORE_WatchContext *wc = value;
307 struct StoreKeyHashMessage *hm; 312 struct StoreKeyHashMessage *hm;
308 313
309 if(GNUNET_YES == wc->request_sent) 314 if (GNUNET_YES == wc->request_sent)
310 { /* Envelope gone, create new one. */ 315 { /* Envelope gone, create new one. */
311 wc->ev = GNUNET_MQ_msg(hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH); 316 wc->ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
312 hm->keyhash = wc->keyhash; 317 hm->keyhash = wc->keyhash;
313 wc->request_sent = GNUNET_NO; 318 wc->request_sent = GNUNET_NO;
314 } 319 }
315 GNUNET_MQ_notify_sent(wc->ev, &watch_request_sent, wc); 320 GNUNET_MQ_notify_sent (wc->ev, &watch_request_sent, wc);
316 GNUNET_MQ_send(h->mq, wc->ev); 321 GNUNET_MQ_send (h->mq, wc->ev);
317 return GNUNET_YES; 322 return GNUNET_YES;
318} 323}
319 324
325
320/** 326/**
321 * Close the existing connection to PEERSTORE and reconnect. 327 * Close the existing connection to PEERSTORE and reconnect.
322 * 328 *
@@ -330,10 +336,10 @@ reconnect (struct GNUNET_PEERSTORE_Handle *h)
330 void *icb_cls; 336 void *icb_cls;
331 struct GNUNET_PEERSTORE_StoreContext *sc; 337 struct GNUNET_PEERSTORE_StoreContext *sc;
332 338
333 LOG(GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n"); 339 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n");
334 if (NULL != h->mq) 340 if (NULL != h->mq)
335 { 341 {
336 GNUNET_MQ_destroy(h->mq); 342 GNUNET_MQ_destroy (h->mq);
337 h->mq = NULL; 343 h->mq = NULL;
338 } 344 }
339 if (NULL != h->client) 345 if (NULL != h->client)
@@ -342,17 +348,15 @@ reconnect (struct GNUNET_PEERSTORE_Handle *h)
342 h->client = NULL; 348 h->client = NULL;
343 } 349 }
344 h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg); 350 h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg);
345 GNUNET_assert(NULL != h->client); 351 GNUNET_assert (NULL != h->client);
346 h->mq = GNUNET_MQ_queue_for_connection_client(h->client, 352 h->mq =
347 mq_handlers, 353 GNUNET_MQ_queue_for_connection_client (h->client, mq_handlers,
348 &handle_client_error, 354 &handle_client_error, h);
349 h); 355 LOG (GNUNET_ERROR_TYPE_DEBUG,
350 LOG(GNUNET_ERROR_TYPE_DEBUG, 356 "Resending pending requests after reconnect.\n");
351 "Resending pending requests after reconnect.\n");
352 if (NULL != h->watches) 357 if (NULL != h->watches)
353 { 358 {
354 GNUNET_CONTAINER_multihashmap_iterate(h->watches, 359 GNUNET_CONTAINER_multihashmap_iterate (h->watches, &rewatch_it, h);
355 &rewatch_it, h);
356 } 360 }
357 ic = h->iterate_head; 361 ic = h->iterate_head;
358 while (NULL != ic) 362 while (NULL != ic)
@@ -361,26 +365,27 @@ reconnect (struct GNUNET_PEERSTORE_Handle *h)
361 { 365 {
362 icb = ic->callback; 366 icb = ic->callback;
363 icb_cls = ic->callback_cls; 367 icb_cls = ic->callback_cls;
364 GNUNET_PEERSTORE_iterate_cancel(ic); 368 GNUNET_PEERSTORE_iterate_cancel (ic);
365 if(NULL != icb) 369 if (NULL != icb)
366 icb(icb_cls, NULL,_("Iteration canceled due to reconnection.")); 370 icb (icb_cls, NULL, _("Iteration canceled due to reconnection."));
367 } 371 }
368 else 372 else
369 { 373 {
370 GNUNET_MQ_notify_sent(ic->ev, &iterate_request_sent, ic); 374 GNUNET_MQ_notify_sent (ic->ev, &iterate_request_sent, ic);
371 GNUNET_MQ_send(h->mq, ic->ev); 375 GNUNET_MQ_send (h->mq, ic->ev);
372 } 376 }
373 ic = ic->next; 377 ic = ic->next;
374 } 378 }
375 sc = h->store_head; 379 sc = h->store_head;
376 while (NULL != sc) 380 while (NULL != sc)
377 { 381 {
378 GNUNET_MQ_notify_sent(sc->ev, &store_request_sent, sc); 382 GNUNET_MQ_notify_sent (sc->ev, &store_request_sent, sc);
379 GNUNET_MQ_send(h->mq, sc->ev); 383 GNUNET_MQ_send (h->mq, sc->ev);
380 sc = sc->next; 384 sc = sc->next;
381 } 385 }
382} 386}
383 387
388
384/** 389/**
385 * Iterator over watch requests to cancel them. 390 * Iterator over watch requests to cancel them.
386 * 391 *
@@ -398,6 +403,7 @@ destroy_watch (void *cls, const struct GNUNET_HashCode *key, void *value)
398 return GNUNET_YES; 403 return GNUNET_YES;
399} 404}
400 405
406
401/** 407/**
402 * Kill the connection to the service. This can be delayed in case of pending 408 * Kill the connection to the service. This can be delayed in case of pending
403 * STORE requests and the user explicitly asked to sync first. Otherwise it is 409 * STORE requests and the user explicitly asked to sync first. Otherwise it is
@@ -405,11 +411,12 @@ destroy_watch (void *cls, const struct GNUNET_HashCode *key, void *value)
405 * 411 *
406 * @param h Handle to the service. 412 * @param h Handle to the service.
407 */ 413 */
408static void do_disconnect (struct GNUNET_PEERSTORE_Handle *h) 414static void
415do_disconnect (struct GNUNET_PEERSTORE_Handle *h)
409{ 416{
410 if(NULL != h->mq) 417 if (NULL != h->mq)
411 { 418 {
412 GNUNET_MQ_destroy(h->mq); 419 GNUNET_MQ_destroy (h->mq);
413 h->mq = NULL; 420 h->mq = NULL;
414 } 421 }
415 if (NULL != h->client) 422 if (NULL != h->client)
@@ -417,9 +424,10 @@ static void do_disconnect (struct GNUNET_PEERSTORE_Handle *h)
417 GNUNET_CLIENT_disconnect (h->client); 424 GNUNET_CLIENT_disconnect (h->client);
418 h->client = NULL; 425 h->client = NULL;
419 } 426 }
420 GNUNET_free(h); 427 GNUNET_free (h);
421} 428}
422 429
430
423/** 431/**
424 * Connect to the PEERSTORE service. 432 * Connect to the PEERSTORE service.
425 * 433 *
@@ -431,27 +439,28 @@ GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
431 struct GNUNET_PEERSTORE_Handle *h; 439 struct GNUNET_PEERSTORE_Handle *h;
432 440
433 h = GNUNET_new (struct GNUNET_PEERSTORE_Handle); 441 h = GNUNET_new (struct GNUNET_PEERSTORE_Handle);
442
434 h->client = GNUNET_CLIENT_connect ("peerstore", cfg); 443 h->client = GNUNET_CLIENT_connect ("peerstore", cfg);
435 if(NULL == h->client) 444 if (NULL == h->client)
436 { 445 {
437 GNUNET_free(h); 446 GNUNET_free (h);
438 return NULL; 447 return NULL;
439 } 448 }
440 h->cfg = cfg; 449 h->cfg = cfg;
441 h->disconnecting = GNUNET_NO; 450 h->disconnecting = GNUNET_NO;
442 h->mq = GNUNET_MQ_queue_for_connection_client(h->client, 451 h->mq =
443 mq_handlers, 452 GNUNET_MQ_queue_for_connection_client (h->client, mq_handlers,
444 &handle_client_error, 453 &handle_client_error, h);
445 h); 454 if (NULL == h->mq)
446 if(NULL == h->mq)
447 { 455 {
448 GNUNET_free(h); 456 GNUNET_free (h);
449 return NULL; 457 return NULL;
450 } 458 }
451 LOG(GNUNET_ERROR_TYPE_DEBUG, "New connection created\n"); 459 LOG (GNUNET_ERROR_TYPE_DEBUG, "New connection created\n");
452 return h; 460 return h;
453} 461}
454 462
463
455/** 464/**
456 * Disconnect from the PEERSTORE service. Any pending ITERATE and WATCH requests 465 * Disconnect from the PEERSTORE service. Any pending ITERATE and WATCH requests
457 * will be canceled. 466 * will be canceled.
@@ -468,11 +477,11 @@ GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first)
468 struct GNUNET_PEERSTORE_StoreContext *sc; 477 struct GNUNET_PEERSTORE_StoreContext *sc;
469 struct GNUNET_PEERSTORE_StoreContext *sc_iter; 478 struct GNUNET_PEERSTORE_StoreContext *sc_iter;
470 479
471 LOG(GNUNET_ERROR_TYPE_DEBUG, "Disconnecting.\n"); 480 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting.\n");
472 if(NULL != h->watches) 481 if (NULL != h->watches)
473 { 482 {
474 GNUNET_CONTAINER_multihashmap_iterate (h->watches, &destroy_watch, NULL); 483 GNUNET_CONTAINER_multihashmap_iterate (h->watches, &destroy_watch, NULL);
475 GNUNET_CONTAINER_multihashmap_destroy(h->watches); 484 GNUNET_CONTAINER_multihashmap_destroy (h->watches);
476 h->watches = NULL; 485 h->watches = NULL;
477 } 486 }
478 ic_iter = h->iterate_head; 487 ic_iter = h->iterate_head;
@@ -487,7 +496,7 @@ GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first)
487 if (GNUNET_YES == sync_first) 496 if (GNUNET_YES == sync_first)
488 { 497 {
489 LOG (GNUNET_ERROR_TYPE_DEBUG, 498 LOG (GNUNET_ERROR_TYPE_DEBUG,
490 "Delaying disconnection due to pending store requests.\n"); 499 "Delaying disconnection due to pending store requests.\n");
491 h->disconnecting = GNUNET_YES; 500 h->disconnecting = GNUNET_YES;
492 return; 501 return;
493 } 502 }
@@ -512,7 +521,8 @@ GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first)
512 * 521 *
513 * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *' 522 * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *'
514 */ 523 */
515static void store_request_sent (void *cls) 524static void
525store_request_sent (void *cls)
516{ 526{
517 struct GNUNET_PEERSTORE_StoreContext *sc = cls; 527 struct GNUNET_PEERSTORE_StoreContext *sc = cls;
518 GNUNET_PEERSTORE_Continuation cont; 528 GNUNET_PEERSTORE_Continuation cont;
@@ -521,11 +531,12 @@ static void store_request_sent (void *cls)
521 sc->ev = NULL; 531 sc->ev = NULL;
522 cont = sc->cont; 532 cont = sc->cont;
523 cont_cls = sc->cont_cls; 533 cont_cls = sc->cont_cls;
524 GNUNET_PEERSTORE_store_cancel(sc); 534 GNUNET_PEERSTORE_store_cancel (sc);
525 if(NULL != cont) 535 if (NULL != cont)
526 cont(cont_cls, GNUNET_OK); 536 cont (cont_cls, GNUNET_OK);
527} 537}
528 538
539
529/** 540/**
530 * Cancel a store request 541 * Cancel a store request
531 * 542 *
@@ -536,17 +547,18 @@ GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
536{ 547{
537 struct GNUNET_PEERSTORE_Handle *h = sc->h; 548 struct GNUNET_PEERSTORE_Handle *h = sc->h;
538 549
539 if(NULL != sc->ev) 550 if (NULL != sc->ev)
540 { 551 {
541 GNUNET_MQ_send_cancel(sc->ev); 552 GNUNET_MQ_send_cancel (sc->ev);
542 sc->ev = NULL; 553 sc->ev = NULL;
543 } 554 }
544 GNUNET_CONTAINER_DLL_remove(sc->h->store_head, sc->h->store_tail, sc); 555 GNUNET_CONTAINER_DLL_remove (sc->h->store_head, sc->h->store_tail, sc);
545 GNUNET_free(sc); 556 GNUNET_free (sc);
546 if (GNUNET_YES == h->disconnecting && NULL == h->store_head) 557 if (GNUNET_YES == h->disconnecting && NULL == h->store_head)
547 do_disconnect (h); 558 do_disconnect (h);
548} 559}
549 560
561
550/** 562/**
551 * Store a new entry in the PEERSTORE. 563 * Store a new entry in the PEERSTORE.
552 * Note that stored entries can be lost in some cases 564 * Note that stored entries can be lost in some cases
@@ -566,41 +578,35 @@ GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
566struct GNUNET_PEERSTORE_StoreContext * 578struct GNUNET_PEERSTORE_StoreContext *
567GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h, 579GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
568 const char *sub_system, 580 const char *sub_system,
569 const struct GNUNET_PeerIdentity *peer, 581 const struct GNUNET_PeerIdentity *peer, const char *key,
570 const char *key, 582 const void *value, size_t size,
571 const void *value,
572 size_t size,
573 struct GNUNET_TIME_Absolute expiry, 583 struct GNUNET_TIME_Absolute expiry,
574 enum GNUNET_PEERSTORE_StoreOption options, 584 enum GNUNET_PEERSTORE_StoreOption options,
575 GNUNET_PEERSTORE_Continuation cont, 585 GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
576 void *cont_cls)
577{ 586{
578 struct GNUNET_MQ_Envelope *ev; 587 struct GNUNET_MQ_Envelope *ev;
579 struct GNUNET_PEERSTORE_StoreContext *sc; 588 struct GNUNET_PEERSTORE_StoreContext *sc;
580 589
581 LOG (GNUNET_ERROR_TYPE_DEBUG, 590 LOG (GNUNET_ERROR_TYPE_DEBUG,
582 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n", 591 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n",
583 size, sub_system, GNUNET_i2s (peer), key); 592 size, sub_system, GNUNET_i2s (peer), key);
584 ev = PEERSTORE_create_record_mq_envelope(sub_system, 593 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, value, size,
585 peer, 594 &expiry, options,
586 key, 595 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
587 value, 596 sc = GNUNET_new (struct GNUNET_PEERSTORE_StoreContext);
588 size, 597
589 &expiry,
590 options,
591 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
592 sc = GNUNET_new(struct GNUNET_PEERSTORE_StoreContext);
593 sc->ev = ev; 598 sc->ev = ev;
594 sc->cont = cont; 599 sc->cont = cont;
595 sc->cont_cls = cont_cls; 600 sc->cont_cls = cont_cls;
596 sc->h = h; 601 sc->h = h;
597 GNUNET_CONTAINER_DLL_insert_tail(h->store_head, h->store_tail, sc); 602 GNUNET_CONTAINER_DLL_insert_tail (h->store_head, h->store_tail, sc);
598 GNUNET_MQ_notify_sent(ev, &store_request_sent, sc); 603 GNUNET_MQ_notify_sent (ev, &store_request_sent, sc);
599 GNUNET_MQ_send(h->mq, ev); 604 GNUNET_MQ_send (h->mq, ev);
600 return sc; 605 return sc;
601 606
602} 607}
603 608
609
604/******************************************************************************/ 610/******************************************************************************/
605/******************* ITERATE FUNCTIONS *********************/ 611/******************* ITERATE FUNCTIONS *********************/
606/******************************************************************************/ 612/******************************************************************************/
@@ -623,62 +629,67 @@ handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg)
623 int continue_iter; 629 int continue_iter;
624 630
625 ic = h->iterate_head; 631 ic = h->iterate_head;
626 if(NULL == ic) 632 if (NULL == ic)
627 { 633 {
628 LOG(GNUNET_ERROR_TYPE_ERROR, _("Unexpected iteration response, this should not happen.\n")); 634 LOG (GNUNET_ERROR_TYPE_ERROR,
629 reconnect(h); 635 _("Unexpected iteration response, this should not happen.\n"));
636 reconnect (h);
630 return; 637 return;
631 } 638 }
632 callback = ic->callback; 639 callback = ic->callback;
633 callback_cls = ic->callback_cls; 640 callback_cls = ic->callback_cls;
634 if(NULL == msg) /* Connection error */ 641 if (NULL == msg) /* Connection error */
635 { 642 {
636 643
637 if(NULL != callback) 644 if (NULL != callback)
638 callback(callback_cls, NULL, 645 callback (callback_cls, NULL,
639 _("Error communicating with `PEERSTORE' service.")); 646 _("Error communicating with `PEERSTORE' service."));
640 reconnect(h); 647 reconnect (h);
641 return; 648 return;
642 } 649 }
643 msg_type = ntohs(msg->type); 650 msg_type = ntohs (msg->type);
644 if(GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END == msg_type) 651 if (GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END == msg_type)
645 { 652 {
646 ic->request_sent = GNUNET_NO; 653 ic->request_sent = GNUNET_NO;
647 GNUNET_PEERSTORE_iterate_cancel(ic); 654 GNUNET_PEERSTORE_iterate_cancel (ic);
648 if(NULL != callback) 655 if (NULL != callback)
649 callback(callback_cls, NULL, NULL); 656 callback (callback_cls, NULL, NULL);
650 return; 657 return;
651 } 658 }
652 if(NULL != callback) 659 if (NULL != callback)
653 { 660 {
654 record = PEERSTORE_parse_record_message(msg); 661 record = PEERSTORE_parse_record_message (msg);
655 if(NULL == record) 662 if (NULL == record)
656 continue_iter = callback(callback_cls, NULL, _("Received a malformed response from service.")); 663 continue_iter =
664 callback (callback_cls, NULL,
665 _("Received a malformed response from service."));
657 else 666 else
658 { 667 {
659 continue_iter = callback(callback_cls, record, NULL); 668 continue_iter = callback (callback_cls, record, NULL);
660 PEERSTORE_destroy_record(record); 669 PEERSTORE_destroy_record (record);
661 } 670 }
662 if(GNUNET_NO == continue_iter) 671 if (GNUNET_NO == continue_iter)
663 ic->callback = NULL; 672 ic->callback = NULL;
664 } 673 }
665
666} 674}
667 675
676
668/** 677/**
669 * Callback after MQ envelope is sent 678 * Callback after MQ envelope is sent
670 * 679 *
671 * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *' 680 * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *'
672 */ 681 */
673static void iterate_request_sent (void *cls) 682static void
683iterate_request_sent (void *cls)
674{ 684{
675 struct GNUNET_PEERSTORE_IterateContext *ic = cls; 685 struct GNUNET_PEERSTORE_IterateContext *ic = cls;
676 686
677 LOG(GNUNET_ERROR_TYPE_DEBUG, "Iterate request sent to service.\n"); 687 LOG (GNUNET_ERROR_TYPE_DEBUG, "Iterate request sent to service.\n");
678 ic->request_sent = GNUNET_YES; 688 ic->request_sent = GNUNET_YES;
679 ic->ev = NULL; 689 ic->ev = NULL;
680} 690}
681 691
692
682/** 693/**
683 * Called when the iterate request is timedout 694 * Called when the iterate request is timedout
684 * 695 *
@@ -691,9 +702,10 @@ iterate_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
691 struct GNUNET_PEERSTORE_IterateContext *ic = cls; 702 struct GNUNET_PEERSTORE_IterateContext *ic = cls;
692 703
693 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK; 704 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
694 GNUNET_PEERSTORE_iterate_cancel(ic); 705 GNUNET_PEERSTORE_iterate_cancel (ic);
695} 706}
696 707
708
697/** 709/**
698 * Cancel an iterate request 710 * Cancel an iterate request
699 * Please do not call after the iterate request is done 711 * Please do not call after the iterate request is done
@@ -703,25 +715,26 @@ iterate_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
703void 715void
704GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic) 716GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic)
705{ 717{
706 if(GNUNET_SCHEDULER_NO_TASK != ic->timeout_task) 718 if (GNUNET_SCHEDULER_NO_TASK != ic->timeout_task)
707 { 719 {
708 GNUNET_SCHEDULER_cancel(ic->timeout_task); 720 GNUNET_SCHEDULER_cancel (ic->timeout_task);
709 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK; 721 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
710 } 722 }
711 if(GNUNET_NO == ic->request_sent) 723 if (GNUNET_NO == ic->request_sent)
712 { 724 {
713 if(NULL != ic->ev) 725 if (NULL != ic->ev)
714 { 726 {
715 GNUNET_MQ_send_cancel(ic->ev); 727 GNUNET_MQ_send_cancel (ic->ev);
716 ic->ev = NULL; 728 ic->ev = NULL;
717 } 729 }
718 GNUNET_CONTAINER_DLL_remove(ic->h->iterate_head, ic->h->iterate_tail, ic); 730 GNUNET_CONTAINER_DLL_remove (ic->h->iterate_head, ic->h->iterate_tail, ic);
719 GNUNET_free(ic); 731 GNUNET_free (ic);
720 } 732 }
721 else 733 else
722 ic->callback = NULL; 734 ic->callback = NULL;
723} 735}
724 736
737
725/** 738/**
726 * Iterate over records matching supplied key information 739 * Iterate over records matching supplied key information
727 * 740 *
@@ -736,38 +749,36 @@ GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic)
736 */ 749 */
737struct GNUNET_PEERSTORE_IterateContext * 750struct GNUNET_PEERSTORE_IterateContext *
738GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h, 751GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
739 const char *sub_system, 752 const char *sub_system,
740 const struct GNUNET_PeerIdentity *peer, 753 const struct GNUNET_PeerIdentity *peer,
741 const char *key, 754 const char *key, struct GNUNET_TIME_Relative timeout,
742 struct GNUNET_TIME_Relative timeout, 755 GNUNET_PEERSTORE_Processor callback,
743 GNUNET_PEERSTORE_Processor callback, void *callback_cls) 756 void *callback_cls)
744{ 757{
745 struct GNUNET_MQ_Envelope *ev; 758 struct GNUNET_MQ_Envelope *ev;
746 struct GNUNET_PEERSTORE_IterateContext *ic; 759 struct GNUNET_PEERSTORE_IterateContext *ic;
747 760
748 ev = PEERSTORE_create_record_mq_envelope(sub_system, 761 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, NULL, 0,
749 peer, 762 NULL, 0,
750 key, 763 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
751 NULL, 764 ic = GNUNET_new (struct GNUNET_PEERSTORE_IterateContext);
752 0, 765
753 NULL,
754 0,
755 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
756 ic = GNUNET_new(struct GNUNET_PEERSTORE_IterateContext);
757 ic->callback = callback; 766 ic->callback = callback;
758 ic->callback_cls = callback_cls; 767 ic->callback_cls = callback_cls;
759 ic->ev = ev; 768 ic->ev = ev;
760 ic->h = h; 769 ic->h = h;
761 ic->request_sent = GNUNET_NO; 770 ic->request_sent = GNUNET_NO;
762 GNUNET_CONTAINER_DLL_insert_tail(h->iterate_head, h->iterate_tail, ic); 771 GNUNET_CONTAINER_DLL_insert_tail (h->iterate_head, h->iterate_tail, ic);
763 LOG(GNUNET_ERROR_TYPE_DEBUG, 772 LOG (GNUNET_ERROR_TYPE_DEBUG,
764 "Sending an iterate request for sub system `%s'\n", sub_system); 773 "Sending an iterate request for sub system `%s'\n", sub_system);
765 GNUNET_MQ_notify_sent(ev, &iterate_request_sent, ic); 774 GNUNET_MQ_notify_sent (ev, &iterate_request_sent, ic);
766 GNUNET_MQ_send(h->mq, ev); 775 GNUNET_MQ_send (h->mq, ev);
767 ic->timeout_task = GNUNET_SCHEDULER_add_delayed(timeout, &iterate_timeout, ic); 776 ic->timeout_task =
777 GNUNET_SCHEDULER_add_delayed (timeout, &iterate_timeout, ic);
768 return ic; 778 return ic;
769} 779}
770 780
781
771/******************************************************************************/ 782/******************************************************************************/
772/******************* WATCH FUNCTIONS *********************/ 783/******************* WATCH FUNCTIONS *********************/
773/******************************************************************************/ 784/******************************************************************************/
@@ -786,36 +797,38 @@ handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg)
786 struct GNUNET_HashCode keyhash; 797 struct GNUNET_HashCode keyhash;
787 struct GNUNET_PEERSTORE_WatchContext *wc; 798 struct GNUNET_PEERSTORE_WatchContext *wc;
788 799
789 if(NULL == msg) 800 if (NULL == msg)
790 { 801 {
791 LOG(GNUNET_ERROR_TYPE_ERROR, 802 LOG (GNUNET_ERROR_TYPE_ERROR,
792 _("Problem receiving a watch response, no way to determine which request.\n")); 803 _
793 reconnect(h); 804 ("Problem receiving a watch response, no way to determine which request.\n"));
805 reconnect (h);
794 return; 806 return;
795 } 807 }
796 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received a watch record from service.\n"); 808 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a watch record from service.\n");
797 record = PEERSTORE_parse_record_message(msg); 809 record = PEERSTORE_parse_record_message (msg);
798 PEERSTORE_hash_key(record->sub_system, 810 PEERSTORE_hash_key (record->sub_system, record->peer, record->key, &keyhash);
799 record->peer, record->key, &keyhash); 811 wc = GNUNET_CONTAINER_multihashmap_get (h->watches, &keyhash);
800 wc = GNUNET_CONTAINER_multihashmap_get(h->watches, &keyhash); 812 if (NULL == wc)
801 if(NULL == wc)
802 { 813 {
803 LOG(GNUNET_ERROR_TYPE_ERROR, 814 LOG (GNUNET_ERROR_TYPE_ERROR,
804 _("Received a watch result for a non existing watch.\n")); 815 _("Received a watch result for a non existing watch.\n"));
805 reconnect(h); 816 reconnect (h);
806 return; 817 return;
807 } 818 }
808 if(NULL != wc->callback) 819 if (NULL != wc->callback)
809 wc->callback(wc->callback_cls, record, NULL); 820 wc->callback (wc->callback_cls, record, NULL);
810 PEERSTORE_destroy_record(record); 821 PEERSTORE_destroy_record (record);
811} 822}
812 823
824
813/** 825/**
814 * Callback after MQ envelope is sent 826 * Callback after MQ envelope is sent
815 * 827 *
816 * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *' 828 * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *'
817 */ 829 */
818static void watch_request_sent (void *cls) 830static void
831watch_request_sent (void *cls)
819{ 832{
820 struct GNUNET_PEERSTORE_WatchContext *wc = cls; 833 struct GNUNET_PEERSTORE_WatchContext *wc = cls;
821 834
@@ -823,37 +836,39 @@ static void watch_request_sent (void *cls)
823 wc->ev = NULL; 836 wc->ev = NULL;
824} 837}
825 838
839
826/** 840/**
827 * Cancel a watch request 841 * Cancel a watch request
828 * 842 *
829 * @param wc handle to the watch request 843 * @param wc handle to the watch request
830 */ 844 */
831void 845void
832GNUNET_PEERSTORE_watch_cancel(struct GNUNET_PEERSTORE_WatchContext *wc) 846GNUNET_PEERSTORE_watch_cancel (struct GNUNET_PEERSTORE_WatchContext *wc)
833{ 847{
834 struct GNUNET_PEERSTORE_Handle *h = wc->h; 848 struct GNUNET_PEERSTORE_Handle *h = wc->h;
835 struct GNUNET_MQ_Envelope *ev; 849 struct GNUNET_MQ_Envelope *ev;
836 struct StoreKeyHashMessage *hm; 850 struct StoreKeyHashMessage *hm;
837 851
838 LOG(GNUNET_ERROR_TYPE_DEBUG, "Canceling watch.\n"); 852 LOG (GNUNET_ERROR_TYPE_DEBUG, "Canceling watch.\n");
839 if(GNUNET_YES == wc->request_sent) /* If request already sent to service, send a cancel request. */ 853 if (GNUNET_YES == wc->request_sent) /* If request already sent to service, send a cancel request. */
840 { 854 {
841 ev = GNUNET_MQ_msg(hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL); 855 ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL);
842 hm->keyhash = wc->keyhash; 856 hm->keyhash = wc->keyhash;
843 GNUNET_MQ_send(h->mq, ev); 857 GNUNET_MQ_send (h->mq, ev);
844 wc->callback = NULL; 858 wc->callback = NULL;
845 wc->callback_cls = NULL; 859 wc->callback_cls = NULL;
846 } 860 }
847 if(NULL != wc->ev) 861 if (NULL != wc->ev)
848 { 862 {
849 GNUNET_MQ_send_cancel(wc->ev); 863 GNUNET_MQ_send_cancel (wc->ev);
850 wc->ev = NULL; 864 wc->ev = NULL;
851 } 865 }
852 GNUNET_CONTAINER_multihashmap_remove(h->watches, &wc->keyhash, wc); 866 GNUNET_CONTAINER_multihashmap_remove (h->watches, &wc->keyhash, wc);
853 GNUNET_free(wc); 867 GNUNET_free (wc);
854 868
855} 869}
856 870
871
857/** 872/**
858 * Request watching a given key 873 * Request watching a given key
859 * User will be notified with any new values added to key 874 * User will be notified with any new values added to key
@@ -868,36 +883,36 @@ GNUNET_PEERSTORE_watch_cancel(struct GNUNET_PEERSTORE_WatchContext *wc)
868 */ 883 */
869struct GNUNET_PEERSTORE_WatchContext * 884struct GNUNET_PEERSTORE_WatchContext *
870GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h, 885GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h,
871 const char *sub_system, 886 const char *sub_system,
872 const struct GNUNET_PeerIdentity *peer, 887 const struct GNUNET_PeerIdentity *peer, const char *key,
873 const char *key, 888 GNUNET_PEERSTORE_Processor callback, void *callback_cls)
874 GNUNET_PEERSTORE_Processor callback, void *callback_cls)
875{ 889{
876 struct GNUNET_MQ_Envelope *ev; 890 struct GNUNET_MQ_Envelope *ev;
877 struct StoreKeyHashMessage *hm; 891 struct StoreKeyHashMessage *hm;
878 struct GNUNET_PEERSTORE_WatchContext *wc; 892 struct GNUNET_PEERSTORE_WatchContext *wc;
879 893
880 GNUNET_assert(NULL != sub_system); 894 GNUNET_assert (NULL != sub_system);
881 GNUNET_assert(NULL != peer); 895 GNUNET_assert (NULL != peer);
882 GNUNET_assert(NULL != key); 896 GNUNET_assert (NULL != key);
883 ev = GNUNET_MQ_msg(hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH); 897 ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
884 PEERSTORE_hash_key(sub_system, peer, key, &hm->keyhash); 898 PEERSTORE_hash_key (sub_system, peer, key, &hm->keyhash);
885 wc = GNUNET_new(struct GNUNET_PEERSTORE_WatchContext); 899 wc = GNUNET_new (struct GNUNET_PEERSTORE_WatchContext);
900
886 wc->callback = callback; 901 wc->callback = callback;
887 wc->callback_cls = callback_cls; 902 wc->callback_cls = callback_cls;
888 wc->ev = ev; 903 wc->ev = ev;
889 wc->h = h; 904 wc->h = h;
890 wc->request_sent = GNUNET_NO; 905 wc->request_sent = GNUNET_NO;
891 wc->keyhash = hm->keyhash; 906 wc->keyhash = hm->keyhash;
892 if(NULL == h->watches) 907 if (NULL == h->watches)
893 h->watches = GNUNET_CONTAINER_multihashmap_create(5, GNUNET_NO); 908 h->watches = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
894 GNUNET_CONTAINER_multihashmap_put(h->watches, &wc->keyhash, 909 GNUNET_CONTAINER_multihashmap_put (h->watches, &wc->keyhash, wc,
895 wc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE); 910 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
896 LOG(GNUNET_ERROR_TYPE_DEBUG, 911 LOG (GNUNET_ERROR_TYPE_DEBUG,
897 "Sending a watch request for ss `%s', peer `%s', key `%s'.\n", 912 "Sending a watch request for ss `%s', peer `%s', key `%s'.\n",
898 sub_system, GNUNET_i2s(peer), key); 913 sub_system, GNUNET_i2s (peer), key);
899 GNUNET_MQ_notify_sent(ev, &watch_request_sent, wc); 914 GNUNET_MQ_notify_sent (ev, &watch_request_sent, wc);
900 GNUNET_MQ_send(h->mq, ev); 915 GNUNET_MQ_send (h->mq, ev);
901 return wc; 916 return wc;
902} 917}
903 918