From 51a5ecde0d0bca6e6f9998432ace8e67ac90e995 Mon Sep 17 00:00:00 2001 From: Irene Bandera Date: Mon, 26 Aug 2024 10:11:08 +0200 Subject: [PATCH] Refactor code for clarity and separation of concerns Signed-off-by: Irene Bandera --- .../rtps/DiscoveryServerParticipant.cpp | 343 +++++++++--------- 1 file changed, 169 insertions(+), 174 deletions(-) diff --git a/ddspipe_participants/src/cpp/participant/rtps/DiscoveryServerParticipant.cpp b/ddspipe_participants/src/cpp/participant/rtps/DiscoveryServerParticipant.cpp index 943268ed..272b3358 100644 --- a/ddspipe_participants/src/cpp/participant/rtps/DiscoveryServerParticipant.cpp +++ b/ddspipe_participants/src/cpp/participant/rtps/DiscoveryServerParticipant.cpp @@ -55,14 +55,11 @@ DiscoveryServerParticipant::reckon_participant_attributes_( const auto& tls_config = configuration->tls_configuration; // Needed values to check at the end if descriptor must be set - bool has_listening_addresses = false; - bool has_connection_addresses = false; bool has_listening_tcp_ipv4 = false; bool has_listening_tcp_ipv6 = false; - bool has_connection_tcp_ipv4 = false; - bool has_connection_tcp_ipv6 = false; - bool has_udp_ipv4 = false; - bool has_udp_ipv6 = false; + bool has_listening_udp_ipv4 = false; + bool has_listening_udp_ipv6 = false; + bool has_connection_descriptor = false; params.useBuiltinTransports = false; @@ -79,16 +76,46 @@ DiscoveryServerParticipant::reckon_participant_attributes_( continue; } - has_listening_addresses = true; + // Create DS SERVER locator + eprosima::fastdds::rtps::Locator listening_locator; + listening_locator.kind = address.get_locator_kind(); + eprosima::fastdds::rtps::IPLocator::setPhysicalPort(listening_locator, address.port()); - // TCP Listening WAN address - if (address.is_tcp()) + std::shared_ptr descriptor; + + switch (address.get_locator_kind()) { - if (address.is_ipv4()) + case LOCATOR_KIND_UDPv4: + { + has_listening_udp_ipv4 = true; + + auto descriptor_tmp = + create_descriptor(configuration->whitelist); + descriptor = descriptor_tmp; + + eprosima::fastdds::rtps::IPLocator::setIPv4(listening_locator, address.ip()); + + break; + } + + case LOCATOR_KIND_UDPv6: + { + has_listening_udp_ipv6 = true; + + auto descriptor_tmp = + create_descriptor(configuration->whitelist); + descriptor = descriptor_tmp; + + eprosima::fastdds::rtps::IPLocator::setIPv6(listening_locator, address.ip()); + + break; + } + + case LOCATOR_KIND_TCPv4: { has_listening_tcp_ipv4 = true; - std::shared_ptr descriptor; + std::shared_ptr descriptor_tmp; // We check if several descriptors share a WAN address. // If so, we add a new port to the previously created descriptor. @@ -103,7 +130,7 @@ DiscoveryServerParticipant::reckon_participant_attributes_( if ((tmp_descriptor != nullptr) && (address.ip() == tmp_descriptor->get_WAN_address())) { // Save in the new descriptor the previously added descriptor with the same WAN address - descriptor = tmp_descriptor; + descriptor_tmp = tmp_descriptor; // Set that a descriptor with same WAN address was found same_wan_addr = true; // Remove the previously added descriptor as this will be replaced by the same one updated with @@ -116,87 +143,69 @@ DiscoveryServerParticipant::reckon_participant_attributes_( // Add the new locator to the descriptor if another with the same wan address was found if (same_wan_addr) { - descriptor->add_listener_port(address.port()); + descriptor_tmp->add_listener_port(address.port()); } else { - descriptor = create_descriptor( - configuration->whitelist); - descriptor->add_listener_port(address.port()); - descriptor->set_WAN_address(address.ip()); + descriptor_tmp = + create_descriptor(configuration->whitelist); + + descriptor_tmp->add_listener_port(address.port()); + descriptor_tmp->set_WAN_address(address.ip()); // Enable TLS if (tls_config.is_active()) { - tls_config.enable_tls(descriptor); + tls_config.enable_tls(descriptor_tmp); } - } - params.userTransports.push_back(descriptor); + descriptor = descriptor_tmp; + + eprosima::fastdds::rtps::IPLocator::setPhysicalPort(listening_locator, address.external_port()); + eprosima::fastdds::rtps::IPLocator::setLogicalPort(listening_locator, address.external_port()); + eprosima::fastdds::rtps::IPLocator::setIPv4(listening_locator, address.ip()); + + break; } - else + + case LOCATOR_KIND_TCPv6: { has_listening_tcp_ipv6 = true; - std::shared_ptr descriptor = - create_descriptor(configuration->whitelist); - - descriptor->add_listener_port(address.port()); + auto descriptor_tmp = + create_descriptor(configuration->whitelist); + descriptor_tmp->add_listener_port(address.port()); // Enable TLS if (tls_config.is_active()) { - tls_config.enable_tls(descriptor); + tls_config.enable_tls(descriptor_tmp); } - params.userTransports.push_back(descriptor); - } - } - else - { - has_udp_ipv4 = address.is_ipv4(); - has_udp_ipv6 = !address.is_ipv4(); - } + descriptor = descriptor_tmp; - // For any, UDP or TCP - // Create Locator - eprosima::fastdds::rtps::Locator_t listening_locator; - eprosima::fastdds::rtps::Locator_t connection_locator; - listening_locator.kind = address.get_locator_kind(); - connection_locator.kind = address.get_locator_kind(); + eprosima::fastdds::rtps::IPLocator::setPhysicalPort(listening_locator, address.external_port()); + eprosima::fastdds::rtps::IPLocator::setLogicalPort(listening_locator, address.external_port()); + eprosima::fastdds::rtps::IPLocator::setIPv6(listening_locator, address.ip()); - // IP - if (address.is_ipv4()) - { - eprosima::fastdds::rtps::IPLocator::setIPv4(listening_locator, address.ip()); - eprosima::fastdds::rtps::IPLocator::setIPv4(connection_locator, address.ip()); - } - else - { - eprosima::fastdds::rtps::IPLocator::setIPv6(listening_locator, address.ip()); - eprosima::fastdds::rtps::IPLocator::setIPv6(connection_locator, address.ip()); + break; + } + + default: + break; } - // Port - eprosima::fastdds::rtps::IPLocator::setPhysicalPort(listening_locator, address.port()); - eprosima::fastdds::rtps::IPLocator::setPhysicalPort(connection_locator, address.external_port()); + // Add descriptor + params.userTransports.push_back(descriptor); - if (address.is_tcp()) - { - // Server side - // Internal local port is the one passed to add_listener_port (port value). - // If external port is not defined, it gets internal port value. Therefore, the physical - // port announced is equal to the internal port. - // If external port is defined, announced port is external port. This is the one clients, - // should try to connect, which should match network router public port. - eprosima::fastdds::rtps::IPLocator::setLogicalPort(listening_locator, address.port()); - eprosima::fastdds::rtps::IPLocator::setLogicalPort(connection_locator, address.external_port()); - } + // Set participant as SERVER + params.builtin.discovery_config.discoveryProtocol = + eprosima::fastdds::rtps::DiscoveryProtocol::SERVER; - // Add listening address to builtin + // Set SERVER's listening locator for PDP + params.defaultUnicastLocatorList.push_back(listening_locator); params.builtin.metatrafficUnicastLocatorList.push_back(listening_locator); - params.defaultUnicastLocatorList.push_back(listening_locator); // needed??? logDebug(DDSPIPE_DISCOVERYSERVER_PARTICIPANT, "Add listening address " << address << " to Participant " << configuration->id << "."); @@ -227,136 +236,122 @@ DiscoveryServerParticipant::reckon_participant_attributes_( continue; } - has_connection_addresses = true; - - eprosima::fastdds::rtps::Locator_t server_locator; - - // KIND + // Create DS locator + eprosima::fastdds::rtps::Locator server_locator; server_locator.kind = address.get_locator_kind(); - - // In case it is TCP mark has_connection_tcp as true - if (address.is_tcp()) - { - has_connection_tcp_ipv4 = address.is_ipv4(); - has_connection_tcp_ipv6 = !address.is_ipv4(); - } - else - { - has_udp_ipv4 = address.is_ipv4(); - has_udp_ipv6 = !address.is_ipv4(); - } - - // IP - if (address.is_ipv4()) - { - eprosima::fastdds::rtps::IPLocator::setIPv4(server_locator, address.ip()); - } - else - { - eprosima::fastdds::rtps::IPLocator::setIPv6(server_locator, address.ip()); - } - - // PORT eprosima::fastdds::rtps::IPLocator::setPhysicalPort(server_locator, address.port()); - if (address.is_tcp()) + std::shared_ptr descriptor; + + switch (address.get_locator_kind()) { - eprosima::fastdds::rtps::IPLocator::setLogicalPort(server_locator, address.port()); - // Warning: Logical port is not needed unless domain could change - } + case LOCATOR_KIND_UDPv4: + { + if (!has_listening_udp_ipv4) + { + has_connection_descriptor = true; + auto descriptor_tmp = + create_descriptor(configuration->whitelist); + // descriptor_tmp->interfaceWhiteList.push_back(address.ip()); + descriptor = descriptor_tmp; + } - // Add as remote server and add it to builtin - params.builtin.discovery_config.m_DiscoveryServers.push_back(server_locator); + eprosima::fastdds::rtps::IPLocator::setIPv4(server_locator, address.ip()); - logDebug(DDSPIPE_DISCOVERYSERVER_PARTICIPANT, - "Add connection address " << address << " to Server Participant " << configuration->id << "."); - } - } - - ///// - // Set this participant as a SERVER if has listening locators - if (has_listening_addresses) - { - params.builtin.discovery_config.discoveryProtocol = - fastdds::rtps::DiscoveryProtocol::SERVER; - } - else - { - params.builtin.discovery_config.discoveryProtocol = - fastdds::rtps::DiscoveryProtocol::SUPER_CLIENT; + break; + } - if (!has_connection_addresses) - { - EPROSIMA_LOG_WARNING(DDSPIPE_DISCOVERYSERVER_PARTICIPANT, - "Creating Participant " << configuration->id << " without listening or connection addresses. " << - "It will not communicate with any other Participant."); - } - } + case LOCATOR_KIND_UDPv6: + { + if (!has_listening_udp_ipv6) + { + has_connection_descriptor = true; + auto descriptor_tmp = + create_descriptor(configuration->whitelist); + // descriptor_tmp->interfaceWhiteList.push_back(address.ip()); + descriptor = descriptor_tmp; + } - ///// - // Set Server Guid - params.prefix = discovery_server_guid_prefix; + eprosima::fastdds::rtps::IPLocator::setIPv6(server_locator, address.ip()); - ///// - // Create specific descriptors if needed + break; + } - // If has TCP connections but not TCP listening addresses, it must specify the TCP transport - if (has_connection_tcp_ipv4 && !has_listening_tcp_ipv4) - { - std::shared_ptr descriptor = - create_descriptor(configuration->whitelist); + case LOCATOR_KIND_TCPv4: + { + if (!has_listening_tcp_ipv4) + { + has_connection_descriptor = true; + auto descriptor_tmp = + create_descriptor(configuration->whitelist); + descriptor_tmp->add_listener_port(0); + // descriptor_tmp->interfaceWhiteList.push_back(address.ip()); + + // Enable TLS + if (tls_config.is_active()) + { + tls_config.enable_tls(descriptor_tmp, true); + } + + descriptor = descriptor_tmp; + } - descriptor->add_listener_port(0); + eprosima::fastdds::rtps::IPLocator::setLogicalPort(server_locator, address.port()); + eprosima::fastdds::rtps::IPLocator::setIPv4(server_locator, address.ip()); - // Enable TLS - if (tls_config.is_active()) - { - tls_config.enable_tls(descriptor, true); - } + break; + } - params.userTransports.push_back(descriptor); + case LOCATOR_KIND_TCPv6: + { + if (!has_listening_tcp_ipv6) + { + has_connection_descriptor = true; + auto descriptor_tmp = + create_descriptor(configuration->whitelist); + descriptor_tmp->add_listener_port(0); + // descriptor_tmp->interfaceWhiteList.push_back(address.ip()); + + // Enable TLS + if (tls_config.is_active()) + { + tls_config.enable_tls(descriptor_tmp, true); + } + + descriptor = descriptor_tmp; + } - logDebug(DDSPIPE_DISCOVERYSERVER_PARTICIPANT, - "Adding TCPv4 Transport to Participant " << configuration->id << "."); - } - if (has_connection_tcp_ipv6 && !has_listening_tcp_ipv6) - { - std::shared_ptr descriptor = - create_descriptor(configuration->whitelist); + eprosima::fastdds::rtps::IPLocator::setLogicalPort(server_locator, address.port()); + eprosima::fastdds::rtps::IPLocator::setIPv6(server_locator, address.ip()); - descriptor->add_listener_port(0); + break; + } - // Enable TLS - if (tls_config.is_active()) - { - tls_config.enable_tls(descriptor, true); - } + default: + break; + } - params.userTransports.push_back(descriptor); + if (has_connection_descriptor) + { + // Set participant as DS CLIENT + params.builtin.discovery_config.discoveryProtocol = + eprosima::fastdds::rtps::DiscoveryProtocol::SUPER_CLIENT; - logDebug(DDSPIPE_DISCOVERYSERVER_PARTICIPANT, - "Adding TCPv6 Transport to Participant " << configuration->id << "."); - } + // Add descriptor + params.userTransports.push_back(descriptor); - // If has UDP, create descriptor because it has not been created yet - if (has_udp_ipv4) - { - std::shared_ptr descriptor = - create_descriptor(configuration->whitelist); - params.userTransports.push_back(descriptor); + logDebug(DDSPIPE_DISCOVERYSERVER_PARTICIPANT, + "Add connection address " << address << " to Participant " << configuration->id << "."); + } - logDebug(DDSPIPE_DISCOVERYSERVER_PARTICIPANT, - "Adding UDPv4 Transport to Participant " << configuration->id << "."); + // Add remote SERVER to CLIENT's list of SERVERs + params.builtin.discovery_config.m_DiscoveryServers.push_back(server_locator); + } } - if (has_udp_ipv6) - { - std::shared_ptr descriptor = - create_descriptor(configuration->whitelist); - params.userTransports.push_back(descriptor); - logDebug(DDSPIPE_DISCOVERYSERVER_PARTICIPANT, - "Adding UDPv6 Transport to Participant " << configuration->id << "."); - } + ///// + // Set Server Guid + params.prefix = discovery_server_guid_prefix; logDebug(DDSPIPE_DISCOVERYSERVER_PARTICIPANT, "Configured Participant " << configuration->id << " with server guid: " <<