From 8727c5c69097aeb474ce6039bf31650400b6286a Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 2 Feb 2015 23:16:13 +0000 Subject: reduce callback insanity for callbacks that are always bound to only one value --- src/transport/gnunet-service-transport.c | 109 +---------- src/transport/gnunet-service-transport.h | 2 + .../gnunet-service-transport_neighbours.c | 210 +++++++++++++-------- .../gnunet-service-transport_neighbours.h | 16 +- 4 files changed, 133 insertions(+), 204 deletions(-) (limited to 'src') diff --git a/src/transport/gnunet-service-transport.c b/src/transport/gnunet-service-transport.c index ca0f6b813..cd6bd9dff 100644 --- a/src/transport/gnunet-service-transport.c +++ b/src/transport/gnunet-service-transport.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - (C) 2010,2011 Christian Grothoff (and other contributing authors) + (C) 2010-2015 Christian Grothoff (and other contributing authors) GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -131,11 +131,6 @@ struct GNUNET_ATS_SchedulingHandle *GST_ats; */ struct GNUNET_TIME_Relative hello_expiration; -/** - * DEBUGGING connection counter - */ -static int connections; - /** * Head of DLL of asynchronous tasks to kill sessions. */ @@ -844,102 +839,6 @@ ats_request_address_change (void *cls, } -/** - * Function called to notify transport users that another - * peer connected to us. - * - * @param cls closure - * @param peer the peer that connected - * @param bandwidth_in inbound bandwidth in NBO - * @param bandwidth_out outbound bandwidth in NBO - */ -static void -neighbours_connect_notification (void *cls, - const struct GNUNET_PeerIdentity *peer, - struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in, - struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out) -{ - size_t len = sizeof(struct ConnectInfoMessage); - char buf[len] GNUNET_ALIGN; - struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf; - - connections++; - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "We are now connected to peer `%s' and %u peers in total\n", - GNUNET_i2s (peer), - connections); - connect_msg->header.size = htons (sizeof(buf)); - connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT); - connect_msg->id = *peer; - connect_msg->quota_in = bandwidth_in; - connect_msg->quota_out = bandwidth_out; - GST_clients_broadcast (&connect_msg->header, GNUNET_NO); -} - - -/** - * Function called to notify transport users that another - * peer disconnected from us. - * - * @param cls closure - * @param peer the peer that disconnected - */ -static void -neighbours_disconnect_notification (void *cls, - const struct GNUNET_PeerIdentity *peer) -{ - struct DisconnectInfoMessage disconnect_msg; - - connections--; - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Peer `%s' disconnected and we are connected to %u peers\n", - GNUNET_i2s (peer), - connections); - - GST_manipulation_peer_disconnect (peer); - disconnect_msg.header.size = htons (sizeof(struct DisconnectInfoMessage)); - disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT); - disconnect_msg.reserved = htonl (0); - disconnect_msg.peer = *peer; - GST_clients_broadcast (&disconnect_msg.header, GNUNET_NO); -} - - -/** - * Function called to notify transport users that a neighbour peer changed its - * active address. - * - * @param cls closure - * @param peer identity of the peer - * @param address address possibly NULL if peer is not connected - * @param state current state this peer is in - * @param state_timeout timeout for the current state of the peer - * @param bandwidth_in bandwidth assigned inbound, 0 on disconnect - * @param bandwidth_out bandwidth assigned outbound, 0 on disconnect - */ -static void -neighbours_changed_notification (void *cls, - const struct GNUNET_PeerIdentity *peer, - const struct GNUNET_HELLO_Address *address, - enum GNUNET_TRANSPORT_PeerState state, - struct GNUNET_TIME_Absolute state_timeout, - struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in, - struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out) -{ - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Notifying about change for peer `%s' with address `%s' in state `%s' timing out at %s\n", - GNUNET_i2s (peer), - GST_plugins_a2s (address), - GNUNET_TRANSPORT_ps2s (state), - GNUNET_STRINGS_absolute_time_to_string (state_timeout)); - /* FIXME: include bandwidth in notification! */ - GST_clients_broadcast_peer_notification (peer, - address, - state, - state_timeout); -} - - /** * Function called when the service shuts down. Unloads our plugins * and cancels pending validations. @@ -1097,11 +996,7 @@ run (void *cls, &plugin_env_session_end, &plugin_env_address_to_type, &plugin_env_update_metrics); - GST_neighbours_start (NULL, - &neighbours_connect_notification, - &neighbours_disconnect_notification, - &neighbours_changed_notification, - (max_fd / 3) * 2); + GST_neighbours_start ((max_fd / 3) * 2); GST_clients_start (GST_server); GST_validation_start ((max_fd / 3)); } diff --git a/src/transport/gnunet-service-transport.h b/src/transport/gnunet-service-transport.h index 34b0083a2..350ae5b6c 100644 --- a/src/transport/gnunet-service-transport.h +++ b/src/transport/gnunet-service-transport.h @@ -101,5 +101,7 @@ GST_receive_callback (void *cls, const struct GNUNET_MessageHeader *message); + + #endif /* end of file gnunet-service-transport_plugins.h */ diff --git a/src/transport/gnunet-service-transport_neighbours.c b/src/transport/gnunet-service-transport_neighbours.c index 401e6eb2b..f1095095a 100644 --- a/src/transport/gnunet-service-transport_neighbours.c +++ b/src/transport/gnunet-service-transport_neighbours.c @@ -29,6 +29,7 @@ #include "gnunet-service-transport_blacklist.h" #include "gnunet-service-transport_clients.h" #include "gnunet-service-transport_neighbours.h" +#include "gnunet-service-transport_manipulation.h" #include "gnunet-service-transport_plugins.h" #include "gnunet-service-transport_validation.h" #include "gnunet-service-transport.h" @@ -506,26 +507,6 @@ static struct BlacklistCheckSwitchContext *pending_bc_head; */ static struct BlacklistCheckSwitchContext *pending_bc_tail; -/** - * Closure for #connect_notify_cb, #disconnect_notify_cb and #neighbour_change_cb - */ -static void *callback_cls; - -/** - * Function to call when we connected to a neighbour. - */ -static NotifyConnect connect_notify_cb; - -/** - * Function to call when we disconnected from a neighbour. - */ -static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb; - -/** - * Function to call when a neighbour changed address, state or bandwidth. - */ -static GNUNET_TRANSPORT_NeighbourChangeCallback neighbour_change_cb; - /** * counter for connected neighbours */ @@ -566,6 +547,93 @@ print_ack_state (enum GST_ACK_State s) } } + +/** + * Notify our clients that another peer connected to us. + * + * @param peer the peer that connected + * @param bandwidth_in inbound bandwidth in NBO + * @param bandwidth_out outbound bandwidth in NBO + */ +static void +neighbours_connect_notification (const struct GNUNET_PeerIdentity *peer, + struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in, + struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out) +{ + size_t len = sizeof(struct ConnectInfoMessage); + char buf[len] GNUNET_ALIGN; + struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf; + + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "We are now connected to peer `%s'\n", + GNUNET_i2s (peer)); + connect_msg->header.size = htons (sizeof(buf)); + connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT); + connect_msg->id = *peer; + connect_msg->quota_in = bandwidth_in; + connect_msg->quota_out = bandwidth_out; + GST_clients_broadcast (&connect_msg->header, GNUNET_NO); +} + + +/** + * Notify our clients (and manipulation) that a peer disconnected from + * us. + * + * @param peer the peer that disconnected + */ +static void +neighbours_disconnect_notification (const struct GNUNET_PeerIdentity *peer) +{ + struct DisconnectInfoMessage disconnect_msg; + + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Peer `%s' disconnected\n", + GNUNET_i2s (peer)); + GST_manipulation_peer_disconnect (peer); + disconnect_msg.header.size = htons (sizeof(struct DisconnectInfoMessage)); + disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT); + disconnect_msg.reserved = htonl (0); + disconnect_msg.peer = *peer; + GST_clients_broadcast (&disconnect_msg.header, + GNUNET_NO); +} + + +/** + * Notify transport clients that a neighbour peer changed its active + * address. + * + * @param peer identity of the peer + * @param address address possibly NULL if peer is not connected + * @param state current state this peer is in + * @param state_timeout timeout for the current state of the peer + * @param bandwidth_in bandwidth assigned inbound, 0 on disconnect + * @param bandwidth_out bandwidth assigned outbound, 0 on disconnect + */ +static void +neighbours_changed_notification (const struct GNUNET_PeerIdentity *peer, + const struct GNUNET_HELLO_Address *address, + enum GNUNET_TRANSPORT_PeerState state, + struct GNUNET_TIME_Absolute state_timeout, + struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in, + struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out) +{ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Notifying about change for peer `%s' with address `%s' in state `%s' timing out at %s\n", + GNUNET_i2s (peer), + GST_plugins_a2s (address), + GNUNET_TRANSPORT_ps2s (state), + GNUNET_STRINGS_absolute_time_to_string (state_timeout)); + /* FIXME: include bandwidth in notification! */ + GST_clients_broadcast_peer_notification (peer, + address, + state, + state_timeout); +} + + + /** * Lookup a neighbour entry in the neighbours hash map. * @@ -666,13 +734,12 @@ set_state (struct NeighbourMapEntry *n, "Neighbour `%s' changed state to %s\n", GNUNET_i2s (&n->id), GNUNET_TRANSPORT_ps2s(s)); - neighbour_change_cb (callback_cls, - &n->id, - n->primary_address.address, - n->state, - n->timeout, - n->primary_address.bandwidth_in, - n->primary_address.bandwidth_out); + neighbours_changed_notification (&n->id, + n->primary_address.address, + n->state, + n->timeout, + n->primary_address.bandwidth_in, + n->primary_address.bandwidth_out); } @@ -695,13 +762,12 @@ set_state_and_timeout (struct NeighbourMapEntry *n, GNUNET_i2s (&n->id), GNUNET_TRANSPORT_ps2s(s), GNUNET_STRINGS_absolute_time_to_string (timeout)); - neighbour_change_cb (callback_cls, - &n->id, - n->primary_address.address, - n->state, - n->timeout, - n->primary_address.bandwidth_in, - n->primary_address.bandwidth_out); + neighbours_changed_notification (&n->id, + n->primary_address.address, + n->state, + n->timeout, + n->primary_address.bandwidth_in, + n->primary_address.bandwidth_out); } @@ -720,12 +786,11 @@ set_timeout (struct NeighbourMapEntry *n, "Neighbour `%s' changed timeout %s\n", GNUNET_i2s (&n->id), GNUNET_STRINGS_absolute_time_to_string (timeout)); - neighbour_change_cb (callback_cls, - &n->id, - n->primary_address.address, - n->state, n->timeout, - n->primary_address.bandwidth_in, - n->primary_address.bandwidth_out); + neighbours_changed_notification (&n->id, + n->primary_address.address, + n->state, n->timeout, + n->primary_address.bandwidth_in, + n->primary_address.bandwidth_out); } @@ -875,13 +940,12 @@ set_primary_address (struct NeighbourMapEntry *n, GNUNET_i2s (&n->id), GST_plugins_a2s(address)); - neighbour_change_cb (callback_cls, - &n->id, - n->primary_address.address, - n->state, - n->timeout, - n->primary_address.bandwidth_in, - n->primary_address.bandwidth_out); + neighbours_changed_notification (&n->id, + n->primary_address.address, + n->state, + n->timeout, + n->primary_address.bandwidth_in, + n->primary_address.bandwidth_out); } @@ -895,13 +959,12 @@ static void unset_primary_address (struct NeighbourMapEntry *n) { /* Notify monitoring about change */ - neighbour_change_cb (callback_cls, - &n->id, - n->primary_address.address, - n->state, - n->timeout, - GNUNET_BANDWIDTH_value_init (0), - GNUNET_BANDWIDTH_value_init (0)); + neighbours_changed_notification (&n->id, + n->primary_address.address, + n->state, + n->timeout, + GNUNET_BANDWIDTH_value_init (0), + GNUNET_BANDWIDTH_value_init (0)); /* Unset primary address */ free_address (&n->primary_address); } @@ -957,7 +1020,7 @@ free_neighbour (struct NeighbourMapEntry *n, gettext_noop ("# peers connected"), --neighbours_connected, GNUNET_NO); - disconnect_notify_cb (callback_cls, &n->id); + neighbours_disconnect_notification (&n->id); } /* Mark peer as disconnected */ @@ -1184,7 +1247,7 @@ disconnect_neighbour (struct NeighbourMapEntry *n) gettext_noop ("# peers connected"), --neighbours_connected, GNUNET_NO); - disconnect_notify_cb (callback_cls, &n->id); + neighbours_disconnect_notification (&n->id); set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT); break; case GNUNET_TRANSPORT_PS_RECONNECT_ATS: @@ -3213,10 +3276,9 @@ GST_neighbours_handle_session_syn_ack (const struct GNUNET_MessageHeader *messag gettext_noop ("# peers connected"), ++neighbours_connected, GNUNET_NO); - connect_notify_cb (callback_cls, - &n->id, - n->primary_address.bandwidth_in, - n->primary_address.bandwidth_out); + neighbours_connect_notification (&n->id, + n->primary_address.bandwidth_in, + n->primary_address.bandwidth_out); /* Tell ATS that the outbound session we created to send SYN was successful */ set_primary_address (n, n->primary_address.address, @@ -3502,10 +3564,9 @@ GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message, if (GNUNET_NO == GST_neighbours_test_connected(&n->id)) { /* Notify about connection */ - connect_notify_cb (callback_cls, - &n->id, - n->primary_address.bandwidth_in, - n->primary_address.bandwidth_out); + neighbours_connect_notification (&n->id, + n->primary_address.bandwidth_in, + n->primary_address.bandwidth_out); GNUNET_STATISTICS_set (GST_stats, gettext_noop ("# peers connected"), ++neighbours_connected, @@ -3833,24 +3894,11 @@ GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer) /** * Initialize the neighbours subsystem. * - * @param cls closure for callbacks - * @param connect_cb function to call if we connect to a peer - * @param disconnect_cb function to call if we disconnect from a peer - * @param peer_address_cb function to call if we change an active address - * of a neighbour * @param max_fds maximum number of fds to use */ void -GST_neighbours_start (void *cls, - NotifyConnect connect_cb, - GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb, - GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb, - unsigned int max_fds) -{ - callback_cls = cls; - connect_notify_cb = connect_cb; - disconnect_notify_cb = disconnect_cb; - neighbour_change_cb = peer_address_cb; +GST_neighbours_start (unsigned int max_fds) +{ neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO); registered_quota_notifications = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO); util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL, @@ -3926,10 +3974,6 @@ GST_neighbours_stop () registered_quota_notifications = NULL; neighbours = NULL; - callback_cls = NULL; - connect_notify_cb = NULL; - disconnect_notify_cb = NULL; - neighbour_change_cb = NULL; } diff --git a/src/transport/gnunet-service-transport_neighbours.h b/src/transport/gnunet-service-transport_neighbours.h index 0c07557d0..093bd06e1 100644 --- a/src/transport/gnunet-service-transport_neighbours.h +++ b/src/transport/gnunet-service-transport_neighbours.h @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - (C) 2010,2011 Christian Grothoff (and other contributing authors) + (C) 2010-2015 Christian Grothoff (and other contributing authors) GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -33,26 +33,14 @@ #include "transport.h" #include "gnunet_util_lib.h" -// TODO: -// - ATS and similar info is a bit lacking in the API right now... - - /** * Initialize the neighbours subsystem. * - * @param cls closure for callbacks - * @param connect_cb function to call if we connect to a peer - * @param disconnect_cb function to call if we disconnect from a peer - * @param peer_address_cb function to call if a neighbour's active address changes * @param max_fds maximum number of fds to use */ void -GST_neighbours_start (void *cls, - NotifyConnect connect_cb, - GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb, - GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb, - unsigned int max_fds); +GST_neighbours_start (unsigned int max_fds); /** -- cgit v1.2.3