From b74a439892768274c39349e4c6974074e7e1203a Mon Sep 17 00:00:00 2001 From: xrs Date: Sat, 29 Feb 2020 17:58:00 +0100 Subject: refactor function logs; refactor conntect to services --- src/cadet/test_cadeT.c | 99 +++++++++++++++++++++++++++++++------------------- 1 file changed, 61 insertions(+), 38 deletions(-) diff --git a/src/cadet/test_cadeT.c b/src/cadet/test_cadeT.c index f489ca56d..cf335a948 100644 --- a/src/cadet/test_cadeT.c +++ b/src/cadet/test_cadeT.c @@ -47,6 +47,7 @@ * * Development * - red -> green -> refactor (cyclic) + * - be aware of Continuation Passing Style (CPS) programming */ #include "platform.h" #include "gnunet_testbed_service.h" @@ -60,12 +61,12 @@ /** * Testbed operation for connecting to the services. */ -struct GNUNET_TESTBED_Operation *testbed_to_svc[2]; +static struct GNUNET_TESTBED_Operation *testbed_to_svc[2]; /** * Testbed operation for requesting peer information. */ -struct GNUNET_TESTBED_Operation *testbed_info_req[2]; +static struct GNUNET_TESTBED_Operation *testbed_info_req[2]; /** * Port name kown by the two peers. @@ -80,21 +81,32 @@ static int test_result = 0; // FIXME: temp cnt static int cnt = 0; +/** + * Counter for gathering peerinformation. + */ +static int peerinfo_cnt = 0; + /** * Structure for storing information of testbed peers. */ -struct testbed_peers +struct TEST_PEERS { /** * Index of the peer. */ - int index; + int idx; /** * Peer Identity. */ struct GNUNET_PeerIdentity id; -} testbed_peers[2]; + + struct GNUNET_TESTBED_Peer *testbed_peer; + + int ready; + +} test_peers[2]; + /****************************** TEST LOGIC ********************************/ @@ -115,7 +127,7 @@ disconnect_from_peer (void *cls, { struct GNUNET_CADET_Handle *cadet = op_result; - GNUNET_log (GNUNET_ERROR_TYPE_INFO, "disconnect_from_cadet ()\n"); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%s\n", __func__); GNUNET_CADET_disconnect (cadet); } @@ -135,7 +147,7 @@ setup_initiating_peer (void *cls, struct GNUNET_PeerIdentity *destination; struct GNUNET_CADET_Channel *channel; - GNUNET_log (GNUNET_ERROR_TYPE_INFO, "setup_initiating_peer()\n"); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%s\n", __func__); cadet = GNUNET_CADET_connect (cfg); @@ -174,7 +186,7 @@ setup_listening_peer (void *cls, struct GNUNET_CADET_Handle *cadet; struct GNUNET_CADET_Port *port; - GNUNET_log (GNUNET_ERROR_TYPE_INFO, "setup_listening_peer()\n"); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%s\n", __func__); cadet = GNUNET_CADET_connect (cfg); @@ -198,26 +210,45 @@ check_test_readyness (void *cls, void *ca_result, const char *emsg) { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, "check_test_readyness()\n"); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%s\n", __func__); if (2 == ++cnt) GNUNET_SCHEDULER_shutdown (); } -static void -process_info_req (void *cb_cls, - struct GNUNET_TESTBED_Operation *op, - const struct GNUNET_TESTBED_PeerInformation *pinfo, - const char *emsg) +static int +peerinfo_complete () { - struct testbed_peers *testbed_peer = cb_cls; - struct GNUNET_PeerIdentity id = testbed_peer->id; - - GNUNET_memcpy (&id, pinfo->result.id, sizeof (struct GNUNET_PeerIdentity)); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer %s ready\n", GNUNET_i2s (&id)); + return (REQUESTED_PEERS == ++peerinfo_cnt) ? GNUNET_YES : GNUNET_NO; +} - // TODO: connect_to_peer_services +static void +connect_to_service (void *cb_cls, + struct GNUNET_TESTBED_Operation *op, + const struct GNUNET_TESTBED_PeerInformation *pinfo, + const char *emsg) +{ + struct TEST_PEERS *test_peer = cb_cls; + + // Store peer ID. + test_peer->id = *(pinfo->result.id); + + if (peerinfo_complete()) + { + testbed_to_svc[1] = + GNUNET_TESTBED_service_connect (NULL, test_peers[1].testbed_peer, + "cadet", + &check_test_readyness, NULL, + &setup_listening_peer, + &disconnect_from_peer, NULL); + testbed_to_svc[0] = + GNUNET_TESTBED_service_connect (NULL, test_peers[0].testbed_peer, + "cadet", + &check_test_readyness, NULL, + &setup_initiating_peer, + &disconnect_from_peer, NULL); + } } static void @@ -228,33 +259,25 @@ connect_to_peers (void *cls, unsigned int links_succeeded, unsigned int links_failed) { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, "connect_to_peers()\n"); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%s\n", __func__); GNUNET_assert (0 == links_failed); for (int i=0; i