X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fpeerstore%2Fpeerstore_api.c;h=2138b23ff322033757e4aefa6c457157ef90c47c;hb=0f649608b4d8c3f82fd1f240bc0d28c2713c7699;hp=bebf9678ee3fc4a11a6eebdf4ce8f53a6a2ea6fa;hpb=4371b0fcd9b50f78ca27ac48adf2f4663b2dfc9b;p=oweals%2Fgnunet.git diff --git a/src/peerstore/peerstore_api.c b/src/peerstore/peerstore_api.c index bebf9678e..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,11 +17,11 @@ 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" @@ -107,21 +107,51 @@ struct GNUNET_PEERSTORE_StoreContext */ struct GNUNET_PEERSTORE_Handle *h; - /** - * MQ Envelope with store request message - */ - struct GNUNET_MQ_Envelope *ev; - /** * Continuation called with service response */ GNUNET_PEERSTORE_Continuation cont; /** - * Closure for 'cont' + * Closure for @e cont */ void *cont_cls; + /** + * 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; + }; /** @@ -145,9 +175,24 @@ struct GNUNET_PEERSTORE_IterateContext struct GNUNET_PEERSTORE_Handle *h; /** - * MQ Envelope with iterate request message + * Which subsystem does the store? */ - struct GNUNET_MQ_Envelope *ev; + char *sub_system; + + /** + * Peer the store is for. + */ + struct GNUNET_PeerIdentity peer; + + /** + * Key for the store operation. + */ + char *key; + + /** + * Operation timeout + */ + struct GNUNET_TIME_Relative timeout; /** * Callback with each matching record @@ -155,21 +200,20 @@ struct GNUNET_PEERSTORE_IterateContext GNUNET_PEERSTORE_Processor callback; /** - * Closure for 'callback' + * Closure for @e callback */ void *callback_cls; /** - * #GNUNET_YES / #GNUNET_NO - * if sent, cannot be canceled + * #GNUNET_YES if we are currently processing records. */ - int request_sent; + int iterating; /** * Task identifier for the function called * on iterate request timeout */ - GNUNET_SCHEDULER_TaskIdentifier timeout_task; + struct GNUNET_SCHEDULER_Task * timeout_task; }; @@ -193,18 +237,13 @@ struct GNUNET_PEERSTORE_WatchContext */ struct GNUNET_PEERSTORE_Handle *h; - /** - * MQ Envelope with watch request message - */ - struct GNUNET_MQ_Envelope *ev; - /** * Callback with each record received */ GNUNET_PEERSTORE_Processor callback; /** - * Closure for 'callback' + * Closure for @e callback */ void *callback_cls; @@ -213,12 +252,6 @@ struct GNUNET_PEERSTORE_WatchContext */ struct GNUNET_HashCode keyhash; - /** - * #GNUNET_YES / #GNUNET_NO - * if sent, cannot be canceled - */ - int request_sent; - }; /******************************************************************************/ @@ -251,29 +284,26 @@ handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg); static void reconnect (struct GNUNET_PEERSTORE_Handle *h); -/** - * Callback after MQ envelope is sent - * - * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *' - */ -static void -watch_request_sent (void *cls); /** * Callback after MQ envelope is sent * - * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *' + * @param cls a `struct GNUNET_PEERSTORE_StoreContext *` */ static void -iterate_request_sent (void *cls); +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); +} -/** - * Callback after MQ envelope is sent - * - * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *' - */ -static void -store_request_sent (void *cls); /** * MQ message handlers @@ -303,6 +333,11 @@ handle_client_error (void *cls, enum GNUNET_MQ_Error error) /** * Iterator over previous watches to resend them + * + * @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) */ static int rewatch_it (void *cls, const struct GNUNET_HashCode *key, void *value) @@ -310,19 +345,37 @@ rewatch_it (void *cls, const struct GNUNET_HashCode *key, void *value) struct GNUNET_PEERSTORE_Handle *h = cls; struct GNUNET_PEERSTORE_WatchContext *wc = value; struct StoreKeyHashMessage *hm; + struct GNUNET_MQ_Envelope *ev; - if (GNUNET_YES == wc->request_sent) - { /* Envelope gone, create new one. */ - wc->ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH); - hm->keyhash = wc->keyhash; - wc->request_sent = GNUNET_NO; - } - GNUNET_MQ_notify_sent (wc->ev, &watch_request_sent, wc); - GNUNET_MQ_send (h->mq, wc->ev); + ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH); + hm->keyhash = wc->keyhash; + GNUNET_MQ_send (h->mq, ev); return GNUNET_YES; } +/** + * 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")); +} + + /** * Close the existing connection to PEERSTORE and reconnect. * @@ -332,11 +385,22 @@ 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_MQ_Envelope *ev; LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n"); + for (ic = h->iterate_head; NULL != ic; ic = next) + { + 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.")); + } if (NULL != h->mq) { GNUNET_MQ_destroy (h->mq); @@ -347,6 +411,7 @@ reconnect (struct GNUNET_PEERSTORE_Handle *h) GNUNET_CLIENT_disconnect (h->client); h->client = NULL; } + h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg); GNUNET_assert (NULL != h->client); h->mq = @@ -355,33 +420,26 @@ reconnect (struct GNUNET_PEERSTORE_Handle *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); - } - ic = h->iterate_head; - while (NULL != ic) + for (ic = h->iterate_head; NULL != ic; ic = ic->next) { - if (GNUNET_YES == ic->request_sent) - { - 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.")); - } - else - { - GNUNET_MQ_notify_sent (ic->ev, &iterate_request_sent, ic); - GNUNET_MQ_send (h->mq, ic->ev); - } - ic = ic->next; + 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); } - sc = h->store_head; - while (NULL != sc) + for (sc = h->store_head; NULL != sc; sc = sc->next) { - GNUNET_MQ_notify_sent (sc->ev, &store_request_sent, sc); - GNUNET_MQ_send (h->mq, sc->ev); - 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); } } @@ -431,6 +489,7 @@ do_disconnect (struct GNUNET_PEERSTORE_Handle *h) /** * Connect to the PEERSTORE service. * + * @param cfg configuration to use * @return NULL on error */ struct GNUNET_PEERSTORE_Handle * @@ -453,6 +512,7 @@ GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) &handle_client_error, h); if (NULL == h->mq) { + GNUNET_CLIENT_disconnect (h->client); GNUNET_free (h); return NULL; } @@ -487,6 +547,7 @@ GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first) 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); @@ -516,26 +577,6 @@ GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first) /******************* STORE FUNCTIONS *********************/ /******************************************************************************/ -/** - * Callback after MQ envelope is sent - * - * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *' - */ -static void -store_request_sent (void *cls) -{ - struct GNUNET_PEERSTORE_StoreContext *sc = cls; - GNUNET_PEERSTORE_Continuation cont; - void *cont_cls; - - sc->ev = NULL; - cont = sc->cont; - cont_cls = sc->cont_cls; - GNUNET_PEERSTORE_store_cancel (sc); - if (NULL != cont) - cont (cont_cls, GNUNET_OK); -} - /** * Cancel a store request @@ -547,14 +588,12 @@ GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc) { struct GNUNET_PEERSTORE_Handle *h = sc->h; - if (NULL != sc->ev) - { - GNUNET_MQ_send_cancel (sc->ev); - sc->ev = NULL; - } 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) + if ((GNUNET_YES == h->disconnecting) && (NULL == h->store_head)) do_disconnect (h); } @@ -573,7 +612,7 @@ GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc) * @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 'cont' + * @param cont_cls Closure for @a cont */ struct GNUNET_PEERSTORE_StoreContext * GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h, @@ -595,10 +634,17 @@ GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE); sc = GNUNET_new (struct GNUNET_PEERSTORE_StoreContext); - sc->ev = ev; + 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); @@ -614,7 +660,7 @@ GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h, /** * When a response for iterate request is received * - * @param cls a 'struct GNUNET_PEERSTORE_Handle *' + * @param cls a `struct GNUNET_PEERSTORE_Handle *` * @param msg message received, NULL on timeout or fatal error */ static void @@ -636,11 +682,11 @@ handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg) reconnect (h); return; } + ic->iterating = GNUNET_YES; callback = ic->callback; callback_cls = ic->callback_cls; if (NULL == msg) /* Connection error */ { - if (NULL != callback) callback (callback_cls, NULL, _("Error communicating with `PEERSTORE' service.")); @@ -650,7 +696,7 @@ handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg) msg_type = ntohs (msg->type); if (GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END == msg_type) { - ic->request_sent = GNUNET_NO; + ic->iterating = GNUNET_NO; GNUNET_PEERSTORE_iterate_cancel (ic); if (NULL != callback) callback (callback_cls, NULL, NULL); @@ -674,38 +720,6 @@ handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg) } -/** - * Callback after MQ envelope is sent - * - * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *' - */ -static void -iterate_request_sent (void *cls) -{ - struct GNUNET_PEERSTORE_IterateContext *ic = cls; - - LOG (GNUNET_ERROR_TYPE_DEBUG, "Iterate request sent to service.\n"); - ic->request_sent = GNUNET_YES; - ic->ev = NULL; -} - - -/** - * 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; - - ic->timeout_task = GNUNET_SCHEDULER_NO_TASK; - GNUNET_PEERSTORE_iterate_cancel (ic); -} - - /** * Cancel an iterate request * Please do not call after the iterate request is done @@ -715,19 +729,17 @@ iterate_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) void GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic) { - if (GNUNET_SCHEDULER_NO_TASK != ic->timeout_task) + if (NULL != ic->timeout_task) { GNUNET_SCHEDULER_cancel (ic->timeout_task); - ic->timeout_task = GNUNET_SCHEDULER_NO_TASK; + ic->timeout_task = NULL; } - if (GNUNET_NO == ic->request_sent) + if (GNUNET_NO == ic->iterating) { - if (NULL != ic->ev) - { - GNUNET_MQ_send_cancel (ic->ev); - ic->ev = NULL; - } 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 @@ -765,13 +777,18 @@ GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h, ic->callback = callback; ic->callback_cls = callback_cls; - ic->ev = ev; ic->h = h; - ic->request_sent = GNUNET_NO; - GNUNET_CONTAINER_DLL_insert_tail (h->iterate_head, h->iterate_tail, ic); + 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_notify_sent (ev, &iterate_request_sent, ic); GNUNET_MQ_send (h->mq, ev); ic->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout, &iterate_timeout, ic); @@ -786,7 +803,7 @@ GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h, /** * When a watch record is received * - * @param cls a 'struct GNUNET_PEERSTORE_Handle *' + * @param cls a `struct GNUNET_PEERSTORE_Handle *` * @param msg message received, NULL on timeout or fatal error */ static void @@ -808,11 +825,13 @@ handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg) 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) { LOG (GNUNET_ERROR_TYPE_ERROR, _("Received a watch result for a non existing watch.\n")); + PEERSTORE_destroy_record (record); reconnect (h); return; } @@ -822,21 +841,6 @@ handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg) } -/** - * Callback after MQ envelope is sent - * - * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *' - */ -static void -watch_request_sent (void *cls) -{ - struct GNUNET_PEERSTORE_WatchContext *wc = cls; - - wc->request_sent = GNUNET_YES; - wc->ev = NULL; -} - - /** * Cancel a watch request * @@ -849,23 +853,15 @@ GNUNET_PEERSTORE_watch_cancel (struct GNUNET_PEERSTORE_WatchContext *wc) struct GNUNET_MQ_Envelope *ev; struct StoreKeyHashMessage *hm; - LOG (GNUNET_ERROR_TYPE_DEBUG, "Canceling watch.\n"); - if (GNUNET_YES == wc->request_sent) /* If request already sent to service, send a cancel request. */ - { - ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL); - hm->keyhash = wc->keyhash; - GNUNET_MQ_send (h->mq, ev); - wc->callback = NULL; - wc->callback_cls = NULL; - } - if (NULL != wc->ev) - { - GNUNET_MQ_send_cancel (wc->ev); - wc->ev = NULL; - } - GNUNET_CONTAINER_multihashmap_remove (h->watches, &wc->keyhash, wc); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "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); - } @@ -895,23 +891,27 @@ GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h, 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); + 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->ev = ev; wc->h = h; - wc->request_sent = GNUNET_NO; wc->keyhash = hm->keyhash; if (NULL == h->watches) h->watches = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO); - GNUNET_CONTAINER_multihashmap_put (h->watches, &wc->keyhash, wc, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE); + 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_notify_sent (ev, &watch_request_sent, wc); + sub_system, + GNUNET_i2s (peer), + key); GNUNET_MQ_send (h->mq, ev); return wc; }