ROS 2 rclcpp + rcl - rolling  rolling-a919a6e5
ROS 2 C++ Client Library with ROS Client Library
Classes | Public Types | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
rclcpp::Client< ServiceT > Class Template Reference
Inheritance diagram for rclcpp::Client< ServiceT >:
Inheritance graph
[legend]
Collaboration diagram for rclcpp::Client< ServiceT >:
Collaboration graph
[legend]

Classes

struct  FutureAndRequestId
 A convenient Client::Future and request id pair. More...
 
struct  SharedFutureAndRequestId
 A convenient Client::SharedFuture and request id pair. More...
 
struct  SharedFutureWithRequestAndRequestId
 A convenient Client::SharedFutureWithRequest and request id pair. More...
 

Public Types

using Request = typename ServiceT::Request
 
using Response = typename ServiceT::Response
 
using SharedRequest = typename ServiceT::Request::SharedPtr
 
using SharedResponse = typename ServiceT::Response::SharedPtr
 
using Promise = std::promise< SharedResponse >
 
using PromiseWithRequest = std::promise< std::pair< SharedRequest, SharedResponse > >
 
using SharedPromise = std::shared_ptr< Promise >
 
using SharedPromiseWithRequest = std::shared_ptr< PromiseWithRequest >
 
using Future = std::future< SharedResponse >
 
using SharedFuture = std::shared_future< SharedResponse >
 
using SharedFutureWithRequest = std::shared_future< std::pair< SharedRequest, SharedResponse > >
 
using CallbackType = std::function< void(SharedFuture)>
 
using CallbackWithRequestType = std::function< void(SharedFutureWithRequest)>
 

Public Member Functions

 Client (rclcpp::node_interfaces::NodeBaseInterface *node_base, rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph, const std::string &service_name, rcl_client_options_t &client_options)
 Default constructor. More...
 
bool take_response (typename ServiceT::Response &response_out, rmw_request_id_t &request_header_out)
 Take the next response for this client. More...
 
std::shared_ptr< void > create_response () override
 Create a shared pointer with the response type. More...
 
std::shared_ptr< rmw_request_id_t > create_request_header () override
 Create a shared pointer with a rmw_request_id_t. More...
 
void handle_response (std::shared_ptr< rmw_request_id_t > request_header, std::shared_ptr< void > response) override
 Handle a server response. More...
 
FutureAndRequestId async_send_request (SharedRequest request)
 Send a request to the service server. More...
 
template<typename CallbackT , typename std::enable_if< rclcpp::function_traits::same_arguments< CallbackT, CallbackType >::value >::type * = nullptr>
SharedFutureAndRequestId async_send_request (SharedRequest request, CallbackT &&cb)
 Send a request to the service server and schedule a callback in the executor. More...
 
template<typename CallbackT , typename std::enable_if< rclcpp::function_traits::same_arguments< CallbackT, CallbackWithRequestType >::value >::type * = nullptr>
SharedFutureWithRequestAndRequestId async_send_request (SharedRequest request, CallbackT &&cb)
 Send a request to the service server and schedule a callback in the executor. More...
 
bool remove_pending_request (int64_t request_id)
 Cleanup a pending request. More...
 
bool remove_pending_request (const FutureAndRequestId &future)
 Cleanup a pending request. More...
 
bool remove_pending_request (const SharedFutureAndRequestId &future)
 Cleanup a pending request. More...
 
bool remove_pending_request (const SharedFutureWithRequestAndRequestId &future)
 Cleanup a pending request. More...
 
size_t prune_pending_requests ()
 Clean all pending requests. More...
 
template<typename AllocatorT = std::allocator<int64_t>>
size_t prune_requests_older_than (std::chrono::time_point< std::chrono::system_clock > time_point, std::vector< int64_t, AllocatorT > *pruned_requests=nullptr)
 Clean all pending requests older than a time_point. More...
 
void configure_introspection (Clock::SharedPtr clock, const QoS &qos_service_event_pub, rcl_service_introspection_state_t introspection_state)
 Configure client introspection. More...
 
- Public Member Functions inherited from rclcpp::ClientBase
RCLCPP_PUBLIC ClientBase (rclcpp::node_interfaces::NodeBaseInterface *node_base, rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph)
 
RCLCPP_PUBLIC bool take_type_erased_response (void *response_out, rmw_request_id_t &request_header_out)
 Take the next response for this client as a type erased pointer. More...
 
RCLCPP_PUBLIC const char * get_service_name () const
 Return the name of the service. More...
 
RCLCPP_PUBLIC std::shared_ptr< rcl_client_tget_client_handle ()
 Return the rcl_client_t client handle in a std::shared_ptr. More...
 
RCLCPP_PUBLIC std::shared_ptr< const rcl_client_tget_client_handle () const
 Return the rcl_client_t client handle in a std::shared_ptr. More...
 
RCLCPP_PUBLIC bool service_is_ready () const
 Return if the service is ready. More...
 
template<typename RepT = int64_t, typename RatioT = std::milli>
bool wait_for_service (std::chrono::duration< RepT, RatioT > timeout=std::chrono::duration< RepT, RatioT >(-1))
 Wait for a service to be ready. More...
 
RCLCPP_PUBLIC bool exchange_in_use_by_wait_set_state (bool in_use_state)
 Exchange the "in use by wait set" state for this client. More...
 
RCLCPP_PUBLIC rclcpp::QoS get_request_publisher_actual_qos () const
 Get the actual request publsher QoS settings, after the defaults have been determined. More...
 
RCLCPP_PUBLIC rclcpp::QoS get_response_subscription_actual_qos () const
 Get the actual response subscription QoS settings, after the defaults have been determined. More...
 
void set_on_new_response_callback (std::function< void(size_t)> callback)
 Set a callback to be called when each new response is received. More...
 
void clear_on_new_response_callback ()
 Unset the callback registered for new responses, if any.
 

Protected Types

using CallbackTypeValueVariant = std::tuple< CallbackType, SharedFuture, Promise >
 
using CallbackWithRequestTypeValueVariant = std::tuple< CallbackWithRequestType, SharedRequest, SharedFutureWithRequest, PromiseWithRequest >
 
using CallbackInfoVariant = std::variant< std::promise< SharedResponse >, CallbackTypeValueVariant, CallbackWithRequestTypeValueVariant >
 

Protected Member Functions

int64_t async_send_request_impl (const Request &request, CallbackInfoVariant value)
 
std::optional< CallbackInfoVariant > get_and_erase_pending_request (int64_t request_number)
 
- Protected Member Functions inherited from rclcpp::ClientBase
RCLCPP_PUBLIC bool wait_for_service_nanoseconds (std::chrono::nanoseconds timeout)
 
RCLCPP_PUBLIC rcl_node_tget_rcl_node_handle ()
 
RCLCPP_PUBLIC const rcl_node_tget_rcl_node_handle () const
 
RCLCPP_PUBLIC void set_on_new_response_callback (rcl_event_callback_t callback, const void *user_data)
 

Protected Attributes

std::unordered_map< int64_t, std::pair< std::chrono::time_point< std::chrono::system_clock >, CallbackInfoVariant > > pending_requests_
 
std::mutex pending_requests_mutex_
 
- Protected Attributes inherited from rclcpp::ClientBase
rclcpp::node_interfaces::NodeGraphInterface::WeakPtr node_graph_
 
std::shared_ptr< rcl_node_tnode_handle_
 
std::shared_ptr< rclcpp::Contextcontext_
 
rclcpp::Logger node_logger_
 
std::recursive_mutex callback_mutex_
 
std::function< void(size_t)> on_new_response_callback_ {nullptr}
 
std::shared_ptr< rcl_client_tclient_handle_
 
std::atomic< bool > in_use_by_wait_set_ {false}
 

Detailed Description

template<typename ServiceT>
class rclcpp::Client< ServiceT >

Definition at line 394 of file client.hpp.

Constructor & Destructor Documentation

◆ Client()

template<typename ServiceT >
rclcpp::Client< ServiceT >::Client ( rclcpp::node_interfaces::NodeBaseInterface node_base,
rclcpp::node_interfaces::NodeGraphInterface::SharedPtr  node_graph,
const std::string &  service_name,
rcl_client_options_t client_options 
)
inline

Default constructor.

The constructor for a Client is almost never called directly. Instead, clients should be instantiated through the function rclcpp::create_client().

Parameters
[in]node_baseNodeBaseInterface pointer that is used in part of the setup.
[in]node_graphThe node graph interface of the corresponding node.
[in]service_nameName of the topic to publish to.
[in]client_optionsoptions for the client.

Definition at line 478 of file client.hpp.

References rclcpp::expand_topic_or_service_name(), rclcpp::ClientBase::get_client_handle(), rcl_client_init(), rcl_node_get_name(), rcl_node_get_namespace(), RCL_RET_OK, and RCL_RET_SERVICE_NAME_INVALID.

Here is the call graph for this function:

Member Function Documentation

◆ async_send_request() [1/3]

template<typename ServiceT >
FutureAndRequestId rclcpp::Client< ServiceT >::async_send_request ( SharedRequest  request)
inline

Send a request to the service server.

This method returns a FutureAndRequestId instance that can be passed to Executor::spin_until_future_complete() to wait until it has been completed.

If the future never completes, e.g. the call to Executor::spin_until_future_complete() times out, Client::remove_pending_request() must be called to clean the client internal state. Not doing so will make the Client instance to use more memory each time a response is not received from the service server.

auto future = client->async_send_request(my_request);
if (
rclcpp::FutureReturnCode::TIMEOUT ==
executor->spin_until_future_complete(future, timeout))
{
client->remove_pending_request(future);
// handle timeout
} else {
handle_response(future.get());
}
void handle_response(std::shared_ptr< rmw_request_id_t > request_header, std::shared_ptr< void > response) override
Handle a server response.
Definition: client.hpp:558
Parameters
[in]requestrequest to be send.
Returns
a FutureAndRequestId instance.

Definition at line 620 of file client.hpp.

◆ async_send_request() [2/3]

template<typename ServiceT >
template<typename CallbackT , typename std::enable_if< rclcpp::function_traits::same_arguments< CallbackT, CallbackType >::value >::type * = nullptr>
SharedFutureAndRequestId rclcpp::Client< ServiceT >::async_send_request ( SharedRequest  request,
CallbackT &&  cb 
)
inline

Send a request to the service server and schedule a callback in the executor.

Similar to the previous overload, but a callback will automatically be called when a response is received.

If the callback is never called, because we never got a reply for the service server, remove_pending_request() has to be called with the returned request id or prune_pending_requests(). Not doing so will make the Client instance use more memory each time a response is not received from the service server. In this case, it's convenient to setup a timer to cleanup the pending requests. See for example the examples_rclcpp_async_client package in https://github.com/ros2/examples.

Parameters
[in]requestrequest to be send.
[in]cbcallback that will be called when we get a response for this request.
Returns
the request id representing the request just sent.

Definition at line 655 of file client.hpp.

◆ async_send_request() [3/3]

template<typename ServiceT >
template<typename CallbackT , typename std::enable_if< rclcpp::function_traits::same_arguments< CallbackT, CallbackWithRequestType >::value >::type * = nullptr>
SharedFutureWithRequestAndRequestId rclcpp::Client< ServiceT >::async_send_request ( SharedRequest  request,
CallbackT &&  cb 
)
inline

Send a request to the service server and schedule a callback in the executor.

Similar to the previous method, but you can get both the request and response in the callback.

Parameters
[in]requestrequest to be send.
[in]cbcallback that will be called when we get a response for this request.
Returns
the request id representing the request just sent.

Definition at line 686 of file client.hpp.

◆ configure_introspection()

template<typename ServiceT >
void rclcpp::Client< ServiceT >::configure_introspection ( Clock::SharedPtr  clock,
const QoS qos_service_event_pub,
rcl_service_introspection_state_t  introspection_state 
)
inline

Configure client introspection.

Parameters
[in]clockclock to use to generate introspection timestamps
[in]qos_service_event_pubQoS settings to use when creating the introspection publisher
[in]introspection_statethe state to set introspection to
Exceptions
anythingrclcpp::exceptions::throw_from_rcl_error can throw if it failed to configure introspection.

Definition at line 797 of file client.hpp.

References rclcpp::QoS::get_rmw_qos_profile(), rcl_publisher_options_s::qos, rcl_client_configure_service_introspection(), rcl_publisher_get_default_options(), and RCL_RET_OK.

Here is the call graph for this function:

◆ create_request_header()

template<typename ServiceT >
std::shared_ptr<rmw_request_id_t> rclcpp::Client< ServiceT >::create_request_header ( )
inlineoverridevirtual

Create a shared pointer with a rmw_request_id_t.

Returns
shared pointer with a rmw_request_id_t

Implements rclcpp::ClientBase.

Definition at line 545 of file client.hpp.

◆ create_response()

template<typename ServiceT >
std::shared_ptr<void> rclcpp::Client< ServiceT >::create_response ( )
inlineoverridevirtual

Create a shared pointer with the response type.

Returns
shared pointer with the response type

Implements rclcpp::ClientBase.

Definition at line 535 of file client.hpp.

◆ handle_response()

template<typename ServiceT >
void rclcpp::Client< ServiceT >::handle_response ( std::shared_ptr< rmw_request_id_t >  request_header,
std::shared_ptr< void >  response 
)
inlineoverridevirtual

Handle a server response.

Parameters
[in]request_headerused to check if the secuence number is valid
[in]responsemessage with the server response

Implements rclcpp::ClientBase.

Definition at line 558 of file client.hpp.

◆ prune_pending_requests()

template<typename ServiceT >
size_t rclcpp::Client< ServiceT >::prune_pending_requests ( )
inline

Clean all pending requests.

Returns
number of pending requests that were removed.

Definition at line 759 of file client.hpp.

◆ prune_requests_older_than()

template<typename ServiceT >
template<typename AllocatorT = std::allocator<int64_t>>
size_t rclcpp::Client< ServiceT >::prune_requests_older_than ( std::chrono::time_point< std::chrono::system_clock >  time_point,
std::vector< int64_t, AllocatorT > *  pruned_requests = nullptr 
)
inline

Clean all pending requests older than a time_point.

Parameters
[in]time_pointRequests that were sent before this point are going to be removed.
[in,out]pruned_requestsRemoved requests id will be pushed to the vector if a pointer is provided.
Returns
number of pending requests that were removed.

Definition at line 776 of file client.hpp.

◆ remove_pending_request() [1/4]

template<typename ServiceT >
bool rclcpp::Client< ServiceT >::remove_pending_request ( const FutureAndRequestId future)
inline

Cleanup a pending request.

Convenient overload, same as:

Client::remove_pending_request(this, future.request_id).

Definition at line 725 of file client.hpp.

References rclcpp::Client< ServiceT >::remove_pending_request().

Here is the call graph for this function:

◆ remove_pending_request() [2/4]

template<typename ServiceT >
bool rclcpp::Client< ServiceT >::remove_pending_request ( const SharedFutureAndRequestId future)
inline

Cleanup a pending request.

Convenient overload, same as:

Client::remove_pending_request(this, future.request_id).

Definition at line 737 of file client.hpp.

References rclcpp::Client< ServiceT >::remove_pending_request().

Here is the call graph for this function:

◆ remove_pending_request() [3/4]

template<typename ServiceT >
bool rclcpp::Client< ServiceT >::remove_pending_request ( const SharedFutureWithRequestAndRequestId future)
inline

Cleanup a pending request.

Convenient overload, same as:

Client::remove_pending_request(this, future.request_id).

Definition at line 749 of file client.hpp.

References rclcpp::Client< ServiceT >::remove_pending_request().

Here is the call graph for this function:

◆ remove_pending_request() [4/4]

template<typename ServiceT >
bool rclcpp::Client< ServiceT >::remove_pending_request ( int64_t  request_id)
inline

Cleanup a pending request.

This notifies the client that we have waited long enough for a response from the server to come, we have given up and we are not waiting for a response anymore.

Not calling this will make the client start using more memory for each request that never got a reply from the server.

Parameters
[in]request_idrequest id returned by async_send_request().
Returns
true when a pending request was removed, false if not (e.g. a response was received).

Definition at line 712 of file client.hpp.

Referenced by rclcpp::Client< ServiceT >::remove_pending_request().

Here is the caller graph for this function:

◆ take_response()

template<typename ServiceT >
bool rclcpp::Client< ServiceT >::take_response ( typename ServiceT::Response &  response_out,
rmw_request_id_t &  request_header_out 
)
inline

Take the next response for this client.

See also
ClientBase::take_type_erased_response().
Parameters
[out]response_outThe reference to a Service Response into which the middleware will copy the response being taken.
[out]request_header_outThe request header to be filled by the middleware when taking, and which can be used to associte the response to a specific request.
Returns
true if the response was taken, otherwise false.
Exceptions
rclcpp::exceptions::RCLErrorbased exceptions if the underlying rcl function fail.

Definition at line 525 of file client.hpp.

References rclcpp::ClientBase::take_type_erased_response().

Here is the call graph for this function:

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