2 This file is part of GNUnet.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file peerstore/peerstore_api.c
23 * @brief API for peerstore
24 * @author Omar Tarabai
27 #include "gnunet_util_lib.h"
28 #include "peerstore.h"
29 #include "peerstore_common.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "peerstore-api",__VA_ARGS__)
33 /******************************************************************************/
34 /************************ DATA STRUCTURES ****************************/
35 /******************************************************************************/
38 * Handle to the PEERSTORE service.
40 struct GNUNET_PEERSTORE_Handle
46 const struct GNUNET_CONFIGURATION_Handle *cfg;
49 * Connection to the service.
51 struct GNUNET_CLIENT_Connection *client;
56 struct GNUNET_MQ_Handle *mq;
59 * Head of active STORE requests.
61 struct GNUNET_PEERSTORE_StoreContext *store_head;
64 * Tail of active STORE requests.
66 struct GNUNET_PEERSTORE_StoreContext *store_tail;
69 * Head of active ITERATE requests.
71 struct GNUNET_PEERSTORE_IterateContext *iterate_head;
74 * Tail of active ITERATE requests.
76 struct GNUNET_PEERSTORE_IterateContext *iterate_tail;
79 * Hashmap of watch requests
81 struct GNUNET_CONTAINER_MultiHashMap *watches;
86 * Context for a store request
88 struct GNUNET_PEERSTORE_StoreContext
93 struct GNUNET_PEERSTORE_StoreContext *next;
98 struct GNUNET_PEERSTORE_StoreContext *prev;
101 * Handle to the PEERSTORE service.
103 struct GNUNET_PEERSTORE_Handle *h;
106 * MQ Envelope with store request message
108 struct GNUNET_MQ_Envelope *ev;
111 * Continuation called with service response
113 GNUNET_PEERSTORE_Continuation cont;
121 * #GNUNET_YES / #GNUNET_NO
122 * if sent, cannot be canceled
129 * Context for a iterate request
131 struct GNUNET_PEERSTORE_IterateContext
136 struct GNUNET_PEERSTORE_IterateContext *next;
141 struct GNUNET_PEERSTORE_IterateContext *prev;
144 * Handle to the PEERSTORE service.
146 struct GNUNET_PEERSTORE_Handle *h;
149 * MQ Envelope with iterate request message
151 struct GNUNET_MQ_Envelope *ev;
154 * Callback with each matching record
156 GNUNET_PEERSTORE_Processor callback;
159 * Closure for 'callback'
164 * #GNUNET_YES / #GNUNET_NO
165 * if sent, cannot be canceled
170 * Task identifier for the function called
171 * on iterate request timeout
173 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
178 * Context for a watch request
180 struct GNUNET_PEERSTORE_WatchContext
185 struct GNUNET_PEERSTORE_WatchContext *next;
190 struct GNUNET_PEERSTORE_WatchContext *prev;
193 * Handle to the PEERSTORE service.
195 struct GNUNET_PEERSTORE_Handle *h;
198 * MQ Envelope with watch request message
200 struct GNUNET_MQ_Envelope *ev;
203 * Callback with each record received
205 GNUNET_PEERSTORE_Processor callback;
208 * Closure for 'callback'
213 * Hash of the combined key
215 struct GNUNET_HashCode keyhash;
218 * #GNUNET_YES / #GNUNET_NO
219 * if sent, cannot be canceled
225 /******************************************************************************/
226 /******************* DECLARATIONS *********************/
227 /******************************************************************************/
230 * When a response for store request is received
232 * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *'
233 * @param msg message received, NULL on timeout or fatal error
235 void handle_store_result (void *cls, const struct GNUNET_MessageHeader *msg);
238 * When a response for iterate request is received
240 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
241 * @param msg message received, NULL on timeout or fatal error
243 void handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg);
246 * When a watch record is received
248 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
249 * @param msg message received, NULL on timeout or fatal error
251 void handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg);
254 * Close the existing connection to PEERSTORE and reconnect.
256 * @param h handle to the service
259 reconnect (struct GNUNET_PEERSTORE_Handle *h);
262 * MQ message handlers
264 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
265 {&handle_store_result, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK, sizeof(struct GNUNET_MessageHeader)},
266 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD, 0},
267 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END, sizeof(struct GNUNET_MessageHeader)},
268 {&handle_watch_result, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD, 0},
269 GNUNET_MQ_HANDLERS_END
272 /******************************************************************************/
273 /******************* CONNECTION FUNCTIONS *********************/
274 /******************************************************************************/
277 handle_client_error (void *cls, enum GNUNET_MQ_Error error)
279 struct GNUNET_PEERSTORE_Handle *h = cls;
281 LOG(GNUNET_ERROR_TYPE_ERROR, "Received an error notification from MQ of type: %d\n", error);
286 * Close the existing connection to PEERSTORE and reconnect.
288 * @param h handle to the service
291 reconnect (struct GNUNET_PEERSTORE_Handle *h)
293 LOG(GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n");
296 GNUNET_MQ_destroy(h->mq);
299 if (NULL != h->client)
301 GNUNET_CLIENT_disconnect (h->client);
304 h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg);
305 //FIXME: retry connecting if fails again (client == NULL)
306 h->mq = GNUNET_MQ_queue_for_connection_client(h->client,
308 &handle_client_error,
310 //FIXME: resend pending requests after reconnecting
315 * Connect to the PEERSTORE service.
317 * @return NULL on error
319 struct GNUNET_PEERSTORE_Handle *
320 GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
322 struct GNUNET_PEERSTORE_Handle *h;
324 h = GNUNET_new (struct GNUNET_PEERSTORE_Handle);
325 h->client = GNUNET_CLIENT_connect ("peerstore", cfg);
326 if(NULL == h->client)
332 h->mq = GNUNET_MQ_queue_for_connection_client(h->client,
334 &handle_client_error,
341 LOG(GNUNET_ERROR_TYPE_DEBUG, "New connection created\n");
346 * Disconnect from the PEERSTORE service
347 * Do not call in case of pending requests
349 * @param h handle to disconnect
352 GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h)
354 if(NULL != h->watches)
356 GNUNET_CONTAINER_multihashmap_destroy(h->watches);
361 GNUNET_MQ_destroy(h->mq);
364 if (NULL != h->client)
366 GNUNET_CLIENT_disconnect (h->client);
370 LOG(GNUNET_ERROR_TYPE_DEBUG, "Disconnected, BYE!\n");
374 /******************************************************************************/
375 /******************* STORE FUNCTIONS *********************/
376 /******************************************************************************/
379 * When a response for store request is received
381 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
382 * @param msg message received, NULL on timeout or fatal error
384 void handle_store_result (void *cls, const struct GNUNET_MessageHeader *msg)
386 struct GNUNET_PEERSTORE_Handle *h = cls;
387 struct GNUNET_PEERSTORE_StoreContext *sc;
388 GNUNET_PEERSTORE_Continuation cont;
394 LOG(GNUNET_ERROR_TYPE_ERROR, "Unexpected store response, this should not happen.\n");
399 cont_cls = sc->cont_cls;
400 GNUNET_CONTAINER_DLL_remove(h->store_head, h->store_tail, sc);
402 if(NULL == msg) /* Connection error */
405 cont(cont_cls, GNUNET_SYSERR);
409 if(NULL != cont) /* Run continuation */
410 cont(cont_cls, GNUNET_OK);
415 * Callback after MQ envelope is sent
417 * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *'
419 void store_request_sent (void *cls)
421 struct GNUNET_PEERSTORE_StoreContext *sc = cls;
423 sc->request_sent = GNUNET_YES;
428 * Cancel a store request
430 * @param sc Store request context
433 GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
435 LOG(GNUNET_ERROR_TYPE_DEBUG,
436 "Canceling store request.\n");
437 if(GNUNET_NO == sc->request_sent)
441 GNUNET_MQ_send_cancel(sc->ev);
444 GNUNET_CONTAINER_DLL_remove(sc->h->store_head, sc->h->store_tail, sc);
448 { /* request already sent, will have to wait for response */
455 * Store a new entry in the PEERSTORE
457 * @param h Handle to the PEERSTORE service
458 * @param sub_system name of the sub system
459 * @param peer Peer Identity
460 * @param key entry key
461 * @param value entry value BLOB
462 * @param size size of 'value'
463 * @param expiry absolute time after which the entry is (possibly) deleted
464 * @param cont Continuation function after the store request is processed
465 * @param cont_cls Closure for 'cont'
467 struct GNUNET_PEERSTORE_StoreContext *
468 GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
469 const char *sub_system,
470 const struct GNUNET_PeerIdentity *peer,
474 struct GNUNET_TIME_Absolute expiry,
475 GNUNET_PEERSTORE_Continuation cont,
478 struct GNUNET_MQ_Envelope *ev; //FIXME: add 'replace' flag in store function (similar to multihashmap)
479 struct GNUNET_PEERSTORE_StoreContext *sc;
481 LOG (GNUNET_ERROR_TYPE_DEBUG,
482 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n",
483 size, sub_system, GNUNET_i2s (peer), key);
484 ev = PEERSTORE_create_record_mq_envelope(sub_system,
490 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
491 sc = GNUNET_new(struct GNUNET_PEERSTORE_StoreContext);
494 sc->cont_cls = cont_cls;
496 sc->request_sent = GNUNET_NO;
497 GNUNET_CONTAINER_DLL_insert(h->store_head, h->store_tail, sc);
498 GNUNET_MQ_notify_sent(ev, &store_request_sent, sc);
499 GNUNET_MQ_send(h->mq, ev);
504 /******************************************************************************/
505 /******************* ITERATE FUNCTIONS *********************/
506 /******************************************************************************/
509 * When a response for iterate request is received
511 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
512 * @param msg message received, NULL on timeout or fatal error
514 void handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg)
516 struct GNUNET_PEERSTORE_Handle *h = cls;
517 struct GNUNET_PEERSTORE_IterateContext *ic;
518 GNUNET_PEERSTORE_Processor callback;
521 struct GNUNET_PEERSTORE_Record *record;
524 ic = h->iterate_head;
527 LOG(GNUNET_ERROR_TYPE_ERROR, "Unexpected iteration response, this should not happen.\n");
531 callback = ic->callback;
532 callback_cls = ic->callback_cls;
533 if(NULL == msg) /* Connection error */
537 callback(callback_cls, NULL,
538 _("Error communicating with `PEERSTORE' service."));
542 msg_type = ntohs(msg->type);
543 if(GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END == msg_type)
545 GNUNET_PEERSTORE_iterate_cancel(ic);
547 callback(callback_cls, NULL, NULL);
552 record = PEERSTORE_parse_record_message(msg);
554 continue_iter = callback(callback_cls, record, _("Received a malformed response from service."));
556 continue_iter = callback(callback_cls, record, NULL);
557 if(GNUNET_NO == continue_iter)
564 * Callback after MQ envelope is sent
566 * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *'
568 void iterate_request_sent (void *cls)
570 struct GNUNET_PEERSTORE_IterateContext *ic = cls;
572 LOG(GNUNET_ERROR_TYPE_DEBUG, "Iterate request sent to service.\n");
573 ic->request_sent = GNUNET_YES;
578 * Called when the iterate request is timedout
580 * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *'
582 void iterate_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
584 struct GNUNET_PEERSTORE_IterateContext *ic = cls;
586 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
587 GNUNET_PEERSTORE_iterate_cancel(ic);
591 * Cancel an iterate request
592 * Please do not call after the iterate request is done
594 * @param ic Iterate request context as returned by GNUNET_PEERSTORE_iterate()
597 GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic)
599 LOG(GNUNET_ERROR_TYPE_DEBUG, "Canceling iterate request.\n");
600 if(GNUNET_SCHEDULER_NO_TASK != ic->timeout_task)
602 GNUNET_SCHEDULER_cancel(ic->timeout_task);
603 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
605 if(GNUNET_NO == ic->request_sent)
609 GNUNET_MQ_send_cancel(ic->ev);
612 GNUNET_CONTAINER_DLL_remove(ic->h->iterate_head, ic->h->iterate_tail, ic);
620 * Iterate over records matching supplied key information
622 * @param h handle to the PEERSTORE service
623 * @param sub_system name of sub system
624 * @param peer Peer identity (can be NULL)
625 * @param key entry key string (can be NULL)
626 * @param timeout time after which the iterate request is canceled
627 * @param callback function called with each matching record, all NULL's on end
628 * @param callback_cls closure for @a callback
629 * @return Handle to iteration request
631 struct GNUNET_PEERSTORE_IterateContext *
632 GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
633 const char *sub_system,
634 const struct GNUNET_PeerIdentity *peer,
636 struct GNUNET_TIME_Relative timeout,
637 GNUNET_PEERSTORE_Processor callback, void *callback_cls)
639 struct GNUNET_MQ_Envelope *ev;
640 struct GNUNET_PEERSTORE_IterateContext *ic;
642 ev = PEERSTORE_create_record_mq_envelope(sub_system,
648 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
649 ic = GNUNET_new(struct GNUNET_PEERSTORE_IterateContext);
650 ic->callback = callback;
651 ic->callback_cls = callback_cls;
654 ic->request_sent = GNUNET_NO;
655 GNUNET_CONTAINER_DLL_insert(h->iterate_head, h->iterate_tail, ic);
656 LOG(GNUNET_ERROR_TYPE_DEBUG,
657 "Sending an iterate request for sub system `%s'\n", sub_system);
658 GNUNET_MQ_notify_sent(ev, &iterate_request_sent, ic);
659 GNUNET_MQ_send(h->mq, ev);
660 ic->timeout_task = GNUNET_SCHEDULER_add_delayed(timeout, &iterate_timeout, ic);
664 /******************************************************************************/
665 /******************* WATCH FUNCTIONS *********************/
666 /******************************************************************************/
669 * When a watch record is received
671 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
672 * @param msg message received, NULL on timeout or fatal error
674 void handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg)
676 struct GNUNET_PEERSTORE_Handle *h = cls;
677 struct GNUNET_PEERSTORE_Record *record;
678 struct GNUNET_HashCode keyhash;
679 struct GNUNET_PEERSTORE_WatchContext *wc;
683 LOG(GNUNET_ERROR_TYPE_ERROR,
684 "Problem receiving a watch response, no way to determine which request.\n");
688 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received a watch record from service.\n");
689 record = PEERSTORE_parse_record_message(msg);
690 PEERSTORE_hash_key(record->sub_system,
691 record->peer, record->key, &keyhash);
692 wc = GNUNET_CONTAINER_multihashmap_get(h->watches, &keyhash);
693 if(NULL != wc->callback)
694 wc->callback(wc->callback_cls, record, NULL);
695 /* TODO: destroy record */
699 * Callback after MQ envelope is sent
701 * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *'
703 void watch_request_sent (void *cls)
705 struct GNUNET_PEERSTORE_WatchContext *wc = cls;
707 wc->request_sent = GNUNET_YES;
712 * Cancel a watch request
714 * @wc handle to the watch request
717 GNUNET_PEERSTORE_watch_cancel(struct GNUNET_PEERSTORE_WatchContext *wc)
719 struct GNUNET_PEERSTORE_Handle *h = wc->h;
720 struct GNUNET_MQ_Envelope *ev;
721 struct StoreKeyHashMessage *hm;
723 LOG(GNUNET_ERROR_TYPE_DEBUG, "Canceling watch.\n");
724 if(GNUNET_YES == wc->request_sent) /* If request already sent to service, send a cancel request. */
726 ev = GNUNET_MQ_msg(hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL);
727 GNUNET_MQ_send(h->mq, ev);
729 wc->callback_cls = NULL;
733 GNUNET_MQ_send_cancel(wc->ev);
736 GNUNET_CONTAINER_multihashmap_remove(h->watches, &wc->keyhash, wc);
742 * Request watching a given key
743 * User will be notified with any new values added to key
745 * @param h handle to the PEERSTORE service
746 * @param sub_system name of sub system
747 * @param peer Peer identity
748 * @param key entry key string
749 * @param callback function called with each new value
750 * @param callback_cls closure for @a callback
751 * @return Handle to watch request
753 struct GNUNET_PEERSTORE_WatchContext *
754 GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h,
755 const char *sub_system,
756 const struct GNUNET_PeerIdentity *peer,
758 GNUNET_PEERSTORE_Processor callback, void *callback_cls)
760 struct GNUNET_MQ_Envelope *ev;
761 struct StoreKeyHashMessage *hm;
762 struct GNUNET_PEERSTORE_WatchContext *wc;
764 ev = GNUNET_MQ_msg(hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
765 PEERSTORE_hash_key(sub_system, peer, key, &hm->keyhash);
766 wc = GNUNET_new(struct GNUNET_PEERSTORE_WatchContext);
767 wc->callback = callback;
768 wc->callback_cls = callback_cls;
771 wc->request_sent = GNUNET_NO;
772 wc->keyhash = hm->keyhash;
773 if(NULL == h->watches)
774 h->watches = GNUNET_CONTAINER_multihashmap_create(5, GNUNET_NO);
775 GNUNET_CONTAINER_multihashmap_put(h->watches, &wc->keyhash,
776 wc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
777 LOG(GNUNET_ERROR_TYPE_DEBUG,
778 "Sending a watch request for sub system `%s'.\n", sub_system);
779 GNUNET_MQ_notify_sent(ev, &watch_request_sent, wc);
780 GNUNET_MQ_send(h->mq, ev);
784 /* end of peerstore_api.c */