This repository was archived by the owner on Oct 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathrmw_node.cpp
More file actions
297 lines (269 loc) · 9.37 KB
/
rmw_node.cpp
File metadata and controls
297 lines (269 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2014-2015 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <ccpp_dds_dcps.h>
#include <dds_dcps.h>
#include "rmw/allocators.h"
#include "rmw/error_handling.h"
#include "rmw/impl/cpp/macros.hpp"
#include "rmw/rmw.h"
#include "rmw/types.h"
#include "identifier.hpp"
#include "types.hpp"
// The extern "C" here enforces that overloading is not used.
extern "C"
{
rmw_node_t *
rmw_create_node(const char * name, size_t domain_id)
{
if (!name) {
RMW_SET_ERROR_MSG("name is null");
return nullptr;
}
DDS::DomainParticipantFactory_var dp_factory = DDS::DomainParticipantFactory::get_instance();
if (!dp_factory) {
RMW_SET_ERROR_MSG("failed to get domain participant factory");
return nullptr;
}
DDS::DomainId_t domain = static_cast<DDS::DomainId_t>(domain_id);
DDS::DomainParticipant * participant = nullptr;
// Make sure that the OSPL_URI is set, otherwise node creation will fail.
char * ospl_uri = nullptr;
const char * ospl_uri_env = "OSPL_URI";
#ifndef _WIN32
ospl_uri = getenv(ospl_uri_env);
#else
size_t ospl_uri_size;
_dupenv_s(&ospl_uri, &ospl_uri_size, ospl_uri_env);
#endif
if (!ospl_uri) {
RMW_SET_ERROR_MSG("OSPL_URI not set");
return nullptr;
} else {
#ifdef _WIN32
free(ospl_uri);
#endif
}
// Ensure the ROS_DOMAIN_ID env variable is set, otherwise parsing of the config may fail.
// Also make sure the it is set to the domain_id passed in, otherwise it will fail.
// But first backup the current ROS_DOMAIN_ID.
char * ros_domain_id = nullptr;
const char * env_var = "ROS_DOMAIN_ID";
#ifndef _WIN32
ros_domain_id = getenv(env_var);
#else
size_t ros_domain_id_size;
_dupenv_s(&ros_domain_id, &ros_domain_id_size, env_var);
#endif
// On Windows, setting the ROS_DOMAIN_ID does not fix the problem, so error early.
#ifdef _WIN32
if (!ros_domain_id) {
RMW_SET_ERROR_MSG("environment variable ROS_DOMAIN_ID is not set");
fprintf(stderr, "[rmw_opensplice_cpp]: error: %s\n", rmw_get_error_string_safe());
return nullptr;
}
#endif
// Set the ROS_DOMAIN_ID explicitly (if not Windows).
#ifndef _WIN32
auto domain_id_as_string = std::to_string(domain_id);
int ret = 0;
ret = setenv(env_var, domain_id_as_string.c_str(), 1);
if (0 != ret) {
RMW_SET_ERROR_MSG("failed to set the ROS_DOMAIN_ID");
return nullptr;
}
#endif
participant = dp_factory->create_participant(
domain, PARTICIPANT_QOS_DEFAULT, NULL, DDS::STATUS_MASK_NONE);
if (!participant) {
RMW_SET_ERROR_MSG("failed to create domain participant");
return NULL;
}
// Restore the ROS_DOMAIN_ID if necessary (and not Windows).
#ifndef _WIN32
if (ros_domain_id) {
ret = setenv(env_var, ros_domain_id, 1);
if (0 != ret) {
RMW_SET_ERROR_MSG("failed to reset the ROS_DOMAIN_ID");
return nullptr;
}
} else {
// Otherwise unset it again.
ret = unsetenv(env_var);
if (0 != ret) {
RMW_SET_ERROR_MSG("failed to unset the ROS_DOMAIN_ID");
return nullptr;
}
}
#endif
rmw_node_t * node = nullptr;
OpenSpliceStaticNodeInfo * node_info = nullptr;
CustomPublisherListener * publisher_listener = nullptr;
CustomSubscriberListener * subscriber_listener = nullptr;
void * buf = nullptr;
DDS::DataReader * data_reader = nullptr;
DDS::PublicationBuiltinTopicDataDataReader * builtin_publication_datareader = nullptr;
DDS::SubscriptionBuiltinTopicDataDataReader * builtin_subscription_datareader = nullptr;
DDS::Subscriber * builtin_subscriber = participant->get_builtin_subscriber();
if (!builtin_subscriber) {
RMW_SET_ERROR_MSG("builtin subscriber handle is null");
goto fail;
}
// setup publisher listener
data_reader = builtin_subscriber->lookup_datareader("DCPSPublication");
builtin_publication_datareader =
DDS::PublicationBuiltinTopicDataDataReader::_narrow(data_reader);
if (!builtin_publication_datareader) {
RMW_SET_ERROR_MSG("builtin publication datareader handle is null");
goto fail;
}
buf = rmw_allocate(sizeof(CustomPublisherListener));
if (!buf) {
RMW_SET_ERROR_MSG("failed to allocate memory");
goto fail;
}
RMW_TRY_PLACEMENT_NEW(publisher_listener, buf, goto fail, CustomPublisherListener)
buf = nullptr;
builtin_publication_datareader->set_listener(publisher_listener, DDS::DATA_AVAILABLE_STATUS);
data_reader = builtin_subscriber->lookup_datareader("DCPSSubscription");
builtin_subscription_datareader =
DDS::SubscriptionBuiltinTopicDataDataReader::_narrow(data_reader);
if (!builtin_subscription_datareader) {
RMW_SET_ERROR_MSG("builtin subscription datareader handle is null");
goto fail;
}
// setup subscriber listener
buf = rmw_allocate(sizeof(CustomSubscriberListener));
if (!buf) {
RMW_SET_ERROR_MSG("failed to allocate memory");
goto fail;
}
RMW_TRY_PLACEMENT_NEW(subscriber_listener, buf, goto fail, CustomSubscriberListener)
buf = nullptr;
builtin_subscription_datareader->set_listener(subscriber_listener, DDS::DATA_AVAILABLE_STATUS);
node = rmw_node_allocate();
if (!node) {
RMW_SET_ERROR_MSG("failed to allocate rmw_node_t");
goto fail;
}
node->name = reinterpret_cast<const char *>(rmw_allocate(sizeof(char) * strlen(name) + 1));
if (!node->name) {
RMW_SET_ERROR_MSG("failed to allocate memory for node name");
goto fail;
}
memcpy(const_cast<char *>(node->name), name, strlen(name) + 1);
buf = rmw_allocate(sizeof(OpenSpliceStaticNodeInfo));
if (!buf) {
RMW_SET_ERROR_MSG("failed to allocate memory");
goto fail;
}
RMW_TRY_PLACEMENT_NEW(node_info, buf, goto fail, OpenSpliceStaticNodeInfo)
buf = nullptr;
node_info->participant = participant;
node_info->publisher_listener = publisher_listener;
node_info->subscriber_listener = subscriber_listener;
node->implementation_identifier = opensplice_cpp_identifier;
node->data = node_info;
return node;
fail:
if (participant) {
if (dp_factory->delete_participant(participant) != DDS::RETCODE_OK) {
std::stringstream ss;
ss << "leaking domain participant while handling failure at: " <<
__FILE__ << ":" << __LINE__ << '\n';
(std::cerr << ss.str()).flush();
}
}
if (publisher_listener) {
RMW_TRY_DESTRUCTOR_FROM_WITHIN_FAILURE(
publisher_listener->~CustomPublisherListener(), CustomPublisherListener)
rmw_free(publisher_listener);
}
if (subscriber_listener) {
RMW_TRY_DESTRUCTOR_FROM_WITHIN_FAILURE(
subscriber_listener->~CustomSubscriberListener(), CustomSubscriberListener)
rmw_free(subscriber_listener);
}
if (node_info) {
RMW_TRY_DESTRUCTOR_FROM_WITHIN_FAILURE(
node_info->~OpenSpliceStaticNodeInfo(), OpenSpliceStaticNodeInfo)
rmw_free(node_info);
}
if (buf) {
rmw_free(buf);
}
if (node) {
if (node->name) {
rmw_free(const_cast<char *>(node->name));
}
rmw_node_free(node);
}
return nullptr;
}
rmw_ret_t
rmw_destroy_node(rmw_node_t * node)
{
if (!node) {
RMW_SET_ERROR_MSG("received null pointer");
return RMW_RET_ERROR;
}
RMW_CHECK_TYPE_IDENTIFIERS_MATCH(
node handle,
node->implementation_identifier, opensplice_cpp_identifier,
return RMW_RET_ERROR)
DDS::DomainParticipantFactory_var dp_factory = DDS::DomainParticipantFactory::get_instance();
if (!dp_factory) {
RMW_SET_ERROR_MSG("failed to get domain participant factory");
return RMW_RET_ERROR;
}
auto node_info = static_cast<OpenSpliceStaticNodeInfo *>(node->data);
if (!node_info) {
RMW_SET_ERROR_MSG("node info handle is null");
return RMW_RET_ERROR;
}
auto participant = static_cast<DDS::DomainParticipant *>(node_info->participant);
if (!participant) {
RMW_SET_ERROR_MSG("participant handle is null");
return RMW_RET_ERROR;
}
auto result = RMW_RET_OK;
// This unregisters types and destroys topics which were shared between
// publishers and subscribers and could not be cleaned up in the delete functions.
if (participant->delete_contained_entities() != DDS::RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to delete contained entities of participant");
result = RMW_RET_ERROR;
}
if (dp_factory->delete_participant(participant) != DDS::RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to delete participant");
result = RMW_RET_ERROR;
}
if (node_info->publisher_listener) {
RMW_TRY_DESTRUCTOR_FROM_WITHIN_FAILURE(
node_info->publisher_listener->~CustomPublisherListener(), CustomPublisherListener)
rmw_free(node_info->publisher_listener);
node_info->publisher_listener = nullptr;
}
if (node_info->subscriber_listener) {
RMW_TRY_DESTRUCTOR_FROM_WITHIN_FAILURE(
node_info->subscriber_listener->~CustomSubscriberListener(), CustomSubscriberListener)
rmw_free(node_info->subscriber_listener);
node_info->subscriber_listener = nullptr;
}
rmw_free(node_info);
node->data = nullptr;
rmw_free(const_cast<char *>(node->name));
node->name = nullptr;
rmw_node_free(node);
return result;
}
} // extern "C"