From 93cf4031938ca0f3d13ce491ba9be2c77763ffae Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 25 Jun 2016 20:33:57 +0000 Subject: [PATCH] remove global variable for barriers, move into controller --- src/testbed/testbed_api.c | 113 +++++++++++++++++ src/testbed/testbed_api.h | 76 ++++++----- src/testbed/testbed_api_barriers.c | 181 +++------------------------ src/testbed/testbed_api_barriers.h | 20 ++- src/testbed/testbed_api_hosts.c | 4 +- src/testbed/testbed_api_operations.c | 2 +- 6 files changed, 194 insertions(+), 202 deletions(-) diff --git a/src/testbed/testbed_api.c b/src/testbed/testbed_api.c index 90b0e06c8..d946b7082 100644 --- a/src/testbed/testbed_api.c +++ b/src/testbed/testbed_api.c @@ -36,6 +36,7 @@ #include "testbed.h" #include "testbed_api.h" +#include "testbed_api_barriers.h" #include "testbed_api_hosts.h" #include "testbed_api_peers.h" #include "testbed_api_operations.h" @@ -1046,6 +1047,118 @@ handle_link_controllers_result (void *cls, } +/** + * Validate #GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS message. + * + * @param cls the controller handle to determine the connection this message + * belongs to + * @param msg the barrier status message + * @return #GNUNET_OK if the message is valid; #GNUNET_SYSERR to tear it + * down signalling an error (message malformed) + */ +static int +check_barrier_status_ (struct GNUNET_TESTBED_Controller *c, + const struct GNUNET_TESTBED_BarrierStatusMsg *msg) +{ + uint16_t msize; + uint16_t name_len; + int status; + const char *name; + size_t emsg_len; + + msize = ntohs (msg->header.size); + name = msg->data; + name_len = ntohs (msg->name_len); + + if (sizeof (struct GNUNET_TESTBED_BarrierStatusMsg) + name_len + 1 > msize) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + if ('\0' != name[name_len]) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + status = ntohs (msg->status); + if (GNUNET_TESTBED_BARRIERSTATUS_ERROR == status) + { + emsg_len = msize - (sizeof (struct GNUNET_TESTBED_BarrierStatusMsg) + name_len + + 1); /* +1!? */ + if (0 == emsg_len) + { + GNUNET_break_op (0); + return GNUNET_SYSERR; + } + } + return GNUNET_OK; +} + + +/** + * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS messages + * + * @param c the controller handle to determine the connection this message + * belongs to + * @param msg the barrier status message + */ +static void +handle_barrier_status_ (struct GNUNET_TESTBED_Controller *c, + const struct GNUNET_TESTBED_BarrierStatusMsg *msg) +{ + struct GNUNET_TESTBED_Barrier *barrier; + char *emsg; + const char *name; + struct GNUNET_HashCode key; + size_t emsg_len; + int status; + uint16_t msize; + uint16_t name_len; + + emsg = NULL; + barrier = NULL; + msize = ntohs (msg->header.size); + name = msg->data; + name_len = ntohs (msg->name_len); + LOG_DEBUG ("Received BARRIER_STATUS msg\n"); + status = ntohs (msg->status); + if (GNUNET_TESTBED_BARRIERSTATUS_ERROR == status) + { + status = -1; + emsg_len = msize - (sizeof (struct GNUNET_TESTBED_BarrierStatusMsg) + name_len + + 1); + emsg = GNUNET_malloc (emsg_len + 1); + memcpy (emsg, + msg->data + name_len + 1, + emsg_len); + } + if (NULL == c->barrier_map) + { + GNUNET_break_op (0); + goto cleanup; + } + GNUNET_CRYPTO_hash (name, name_len, &key); + barrier = GNUNET_CONTAINER_multihashmap_get (c->barrier_map, &key); + if (NULL == barrier) + { + GNUNET_break_op (0); + goto cleanup; + } + GNUNET_assert (NULL != barrier->cb); + if ((GNUNET_YES == barrier->echo) && + (GNUNET_TESTBED_BARRIERSTATUS_CROSSED == status)) + GNUNET_TESTBED_queue_message_ (c, GNUNET_copy_message (&msg->header)); + barrier->cb (barrier->cls, name, barrier, status, emsg); + if (GNUNET_TESTBED_BARRIERSTATUS_INITIALISED == status) + return; /* just initialised; skip cleanup */ + + cleanup: + GNUNET_free_non_null (emsg); + if (NULL != barrier) + GNUNET_TESTBED_barrier_remove_ (barrier); +} + + /** * Handler for messages from controller (testbed service) * diff --git a/src/testbed/testbed_api.h b/src/testbed/testbed_api.h index cf39e3415..7a8e6539a 100644 --- a/src/testbed/testbed_api.h +++ b/src/testbed/testbed_api.h @@ -183,7 +183,8 @@ struct OperationContext * * @param cls closure */ -typedef void (*TESTBED_opcq_empty_cb) (void *cls); +typedef void +(*TESTBED_opcq_empty_cb) (void *cls); /** @@ -273,6 +274,12 @@ struct GNUNET_TESTBED_Controller */ struct OperationQueue *opq_parallel_topology_config_operations; + /** + * handle for hashtable of barrier handles, values are + * of type `struct GNUNET_TESTBED_Barrier`. + */ + struct GNUNET_CONTAINER_MultiHashMap *barrier_map; + /** * The controller event mask */ @@ -291,6 +298,44 @@ struct GNUNET_TESTBED_Controller }; +/** + * Handle for barrier + */ +struct GNUNET_TESTBED_Barrier +{ + /** + * hashcode identifying this barrier in the hashmap + */ + struct GNUNET_HashCode key; + + /** + * The controller handle given while initiliasing this barrier + */ + struct GNUNET_TESTBED_Controller *c; + + /** + * The name of the barrier + */ + char *name; + + /** + * The continuation callback to call when we have a status update on this + */ + GNUNET_TESTBED_barrier_status_cb cb; + + /** + * the closure for the above callback + */ + void *cls; + + /** + * Should the barrier crossed status message be echoed back to the controller? + */ + int echo; +}; + + + /** * Queues a message in send queue for sending to the service * @@ -460,34 +505,5 @@ GNUNET_TESTBED_get_slave_config_ (void *op_cls, uint32_t slave_host_id); -/** - * Validate #GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS message. - * - * @param cls the controller handle to determine the connection this message - * belongs to - * @param msg the barrier status message - * @return #GNUNET_OK if the message is valid; #GNUNET_SYSERR to tear it - * down signalling an error (message malformed) - */ -int -check_barrier_status_ (struct GNUNET_TESTBED_Controller *c, - const struct GNUNET_TESTBED_BarrierStatusMsg *msg); - - -/** - * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS messages. This - * function is defined in @file testbed_api_barriers.c - * - * @param c the controller handle to determine the connection this message - * belongs to - * @param msg the barrier status message - */ -void -handle_barrier_status_ (struct GNUNET_TESTBED_Controller *c, - const struct GNUNET_TESTBED_BarrierStatusMsg *msg); - - - - #endif /* end of testbed_api.h */ diff --git a/src/testbed/testbed_api_barriers.c b/src/testbed/testbed_api_barriers.c index 824dbcdba..0163c0ce4 100644 --- a/src/testbed/testbed_api_barriers.c +++ b/src/testbed/testbed_api_barriers.c @@ -41,48 +41,6 @@ #define LOG_DEBUG(...) \ LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__); -/** - * Handle for barrier - */ -struct GNUNET_TESTBED_Barrier -{ - /** - * hashcode identifying this barrier in the hashmap - */ - struct GNUNET_HashCode key; - - /** - * The controller handle given while initiliasing this barrier - */ - struct GNUNET_TESTBED_Controller *c; - - /** - * The name of the barrier - */ - char *name; - - /** - * The continuation callback to call when we have a status update on this - */ - GNUNET_TESTBED_barrier_status_cb cb; - - /** - * the closure for the above callback - */ - void *cls; - - /** - * Should the barrier crossed status message be echoed back to the controller? - */ - int echo; -}; - - -/** - * handle for hashtable of barrier handles - */ -static struct GNUNET_CONTAINER_MultiHashMap *barrier_map; - /** * Remove a barrier and it was the last one in the barrier hash map, destroy the @@ -90,133 +48,23 @@ static struct GNUNET_CONTAINER_MultiHashMap *barrier_map; * * @param barrier the barrier to remove */ -static void -barrier_remove (struct GNUNET_TESTBED_Barrier *barrier) +void +GNUNET_TESTBED_barrier_remove_ (struct GNUNET_TESTBED_Barrier *barrier) { - GNUNET_assert (NULL != barrier_map); /* No barriers present */ + struct GNUNET_TESTBED_Controller *c = barrier->c; + + GNUNET_assert (NULL != c->barrier_map); /* No barriers present */ GNUNET_assert (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_remove (barrier_map, + GNUNET_CONTAINER_multihashmap_remove (c->barrier_map, &barrier->key, barrier)); GNUNET_free (barrier->name); GNUNET_free (barrier); - if (0 == GNUNET_CONTAINER_multihashmap_size (barrier_map)) + if (0 == GNUNET_CONTAINER_multihashmap_size (c->barrier_map)) { - GNUNET_CONTAINER_multihashmap_destroy (barrier_map); - barrier_map = NULL; - } -} - - -/** - * Validate #GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS message. - * - * @param cls the controller handle to determine the connection this message - * belongs to - * @param msg the barrier status message - * @return #GNUNET_OK if the message is valid; #GNUNET_SYSERR to tear it - * down signalling an error (message malformed) - */ -int -check_barrier_status_ (struct GNUNET_TESTBED_Controller *c, - const struct GNUNET_TESTBED_BarrierStatusMsg *msg) -{ - uint16_t msize; - uint16_t name_len; - int status; - const char *name; - size_t emsg_len; - - msize = ntohs (msg->header.size); - name = msg->data; - name_len = ntohs (msg->name_len); - - if (sizeof (struct GNUNET_TESTBED_BarrierStatusMsg) + name_len + 1 > msize) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - if ('\0' != name[name_len]) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - status = ntohs (msg->status); - if (GNUNET_TESTBED_BARRIERSTATUS_ERROR == status) - { - emsg_len = msize - (sizeof (struct GNUNET_TESTBED_BarrierStatusMsg) + name_len - + 1); /* +1!? */ - if (0 == emsg_len) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - } - return GNUNET_OK; -} - - -/** - * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS messages - * - * @param c the controller handle to determine the connection this message - * belongs to - * @param msg the barrier status message - */ -void -handle_barrier_status_ (struct GNUNET_TESTBED_Controller *c, - const struct GNUNET_TESTBED_BarrierStatusMsg *msg) -{ - struct GNUNET_TESTBED_Barrier *barrier; - char *emsg; - const char *name; - struct GNUNET_HashCode key; - size_t emsg_len; - int status; - uint16_t msize; - uint16_t name_len; - - emsg = NULL; - barrier = NULL; - msize = ntohs (msg->header.size); - name = msg->data; - name_len = ntohs (msg->name_len); - LOG_DEBUG ("Received BARRIER_STATUS msg\n"); - status = ntohs (msg->status); - if (GNUNET_TESTBED_BARRIERSTATUS_ERROR == status) - { - status = -1; - emsg_len = msize - (sizeof (struct GNUNET_TESTBED_BarrierStatusMsg) + name_len - + 1); - emsg = GNUNET_malloc (emsg_len + 1); - memcpy (emsg, - msg->data + name_len + 1, - emsg_len); - } - if (NULL == barrier_map) - { - GNUNET_break_op (0); - goto cleanup; - } - GNUNET_CRYPTO_hash (name, name_len, &key); - barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map, &key); - if (NULL == barrier) - { - GNUNET_break_op (0); - goto cleanup; + GNUNET_CONTAINER_multihashmap_destroy (c->barrier_map); + c->barrier_map = NULL; } - GNUNET_assert (NULL != barrier->cb); - if ((GNUNET_YES == barrier->echo) && - (GNUNET_TESTBED_BARRIERSTATUS_CROSSED == status)) - GNUNET_TESTBED_queue_message_ (c, GNUNET_copy_message (&msg->header)); - barrier->cb (barrier->cls, name, barrier, status, emsg); - if (GNUNET_TESTBED_BARRIERSTATUS_INITIALISED == status) - return; /* just initialised; skip cleanup */ - - cleanup: - GNUNET_free_non_null (emsg); - if (NULL != barrier) - barrier_remove (barrier); } @@ -254,10 +102,11 @@ GNUNET_TESTBED_barrier_init_ (struct GNUNET_TESTBED_Controller *controller, name_len = strlen (name); GNUNET_assert (0 < name_len); GNUNET_CRYPTO_hash (name, name_len, &key); - if (NULL == barrier_map) - barrier_map = GNUNET_CONTAINER_multihashmap_create (3, GNUNET_YES); + if (NULL == controller->barrier_map) + controller->barrier_map = GNUNET_CONTAINER_multihashmap_create (3, GNUNET_YES); if (GNUNET_YES == - GNUNET_CONTAINER_multihashmap_contains (barrier_map, &key)) + GNUNET_CONTAINER_multihashmap_contains (controller->barrier_map, + &key)) { GNUNET_break (0); return NULL; @@ -271,7 +120,7 @@ GNUNET_TESTBED_barrier_init_ (struct GNUNET_TESTBED_Controller *controller, barrier->echo = echo; (void) memcpy (&barrier->key, &key, sizeof (struct GNUNET_HashCode)); GNUNET_assert (GNUNET_OK == - GNUNET_CONTAINER_multihashmap_put (barrier_map, + GNUNET_CONTAINER_multihashmap_put (controller->barrier_map, &barrier->key, barrier, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)); @@ -328,7 +177,7 @@ GNUNET_TESTBED_barrier_cancel (struct GNUNET_TESTBED_Barrier *barrier) msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_CANCEL); (void) memcpy (msg->name, barrier->name, strlen (barrier->name)); GNUNET_TESTBED_queue_message_ (barrier->c, &msg->header); - barrier_remove (barrier); + GNUNET_TESTBED_barrier_remove_ (barrier); } diff --git a/src/testbed/testbed_api_barriers.h b/src/testbed/testbed_api_barriers.h index 9aa988f9e..b839a4ce4 100644 --- a/src/testbed/testbed_api_barriers.h +++ b/src/testbed/testbed_api_barriers.h @@ -24,6 +24,8 @@ * exposed as user API) * @author Sree Harsha Totakura */ +#ifndef TESTBED_API_BARRIERS_H +#define TESTBED_API_BARRIERS_H #include "gnunet_testbed_service.h" @@ -40,7 +42,7 @@ * @param cb the callback to call when the barrier is reached or upon error. * Cannot be NULL. * @param cls closure for the above callback - * @param echo GNUNET_YES to echo the barrier crossed status message back to the + * @param echo #GNUNET_YES to echo the barrier crossed status message back to the * controller * @return barrier handle; NULL upon error */ @@ -48,5 +50,19 @@ struct GNUNET_TESTBED_Barrier * GNUNET_TESTBED_barrier_init_ (struct GNUNET_TESTBED_Controller *controller, const char *name, unsigned int quorum, - GNUNET_TESTBED_barrier_status_cb cb, void *cls, + GNUNET_TESTBED_barrier_status_cb cb, + void *cls, int echo); + + +/** + * Remove a barrier and it was the last one in the barrier hash map, destroy the + * hash map + * + * @param barrier the barrier to remove + */ +void +GNUNET_TESTBED_barrier_remove_ (struct GNUNET_TESTBED_Barrier *barrier); + + +#endif diff --git a/src/testbed/testbed_api_hosts.c b/src/testbed/testbed_api_hosts.c index d371108ec..5b1df615e 100644 --- a/src/testbed/testbed_api_hosts.c +++ b/src/testbed/testbed_api_hosts.c @@ -202,8 +202,7 @@ GNUNET_TESTBED_host_lookup_by_id_ (uint32_t id) */ struct GNUNET_TESTBED_Host * GNUNET_TESTBED_host_create_by_id_ (uint32_t id, - const struct GNUNET_CONFIGURATION_Handle - *cfg) + const struct GNUNET_CONFIGURATION_Handle *cfg) { return GNUNET_TESTBED_host_create_with_id (id, NULL, NULL, cfg, 0); } @@ -395,7 +394,6 @@ GNUNET_TESTBED_hosts_load_from_file (const char *filename, *cfg, struct GNUNET_TESTBED_Host ***hosts) { - //struct GNUNET_TESTBED_Host **host_array; struct GNUNET_TESTBED_Host *starting_host; char *data; char *buf; diff --git a/src/testbed/testbed_api_operations.c b/src/testbed/testbed_api_operations.c index 7f5aaa873..542f35b3c 100644 --- a/src/testbed/testbed_api_operations.c +++ b/src/testbed/testbed_api_operations.c @@ -400,7 +400,7 @@ static unsigned int n_expired_opqs; /** * The id of the task to process the ready queue */ -struct GNUNET_SCHEDULER_Task * process_rq_task_id; +struct GNUNET_SCHEDULER_Task *process_rq_task_id; /** -- 2.25.1