From fe92967f50a918352370fb95c6bc0f6d2a5eb6c2 Mon Sep 17 00:00:00 2001 From: Sree Harsha Totakura Date: Fri, 27 Apr 2012 22:41:10 +0000 Subject: [PATCH] -hashmap for storing lockingrequests in lockmanager --- src/lockmanager/lockmanager_api.c | 138 +++++++++++++++++++------ src/lockmanager/test_lockmanager_api.c | 101 +++++++++++++++--- 2 files changed, 194 insertions(+), 45 deletions(-) diff --git a/src/lockmanager/lockmanager_api.c b/src/lockmanager/lockmanager_api.c index bfd06275c..7e20e27de 100644 --- a/src/lockmanager/lockmanager_api.c +++ b/src/lockmanager/lockmanager_api.c @@ -26,9 +26,11 @@ #include "platform.h" #include "gnunet_common.h" -#include "gnunet_protocols.h" +#include "gnunet_container_lib.h" #include "gnunet_client_lib.h" +#include "gnunet_crypto_lib.h" #include "gnunet_lockmanager_service.h" +#include "gnunet_protocols.h" #include "lockmanager.h" @@ -49,6 +51,11 @@ struct GNUNET_LOCKMANAGER_Handle * The client connection to the service */ struct GNUNET_CLIENT_Connection *conn; + + /** + * Hashmap handle + */ + struct GNUNET_CONTAINER_MultiHashMap *hashmap; }; @@ -100,16 +107,18 @@ struct GNUNET_LOCKMANAGER_LockingRequest /** - * Message handler for SUCCESS messages + * Handler for server replies * * @param cls the LOCKMANAGER_Handle - * @param msg message received, NULL on timeout or fatal error + * @param msg received message, NULL on timeout or fatal error */ static void -handle_success (void *cls, +handle_replies (void *cls, const struct GNUNET_MessageHeader *msg) { if (NULL == msg) + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Lockmanager service not available or went down\n"); return; LOG (GNUNET_ERROR_TYPE_DEBUG, @@ -117,24 +126,6 @@ handle_success (void *cls, } -/** - * We wait for DUMMY message which will never be sent by the server. However, - * in case the server shuts-down/crashes/restarts we are notified by this call - * back with a NULL for msg. - * - * @param cls closure - * @param msg message received, NULL on timeout or fatal error - */ -static void -handle_server_crash (void *cls, - const struct GNUNET_MessageHeader *msg) -{ - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Lockmanager service not available or went down\n"); - -} - - /** * Transmit notify for sending message to server * @@ -158,10 +149,65 @@ transmit_notify (void *cls, size_t size, void *buf) GNUNET_assert (size >= msg_size); memcpy (buf, msg, msg_size); GNUNET_free (msg); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Message of size %u sent\n", msg_size); return msg_size; } +/** + * Iterator to free hash map entries. + * + * @param cls NULL + * @param key current key code + * @param value value in the hash map + * @return GNUNET_YES if we should continue to + * iterate, + * GNUNET_NO if not. + */ +static int +free_iterator(void *cls, + const GNUNET_HashCode * key, + void *value) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Clearing locking request\n"); + GNUNET_free (value); + return GNUNET_YES; +} + + +/** + * Generate hash with domain name and the lock + * + * @param domain NULL terminated domain name + * + * @param domain_length the length of the domain name including the terminating + * NULL if already known; 0 to calculate + * + * @param lock the lock number + * @param where to write the generated hash + */ +static void +hash_domain_and_lock (const char *domain, + uint16_t domain_length, + uint32_t lock, + GNUNET_HashCode *ret) +{ + unsigned int str_len; + uint32_t *block; + size_t block_size; + + str_len = (0 == domain_length) ? strlen (domain) : domain_length - 1; + block_size = sizeof (uint32_t) + str_len; + block = GNUNET_malloc (block_size); + /* Copy data */ + *block = lock; + memcpy (&block[1], domain, str_len); + + GNUNET_CRYPTO_hash (block, block_size, ret); + GNUNET_free (block); +} /*******************/ /* API Definitions */ @@ -189,17 +235,14 @@ GNUNET_LOCKMANAGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) return NULL; } + h->hashmap = GNUNET_CONTAINER_multihashmap_create (15); + GNUNET_assert (NULL != h->hashmap); + GNUNET_CLIENT_receive (h->conn, - &handle_server_crash, + &handle_replies, NULL, GNUNET_TIME_UNIT_FOREVER_REL); - /* FIXME: Assertions fail in client.c if trying to receive multiple messages */ - /* GNUNET_CLIENT_receive (h->conn, */ - /* &handle_success, */ - /* h, */ - /* GNUNET_TIME_UNIT_FOREVER_REL); */ - LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__); return h; } @@ -215,6 +258,18 @@ GNUNET_LOCKMANAGER_disconnect (struct GNUNET_LOCKMANAGER_Handle *handle) { LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__); GNUNET_CLIENT_disconnect (handle->conn); + + if (0 != GNUNET_CONTAINER_multihashmap_size (handle->hashmap)) + { + LOG (GNUNET_ERROR_TYPE_WARNING, + "Some locking requests are still present. Cancel them before " + "calling %s\n", __func__); + GNUNET_CONTAINER_multihashmap_iterate (handle->hashmap, + &free_iterator, + NULL); + } + GNUNET_CONTAINER_multihashmap_destroy (handle->hashmap); + GNUNET_free (handle); LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__); } @@ -254,6 +309,7 @@ GNUNET_LOCKMANAGER_acquire_lock (struct GNUNET_LOCKMANAGER_Handle *handle, { struct GNUNET_LOCKMANAGER_LockingRequest *r; struct GNUNET_LOCKMANAGER_Message *msg; + struct GNUNET_HashCode hash; uint16_t msg_size; LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__); @@ -271,6 +327,7 @@ GNUNET_LOCKMANAGER_acquire_lock (struct GNUNET_LOCKMANAGER_Handle *handle, msg->lock = htonl (lock); memcpy (&msg[1], r->domain, r->domain_name_length); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Queueing ACQUIRE message\n"); r->transmit_handle = GNUNET_CLIENT_notify_transmit_ready (r->handle->conn, msg_size, @@ -278,6 +335,15 @@ GNUNET_LOCKMANAGER_acquire_lock (struct GNUNET_LOCKMANAGER_Handle *handle, GNUNET_NO, *transmit_notify, msg); + hash_domain_and_lock (r->domain, + r->domain_name_length, + r->lock, + &hash); + GNUNET_CONTAINER_multihashmap_put (r->handle->hashmap, + &hash, + r, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); + LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__); return r; } @@ -296,6 +362,8 @@ void GNUNET_LOCKMANAGER_cancel_request (struct GNUNET_LOCKMANAGER_LockingRequest *request) { + struct GNUNET_HashCode hash; + LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__); /* FIXME: Stop ACQUIRE retransmissions */ if (GNUNET_LOCKMANAGER_SUCCESS == request->status) @@ -318,6 +386,16 @@ GNUNET_LOCKMANAGER_cancel_request (struct GNUNET_LOCKMANAGER_LockingRequest &transmit_notify, msg); } - LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__); + LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__); + + hash_domain_and_lock (request->domain, + request->domain_name_length, + request->lock, + &hash); + + GNUNET_assert (GNUNET_YES == + GNUNET_CONTAINER_multihashmap_remove + (request->handle->hashmap, &hash, request)); + GNUNET_free (request); } diff --git a/src/lockmanager/test_lockmanager_api.c b/src/lockmanager/test_lockmanager_api.c index 144f7b43c..9137768e8 100644 --- a/src/lockmanager/test_lockmanager_api.c +++ b/src/lockmanager/test_lockmanager_api.c @@ -51,22 +51,32 @@ static struct GNUNET_OS_Process *arm_pid = NULL; /** * Configuration Handle */ -struct GNUNET_CONFIGURATION_Handle *config; +static struct GNUNET_CONFIGURATION_Handle *config; /** - * Testing function + * The handle to the lockmanager service + */ +static struct GNUNET_LOCKMANAGER_Handle *handle; + +/** + * The locking request + */ +static struct GNUNET_LOCKMANAGER_LockingRequest *request; + +/** + * Shutdown nicely * - * @param cls NULL + * @param cls * @param tc the task context */ static void -test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - struct GNUNET_LOCKMANAGER_Handle *handle; - - handle = GNUNET_LOCKMANAGER_connect (config); - GNUNET_assert (NULL != handle); - +do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + if (NULL != request) + { + GNUNET_LOCKMANAGER_cancel_request (request); + request = NULL; + } GNUNET_LOCKMANAGER_disconnect (handle); if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM)) { @@ -75,7 +85,68 @@ test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) } GNUNET_OS_process_wait (arm_pid); GNUNET_OS_process_close (arm_pid); - result = GNUNET_OK; + if (GNUNET_SYSERR != result) + result = GNUNET_OK; +} + + +/** + * Shutdown nicely + * + * @param cls + * @param tc the task context + */ +static void +do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + result = GNUNET_SYSERR; + do_shutdown (cls, tc); +} + +/** + * Callback for lock status changes + * + * @param cls the closure from GNUNET_LOCKMANAGER_lock call + * + * @param domain_name the locking domain of the lock + * + * @param lock the lock for which this status is relevant + * + * @param status GNUNET_LOCKMANAGER_SUCCESS if the lock has been successfully + * acquired; GNUNET_LOCKMANAGER_RELEASE when the acquired lock is lost + */ +static void +status_cb (void *cls, + const char *domain_name, + uint32_t lock, + enum GNUNET_LOCKMANAGER_Status status) +{ + GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (1), + &do_shutdown, + NULL); +} + + +/** + * Testing function + * + * @param cls NULL + * @param tc the task context + */ +static void +test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + handle = GNUNET_LOCKMANAGER_connect (config); + GNUNET_assert (NULL != handle); + + request = GNUNET_LOCKMANAGER_acquire_lock (handle, + "GNUNET_LOCKMANAGER_TESTING", + 99, + &status_cb, + NULL); + GNUNET_SCHEDULER_add_delayed (TIME_REL_SECONDS (10), + &do_abort, + NULL); } @@ -127,15 +198,15 @@ int main (int argc, char **argv) if (GNUNET_OK != ret) { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n", - ret); + LOG (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n", + ret); return 1; } if (GNUNET_SYSERR == result) { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n"); + LOG (GNUNET_ERROR_TYPE_WARNING, "test failed\n"); return 1; } - GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test ok\n"); + LOG (GNUNET_ERROR_TYPE_INFO, "test ok\n"); return 0; } -- 2.25.1