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 namespace service
39 {
40 
42 {
43 public:
45 
48  std::shared_ptr<rcl_node_t> node_handle,
49  const std::string & service_name);
50 
52  explicit ServiceBase(
53  std::shared_ptr<rcl_node_t> node_handle);
54 
56  virtual ~ServiceBase();
57 
61 
65 
67  const rcl_service_t *
68  get_service_handle() const;
69 
72  virtual void handle_request(
73  std::shared_ptr<rmw_request_id_t> request_header,
74  std::shared_ptr<void> request) = 0;
75 
76 protected:
77  RCLCPP_DISABLE_COPY(ServiceBase)
78 
80  rcl_node_t *
82 
84  const rcl_node_t *
85  get_rcl_node_handle() const;
86 
88 
91  bool owns_rcl_handle_ = true;
92 };
93 
95 
96 template<typename ServiceT>
97 class Service : public ServiceBase
98 {
99 public:
100  using CallbackType = std::function<
101  void(
104 
106  void(
111 
113  std::shared_ptr<rcl_node_t> node_handle,
114  const std::string & service_name,
115  AnyServiceCallback<ServiceT> any_callback,
116  rcl_service_options_t & service_options)
117  : ServiceBase(node_handle, service_name), any_callback_(any_callback)
118  {
119  using rosidl_typesupport_cpp::get_service_type_support_handle;
120  auto service_type_support_handle = get_service_type_support_handle<ServiceT>();
121 
122  // rcl does the static memory allocation here
124  *service_handle_ = rcl_get_zero_initialized_service();
125 
127  service_handle_,
128  node_handle.get(),
129  service_type_support_handle,
130  service_name.c_str(),
131  &service_options);
132  if (ret != RCL_RET_OK) {
133  if (ret == RCL_RET_SERVICE_NAME_INVALID) {
134  auto rcl_node_handle = get_rcl_node_handle();
135  // this will throw on any validation problem
136  rcl_reset_error();
138  service_name,
139  rcl_node_get_name(rcl_node_handle),
140  rcl_node_get_namespace(rcl_node_handle),
141  true);
142  }
143 
144  rclcpp::exceptions::throw_from_rcl_error(ret, "could not create service");
145  }
146  }
147 
149  std::shared_ptr<rcl_node_t> node_handle,
150  rcl_service_t * service_handle,
151  AnyServiceCallback<ServiceT> any_callback)
152  : ServiceBase(node_handle),
153  any_callback_(any_callback)
154  {
155  // check if service handle was initialized
156  // TODO(karsten1987): Take this verification
157  // directly in rcl_*_t
158  // see: https://github.com/ros2/rcl/issues/81
159  if (!service_handle->impl) {
160  // *INDENT-OFF* (prevent uncrustify from making unnecessary indents here)
161  throw std::runtime_error(
162  std::string("rcl_service_t in constructor argument must be initialized beforehand."));
163  // *INDENT-ON*
164  }
165  service_handle_ = service_handle;
167  owns_rcl_handle_ = false;
168  }
169 
170  Service() = delete;
171 
172  virtual ~Service()
173  {
174  // check if you have ownership of the handle
175  if (owns_rcl_handle_) {
178  ss << "Error in destruction of rcl service_handle_ handle: " <<
179  rcl_get_error_string_safe() << '\n';
180  (std::cerr << ss.str()).flush();
181  rcl_reset_error();
182  }
183  delete service_handle_;
184  }
185  }
186 
188  {
189  return std::shared_ptr<void>(new typename ServiceT::Request());
190  }
191 
193  {
194  // TODO(wjwwood): This should probably use rmw_request_id's allocator.
195  // (since it is a C type)
197  }
198 
200  std::shared_ptr<void> request)
201  {
202  auto typed_request = std::static_pointer_cast<typename ServiceT::Request>(request);
203  auto response = std::shared_ptr<typename ServiceT::Response>(new typename ServiceT::Response);
204  any_callback_.dispatch(request_header, typed_request, response);
205  send_response(request_header, response);
206  }
207 
211  {
212  rcl_ret_t status = rcl_send_response(get_service_handle(), req_id.get(), response.get());
213 
214  if (status != RCL_RET_OK) {
215  rclcpp::exceptions::throw_from_rcl_error(status, "failed to send response");
216  }
217  }
218 
219 private:
221 
222  AnyServiceCallback<ServiceT> any_callback_;
223 };
224 
225 } // namespace service
226 } // namespace rclcpp
227 
228 #endif // RCLCPP__SERVICE_HPP_
const char * rcl_service_get_service_name(const rcl_service_t *service)
virtual std::shared_ptr< void > create_request()=0
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)
Definition: service.hpp:97
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
#define rcl_reset_error
rmw_ret_t rcl_ret_t
Service(std::shared_ptr< rcl_node_t > node_handle, rcl_service_t *service_handle, AnyServiceCallback< ServiceT > any_callback)
Definition: service.hpp:148
rcl_node_t * get_rcl_node_handle()
struct rcl_service_impl_t * impl
Definition: allocator_common.hpp:24
bool owns_rcl_handle_
Definition: service.hpp:91
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
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.
Definition: service.hpp:41
std::shared_ptr< rcl_node_t > node_handle_
Definition: service.hpp:87
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
ServiceBase(std::shared_ptr< rcl_node_t > node_handle, const std::string &service_name)
T str(T... args)
std::shared_ptr< void > create_request()
Definition: service.hpp:187
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)
#define RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(...)
Definition: macros.hpp:51
virtual std::shared_ptr< rmw_request_id_t > create_request_header()=0
T get(T... args)
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
std::shared_ptr< rmw_request_id_t > create_request_header()
Definition: service.hpp:192
#define RCL_RET_SERVICE_NAME_INVALID
const char * rcl_node_get_name(const rcl_node_t *node)
rcl_service_t * get_service_handle()
virtual ~Service()
Definition: service.hpp:172
const char * rcl_node_get_namespace(const rcl_node_t *node)
void handle_request(std::shared_ptr< rmw_request_id_t > request_header, std::shared_ptr< void > request)
Definition: service.hpp:199
rcl_service_t rcl_get_zero_initialized_service(void)
void send_response(std::shared_ptr< rmw_request_id_t > req_id, std::shared_ptr< typename ServiceT::Response > response)
Definition: service.hpp:208
rcl_service_t * service_handle_
Definition: service.hpp:89
Definition: any_service_callback.hpp:34
virtual void handle_request(std::shared_ptr< rmw_request_id_t > request_header, std::shared_ptr< void > request)=0
std::string service_name_
Definition: service.hpp:90