Skip to content

Commit ac3b908

Browse files
authored
Load profiles based on topic names in rmw_fastrtps_dynamic_cpp (#497)
Signed-off-by: EduPonz <[email protected]>
1 parent b20809b commit ac3b908

File tree

4 files changed

+90
-16
lines changed

4 files changed

+90
-16
lines changed

rmw_fastrtps_dynamic_cpp/src/publisher.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
#include <string>
1717

18+
#include "fastrtps/xmlparser/XMLProfileManager.h"
19+
1820
#include "rcutils/error_handling.h"
1921

2022
#include "rmw/allocators.h"
@@ -42,6 +44,7 @@ using Domain = eprosima::fastrtps::Domain;
4244
using Participant = eprosima::fastrtps::Participant;
4345
using TopicDataType = eprosima::fastrtps::TopicDataType;
4446
using TypeSupportProxy = rmw_fastrtps_dynamic_cpp::TypeSupportProxy;
47+
using XMLProfileManager = eprosima::fastrtps::xmlparser::XMLProfileManager;
4548

4649
rmw_publisher_t *
4750
rmw_fastrtps_dynamic_cpp::create_publisher(
@@ -105,12 +108,15 @@ rmw_fastrtps_dynamic_cpp::create_publisher(
105108
return nullptr;
106109
}
107110

108-
CustomPublisherInfo * info = nullptr;
109-
rmw_publisher_t * rmw_publisher = nullptr;
111+
// If the user defined an XML file via env "FASTRTPS_DEFAULT_PROFILES_FILE", try to load
112+
// publisher which profile name matches with topic_name. If such profile does not exist,
113+
// then use the default attributes.
110114
eprosima::fastrtps::PublisherAttributes publisherParam;
115+
Domain::getDefaultPublisherAttributes(publisherParam); // Loads the XML file if not loaded
116+
XMLProfileManager::fillPublisherAttributes(topic_name, publisherParam, false);
111117

112-
// Load default XML profile.
113-
Domain::getDefaultPublisherAttributes(publisherParam);
118+
CustomPublisherInfo * info = nullptr;
119+
rmw_publisher_t * rmw_publisher = nullptr;
114120

115121
info = new (std::nothrow) CustomPublisherInfo();
116122
if (!info) {

rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,15 @@
4040
#include "type_support_common.hpp"
4141
#include "type_support_registry.hpp"
4242

43+
#include "fastrtps/xmlparser/XMLProfileManager.h"
44+
4345
using BaseTypeSupport = rmw_fastrtps_dynamic_cpp::BaseTypeSupport;
4446
using Domain = eprosima::fastrtps::Domain;
4547
using Participant = eprosima::fastrtps::Participant;
4648
using TopicDataType = eprosima::fastrtps::TopicDataType;
4749
using TypeSupportProxy = rmw_fastrtps_dynamic_cpp::TypeSupportProxy;
50+
using XMLProfileManager = eprosima::fastrtps::xmlparser::XMLProfileManager;
51+
using XMLP_ret = eprosima::fastrtps::xmlparser::XMLP_ret;
4852

4953
extern "C"
5054
{
@@ -118,6 +122,9 @@ rmw_create_client(
118122
eprosima::fastrtps::SubscriberAttributes subscriberParam;
119123
eprosima::fastrtps::PublisherAttributes publisherParam;
120124
rmw_client_t * rmw_client = nullptr;
125+
eprosima::fastrtps::fixed_string<255> sub_topic_name;
126+
eprosima::fastrtps::fixed_string<255> pub_topic_name;
127+
std::string topic_name_fallback;
121128

122129
info = new CustomClientInfo();
123130
info->participant_ = participant;
@@ -181,15 +188,41 @@ rmw_create_client(
181188
_register_type(participant, info->response_type_support_);
182189
}
183190

191+
// If FASTRTPS_DEFAULT_PROFILES_FILE defined, fill subscriber attributes with a subscriber profile
192+
// located based of topic name defined by _create_topic_name(). If no profile is found, a search
193+
// with profile_name "client" is attempted. Else, use the default attributes.
194+
topic_name_fallback = "client";
195+
sub_topic_name = _create_topic_name(
196+
qos_policies, ros_service_response_prefix, service_name, "Reply");
197+
Domain::getDefaultSubscriberAttributes(subscriberParam);
198+
199+
if (XMLProfileManager::fillSubscriberAttributes(
200+
sub_topic_name.to_string(), subscriberParam, false) != XMLP_ret::XML_OK)
201+
{
202+
XMLProfileManager::fillSubscriberAttributes(topic_name_fallback, subscriberParam, false);
203+
}
204+
184205
if (!participant_info->leave_middleware_default_qos) {
185206
subscriberParam.historyMemoryPolicy =
186207
eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
187208
}
188209

189210
subscriberParam.topic.topicKind = eprosima::fastrtps::rtps::NO_KEY;
190211
subscriberParam.topic.topicDataType = response_type_name;
191-
subscriberParam.topic.topicName = _create_topic_name(
192-
qos_policies, ros_service_response_prefix, service_name, "Reply");
212+
subscriberParam.topic.topicName = sub_topic_name;
213+
214+
// If FASTRTPS_DEFAULT_PROFILES_FILE defined, fill publisher attributes with a publisher profile
215+
// located based of topic name defined by _create_topic_name(). If no profile is found, a search
216+
// with profile_name "client" is attempted. Else, use the default attributes.
217+
pub_topic_name = _create_topic_name(
218+
qos_policies, ros_service_requester_prefix, service_name, "Request");
219+
Domain::getDefaultPublisherAttributes(publisherParam);
220+
221+
if (XMLProfileManager::fillPublisherAttributes(
222+
pub_topic_name.to_string(), publisherParam, false) != XMLP_ret::XML_OK)
223+
{
224+
XMLProfileManager::fillPublisherAttributes(topic_name_fallback, publisherParam, false);
225+
}
193226

194227
if (!participant_info->leave_middleware_default_qos) {
195228
publisherParam.historyMemoryPolicy =
@@ -203,8 +236,7 @@ rmw_create_client(
203236

204237
publisherParam.topic.topicKind = eprosima::fastrtps::rtps::NO_KEY;
205238
publisherParam.topic.topicDataType = request_type_name;
206-
publisherParam.topic.topicName = _create_topic_name(
207-
qos_policies, ros_service_requester_prefix, service_name, "Request");
239+
publisherParam.topic.topicName = pub_topic_name;
208240

209241
RCUTILS_LOG_DEBUG_NAMED(
210242
"rmw_fastrtps_dynamic_cpp",

rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@
5252
#include "type_support_common.hpp"
5353
#include "type_support_registry.hpp"
5454

55+
#include "fastrtps/xmlparser/XMLProfileManager.h"
56+
5557
using BaseTypeSupport = rmw_fastrtps_dynamic_cpp::BaseTypeSupport;
5658
using Domain = eprosima::fastrtps::Domain;
5759
using Participant = eprosima::fastrtps::Participant;
5860
using TopicDataType = eprosima::fastrtps::TopicDataType;
5961
using TypeSupportProxy = rmw_fastrtps_dynamic_cpp::TypeSupportProxy;
62+
using XMLProfileManager = eprosima::fastrtps::xmlparser::XMLProfileManager;
63+
using XMLP_ret = eprosima::fastrtps::xmlparser::XMLP_ret;
6064

6165
extern "C"
6266
{
@@ -130,6 +134,10 @@ rmw_create_service(
130134
eprosima::fastrtps::SubscriberAttributes subscriberParam;
131135
eprosima::fastrtps::PublisherAttributes publisherParam;
132136
rmw_service_t * rmw_service = nullptr;
137+
eprosima::fastrtps::fixed_string<255> sub_topic_name;
138+
eprosima::fastrtps::fixed_string<255> pub_topic_name;
139+
std::string topic_name_fallback;
140+
133141

134142
info = new CustomServiceInfo();
135143
info->participant_ = participant;
@@ -191,15 +199,40 @@ rmw_create_service(
191199
_register_type(participant, info->response_type_support_);
192200
}
193201

202+
// If FASTRTPS_DEFAULT_PROFILES_FILE defined, fill subscriber attributes with a subscriber profile
203+
// located based of topic name defined by _create_topic_name(). If no profile is found, a search
204+
// with profile_name "service" is attempted. Else, use the default attributes.
205+
topic_name_fallback = "service";
206+
sub_topic_name = _create_topic_name(
207+
qos_policies, ros_service_requester_prefix, service_name, "Request");
208+
Domain::getDefaultSubscriberAttributes(subscriberParam);
209+
210+
if (XMLProfileManager::fillSubscriberAttributes(
211+
sub_topic_name.to_string(), subscriberParam, false) != XMLP_ret::XML_OK)
212+
{
213+
XMLProfileManager::fillSubscriberAttributes(topic_name_fallback, subscriberParam, false);
214+
}
215+
194216
if (!impl->leave_middleware_default_qos) {
195217
subscriberParam.historyMemoryPolicy =
196218
eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
197219
}
198-
199220
subscriberParam.topic.topicKind = eprosima::fastrtps::rtps::NO_KEY;
200221
subscriberParam.topic.topicDataType = request_type_name;
201-
subscriberParam.topic.topicName = _create_topic_name(
202-
qos_policies, ros_service_requester_prefix, service_name, "Request");
222+
subscriberParam.topic.topicName = sub_topic_name;
223+
224+
// If FASTRTPS_DEFAULT_PROFILES_FILE defined, fill publisher attributes with a publisher profile
225+
// located based of topic name defined by _create_topic_name(). If no profile is found, a search
226+
// with profile_name "service" is attempted. Else, use the default attributes.
227+
pub_topic_name = _create_topic_name(
228+
qos_policies, ros_service_response_prefix, service_name, "Reply");
229+
Domain::getDefaultPublisherAttributes(publisherParam);
230+
231+
if (XMLProfileManager::fillPublisherAttributes(
232+
pub_topic_name.to_string(), publisherParam, false) != XMLP_ret::XML_OK)
233+
{
234+
XMLProfileManager::fillPublisherAttributes(topic_name_fallback, publisherParam, false);
235+
}
203236

204237
if (!impl->leave_middleware_default_qos) {
205238
publisherParam.historyMemoryPolicy =
@@ -213,8 +246,7 @@ rmw_create_service(
213246

214247
publisherParam.topic.topicKind = eprosima::fastrtps::rtps::NO_KEY;
215248
publisherParam.topic.topicDataType = response_type_name;
216-
publisherParam.topic.topicName = _create_topic_name(
217-
qos_policies, ros_service_response_prefix, service_name, "Reply");
249+
publisherParam.topic.topicName = pub_topic_name;
218250

219251
RCUTILS_LOG_DEBUG_NAMED(
220252
"rmw_fastrtps_dynamic_cpp",

rmw_fastrtps_dynamic_cpp/src/subscription.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include "fastrtps/participant/Participant.h"
3535
#include "fastrtps/subscriber/Subscriber.h"
36+
#include "fastrtps/xmlparser/XMLProfileManager.h"
3637

3738
#include "rmw_fastrtps_dynamic_cpp/identifier.hpp"
3839
#include "rmw_fastrtps_dynamic_cpp/subscription.hpp"
@@ -45,7 +46,7 @@ using Domain = eprosima::fastrtps::Domain;
4546
using Participant = eprosima::fastrtps::Participant;
4647
using TopicDataType = eprosima::fastrtps::TopicDataType;
4748
using TypeSupportProxy = rmw_fastrtps_dynamic_cpp::TypeSupportProxy;
48-
49+
using XMLProfileManager = eprosima::fastrtps::xmlparser::XMLProfileManager;
4950

5051
namespace rmw_fastrtps_dynamic_cpp
5152
{
@@ -111,9 +112,12 @@ create_subscription(
111112
return nullptr;
112113
}
113114

114-
// Load default XML profile.
115+
// If the user defined an XML file via env "FASTRTPS_DEFAULT_PROFILES_FILE", try to load
116+
// subscriber which profile name matches with topic_name. If such profile does not exist, then use
117+
// the default attributes.
115118
eprosima::fastrtps::SubscriberAttributes subscriberParam;
116-
Domain::getDefaultSubscriberAttributes(subscriberParam);
119+
Domain::getDefaultSubscriberAttributes(subscriberParam); // Loads the XML file if not loaded
120+
XMLProfileManager::fillSubscriberAttributes(topic_name, subscriberParam, false);
117121

118122
CustomSubscriberInfo * info = new (std::nothrow) CustomSubscriberInfo();
119123
if (!info) {

0 commit comments

Comments
 (0)