2 This file is part of GNUnet.
3 (C) 2013-2014 Christian Grothoff (and other contributing authors)
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.
21 * @file peerstore/peerstore_api.c
22 * @brief API for peerstore
23 * @author Omar Tarabai
24 * @author Christian Grothoff
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;
84 * Are we in the process of disconnecting but need to sync first?
91 * Context for a store request
93 struct GNUNET_PEERSTORE_StoreContext
98 struct GNUNET_PEERSTORE_StoreContext *next;
103 struct GNUNET_PEERSTORE_StoreContext *prev;
106 * Handle to the PEERSTORE service.
108 struct GNUNET_PEERSTORE_Handle *h;
111 * Continuation called with service response
113 GNUNET_PEERSTORE_Continuation cont;
116 * Closure for @e cont
121 * Which subsystem does the store?
126 * Key for the store operation.
131 * Contains @e size bytes.
136 * MQ Envelope with store request message
138 struct GNUNET_MQ_Envelope *ev;
141 * Peer the store is for.
143 struct GNUNET_PeerIdentity peer;
146 * Number of bytes in @e value.
151 * When does the value expire?
153 struct GNUNET_TIME_Absolute expiry;
156 * Options for the store operation.
158 enum GNUNET_PEERSTORE_StoreOption options;
163 * Context for a iterate request
165 struct GNUNET_PEERSTORE_IterateContext
170 struct GNUNET_PEERSTORE_IterateContext *next;
175 struct GNUNET_PEERSTORE_IterateContext *prev;
178 * Handle to the PEERSTORE service.
180 struct GNUNET_PEERSTORE_Handle *h;
183 * Which subsystem does the store?
188 * Peer the store is for.
190 struct GNUNET_PeerIdentity peer;
193 * Key for the store operation.
200 struct GNUNET_TIME_Relative timeout;
203 * MQ Envelope with iterate request message
205 struct GNUNET_MQ_Envelope *ev;
208 * Callback with each matching record
210 GNUNET_PEERSTORE_Processor callback;
213 * Closure for @e callback
218 * #GNUNET_YES / #GNUNET_NO
219 * Iterate request has been sent and we are still expecting records
224 * Task identifier for the function called
225 * on iterate request timeout
227 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
232 * Context for a watch request
234 struct GNUNET_PEERSTORE_WatchContext
239 struct GNUNET_PEERSTORE_WatchContext *next;
244 struct GNUNET_PEERSTORE_WatchContext *prev;
247 * Handle to the PEERSTORE service.
249 struct GNUNET_PEERSTORE_Handle *h;
252 * MQ Envelope with watch request message
254 struct GNUNET_MQ_Envelope *ev;
257 * Callback with each record received
259 GNUNET_PEERSTORE_Processor callback;
262 * Closure for @e callback
267 * Hash of the combined key
269 struct GNUNET_HashCode keyhash;
272 * #GNUNET_YES / #GNUNET_NO
273 * if sent, cannot be canceled
279 /******************************************************************************/
280 /******************* DECLARATIONS *********************/
281 /******************************************************************************/
284 * When a response for iterate request is received
286 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
287 * @param msg message received, NULL on timeout or fatal error
290 handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg);
293 * When a watch record is received
295 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
296 * @param msg message received, NULL on timeout or fatal error
299 handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg);
302 * Close the existing connection to PEERSTORE and reconnect.
304 * @param h handle to the service
307 reconnect (struct GNUNET_PEERSTORE_Handle *h);
310 * Callback after MQ envelope is sent
312 * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *'
315 watch_request_sent (void *cls)
317 struct GNUNET_PEERSTORE_WatchContext *wc = cls;
319 wc->request_sent = GNUNET_YES;
325 * Callback after MQ envelope is sent
327 * @param cls a `struct GNUNET_PEERSTORE_IterateContext *`
330 iterate_request_sent (void *cls)
332 struct GNUNET_PEERSTORE_IterateContext *ic = cls;
334 LOG (GNUNET_ERROR_TYPE_DEBUG, "Iterate request sent to service.\n");
335 ic->iterating = GNUNET_YES;
341 * Callback after MQ envelope is sent
343 * @param cls a `struct GNUNET_PEERSTORE_StoreContext *`
346 store_request_sent (void *cls)
348 struct GNUNET_PEERSTORE_StoreContext *sc = cls;
349 GNUNET_PEERSTORE_Continuation cont;
354 cont_cls = sc->cont_cls;
355 GNUNET_PEERSTORE_store_cancel (sc);
357 cont (cont_cls, GNUNET_OK);
362 * MQ message handlers
364 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
365 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD, 0},
366 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END,
367 sizeof (struct GNUNET_MessageHeader)},
368 {&handle_watch_result, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD, 0},
369 GNUNET_MQ_HANDLERS_END
372 /******************************************************************************/
373 /******************* CONNECTION FUNCTIONS *********************/
374 /******************************************************************************/
377 handle_client_error (void *cls, enum GNUNET_MQ_Error error)
379 struct GNUNET_PEERSTORE_Handle *h = cls;
381 LOG (GNUNET_ERROR_TYPE_ERROR,
382 _("Received an error notification from MQ of type: %d\n"), error);
388 * Iterator over previous watches to resend them
391 rewatch_it (void *cls, const struct GNUNET_HashCode *key, void *value)
393 struct GNUNET_PEERSTORE_Handle *h = cls;
394 struct GNUNET_PEERSTORE_WatchContext *wc = value;
395 struct StoreKeyHashMessage *hm;
397 if (GNUNET_YES == wc->request_sent)
398 { /* Envelope gone, create new one. */
399 wc->ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
400 hm->keyhash = wc->keyhash;
401 wc->request_sent = GNUNET_NO;
403 GNUNET_MQ_notify_sent (wc->ev, &watch_request_sent, wc);
404 GNUNET_MQ_send (h->mq, wc->ev);
410 * Called when the iterate request is timedout
412 * @param cls a `struct GNUNET_PEERSTORE_IterateContext *`
413 * @param tc Scheduler task context (unused)
416 iterate_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
418 struct GNUNET_PEERSTORE_IterateContext *ic = cls;
419 GNUNET_PEERSTORE_Processor callback;
422 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
423 callback = ic->callback;
424 callback_cls = ic->callback_cls;
425 GNUNET_PEERSTORE_iterate_cancel (ic);
426 if (NULL != callback)
427 callback (callback_cls, NULL, _("timeout"));
432 * Close the existing connection to PEERSTORE and reconnect.
434 * @param h handle to the service
437 reconnect (struct GNUNET_PEERSTORE_Handle *h)
439 struct GNUNET_PEERSTORE_IterateContext *ic;
440 GNUNET_PEERSTORE_Processor icb;
442 struct GNUNET_PEERSTORE_StoreContext *sc;
444 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n");
445 for (sc = h->store_head; NULL != sc; sc = sc->next)
449 GNUNET_MQ_send_cancel (sc->ev);
455 GNUNET_MQ_destroy (h->mq);
458 if (NULL != h->client)
460 GNUNET_CLIENT_disconnect (h->client);
463 h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg);
464 GNUNET_assert (NULL != h->client);
466 GNUNET_MQ_queue_for_connection_client (h->client, mq_handlers,
467 &handle_client_error, h);
468 LOG (GNUNET_ERROR_TYPE_DEBUG,
469 "Resending pending requests after reconnect.\n");
470 if (NULL != h->watches)
471 GNUNET_CONTAINER_multihashmap_iterate (h->watches, &rewatch_it, h);
472 for (ic = h->iterate_head; NULL != ic; ic = ic->next)
474 if (GNUNET_YES == ic->iterating)
477 icb_cls = ic->callback_cls;
478 GNUNET_PEERSTORE_iterate_cancel (ic);
480 icb (icb_cls, NULL, _("Iteration canceled due to reconnection."));
484 if (GNUNET_SCHEDULER_NO_TASK != ic->timeout_task)
486 GNUNET_SCHEDULER_cancel (ic->timeout_task);
487 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
491 GNUNET_MQ_send_cancel (ic->ev);
495 PEERSTORE_create_record_mq_envelope (ic->sub_system, &ic->peer,
496 ic->key, NULL, 0, NULL, 0,
497 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
498 GNUNET_MQ_notify_sent (ic->ev, &iterate_request_sent, ic);
499 GNUNET_MQ_send (h->mq, ic->ev);
501 GNUNET_SCHEDULER_add_delayed (ic->timeout, &iterate_timeout, ic);
504 for (sc = h->store_head; NULL != sc; sc = sc->next)
507 PEERSTORE_create_record_mq_envelope (sc->sub_system, &sc->peer, sc->key,
508 sc->value, sc->size, &sc->expiry,
510 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
511 GNUNET_MQ_notify_sent (sc->ev, &store_request_sent, sc);
512 GNUNET_MQ_send (h->mq, sc->ev);
518 * Iterator over watch requests to cancel them.
521 * @param key key to the watch request
522 * @param value watch context
523 * @return #GNUNET_YES to continue iteration
526 destroy_watch (void *cls, const struct GNUNET_HashCode *key, void *value)
528 struct GNUNET_PEERSTORE_WatchContext *wc = value;
530 GNUNET_PEERSTORE_watch_cancel (wc);
536 * Kill the connection to the service. This can be delayed in case of pending
537 * STORE requests and the user explicitly asked to sync first. Otherwise it is
538 * performed instantly.
540 * @param h Handle to the service.
543 do_disconnect (struct GNUNET_PEERSTORE_Handle *h)
547 GNUNET_MQ_destroy (h->mq);
550 if (NULL != h->client)
552 GNUNET_CLIENT_disconnect (h->client);
560 * Connect to the PEERSTORE service.
562 * @param cfg configuration to use
563 * @return NULL on error
565 struct GNUNET_PEERSTORE_Handle *
566 GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
568 struct GNUNET_PEERSTORE_Handle *h;
570 h = GNUNET_new (struct GNUNET_PEERSTORE_Handle);
572 h->client = GNUNET_CLIENT_connect ("peerstore", cfg);
573 if (NULL == h->client)
579 h->disconnecting = GNUNET_NO;
581 GNUNET_MQ_queue_for_connection_client (h->client, mq_handlers,
582 &handle_client_error, h);
585 GNUNET_CLIENT_disconnect (h->client);
589 LOG (GNUNET_ERROR_TYPE_DEBUG, "New connection created\n");
595 * Disconnect from the PEERSTORE service. Any pending ITERATE and WATCH requests
597 * Any pending STORE requests will depend on @e snyc_first flag.
599 * @param h handle to disconnect
600 * @param sync_first send any pending STORE requests before disconnecting
603 GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first)
605 struct GNUNET_PEERSTORE_IterateContext *ic;
606 struct GNUNET_PEERSTORE_IterateContext *ic_iter;
607 struct GNUNET_PEERSTORE_StoreContext *sc;
608 struct GNUNET_PEERSTORE_StoreContext *sc_iter;
610 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting.\n");
611 if (NULL != h->watches)
613 GNUNET_CONTAINER_multihashmap_iterate (h->watches, &destroy_watch, NULL);
614 GNUNET_CONTAINER_multihashmap_destroy (h->watches);
617 ic_iter = h->iterate_head;
618 while (NULL != ic_iter)
622 ic_iter = ic_iter->next;
623 GNUNET_PEERSTORE_iterate_cancel (ic);
625 if (NULL != h->store_head)
627 if (GNUNET_YES == sync_first)
629 LOG (GNUNET_ERROR_TYPE_DEBUG,
630 "Delaying disconnection due to pending store requests.\n");
631 h->disconnecting = GNUNET_YES;
634 sc_iter = h->store_head;
635 while (NULL != sc_iter)
638 sc_iter = sc_iter->next;
639 GNUNET_PEERSTORE_store_cancel (sc);
646 /******************************************************************************/
647 /******************* STORE FUNCTIONS *********************/
648 /******************************************************************************/
652 * Cancel a store request
654 * @param sc Store request context
657 GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
659 struct GNUNET_PEERSTORE_Handle *h = sc->h;
663 GNUNET_MQ_send_cancel (sc->ev);
666 GNUNET_CONTAINER_DLL_remove (sc->h->store_head, sc->h->store_tail, sc);
667 GNUNET_free (sc->sub_system);
668 GNUNET_free (sc->value);
669 GNUNET_free (sc->key);
671 if ((GNUNET_YES == h->disconnecting) && (NULL == h->store_head))
677 * Store a new entry in the PEERSTORE.
678 * Note that stored entries can be lost in some cases
679 * such as power failure.
681 * @param h Handle to the PEERSTORE service
682 * @param sub_system name of the sub system
683 * @param peer Peer Identity
684 * @param key entry key
685 * @param value entry value BLOB
686 * @param size size of @e value
687 * @param expiry absolute time after which the entry is (possibly) deleted
688 * @param options options specific to the storage operation
689 * @param cont Continuation function after the store request is sent
690 * @param cont_cls Closure for @a cont
692 struct GNUNET_PEERSTORE_StoreContext *
693 GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
694 const char *sub_system,
695 const struct GNUNET_PeerIdentity *peer, const char *key,
696 const void *value, size_t size,
697 struct GNUNET_TIME_Absolute expiry,
698 enum GNUNET_PEERSTORE_StoreOption options,
699 GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
701 struct GNUNET_MQ_Envelope *ev;
702 struct GNUNET_PEERSTORE_StoreContext *sc;
704 LOG (GNUNET_ERROR_TYPE_DEBUG,
705 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n",
706 size, sub_system, GNUNET_i2s (peer), key);
707 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, value, size,
709 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
710 sc = GNUNET_new (struct GNUNET_PEERSTORE_StoreContext);
712 sc->sub_system = GNUNET_strdup (sub_system);
714 sc->key = GNUNET_strdup (key);
715 sc->value = GNUNET_memdup (value, size);
718 sc->options = options;
720 sc->cont_cls = cont_cls;
723 GNUNET_CONTAINER_DLL_insert_tail (h->store_head, h->store_tail, sc);
724 GNUNET_MQ_notify_sent (ev, &store_request_sent, sc);
725 GNUNET_MQ_send (h->mq, ev);
731 /******************************************************************************/
732 /******************* ITERATE FUNCTIONS *********************/
733 /******************************************************************************/
736 * When a response for iterate request is received
738 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
739 * @param msg message received, NULL on timeout or fatal error
742 handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg)
744 struct GNUNET_PEERSTORE_Handle *h = cls;
745 struct GNUNET_PEERSTORE_IterateContext *ic;
746 GNUNET_PEERSTORE_Processor callback;
749 struct GNUNET_PEERSTORE_Record *record;
752 ic = h->iterate_head;
755 LOG (GNUNET_ERROR_TYPE_ERROR,
756 _("Unexpected iteration response, this should not happen.\n"));
760 callback = ic->callback;
761 callback_cls = ic->callback_cls;
762 if (NULL == msg) /* Connection error */
764 if (NULL != callback)
765 callback (callback_cls, NULL,
766 _("Error communicating with `PEERSTORE' service."));
770 msg_type = ntohs (msg->type);
771 if (GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END == msg_type)
773 ic->iterating = GNUNET_NO;
774 GNUNET_PEERSTORE_iterate_cancel (ic);
775 if (NULL != callback)
776 callback (callback_cls, NULL, NULL);
779 if (NULL != callback)
781 record = PEERSTORE_parse_record_message (msg);
784 callback (callback_cls, NULL,
785 _("Received a malformed response from service."));
788 continue_iter = callback (callback_cls, record, NULL);
789 PEERSTORE_destroy_record (record);
791 if (GNUNET_NO == continue_iter)
798 * Cancel an iterate request
799 * Please do not call after the iterate request is done
801 * @param ic Iterate request context as returned by GNUNET_PEERSTORE_iterate()
804 GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic)
806 if (GNUNET_SCHEDULER_NO_TASK != ic->timeout_task)
808 GNUNET_SCHEDULER_cancel (ic->timeout_task);
809 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
811 if (GNUNET_NO == ic->iterating)
815 GNUNET_MQ_send_cancel (ic->ev);
818 GNUNET_CONTAINER_DLL_remove (ic->h->iterate_head, ic->h->iterate_tail, ic);
819 GNUNET_free (ic->sub_system);
821 GNUNET_free (ic->key);
830 * Iterate over records matching supplied key information
832 * @param h handle to the PEERSTORE service
833 * @param sub_system name of sub system
834 * @param peer Peer identity (can be NULL)
835 * @param key entry key string (can be NULL)
836 * @param timeout time after which the iterate request is canceled
837 * @param callback function called with each matching record, all NULL's on end
838 * @param callback_cls closure for @a callback
839 * @return Handle to iteration request
841 struct GNUNET_PEERSTORE_IterateContext *
842 GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
843 const char *sub_system,
844 const struct GNUNET_PeerIdentity *peer,
845 const char *key, struct GNUNET_TIME_Relative timeout,
846 GNUNET_PEERSTORE_Processor callback,
849 struct GNUNET_MQ_Envelope *ev;
850 struct GNUNET_PEERSTORE_IterateContext *ic;
852 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, NULL, 0,
854 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
855 ic = GNUNET_new (struct GNUNET_PEERSTORE_IterateContext);
857 ic->callback = callback;
858 ic->callback_cls = callback_cls;
861 ic->sub_system = GNUNET_strdup (sub_system);
865 ic->key = GNUNET_strdup (key);
866 ic->timeout = timeout;
867 ic->iterating = GNUNET_NO;
868 GNUNET_CONTAINER_DLL_insert_tail (h->iterate_head, h->iterate_tail, ic);
869 LOG (GNUNET_ERROR_TYPE_DEBUG,
870 "Sending an iterate request for sub system `%s'\n", sub_system);
871 GNUNET_MQ_notify_sent (ev, &iterate_request_sent, ic);
872 GNUNET_MQ_send (h->mq, ev);
874 GNUNET_SCHEDULER_add_delayed (timeout, &iterate_timeout, ic);
879 /******************************************************************************/
880 /******************* WATCH FUNCTIONS *********************/
881 /******************************************************************************/
884 * When a watch record is received
886 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
887 * @param msg message received, NULL on timeout or fatal error
890 handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg)
892 struct GNUNET_PEERSTORE_Handle *h = cls;
893 struct GNUNET_PEERSTORE_Record *record;
894 struct GNUNET_HashCode keyhash;
895 struct GNUNET_PEERSTORE_WatchContext *wc;
899 LOG (GNUNET_ERROR_TYPE_ERROR,
901 ("Problem receiving a watch response, no way to determine which request.\n"));
905 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a watch record from service.\n");
906 record = PEERSTORE_parse_record_message (msg);
907 PEERSTORE_hash_key (record->sub_system, record->peer, record->key, &keyhash);
908 wc = GNUNET_CONTAINER_multihashmap_get (h->watches, &keyhash);
911 LOG (GNUNET_ERROR_TYPE_ERROR,
912 _("Received a watch result for a non existing watch.\n"));
913 PEERSTORE_destroy_record (record);
917 if (NULL != wc->callback)
918 wc->callback (wc->callback_cls, record, NULL);
919 PEERSTORE_destroy_record (record);
924 * Cancel a watch request
926 * @param wc handle to the watch request
929 GNUNET_PEERSTORE_watch_cancel (struct GNUNET_PEERSTORE_WatchContext *wc)
931 struct GNUNET_PEERSTORE_Handle *h = wc->h;
932 struct GNUNET_MQ_Envelope *ev;
933 struct StoreKeyHashMessage *hm;
935 LOG (GNUNET_ERROR_TYPE_DEBUG, "Canceling watch.\n");
936 if (GNUNET_YES == wc->request_sent) /* If request already sent to service, send a cancel request. */
938 ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL);
939 hm->keyhash = wc->keyhash;
940 GNUNET_MQ_send (h->mq, ev);
942 wc->callback_cls = NULL;
946 GNUNET_MQ_send_cancel (wc->ev);
949 GNUNET_CONTAINER_multihashmap_remove (h->watches, &wc->keyhash, wc);
956 * Request watching a given key
957 * User will be notified with any new values added to key
959 * @param h handle to the PEERSTORE service
960 * @param sub_system name of sub system
961 * @param peer Peer identity
962 * @param key entry key string
963 * @param callback function called with each new value
964 * @param callback_cls closure for @a callback
965 * @return Handle to watch request
967 struct GNUNET_PEERSTORE_WatchContext *
968 GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h,
969 const char *sub_system,
970 const struct GNUNET_PeerIdentity *peer, const char *key,
971 GNUNET_PEERSTORE_Processor callback, void *callback_cls)
973 struct GNUNET_MQ_Envelope *ev;
974 struct StoreKeyHashMessage *hm;
975 struct GNUNET_PEERSTORE_WatchContext *wc;
977 GNUNET_assert (NULL != sub_system);
978 GNUNET_assert (NULL != peer);
979 GNUNET_assert (NULL != key);
980 ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
981 PEERSTORE_hash_key (sub_system, peer, key, &hm->keyhash);
982 wc = GNUNET_new (struct GNUNET_PEERSTORE_WatchContext);
984 wc->callback = callback;
985 wc->callback_cls = callback_cls;
988 wc->request_sent = GNUNET_NO;
989 wc->keyhash = hm->keyhash;
990 if (NULL == h->watches)
991 h->watches = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
992 GNUNET_CONTAINER_multihashmap_put (h->watches, &wc->keyhash, wc,
993 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
994 LOG (GNUNET_ERROR_TYPE_DEBUG,
995 "Sending a watch request for ss `%s', peer `%s', key `%s'.\n",
996 sub_system, GNUNET_i2s (peer), key);
997 GNUNET_MQ_notify_sent (ev, &watch_request_sent, wc);
998 GNUNET_MQ_send (h->mq, ev);
1002 /* end of peerstore_api.c */