rclcpp  master
C++ ROS Client Library API
service.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__SERVICE_HPP_
16 #define RCLCPP__SERVICE_HPP_
17 
18 #include <atomic>
19 #include <functional>
20 #include <iostream>
21 #include <memory>
22 #include <sstream>
23 #include <string>
24 
25 #include "rcl/error_handling.h"
26 #include "rcl/service.h"
27 
29 #include "rclcpp/exceptions.hpp"
30 #include "rclcpp/macros.hpp"
34 #include "rclcpp/logging.hpp"
35 #include "rmw/error_handling.h"
36 #include "rmw/rmw.h"
37 #include "tracetools/tracetools.h"
38 
39 namespace rclcpp
40 {
41 
43 {
44 public:
46 
48  explicit ServiceBase(std::shared_ptr<rcl_node_t> node_handle);
49 
51  virtual ~ServiceBase();
52 
54 
56  const char *
58 
60 
67 
69 
75  get_service_handle() const;
76 
78 
93  bool
94  take_type_erased_request(void * request_out, rmw_request_id_t & request_id_out);
95 
96  virtual
98  create_request() = 0;
99 
100  virtual
102  create_request_header() = 0;
103 
104  virtual
105  void
107  std::shared_ptr<rmw_request_id_t> request_header,
108  std::shared_ptr<void> request) = 0;
109 
111 
121  bool
122  exchange_in_use_by_wait_set_state(bool in_use_state);
123 
124 protected:
126 
128  rcl_node_t *
130 
132  const rcl_node_t *
133  get_rcl_node_handle() const;
134 
136 
138  bool owns_rcl_handle_ = true;
139 
141 };
142 
143 template<typename ServiceT>
144 class Service : public ServiceBase
145 {
146 public:
147  using CallbackType = std::function<
148  void (
151 
153  void (
158 
159 
160 
171  std::shared_ptr<rcl_node_t> node_handle,
172  const std::string & service_name,
173  AnyServiceCallback<ServiceT> any_callback,
174  rcl_service_options_t & service_options)
175  : ServiceBase(node_handle), any_callback_(any_callback)
176  {
177  using rosidl_typesupport_cpp::get_service_type_support_handle;
178  auto service_type_support_handle = get_service_type_support_handle<ServiceT>();
179 
180  std::weak_ptr<rcl_node_t> weak_node_handle(node_handle_);
181  // rcl does the static memory allocation here
183  new rcl_service_t, [weak_node_handle](rcl_service_t * service)
184  {
185  auto handle = weak_node_handle.lock();
186  if (handle) {
187  if (rcl_service_fini(service, handle.get()) != RCL_RET_OK) {
188  RCLCPP_ERROR(
189  rclcpp::get_node_logger(handle.get()).get_child("rclcpp"),
190  "Error in destruction of rcl service handle: %s",
191  rcl_get_error_string().str);
192  rcl_reset_error();
193  }
194  } else {
195  RCLCPP_ERROR(
196  rclcpp::get_logger("rclcpp"),
197  "Error in destruction of rcl service handle: "
198  "the Node Handle was destructed too early. You will leak memory");
199  }
200  delete service;
201  });
202  *service_handle_.get() = rcl_get_zero_initialized_service();
203 
204  rcl_ret_t ret = rcl_service_init(
206  node_handle.get(),
207  service_type_support_handle,
208  service_name.c_str(),
209  &service_options);
210  if (ret != RCL_RET_OK) {
211  if (ret == RCL_RET_SERVICE_NAME_INVALID) {
212  auto rcl_node_handle = get_rcl_node_handle();
213  // this will throw on any validation problem
214  rcl_reset_error();
216  service_name,
217  rcl_node_get_name(rcl_node_handle),
218  rcl_node_get_namespace(rcl_node_handle),
219  true);
220  }
221 
222  rclcpp::exceptions::throw_from_rcl_error(ret, "could not create service");
223  }
224  TRACEPOINT(
225  rclcpp_service_callback_added,
226  (const void *)get_service_handle().get(),
227  (const void *)&any_callback_);
228 #ifndef TRACETOOLS_DISABLED
229  any_callback_.register_callback_for_tracing();
230 #endif
231  }
232 
234 
244  std::shared_ptr<rcl_node_t> node_handle,
245  std::shared_ptr<rcl_service_t> service_handle,
246  AnyServiceCallback<ServiceT> any_callback)
247  : ServiceBase(node_handle),
248  any_callback_(any_callback)
249  {
250  // check if service handle was initialized
251  if (!rcl_service_is_valid(service_handle.get())) {
252  // *INDENT-OFF* (prevent uncrustify from making unnecessary indents here)
253  throw std::runtime_error(
254  std::string("rcl_service_t in constructor argument must be initialized beforehand."));
255  // *INDENT-ON*
256  }
257 
258  service_handle_ = service_handle;
259  TRACEPOINT(
260  rclcpp_service_callback_added,
261  (const void *)get_service_handle().get(),
262  (const void *)&any_callback_);
263 #ifndef TRACETOOLS_DISABLED
264  any_callback_.register_callback_for_tracing();
265 #endif
266  }
267 
269 
279  std::shared_ptr<rcl_node_t> node_handle,
280  rcl_service_t * service_handle,
281  AnyServiceCallback<ServiceT> any_callback)
282  : ServiceBase(node_handle),
283  any_callback_(any_callback)
284  {
285  // check if service handle was initialized
286  if (!rcl_service_is_valid(service_handle)) {
287  // *INDENT-OFF* (prevent uncrustify from making unnecessary indents here)
288  throw std::runtime_error(
289  std::string("rcl_service_t in constructor argument must be initialized beforehand."));
290  // *INDENT-ON*
291  }
292 
293  // In this case, rcl owns the service handle memory
295  service_handle_->impl = service_handle->impl;
296  TRACEPOINT(
297  rclcpp_service_callback_added,
298  (const void *)get_service_handle().get(),
299  (const void *)&any_callback_);
300 #ifndef TRACETOOLS_DISABLED
301  any_callback_.register_callback_for_tracing();
302 #endif
303  }
304 
305  Service() = delete;
306 
307  virtual ~Service()
308  {
309  }
310 
312 
323  bool
324  take_request(typename ServiceT::Request & request_out, rmw_request_id_t & request_id_out)
325  {
326  return this->take_type_erased_request(&request_out, request_id_out);
327  }
328 
330  create_request() override
331  {
332  return std::make_shared<typename ServiceT::Request>();
333  }
334 
337  {
338  return std::make_shared<rmw_request_id_t>();
339  }
340 
341  void
343  std::shared_ptr<rmw_request_id_t> request_header,
344  std::shared_ptr<void> request) override
345  {
346  auto typed_request = std::static_pointer_cast<typename ServiceT::Request>(request);
347  auto response = std::make_shared<typename ServiceT::Response>();
348  any_callback_.dispatch(request_header, typed_request, response);
349  send_response(*request_header, *response);
350  }
351 
352  [[deprecated("use the send_response() which takes references instead of shared pointers")]]
353  void
357  {
358  send_response(*req_id, *response);
359  }
360 
361  void
362  send_response(rmw_request_id_t & req_id, typename ServiceT::Response & response)
363  {
364  rcl_ret_t ret = rcl_send_response(get_service_handle().get(), &req_id, &response);
365 
366  if (ret != RCL_RET_OK) {
367  rclcpp::exceptions::throw_from_rcl_error(ret, "failed to send response");
368  }
369  }
370 
371 private:
373 
374  AnyServiceCallback<ServiceT> any_callback_;
375 };
376 
377 } // namespace rclcpp
378 
379 #endif // RCLCPP__SERVICE_HPP_
rclcpp::ServiceBase::handle_request
virtual void handle_request(std::shared_ptr< rmw_request_id_t > request_header, std::shared_ptr< void > request)=0
rcl_node_t
std::weak_ptr::lock
T lock(T... args)
exceptions.hpp
rclcpp::AnyServiceCallback
Definition: any_service_callback.hpp:33
rcl_service_t::impl
struct rcl_service_impl_t * impl
std::string
std::shared_ptr< rcl_node_t >
RCLCPP_DISABLE_COPY
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
rclcpp::ServiceBase::~ServiceBase
virtual ~ServiceBase()
rmw.h
rclcpp::ServiceBase::owns_rcl_handle_
bool owns_rcl_handle_
Definition: service.hpp:138
rcl_service_options_t
error_handling.h
rclcpp::Service
Definition: service.hpp:144
rclcpp::get_logger
Logger get_logger(const std::string &name)
Return a named logger.
std::shared_ptr::get
T get(T... args)
std::function< void(const std::shared_ptr< typename ServiceT::Request >, std::shared_ptr< typename ServiceT::Response >)>
rclcpp::Service::Service
Service()=delete
rclcpp::Service::send_response
void send_response(rmw_request_id_t &req_id, typename ServiceT::Response &response)
Definition: service.hpp:362
rclcpp::Service::Service
Service(std::shared_ptr< rcl_node_t > node_handle, std::shared_ptr< rcl_service_t > service_handle, AnyServiceCallback< ServiceT > any_callback)
Default constructor.
Definition: service.hpp:243
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
rclcpp::ServiceBase::service_handle_
std::shared_ptr< rcl_service_t > service_handle_
Definition: service.hpp:137
RCLCPP_PUBLIC
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
rclcpp::Service::create_request
std::shared_ptr< void > create_request() override
Definition: service.hpp:330
rclcpp::ServiceBase::create_request_header
virtual std::shared_ptr< rmw_request_id_t > create_request_header()=0
rclcpp::Service::~Service
virtual ~Service()
Definition: service.hpp:307
RCLCPP_SMART_PTR_DEFINITIONS
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
macros.hpp
rclcpp::ServiceBase::get_service_name
const char * get_service_name()
Return the name of the service.
rclcpp::expand_topic_or_service_name
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.
logging.hpp
any_service_callback.hpp
expand_topic_or_service_name.hpp
rmw_request_id_t
std::runtime_error
std::atomic< bool >
rclcpp::ServiceBase::ServiceBase
ServiceBase(std::shared_ptr< rcl_node_t > node_handle)
rclcpp::ServiceBase::exchange_in_use_by_wait_set_state
bool exchange_in_use_by_wait_set_state(bool in_use_state)
Exchange the "in use by wait set" state for this service.
std::weak_ptr
rclcpp::Service::Service
Service(std::shared_ptr< rcl_node_t > node_handle, rcl_service_t *service_handle, AnyServiceCallback< ServiceT > any_callback)
Default constructor.
Definition: service.hpp:278
rclcpp::ServiceBase::get_service_handle
std::shared_ptr< rcl_service_t > get_service_handle()
Return the rcl_service_t service handle in a std::shared_ptr.
type_support_decl.hpp
rclcpp::Service::handle_request
void handle_request(std::shared_ptr< rmw_request_id_t > request_header, std::shared_ptr< void > request) override
Definition: service.hpp:342
visibility_control.hpp
std
rclcpp::ServiceBase::create_request
virtual std::shared_ptr< void > create_request()=0
rclcpp::Service::send_response
void send_response(std::shared_ptr< rmw_request_id_t > req_id, std::shared_ptr< typename ServiceT::Response > response)
Definition: service.hpp:354
rclcpp::ServiceBase::in_use_by_wait_set_
std::atomic< bool > in_use_by_wait_set_
Definition: service.hpp:140
rclcpp::ServiceBase::node_handle_
std::shared_ptr< rcl_node_t > node_handle_
Definition: service.hpp:135
rclcpp::ServiceBase::get_rcl_node_handle
rcl_node_t * get_rcl_node_handle()
rclcpp::Service::create_request_header
std::shared_ptr< rmw_request_id_t > create_request_header() override
Definition: service.hpp:336
RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE
#define RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(...)
Definition: macros.hpp:51
rclcpp::exceptions::throw_from_rcl_error
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.
rcl_service_t
rclcpp::ServiceBase
Definition: service.hpp:42
rclcpp::ServiceBase::take_type_erased_request
bool take_type_erased_request(void *request_out, rmw_request_id_t &request_id_out)
Take the next request from the service as a type erased pointer.
RCLCPP_ERROR
#define RCLCPP_ERROR(logger,...)
Definition: logging.hpp:1419
rclcpp::Service::take_request
bool take_request(typename ServiceT::Request &request_out, rmw_request_id_t &request_id_out)
Take the next request from the service.
Definition: service.hpp:324