From 9ec9052c1cee478ab24f66a466774d6f63ace858 Mon Sep 17 00:00:00 2001 From: Sree Harsha Totakura Date: Mon, 3 Sep 2012 10:45:57 +0000 Subject: [PATCH] controller callback added in GNUNET_TESTBED_test_run --- src/testbed/test_testbed_api_test.c | 65 +++++++++++++++++++++++++---- src/testbed/testbed_api_test.c | 41 ++++++++++++++++-- src/testbed/testbed_api_testbed.c | 5 ++- 3 files changed, 100 insertions(+), 11 deletions(-) diff --git a/src/testbed/test_testbed_api_test.c b/src/testbed/test_testbed_api_test.c index 83d18ce0c..43a5628ad 100644 --- a/src/testbed/test_testbed_api_test.c +++ b/src/testbed/test_testbed_api_test.c @@ -33,6 +33,16 @@ */ #define NUM_PEERS 25 +/** + * Array of peers + */ +static struct GNUNET_TESTBED_Peer **peers; + +/** + * Operation handle + */ +static struct GNUNET_TESTBED_Operation *op; + /** * Testing result */ @@ -52,6 +62,42 @@ do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc) } +/** + * Controller event callback + * + * @param cls NULL + * @param event the controller event + */ +static void +controller_event_cb (void *cls, + const struct GNUNET_TESTBED_EventInformation *event) +{ + switch (event->type) + { + case GNUNET_TESTBED_ET_CONNECT: + GNUNET_assert (event->details.peer_connect.peer1 == peers[0]); + GNUNET_assert (event->details.peer_connect.peer2 == peers[1]); + GNUNET_TESTBED_operation_done (op); + op = GNUNET_TESTBED_peer_get_information (peers[0], + GNUNET_TESTBED_PIT_IDENTITY); + break; + case GNUNET_TESTBED_ET_OPERATION_FINISHED: + GNUNET_assert (event->details.operation_finished.operation == op); + GNUNET_assert (NULL == event->details.operation_finished.op_cls); + GNUNET_assert (NULL == event->details.operation_finished.emsg); + GNUNET_assert (GNUNET_TESTBED_PIT_IDENTITY == + event->details.operation_finished.pit); + GNUNET_assert (NULL != event->details.operation_finished.op_result.pid); + GNUNET_TESTBED_operation_done (op); + result = GNUNET_OK; + GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); + break; + default: + GNUNET_assert (0); + } +} + + /** * Signature of a main function for a testcase. * @@ -61,18 +107,17 @@ do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc) */ static void test_master (void *cls, unsigned int num_peers, - struct GNUNET_TESTBED_Peer **peers) + struct GNUNET_TESTBED_Peer **peers_) { unsigned int peer; GNUNET_assert (NULL == cls); GNUNET_assert (NUM_PEERS == num_peers); - GNUNET_assert (NULL != peers); + GNUNET_assert (NULL != peers_); for (peer = 0; peer < num_peers; peer++) - GNUNET_assert (NULL != peers[peer]); - result = GNUNET_OK; - /* Artificial delay for shutdown */ - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_shutdown, NULL); + GNUNET_assert (NULL != peers_[peer]); + peers = peers_; + op = GNUNET_TESTBED_overlay_connect (NULL, peers[0], peers[1]); } @@ -82,9 +127,15 @@ test_master (void *cls, unsigned int num_peers, int main (int argc, char **argv) { + uint64_t event_mask; + result = GNUNET_SYSERR; + event_mask = 0; + event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT); + event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED); GNUNET_TESTBED_test_run ("test_testbed_api_test", "test_testbed_api.conf", - NUM_PEERS, &test_master, NULL); + NUM_PEERS, event_mask, &controller_event_cb, NULL, + &test_master, NULL); if (GNUNET_OK != result) return 1; return 0; diff --git a/src/testbed/testbed_api_test.c b/src/testbed/testbed_api_test.c index c040e1c79..0a15b3db9 100644 --- a/src/testbed/testbed_api_test.c +++ b/src/testbed/testbed_api_test.c @@ -43,6 +43,21 @@ struct TestRunContext */ void *test_master_cls; + /** + * The controller event callback + */ + GNUNET_TESTBED_ControllerCallback cc; + + /** + * Closure for the above callback + */ + void *cc_cls; + + /** + * event mask for the controller callback + */ + uint64_t event_mask; + /** * Number of peers to start */ @@ -72,6 +87,8 @@ controller_event_cb (void *cls, { struct TestRunContext *rc = cls; + if ((NULL != rc->cc) && (0 != (rc->event_mask & (1LL << event->type)))) + rc->cc (rc->cc_cls, event); if (rc->peer_cnt == rc->num_peers) return; GNUNET_assert (GNUNET_TESTBED_ET_PEER_START == event->type); @@ -110,9 +127,12 @@ run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *config) { struct TestRunContext *rc = cls; - - GNUNET_TESTBED_run (NULL, config, rc->num_peers, 0, &controller_event_cb, rc, - &master_task, rc); + uint64_t event_mask; + + event_mask = rc->event_mask; + event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START); + GNUNET_TESTBED_run (NULL, config, rc->num_peers, event_mask, + &controller_event_cb, rc, &master_task, rc); } @@ -136,12 +156,24 @@ run (void *cls, char *const *args, const char *cfgfile, * @param cfg_filename configuration filename to use * (for testbed, controller and peers) * @param num_peers number of peers to start + * @param event_mask bit mask with set of events to call 'cc' for; + * or-ed values of "1LL" shifted by the + * respective 'enum GNUNET_TESTBED_EventType' + * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...") + * @param cc controller callback to invoke on events; This callback is called + * for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't + * set in the event_mask as this is the only way get access to the + * handle of each peer + * @param cc_cls closure for cc * @param test_master task to run once the test is ready * @param test_master_cls closure for 'task'. */ void GNUNET_TESTBED_test_run (const char *testname, const char *cfg_filename, unsigned int num_peers, + uint64_t event_mask, + GNUNET_TESTBED_ControllerCallback cc, + void *cc_cls, GNUNET_TESTBED_TestMaster test_master, void *test_master_cls) { @@ -165,6 +197,9 @@ GNUNET_TESTBED_test_run (const char *testname, const char *cfg_filename, rc->test_master = test_master; rc->test_master_cls = test_master_cls; rc->num_peers = num_peers; + rc->event_mask = event_mask; + rc->cc = cc; + rc->cc_cls = cc_cls; (void) GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2, testname, "nohelp", options, &run, rc); GNUNET_free (rc); diff --git a/src/testbed/testbed_api_testbed.c b/src/testbed/testbed_api_testbed.c index 6e6145aaa..76aec1d97 100644 --- a/src/testbed/testbed_api_testbed.c +++ b/src/testbed/testbed_api_testbed.c @@ -470,7 +470,10 @@ shutdown_run_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) * or-ed values of "1LL" shifted by the * respective 'enum GNUNET_TESTBED_EventType' * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...") - * @param cc controller callback to invoke on events + * @param cc controller callback to invoke on events; This callback is called + * for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't + * set in the event_mask as this is the only way get access to the + * handle of each peer * @param cc_cls closure for cc * @param master task to run once the testbed is ready */ -- 2.25.1