From 40c8383cbd3decba4d605d6f94b102a1ed18a825 Mon Sep 17 00:00:00 2001 From: Sree Harsha Totakura Date: Thu, 21 Jun 2012 14:42:54 +0000 Subject: [PATCH] -added stream listen success callback --- src/include/gnunet_stream_lib.h | 19 +++++++- src/stream/stream_api.c | 19 +++++++- src/stream/test_stream_2peers.c | 22 ++++----- src/stream/test_stream_2peers_halfclose.c | 20 ++++---- src/stream/test_stream_big.c | 31 ++++++------ src/stream/test_stream_local.c | 27 +++++------ src/stream/test_stream_local.conf | 1 + src/stream/test_stream_sequence_wraparound.c | 50 +++++++++----------- 8 files changed, 105 insertions(+), 84 deletions(-) diff --git a/src/include/gnunet_stream_lib.h b/src/include/gnunet_stream_lib.h index c8a207d59..fd44ccfa0 100644 --- a/src/include/gnunet_stream_lib.h +++ b/src/include/gnunet_stream_lib.h @@ -85,6 +85,13 @@ typedef void (*GNUNET_STREAM_OpenCallback) (void *cls, struct GNUNET_STREAM_Socket *socket); +/** + * Callback for signalling stream listen success; See + * GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS + */ +typedef void (*GNUNET_STREAM_ListenSuccessCallback) (void); + + /** * Options for the stream. */ @@ -112,9 +119,17 @@ enum GNUNET_STREAM_Option GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER, /** - * Listen socket timeout in milliseconds + * Listen socket timeout in milliseconds given as uint32_t + */ + GNUNET_STREAM_OPTION_LISTEN_TIMEOUT, + + /** + * Option to register a callback when stream listening is + * successfull. Takes parameter of the form + * GNUNET_STREAM_ListenSuccessCallback. The callback is only called if + * listen is successful */ - GNUNET_STREAM_OPTION_LISTEN_TIMEOUT + GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS }; diff --git a/src/stream/stream_api.c b/src/stream/stream_api.c index cd89a1de3..f042ccc93 100644 --- a/src/stream/stream_api.c +++ b/src/stream/stream_api.c @@ -388,6 +388,11 @@ struct GNUNET_STREAM_ListenSocket */ struct GNUNET_LOCKMANAGER_LockingRequest *locking_request; + /** + * Callback to call after acquring a lock and listening + */ + GNUNET_STREAM_ListenSuccessCallback listen_ok_cb; + /** * The callback function which is called after successful opening socket */ @@ -2873,6 +2878,10 @@ lock_status_change_cb (void *cls, const char *domain, uint32_t lock, server_message_handlers, ports); GNUNET_assert (NULL != lsocket->mesh); + if (NULL != lsocket->listen_ok_cb) + { + (void) lsocket->listen_ok_cb (); + } } } if (GNUNET_LOCKMANAGER_RELEASE == status) @@ -2939,6 +2948,9 @@ GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg, case GNUNET_STREAM_OPTION_LISTEN_TIMEOUT: GNUNET_break (0); /* Option irrelevant in STREAM_open */ break; + case GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS: + GNUNET_break (0); /* Option irrelevant in STREAM_open */ + break; case GNUNET_STREAM_OPTION_END: break; } @@ -3200,7 +3212,8 @@ GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg, lsocket->retransmit_timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, default_timeout); lsocket->testing_active = GNUNET_NO; - listen_timeout = TIME_REL_SECS (60); /* A minute for listen timeout */ + lsocket->listen_ok_cb = NULL; + listen_timeout = TIME_REL_SECS (60); /* A minute for listen timeout */ va_start (vargs, listen_cb_cls); do { option = va_arg (vargs, enum GNUNET_STREAM_Option); @@ -3219,6 +3232,10 @@ GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg, listen_timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, va_arg (vargs, uint32_t)); break; + case GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS: + lsocket->listen_ok_cb = va_arg (vargs, + GNUNET_STREAM_ListenSuccessCallback); + break; case GNUNET_STREAM_OPTION_END: break; } diff --git a/src/stream/test_stream_2peers.c b/src/stream/test_stream_2peers.c index ecb4b848c..4313f646b 100644 --- a/src/stream/test_stream_2peers.c +++ b/src/stream/test_stream_2peers.c @@ -436,15 +436,11 @@ stream_listen_cb (void *cls, /** - * Task for connecting the peer to stream as client - * - * @param cls PeerData - * @param tc the TaskContext + * Listen success callback; connects a peer to stream as client */ static void -stream_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +stream_connect (void) { - /* Connect to stream library */ peer1.socket = GNUNET_STREAM_open (d1->cfg, &d2->id, /* Null for local peer? */ 10, /* App port */ @@ -494,13 +490,15 @@ peergroup_ready (void *cls, const char *emsg) GNUNET_i2s (&peer2.our_id), GNUNET_i2s (&d2->id)); - peer2_listen_socket = GNUNET_STREAM_listen (d2->cfg, - 10, /* App port */ - &stream_listen_cb, - NULL, - GNUNET_STREAM_OPTION_END); + peer2_listen_socket = + GNUNET_STREAM_listen (d2->cfg, + 10, /* App port */ + &stream_listen_cb, + NULL, + GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS, + &stream_connect, + GNUNET_STREAM_OPTION_END); GNUNET_assert (NULL != peer2_listen_socket); - GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(2), &stream_connect, NULL); } diff --git a/src/stream/test_stream_2peers_halfclose.c b/src/stream/test_stream_2peers_halfclose.c index 0e78dec20..8de758ea0 100644 --- a/src/stream/test_stream_2peers_halfclose.c +++ b/src/stream/test_stream_2peers_halfclose.c @@ -662,13 +662,10 @@ stream_listen_cb (void *cls, /** - * Task for connecting the peer to stream as client - * - * @param cls PeerData - * @param tc the TaskContext + * Listen success callback; connects a peer to stream as client */ static void -stream_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +stream_connect (void) { /* Connect to stream library */ peer1.socket = GNUNET_STREAM_open (d1->cfg, @@ -720,12 +717,15 @@ peergroup_ready (void *cls, const char *emsg) GNUNET_i2s (&peer2.our_id), GNUNET_i2s (&d2->id)); - peer2_listen_socket = GNUNET_STREAM_listen (d2->cfg, - 10, /* App port */ - &stream_listen_cb, - NULL); + peer2_listen_socket = + GNUNET_STREAM_listen (d2->cfg, + 10, /* App port */ + &stream_listen_cb, + NULL, + GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS, + &stream_connect, + GNUNET_STREAM_OPTION_END); GNUNET_assert (NULL != peer2_listen_socket); - GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(2), &stream_connect, NULL); } diff --git a/src/stream/test_stream_big.c b/src/stream/test_stream_big.c index dff5cc0a6..119028807 100644 --- a/src/stream/test_stream_big.c +++ b/src/stream/test_stream_big.c @@ -324,15 +324,12 @@ stream_listen_cb (void *cls, /** - * Task for connecting the peer to stream as client - * - * @param cls PeerData - * @param tc the TaskContext + * Listen success callback; connects a peer to stream as client */ static void -stream_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +stream_connect (void) { - struct PeerData *peer = cls; + struct PeerData *peer = &peer1; /* Connect to stream */ peer->socket = GNUNET_STREAM_open (config, @@ -360,20 +357,22 @@ run (void *cls, struct GNUNET_PeerIdentity self; GNUNET_TESTING_peer_get_identity (peer, &self); - config = cfg; + config = cfg; + peer2_listen_socket = + GNUNET_STREAM_listen (config, + 10, /* App port */ + &stream_listen_cb, + NULL, + GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS, + &stream_connect, + GNUNET_STREAM_OPTION_END); + GNUNET_assert (NULL != peer2_listen_socket); + peer1.self = self; + peer2.self = self; abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60), &do_abort, NULL); - peer2_listen_socket = GNUNET_STREAM_listen (config, - 10, /* App port */ - &stream_listen_cb, - NULL, - GNUNET_STREAM_OPTION_END); - GNUNET_assert (NULL != peer2_listen_socket); - peer1.self = self; - peer2.self = self; - GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(2), &stream_connect, &peer1); } /** diff --git a/src/stream/test_stream_local.c b/src/stream/test_stream_local.c index e660d77d7..af1b8ff84 100644 --- a/src/stream/test_stream_local.c +++ b/src/stream/test_stream_local.c @@ -335,19 +335,12 @@ stream_listen_cb (void *cls, /** - * Task for connecting the peer to stream as client - * - * @param cls PeerData - * @param tc the TaskContext + * Listen success callback; connects a peer to stream as client */ static void -stream_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +stream_connect (void) { - struct PeerData *peer = cls; - - GNUNET_assert (&peer1 == peer); - /* Connect to stream library */ - peer->socket = GNUNET_STREAM_open (config, + peer1.socket = GNUNET_STREAM_open (config, &self_id, 10, /* App port */ &stream_open_cb, @@ -368,13 +361,15 @@ run (void *cls, config = cfg; self = peer; GNUNET_TESTING_peer_get_identity (peer, &self_id); - peer2_listen_socket = GNUNET_STREAM_listen (config, - 10, /* App port */ - &stream_listen_cb, - &peer2, - GNUNET_STREAM_OPTION_END); + peer2_listen_socket = + GNUNET_STREAM_listen (config, + 10, /* App port */ + &stream_listen_cb, + &peer2, + GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS, + &stream_connect, + GNUNET_STREAM_OPTION_END); GNUNET_assert (NULL != peer2_listen_socket); - GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(2), &stream_connect, &peer1); abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30), &do_abort, diff --git a/src/stream/test_stream_local.conf b/src/stream/test_stream_local.conf index 90ddf41c6..09929ca27 100644 --- a/src/stream/test_stream_local.conf +++ b/src/stream/test_stream_local.conf @@ -2,6 +2,7 @@ AUTOSTART = NO ACCEPT_FROM = 127.0.0.1; HOSTNAME = localhost +PORT = 12101 [fs] AUTOSTART = NO diff --git a/src/stream/test_stream_sequence_wraparound.c b/src/stream/test_stream_sequence_wraparound.c index ffd2eb5fb..3ea6596bd 100644 --- a/src/stream/test_stream_sequence_wraparound.c +++ b/src/stream/test_stream_sequence_wraparound.c @@ -313,28 +313,22 @@ stream_listen_cb (void *cls, /** - * Task for connecting the peer to stream as client - * - * @param cls PeerData - * @param tc the TaskContext + * Listen success callback; connects a peer to stream as client */ static void -stream_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +stream_connect (void) { - struct PeerData *peer = cls; - - GNUNET_assert (&peer1 == peer); - /* Connect to stream */ - peer->socket = GNUNET_STREAM_open (config, - &self_id, /* Null for local peer? */ - 10, /* App port */ - &stream_open_cb, - &peer1, - GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER, - UINT32_MAX - GNUNET_CRYPTO_random_u32 - (GNUNET_CRYPTO_QUALITY_WEAK, 64), - GNUNET_STREAM_OPTION_END); - GNUNET_assert (NULL != peer->socket); + peer1.socket = + GNUNET_STREAM_open (config, + &self_id, /* Null for local peer? */ + 10, /* App port */ + &stream_open_cb, + &peer1, + GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER, + UINT32_MAX - GNUNET_CRYPTO_random_u32 + (GNUNET_CRYPTO_QUALITY_WEAK, 64), + GNUNET_STREAM_OPTION_END); + GNUNET_assert (NULL != peer1.socket); } @@ -349,15 +343,17 @@ run (void *cls, config = cfg; self = peer; (void) GNUNET_TESTING_peer_get_identity (peer, &self_id); - peer2_listen_socket = GNUNET_STREAM_listen (config, - 10, /* App port */ - &stream_listen_cb, - NULL, - GNUNET_STREAM_OPTION_LISTEN_TIMEOUT, - 30 * 1000, - GNUNET_STREAM_OPTION_END); + peer2_listen_socket = + GNUNET_STREAM_listen (config, + 10, /* App port */ + &stream_listen_cb, + NULL, + GNUNET_STREAM_OPTION_LISTEN_TIMEOUT, + 60 * 1000, + GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS, + &stream_connect, + GNUNET_STREAM_OPTION_END); GNUNET_assert (NULL != peer2_listen_socket); - GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(10), &stream_connect, &peer1); abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100), &do_abort, -- 2.25.1