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::GenericClient Class Reference
Inheritance diagram for rclcpp::GenericClient:
Inheritance graph
[legend]
Collaboration diagram for rclcpp::GenericClient:
Collaboration graph
[legend]

Classes

struct  FutureAndRequestId
 A convenient GenericClient::Future and request id pair. More...
 
struct  SharedFutureAndRequestId
 A convenient GenericClient::SharedFuture and request id pair. More...
 

Public Types

using Request = void *
 
using Response = void *
 
using SharedResponse = std::shared_ptr< void >
 
using Promise = std::promise< SharedResponse >
 
using SharedPromise = std::shared_ptr< Promise >
 
using Future = std::future< SharedResponse >
 
using SharedFuture = std::shared_future< SharedResponse >
 
using CallbackType = std::function< void(SharedFuture)>
 

Public Member Functions

 GenericClient (rclcpp::node_interfaces::NodeBaseInterface *node_base, rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph, const std::string &service_name, const std::string &service_type, rcl_client_options_t &client_options)
 
RCLCPP_PUBLIC SharedResponse create_response () override
 
RCLCPP_PUBLIC std::shared_ptr< rmw_request_id_t > create_request_header () override
 
RCLCPP_PUBLIC void handle_response (std::shared_ptr< rmw_request_id_t > request_header, std::shared_ptr< void > response) override
 
RCLCPP_PUBLIC FutureAndRequestId async_send_request (const Request 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 (const Request request, CallbackT &&cb)
 Send a request to the service server and schedule a callback in the executor. 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...
 
RCLCPP_PUBLIC size_t prune_pending_requests ()
 Clean all pending requests. More...
 
RCLCPP_PUBLIC bool remove_pending_request (int64_t request_id)
 Cleanup a pending request. More...
 
RCLCPP_PUBLIC bool remove_pending_request (const FutureAndRequestId &future)
 Cleanup a pending request. More...
 
RCLCPP_PUBLIC bool remove_pending_request (const SharedFutureAndRequestId &future)
 Cleanup a pending request. More...
 
RCLCPP_PUBLIC bool take_response (Response response_out, rmw_request_id_t &request_header_out)
 Take the next response for this client. 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 CallbackInfoVariant = std::variant< std::promise< SharedResponse >, CallbackTypeValueVariant >
 

Protected Member Functions

RCLCPP_PUBLIC 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::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

Definition at line 36 of file generic_client.hpp.

Member Function Documentation

◆ async_send_request() [1/2]

GenericClient::FutureAndRequestId rclcpp::GenericClient::async_send_request ( const Request  request)

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, GenericClient::remove_pending_request() must be called to clean the client internal state. Not doing so will make the GenericClient instance to use more memory each time a response is not received from the service server.

auto future = generic_client->async_send_request(my_request);
if (
rclcpp::FutureReturnCode::TIMEOUT ==
executor->spin_until_future_complete(future, timeout))
{
generic_client->remove_pending_request(future);
// handle timeout
} else {
handle_response(future.get());
}
Parameters
[in]requestrequest to be send.
Returns
a FutureAndRequestId instance.

Definition at line 167 of file generic_client.cpp.

◆ async_send_request() [2/2]

template<typename CallbackT , typename std::enable_if< rclcpp::function_traits::same_arguments< CallbackT, CallbackType >::value >::type * = nullptr>
SharedFutureAndRequestId rclcpp::GenericClient::async_send_request ( const Request  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 GenericClient 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.

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 175 of file generic_client.hpp.

◆ prune_pending_requests()

size_t rclcpp::GenericClient::prune_pending_requests ( )

Clean all pending requests.

Returns
number of pending requests that were removed.

Definition at line 123 of file generic_client.cpp.

◆ prune_requests_older_than()

template<typename AllocatorT = std::allocator<int64_t>>
size_t rclcpp::GenericClient::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 197 of file generic_client.hpp.

◆ remove_pending_request() [1/3]

bool rclcpp::GenericClient::remove_pending_request ( const FutureAndRequestId future)

Cleanup a pending request.

Convenient overload, same as:

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

Definition at line 139 of file generic_client.cpp.

◆ remove_pending_request() [2/3]

bool rclcpp::GenericClient::remove_pending_request ( const SharedFutureAndRequestId future)

Cleanup a pending request.

Convenient overload, same as:

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

Definition at line 145 of file generic_client.cpp.

◆ remove_pending_request() [3/3]

bool rclcpp::GenericClient::remove_pending_request ( int64_t  request_id)

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 132 of file generic_client.cpp.

◆ take_response()

RCLCPP_PUBLIC bool rclcpp::GenericClient::take_response ( 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 associate 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 269 of file generic_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 files: