Skip to content

Commit

Permalink
Refs #14221: Fix conficts backport #2246 to 2.1.x
Browse files Browse the repository at this point in the history
Signed-off-by: RaulSanchez <raul@eprosima.com>
  • Loading branch information
rsanchez15 committed Mar 23, 2022
1 parent b63e46e commit 7e30298
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 406 deletions.
23 changes: 23 additions & 0 deletions include/fastdds/rtps/builtin/discovery/participant/PDPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,29 @@ class PDPClient : public PDP
*/
bool match_servers_EDP_endpoints();

/*
* Update the list of remote servers
*/
void update_remote_servers_list();

protected:

/**
* Manually match the local PDP reader with the PDP writer of a given server. The function is
* not thread safe (nts) in the sense that it does not take the PDP mutex. It does however take
* temp_data_lock_
*/
void match_pdp_writer_nts_(
const eprosima::fastdds::rtps::RemoteServerAttributes& server_att);

/**
* Manually match the local PDP writer with the PDP reader of a given server. The function is
* not thread safe (nts) in the sense that it does not take the PDP mutex. It does however take
* temp_data_lock_
*/
void match_pdp_reader_nts_(
const eprosima::fastdds::rtps::RemoteServerAttributes& server_att);

private:

/**
Expand Down
186 changes: 0 additions & 186 deletions src/cpp/rtps/builtin/discovery/participant/PDPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,6 @@ bool PDPClient::match_servers_EDP_endpoints()
return all;
}

<<<<<<< HEAD
=======
void PDPClient::update_remote_servers_list()
{
if (!mp_PDPReader || !mp_PDPWriter)
Expand Down Expand Up @@ -644,189 +642,6 @@ void PDPClient::match_pdp_reader_nts_(
mp_PDPWriter->matched_reader_add(temp_reader_data_);
}

const std::string& ros_discovery_server_env()
{
static std::string servers;
SystemInfo::get_env(DEFAULT_ROS2_MASTER_URI, servers);
return servers;
}

bool load_environment_server_info(
RemoteServerList_t& attributes)
{
return load_environment_server_info(ros_discovery_server_env(), attributes);
}

bool load_environment_server_info(
std::string list,
RemoteServerList_t& attributes)
{
attributes.clear();
if (list.empty())
{
return true;
}

/* Parsing ancillary regex */
// Address should be <letter,numbers,dots>:<number>. We do not need to verify that the first part
// is an IPv4 address, as it is done latter.
const std::regex ROS2_ADDRESS_PATTERN(R"(^([A-Za-z0-9-.]+)?:?(?:(\d+))?$)");
const std::regex ROS2_SERVER_LIST_PATTERN(R"(([^;]*);?)");

try
{
// Do the parsing and populate the list
RemoteServerAttributes server_att;
Locator_t server_locator(LOCATOR_KIND_UDPv4, DEFAULT_ROS2_SERVER_PORT);
int server_id = 0;

std::sregex_iterator server_it(
list.begin(),
list.end(),
ROS2_SERVER_LIST_PATTERN,
std::regex_constants::match_not_null);

while (server_it != std::sregex_iterator())
{
const std::smatch::value_type sm = *++(server_it->cbegin());

if (sm.matched)
{
// now we must parse the inner expression
std::smatch mr;
std::string locator(sm);
if (std::regex_match(locator, mr, ROS2_ADDRESS_PATTERN, std::regex_constants::match_not_null))
{
std::smatch::iterator it = mr.cbegin();

while (++it != mr.cend())
{
std::string address = it->str();

// Check whether the address is IPv4
if (!IPLocator::isIPv4(address))
{
auto response = rtps::IPLocator::resolveNameDNS(address);

// Add the first valid IPv4 address that we can find
if (response.first.size() > 0)
{
address = response.first.begin()->data();
}
}

if (!IPLocator::setIPv4(server_locator, address))
{
std::stringstream ss;
ss << "Wrong ipv4 address passed into the server's list " << address;
throw std::invalid_argument(ss.str());
}

if (IPLocator::isAny(server_locator))
{
// A server cannot be reach in all interfaces, it's clearly a localhost call
IPLocator::setIPv4(server_locator, "127.0.0.1");
}

if (++it != mr.cend())
{
// reset the locator to default
IPLocator::setPhysicalPort(server_locator, DEFAULT_ROS2_SERVER_PORT);

if (it->matched)
{
// note stoi throws also an invalid_argument
int port = stoi(it->str());

if (port > std::numeric_limits<uint16_t>::max())
{
throw std::out_of_range("Too large udp port passed into the server's list");
}

if (!IPLocator::setPhysicalPort(server_locator, static_cast<uint16_t>(port)))
{
std::stringstream ss;
ss << "Wrong udp port passed into the server's list " << it->str();
throw std::invalid_argument(ss.str());
}
}
}
}

// add the server to the list
if (!get_server_client_default_guidPrefix(server_id, server_att.guidPrefix))
{
throw std::invalid_argument("The maximum number of default discovery servers has been reached");
}

server_att.metatrafficUnicastLocatorList.clear();
server_att.metatrafficUnicastLocatorList.push_back(server_locator);
attributes.push_back(server_att);
}
else
{
if (!locator.empty())
{
std::stringstream ss;
ss << "Wrong locator passed into the server's list " << locator;
throw std::invalid_argument(ss.str());
}
// else: it's intencionally empty to hint us to ignore this server
}
}
// advance to the next server if any
++server_it;
++server_id;
}

// Check for server info
if (attributes.empty())
{
throw std::invalid_argument("No default server locators were provided.");
}
}
catch (std::exception& e)
{
logError(SERVER_CLIENT_DISCOVERY, e.what());
attributes.clear();
return false;
}

return true;
}

GUID_t RemoteServerAttributes::GetParticipant() const
{
return GUID_t(guidPrefix, c_EntityId_RTPSParticipant);
}

GUID_t RemoteServerAttributes::GetPDPReader() const
{
return GUID_t(guidPrefix, c_EntityId_SPDPReader);
}

GUID_t RemoteServerAttributes::GetPDPWriter() const
{
return GUID_t(guidPrefix, c_EntityId_SPDPWriter);
}

bool get_server_client_default_guidPrefix(
int id,
GuidPrefix_t& guid)
{
if ( id >= 0
&& id < 256
&& std::istringstream(DEFAULT_ROS2_SERVER_GUIDPREFIX) >> guid)
{
// Third octet denotes the server id
guid.value[2] = static_cast<octet>(id);

return true;
}

return false;
}

bool PDPClient::remove_remote_participant(
const GUID_t& partGUID,
ParticipantDiscoveryInfo::DISCOVERY_STATUS reason)
Expand All @@ -853,7 +668,6 @@ bool PDPClient::remove_remote_participant(
return false;
}

>>>>>>> 436826000 (Discovery Server fix reconnection (#2246))
} /* namespace rtps */
} /* namespace fastrtps */
} /* namespace eprosima */
Loading

0 comments on commit 7e30298

Please sign in to comment.