Skip to content

Commit 106c03a

Browse files
mauropasseJLBuenoLopez
authored andcommitted
Ignore local endpoints (irobot-ros#131)
* Refs #18846: PoC ignore local endpoints: extend RCLCPP API Signed-off-by: JLBuenoLopez-eProsima <[email protected]> * Refs #18846: PoC ignore local endpoints: modify RCLCPP publish logic Signed-off-by: JLBuenoLopez-eProsima <[email protected]> --------- Signed-off-by: JLBuenoLopez-eProsima <[email protected]> Co-authored-by: JLBuenoLopez-eProsima <[email protected]>
1 parent 536e4cc commit 106c03a

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

rclcpp/include/rclcpp/publisher.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class Publisher : public PublisherBase
237237
// It's not possible to do that with an unique_ptr,
238238
// as do_intra_process_publish takes the ownership of the message.
239239
bool inter_process_publish_needed =
240-
get_subscription_count() > get_intra_process_subscription_count();
240+
get_non_local_subscription_count() > 0;
241241

242242
if (inter_process_publish_needed) {
243243
auto shared_msg =
@@ -306,7 +306,7 @@ class Publisher : public PublisherBase
306306
}
307307

308308
bool inter_process_publish_needed =
309-
get_subscription_count() > get_intra_process_subscription_count();
309+
get_non_local_subscription_count() > 0;
310310

311311
if (inter_process_publish_needed) {
312312
ROSMessageType ros_msg;

rclcpp/include/rclcpp/publisher_base.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ class PublisherBase : public std::enable_shared_from_this<PublisherBase>
133133
size_t
134134
get_subscription_count() const;
135135

136+
/// Get non local subscription count
137+
/** \return The number of non local subscriptions. */
138+
RCLCPP_PUBLIC
139+
size_t
140+
get_non_local_subscription_count() const;
141+
136142
/// Get intraprocess subscription count
137143
/** \return The number of intraprocess subscriptions. */
138144
RCLCPP_PUBLIC

rclcpp/src/rclcpp/publisher_base.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,31 @@ PublisherBase::get_subscription_count() const
253253
return inter_process_subscription_count;
254254
}
255255

256+
size_t
257+
PublisherBase::get_non_local_subscription_count() const
258+
{
259+
size_t inter_process_non_local_subscription_count = 0;
260+
261+
rcl_ret_t status = rcl_publisher_get_non_local_subscription_count(
262+
publisher_handle_.get(),
263+
&inter_process_non_local_subscription_count);
264+
265+
if (RCL_RET_PUBLISHER_INVALID == status) {
266+
rcl_reset_error(); /* next call will reset error message if not context */
267+
if (rcl_publisher_is_valid_except_context(publisher_handle_.get())) {
268+
rcl_context_t * context = rcl_publisher_get_context(publisher_handle_.get());
269+
if (nullptr != context && !rcl_context_is_valid(context)) {
270+
/* publisher is invalid due to context being shutdown */
271+
return 0;
272+
}
273+
}
274+
}
275+
if (RCL_RET_OK != status) {
276+
rclcpp::exceptions::throw_from_rcl_error(status, "failed to get get non local subscription count");
277+
}
278+
return inter_process_non_local_subscription_count;
279+
}
280+
256281
size_t
257282
PublisherBase::get_intra_process_subscription_count() const
258283
{

0 commit comments

Comments
 (0)