15 #include "rclcpp/service.hpp"
24 #include "rclcpp/any_service_callback.hpp"
25 #include "rclcpp/detail/cpp_callback_trampoline.hpp"
26 #include "rclcpp/logging.hpp"
27 #include "rclcpp/macros.hpp"
28 #include "rclcpp/qos.hpp"
29 #include "rmw/error_handling.h"
30 #include "rmw/impl/cpp/demangle.hpp"
35 ServiceBase::ServiceBase(
const std::shared_ptr<rcl_node_t> & node_handle)
36 : node_handle_(node_handle),
42 ServiceBase::take_type_erased_request(
void * request_out, rmw_request_id_t & request_id_out)
51 rclcpp::exceptions::throw_from_rcl_error(ret);
62 std::shared_ptr<rcl_service_t>
65 return service_handle_;
68 std::shared_ptr<const rcl_service_t>
71 return service_handle_;
75 ServiceBase::get_rcl_node_handle()
77 return node_handle_.get();
81 ServiceBase::get_rcl_node_handle()
const
83 return node_handle_.get();
89 return in_use_by_wait_set_.exchange(in_use_state);
95 const rmw_qos_profile_t * qos =
99 std::string(
"failed to get service's response publisher qos settings: ") +
100 rcl_get_error_string().str;
102 throw std::runtime_error(msg);
108 return response_publisher_qos;
114 const rmw_qos_profile_t * qos =
118 std::string(
"failed to get service's request subscription qos settings: ") +
119 rcl_get_error_string().str;
121 throw std::runtime_error(msg);
127 return request_subscription_qos;
134 service_handle_.get(),
139 rclcpp::exceptions::throw_from_rcl_error(
140 ret,
"failed to set the on new request callback for service");
148 throw std::invalid_argument(
149 "The callback passed to set_on_new_request_callback "
154 [callback,
this](
size_t number_of_requests) {
156 callback(number_of_requests);
157 }
catch (
const std::exception & exception) {
160 "rclcpp::ServiceBase@" <<
this <<
161 " caught " << rmw::impl::cpp::demangle(exception) <<
162 " exception in user-provided callback for the 'on new request' callback: " <<
167 "rclcpp::ServiceBase@" <<
this <<
168 " caught unhandled exception in user-provided callback " <<
169 "for the 'on new request' callback");
173 std::lock_guard<std::recursive_mutex> lock(callback_mutex_);
179 rclcpp::detail::cpp_callback_trampoline<decltype(new_callback),
const void *,
size_t>,
180 static_cast<const void *
>(&new_callback));
183 on_new_request_callback_ = new_callback;
187 rclcpp::detail::cpp_callback_trampoline<
188 decltype(on_new_request_callback_),
const void *,
size_t>,
189 static_cast<const void *
>(&on_new_request_callback_));
195 std::lock_guard<std::recursive_mutex> lock(callback_mutex_);
196 if (on_new_request_callback_) {
198 on_new_request_callback_ =
nullptr;
Encapsulation of Quality of Service settings.
RCLCPP_PUBLIC bool exchange_in_use_by_wait_set_state(bool in_use_state)
Exchange the "in use by wait set" state for this service.
RCLCPP_PUBLIC rclcpp::QoS get_request_subscription_actual_qos() const
Get the actual request subscription QoS settings, after the defaults have been determined.
RCLCPP_PUBLIC rclcpp::QoS get_response_publisher_actual_qos() const
Get the actual response publisher QoS settings, after the defaults have been determined.
RCLCPP_PUBLIC void clear_on_new_request_callback()
Unset the callback registered for new requests, if any.
RCLCPP_PUBLIC std::shared_ptr< rcl_service_t > get_service_handle()
Return the rcl_service_t service handle in a std::shared_ptr.
RCLCPP_PUBLIC void set_on_new_request_callback(const std::function< void(size_t)> &callback)
Set a callback to be called when each new request is received.
RCLCPP_PUBLIC const char * get_service_name()
Return the name of the service.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC Logger get_node_logger(const rcl_node_t *node)
Return a named logger using an rcl_node_t.
RCL_PUBLIC RCL_WARN_UNUSED const rmw_qos_profile_t * rcl_service_request_subscription_get_actual_qos(const rcl_service_t *service)
Get the actual qos settings of the service's request subscription.
RCL_PUBLIC RCL_WARN_UNUSED const char * rcl_service_get_service_name(const rcl_service_t *service)
Get the topic name for the service.
RCL_PUBLIC RCL_WARN_UNUSED const rmw_qos_profile_t * rcl_service_response_publisher_get_actual_qos(const rcl_service_t *service)
Get the actual qos settings of the service's response publisher.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_service_set_on_new_request_callback(const rcl_service_t *service, rcl_event_callback_t callback, const void *user_data)
Set the on new request callback function for the service.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_take_request(const rcl_service_t *service, rmw_request_id_t *request_header, void *ros_request)
Backwards compatibility function to take a pending ROS request using a rcl service.
Structure which encapsulates a ROS Node.
static QoSInitialization from_rmw(const rmw_qos_profile_t &rmw_qos)
Create a QoSInitialization from an existing rmw_qos_profile_t, using its history and depth.
#define RCL_RET_OK
Success return code.
#define RCL_RET_SERVICE_TAKE_FAILED
Failed to take a request from the service return code.
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.