This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathrmw_publisher.cpp
356 lines (324 loc) · 11.6 KB
/
rmw_publisher.cpp
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright 2014-2017 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 <string>
#include "rmw/allocators.h"
#include "rmw/error_handling.h"
#include "rmw/impl/cpp/macros.hpp"
#include "rmw/rmw.h"
#include "rmw/types.h"
#include "rmw_connext_shared_cpp/qos.hpp"
#include "rmw_connext_shared_cpp/types.hpp"
#include "rmw_connext_cpp/identifier.hpp"
#include "process_topic_and_service_names.hpp"
#include "type_support_common.hpp"
#include "rmw_connext_cpp/connext_static_publisher_info.hpp"
// Uncomment this to get extra console output about discovery.
// This affects code in this file, but there is a similar variable in:
// rmw_connext_shared_cpp/shared_functions.cpp
// #define DISCOVERY_DEBUG_LOGGING 1
extern "C"
{
rmw_publisher_t *
rmw_create_publisher(
const rmw_node_t * node,
const rosidl_message_type_support_t * type_supports,
const char * topic_name,
const rmw_qos_profile_t * qos_profile)
{
if (!node) {
RMW_SET_ERROR_MSG("node handle is null");
return NULL;
}
RMW_CHECK_TYPE_IDENTIFIERS_MATCH(
node handle,
node->implementation_identifier, rti_connext_identifier,
return NULL)
RMW_CONNEXT_EXTRACT_MESSAGE_TYPESUPPORT(type_supports, type_support)
if (!topic_name || strlen(topic_name) == 0) {
RMW_SET_ERROR_MSG("publisher topic is null or empty string");
return NULL;
}
if (!qos_profile) {
RMW_SET_ERROR_MSG("qos_profile is null");
return NULL;
}
auto node_info = static_cast<ConnextNodeInfo *>(node->data);
if (!node_info) {
RMW_SET_ERROR_MSG("node info handle is null");
return NULL;
}
auto participant = static_cast<DDSDomainParticipant *>(node_info->participant);
if (!participant) {
RMW_SET_ERROR_MSG("participant handle is null");
return NULL;
}
const message_type_support_callbacks_t * callbacks =
static_cast<const message_type_support_callbacks_t *>(type_support->data);
if (!callbacks) {
RMW_SET_ERROR_MSG("callbacks handle is null");
return NULL;
}
std::string type_name = _create_type_name(callbacks, "msg");
// Past this point, a failure results in unrolling code in the goto fail block.
bool registered;
DDS_DataWriterQos datawriter_qos;
DDS_PublisherQos publisher_qos;
DDS_ReturnCode_t status;
DDSPublisher * dds_publisher = nullptr;
DDSDataWriter * topic_writer = nullptr;
DDSTopic * topic = nullptr;
DDSTopicDescription * topic_description = nullptr;
void * buf = nullptr;
ConnextStaticPublisherInfo * publisher_info = nullptr;
rmw_publisher_t * publisher = nullptr;
std::string mangled_name = "";
char * partition_str = nullptr;
char * topic_str = nullptr;
// Begin initializing elements
publisher = rmw_publisher_allocate();
if (!publisher) {
RMW_SET_ERROR_MSG("failed to allocate publisher");
goto fail;
}
registered = callbacks->register_type(participant, type_name.c_str());
if (!registered) {
RMW_SET_ERROR_MSG("failed to register type");
goto fail;
}
status = participant->get_default_publisher_qos(publisher_qos);
if (status != DDS_RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to get default publisher qos");
goto fail;
}
// allocating memory for topic_str and partition_str
if (!_process_topic_name(
topic_name,
qos_profile->avoid_ros_namespace_conventions,
&topic_str,
&partition_str))
{
goto fail;
}
// we have to set the partition array to length 1
// and then set the partition_str in it
if (partition_str) {
if (strlen(partition_str) != 0) { // only set if not empty
publisher_qos.partition.name.ensure_length(1, 1);
// passing ownership to Connext
publisher_qos.partition.name[0] = partition_str;
} else {
DDS_String_free(partition_str);
}
partition_str = nullptr;
}
dds_publisher = participant->create_publisher(
publisher_qos, NULL, DDS_STATUS_MASK_NONE);
if (!dds_publisher) {
RMW_SET_ERROR_MSG("failed to create publisher");
goto fail;
}
topic_description = participant->lookup_topicdescription(topic_str);
if (!topic_description) {
DDS_TopicQos default_topic_qos;
status = participant->get_default_topic_qos(default_topic_qos);
if (status != DDS_RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to get default topic qos");
goto fail;
}
topic = participant->create_topic(
topic_str, type_name.c_str(),
default_topic_qos, NULL, DDS_STATUS_MASK_NONE);
if (!topic) {
RMW_SET_ERROR_MSG("failed to create topic");
goto fail;
}
} else {
DDS_Duration_t timeout = DDS_Duration_t::from_seconds(0);
topic = participant->find_topic(topic_str, timeout);
if (!topic) {
RMW_SET_ERROR_MSG("failed to find topic");
goto fail;
}
}
DDS_String_free(topic_str);
topic_str = nullptr;
if (!get_datawriter_qos(participant, *qos_profile, datawriter_qos)) {
// error string was set within the function
goto fail;
}
topic_writer = dds_publisher->create_datawriter(
topic, datawriter_qos, NULL, DDS_STATUS_MASK_NONE);
if (!topic_writer) {
RMW_SET_ERROR_MSG("failed to create datawriter");
goto fail;
}
// Allocate memory for the ConnextStaticPublisherInfo object.
buf = rmw_allocate(sizeof(ConnextStaticPublisherInfo));
if (!buf) {
RMW_SET_ERROR_MSG("failed to allocate memory");
goto fail;
}
// Use a placement new to construct the ConnextStaticPublisherInfo in the preallocated buffer.
RMW_TRY_PLACEMENT_NEW(publisher_info, buf, goto fail, ConnextStaticPublisherInfo, )
buf = nullptr; // Only free the publisher_info pointer; don't need the buf pointer anymore.
publisher_info->dds_publisher_ = dds_publisher;
publisher_info->topic_writer_ = topic_writer;
publisher_info->callbacks_ = callbacks;
publisher_info->publisher_gid.implementation_identifier = rti_connext_identifier;
static_assert(
sizeof(ConnextPublisherGID) <= RMW_GID_STORAGE_SIZE,
"RMW_GID_STORAGE_SIZE insufficient to store the rmw_connext_cpp GID implemenation."
);
// Zero the data memory.
memset(publisher_info->publisher_gid.data, 0, RMW_GID_STORAGE_SIZE);
{
auto publisher_gid =
reinterpret_cast<ConnextPublisherGID *>(publisher_info->publisher_gid.data);
publisher_gid->publication_handle = topic_writer->get_instance_handle();
}
publisher_info->publisher_gid.implementation_identifier = rti_connext_identifier;
publisher->implementation_identifier = rti_connext_identifier;
publisher->data = publisher_info;
publisher->topic_name = reinterpret_cast<const char *>(rmw_allocate(strlen(topic_name) + 1));
if (!publisher->topic_name) {
RMW_SET_ERROR_MSG("failed to allocate memory for node name");
goto fail;
}
memcpy(const_cast<char *>(publisher->topic_name), topic_name, strlen(topic_name) + 1);
if (!qos_profile->avoid_ros_namespace_conventions) {
mangled_name =
std::string(publisher_qos.partition.name[0]) +
"/" +
topic_writer->get_topic()->get_name();
} else {
mangled_name = topic_name;
}
node_info->publisher_listener->add_information(
dds_publisher->get_instance_handle(), mangled_name.c_str(), type_name, EntityType::Publisher);
node_info->publisher_listener->trigger_graph_guard_condition();
// TODO(karsten1987): replace this block with logging macros
#ifdef DISCOVERY_DEBUG_LOGGING
fprintf(stderr, "******* Creating Publisher Details: ********\n");
fprintf(stderr, "Publisher partition %s\n", publisher_qos.partition.name[0]);
fprintf(stderr, "Publisher topic %s\n", topic_writer->get_topic()->get_name());
fprintf(stderr, "Publisher address %p\n", static_cast<void *>(dds_publisher));
fprintf(stderr, "******\n");
#endif
return publisher;
fail:
if (partition_str) {
DDS_String_free(partition_str);
partition_str = nullptr;
}
if (topic_str) {
DDS_String_free(topic_str);
topic_str = nullptr;
}
if (publisher) {
rmw_publisher_free(publisher);
}
// Assumption: participant is valid.
if (dds_publisher) {
if (topic_writer) {
if (dds_publisher->delete_datawriter(topic_writer) != DDS_RETCODE_OK) {
std::stringstream ss;
ss << "leaking datawriter while handling failure at " <<
__FILE__ << ":" << __LINE__ << '\n';
(std::cerr << ss.str()).flush();
}
}
if (participant->delete_publisher(dds_publisher) != DDS_RETCODE_OK) {
std::stringstream ss;
ss << "leaking publisher while handling failure at " <<
__FILE__ << ":" << __LINE__ << '\n';
(std::cerr << ss.str()).flush();
}
}
if (publisher_info) {
RMW_TRY_DESTRUCTOR_FROM_WITHIN_FAILURE(
publisher_info->~ConnextStaticPublisherInfo(), ConnextStaticPublisherInfo)
rmw_free(publisher_info);
}
if (buf) {
rmw_free(buf);
}
return NULL;
}
rmw_ret_t
rmw_destroy_publisher(rmw_node_t * node, rmw_publisher_t * publisher)
{
if (!node) {
RMW_SET_ERROR_MSG("node handle is null");
return RMW_RET_ERROR;
}
RMW_CHECK_TYPE_IDENTIFIERS_MATCH(
node handle,
node->implementation_identifier, rti_connext_identifier,
return RMW_RET_ERROR)
if (!publisher) {
RMW_SET_ERROR_MSG("publisher handle is null");
return RMW_RET_ERROR;
}
RMW_CHECK_TYPE_IDENTIFIERS_MATCH(
publisher handle,
publisher->implementation_identifier, rti_connext_identifier,
return RMW_RET_ERROR)
auto node_info = static_cast<ConnextNodeInfo *>(node->data);
if (!node_info) {
RMW_SET_ERROR_MSG("node info handle is null");
return RMW_RET_ERROR;
}
auto participant = static_cast<DDSDomainParticipant *>(node_info->participant);
if (!participant) {
RMW_SET_ERROR_MSG("participant handle is null");
return RMW_RET_ERROR;
}
// TODO(wjwwood): need to figure out when to unregister types with the participant.
ConnextStaticPublisherInfo * publisher_info =
static_cast<ConnextStaticPublisherInfo *>(publisher->data);
if (publisher_info) {
node_info->publisher_listener->remove_information(
publisher_info->dds_publisher_->get_instance_handle(), EntityType::Publisher);
node_info->publisher_listener->trigger_graph_guard_condition();
DDSPublisher * dds_publisher = publisher_info->dds_publisher_;
if (dds_publisher) {
if (publisher_info->topic_writer_) {
if (dds_publisher->delete_datawriter(publisher_info->topic_writer_) != DDS_RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to delete datawriter");
return RMW_RET_ERROR;
}
publisher_info->topic_writer_ = nullptr;
}
if (participant->delete_publisher(dds_publisher) != DDS_RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to delete publisher");
return RMW_RET_ERROR;
}
publisher_info->dds_publisher_ = nullptr;
} else if (publisher_info->topic_writer_) {
RMW_SET_ERROR_MSG("cannot delete datawriter because the publisher is null");
return RMW_RET_ERROR;
}
RMW_TRY_DESTRUCTOR(
publisher_info->~ConnextStaticPublisherInfo(),
ConnextStaticPublisherInfo, return RMW_RET_ERROR)
rmw_free(publisher_info);
publisher->data = nullptr;
}
if (publisher->topic_name) {
rmw_free(const_cast<char *>(publisher->topic_name));
}
rmw_publisher_free(publisher);
return RMW_RET_OK;
}
} // extern "C"