rclcpp  master
C++ ROS Client Library API
subscription.hpp
Go to the documentation of this file.
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__SUBSCRIPTION_HPP_
16 #define RCLCPP__SUBSCRIPTION_HPP_
17 
18 #include <rmw/error_handling.h>
19 #include <rmw/rmw.h>
20 
21 #include <functional>
22 #include <iostream>
23 #include <memory>
24 #include <sstream>
25 #include <string>
26 
27 #include "rcl/error_handling.h"
28 #include "rcl/subscription.h"
29 
30 #include "rcl_interfaces/msg/intra_process_message.hpp"
31 
32 #include "rclcpp/exceptions.hpp"
33 #include "rclcpp/macros.hpp"
39 
40 namespace rclcpp
41 {
42 
43 namespace node_interfaces
44 {
45 class NodeTopicsInterface;
46 } // namespace node_interfaces
47 
51 {
52 public:
54 
55 
56 
64  std::shared_ptr<rcl_node_t> node_handle,
65  const rosidl_message_type_support_t & type_support_handle,
66  const std::string & topic_name,
67  const rcl_subscription_options_t & subscription_options);
68 
71  virtual ~SubscriptionBase();
72 
75  const char *
76  get_topic_name() const;
77 
81 
83  const rcl_subscription_t *
85 
87  virtual const rcl_subscription_t *
89 
91 
92  virtual std::shared_ptr<void>
93  create_message() = 0;
95 
99  virtual void
100  handle_message(std::shared_ptr<void> & message, const rmw_message_info_t & message_info) = 0;
101 
103 
104  virtual void
105  return_message(std::shared_ptr<void> & message) = 0;
106 
107  virtual void
109  rcl_interfaces::msg::IntraProcessMessage & ipm,
110  const rmw_message_info_t & message_info) = 0;
111 
112 protected:
116 
117 private:
119 };
120 
122 template<typename MessageT, typename Alloc = std::allocator<void>>
124 {
126 
127 public:
129  using MessageAlloc = typename MessageAllocTraits::allocator_type;
132 
134 
135 
136 
146  std::shared_ptr<rcl_node_t> node_handle,
147  const std::string & topic_name,
148  const rcl_subscription_options_t & subscription_options,
149  AnySubscriptionCallback<MessageT, Alloc> callback,
150  typename message_memory_strategy::MessageMemoryStrategy<MessageT, Alloc>::SharedPtr
151  memory_strategy = message_memory_strategy::MessageMemoryStrategy<MessageT,
152  Alloc>::create_default())
154  node_handle,
155  *rosidl_typesupport_cpp::get_message_type_support_handle<MessageT>(),
156  topic_name,
157  subscription_options),
158  any_callback_(callback),
159  message_memory_strategy_(memory_strategy),
160  get_intra_process_message_callback_(nullptr),
161  matches_any_intra_process_publishers_(nullptr)
162  {}
163 
165 
171  Alloc>::SharedPtr message_memory_strategy)
172  {
173  message_memory_strategy_ = message_memory_strategy;
174  }
176  {
177  /* The default message memory strategy provides a dynamically allocated message on each call to
178  * create_message, though alternative memory strategies that re-use a preallocated message may be
179  * used (see rclcpp/strategies/message_pool_memory_strategy.hpp).
180  */
181  return message_memory_strategy_->borrow_message();
182  }
183 
184  void handle_message(std::shared_ptr<void> & message, const rmw_message_info_t & message_info)
185  {
186  if (matches_any_intra_process_publishers_) {
187  if (matches_any_intra_process_publishers_(&message_info.publisher_gid)) {
188  // In this case, the message will be delivered via intra process and
189  // we should ignore this copy of the message.
190  return;
191  }
192  }
193  auto typed_message = std::static_pointer_cast<MessageT>(message);
194  any_callback_.dispatch(typed_message, message_info);
195  }
196 
198 
200  {
201  auto typed_message = std::static_pointer_cast<MessageT>(message);
202  message_memory_strategy_->return_message(typed_message);
203  }
204 
206  rcl_interfaces::msg::IntraProcessMessage & ipm,
207  const rmw_message_info_t & message_info)
208  {
209  if (!get_intra_process_message_callback_) {
210  // throw std::runtime_error(
211  // "handle_intra_process_message called before setup_intra_process");
212  // TODO(wjwwood): for now, this could mean that intra process was just not enabled.
213  // However, this can only really happen if this node has it disabled, but the other doesn't.
214  return;
215  }
216  MessageUniquePtr msg;
217  get_intra_process_message_callback_(
218  ipm.publisher_id,
219  ipm.message_sequence,
220  intra_process_subscription_id_,
221  msg);
222  if (!msg) {
223  // This either occurred because the publisher no longer exists or the
224  // message requested is no longer being stored.
225  // TODO(wjwwood): should we notify someone of this? log error, log warning?
226  return;
227  }
228  any_callback_.dispatch_intra_process(msg, message_info);
229  }
230 
231  using GetMessageCallbackType =
234 
237  uint64_t intra_process_subscription_id,
238  GetMessageCallbackType get_message_callback,
239  MatchesAnyPublishersCallbackType matches_any_publisher_callback,
240  const rcl_subscription_options_t & intra_process_options)
241  {
242  std::string intra_process_topic_name = std::string(get_topic_name()) + "/_intra";
245  node_handle_.get(),
247  intra_process_topic_name.c_str(),
248  &intra_process_options);
249  if (ret != RCL_RET_OK) {
250  if (ret == RCL_RET_TOPIC_NAME_INVALID) {
251  auto rcl_node_handle = node_handle_.get();
252  // this will throw on any validation problem
253  rcl_reset_error();
255  intra_process_topic_name,
256  rcl_node_get_name(rcl_node_handle),
257  rcl_node_get_namespace(rcl_node_handle));
258  }
259 
260  rclcpp::exceptions::throw_from_rcl_error(ret, "could not create intra process subscription");
261  }
262 
263  intra_process_subscription_id_ = intra_process_subscription_id;
264  get_intra_process_message_callback_ = get_message_callback;
265  matches_any_intra_process_publishers_ = matches_any_publisher_callback;
266  }
267 
269  const rcl_subscription_t *
271  {
272  if (!get_intra_process_message_callback_) {
273  return nullptr;
274  }
276  }
277 
278 private:
280 
283  message_memory_strategy_;
284 
285  GetMessageCallbackType get_intra_process_message_callback_;
286  MatchesAnyPublishersCallbackType matches_any_intra_process_publishers_;
287  uint64_t intra_process_subscription_id_;
288 };
289 
290 } // namespace rclcpp
291 
292 #endif // RCLCPP__SUBSCRIPTION_HPP_
std::shared_ptr< rcl_node_t > node_handle_
Definition: subscription.hpp:115
rcl_subscription_t intra_process_subscription_handle_
Definition: subscription.hpp:113
Subscription implementation, templated on the type of message this subscription receives.
Definition: subscription.hpp:123
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
std::function< void(uint64_t, uint64_t, uint64_t, MessageUniquePtr &)> GetMessageCallbackType
Definition: subscription.hpp:232
allocator::Deleter< MessageAlloc, MessageT > MessageDeleter
Definition: subscription.hpp:130
const char * get_topic_name() const
Get the topic that this subscription is subscribed on.
#define rcl_reset_error
rmw_ret_t rcl_ret_t
Definition: allocator_common.hpp:24
virtual std::shared_ptr< void > create_message()=0
Borrow a new message.
void set_message_memory_strategy(typename message_memory_strategy::MessageMemoryStrategy< MessageT, Alloc >::SharedPtr message_memory_strategy)
Support dynamically setting the message memory strategy.
Definition: subscription.hpp:169
virtual const rcl_subscription_t * get_intra_process_subscription_handle() const
rcl_ret_t rcl_subscription_init(rcl_subscription_t *subscription, const rcl_node_t *node, const rosidl_message_type_support_t *type_support, const char *topic_name, const rcl_subscription_options_t *options)
std::function< bool(const rmw_gid_t *)> MatchesAnyPublishersCallbackType
Definition: subscription.hpp:233
std::string expand_topic_or_service_name(const std::string &name, const std::string &node_name, const std::string &namespace_, bool is_service=false)
Expand a topic or service name and throw if it is not valid.
void return_message(std::shared_ptr< void > &message)
Return the loaned message.
Definition: subscription.hpp:199
#define RCL_RET_OK
Definition: any_subscription_callback.hpp:34
void throw_from_rcl_error(rcl_ret_t ret, const std::string &prefix="", const rcl_error_state_t *error_state=nullptr, void(*reset_error)()=rcl_reset_error)
Throw a C++ std::exception which was created based on an rcl error.
virtual void handle_intra_process_message(rcl_interfaces::msg::IntraProcessMessage &ipm, const rmw_message_info_t &message_info)=0
typename std::allocator_traits< Alloc >::template rebind_traits< T > AllocRebind
Definition: allocator_common.hpp:30
allocator::AllocRebind< MessageT, Alloc > MessageAllocTraits
Definition: subscription.hpp:128
const rosidl_message_type_support_t * get_intra_process_message_msg_type_support()
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
Definition: subscription.hpp:50
Pure virtual interface class for the NodeTopics part of the Node API.
Definition: node_topics_interface.hpp:38
std::shared_ptr< void > create_message()
Borrow a new message.
Definition: subscription.hpp:175
virtual void handle_message(std::shared_ptr< void > &message, const rmw_message_info_t &message_info)=0
Check if we need to handle the message, and execute the callback if we do.
T static_pointer_cast(T... args)
rcl_subscription_t * get_subscription_handle()
#define RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(...)
Definition: macros.hpp:51
Default allocation strategy for messages received by subscriptions.
Definition: message_memory_strategy.hpp:33
void setup_intra_process(uint64_t intra_process_subscription_id, GetMessageCallbackType get_message_callback, MatchesAnyPublishersCallbackType matches_any_publisher_callback, const rcl_subscription_options_t &intra_process_options)
Implemenation detail.
Definition: subscription.hpp:236
void handle_intra_process_message(rcl_interfaces::msg::IntraProcessMessage &ipm, const rmw_message_info_t &message_info)
Definition: subscription.hpp:205
T get(T... args)
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
rmw_gid_t publisher_gid
void handle_message(std::shared_ptr< void > &message, const rmw_message_info_t &message_info)
Check if we need to handle the message, and execute the callback if we do.
Definition: subscription.hpp:184
SubscriptionBase(std::shared_ptr< rcl_node_t > node_handle, const rosidl_message_type_support_t &type_support_handle, const std::string &topic_name, const rcl_subscription_options_t &subscription_options)
Default constructor.
const rcl_subscription_t * get_intra_process_subscription_handle() const
Implemenation detail.
Definition: subscription.hpp:270
T c_str(T... args)
#define RCL_RET_TOPIC_NAME_INVALID
const char * rcl_node_get_name(const rcl_node_t *node)
const char * rcl_node_get_namespace(const rcl_node_t *node)
typename std::conditional< std::is_same< typename std::allocator_traits< Alloc >::template rebind_alloc< T >, typename std::allocator< void >::template rebind< T >::other >::value, std::default_delete< T >, AllocatorDeleter< Alloc > >::type Deleter
Definition: allocator_deleter.hpp:101
rcl_subscription_t subscription_handle_
Definition: subscription.hpp:114
virtual ~SubscriptionBase()
Default destructor.
rcl_subscription_t rcl_get_zero_initialized_subscription(void)
typename MessageAllocTraits::allocator_type MessageAlloc
Definition: subscription.hpp:129
virtual void return_message(std::shared_ptr< void > &message)=0
Return the message borrowed in create_message.