From 07dd8e7624cdc8fb02fe69fdd20ed83691d355fc Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 16 Apr 2014 09:10:58 +0000 Subject: [PATCH] -signal monitor disconnect via callback --- src/transport/gnunet-transport.c | 99 +++++++++++++++--------- src/transport/transport_api_monitoring.c | 34 ++++---- 2 files changed, 83 insertions(+), 50 deletions(-) diff --git a/src/transport/gnunet-transport.c b/src/transport/gnunet-transport.c index 707b796dd..c701970d1 100644 --- a/src/transport/gnunet-transport.c +++ b/src/transport/gnunet-transport.c @@ -310,18 +310,21 @@ struct MonitoredPeer }; - int destroy_it (void *cls, - const struct GNUNET_PeerIdentity *key, - void *value) +static int +destroy_it (void *cls, + const struct GNUNET_PeerIdentity *key, + void *value) { - struct MonitoredPeer *m = value; - GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (monitored_peers, - key, value)); - GNUNET_free_non_null (m->address); - GNUNET_free (value); - return GNUNET_OK; + struct MonitoredPeer *m = value; + + GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (monitored_peers, + key, value)); + GNUNET_free_non_null (m->address); + GNUNET_free (value); + return GNUNET_OK; } + /** * Task run in monitor mode when the user presses CTRL-C to abort. * Stops monitoring activity. @@ -567,11 +570,13 @@ fail_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) static void resolve_validation_address (const struct GNUNET_PeerIdentity *id, - const struct GNUNET_HELLO_Address *address, int numeric, - struct GNUNET_TIME_Absolute last_validation, - struct GNUNET_TIME_Absolute valid_until, - struct GNUNET_TIME_Absolute next_validation, - enum GNUNET_TRANSPORT_ValidationState state); + const struct GNUNET_HELLO_Address *address, + int numeric, + struct GNUNET_TIME_Absolute last_validation, + struct GNUNET_TIME_Absolute valid_until, + struct GNUNET_TIME_Absolute next_validation, + enum GNUNET_TRANSPORT_ValidationState state); + static void process_validation_string (void *cls, const char *address) @@ -685,13 +690,14 @@ resolve_validation_address (const struct GNUNET_PeerIdentity *id, } -void process_validation_cb (void *cls, - const struct GNUNET_PeerIdentity *peer, - const struct GNUNET_HELLO_Address *address, - struct GNUNET_TIME_Absolute last_validation, - struct GNUNET_TIME_Absolute valid_until, - struct GNUNET_TIME_Absolute next_validation, - enum GNUNET_TRANSPORT_ValidationState state) +static void +process_validation_cb (void *cls, + const struct GNUNET_PeerIdentity *peer, + const struct GNUNET_HELLO_Address *address, + struct GNUNET_TIME_Absolute last_validation, + struct GNUNET_TIME_Absolute valid_until, + struct GNUNET_TIME_Absolute next_validation, + enum GNUNET_TRANSPORT_ValidationState state) { if ((NULL == peer) && (NULL == address)) { @@ -716,6 +722,7 @@ void process_validation_cb (void *cls, valid_until, next_validation, state); } + static void run_nat_test () { @@ -736,6 +743,7 @@ run_nat_test () head->tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, head); } + /** * Test our plugin's configuration (NAT traversal, etc.). * @@ -1009,16 +1017,21 @@ notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer, } } + static void resolve_peer_address (const struct GNUNET_PeerIdentity *id, - const struct GNUNET_HELLO_Address *address, int numeric, - enum GNUNET_TRANSPORT_PeerState state, - struct GNUNET_TIME_Absolute state_timeout); + const struct GNUNET_HELLO_Address *address, + int numeric, + enum GNUNET_TRANSPORT_PeerState state, + struct GNUNET_TIME_Absolute state_timeout); + static void -print_info (const struct GNUNET_PeerIdentity *id, const char *transport, - const char *addr, enum GNUNET_TRANSPORT_PeerState state, - struct GNUNET_TIME_Absolute state_timeout) +print_info (const struct GNUNET_PeerIdentity *id, + const char *transport, + const char *addr, + enum GNUNET_TRANSPORT_PeerState state, + struct GNUNET_TIME_Absolute state_timeout) { if ( ((GNUNET_YES == iterate_connections) && (GNUNET_YES == iterate_all)) || @@ -1039,6 +1052,7 @@ print_info (const struct GNUNET_PeerIdentity *id, const char *transport, } } + static void process_peer_string (void *cls, const char *address) { @@ -1090,11 +1104,13 @@ process_peer_string (void *cls, const char *address) } } + static void resolve_peer_address (const struct GNUNET_PeerIdentity *id, - const struct GNUNET_HELLO_Address *address, int numeric, - enum GNUNET_TRANSPORT_PeerState state, - struct GNUNET_TIME_Absolute state_timeout) + const struct GNUNET_HELLO_Address *address, + int numeric, + enum GNUNET_TRANSPORT_PeerState state, + struct GNUNET_TIME_Absolute state_timeout) { struct PeerResolutionContext *rc; @@ -1114,6 +1130,7 @@ resolve_peer_address (const struct GNUNET_PeerIdentity *id, RESOLUTION_TIMEOUT, &process_peer_string, rc); } + /** * Function called with information about a peers during a one shot iteration * @@ -1126,13 +1143,20 @@ resolve_peer_address (const struct GNUNET_PeerIdentity *id, * */ static void -process_peer_iteration_cb (void *cls, const struct GNUNET_PeerIdentity *peer, - const struct GNUNET_HELLO_Address *address, - enum GNUNET_TRANSPORT_PeerState state, - struct GNUNET_TIME_Absolute state_timeout) +process_peer_iteration_cb (void *cls, + const struct GNUNET_PeerIdentity *peer, + const struct GNUNET_HELLO_Address *address, + enum GNUNET_TRANSPORT_PeerState state, + struct GNUNET_TIME_Absolute state_timeout) { - if (peer == NULL ) + if (NULL == peer) { + if (monitor_connections) + { + FPRINTF (stdout, + _("Monitor disconnected from transport service. Reconnecting.\n")); + return; + } /* done */ address_resolution_in_progress = GNUNET_NO; pic = NULL; @@ -1150,8 +1174,9 @@ process_peer_iteration_cb (void *cls, const struct GNUNET_PeerIdentity *peer, op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout, NULL ); - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received address for peer `%s': %s\n", - GNUNET_i2s (peer), address->transport_name); + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Received address for peer `%s': %s\n", + GNUNET_i2s (peer), address->transport_name); if (NULL != address) resolve_peer_address (peer, address, numeric, state, state_timeout); diff --git a/src/transport/transport_api_monitoring.c b/src/transport/transport_api_monitoring.c index f8dfc9ead..624ed4f74 100644 --- a/src/transport/transport_api_monitoring.c +++ b/src/transport/transport_api_monitoring.c @@ -375,6 +375,10 @@ reconnect_val_ctx (struct GNUNET_TRANSPORT_ValidationMonitoringContext *val_ctx) GNUNET_assert (GNUNET_NO == val_ctx->one_shot); GNUNET_CLIENT_disconnect (val_ctx->client); val_ctx->client = NULL; + /* notify clients about (re)connect */ + val_ctx->cb (val_ctx->cb_cls, NULL, NULL, + GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TIME_UNIT_ZERO_ABS, + GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TRANSPORT_VS_TIMEOUT); val_ctx->backoff = GNUNET_TIME_STD_BACKOFF (val_ctx->backoff); val_ctx->reconnect_task = GNUNET_SCHEDULER_add_delayed (val_ctx->backoff, &do_val_connect, @@ -384,7 +388,7 @@ reconnect_val_ctx (struct GNUNET_TRANSPORT_ValidationMonitoringContext *val_ctx) /** * Function called with responses from the service. * - * @param cls our 'struct GNUNET_TRANSPORT_PeerMonitoringContext*' + * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *` * @param msg NULL on timeout or error, otherwise presumably a * message with the human-readable address */ @@ -400,7 +404,7 @@ val_response_processor (void *cls, const struct GNUNET_MessageHeader *msg) size_t tlen; size_t alen; - if (msg == NULL) + if (NULL == msg) { if (val_ctx->one_shot) { @@ -530,15 +534,17 @@ val_response_processor (void *cls, const struct GNUNET_MessageHeader *msg) GNUNET_HELLO_address_free (address); } /* expect more replies */ - GNUNET_CLIENT_receive (val_ctx->client, &val_response_processor, - val_ctx, GNUNET_TIME_absolute_get_remaining (val_ctx->timeout)); + GNUNET_CLIENT_receive (val_ctx->client, + &val_response_processor, + val_ctx, + GNUNET_TIME_absolute_get_remaining (val_ctx->timeout)); } /** * Function called with responses from the service. * - * @param cls our 'struct GNUNET_TRANSPORT_PeerMonitoringContext*' + * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *` * @param msg NULL on timeout or error, otherwise presumably a * message with the human-readable address */ @@ -577,6 +583,7 @@ peer_response_processor (void *cls, const struct GNUNET_MessageHeader *msg) /* Done! */ if (pal_ctx->one_shot) { + /* iteration finished */ pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS); GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx); @@ -594,6 +601,7 @@ peer_response_processor (void *cls, const struct GNUNET_MessageHeader *msg) GNUNET_break (0); if (pal_ctx->one_shot) { + /* iteration finished (with error) */ pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS); GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx); @@ -697,19 +705,19 @@ peer_response_processor (void *cls, const struct GNUNET_MessageHeader *msg) * @param cfg configuration to use * @param peer a specific peer identity to obtain information for, * NULL for all peers - * @param one_shot GNUNET_YES to return the current state and then end (with NULL+NULL), - * GNUNET_NO to monitor peers continuously + * @param one_shot #GNUNET_YES to return the current state and then end (with NULL+NULL), + * #GNUNET_NO to monitor peers continuously * @param timeout how long is the lookup allowed to take at most * @param peer_callback function to call with the results - * @param peer_callback_cls closure for peer_address_callback + * @param peer_callback_cls closure for @a peer_address_callback */ struct GNUNET_TRANSPORT_PeerMonitoringContext * GNUNET_TRANSPORT_monitor_peers (const struct GNUNET_CONFIGURATION_Handle *cfg, - const struct GNUNET_PeerIdentity *peer, - int one_shot, - struct GNUNET_TIME_Relative timeout, - GNUNET_TRANSPORT_PeerIterateCallback peer_callback, - void *peer_callback_cls) + const struct GNUNET_PeerIdentity *peer, + int one_shot, + struct GNUNET_TIME_Relative timeout, + GNUNET_TRANSPORT_PeerIterateCallback peer_callback, + void *peer_callback_cls) { struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx; struct GNUNET_CLIENT_Connection *client; -- 2.25.1