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 struct GNUNET_PEERSTORE_IterateContext *ic_tmp;
441 GNUNET_PEERSTORE_Processor icb;
443 struct GNUNET_PEERSTORE_StoreContext *sc;
445 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n");
446 for (sc = h->store_head; NULL != sc; sc = sc->next)
450 GNUNET_MQ_send_cancel (sc->ev);
454 ic = h->iterate_head;
457 if (GNUNET_YES == ic->iterating)
460 icb_cls = ic->callback_cls;
461 ic->iterating = GNUNET_NO;
464 GNUNET_PEERSTORE_iterate_cancel (ic_tmp);
466 icb (icb_cls, NULL, _("Iteration canceled due to reconnection."));
470 if (GNUNET_SCHEDULER_NO_TASK != ic->timeout_task)
472 GNUNET_SCHEDULER_cancel (ic->timeout_task);
473 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
477 GNUNET_MQ_send_cancel (ic->ev);
485 GNUNET_MQ_destroy (h->mq);
488 if (NULL != h->client)
490 GNUNET_CLIENT_disconnect (h->client);
493 h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg);
494 GNUNET_assert (NULL != h->client);
496 GNUNET_MQ_queue_for_connection_client (h->client, mq_handlers,
497 &handle_client_error, h);
498 LOG (GNUNET_ERROR_TYPE_DEBUG,
499 "Resending pending requests after reconnect.\n");
500 if (NULL != h->watches)
501 GNUNET_CONTAINER_multihashmap_iterate (h->watches, &rewatch_it, h);
502 for (ic = h->iterate_head; NULL != ic; ic = ic->next)
505 PEERSTORE_create_record_mq_envelope (ic->sub_system, &ic->peer, ic->key,
507 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
508 GNUNET_MQ_notify_sent (ic->ev, &iterate_request_sent, ic);
509 GNUNET_MQ_send (h->mq, ic->ev);
511 GNUNET_SCHEDULER_add_delayed (ic->timeout, &iterate_timeout, ic);
513 for (sc = h->store_head; NULL != sc; sc = sc->next)
516 PEERSTORE_create_record_mq_envelope (sc->sub_system, &sc->peer, sc->key,
517 sc->value, sc->size, &sc->expiry,
519 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
520 GNUNET_MQ_notify_sent (sc->ev, &store_request_sent, sc);
521 GNUNET_MQ_send (h->mq, sc->ev);
527 * Iterator over watch requests to cancel them.
530 * @param key key to the watch request
531 * @param value watch context
532 * @return #GNUNET_YES to continue iteration
535 destroy_watch (void *cls, const struct GNUNET_HashCode *key, void *value)
537 struct GNUNET_PEERSTORE_WatchContext *wc = value;
539 GNUNET_PEERSTORE_watch_cancel (wc);
545 * Kill the connection to the service. This can be delayed in case of pending
546 * STORE requests and the user explicitly asked to sync first. Otherwise it is
547 * performed instantly.
549 * @param h Handle to the service.
552 do_disconnect (struct GNUNET_PEERSTORE_Handle *h)
556 GNUNET_MQ_destroy (h->mq);
559 if (NULL != h->client)
561 GNUNET_CLIENT_disconnect (h->client);
569 * Connect to the PEERSTORE service.
571 * @param cfg configuration to use
572 * @return NULL on error
574 struct GNUNET_PEERSTORE_Handle *
575 GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
577 struct GNUNET_PEERSTORE_Handle *h;
579 h = GNUNET_new (struct GNUNET_PEERSTORE_Handle);
581 h->client = GNUNET_CLIENT_connect ("peerstore", cfg);
582 if (NULL == h->client)
588 h->disconnecting = GNUNET_NO;
590 GNUNET_MQ_queue_for_connection_client (h->client, mq_handlers,
591 &handle_client_error, h);
594 GNUNET_CLIENT_disconnect (h->client);
598 LOG (GNUNET_ERROR_TYPE_DEBUG, "New connection created\n");
604 * Disconnect from the PEERSTORE service. Any pending ITERATE and WATCH requests
606 * Any pending STORE requests will depend on @e snyc_first flag.
608 * @param h handle to disconnect
609 * @param sync_first send any pending STORE requests before disconnecting
612 GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first)
614 struct GNUNET_PEERSTORE_IterateContext *ic;
615 struct GNUNET_PEERSTORE_IterateContext *ic_iter;
616 struct GNUNET_PEERSTORE_StoreContext *sc;
617 struct GNUNET_PEERSTORE_StoreContext *sc_iter;
619 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting.\n");
620 if (NULL != h->watches)
622 GNUNET_CONTAINER_multihashmap_iterate (h->watches, &destroy_watch, NULL);
623 GNUNET_CONTAINER_multihashmap_destroy (h->watches);
626 ic_iter = h->iterate_head;
627 while (NULL != ic_iter)
631 ic_iter = ic_iter->next;
632 GNUNET_PEERSTORE_iterate_cancel (ic);
634 if (NULL != h->store_head)
636 if (GNUNET_YES == sync_first)
638 LOG (GNUNET_ERROR_TYPE_DEBUG,
639 "Delaying disconnection due to pending store requests.\n");
640 h->disconnecting = GNUNET_YES;
643 sc_iter = h->store_head;
644 while (NULL != sc_iter)
647 sc_iter = sc_iter->next;
648 GNUNET_PEERSTORE_store_cancel (sc);
655 /******************************************************************************/
656 /******************* STORE FUNCTIONS *********************/
657 /******************************************************************************/
661 * Cancel a store request
663 * @param sc Store request context
666 GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
668 struct GNUNET_PEERSTORE_Handle *h = sc->h;
672 GNUNET_MQ_send_cancel (sc->ev);
675 GNUNET_CONTAINER_DLL_remove (sc->h->store_head, sc->h->store_tail, sc);
676 GNUNET_free (sc->sub_system);
677 GNUNET_free (sc->value);
678 GNUNET_free (sc->key);
680 if ((GNUNET_YES == h->disconnecting) && (NULL == h->store_head))
686 * Store a new entry in the PEERSTORE.
687 * Note that stored entries can be lost in some cases
688 * such as power failure.
690 * @param h Handle to the PEERSTORE service
691 * @param sub_system name of the sub system
692 * @param peer Peer Identity
693 * @param key entry key
694 * @param value entry value BLOB
695 * @param size size of @e value
696 * @param expiry absolute time after which the entry is (possibly) deleted
697 * @param options options specific to the storage operation
698 * @param cont Continuation function after the store request is sent
699 * @param cont_cls Closure for @a cont
701 struct GNUNET_PEERSTORE_StoreContext *
702 GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
703 const char *sub_system,
704 const struct GNUNET_PeerIdentity *peer, const char *key,
705 const void *value, size_t size,
706 struct GNUNET_TIME_Absolute expiry,
707 enum GNUNET_PEERSTORE_StoreOption options,
708 GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
710 struct GNUNET_MQ_Envelope *ev;
711 struct GNUNET_PEERSTORE_StoreContext *sc;
713 LOG (GNUNET_ERROR_TYPE_DEBUG,
714 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n",
715 size, sub_system, GNUNET_i2s (peer), key);
716 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, value, size,
718 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
719 sc = GNUNET_new (struct GNUNET_PEERSTORE_StoreContext);
721 sc->sub_system = GNUNET_strdup (sub_system);
723 sc->key = GNUNET_strdup (key);
724 sc->value = GNUNET_memdup (value, size);
727 sc->options = options;
729 sc->cont_cls = cont_cls;
732 GNUNET_CONTAINER_DLL_insert_tail (h->store_head, h->store_tail, sc);
733 GNUNET_MQ_notify_sent (ev, &store_request_sent, sc);
734 GNUNET_MQ_send (h->mq, ev);
740 /******************************************************************************/
741 /******************* ITERATE FUNCTIONS *********************/
742 /******************************************************************************/
745 * When a response for iterate request is received
747 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
748 * @param msg message received, NULL on timeout or fatal error
751 handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg)
753 struct GNUNET_PEERSTORE_Handle *h = cls;
754 struct GNUNET_PEERSTORE_IterateContext *ic;
755 GNUNET_PEERSTORE_Processor callback;
758 struct GNUNET_PEERSTORE_Record *record;
761 ic = h->iterate_head;
764 LOG (GNUNET_ERROR_TYPE_ERROR,
765 _("Unexpected iteration response, this should not happen.\n"));
769 callback = ic->callback;
770 callback_cls = ic->callback_cls;
771 if (NULL == msg) /* Connection error */
773 if (NULL != callback)
774 callback (callback_cls, NULL,
775 _("Error communicating with `PEERSTORE' service."));
779 msg_type = ntohs (msg->type);
780 if (GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END == msg_type)
782 ic->iterating = GNUNET_NO;
783 GNUNET_PEERSTORE_iterate_cancel (ic);
784 if (NULL != callback)
785 callback (callback_cls, NULL, NULL);
788 if (NULL != callback)
790 record = PEERSTORE_parse_record_message (msg);
793 callback (callback_cls, NULL,
794 _("Received a malformed response from service."));
797 continue_iter = callback (callback_cls, record, NULL);
798 PEERSTORE_destroy_record (record);
800 if (GNUNET_NO == continue_iter)
807 * Cancel an iterate request
808 * Please do not call after the iterate request is done
810 * @param ic Iterate request context as returned by GNUNET_PEERSTORE_iterate()
813 GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic)
815 if (GNUNET_SCHEDULER_NO_TASK != ic->timeout_task)
817 GNUNET_SCHEDULER_cancel (ic->timeout_task);
818 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
820 if (GNUNET_NO == ic->iterating)
824 GNUNET_MQ_send_cancel (ic->ev);
827 GNUNET_CONTAINER_DLL_remove (ic->h->iterate_head, ic->h->iterate_tail, ic);
828 GNUNET_free (ic->sub_system);
830 GNUNET_free (ic->key);
839 * Iterate over records matching supplied key information
841 * @param h handle to the PEERSTORE service
842 * @param sub_system name of sub system
843 * @param peer Peer identity (can be NULL)
844 * @param key entry key string (can be NULL)
845 * @param timeout time after which the iterate request is canceled
846 * @param callback function called with each matching record, all NULL's on end
847 * @param callback_cls closure for @a callback
848 * @return Handle to iteration request
850 struct GNUNET_PEERSTORE_IterateContext *
851 GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
852 const char *sub_system,
853 const struct GNUNET_PeerIdentity *peer,
854 const char *key, struct GNUNET_TIME_Relative timeout,
855 GNUNET_PEERSTORE_Processor callback,
858 struct GNUNET_MQ_Envelope *ev;
859 struct GNUNET_PEERSTORE_IterateContext *ic;
861 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, NULL, 0,
863 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
864 ic = GNUNET_new (struct GNUNET_PEERSTORE_IterateContext);
866 ic->callback = callback;
867 ic->callback_cls = callback_cls;
870 ic->sub_system = GNUNET_strdup (sub_system);
874 ic->key = GNUNET_strdup (key);
875 ic->timeout = timeout;
876 ic->iterating = GNUNET_NO;
877 GNUNET_CONTAINER_DLL_insert_tail (h->iterate_head, h->iterate_tail, ic);
878 LOG (GNUNET_ERROR_TYPE_DEBUG,
879 "Sending an iterate request for sub system `%s'\n", sub_system);
880 GNUNET_MQ_notify_sent (ev, &iterate_request_sent, ic);
881 GNUNET_MQ_send (h->mq, ev);
883 GNUNET_SCHEDULER_add_delayed (timeout, &iterate_timeout, ic);
888 /******************************************************************************/
889 /******************* WATCH FUNCTIONS *********************/
890 /******************************************************************************/
893 * When a watch record is received
895 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
896 * @param msg message received, NULL on timeout or fatal error
899 handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg)
901 struct GNUNET_PEERSTORE_Handle *h = cls;
902 struct GNUNET_PEERSTORE_Record *record;
903 struct GNUNET_HashCode keyhash;
904 struct GNUNET_PEERSTORE_WatchContext *wc;
908 LOG (GNUNET_ERROR_TYPE_ERROR,
910 ("Problem receiving a watch response, no way to determine which request.\n"));
914 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a watch record from service.\n");
915 record = PEERSTORE_parse_record_message (msg);
916 PEERSTORE_hash_key (record->sub_system, record->peer, record->key, &keyhash);
917 wc = GNUNET_CONTAINER_multihashmap_get (h->watches, &keyhash);
920 LOG (GNUNET_ERROR_TYPE_ERROR,
921 _("Received a watch result for a non existing watch.\n"));
922 PEERSTORE_destroy_record (record);
926 if (NULL != wc->callback)
927 wc->callback (wc->callback_cls, record, NULL);
928 PEERSTORE_destroy_record (record);
933 * Cancel a watch request
935 * @param wc handle to the watch request
938 GNUNET_PEERSTORE_watch_cancel (struct GNUNET_PEERSTORE_WatchContext *wc)
940 struct GNUNET_PEERSTORE_Handle *h = wc->h;
941 struct GNUNET_MQ_Envelope *ev;
942 struct StoreKeyHashMessage *hm;
944 LOG (GNUNET_ERROR_TYPE_DEBUG, "Canceling watch.\n");
945 if (GNUNET_YES == wc->request_sent) /* If request already sent to service, send a cancel request. */
947 ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL);
948 hm->keyhash = wc->keyhash;
949 GNUNET_MQ_send (h->mq, ev);
951 wc->callback_cls = NULL;
955 GNUNET_MQ_send_cancel (wc->ev);
958 GNUNET_CONTAINER_multihashmap_remove (h->watches, &wc->keyhash, wc);
965 * Request watching a given key
966 * User will be notified with any new values added to key
968 * @param h handle to the PEERSTORE service
969 * @param sub_system name of sub system
970 * @param peer Peer identity
971 * @param key entry key string
972 * @param callback function called with each new value
973 * @param callback_cls closure for @a callback
974 * @return Handle to watch request
976 struct GNUNET_PEERSTORE_WatchContext *
977 GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h,
978 const char *sub_system,
979 const struct GNUNET_PeerIdentity *peer, const char *key,
980 GNUNET_PEERSTORE_Processor callback, void *callback_cls)
982 struct GNUNET_MQ_Envelope *ev;
983 struct StoreKeyHashMessage *hm;
984 struct GNUNET_PEERSTORE_WatchContext *wc;
986 GNUNET_assert (NULL != sub_system);
987 GNUNET_assert (NULL != peer);
988 GNUNET_assert (NULL != key);
989 ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
990 PEERSTORE_hash_key (sub_system, peer, key, &hm->keyhash);
991 wc = GNUNET_new (struct GNUNET_PEERSTORE_WatchContext);
993 wc->callback = callback;
994 wc->callback_cls = callback_cls;
997 wc->request_sent = GNUNET_NO;
998 wc->keyhash = hm->keyhash;
999 if (NULL == h->watches)
1000 h->watches = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
1001 GNUNET_CONTAINER_multihashmap_put (h->watches, &wc->keyhash, wc,
1002 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1003 LOG (GNUNET_ERROR_TYPE_DEBUG,
1004 "Sending a watch request for ss `%s', peer `%s', key `%s'.\n",
1005 sub_system, GNUNET_i2s (peer), key);
1006 GNUNET_MQ_notify_sent (ev, &watch_request_sent, wc);
1007 GNUNET_MQ_send (h->mq, ev);
1011 /* end of peerstore_api.c */