aboutsummaryrefslogtreecommitdiff
path: root/src/peerstore/peerstore_api.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2014-12-16 22:45:29 +0000
committerChristian Grothoff <christian@grothoff.org>2014-12-16 22:45:29 +0000
commit80f9251063c14ebbb087d042ad64a178b17d1995 (patch)
tree9921ebab802bacf04db5ddbd099811c61a60aa17 /src/peerstore/peerstore_api.c
parentf4a5d273b10f716b5c218e70c26548311457c4a0 (diff)
downloadgnunet-80f9251063c14ebbb087d042ad64a178b17d1995.tar.gz
gnunet-80f9251063c14ebbb087d042ad64a178b17d1995.zip
-towards addressing #3581
Diffstat (limited to 'src/peerstore/peerstore_api.c')
-rw-r--r--src/peerstore/peerstore_api.c199
1 files changed, 140 insertions, 59 deletions
diff --git a/src/peerstore/peerstore_api.c b/src/peerstore/peerstore_api.c
index a8d372884..90594f73a 100644
--- a/src/peerstore/peerstore_api.c
+++ b/src/peerstore/peerstore_api.c
@@ -108,19 +108,54 @@ struct GNUNET_PEERSTORE_StoreContext
108 struct GNUNET_PEERSTORE_Handle *h; 108 struct GNUNET_PEERSTORE_Handle *h;
109 109
110 /** 110 /**
111 * Continuation called with service response
112 */
113 GNUNET_PEERSTORE_Continuation cont;
114
115 /**
116 * Closure for @e cont
117 */
118 void *cont_cls;
119
120 /**
121 * Which subsystem does the store?
122 */
123 char *sub_system;
124
125 /**
126 * Key for the store operation.
127 */
128 char *key;
129
130 /**
131 * Contains @e size bytes.
132 */
133 void *value;
134
135 /**
111 * MQ Envelope with store request message 136 * MQ Envelope with store request message
112 */ 137 */
113 struct GNUNET_MQ_Envelope *ev; 138 struct GNUNET_MQ_Envelope *ev;
114 139
115 /** 140 /**
116 * Continuation called with service response 141 * Peer the store is for.
117 */ 142 */
118 GNUNET_PEERSTORE_Continuation cont; 143 struct GNUNET_PeerIdentity peer;
119 144
120 /** 145 /**
121 * Closure for 'cont' 146 * Number of bytes in @e value.
122 */ 147 */
123 void *cont_cls; 148 size_t size;
149
150 /**
151 * When does the value expire?
152 */
153 struct GNUNET_TIME_Absolute expiry;
154
155 /**
156 * Options for the store operation.
157 */
158 enum GNUNET_PEERSTORE_StoreOption options;
124 159
125}; 160};
126 161
@@ -155,7 +190,7 @@ struct GNUNET_PEERSTORE_IterateContext
155 GNUNET_PEERSTORE_Processor callback; 190 GNUNET_PEERSTORE_Processor callback;
156 191
157 /** 192 /**
158 * Closure for 'callback' 193 * Closure for @e callback
159 */ 194 */
160 void *callback_cls; 195 void *callback_cls;
161 196
@@ -204,7 +239,7 @@ struct GNUNET_PEERSTORE_WatchContext
204 GNUNET_PEERSTORE_Processor callback; 239 GNUNET_PEERSTORE_Processor callback;
205 240
206 /** 241 /**
207 * Closure for 'callback' 242 * Closure for @e callback
208 */ 243 */
209 void *callback_cls; 244 void *callback_cls;
210 245
@@ -267,13 +302,27 @@ watch_request_sent (void *cls);
267static void 302static void
268iterate_request_sent (void *cls); 303iterate_request_sent (void *cls);
269 304
305
270/** 306/**
271 * Callback after MQ envelope is sent 307 * Callback after MQ envelope is sent
272 * 308 *
273 * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *' 309 * @param cls a `struct GNUNET_PEERSTORE_StoreContext *`
274 */ 310 */
275static void 311static void
276store_request_sent (void *cls); 312store_request_sent (void *cls)
313{
314 struct GNUNET_PEERSTORE_StoreContext *sc = cls;
315 GNUNET_PEERSTORE_Continuation cont;
316 void *cont_cls;
317
318 sc->ev = NULL;
319 cont = sc->cont;
320 cont_cls = sc->cont_cls;
321 GNUNET_PEERSTORE_store_cancel (sc);
322 if (NULL != cont)
323 cont (cont_cls, GNUNET_OK);
324}
325
277 326
278/** 327/**
279 * MQ message handlers 328 * MQ message handlers
@@ -305,7 +354,9 @@ handle_client_error (void *cls, enum GNUNET_MQ_Error error)
305 * Iterator over previous watches to resend them 354 * Iterator over previous watches to resend them
306 */ 355 */
307static int 356static int
308rewatch_it (void *cls, const struct GNUNET_HashCode *key, void *value) 357rewatch_it (void *cls,
358 const struct GNUNET_HashCode *key,
359 void *value)
309{ 360{
310 struct GNUNET_PEERSTORE_Handle *h = cls; 361 struct GNUNET_PEERSTORE_Handle *h = cls;
311 struct GNUNET_PEERSTORE_WatchContext *wc = value; 362 struct GNUNET_PEERSTORE_WatchContext *wc = value;
@@ -336,7 +387,16 @@ reconnect (struct GNUNET_PEERSTORE_Handle *h)
336 void *icb_cls; 387 void *icb_cls;
337 struct GNUNET_PEERSTORE_StoreContext *sc; 388 struct GNUNET_PEERSTORE_StoreContext *sc;
338 389
339 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n"); 390 LOG (GNUNET_ERROR_TYPE_DEBUG,
391 "Reconnecting...\n");
392 for (sc = h->store_head; NULL != sc; sc = sc->next)
393 {
394 if (NULL != sc->ev)
395 {
396 GNUNET_MQ_send_cancel (sc->ev);
397 sc->ev = NULL;
398 }
399 }
340 if (NULL != h->mq) 400 if (NULL != h->mq)
341 { 401 {
342 GNUNET_MQ_destroy (h->mq); 402 GNUNET_MQ_destroy (h->mq);
@@ -355,11 +415,10 @@ reconnect (struct GNUNET_PEERSTORE_Handle *h)
355 LOG (GNUNET_ERROR_TYPE_DEBUG, 415 LOG (GNUNET_ERROR_TYPE_DEBUG,
356 "Resending pending requests after reconnect.\n"); 416 "Resending pending requests after reconnect.\n");
357 if (NULL != h->watches) 417 if (NULL != h->watches)
358 { 418 GNUNET_CONTAINER_multihashmap_iterate (h->watches,
359 GNUNET_CONTAINER_multihashmap_iterate (h->watches, &rewatch_it, h); 419 &rewatch_it,
360 } 420 h);
361 ic = h->iterate_head; 421 for (ic = h->iterate_head; NULL != ic; ic = ic->next)
362 while (NULL != ic)
363 { 422 {
364 if (GNUNET_YES == ic->iterating) 423 if (GNUNET_YES == ic->iterating)
365 { 424 {
@@ -367,21 +426,32 @@ reconnect (struct GNUNET_PEERSTORE_Handle *h)
367 icb_cls = ic->callback_cls; 426 icb_cls = ic->callback_cls;
368 GNUNET_PEERSTORE_iterate_cancel (ic); 427 GNUNET_PEERSTORE_iterate_cancel (ic);
369 if (NULL != icb) 428 if (NULL != icb)
370 icb (icb_cls, NULL, _("Iteration canceled due to reconnection.")); 429 icb (icb_cls,
430 NULL,
431 _("Iteration canceled due to reconnection."));
371 } 432 }
372 else 433 else
373 { 434 {
374 GNUNET_MQ_notify_sent (ic->ev, &iterate_request_sent, ic); 435 GNUNET_MQ_notify_sent (ic->ev,
436 &iterate_request_sent,
437 ic);
375 GNUNET_MQ_send (h->mq, ic->ev); 438 GNUNET_MQ_send (h->mq, ic->ev);
376 } 439 }
377 ic = ic->next;
378 } 440 }
379 sc = h->store_head; 441 for (sc = h->store_head; NULL != sc; sc = sc->next)
380 while (NULL != sc)
381 { 442 {
382 GNUNET_MQ_notify_sent (sc->ev, &store_request_sent, sc); 443 sc->ev = PEERSTORE_create_record_mq_envelope (sc->sub_system,
444 &sc->peer,
445 sc->key,
446 sc->value,
447 sc->size,
448 &sc->expiry,
449 sc->options,
450 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
451 GNUNET_MQ_notify_sent (sc->ev,
452 &store_request_sent,
453 sc);
383 GNUNET_MQ_send (h->mq, sc->ev); 454 GNUNET_MQ_send (h->mq, sc->ev);
384 sc = sc->next;
385 } 455 }
386} 456}
387 457
@@ -395,7 +465,9 @@ reconnect (struct GNUNET_PEERSTORE_Handle *h)
395 * @return #GNUNET_YES to continue iteration 465 * @return #GNUNET_YES to continue iteration
396 */ 466 */
397static int 467static int
398destroy_watch (void *cls, const struct GNUNET_HashCode *key, void *value) 468destroy_watch (void *cls,
469 const struct GNUNET_HashCode *key,
470 void *value)
399{ 471{
400 struct GNUNET_PEERSTORE_WatchContext *wc = value; 472 struct GNUNET_PEERSTORE_WatchContext *wc = value;
401 473
@@ -431,6 +503,7 @@ do_disconnect (struct GNUNET_PEERSTORE_Handle *h)
431/** 503/**
432 * Connect to the PEERSTORE service. 504 * Connect to the PEERSTORE service.
433 * 505 *
506 * @param cfg configuration to use
434 * @return NULL on error 507 * @return NULL on error
435 */ 508 */
436struct GNUNET_PEERSTORE_Handle * 509struct GNUNET_PEERSTORE_Handle *
@@ -453,10 +526,12 @@ GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
453 &handle_client_error, h); 526 &handle_client_error, h);
454 if (NULL == h->mq) 527 if (NULL == h->mq)
455 { 528 {
529 GNUNET_CLIENT_disconnect (h->client);
456 GNUNET_free (h); 530 GNUNET_free (h);
457 return NULL; 531 return NULL;
458 } 532 }
459 LOG (GNUNET_ERROR_TYPE_DEBUG, "New connection created\n"); 533 LOG (GNUNET_ERROR_TYPE_DEBUG,
534 "New connection created\n");
460 return h; 535 return h;
461} 536}
462 537
@@ -477,7 +552,8 @@ GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first)
477 struct GNUNET_PEERSTORE_StoreContext *sc; 552 struct GNUNET_PEERSTORE_StoreContext *sc;
478 struct GNUNET_PEERSTORE_StoreContext *sc_iter; 553 struct GNUNET_PEERSTORE_StoreContext *sc_iter;
479 554
480 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting.\n"); 555 LOG (GNUNET_ERROR_TYPE_DEBUG,
556 "Disconnecting.\n");
481 if (NULL != h->watches) 557 if (NULL != h->watches)
482 { 558 {
483 GNUNET_CONTAINER_multihashmap_iterate (h->watches, 559 GNUNET_CONTAINER_multihashmap_iterate (h->watches,
@@ -519,26 +595,6 @@ GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first)
519/******************* STORE FUNCTIONS *********************/ 595/******************* STORE FUNCTIONS *********************/
520/******************************************************************************/ 596/******************************************************************************/
521 597
522/**
523 * Callback after MQ envelope is sent
524 *
525 * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *'
526 */
527static void
528store_request_sent (void *cls)
529{
530 struct GNUNET_PEERSTORE_StoreContext *sc = cls;
531 GNUNET_PEERSTORE_Continuation cont;
532 void *cont_cls;
533
534 sc->ev = NULL;
535 cont = sc->cont;
536 cont_cls = sc->cont_cls;
537 GNUNET_PEERSTORE_store_cancel (sc);
538 if (NULL != cont)
539 cont (cont_cls, GNUNET_OK);
540}
541
542 598
543/** 599/**
544 * Cancel a store request 600 * Cancel a store request
@@ -555,9 +611,15 @@ GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
555 GNUNET_MQ_send_cancel (sc->ev); 611 GNUNET_MQ_send_cancel (sc->ev);
556 sc->ev = NULL; 612 sc->ev = NULL;
557 } 613 }
558 GNUNET_CONTAINER_DLL_remove (sc->h->store_head, sc->h->store_tail, sc); 614 GNUNET_CONTAINER_DLL_remove (sc->h->store_head,
615 sc->h->store_tail,
616 sc);
617 GNUNET_free (sc->sub_system);
618 GNUNET_free (sc->value);
619 GNUNET_free (sc->key);
559 GNUNET_free (sc); 620 GNUNET_free (sc);
560 if (GNUNET_YES == h->disconnecting && NULL == h->store_head) 621 if ( (GNUNET_YES == h->disconnecting) &&
622 (NULL == h->store_head) )
561 do_disconnect (h); 623 do_disconnect (h);
562} 624}
563 625
@@ -576,16 +638,19 @@ GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
576 * @param expiry absolute time after which the entry is (possibly) deleted 638 * @param expiry absolute time after which the entry is (possibly) deleted
577 * @param options options specific to the storage operation 639 * @param options options specific to the storage operation
578 * @param cont Continuation function after the store request is sent 640 * @param cont Continuation function after the store request is sent
579 * @param cont_cls Closure for 'cont' 641 * @param cont_cls Closure for @a cont
580 */ 642 */
581struct GNUNET_PEERSTORE_StoreContext * 643struct GNUNET_PEERSTORE_StoreContext *
582GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h, 644GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
583 const char *sub_system, 645 const char *sub_system,
584 const struct GNUNET_PeerIdentity *peer, const char *key, 646 const struct GNUNET_PeerIdentity *peer,
585 const void *value, size_t size, 647 const char *key,
648 const void *value,
649 size_t size,
586 struct GNUNET_TIME_Absolute expiry, 650 struct GNUNET_TIME_Absolute expiry,
587 enum GNUNET_PEERSTORE_StoreOption options, 651 enum GNUNET_PEERSTORE_StoreOption options,
588 GNUNET_PEERSTORE_Continuation cont, void *cont_cls) 652 GNUNET_PEERSTORE_Continuation cont,
653 void *cont_cls)
589{ 654{
590 struct GNUNET_MQ_Envelope *ev; 655 struct GNUNET_MQ_Envelope *ev;
591 struct GNUNET_PEERSTORE_StoreContext *sc; 656 struct GNUNET_PEERSTORE_StoreContext *sc;
@@ -593,17 +658,32 @@ GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
593 LOG (GNUNET_ERROR_TYPE_DEBUG, 658 LOG (GNUNET_ERROR_TYPE_DEBUG,
594 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n", 659 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n",
595 size, sub_system, GNUNET_i2s (peer), key); 660 size, sub_system, GNUNET_i2s (peer), key);
596 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, value, size, 661 ev = PEERSTORE_create_record_mq_envelope (sub_system,
597 &expiry, options, 662 peer,
663 key,
664 value,
665 size,
666 &expiry,
667 options,
598 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE); 668 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
599 sc = GNUNET_new (struct GNUNET_PEERSTORE_StoreContext); 669 sc = GNUNET_new (struct GNUNET_PEERSTORE_StoreContext);
600 670 sc->sub_system = GNUNET_strdup (sub_system);
601 sc->ev = ev; 671 sc->peer = *peer;
672 sc->key = GNUNET_strdup (key);
673 sc->value = GNUNET_memdup (value, size);
674 sc->size = size;
675 sc->expiry = expiry;
676 sc->options = options;
602 sc->cont = cont; 677 sc->cont = cont;
603 sc->cont_cls = cont_cls; 678 sc->cont_cls = cont_cls;
604 sc->h = h; 679 sc->h = h;
605 GNUNET_CONTAINER_DLL_insert_tail (h->store_head, h->store_tail, sc); 680
606 GNUNET_MQ_notify_sent (ev, &store_request_sent, sc); 681 GNUNET_CONTAINER_DLL_insert_tail (h->store_head,
682 h->store_tail,
683 sc);
684 GNUNET_MQ_notify_sent (ev,
685 &store_request_sent,
686 sc);
607 GNUNET_MQ_send (h->mq, ev); 687 GNUNET_MQ_send (h->mq, ev);
608 return sc; 688 return sc;
609 689
@@ -617,11 +697,12 @@ GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
617/** 697/**
618 * When a response for iterate request is received 698 * When a response for iterate request is received
619 * 699 *
620 * @param cls a 'struct GNUNET_PEERSTORE_Handle *' 700 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
621 * @param msg message received, NULL on timeout or fatal error 701 * @param msg message received, NULL on timeout or fatal error
622 */ 702 */
623static void 703static void
624handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg) 704handle_iterate_result (void *cls,
705 const struct GNUNET_MessageHeader *msg)
625{ 706{
626 struct GNUNET_PEERSTORE_Handle *h = cls; 707 struct GNUNET_PEERSTORE_Handle *h = cls;
627 struct GNUNET_PEERSTORE_IterateContext *ic; 708 struct GNUNET_PEERSTORE_IterateContext *ic;