ROS 2 rclcpp + rcl - rolling  rolling-20536064
ROS 2 C++ Client Library with ROS Client Library
publisher_base.hpp
1 // Copyright 2014 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCLCPP__PUBLISHER_BASE_HPP_
16 #define RCLCPP__PUBLISHER_BASE_HPP_
17 
18 #include <rmw/error_handling.h>
19 #include <rmw/rmw.h>
20 
21 #include <chrono>
22 #include <functional>
23 #include <iostream>
24 #include <memory>
25 #include <sstream>
26 #include <string>
27 #include <unordered_map>
28 #include <utility>
29 #include <vector>
30 
31 #include "rcl/publisher.h"
32 
33 #include "rclcpp/macros.hpp"
34 #include "rclcpp/network_flow_endpoint.hpp"
35 #include "rclcpp/qos.hpp"
36 #include "rclcpp/event_handler.hpp"
37 #include "rclcpp/visibility_control.hpp"
38 #include "rcpputils/time.hpp"
39 
40 namespace rclcpp
41 {
42 
43 // Forward declaration is used for friend statement.
44 namespace node_interfaces
45 {
46 class NodeBaseInterface;
47 class NodeTopicsInterface;
48 } // namespace node_interfaces
49 
50 namespace experimental
51 {
56 class IntraProcessManager;
57 } // namespace experimental
58 
59 class PublisherBase : public std::enable_shared_from_this<PublisherBase>
60 {
61  friend ::rclcpp::node_interfaces::NodeTopicsInterface;
62 
63 public:
64  RCLCPP_SMART_PTR_DEFINITIONS(PublisherBase)
65 
66 
75  RCLCPP_PUBLIC
78  const std::string & topic,
79  const rosidl_message_type_support_t & type_support,
80  const rcl_publisher_options_t & publisher_options,
81  const PublisherEventCallbacks & event_callbacks,
82  bool use_default_callbacks);
83 
84  RCLCPP_PUBLIC
85  virtual ~PublisherBase();
86 
88 
97  RCLCPP_PUBLIC
98  static
99  bool
101 
103  RCLCPP_PUBLIC
104  void
105  bind_event_callbacks(const PublisherEventCallbacks & event_callbacks, bool use_default_callbacks);
106 
108 
109  RCLCPP_PUBLIC
110  const char *
111  get_topic_name() const;
112 
114 
115  RCLCPP_PUBLIC
116  size_t
117  get_queue_size() const;
118 
120 
121  RCLCPP_PUBLIC
122  const rmw_gid_t &
123  get_gid() const;
124 
126 
127  RCLCPP_PUBLIC
128  std::shared_ptr<rcl_publisher_t>
130 
132 
133  RCLCPP_PUBLIC
134  std::shared_ptr<const rcl_publisher_t>
135  get_publisher_handle() const;
136 
138 
139  RCLCPP_PUBLIC
140  const
141  std::unordered_map<rcl_publisher_event_type_t, std::shared_ptr<rclcpp::EventHandlerBase>> &
142  get_event_handlers() const;
143 
145 
146  RCLCPP_PUBLIC
147  size_t
148  get_subscription_count() const;
149 
151 
152  RCLCPP_PUBLIC
153  size_t
155 
157 
158  RCLCPP_PUBLIC
159  bool
161 
163 
170  RCLCPP_PUBLIC
171  RCUTILS_WARN_UNUSED
172  bool
173  assert_liveliness() const;
174 
176 
186  RCLCPP_PUBLIC
188  get_actual_qos() const;
189 
191 
195  RCLCPP_PUBLIC
196  bool
197  can_loan_messages() const;
198 
200 
205  RCLCPP_PUBLIC
206  bool
207  operator==(const rmw_gid_t & gid) const;
208 
210 
215  RCLCPP_PUBLIC
216  bool
217  operator==(const rmw_gid_t * gid) const;
218 
219  using IntraProcessManagerSharedPtr =
220  std::shared_ptr<rclcpp::experimental::IntraProcessManager>;
221 
223  RCLCPP_PUBLIC
224  void
226  uint64_t intra_process_publisher_id,
227  const IntraProcessManagerSharedPtr & ipm);
228 
230 
234  RCLCPP_PUBLIC
235  std::vector<rclcpp::NetworkFlowEndpoint>
237 
239 
245  RCLCPP_PUBLIC
246  size_t
248 
250 
271  template<typename DurationRepT = int64_t, typename DurationT = std::milli>
272  bool
274  std::chrono::duration<DurationRepT, DurationT> timeout =
275  std::chrono::duration<DurationRepT, DurationT>(-1)) const
276  {
277  rcl_duration_value_t rcl_timeout = rcpputils::convert_to_nanoseconds(timeout).count();
278 
279  rcl_ret_t ret = rcl_publisher_wait_for_all_acked(publisher_handle_.get(), rcl_timeout);
280  if (ret == RCL_RET_OK) {
281  return true;
282  } else if (ret == RCL_RET_TIMEOUT) {
283  return false;
284  } else {
285  rclcpp::exceptions::throw_from_rcl_error(ret);
286  }
287  }
288 
290 
315  RCLCPP_PUBLIC
316  void
318  const std::function<void(size_t)> & callback,
319  rcl_publisher_event_type_t event_type);
320 
322  RCLCPP_PUBLIC
323  void
325 
326 protected:
327  template<typename EventCallbackT>
328  void
329  add_event_handler(
330  const EventCallbackT & callback,
331  const rcl_publisher_event_type_t event_type)
332  {
333  auto handler = std::make_shared<EventHandler<EventCallbackT,
334  std::shared_ptr<rcl_publisher_t>>>(
335  callback,
337  publisher_handle_,
338  event_type);
339  event_handlers_.insert(std::make_pair(event_type, handler));
340  }
341 
342  RCLCPP_PUBLIC
343  void default_incompatible_qos_callback(QOSOfferedIncompatibleQoSInfo & info) const;
344 
345  RCLCPP_PUBLIC
346  void default_incompatible_type_callback(IncompatibleTypeInfo & info) const;
347 
348  std::shared_ptr<rcl_node_t> rcl_node_handle_;
349 
350  std::shared_ptr<rcl_publisher_t> publisher_handle_;
351 
352  std::unordered_map<rcl_publisher_event_type_t,
353  std::shared_ptr<rclcpp::EventHandlerBase>> event_handlers_;
354 
355  using IntraProcessManagerWeakPtr =
356  std::weak_ptr<rclcpp::experimental::IntraProcessManager>;
357  bool intra_process_is_enabled_;
358  IntraProcessManagerWeakPtr weak_ipm_;
359  uint64_t intra_process_publisher_id_;
360 
361  rmw_gid_t rmw_gid_;
362 
363  const rosidl_message_type_support_t type_support_;
364 
365  const PublisherEventCallbacks event_callbacks_;
366 };
367 
368 } // namespace rclcpp
369 
370 #endif // RCLCPP__PUBLISHER_BASE_HPP_
RCLCPP_PUBLIC const rmw_gid_t & get_gid() const
Get the global identifier for this publisher (used in rmw and by DDS).
RCLCPP_PUBLIC void set_on_new_qos_event_callback(const std::function< void(size_t)> &callback, rcl_publisher_event_type_t event_type)
Set a callback to be called when each new qos event instance occurs.
RCLCPP_PUBLIC std::shared_ptr< rcl_publisher_t > get_publisher_handle()
Get the rcl publisher handle.
RCLCPP_PUBLIC size_t get_intra_process_subscription_count() const
Get intraprocess subscription count.
RCLCPP_PUBLIC PublisherBase(rclcpp::node_interfaces::NodeBaseInterface *node_base, const std::string &topic, const rosidl_message_type_support_t &type_support, const rcl_publisher_options_t &publisher_options, const PublisherEventCallbacks &event_callbacks, bool use_default_callbacks)
Default constructor.
RCLCPP_PUBLIC void clear_on_new_qos_event_callback(rcl_publisher_event_type_t event_type)
Unset the callback registered for new qos events, if any.
RCLCPP_PUBLIC void bind_event_callbacks(const PublisherEventCallbacks &event_callbacks, bool use_default_callbacks)
Add event handlers for passed in event_callbacks.
RCLCPP_PUBLIC rclcpp::QoS get_actual_qos() const
Get the actual QoS settings, after the defaults have been determined.
RCLCPP_PUBLIC const char * get_topic_name() const
Get the topic that this publisher publishes on.
RCLCPP_PUBLIC void setup_intra_process(uint64_t intra_process_publisher_id, const IntraProcessManagerSharedPtr &ipm)
Implementation utility function used to setup intra process publishing after creation.
RCLCPP_PUBLIC bool operator==(const rmw_gid_t &gid) const
Compare this publisher to a gid.
RCLCPP_PUBLIC size_t get_queue_size() const
Get the queue size for this publisher.
RCLCPP_PUBLIC bool can_loan_messages() const
Check if publisher instance can loan messages.
RCLCPP_PUBLIC bool is_durability_transient_local() const
Get if durability is transient local.
RCLCPP_PUBLIC size_t get_subscription_count() const
Get subscription count.
RCLCPP_PUBLIC std::vector< rclcpp::NetworkFlowEndpoint > get_network_flow_endpoints() const
Get network flow endpoints.
bool wait_for_all_acked(std::chrono::duration< DurationRepT, DurationT > timeout=std::chrono::duration< DurationRepT, DurationT >(-1)) const
Wait until all published messages are acknowledged or until the specified timeout elapses.
RCLCPP_PUBLIC RCUTILS_WARN_UNUSED bool assert_liveliness() const
Manually assert that this Publisher is alive (for RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_TOPIC).
RCLCPP_PUBLIC const std::unordered_map< rcl_publisher_event_type_t, std::shared_ptr< rclcpp::EventHandlerBase > > & get_event_handlers() const
Get all the QoS event handlers associated with this publisher.
RCLCPP_PUBLIC size_t lowest_available_ipm_capacity() const
Return the lowest available capacity for all subscription buffers.
static RCLCPP_PUBLIC bool event_type_is_supported(const rcl_publisher_event_type_t event_type)
Check if a publisher event type is supported by the active RMW implementation.
Encapsulation of Quality of Service settings.
Definition: qos.hpp:114
Pure virtual interface class for the NodeBase part of the Node API.
enum rcl_publisher_event_type_e rcl_publisher_event_type_t
Enumeration of all of the publisher events that may fire.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_publisher_event_init(rcl_event_t *event, const rcl_publisher_t *publisher, const rcl_publisher_event_type_t event_type)
Initialize an rcl_event_t with a publisher.
Definition: event.c:43
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_publisher_wait_for_all_acked(const rcl_publisher_t *publisher, rcl_duration_value_t timeout)
Wait until all published message data is acknowledged or until the specified timeout elapses.
Definition: publisher.c:344
Options available for a rcl publisher.
Definition: publisher.h:44
Contains callbacks for various types of events a Publisher can receive from the middleware.
rcutils_duration_value_t rcl_duration_value_t
A duration of time, measured in nanoseconds.
Definition: time.h:48
#define RCL_RET_OK
Success return code.
Definition: types.h:27
#define RCL_RET_TIMEOUT
Timeout occurred return code.
Definition: types.h:31
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:24