mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
mrs_lib::ServiceClient< ServiceType > Class Template Reference

rclcpp::Client wrapper with coroutines support. More...

#include <service_client.hpp>

Public Member Functions

 ServiceClient (const ServiceClientOptions &options, std::string_view service_name)
 Construct the client with the specified options.
Task< std::expected< std::shared_ptr< Response >, std::string > > call (const std::shared_ptr< Request > &request)
 Coroutine call of the service.
std::expected< std::future< std::shared_ptr< Response > >, std::string > call_async (const std::shared_ptr< Request > &request)
 Async call of the service.
std::string get_service_name () const
 Returns the resolved (full) name of the service handled by this service client.
std::string get_unresolved_service_name () const
 Returns the unresolved name of the service handled by this service client.
template<typename RepT = int64_t, typename RatioT = std::milli>
bool wait_for_service (std::chrono::duration< RepT, RatioT > timeout)
 Wait for the service to become available.
bool is_service_ready () const
 Check if the service is available.
size_t prune_pending_requests ()
 Remove all pending requests.

Detailed Description

template<class ServiceType>
class mrs_lib::ServiceClient< ServiceType >

rclcpp::Client wrapper with coroutines support.

Template Parameters
ServiceTypeType of the service this client will call.

When using services, it is often useful to wait for the response right after sending it. ServiceClient makes this pattern simpler by allowing the use of coroutines. It provides ServiceClient::call, a coroutine interface for calling the service.

Constructor & Destructor Documentation

◆ ServiceClient()

template<class ServiceType>
mrs_lib::ServiceClient< ServiceType >::ServiceClient ( const ServiceClientOptions & options,
std::string_view service_name )

Construct the client with the specified options.

Parameters
optionsOptions to configure the service client.
service_nameThe name of the service to call using this client.

Member Function Documentation

◆ call()

template<class ServiceType>
auto mrs_lib::ServiceClient< ServiceType >::call ( const std::shared_ptr< Request > & request)

Coroutine call of the service.

Parameters
requestRequest to send to the service server.
Returns
Response of the server if the call succeeded, error message otherwise.

◆ call_async()

template<class ServiceType>
auto mrs_lib::ServiceClient< ServiceType >::call_async ( const std::shared_ptr< Request > & request)

Async call of the service.

Parameters
requestRequest to send to the service server.
Returns
Future that will contain the server response if successful, error message otherwise.

When the future is returned, it is up to the user to check, if the service server responded. This can be done for example by checking the readiness of the future in a timer callback.

Warning
If the future is not ready, calling get on the future will block the current thread until the response is sent. This may cause deadlocks! (Eg. when service client is on the same mutually exclusive callback group as the currently running function)

◆ get_service_name()

template<class ServiceType>
std::string mrs_lib::ServiceClient< ServiceType >::get_service_name ( ) const
nodiscard

Returns the resolved (full) name of the service handled by this service client.

Returns
The name of the handled service.

◆ get_unresolved_service_name()

template<class ServiceType>
std::string mrs_lib::ServiceClient< ServiceType >::get_unresolved_service_name ( ) const
nodiscard

Returns the unresolved name of the service handled by this service client.

Returns
The name of the handled service.

◆ is_service_ready()

template<class ServiceType>
bool mrs_lib::ServiceClient< ServiceType >::is_service_ready ( ) const
nodiscard

Check if the service is available.

Returns
true if the service is available, false otherwise.

◆ prune_pending_requests()

template<class ServiceType>
size_t mrs_lib::ServiceClient< ServiceType >::prune_pending_requests ( )

Remove all pending requests.

Returns
Number of pending requests that were removed.
Warning
Calling this will not wake up threads that are waiting on future::get. These threads will be deadlocked, unless woken by some other means. Coroutines waiting on ServiceClient::call will be cancelled.

◆ wait_for_service()

template<class ServiceType>
template<typename RepT, typename RatioT>
bool mrs_lib::ServiceClient< ServiceType >::wait_for_service ( std::chrono::duration< RepT, RatioT > timeout)
nodiscard

Wait for the service to become available.

Template Parameters
RepTArithmetic type representing the number of ticks.
RatioTstd::ratio representing the tick period.
Parameters
timeoutMaximum time to wait for the service to be available.
Returns
true if the service became available within the timeout, false otherwise.

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