ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
rclcpp::ServiceBase Class Referenceabstract
Inheritance diagram for rclcpp::ServiceBase:
Inheritance graph
[legend]
Collaboration diagram for rclcpp::ServiceBase:
Collaboration graph
[legend]

Public Member Functions

RCLCPP_PUBLIC ServiceBase (std::shared_ptr< rcl_node_t > node_handle)
 
RCLCPP_PUBLIC const char * get_service_name ()
 Return the name of the service. More...
 
RCLCPP_PUBLIC std::shared_ptr< rcl_service_tget_service_handle ()
 Return the rcl_service_t service handle in a std::shared_ptr. More...
 
RCLCPP_PUBLIC std::shared_ptr< const rcl_service_tget_service_handle () const
 Return the rcl_service_t service handle in a std::shared_ptr. More...
 
RCLCPP_PUBLIC 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. More...
 
virtual std::shared_ptr< void > create_request ()=0
 
virtual std::shared_ptr< rmw_request_id_t > create_request_header ()=0
 
virtual void handle_request (std::shared_ptr< rmw_request_id_t > request_header, std::shared_ptr< void > request)=0
 
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. More...
 
RCLCPP_PUBLIC rclcpp::QoS get_response_publisher_actual_qos () const
 Get the actual response publisher QoS settings, after the defaults have been determined. More...
 
RCLCPP_PUBLIC rclcpp::QoS get_request_subscription_actual_qos () const
 Get the actual request subscription QoS settings, after the defaults have been determined. More...
 
void set_on_new_request_callback (std::function< void(size_t)> callback)
 Set a callback to be called when each new request is received. More...
 
void clear_on_new_request_callback ()
 Unset the callback registered for new requests, if any.
 

Protected Member Functions

RCLCPP_PUBLIC rcl_node_tget_rcl_node_handle ()
 
RCLCPP_PUBLIC const rcl_node_tget_rcl_node_handle () const
 
RCLCPP_PUBLIC void set_on_new_request_callback (rcl_event_callback_t callback, const void *user_data)
 

Protected Attributes

std::shared_ptr< rcl_node_tnode_handle_
 
std::shared_ptr< rcl_service_tservice_handle_
 
bool owns_rcl_handle_ = true
 
rclcpp::Logger node_logger_
 
std::atomic< bool > in_use_by_wait_set_ {false}
 
std::recursive_mutex callback_mutex_
 
std::function< void(size_t)> on_new_request_callback_ {nullptr}
 

Detailed Description

Definition at line 49 of file service.hpp.

Member Function Documentation

◆ exchange_in_use_by_wait_set_state()

bool ServiceBase::exchange_in_use_by_wait_set_state ( bool  in_use_state)

Exchange the "in use by wait set" state for this service.

This is used to ensure this service is not used by multiple wait sets at the same time.

Parameters
[in]in_use_statethe new state to exchange into the state, true indicates it is now in use by a wait set, and false is that it is no longer in use by a wait set.
Returns
the previous state.

Definition at line 86 of file service.cpp.

◆ get_request_subscription_actual_qos()

rclcpp::QoS ServiceBase::get_request_subscription_actual_qos ( ) const

Get the actual request subscription QoS settings, after the defaults have been determined.

The actual configuration applied when using RMW_QOS_POLICY_*_SYSTEM_DEFAULT can only be resolved after the creation of the service, and it depends on the underlying rmw implementation. If the underlying setting in use can't be represented in ROS terms, it will be set to RMW_QOS_POLICY_*_UNKNOWN. May throw runtime_error when an unexpected error occurs.

Returns
The actual request subscription qos settings.
Exceptions
std::runtime_errorif failed to get qos settings

Definition at line 111 of file service.cpp.

References rclcpp::QoSInitialization::from_rmw(), and rcl_service_request_subscription_get_actual_qos().

Here is the call graph for this function:

◆ get_response_publisher_actual_qos()

rclcpp::QoS ServiceBase::get_response_publisher_actual_qos ( ) const

Get the actual response publisher QoS settings, after the defaults have been determined.

The actual configuration applied when using RMW_QOS_POLICY_*_SYSTEM_DEFAULT can only be resolved after the creation of the service, and it depends on the underlying rmw implementation. If the underlying setting in use can't be represented in ROS terms, it will be set to RMW_QOS_POLICY_*_UNKNOWN. May throw runtime_error when an unexpected error occurs.

Returns
The actual response publisher qos settings.
Exceptions
std::runtime_errorif failed to get qos settings

Definition at line 92 of file service.cpp.

References rclcpp::QoSInitialization::from_rmw(), and rcl_service_response_publisher_get_actual_qos().

Here is the call graph for this function:

◆ get_service_handle() [1/2]

std::shared_ptr< rcl_service_t > ServiceBase::get_service_handle ( )

Return the rcl_service_t service handle in a std::shared_ptr.

This handle remains valid after the Service is destroyed. The actual rcl service is not finalized until it is out of scope everywhere.

Definition at line 62 of file service.cpp.

Referenced by get_service_name(), rclcpp::Service< ServiceT >::Service(), and take_type_erased_request().

Here is the caller graph for this function:

◆ get_service_handle() [2/2]

std::shared_ptr< const rcl_service_t > ServiceBase::get_service_handle ( ) const

Return the rcl_service_t service handle in a std::shared_ptr.

This handle remains valid after the Service is destroyed. The actual rcl service is not finalized until it is out of scope everywhere.

Definition at line 68 of file service.cpp.

◆ get_service_name()

const char * ServiceBase::get_service_name ( )

Return the name of the service.

Returns
The name of the service.

Definition at line 56 of file service.cpp.

References get_service_handle(), and rcl_service_get_service_name().

Here is the call graph for this function:

◆ set_on_new_request_callback()

void rclcpp::ServiceBase::set_on_new_request_callback ( std::function< void(size_t)>  callback)
inline

Set a callback to be called when each new request is received.

The callback receives a size_t which is the number of requests received since the last time this callback was called. Normally this is 1, but can be > 1 if requests were received before any callback was set.

Since this callback is called from the middleware, you should aim to make it fast and not blocking. If you need to do a lot of work or wait for some other event, you should spin it off to another thread, otherwise you risk blocking the middleware.

Calling it again will clear any previously set callback.

An exception will be thrown if the callback is not callable.

This function is thread-safe.

If you want more information available in the callback, like the service or other information, you may use a lambda with captures or std::bind.

See also
rmw_service_set_on_new_request_callback
rcl_service_set_on_new_request_callback
Parameters
[in]callbackfunctor to be called when a new request is received

Definition at line 191 of file service.hpp.

Referenced by clear_on_new_request_callback().

Here is the caller graph for this function:

◆ take_type_erased_request()

bool ServiceBase::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.

This type erased version of

See also
Service::take_request() is useful when using the service in a type agnostic way with methods like ServiceBase::create_request(), ServiceBase::create_request_header(), and ServiceBase::handle_request().
Parameters
[out]request_outThe type erased pointer to a service request object into which the middleware will copy the taken request.
[out]request_id_outThe output id for the request which can be used to associate response with this request in the future.
Returns
true if the request was taken, otherwise false.
Exceptions
rclcpp::exceptions::RCLErrorbased exceptions if the underlying rcl calls fail.

Definition at line 41 of file service.cpp.

References get_service_handle(), RCL_RET_OK, RCL_RET_SERVICE_TAKE_FAILED, and rcl_take_request().

Referenced by rclcpp::Service< ServiceT >::take_request().

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: