Skip to content

Commit 20997a0

Browse files
Move easy mode logic to RTPS layer
Signed-off-by: Juan Lopez Fernandez <[email protected]>
1 parent 3d7dcab commit 20997a0

File tree

3 files changed

+225
-46
lines changed

3 files changed

+225
-46
lines changed

src/cpp/fastdds/domain/DomainParticipantImpl.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -274,23 +274,16 @@ ReturnCode_t DomainParticipantImpl::enable()
274274
utils::set_attributes_from_qos(rtps_attr, qos_);
275275
rtps_attr.participantID = participant_id_;
276276

277-
// If DEFAULT_ROS2_MASTER_URI is specified then try to create default client if
278-
// that already exists.
279-
RTPSParticipant* part = RTPSDomainImpl::clientServerEnvironmentCreationOverride(
277+
RTPSParticipant* part = RTPSDomain::createParticipant(
280278
domain_id_,
281279
false,
282280
rtps_attr,
283281
&rtps_listener_);
284282

285283
if (part == nullptr)
286284
{
287-
part = RTPSDomain::createParticipant(domain_id_, false, rtps_attr, &rtps_listener_);
288-
289-
if (part == nullptr)
290-
{
291-
EPROSIMA_LOG_ERROR(DOMAIN_PARTICIPANT, "Problem creating RTPSParticipant");
292-
return RETCODE_ERROR;
293-
}
285+
EPROSIMA_LOG_ERROR(DOMAIN_PARTICIPANT, "Problem creating RTPSParticipant");
286+
return RETCODE_ERROR;
294287
}
295288

296289
guid_ = part->getGuid();

src/cpp/rtps/RTPSDomain.cpp

Lines changed: 203 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ RTPSParticipant* RTPSDomain::createParticipant(
118118
const RTPSParticipantAttributes& attrs,
119119
RTPSParticipantListener* listen)
120120
{
121-
return RTPSDomainImpl::createParticipant(domain_id, true, attrs, listen);
121+
return createParticipant(domain_id, true, attrs, listen);
122122
}
123123

124124
RTPSParticipant* RTPSDomain::createParticipant(
@@ -127,7 +127,20 @@ RTPSParticipant* RTPSDomain::createParticipant(
127127
const RTPSParticipantAttributes& attrs,
128128
RTPSParticipantListener* listen)
129129
{
130-
return RTPSDomainImpl::createParticipant(domain_id, enabled, attrs, listen);
130+
RTPSParticipant* part = nullptr;
131+
RTPSParticipantAttributes env_attrs = attrs;
132+
133+
if (RTPSDomainImpl::client_server_environment_attributes_override(domain_id, env_attrs))
134+
{
135+
part = RTPSDomainImpl::createParticipant(domain_id, enabled, env_attrs, listen);
136+
}
137+
138+
if (nullptr == part)
139+
{
140+
part = RTPSDomainImpl::createParticipant(domain_id, enabled, attrs, listen);
141+
}
142+
143+
return part;
131144
}
132145

133146
bool RTPSDomain::removeRTPSParticipant(
@@ -518,18 +531,188 @@ bool RTPSDomainImpl::removeRTPSReader(
518531
return false;
519532
}
520533

521-
RTPSParticipant* RTPSDomainImpl::clientServerEnvironmentCreationOverride(
534+
// RTPSParticipant* RTPSDomainImpl::clientServerEnvironmentCreationOverride(
535+
// uint32_t domain_id,
536+
// bool enabled,
537+
// const RTPSParticipantAttributes& att,
538+
// RTPSParticipantListener* listen)
539+
// {
540+
// // Check the specified discovery protocol: if other than simple it has priority over ros environment variable
541+
// if (att.builtin.discovery_config.discoveryProtocol != DiscoveryProtocol::SIMPLE)
542+
// {
543+
// EPROSIMA_LOG_INFO(RTPS_DOMAIN, "Detected non simple discovery protocol attributes."
544+
// << " Ignoring auto default client-server setup.");
545+
// return nullptr;
546+
// }
547+
548+
// // We only make the attributes copy when we are sure is worth
549+
// // Is up to the caller guarantee the att argument is not modified during the call
550+
// RTPSParticipantAttributes client_att(att);
551+
552+
// // Check whether we need to initialize in easy mode
553+
// const std::string& easy_mode_env_value = easy_mode_env();
554+
555+
// if (easy_mode_env_value.empty())
556+
// {
557+
// // Retrieve the info from the environment variable
558+
// LocatorList_t& server_list = client_att.builtin.discovery_config.m_DiscoveryServers;
559+
// if (load_environment_server_info(server_list) && server_list.empty())
560+
// {
561+
// // It's not an error, the environment variable may not be set. Any issue with environment
562+
// // variable syntax is EPROSIMA_LOG_ERROR already
563+
// return nullptr;
564+
// }
565+
566+
// // Check if some address requires the UDPv6, TCPv4 or TCPv6 transport
567+
// if (server_list.has_kind<LOCATOR_KIND_UDPv6>() &&
568+
// !has_user_transport<fastdds::rtps::UDPv6TransportDescriptor>(client_att))
569+
// {
570+
// // Extend builtin transports with the UDPv6 transport
571+
// auto descriptor = std::make_shared<fastdds::rtps::UDPv6TransportDescriptor>();
572+
// descriptor->sendBufferSize = client_att.sendSocketBufferSize;
573+
// descriptor->receiveBufferSize = client_att.listenSocketBufferSize;
574+
// client_att.userTransports.push_back(std::move(descriptor));
575+
// }
576+
// if (server_list.has_kind<LOCATOR_KIND_TCPv4>() &&
577+
// !has_user_transport<fastdds::rtps::TCPv4TransportDescriptor>(client_att))
578+
// {
579+
// // Extend builtin transports with the TCPv4 transport
580+
// auto descriptor = std::make_shared<fastdds::rtps::TCPv4TransportDescriptor>();
581+
// // Add automatic port
582+
// descriptor->add_listener_port(0);
583+
// descriptor->sendBufferSize = client_att.sendSocketBufferSize;
584+
// descriptor->receiveBufferSize = client_att.listenSocketBufferSize;
585+
// client_att.userTransports.push_back(std::move(descriptor));
586+
// }
587+
// if (server_list.has_kind<LOCATOR_KIND_TCPv6>() &&
588+
// !has_user_transport<fastdds::rtps::TCPv6TransportDescriptor>(client_att))
589+
// {
590+
// // Extend builtin transports with the TCPv6 transport
591+
// auto descriptor = std::make_shared<fastdds::rtps::TCPv6TransportDescriptor>();
592+
// // Add automatic port
593+
// descriptor->add_listener_port(0);
594+
// descriptor->sendBufferSize = client_att.sendSocketBufferSize;
595+
// descriptor->receiveBufferSize = client_att.listenSocketBufferSize;
596+
// client_att.userTransports.push_back(std::move(descriptor));
597+
// }
598+
599+
// EPROSIMA_LOG_INFO(RTPS_DOMAIN, "Detected auto client-server environment variable."
600+
// << "Trying to create client with the default server setup: "
601+
// << client_att.builtin.discovery_config.m_DiscoveryServers);
602+
603+
// client_att.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol::CLIENT;
604+
// // RemoteServerAttributes already fill in above
605+
606+
// // Check if the client must become a super client
607+
// if (ros_super_client_env())
608+
// {
609+
// client_att.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol::SUPER_CLIENT;
610+
// }
611+
// }
612+
// else
613+
// {
614+
// // SUPER_CLIENT
615+
// client_att.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol::SUPER_CLIENT;
616+
617+
// // P2P transport. Similar to LARGE_DATA, but with UDPv4 unicast
618+
// client_att.useBuiltinTransports = false;
619+
// client_att.setup_transports(BuiltinTransports::P2P);
620+
621+
// // Ignore initialpeers
622+
// client_att.builtin.initialPeersList = LocatorList();
623+
624+
// eprosima::fastdds::rtps::PortParameters port_params;
625+
626+
// auto domain_port = port_params.get_discovery_server_port(domain_id);
627+
628+
// // Add user traffic TCP
629+
// eprosima::fastdds::rtps::Locator_t locator_tcp;
630+
// locator_tcp.kind = LOCATOR_KIND_TCPv4;
631+
632+
// IPLocator::setPhysicalPort(locator_tcp, 0);
633+
// IPLocator::setLogicalPort(locator_tcp, 0);
634+
// // Initialize to the wan interface
635+
// IPLocator::setIPv4(locator_tcp, "0.0.0.0");
636+
637+
// client_att.defaultUnicastLocatorList.push_back(locator_tcp);
638+
639+
// // Add remote DS based on port
640+
// eprosima::fastdds::rtps::Locator_t locator_udp;
641+
// locator_udp.kind = LOCATOR_KIND_UDPv4;
642+
643+
// locator_udp.port = domain_port;
644+
// IPLocator::setIPv4(locator_udp, 127, 0, 0, 1);
645+
646+
// // Point to the well known DS port in the corresponding domain
647+
// client_att.builtin.discovery_config.m_DiscoveryServers.push_back(locator_udp);
648+
649+
// // Load the 'service' profile for ROS2_EASY_MODE services if there is no existing profile yet
650+
// xmlparser::PublisherAttributes attr;
651+
// auto ret_if = xmlparser::XMLProfileManager::fillPublisherAttributes("service", attr, false);
652+
// if (ret_if == xmlparser::XMLP_ret::XML_ERROR)
653+
// {
654+
// // An XML_ERROR is returned if there is no existing profile for the given name
655+
// xmlparser::XMLProfileManager::loadXMLString(EASY_MODE_SERVICE_PROFILE, strlen(EASY_MODE_SERVICE_PROFILE));
656+
// EPROSIMA_LOG_INFO(RTPS_DOMAIN, "Loaded service profile for ROS2_EASY_MODE servers");
657+
// }
658+
// else
659+
// {
660+
// // There is already a profile with the given name. Do not overwrite it
661+
// EPROSIMA_LOG_WARNING(RTPS_DOMAIN, "An XML profile for 'service' was found. When using ROS2_EASY_MODE, please ensure"
662+
// " the max_blocking_time is configured with a value higher than the default.");
663+
// }
664+
665+
// SystemCommandBuilder sys_command;
666+
// int res = sys_command.executable(FAST_DDS_DEFAULT_CLI_SCRIPT_NAME)
667+
// .verb(FAST_DDS_DEFAULT_CLI_DISCOVERY_VERB)
668+
// .verb(FAST_DDS_DEFAULT_CLI_AUTO_VERB)
669+
// .arg("-d")
670+
// .value(std::to_string(domain_id))
671+
// .value(easy_mode_env_value + ":" + std::to_string(domain_id))
672+
// .build_and_call();
673+
// #ifndef _WIN32
674+
// // Adecuate Python subprocess return
675+
// res = WEXITSTATUS(res);
676+
// #endif // _WIN32
677+
678+
// if (res != SystemCommandBuilder::SystemCommandResult::SUCCESS)
679+
// {
680+
// if (res == SystemCommandBuilder::SystemCommandResult::BAD_PARAM)
681+
// {
682+
// EPROSIMA_LOG_ERROR("DOMAIN", "ROS2_EASY_MODE IP connection conflicts with a previous one.");
683+
// }
684+
// else
685+
// {
686+
// EPROSIMA_LOG_ERROR(DOMAIN, "Auto discovery server client setup. Unable to spawn daemon.");
687+
// }
688+
// return nullptr;
689+
// }
690+
// }
691+
692+
// RTPSParticipant* part = createParticipant(domain_id, enabled, client_att, listen);
693+
// if (nullptr != part)
694+
// {
695+
// // Client successfully created
696+
// EPROSIMA_LOG_INFO(RTPS_DOMAIN, "Auto default server-client setup. Default client created.");
697+
// part->mp_impl->client_override(true);
698+
// return part;
699+
// }
700+
701+
// // Unable to create auto server-client default participants
702+
// EPROSIMA_LOG_ERROR(RTPS_DOMAIN, "Auto default server-client setup. Unable to create the client.");
703+
// return nullptr;
704+
// }
705+
706+
bool RTPSDomainImpl::client_server_environment_attributes_override(
522707
uint32_t domain_id,
523-
bool enabled,
524-
const RTPSParticipantAttributes& att,
525-
RTPSParticipantListener* listen)
708+
RTPSParticipantAttributes& att)
526709
{
527710
// Check the specified discovery protocol: if other than simple it has priority over ros environment variable
528711
if (att.builtin.discovery_config.discoveryProtocol != DiscoveryProtocol::SIMPLE)
529712
{
530713
EPROSIMA_LOG_INFO(RTPS_DOMAIN, "Detected non simple discovery protocol attributes."
531714
<< " Ignoring auto default client-server setup.");
532-
return nullptr;
715+
return false;
533716
}
534717

535718
// We only make the attributes copy when we are sure is worth
@@ -547,7 +730,7 @@ RTPSParticipant* RTPSDomainImpl::clientServerEnvironmentCreationOverride(
547730
{
548731
// It's not an error, the environment variable may not be set. Any issue with environment
549732
// variable syntax is EPROSIMA_LOG_ERROR already
550-
return nullptr;
733+
return false;
551734
}
552735

553736
// Check if some address requires the UDPv6, TCPv4 or TCPv6 transport
@@ -672,22 +855,21 @@ RTPSParticipant* RTPSDomainImpl::clientServerEnvironmentCreationOverride(
672855
{
673856
EPROSIMA_LOG_ERROR(DOMAIN, "Auto discovery server client setup. Unable to spawn daemon.");
674857
}
675-
return nullptr;
858+
return false;
676859
}
677860
}
678861

679-
RTPSParticipant* part = createParticipant(domain_id, enabled, client_att, listen);
680-
if (nullptr != part)
681-
{
682-
// Client successfully created
683-
EPROSIMA_LOG_INFO(RTPS_DOMAIN, "Auto default server-client setup. Default client created.");
684-
part->mp_impl->client_override(true);
685-
return part;
686-
}
687-
688-
// Unable to create auto server-client default participants
689-
EPROSIMA_LOG_ERROR(RTPS_DOMAIN, "Auto default server-client setup. Unable to create the client.");
690-
return nullptr;
862+
// RTPSParticipant* part = createParticipant(domain_id, enabled, client_att, listen);
863+
// if (nullptr != part)
864+
// {
865+
// // Client successfully created
866+
// EPROSIMA_LOG_INFO(RTPS_DOMAIN, "Auto default server-client setup. Default client created.");
867+
// part->mp_impl->client_override(true);
868+
// return part;
869+
// }
870+
EPROSIMA_LOG_INFO(RTPS_DOMAIN, "TODO.");
871+
att = client_att;
872+
return true;
691873
}
692874

693875
uint32_t RTPSDomainImpl::getNewId()

src/cpp/rtps/RTPSDomainImpl.hpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,26 @@ class RTPSDomainImpl
114114
static bool removeRTPSParticipant(
115115
RTPSParticipant* p);
116116

117-
/**
118-
* Creates a RTPSParticipant as default server or client if ROS_MASTER_URI environment variable is set.
119-
* @param domain_id DDS domain associated
120-
* @param enabled True if the RTPSParticipant should be enabled on creation. False if it will be enabled later with RTPSParticipant::enable()
121-
* @param attrs RTPSParticipant Attributes.
122-
* @param listen Pointer to the ParticipantListener.
123-
* @return Pointer to the RTPSParticipant.
124-
*
125-
* \warning The returned pointer is invalidated after a call to removeRTPSParticipant() or stopAll(),
126-
* so its use may result in undefined behaviour.
127-
*/
128-
static RTPSParticipant* clientServerEnvironmentCreationOverride(
117+
// /**
118+
// * Creates a RTPSParticipant as default server or client if ROS_MASTER_URI environment variable is set.
119+
// * @param domain_id DDS domain associated
120+
// * @param enabled True if the RTPSParticipant should be enabled on creation. False if it will be enabled later with RTPSParticipant::enable()
121+
// * @param attrs RTPSParticipant Attributes.
122+
// * @param listen Pointer to the ParticipantListener.
123+
// * @return Pointer to the RTPSParticipant.
124+
// *
125+
// * \warning The returned pointer is invalidated after a call to removeRTPSParticipant() or stopAll(),
126+
// * so its use may result in undefined behaviour.
127+
// */
128+
// static RTPSParticipant* clientServerEnvironmentCreationOverride(
129+
// uint32_t domain_id,
130+
// bool enabled,
131+
// const RTPSParticipantAttributes& attrs,
132+
// RTPSParticipantListener* listen /*= nullptr*/);
133+
134+
static bool client_server_environment_attributes_override(
129135
uint32_t domain_id,
130-
bool enabled,
131-
const RTPSParticipantAttributes& attrs,
132-
RTPSParticipantListener* listen /*= nullptr*/);
136+
RTPSParticipantAttributes& attrs);
133137

134138
/**
135139
* Create a RTPSWriter in a participant.

0 commit comments

Comments
 (0)