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 <functional>
19 #include <iostream>
20 #include <memory>
21 #include <sstream>
22 #include <string>
23 
24 #include "rcl/error_handling.h"
25 #include "rcl/service.h"
26 
28 #include "rclcpp/exceptions.hpp"
29 #include "rclcpp/macros.hpp"
33 #include "rmw/error_handling.h"
34 #include "rmw/rmw.h"
35 
36 namespace rclcpp
37 {
38 
40 {
41 public:
43 
46  std::shared_ptr<rcl_node_t> node_handle,
47  const std::string & service_name);
48 
50  explicit ServiceBase(
51  std::shared_ptr<rcl_node_t> node_handle);
52 
54  virtual ~ServiceBase();
55 
59 
63 
65  const rcl_service_t *
66  get_service_handle() const;
67 
70  virtual void handle_request(
71  std::shared_ptr<rmw_request_id_t> request_header,
72  std::shared_ptr<void> request) = 0;
73 
74 protected:
76 
78  rcl_node_t *
80 
82  const rcl_node_t *
83  get_rcl_node_handle() const;
84 
86 
89  bool owns_rcl_handle_ = true;
90 };
91 
92 template<typename ServiceT>
93 class Service : public ServiceBase
94 {
95 public:
97  void(
100 
102  void(
107 
109  std::shared_ptr<rcl_node_t> node_handle,
110  const std::string & service_name,
111  AnyServiceCallback<ServiceT> any_callback,
112  rcl_service_options_t & service_options)
113  : ServiceBase(node_handle, service_name), any_callback_(any_callback)
114  {
115  using rosidl_typesupport_cpp::get_service_type_support_handle;
116  auto service_type_support_handle = get_service_type_support_handle<ServiceT>();
117 
118  // rcl does the static memory allocation here
121 
124  node_handle.get(),
125  service_type_support_handle,
126  service_name.c_str(),
127  &service_options);
128  if (ret != RCL_RET_OK) {
129  if (ret == RCL_RET_SERVICE_NAME_INVALID) {
130  auto rcl_node_handle = get_rcl_node_handle();
131  // this will throw on any validation problem
132  rcl_reset_error();
134  service_name,
135  rcl_node_get_name(rcl_node_handle),
136  rcl_node_get_namespace(rcl_node_handle),
137  true);
138  }
139 
140  rclcpp::exceptions::throw_from_rcl_error(ret, "could not create service");
141  }
142  }
143 
145  std::shared_ptr<rcl_node_t> node_handle,
146  rcl_service_t * service_handle,
147  AnyServiceCallback<ServiceT> any_callback)
148  : ServiceBase(node_handle),
149  any_callback_(any_callback)
150  {
151  // check if service handle was initialized
152  // TODO(karsten1987): Take this verification
153  // directly in rcl_*_t
154  // see: https://github.com/ros2/rcl/issues/81
155  if (!service_handle->impl) {
156  // *INDENT-OFF* (prevent uncrustify from making unnecessary indents here)
157  throw std::runtime_error(
158  std::string("rcl_service_t in constructor argument must be initialized beforehand."));
159  // *INDENT-ON*
160  }
161  service_handle_ = service_handle;
163  owns_rcl_handle_ = false;
164  }
165 
166  Service() = delete;
167 
168  virtual ~Service()
169  {
170  // check if you have ownership of the handle
171  if (owns_rcl_handle_) {
174  ss << "Error in destruction of rcl service_handle_ handle: " <<
175  rcl_get_error_string_safe() << '\n';
176  (std::cerr << ss.str()).flush();
177  rcl_reset_error();
178  }
179  delete service_handle_;
180  }
181  }
182 
184  {
185  return std::shared_ptr<void>(new typename ServiceT::Request());
186  }
187 
189  {
190  // TODO(wjwwood): This should probably use rmw_request_id's allocator.
191  // (since it is a C type)
193  }
194 
196  std::shared_ptr<rmw_request_id_t> request_header,
197  std::shared_ptr<void> request)
198  {
199  auto typed_request = std::static_pointer_cast<typename ServiceT::Request>(request);
200  auto response = std::shared_ptr<typename ServiceT::Response>(new typename ServiceT::Response);
201  any_callback_.dispatch(request_header, typed_request, response);
202  send_response(request_header, response);
203  }
204 
208  {
209  rcl_ret_t status = rcl_send_response(get_service_handle(), req_id.get(), response.get());
210 
211  if (status != RCL_RET_OK) {
212  rclcpp::exceptions::throw_from_rcl_error(status, "failed to send response");
213  }
214  }
215 
216 private:
218 
219  AnyServiceCallback<ServiceT> any_callback_;
220 };
221 
222 } // namespace rclcpp
223 
224 #endif // RCLCPP__SERVICE_HPP_
const char * rcl_service_get_service_name(const rcl_service_t *service)
rcl_service_t * service_handle_
Definition: service.hpp:87
rcl_ret_t rcl_service_init(rcl_service_t *service, const rcl_node_t *node, const rosidl_service_type_support_t *type_support, const char *service_name, const rcl_service_options_t *options)
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
Service(std::shared_ptr< rcl_node_t > node_handle, rcl_service_t *service_handle, AnyServiceCallback< ServiceT > any_callback)
Definition: service.hpp:144
#define rcl_reset_error
std::string service_name_
Definition: service.hpp:88
rmw_ret_t rcl_ret_t
ServiceBase(std::shared_ptr< rcl_node_t > node_handle, const std::string &service_name)
virtual std::shared_ptr< rmw_request_id_t > create_request_header()=0
struct rcl_service_impl_t * impl
Definition: allocator_common.hpp:24
std::shared_ptr< rmw_request_id_t > create_request_header()
Definition: service.hpp:188
std::shared_ptr< void > create_request()
Definition: service.hpp:183
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.
#define RCL_RET_OK
virtual ~ServiceBase()
void send_response(std::shared_ptr< rmw_request_id_t > req_id, std::shared_ptr< typename ServiceT::Response > response)
Definition: service.hpp:205
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 ~Service()
Definition: service.hpp:168
rcl_ret_t rcl_send_response(const rcl_service_t *service, rmw_request_id_t *response_header, void *ros_response)
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
void handle_request(std::shared_ptr< rmw_request_id_t > request_header, std::shared_ptr< void > request)
Definition: service.hpp:195
T str(T... args)
T static_pointer_cast(T... args)
#define rcl_get_error_string_safe
rcl_ret_t rcl_service_fini(rcl_service_t *service, rcl_node_t *node)
rcl_service_t * get_service_handle()
virtual std::shared_ptr< void > create_request()=0
rcl_node_t * get_rcl_node_handle()
Definition: service.hpp:93
#define RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(...)
Definition: macros.hpp:51
T get(T... args)
std::shared_ptr< rcl_node_t > node_handle_
Definition: service.hpp:85
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
#define RCL_RET_SERVICE_NAME_INVALID
const char * rcl_node_get_name(const rcl_node_t *node)
const char * rcl_node_get_namespace(const rcl_node_t *node)
rcl_service_t rcl_get_zero_initialized_service(void)
virtual void handle_request(std::shared_ptr< rmw_request_id_t > request_header, std::shared_ptr< void > request)=0
Definition: service.hpp:39
Definition: any_service_callback.hpp:31
Service()=delete
bool owns_rcl_handle_
Definition: service.hpp:89
std::string get_service_name()