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;
123 * Context for a iterate request
125 struct GNUNET_PEERSTORE_IterateContext
130 struct GNUNET_PEERSTORE_IterateContext *next;
135 struct GNUNET_PEERSTORE_IterateContext *prev;
138 * Handle to the PEERSTORE service.
140 struct GNUNET_PEERSTORE_Handle *h;
143 * MQ Envelope with iterate request message
145 struct GNUNET_MQ_Envelope *ev;
148 * Callback with each matching record
150 GNUNET_PEERSTORE_Processor callback;
153 * Closure for 'callback'
158 * #GNUNET_YES / #GNUNET_NO
159 * if sent, cannot be canceled
164 * Task identifier for the function called
165 * on iterate request timeout
167 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
172 * Context for a watch request
174 struct GNUNET_PEERSTORE_WatchContext
179 struct GNUNET_PEERSTORE_WatchContext *next;
184 struct GNUNET_PEERSTORE_WatchContext *prev;
187 * Handle to the PEERSTORE service.
189 struct GNUNET_PEERSTORE_Handle *h;
192 * MQ Envelope with watch request message
194 struct GNUNET_MQ_Envelope *ev;
197 * Callback with each record received
199 GNUNET_PEERSTORE_Processor callback;
202 * Closure for 'callback'
207 * Hash of the combined key
209 struct GNUNET_HashCode keyhash;
212 * #GNUNET_YES / #GNUNET_NO
213 * if sent, cannot be canceled
219 /******************************************************************************/
220 /******************* DECLARATIONS *********************/
221 /******************************************************************************/
224 * When a response for iterate request is received
226 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
227 * @param msg message received, NULL on timeout or fatal error
229 void handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg);
232 * When a watch record is received
234 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
235 * @param msg message received, NULL on timeout or fatal error
237 void handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg);
240 * Close the existing connection to PEERSTORE and reconnect.
242 * @param h handle to the service
245 reconnect (struct GNUNET_PEERSTORE_Handle *h);
248 * MQ message handlers
250 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
251 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD, 0},
252 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END, sizeof(struct GNUNET_MessageHeader)},
253 {&handle_watch_result, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD, 0},
254 GNUNET_MQ_HANDLERS_END
257 /******************************************************************************/
258 /******************* CONNECTION FUNCTIONS *********************/
259 /******************************************************************************/
262 handle_client_error (void *cls, enum GNUNET_MQ_Error error)
264 struct GNUNET_PEERSTORE_Handle *h = cls;
266 LOG(GNUNET_ERROR_TYPE_ERROR, "Received an error notification from MQ of type: %d\n", error);
271 * Close the existing connection to PEERSTORE and reconnect.
273 * @param h handle to the service
276 reconnect (struct GNUNET_PEERSTORE_Handle *h)
278 LOG(GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n");
281 GNUNET_MQ_destroy(h->mq);
284 if (NULL != h->client)
286 GNUNET_CLIENT_disconnect (h->client);
289 h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg);
290 //FIXME: retry connecting if fails again (client == NULL)
291 h->mq = GNUNET_MQ_queue_for_connection_client(h->client,
293 &handle_client_error,
295 //FIXME: resend pending requests after reconnecting
300 * Connect to the PEERSTORE service.
302 * @return NULL on error
304 struct GNUNET_PEERSTORE_Handle *
305 GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
307 struct GNUNET_PEERSTORE_Handle *h;
309 h = GNUNET_new (struct GNUNET_PEERSTORE_Handle);
310 h->client = GNUNET_CLIENT_connect ("peerstore", cfg);
311 if(NULL == h->client)
317 h->mq = GNUNET_MQ_queue_for_connection_client(h->client,
319 &handle_client_error,
326 LOG(GNUNET_ERROR_TYPE_DEBUG, "New connection created\n");
331 * Disconnect from the PEERSTORE service
332 * Do not call in case of pending requests
334 * @param h handle to disconnect
337 GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h)
339 if(NULL != h->watches)
341 GNUNET_CONTAINER_multihashmap_destroy(h->watches);
346 GNUNET_MQ_destroy(h->mq);
349 if (NULL != h->client)
351 GNUNET_CLIENT_disconnect (h->client);
355 LOG(GNUNET_ERROR_TYPE_DEBUG, "Disconnected, BYE!\n");
359 /******************************************************************************/
360 /******************* STORE FUNCTIONS *********************/
361 /******************************************************************************/
364 * Callback after MQ envelope is sent
366 * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *'
368 void store_request_sent (void *cls)
370 struct GNUNET_PEERSTORE_StoreContext *sc = cls;
371 GNUNET_PEERSTORE_Continuation cont;
376 cont_cls = sc->cont_cls;
377 GNUNET_PEERSTORE_store_cancel(sc);
379 cont(cont_cls, GNUNET_OK);
383 * Cancel a store request
385 * @param sc Store request context
388 GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
390 LOG(GNUNET_ERROR_TYPE_DEBUG,
391 "Canceling store request.\n");
394 GNUNET_MQ_send_cancel(sc->ev);
397 GNUNET_CONTAINER_DLL_remove(sc->h->store_head, sc->h->store_tail, sc);
402 * Store a new entry in the PEERSTORE
404 * @param h Handle to the PEERSTORE service
405 * @param sub_system name of the sub system
406 * @param peer Peer Identity
407 * @param key entry key
408 * @param value entry value BLOB
409 * @param size size of 'value'
410 * @param expiry absolute time after which the entry is (possibly) deleted
411 * @param cont Continuation function after the store request is processed
412 * @param cont_cls Closure for 'cont'
414 struct GNUNET_PEERSTORE_StoreContext *
415 GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
416 const char *sub_system,
417 const struct GNUNET_PeerIdentity *peer,
421 struct GNUNET_TIME_Absolute expiry,
422 GNUNET_PEERSTORE_Continuation cont,
425 struct GNUNET_MQ_Envelope *ev; //FIXME: add 'replace' flag in store function (similar to multihashmap)
426 struct GNUNET_PEERSTORE_StoreContext *sc;
428 LOG (GNUNET_ERROR_TYPE_DEBUG,
429 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n",
430 size, sub_system, GNUNET_i2s (peer), key);
431 ev = PEERSTORE_create_record_mq_envelope(sub_system,
437 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
438 sc = GNUNET_new(struct GNUNET_PEERSTORE_StoreContext);
441 sc->cont_cls = cont_cls;
443 GNUNET_CONTAINER_DLL_insert(h->store_head, h->store_tail, sc);
444 GNUNET_MQ_notify_sent(ev, &store_request_sent, sc);
445 GNUNET_MQ_send(h->mq, ev);
450 /******************************************************************************/
451 /******************* ITERATE FUNCTIONS *********************/
452 /******************************************************************************/
455 * When a response for iterate request is received
457 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
458 * @param msg message received, NULL on timeout or fatal error
460 void handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg)
462 struct GNUNET_PEERSTORE_Handle *h = cls;
463 struct GNUNET_PEERSTORE_IterateContext *ic;
464 GNUNET_PEERSTORE_Processor callback;
467 struct GNUNET_PEERSTORE_Record *record;
470 ic = h->iterate_head;
473 LOG(GNUNET_ERROR_TYPE_ERROR, "Unexpected iteration response, this should not happen.\n");
477 callback = ic->callback;
478 callback_cls = ic->callback_cls;
479 if(NULL == msg) /* Connection error */
483 callback(callback_cls, NULL,
484 _("Error communicating with `PEERSTORE' service."));
488 msg_type = ntohs(msg->type);
489 if(GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END == msg_type)
491 GNUNET_PEERSTORE_iterate_cancel(ic);
493 callback(callback_cls, NULL, NULL);
498 record = PEERSTORE_parse_record_message(msg);
500 continue_iter = callback(callback_cls, record, _("Received a malformed response from service."));
502 continue_iter = callback(callback_cls, record, NULL);
503 if(GNUNET_NO == continue_iter)
510 * Callback after MQ envelope is sent
512 * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *'
514 void iterate_request_sent (void *cls)
516 struct GNUNET_PEERSTORE_IterateContext *ic = cls;
518 LOG(GNUNET_ERROR_TYPE_DEBUG, "Iterate request sent to service.\n");
519 ic->request_sent = GNUNET_YES;
524 * Called when the iterate request is timedout
526 * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *'
528 void iterate_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
530 struct GNUNET_PEERSTORE_IterateContext *ic = cls;
532 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
533 GNUNET_PEERSTORE_iterate_cancel(ic);
537 * Cancel an iterate request
538 * Please do not call after the iterate request is done
540 * @param ic Iterate request context as returned by GNUNET_PEERSTORE_iterate()
543 GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic)
545 LOG(GNUNET_ERROR_TYPE_DEBUG, "Canceling iterate request.\n");
546 if(GNUNET_SCHEDULER_NO_TASK != ic->timeout_task)
548 GNUNET_SCHEDULER_cancel(ic->timeout_task);
549 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
551 if(GNUNET_NO == ic->request_sent)
555 GNUNET_MQ_send_cancel(ic->ev);
558 GNUNET_CONTAINER_DLL_remove(ic->h->iterate_head, ic->h->iterate_tail, ic);
566 * Iterate over records matching supplied key information
568 * @param h handle to the PEERSTORE service
569 * @param sub_system name of sub system
570 * @param peer Peer identity (can be NULL)
571 * @param key entry key string (can be NULL)
572 * @param timeout time after which the iterate request is canceled
573 * @param callback function called with each matching record, all NULL's on end
574 * @param callback_cls closure for @a callback
575 * @return Handle to iteration request
577 struct GNUNET_PEERSTORE_IterateContext *
578 GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
579 const char *sub_system,
580 const struct GNUNET_PeerIdentity *peer,
582 struct GNUNET_TIME_Relative timeout,
583 GNUNET_PEERSTORE_Processor callback, void *callback_cls)
585 struct GNUNET_MQ_Envelope *ev;
586 struct GNUNET_PEERSTORE_IterateContext *ic;
588 ev = PEERSTORE_create_record_mq_envelope(sub_system,
594 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
595 ic = GNUNET_new(struct GNUNET_PEERSTORE_IterateContext);
596 ic->callback = callback;
597 ic->callback_cls = callback_cls;
600 ic->request_sent = GNUNET_NO;
601 GNUNET_CONTAINER_DLL_insert(h->iterate_head, h->iterate_tail, ic);
602 LOG(GNUNET_ERROR_TYPE_DEBUG,
603 "Sending an iterate request for sub system `%s'\n", sub_system);
604 GNUNET_MQ_notify_sent(ev, &iterate_request_sent, ic);
605 GNUNET_MQ_send(h->mq, ev);
606 ic->timeout_task = GNUNET_SCHEDULER_add_delayed(timeout, &iterate_timeout, ic);
610 /******************************************************************************/
611 /******************* WATCH FUNCTIONS *********************/
612 /******************************************************************************/
615 * When a watch record is received
617 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
618 * @param msg message received, NULL on timeout or fatal error
620 void handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg)
622 struct GNUNET_PEERSTORE_Handle *h = cls;
623 struct GNUNET_PEERSTORE_Record *record;
624 struct GNUNET_HashCode keyhash;
625 struct GNUNET_PEERSTORE_WatchContext *wc;
629 LOG(GNUNET_ERROR_TYPE_ERROR,
630 "Problem receiving a watch response, no way to determine which request.\n");
634 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received a watch record from service.\n");
635 record = PEERSTORE_parse_record_message(msg);
636 PEERSTORE_hash_key(record->sub_system,
637 record->peer, record->key, &keyhash);
638 wc = GNUNET_CONTAINER_multihashmap_get(h->watches, &keyhash);
639 if(NULL != wc->callback)
640 wc->callback(wc->callback_cls, record, NULL);
641 /* TODO: destroy record */
645 * Callback after MQ envelope is sent
647 * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *'
649 void watch_request_sent (void *cls)
651 struct GNUNET_PEERSTORE_WatchContext *wc = cls;
653 wc->request_sent = GNUNET_YES;
658 * Cancel a watch request
660 * @wc handle to the watch request
663 GNUNET_PEERSTORE_watch_cancel(struct GNUNET_PEERSTORE_WatchContext *wc)
665 struct GNUNET_PEERSTORE_Handle *h = wc->h;
666 struct GNUNET_MQ_Envelope *ev;
667 struct StoreKeyHashMessage *hm;
669 LOG(GNUNET_ERROR_TYPE_DEBUG, "Canceling watch.\n");
670 if(GNUNET_YES == wc->request_sent) /* If request already sent to service, send a cancel request. */
672 ev = GNUNET_MQ_msg(hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL);
673 GNUNET_MQ_send(h->mq, ev);
675 wc->callback_cls = NULL;
679 GNUNET_MQ_send_cancel(wc->ev);
682 GNUNET_CONTAINER_multihashmap_remove(h->watches, &wc->keyhash, wc);
688 * Request watching a given key
689 * User will be notified with any new values added to key
691 * @param h handle to the PEERSTORE service
692 * @param sub_system name of sub system
693 * @param peer Peer identity
694 * @param key entry key string
695 * @param callback function called with each new value
696 * @param callback_cls closure for @a callback
697 * @return Handle to watch request
699 struct GNUNET_PEERSTORE_WatchContext *
700 GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h,
701 const char *sub_system,
702 const struct GNUNET_PeerIdentity *peer,
704 GNUNET_PEERSTORE_Processor callback, void *callback_cls)
706 struct GNUNET_MQ_Envelope *ev;
707 struct StoreKeyHashMessage *hm;
708 struct GNUNET_PEERSTORE_WatchContext *wc;
710 ev = GNUNET_MQ_msg(hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
711 PEERSTORE_hash_key(sub_system, peer, key, &hm->keyhash);
712 wc = GNUNET_new(struct GNUNET_PEERSTORE_WatchContext);
713 wc->callback = callback;
714 wc->callback_cls = callback_cls;
717 wc->request_sent = GNUNET_NO;
718 wc->keyhash = hm->keyhash;
719 if(NULL == h->watches)
720 h->watches = GNUNET_CONTAINER_multihashmap_create(5, GNUNET_NO);
721 GNUNET_CONTAINER_multihashmap_put(h->watches, &wc->keyhash,
722 wc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
723 LOG(GNUNET_ERROR_TYPE_DEBUG,
724 "Sending a watch request for sub system `%s'.\n", sub_system);
725 GNUNET_MQ_notify_sent(ev, &watch_request_sent, wc);
726 GNUNET_MQ_send(h->mq, ev);
730 /* end of peerstore_api.c */