X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fpeerstore%2Fpeerstore_api.c;h=2138b23ff322033757e4aefa6c457157ef90c47c;hb=0f649608b4d8c3f82fd1f240bc0d28c2713c7699;hp=959d2488a733379fa8c3c9459e8cd3241465c83f;hpb=fbfdc8a66888a992517ee61bfb9c39784b2cee83;p=oweals%2Fgnunet.git diff --git a/src/peerstore/peerstore_api.c b/src/peerstore/peerstore_api.c index 959d2488a..2138b23ff 100644 --- a/src/peerstore/peerstore_api.c +++ b/src/peerstore/peerstore_api.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - (C) + (C) 2013-2014 Christian Grothoff (and other contributing authors) GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -17,15 +17,16 @@ Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - /** * @file peerstore/peerstore_api.c * @brief API for peerstore * @author Omar Tarabai + * @author Christian Grothoff */ #include "platform.h" #include "gnunet_util_lib.h" #include "peerstore.h" +#include "peerstore_common.h" #define LOG(kind,...) GNUNET_log_from (kind, "peerstore-api",__VA_ARGS__) @@ -42,7 +43,7 @@ struct GNUNET_PEERSTORE_Handle /** * Our configuration. */ - const struct GNUNET_CONFIGURATION_Handle *cfg; + const struct GNUNET_CONFIGURATION_Handle *cfg; /** * Connection to the service. @@ -50,57 +51,56 @@ struct GNUNET_PEERSTORE_Handle struct GNUNET_CLIENT_Connection *client; /** - * Head of transmission queue. + * Message queue */ - struct GNUNET_PEERSTORE_RequestContext *rc_head; + struct GNUNET_MQ_Handle *mq; /** - * Tail of transmission queue. + * Head of active STORE requests. */ - struct GNUNET_PEERSTORE_RequestContext *rc_tail; + struct GNUNET_PEERSTORE_StoreContext *store_head; /** - * Handle for the current transmission request, or NULL if none is pending. + * Tail of active STORE requests. */ - struct GNUNET_CLIENT_TransmitHandle *th; + struct GNUNET_PEERSTORE_StoreContext *store_tail; /** - * Head of store requests DLL. + * Head of active ITERATE requests. */ - struct GNUNET_PEERSTORE_StoreContext *sc_head; + struct GNUNET_PEERSTORE_IterateContext *iterate_head; /** - * Tail of store requests DLL. + * Tail of active ITERATE requests. */ - struct GNUNET_PEERSTORE_StoreContext *sc_tail; + struct GNUNET_PEERSTORE_IterateContext *iterate_tail; /** - * ID for a reconnect task. + * Hashmap of watch requests */ - GNUNET_SCHEDULER_TaskIdentifier r_task; + struct GNUNET_CONTAINER_MultiHashMap *watches; /** - * Are we now receiving? + * Are we in the process of disconnecting but need to sync first? */ - int in_receive; + int disconnecting; }; /** - * Entry in the transmission queue to PEERSTORE service. - * + * Context for a store request */ -struct GNUNET_PEERSTORE_RequestContext +struct GNUNET_PEERSTORE_StoreContext { /** - * This is a linked list. + * Kept in a DLL. */ - struct GNUNET_PEERSTORE_RequestContext *next; + struct GNUNET_PEERSTORE_StoreContext *next; /** - * This is a linked list. + * Kept in a DLL. */ - struct GNUNET_PEERSTORE_RequestContext *prev; + struct GNUNET_PEERSTORE_StoreContext *prev; /** * Handle to the PEERSTORE service. @@ -108,37 +108,66 @@ struct GNUNET_PEERSTORE_RequestContext struct GNUNET_PEERSTORE_Handle *h; /** - * Function to call after request has been transmitted, or NULL. + * Continuation called with service response */ GNUNET_PEERSTORE_Continuation cont; /** - * Closure for 'cont'. + * Closure for @e cont */ void *cont_cls; /** - * Number of bytes of the request message (follows after this struct). + * Which subsystem does the store? + */ + char *sub_system; + + /** + * Key for the store operation. + */ + char *key; + + /** + * Contains @e size bytes. + */ + void *value; + + /** + * Peer the store is for. + */ + struct GNUNET_PeerIdentity peer; + + /** + * Number of bytes in @e value. */ size_t size; + /** + * When does the value expire? + */ + struct GNUNET_TIME_Absolute expiry; + + /** + * Options for the store operation. + */ + enum GNUNET_PEERSTORE_StoreOption options; + }; /** - * Context for a store request - * + * Context for a iterate request */ -struct GNUNET_PEERSTORE_StoreContext +struct GNUNET_PEERSTORE_IterateContext { /** * Kept in a DLL. */ - struct GNUNET_PEERSTORE_StoreContext *next; + struct GNUNET_PEERSTORE_IterateContext *next; /** * Kept in a DLL. */ - struct GNUNET_PEERSTORE_StoreContext *prev; + struct GNUNET_PEERSTORE_IterateContext *prev; /** * Handle to the PEERSTORE service. @@ -146,32 +175,107 @@ struct GNUNET_PEERSTORE_StoreContext struct GNUNET_PEERSTORE_Handle *h; /** - * Our entry in the transmission queue. + * Which subsystem does the store? */ - struct GNUNET_PEERSTORE_RequestContext *rc; + char *sub_system; /** - * Function to call with store operation result + * Peer the store is for. */ - GNUNET_PEERSTORE_Continuation cont; + struct GNUNET_PeerIdentity peer; /** - * Closure for 'cont'. + * Key for the store operation. */ - void *cont_cls; + char *key; + + /** + * Operation timeout + */ + struct GNUNET_TIME_Relative timeout; + + /** + * Callback with each matching record + */ + GNUNET_PEERSTORE_Processor callback; + + /** + * Closure for @e callback + */ + void *callback_cls; + + /** + * #GNUNET_YES if we are currently processing records. + */ + int iterating; + + /** + * Task identifier for the function called + * on iterate request timeout + */ + struct GNUNET_SCHEDULER_Task * timeout_task; + +}; + +/** + * Context for a watch request + */ +struct GNUNET_PEERSTORE_WatchContext +{ + /** + * Kept in a DLL. + */ + struct GNUNET_PEERSTORE_WatchContext *next; + + /** + * Kept in a DLL. + */ + struct GNUNET_PEERSTORE_WatchContext *prev; + + /** + * Handle to the PEERSTORE service. + */ + struct GNUNET_PEERSTORE_Handle *h; + + /** + * Callback with each record received + */ + GNUNET_PEERSTORE_Processor callback; + + /** + * Closure for @e callback + */ + void *callback_cls; /** - * Set to GNUNET_YES if we are currently receiving replies from the - * service. + * Hash of the combined key */ - int request_transmitted; + struct GNUNET_HashCode keyhash; }; /******************************************************************************/ -/*********************** DECLARATIONS *************************/ +/******************* DECLARATIONS *********************/ /******************************************************************************/ +/** + * When a response for iterate request is received + * + * @param cls a 'struct GNUNET_PEERSTORE_Handle *' + * @param msg message received, NULL on timeout or fatal error + */ +static void +handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg); + +/** + * When a watch record is received + * + * @param cls a 'struct GNUNET_PEERSTORE_Handle *' + * @param msg message received, NULL on timeout or fatal error + */ +static void +handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg); + /** * Close the existing connection to PEERSTORE and reconnect. * @@ -180,216 +284,300 @@ struct GNUNET_PEERSTORE_StoreContext static void reconnect (struct GNUNET_PEERSTORE_Handle *h); + /** - * Check if we have a request pending in the transmission queue and are - * able to transmit it right now. If so, schedule transmission. + * Callback after MQ envelope is sent * - * @param h handle to the service + * @param cls a `struct GNUNET_PEERSTORE_StoreContext *` */ static void -trigger_transmit (struct GNUNET_PEERSTORE_Handle *h); +store_request_sent (void *cls) +{ + struct GNUNET_PEERSTORE_StoreContext *sc = cls; + GNUNET_PEERSTORE_Continuation cont; + void *cont_cls; + + cont = sc->cont; + cont_cls = sc->cont_cls; + GNUNET_PEERSTORE_store_cancel (sc); + if (NULL != cont) + cont (cont_cls, GNUNET_OK); +} + + +/** + * MQ message handlers + */ +static const struct GNUNET_MQ_MessageHandler mq_handlers[] = { + {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD, 0}, + {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END, + sizeof (struct GNUNET_MessageHeader)}, + {&handle_watch_result, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD, 0}, + GNUNET_MQ_HANDLERS_END +}; /******************************************************************************/ /******************* CONNECTION FUNCTIONS *********************/ /******************************************************************************/ -/** - * Task scheduled to re-try connecting to the peerstore service. - * - * @param cls the 'struct GNUNET_PEERSTORE_Handle' - * @param tc scheduler context - */ static void -reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +handle_client_error (void *cls, enum GNUNET_MQ_Error error) { struct GNUNET_PEERSTORE_Handle *h = cls; - LOG(GNUNET_ERROR_TYPE_DEBUG, "Reconnect task executed\n"); - h->r_task = GNUNET_SCHEDULER_NO_TASK; + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Received an error notification from MQ of type: %d\n"), error); reconnect (h); } + /** - * Connect to the PEERSTORE service. + * Iterator over previous watches to resend them * - * @return NULL on error + * @param cls the `struct GNUNET_PEERSTORE_Handle` + * @param key key for the watch + * @param value the `struct GNUNET_PEERSTORE_WatchContext *` + * @return #GNUNET_YES (continue to iterate) */ -struct GNUNET_PEERSTORE_Handle * -GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) +static int +rewatch_it (void *cls, const struct GNUNET_HashCode *key, void *value) { - struct GNUNET_CLIENT_Connection *client; - struct GNUNET_PEERSTORE_Handle *h; + struct GNUNET_PEERSTORE_Handle *h = cls; + struct GNUNET_PEERSTORE_WatchContext *wc = value; + struct StoreKeyHashMessage *hm; + struct GNUNET_MQ_Envelope *ev; + + ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH); + hm->keyhash = wc->keyhash; + GNUNET_MQ_send (h->mq, ev); + return GNUNET_YES; +} - client = GNUNET_CLIENT_connect ("peerstore", cfg); - if(NULL == client) - return NULL; - h = GNUNET_new (struct GNUNET_PEERSTORE_Handle); - h->client = client; - h->cfg = cfg; - LOG(GNUNET_ERROR_TYPE_DEBUG, "New connection created\n"); - return h; + +/** + * Called when the iterate request is timedout + * + * @param cls a `struct GNUNET_PEERSTORE_IterateContext *` + * @param tc Scheduler task context (unused) + */ +static void +iterate_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + struct GNUNET_PEERSTORE_IterateContext *ic = cls; + GNUNET_PEERSTORE_Processor callback; + void *callback_cls; + + ic->timeout_task = NULL; + callback = ic->callback; + callback_cls = ic->callback_cls; + GNUNET_PEERSTORE_iterate_cancel (ic); + if (NULL != callback) + callback (callback_cls, NULL, _("timeout")); } + /** - * Disconnect from the PEERSTORE service + * Close the existing connection to PEERSTORE and reconnect. * - * @param h handle to disconnect + * @param h handle to the service */ -void -GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h) +static void +reconnect (struct GNUNET_PEERSTORE_Handle *h) { + struct GNUNET_PEERSTORE_IterateContext *ic; + struct GNUNET_PEERSTORE_IterateContext *next; + GNUNET_PEERSTORE_Processor icb; + void *icb_cls; struct GNUNET_PEERSTORE_StoreContext *sc; - struct GNUNET_PEERSTORE_RequestContext *rc; + struct GNUNET_MQ_Envelope *ev; - while (NULL != (sc = h->sc_head)) + LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n"); + for (ic = h->iterate_head; NULL != ic; ic = next) { - GNUNET_break (GNUNET_YES == sc->request_transmitted); - sc->request_transmitted = GNUNET_NO; - GNUNET_PEERSTORE_store_cancel(sc); + next = ic->next; + icb = ic->callback; + icb_cls = ic->callback_cls; + GNUNET_PEERSTORE_iterate_cancel (ic); + if (NULL != icb) + icb (icb_cls, NULL, _("Iteration canceled due to reconnection.")); } - while (NULL != (rc = h->rc_head)) + if (NULL != h->mq) { - GNUNET_CONTAINER_DLL_remove (h->rc_head, h->rc_tail, rc); - if (NULL != rc->cont) - rc->cont (rc->cont_cls, _("aborted due to explicit disconnect request")); - GNUNET_free (rc); - } - if (NULL != h->th) - { - GNUNET_CLIENT_notify_transmit_ready_cancel (h->th); - h->th = NULL; + GNUNET_MQ_destroy (h->mq); + h->mq = NULL; } if (NULL != h->client) { GNUNET_CLIENT_disconnect (h->client); h->client = NULL; } - if (GNUNET_SCHEDULER_NO_TASK != h->r_task) + + h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg); + GNUNET_assert (NULL != h->client); + h->mq = + GNUNET_MQ_queue_for_connection_client (h->client, mq_handlers, + &handle_client_error, h); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Resending pending requests after reconnect.\n"); + if (NULL != h->watches) + GNUNET_CONTAINER_multihashmap_iterate (h->watches, &rewatch_it, h); + for (ic = h->iterate_head; NULL != ic; ic = ic->next) { - GNUNET_SCHEDULER_cancel (h->r_task); - h->r_task = GNUNET_SCHEDULER_NO_TASK; + ev = + PEERSTORE_create_record_mq_envelope (ic->sub_system, &ic->peer, ic->key, + NULL, 0, NULL, 0, + GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE); + GNUNET_MQ_send (h->mq, ev); + ic->timeout_task = + GNUNET_SCHEDULER_add_delayed (ic->timeout, &iterate_timeout, ic); + } + for (sc = h->store_head; NULL != sc; sc = sc->next) + { + ev = + PEERSTORE_create_record_mq_envelope (sc->sub_system, &sc->peer, sc->key, + sc->value, sc->size, &sc->expiry, + sc->options, + GNUNET_MESSAGE_TYPE_PEERSTORE_STORE); + GNUNET_MQ_notify_sent (ev, &store_request_sent, sc); + GNUNET_MQ_send (h->mq, ev); } - GNUNET_free (h); - LOG(GNUNET_ERROR_TYPE_DEBUG, "Disconnected, BYE!\n"); } + /** - * Close the existing connection to PEERSTORE and reconnect. + * Iterator over watch requests to cancel them. * - * @param h handle to the service + * @param cls unsused + * @param key key to the watch request + * @param value watch context + * @return #GNUNET_YES to continue iteration + */ +static int +destroy_watch (void *cls, const struct GNUNET_HashCode *key, void *value) +{ + struct GNUNET_PEERSTORE_WatchContext *wc = value; + + GNUNET_PEERSTORE_watch_cancel (wc); + return GNUNET_YES; +} + + +/** + * Kill the connection to the service. This can be delayed in case of pending + * STORE requests and the user explicitly asked to sync first. Otherwise it is + * performed instantly. + * + * @param h Handle to the service. */ static void -reconnect (struct GNUNET_PEERSTORE_Handle *h) +do_disconnect (struct GNUNET_PEERSTORE_Handle *h) { - LOG(GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n"); - if (GNUNET_SCHEDULER_NO_TASK != h->r_task) + if (NULL != h->mq) { - GNUNET_SCHEDULER_cancel (h->r_task); - h->r_task = GNUNET_SCHEDULER_NO_TASK; - } - if (NULL != h->th) - { - GNUNET_CLIENT_notify_transmit_ready_cancel (h->th); - h->th = NULL; + GNUNET_MQ_destroy (h->mq); + h->mq = NULL; } if (NULL != h->client) { GNUNET_CLIENT_disconnect (h->client); h->client = NULL; } - h->in_receive = GNUNET_NO; - h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg); - if (NULL == h->client) - { - h->r_task = - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task, - h); - return; - } - trigger_transmit (h); + GNUNET_free (h); } + /** - * Transmit the request at the head of the transmission queue - * and trigger continuation (if any). + * Connect to the PEERSTORE service. * - * @param cls the 'struct GNUNET_PEERSTORE_Handle' (with the queue) - * @param size size of the buffer (0 on error) - * @param buf where to copy the message - * @return number of bytes copied to buf + * @param cfg configuration to use + * @return NULL on error */ -static size_t -do_transmit (void *cls, size_t size, void *buf) +struct GNUNET_PEERSTORE_Handle * +GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) { - struct GNUNET_PEERSTORE_Handle *h = cls; - struct GNUNET_PEERSTORE_RequestContext *rc = h->rc_head; - size_t ret; + struct GNUNET_PEERSTORE_Handle *h; + + h = GNUNET_new (struct GNUNET_PEERSTORE_Handle); - h->th = NULL; - if (NULL == rc) - return 0; /* request was canceled in the meantime */ - if (NULL == buf) + h->client = GNUNET_CLIENT_connect ("peerstore", cfg); + if (NULL == h->client) { - /* peerstore service died */ - LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, - "Failed to transmit message to `%s' service.\n", "PEERSTORE"); - GNUNET_CONTAINER_DLL_remove (h->rc_head, h->rc_tail, rc); - reconnect (h); - if (NULL != rc->cont) - rc->cont (rc->cont_cls, _("failed to transmit request (service down?)")); - GNUNET_free (rc); - return 0; + GNUNET_free (h); + return NULL; } - ret = rc->size; - if (size < ret) + h->cfg = cfg; + h->disconnecting = GNUNET_NO; + h->mq = + GNUNET_MQ_queue_for_connection_client (h->client, mq_handlers, + &handle_client_error, h); + if (NULL == h->mq) { - /* change in head of queue (i.e. cancel + add), try again */ - trigger_transmit (h); - return 0; + GNUNET_CLIENT_disconnect (h->client); + GNUNET_free (h); + return NULL; } - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Transmitting request of size %u to `%s' service.\n", ret, "PEERSTORE"); - memcpy (buf, &rc[1], ret); - GNUNET_CONTAINER_DLL_remove (h->rc_head, h->rc_tail, rc); - trigger_transmit (h); - if (NULL != rc->cont) - rc->cont (rc->cont_cls, NULL); - GNUNET_free (rc); - return ret; + LOG (GNUNET_ERROR_TYPE_DEBUG, "New connection created\n"); + return h; } + /** - * Check if we have a request pending in the transmission queue and are - * able to transmit it right now. If so, schedule transmission. + * Disconnect from the PEERSTORE service. Any pending ITERATE and WATCH requests + * will be canceled. + * Any pending STORE requests will depend on @e snyc_first flag. * - * @param h handle to the service + * @param h handle to disconnect + * @param sync_first send any pending STORE requests before disconnecting */ -static void -trigger_transmit (struct GNUNET_PEERSTORE_Handle *h) +void +GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first) { - struct GNUNET_PEERSTORE_RequestContext *rc; + struct GNUNET_PEERSTORE_IterateContext *ic; + struct GNUNET_PEERSTORE_IterateContext *ic_iter; + struct GNUNET_PEERSTORE_StoreContext *sc; + struct GNUNET_PEERSTORE_StoreContext *sc_iter; - if (NULL == (rc = h->rc_head)) - return; /* no requests queued */ - if (NULL != h->th) - return; /* request already pending */ - if (NULL == h->client) + LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting.\n"); + if (NULL != h->watches) { - /* disconnected, try to reconnect */ - reconnect (h); - return; + GNUNET_CONTAINER_multihashmap_iterate (h->watches, &destroy_watch, NULL); + GNUNET_CONTAINER_multihashmap_destroy (h->watches); + h->watches = NULL; + } + ic_iter = h->iterate_head; + while (NULL != ic_iter) + { + GNUNET_break (0); + ic = ic_iter; + ic_iter = ic_iter->next; + GNUNET_PEERSTORE_iterate_cancel (ic); + } + if (NULL != h->store_head) + { + if (GNUNET_YES == sync_first) + { + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Delaying disconnection due to pending store requests.\n"); + h->disconnecting = GNUNET_YES; + return; + } + sc_iter = h->store_head; + while (NULL != sc_iter) + { + sc = sc_iter; + sc_iter = sc_iter->next; + GNUNET_PEERSTORE_store_cancel (sc); + } } - h->th = - GNUNET_CLIENT_notify_transmit_ready (h->client, rc->size, - GNUNET_TIME_UNIT_FOREVER_REL, - GNUNET_YES, - &do_transmit, h); + do_disconnect (h); } + /******************************************************************************/ -/******************* ADD FUNCTIONS *********************/ +/******************* STORE FUNCTIONS *********************/ /******************************************************************************/ + /** * Cancel a store request * @@ -398,221 +586,334 @@ trigger_transmit (struct GNUNET_PEERSTORE_Handle *h) void GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc) { - struct GNUNET_PEERSTORE_Handle *h; + struct GNUNET_PEERSTORE_Handle *h = sc->h; - h = sc->h; - sc->cont = NULL; - if (GNUNET_YES == sc->request_transmitted) - return; /* need to finish processing */ - GNUNET_CONTAINER_DLL_remove (h->sc_head, - h->sc_tail, - sc); - if (NULL != sc->rc) - { - GNUNET_CONTAINER_DLL_remove (h->rc_head, h->rc_tail, sc->rc); - GNUNET_free (sc->rc); - } + GNUNET_CONTAINER_DLL_remove (sc->h->store_head, sc->h->store_tail, sc); + GNUNET_free (sc->sub_system); + GNUNET_free (sc->value); + GNUNET_free (sc->key); GNUNET_free (sc); + if ((GNUNET_YES == h->disconnecting) && (NULL == h->store_head)) + do_disconnect (h); } + /** - * Function called with server response message - * after a store operation is requested + * Store a new entry in the PEERSTORE. + * Note that stored entries can be lost in some cases + * such as power failure. * - * @param cls a 'struct GNUNET_PEERSTORE_Handle' + * @param h Handle to the PEERSTORE service + * @param sub_system name of the sub system + * @param peer Peer Identity + * @param key entry key + * @param value entry value BLOB + * @param size size of @e value + * @param expiry absolute time after which the entry is (possibly) deleted + * @param options options specific to the storage operation + * @param cont Continuation function after the store request is sent + * @param cont_cls Closure for @a cont + */ +struct GNUNET_PEERSTORE_StoreContext * +GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h, + const char *sub_system, + const struct GNUNET_PeerIdentity *peer, const char *key, + const void *value, size_t size, + struct GNUNET_TIME_Absolute expiry, + enum GNUNET_PEERSTORE_StoreOption options, + GNUNET_PEERSTORE_Continuation cont, void *cont_cls) +{ + struct GNUNET_MQ_Envelope *ev; + struct GNUNET_PEERSTORE_StoreContext *sc; + + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n", + size, sub_system, GNUNET_i2s (peer), key); + ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, value, size, + &expiry, options, + GNUNET_MESSAGE_TYPE_PEERSTORE_STORE); + sc = GNUNET_new (struct GNUNET_PEERSTORE_StoreContext); + + sc->sub_system = GNUNET_strdup (sub_system); + sc->peer = *peer; + sc->key = GNUNET_strdup (key); + sc->value = GNUNET_memdup (value, size); + sc->size = size; + sc->expiry = expiry; + sc->options = options; + sc->cont = cont; + sc->cont_cls = cont_cls; + sc->h = h; + + GNUNET_CONTAINER_DLL_insert_tail (h->store_head, h->store_tail, sc); + GNUNET_MQ_notify_sent (ev, &store_request_sent, sc); + GNUNET_MQ_send (h->mq, ev); + return sc; + +} + + +/******************************************************************************/ +/******************* ITERATE FUNCTIONS *********************/ +/******************************************************************************/ + +/** + * When a response for iterate request is received + * + * @param cls a `struct GNUNET_PEERSTORE_Handle *` * @param msg message received, NULL on timeout or fatal error */ static void -store_receive(void *cls, const struct GNUNET_MessageHeader *msg) +handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg) { struct GNUNET_PEERSTORE_Handle *h = cls; - struct GNUNET_PEERSTORE_StoreContext *sc = h->sc_head; - GNUNET_PEERSTORE_Continuation cont; - void *cont_cls; - uint16_t response_type; - uint16_t response_size; - struct StoreResponseMessage *srm; - int malformed = GNUNET_NO; - char *emsg; - - h->in_receive = GNUNET_NO; - if (NULL == sc) + struct GNUNET_PEERSTORE_IterateContext *ic; + GNUNET_PEERSTORE_Processor callback; + void *callback_cls; + uint16_t msg_type; + struct GNUNET_PEERSTORE_Record *record; + int continue_iter; + + ic = h->iterate_head; + if (NULL == ic) { - /* didn't expect a response, reconnect */ + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Unexpected iteration response, this should not happen.\n")); reconnect (h); return; } - cont = sc->cont; - cont_cls = sc->cont_cls; - sc->request_transmitted = GNUNET_NO; - //cancel the request since we only need one response - GNUNET_PEERSTORE_store_cancel(sc); - if(NULL == msg) + ic->iterating = GNUNET_YES; + callback = ic->callback; + callback_cls = ic->callback_cls; + if (NULL == msg) /* Connection error */ { - LOG(GNUNET_ERROR_TYPE_ERROR, "`PEERSTORE' service died\n"); + if (NULL != callback) + callback (callback_cls, NULL, + _("Error communicating with `PEERSTORE' service.")); reconnect (h); - if (NULL != cont) - cont (cont_cls, - _("Failed to receive response from `PEERSTORE' service.")); return; } - response_type = ntohs(msg->type); - response_size = ntohs(msg->size); - if(GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT != response_type) + msg_type = ntohs (msg->type); + if (GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END == msg_type) { - LOG(GNUNET_ERROR_TYPE_ERROR, "Received an unexpected response type: %lu to store request\n", response_type); - reconnect(h); - if (NULL != cont) - cont (cont_cls, - _("Received an unexpected response from `PEERSTORE' service.")); + ic->iterating = GNUNET_NO; + GNUNET_PEERSTORE_iterate_cancel (ic); + if (NULL != callback) + callback (callback_cls, NULL, NULL); return; } - if(response_size < sizeof(struct StoreResponseMessage)) + if (NULL != callback) { - malformed = GNUNET_YES; - } - else - { - srm = (struct StoreResponseMessage *)msg; - if(sizeof(struct StoreResponseMessage) + ntohs(srm->emsg_size) != response_size) - malformed = GNUNET_YES; - } - if(GNUNET_YES == malformed) - { - LOG(GNUNET_ERROR_TYPE_ERROR, "Received a malformed response from `PEERSTORE' service.\n"); - reconnect(h); - if (NULL != cont) - cont (cont_cls, - _("Received a malformed response from `PEERSTORE' service.")); - return; + record = PEERSTORE_parse_record_message (msg); + if (NULL == record) + continue_iter = + callback (callback_cls, NULL, + _("Received a malformed response from service.")); + else + { + continue_iter = callback (callback_cls, record, NULL); + PEERSTORE_destroy_record (record); + } + if (GNUNET_NO == continue_iter) + ic->callback = NULL; } - LOG(GNUNET_ERROR_TYPE_DEBUG, "Received a response of type %lu from server\n", response_type); - trigger_transmit(h); - if ( (GNUNET_NO == h->in_receive) && (NULL != h->sc_head) ) +} + + +/** + * Cancel an iterate request + * Please do not call after the iterate request is done + * + * @param ic Iterate request context as returned by GNUNET_PEERSTORE_iterate() + */ +void +GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic) +{ + if (NULL != ic->timeout_task) { - LOG(GNUNET_ERROR_TYPE_DEBUG, - "A store request was sent but response not received, receiving now.\n"); - h->in_receive = GNUNET_YES; - GNUNET_CLIENT_receive (h->client, - &store_receive, - h, - GNUNET_TIME_UNIT_FOREVER_REL); + GNUNET_SCHEDULER_cancel (ic->timeout_task); + ic->timeout_task = NULL; } - if(NULL != cont) + if (GNUNET_NO == ic->iterating) { - LOG(GNUNET_ERROR_TYPE_DEBUG, "Calling continuation of store request\n"); - srm = (struct StoreResponseMessage *)msg; - emsg = NULL; - if(GNUNET_NO == ntohs(srm->success)) - { - emsg = GNUNET_malloc(ntohs(srm->emsg_size)); - memcpy(emsg, &srm[1], ntohs(srm->emsg_size)); - } - cont(cont_cls, emsg); + GNUNET_CONTAINER_DLL_remove (ic->h->iterate_head, ic->h->iterate_tail, ic); + GNUNET_free (ic->sub_system); + if (NULL != ic->key) + GNUNET_free (ic->key); + GNUNET_free (ic); } + else + ic->callback = NULL; } + /** - * Called after store request is sent - * Waits for response from service + * Iterate over records matching supplied key information * - * @param cls a 'struct GNUNET_PEERSTORE_StoreContext' - * @parma emsg error message (or NULL) + * @param h handle to the PEERSTORE service + * @param sub_system name of sub system + * @param peer Peer identity (can be NULL) + * @param key entry key string (can be NULL) + * @param timeout time after which the iterate request is canceled + * @param callback function called with each matching record, all NULL's on end + * @param callback_cls closure for @a callback + * @return Handle to iteration request */ -void store_trigger_receive(void *cls, const char *emsg) +struct GNUNET_PEERSTORE_IterateContext * +GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h, + const char *sub_system, + const struct GNUNET_PeerIdentity *peer, + const char *key, struct GNUNET_TIME_Relative timeout, + GNUNET_PEERSTORE_Processor callback, + void *callback_cls) { - struct GNUNET_PEERSTORE_StoreContext *sc = cls; - struct GNUNET_PEERSTORE_Handle *h = sc->h; - GNUNET_PEERSTORE_Continuation cont; - void *cont_cls; + struct GNUNET_MQ_Envelope *ev; + struct GNUNET_PEERSTORE_IterateContext *ic; + + ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, NULL, 0, + NULL, 0, + GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE); + ic = GNUNET_new (struct GNUNET_PEERSTORE_IterateContext); + + ic->callback = callback; + ic->callback_cls = callback_cls; + ic->h = h; + ic->sub_system = GNUNET_strdup (sub_system); + if (NULL != peer) + ic->peer = *peer; + if (NULL != key) + ic->key = GNUNET_strdup (key); + ic->timeout = timeout; + GNUNET_CONTAINER_DLL_insert_tail (h->iterate_head, + h->iterate_tail, + ic); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Sending an iterate request for sub system `%s'\n", sub_system); + GNUNET_MQ_send (h->mq, ev); + ic->timeout_task = + GNUNET_SCHEDULER_add_delayed (timeout, &iterate_timeout, ic); + return ic; +} - sc->rc = NULL; - if(NULL != emsg) + +/******************************************************************************/ +/******************* WATCH FUNCTIONS *********************/ +/******************************************************************************/ + +/** + * When a watch record is received + * + * @param cls a `struct GNUNET_PEERSTORE_Handle *` + * @param msg message received, NULL on timeout or fatal error + */ +static void +handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg) +{ + struct GNUNET_PEERSTORE_Handle *h = cls; + struct GNUNET_PEERSTORE_Record *record; + struct GNUNET_HashCode keyhash; + struct GNUNET_PEERSTORE_WatchContext *wc; + + if (NULL == msg) { - cont = sc->cont; - cont_cls = sc->cont_cls; - GNUNET_PEERSTORE_store_cancel (sc); + LOG (GNUNET_ERROR_TYPE_ERROR, + _ + ("Problem receiving a watch response, no way to determine which request.\n")); reconnect (h); - if (NULL != sc->cont) - sc->cont (sc->cont_cls, emsg); return; } - LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for response from `%s' service.\n", - "PEERSTORE"); - sc->request_transmitted = GNUNET_YES; - if (GNUNET_NO == h->in_receive) + LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a watch record from service.\n"); + record = PEERSTORE_parse_record_message (msg); + PEERSTORE_hash_key (record->sub_system, record->peer, record->key, &keyhash); + // FIXME: what if there are multiple watches for the same key? + wc = GNUNET_CONTAINER_multihashmap_get (h->watches, &keyhash); + if (NULL == wc) { - h->in_receive = GNUNET_YES; - GNUNET_CLIENT_receive (h->client, - &store_receive, - h, - GNUNET_TIME_UNIT_FOREVER_REL); + LOG (GNUNET_ERROR_TYPE_ERROR, + _("Received a watch result for a non existing watch.\n")); + PEERSTORE_destroy_record (record); + reconnect (h); + return; } + if (NULL != wc->callback) + wc->callback (wc->callback_cls, record, NULL); + PEERSTORE_destroy_record (record); } + /** - * Store a new entry in the PEERSTORE + * Cancel a watch request * - * @param h Handle to the PEERSTORE service - * @param peer Peer Identity - * @param sub_system name of the sub system - * @param value entry value BLOB - * @param size size of 'value' - * @param lifetime relative time after which the entry is (possibly) deleted - * @param cont Continuation function after the store request is processed - * @param cont_cls Closure for 'cont' + * @param wc handle to the watch request */ -struct GNUNET_PEERSTORE_StoreContext * -GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h, - const struct GNUNET_PeerIdentity *peer, - const char *sub_system, - const void *value, - size_t size, - struct GNUNET_TIME_Relative lifetime, - GNUNET_PEERSTORE_Continuation cont, - void *cont_cls) +void +GNUNET_PEERSTORE_watch_cancel (struct GNUNET_PEERSTORE_WatchContext *wc) { - struct GNUNET_PEERSTORE_RequestContext *rc; - struct StoreRequestMessage *entry; - struct GNUNET_PEERSTORE_StoreContext *sc; - char *ss; - void *val; - size_t sub_system_size; - size_t request_size; + struct GNUNET_PEERSTORE_Handle *h = wc->h; + struct GNUNET_MQ_Envelope *ev; + struct StoreKeyHashMessage *hm; LOG (GNUNET_ERROR_TYPE_DEBUG, - "Storing value (size: %lu) for subsytem `%s' and peer `%s'\n", - size, sub_system, GNUNET_i2s (peer)); - sub_system_size = strlen(sub_system); - request_size = sizeof(struct StoreRequestMessage) + sub_system_size + size; - rc = GNUNET_malloc(sizeof(struct GNUNET_PEERSTORE_RequestContext) + request_size); - rc->h = h; - rc->size = request_size; - entry = (struct StoreRequestMessage *)&rc[1]; - entry->header.size = htons(request_size); - entry->header.type = htons(GNUNET_MESSAGE_TYPE_PEERSTORE_STORE); - entry->peer = *peer; - entry->sub_system_size = htons(sub_system_size); - entry->value_size = htons(size); - entry->lifetime = lifetime; - ss = (char *)&entry[1]; - memcpy(ss, sub_system, sub_system_size); - val = ss + sub_system_size; - memcpy(val, value, size); - sc = GNUNET_new(struct GNUNET_PEERSTORE_StoreContext); - sc->cont = cont; - sc->cont_cls = cont_cls; - sc->h = h; - sc->rc = rc; - sc->request_transmitted = GNUNET_NO; - rc->cont = &store_trigger_receive; - rc->cont_cls = sc; - GNUNET_CONTAINER_DLL_insert_tail(h->rc_head, h->rc_tail, rc); - GNUNET_CONTAINER_DLL_insert_tail(h->sc_head, h->sc_tail, sc); - trigger_transmit (h); - return sc; - + "Canceling watch.\n"); + ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL); + hm->keyhash = wc->keyhash; + GNUNET_MQ_send (h->mq, ev); + GNUNET_CONTAINER_multihashmap_remove (h->watches, + &wc->keyhash, + wc); + GNUNET_free (wc); } +/** + * Request watching a given key + * User will be notified with any new values added to key + * + * @param h handle to the PEERSTORE service + * @param sub_system name of sub system + * @param peer Peer identity + * @param key entry key string + * @param callback function called with each new value + * @param callback_cls closure for @a callback + * @return Handle to watch request + */ +struct GNUNET_PEERSTORE_WatchContext * +GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h, + const char *sub_system, + const struct GNUNET_PeerIdentity *peer, const char *key, + GNUNET_PEERSTORE_Processor callback, void *callback_cls) +{ + struct GNUNET_MQ_Envelope *ev; + struct StoreKeyHashMessage *hm; + struct GNUNET_PEERSTORE_WatchContext *wc; + + GNUNET_assert (NULL != sub_system); + GNUNET_assert (NULL != peer); + GNUNET_assert (NULL != key); + ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH); + PEERSTORE_hash_key (sub_system, + peer, + key, + &hm->keyhash); + wc = GNUNET_new (struct GNUNET_PEERSTORE_WatchContext); + wc->callback = callback; + wc->callback_cls = callback_cls; + wc->h = h; + wc->keyhash = hm->keyhash; + if (NULL == h->watches) + h->watches = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO); + GNUNET_assert (GNUNET_OK == + GNUNET_CONTAINER_multihashmap_put (h->watches, + &wc->keyhash, + wc, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Sending a watch request for ss `%s', peer `%s', key `%s'.\n", + sub_system, + GNUNET_i2s (peer), + key); + GNUNET_MQ_send (h->mq, ev); + return wc; +} + /* end of peerstore_api.c */