Skip to content

Commit 670b58f

Browse files
ORION compatible
Signed-off-by: Eugenio Collado <eugeniocollado@eprosima.com>
1 parent 178f99e commit 670b58f

4 files changed

Lines changed: 65 additions & 69 deletions

File tree

ddsenabler/include/ddsenabler/dds_enabler_runner.hpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,27 @@
3838

3939
namespace eprosima {
4040
namespace ddsenabler {
41-
41+
42+
struct ddsCallbacks
43+
{
44+
participants::DdsNotification data_callback;
45+
participants::ServiceReplyNotification reply_callback;
46+
participants::ServiceRequestNotification request_callback;
47+
participants::DdsTypeNotification type_callback;
48+
participants::DdsTopicNotification topic_callback;
49+
participants::DdsTypeRequest type_req_callback;
50+
participants::DdsTopicRequest topic_req_callback;
51+
participants::DdsLogFunc log_callback;
52+
};
53+
4254
bool create_dds_enabler(
4355
const char* ddsEnablerConfigFile,
44-
participants::DdsNotification data_callback,
45-
participants::ServiceReplyNotification reply_callback,
46-
participants::ServiceRequestNotification request_callback,
47-
participants::DdsTypeNotification type_callback,
48-
participants::DdsTopicNotification topic_callback,
49-
participants::DdsTypeRequest type_req_callback,
50-
participants::DdsTopicRequest topic_req_callback,
51-
participants::DdsLogFunc log_callback,
56+
ddsCallbacks& callbacks,
5257
std::unique_ptr<DDSEnabler>& enabler);
5358

5459
bool create_dds_enabler(
5560
yaml::EnablerConfiguration configuration,
56-
participants::DdsNotification data_callback,
57-
participants::ServiceReplyNotification reply_callback,
58-
participants::ServiceRequestNotification request_callback,
59-
participants::DdsTypeNotification type_callback,
60-
participants::DdsTopicNotification topic_callback,
61-
participants::DdsTypeRequest type_req_callback,
62-
participants::DdsTopicRequest topic_req_callback,
63-
participants::DdsLogFunc log_callback,
61+
ddsCallbacks& callbacks,
6462
std::unique_ptr<DDSEnabler>& enabler);
6563

6664
} /* namespace ddsenabler */

ddsenabler/src/cpp/dds_enabler_runner.cpp

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,7 @@ namespace ddsenabler {
3434

3535
bool create_dds_enabler(
3636
const char* ddsEnablerConfigFile,
37-
participants::DdsNotification data_callback,
38-
participants::ServiceReplyNotification reply_callback,
39-
participants::ServiceRequestNotification request_callback,
40-
participants::DdsTypeNotification type_callback,
41-
participants::DdsTopicNotification topic_callback,
42-
participants::DdsTypeRequest type_req_callback,
43-
participants::DdsTopicRequest topic_req_callback,
44-
participants::DdsLogFunc log_callback,
37+
ddsCallbacks& callbacks,
4538
std::unique_ptr<DDSEnabler>& enabler)
4639
{
4740
std::string dds_enabler_config_file = "";
@@ -54,17 +47,10 @@ bool create_dds_enabler(
5447

5548
bool ret = create_dds_enabler(
5649
configuration,
57-
data_callback,
58-
reply_callback,
59-
request_callback,
60-
type_callback,
61-
topic_callback,
62-
type_req_callback,
63-
topic_req_callback,
64-
log_callback,
50+
callbacks,
6551
enabler);
6652

67-
53+
6854
if(ret)
6955
{
7056
enabler->set_file_watcher(dds_enabler_config_file);
@@ -75,14 +61,7 @@ bool create_dds_enabler(
7561

7662
bool create_dds_enabler(
7763
yaml::EnablerConfiguration configuration,
78-
participants::DdsNotification data_callback,
79-
participants::ServiceReplyNotification reply_callback,
80-
participants::ServiceRequestNotification request_callback,
81-
participants::DdsTypeNotification type_callback,
82-
participants::DdsTopicNotification topic_callback,
83-
participants::DdsTypeRequest type_req_callback,
84-
participants::DdsTopicRequest topic_req_callback,
85-
participants::DdsLogFunc log_callback,
64+
ddsCallbacks& callbacks,
8665
std::unique_ptr<DDSEnabler>& enabler)
8766
{
8867
// Encapsulating execution in block to erase all memory correctly before closing process
@@ -97,7 +76,7 @@ bool create_dds_enabler(
9776
}
9877

9978
// Logging
100-
if(log_callback != nullptr){
79+
if(callbacks.log_callback != nullptr){
10180
// Disable stdout always
10281
configuration.ddspipe_configuration.log_configuration.stdout_enable = false;
10382
const auto log_configuration = configuration.ddspipe_configuration.log_configuration;
@@ -107,7 +86,7 @@ bool create_dds_enabler(
10786

10887
// DDS Enabler Log Consumer
10988
auto* log_consumer = new eprosima::ddsenabler::participants::DDSEnablerLogConsumer(&log_configuration);
110-
log_consumer->set_log_callback(log_callback);
89+
log_consumer->set_log_callback(callbacks.log_callback);
11190

11291
eprosima::utils::Log::RegisterConsumer(
11392
std::unique_ptr<eprosima::ddsenabler::participants::DDSEnablerLogConsumer>(log_consumer));
@@ -131,13 +110,13 @@ bool create_dds_enabler(
131110
enabler.reset(new DDSEnabler(configuration, close_handler));
132111

133112
// TODO: avoid setting callback after having created "enabled" enabler (e.g. pass and set in construction)
134-
enabler->set_data_callback(data_callback);
135-
enabler->set_reply_callback(reply_callback);
136-
enabler->set_request_callback(request_callback);
137-
enabler->set_type_callback(type_callback);
138-
enabler->set_topic_callback(topic_callback);
139-
enabler->set_type_request_callback(type_req_callback);
140-
enabler->set_topic_request_callback(topic_req_callback);
113+
enabler->set_data_callback(callbacks.data_callback);
114+
enabler->set_reply_callback(callbacks.reply_callback);
115+
enabler->set_request_callback(callbacks.request_callback);
116+
enabler->set_type_callback(callbacks.type_callback);
117+
enabler->set_topic_callback(callbacks.topic_callback);
118+
enabler->set_type_request_callback(callbacks.type_req_callback);
119+
enabler->set_topic_request_callback(callbacks.topic_req_callback);
141120

142121
EPROSIMA_LOG_INFO(DDSENABLER_EXECUTION,
143122
"DDS Enabler running.");

ddsenabler_participants/include/ddsenabler_participants/CBWriter.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
#pragma once
2020

21-
#include <nlohmann/json.hpp>
22-
2321
#include <fastdds/dds/xtypes/dynamic_types/DynamicType.hpp>
2422
#include <fastdds/dds/xtypes/dynamic_types/DynamicData.hpp>
2523

@@ -108,7 +106,7 @@ class CBWriter
108106
const CBMessage& msg,
109107
const fastdds::dds::DynamicType::_ref_type& dyn_type) noexcept;
110108

111-
nlohmann::json prepare_json_data(
109+
std::shared_ptr<void> prepare_json_data(
112110
const CBMessage& msg,
113111
const fastdds::dds::DynamicType::_ref_type& dyn_type);
114112

ddsenabler_participants/src/cpp/CBWriter.cpp

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* @file CBWriter.cpp
1717
*/
1818

19+
#include <nlohmann/json.hpp>
20+
1921
#include <fastdds/dds/xtypes/dynamic_types/DynamicDataFactory.hpp>
2022
#include <fastdds/dds/xtypes/dynamic_types/DynamicPubSubType.hpp>
2123
#include <fastdds/dds/xtypes/utils.hpp>
@@ -117,14 +119,21 @@ void CBWriter::write_data(
117119
const CBMessage& msg,
118120
const fastdds::dds::DynamicType::_ref_type& dyn_type)
119121
{
120-
nlohmann::json json_output = prepare_json_data(msg, dyn_type);
121-
122+
std::shared_ptr<void> json_ptr = prepare_json_data(msg, dyn_type);
123+
if (nullptr == json_ptr)
124+
{
125+
EPROSIMA_LOG_ERROR(DDSENABLER_CB_WRITER,
126+
"Not able to generate JSON data for topic " << msg.topic.topic_name() << ".");
127+
return;
128+
}
129+
std::shared_ptr<nlohmann::json> json_output = std::static_pointer_cast<nlohmann::json>(json_ptr);
130+
122131
//STORE DATA
123132
if (data_callback_)
124133
{
125134
data_callback_(
126135
msg.topic.topic_name().c_str(),
127-
json_output.dump(4).c_str(),
136+
json_output->dump(4).c_str(),
128137
msg.publish_time.to_ns()
129138
);
130139
}
@@ -135,48 +144,60 @@ void CBWriter::write_reply(
135144
const fastdds::dds::DynamicType::_ref_type& dyn_type,
136145
const uint64_t request_id)
137146
{
138-
nlohmann::json json_output = prepare_json_data(msg, dyn_type);
147+
std::shared_ptr<void> json_ptr = prepare_json_data(msg, dyn_type);
148+
if (nullptr == json_ptr)
149+
{
150+
EPROSIMA_LOG_ERROR(DDSENABLER_CB_WRITER,
151+
"Not able to generate JSON data for topic " << msg.topic.topic_name() << ".");
152+
return;
153+
}
154+
std::shared_ptr<nlohmann::json> json_output = std::static_pointer_cast<nlohmann::json>(json_ptr);
139155

140156
// Get the service name
141157
std::string service_name = get_service_name(msg.topic.topic_name());
142-
158+
143159
//STORE DATA
144160
if (reply_callback_)
145161
{
146162
reply_callback_(
147163
service_name.c_str(),
148-
json_output.dump(4).c_str(),
164+
json_output->dump(4).c_str(),
149165
request_id,
150166
msg.publish_time.to_ns()
151167
);
152168
}
153-
154169
}
155170

156171
void CBWriter::write_request(
157172
const CBMessage& msg,
158173
const fastdds::dds::DynamicType::_ref_type& dyn_type,
159174
const uint64_t request_id)
160175
{
161-
nlohmann::json json_output = prepare_json_data(msg, dyn_type);
176+
std::shared_ptr<void> json_ptr = prepare_json_data(msg, dyn_type);
177+
if (nullptr == json_ptr)
178+
{
179+
EPROSIMA_LOG_ERROR(DDSENABLER_CB_WRITER,
180+
"Not able to generate JSON data for topic " << msg.topic.topic_name() << ".");
181+
return;
182+
}
183+
std::shared_ptr<nlohmann::json> json_output = std::static_pointer_cast<nlohmann::json>(json_ptr);
162184

163185
// Get the service name
164186
std::string service_name = get_service_name(msg.topic.topic_name());
165-
187+
166188
//STORE DATA
167189
if (request_callback_)
168190
{
169191
request_callback_(
170192
service_name.c_str(),
171-
json_output.dump(4).c_str(),
193+
json_output->dump(4).c_str(),
172194
request_id,
173195
msg.publish_time.to_ns()
174196
);
175197
}
176-
177198
}
178199

179-
nlohmann::json CBWriter::prepare_json_data(
200+
std::shared_ptr<void> CBWriter::prepare_json_data(
180201
const CBMessage& msg,
181202
const fastdds::dds::DynamicType::_ref_type& dyn_type)
182203
{
@@ -192,7 +213,7 @@ nlohmann::json CBWriter::prepare_json_data(
192213
{
193214
EPROSIMA_LOG_ERROR(DDSENABLER_CB_WRITER,
194215
"Not able to get DynamicData from topic " << msg.topic.topic_name() << ".");
195-
return {};
216+
return nullptr;
196217
}
197218

198219
std::stringstream ss_dyn_data;
@@ -202,7 +223,7 @@ nlohmann::json CBWriter::prepare_json_data(
202223
{
203224
EPROSIMA_LOG_ERROR(DDSENABLER_CB_WRITER,
204225
"Not able to serialize data of topic " << msg.topic.topic_name() << " into JSON format.");
205-
return {};
226+
return nullptr;
206227
}
207228

208229
// Create the base JSON structure
@@ -229,7 +250,7 @@ nlohmann::json CBWriter::prepare_json_data(
229250
"Not able to generate JSON data for topic " << msg.topic.topic_name() << ".");
230251
}
231252

232-
return json_output;
253+
return std::make_shared<nlohmann::json>(json_output);
233254
}
234255

235256
fastdds::dds::DynamicData::_ref_type CBWriter::get_dynamic_data_(

0 commit comments

Comments
 (0)