mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
service_client_handler.h
Go to the documentation of this file.
1 
5 #ifndef SERVICE_CLIENT_HANDLER_H
6 #define SERVICE_CLIENT_HANDLER_H
7 
8 #include <ros/ros.h>
9 #include <ros/package.h>
10 
11 #include <string>
12 #include <future>
13 #include <mutex>
14 
15 namespace mrs_lib
16 {
17 
18 /* class ServiceClientHandler_impl //{ */
19 
23 template <class ServiceType>
25 
26 public:
31 
36 
43  ServiceClientHandler_impl(ros::NodeHandle& nh, const std::string& address);
44 
52  bool call(ServiceType& srv);
53 
62  bool call(ServiceType& srv, const int& attempts);
63 
73  bool call(ServiceType& srv, const int& attempts, const double& repeat_delay);
74 
82  std::future<ServiceType> callAsync(ServiceType& srv);
83 
92  std::future<ServiceType> callAsync(ServiceType& srv, const int& attempts);
93 
103  std::future<ServiceType> callAsync(ServiceType& srv, const int& attempts, const double& repeat_delay);
104 
105 private:
106  ros::ServiceClient service_client_;
107  std::mutex mutex_service_client_;
108  std::atomic<bool> service_initialized_;
109 
110  std::string _address_;
111 
112  ServiceType async_data_;
113  int async_attempts_;
114  double async_repeat_delay_;
115  std::mutex mutex_async_;
116 
117  ServiceType asyncRun(void);
118 };
119 
120 //}
121 
122 /* class ServiceClientHandler //{ */
123 
127 template <class ServiceType>
129 
130 public:
135 
140 
149 
156 
163  ServiceClientHandler(ros::NodeHandle& nh, const std::string& address);
164 
171  void initialize(ros::NodeHandle& nh, const std::string& address);
172 
180  bool call(ServiceType& srv);
181 
190  bool call(ServiceType& srv, const int& attempts);
191 
201  bool call(ServiceType& srv, const int& attempts, const double& repeat_delay);
202 
210  std::future<ServiceType> callAsync(ServiceType& srv);
211 
220  std::future<ServiceType> callAsync(ServiceType& srv, const int& attempts);
221 
231  std::future<ServiceType> callAsync(ServiceType& srv, const int& attempts, const double& repeat_delay);
232 
233 private:
234  std::shared_ptr<ServiceClientHandler_impl<ServiceType>> impl_;
235 };
236 
237 //}
238 
239 } // namespace mrs_lib
240 
241 #include <mrs_lib/impl/service_client_handler.hpp>
242 
243 #endif // SERVICE_CLIENT_HANDLER_H
mrs_lib::ServiceClientHandler::~ServiceClientHandler
~ServiceClientHandler(void)
generic destructor
Definition: service_client_handler.h:139
mrs_lib::ServiceClientHandler
user wrapper of the service client handler implementation
Definition: service_client_handler.h:128
mrs_lib::ServiceClientHandler_impl
implementation of the service client handler
Definition: service_client_handler.h:24
mrs_lib::ServiceClientHandler_impl::ServiceClientHandler_impl
ServiceClientHandler_impl(void)
default constructor
Definition: service_client_handler.hpp:14
mrs_lib::ServiceClientHandler::ServiceClientHandler
ServiceClientHandler(void)
generic constructor
Definition: service_client_handler.h:134
mrs_lib
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition: attitude_converter.h:29
mrs_lib::ServiceClientHandler::callAsync
std::future< ServiceType > callAsync(ServiceType &srv)
asynchronous call
Definition: service_client_handler.hpp:284
mrs_lib::ServiceClientHandler_impl::callAsync
std::future< ServiceType > callAsync(ServiceType &srv)
asynchronous service call
Definition: service_client_handler.hpp:123
mrs_lib::ServiceClientHandler_impl::~ServiceClientHandler_impl
~ServiceClientHandler_impl(void)
default destructor
Definition: service_client_handler.h:35
mrs_lib::ServiceClientHandler::initialize
void initialize(ros::NodeHandle &nh, const std::string &address)
initializer
Definition: service_client_handler.hpp:244
mrs_lib::ServiceClientHandler_impl::call
bool call(ServiceType &srv)
"classic" synchronous service call
Definition: service_client_handler.hpp:43
mrs_lib::ServiceClientHandler::call
bool call(ServiceType &srv)
"standard" synchronous call
Definition: service_client_handler.hpp:254
mrs_lib::ServiceClientHandler::operator=
ServiceClientHandler & operator=(const ServiceClientHandler &other)
operator=
Definition: service_client_handler.hpp:204