1 #ifndef SERVICE_CLIENT_HANDLER_HPP
2 #define SERVICE_CLIENT_HANDLER_HPP
13 template <
class ServiceType>
21 template <
class ServiceType>
25 std::scoped_lock lock(mutex_service_client_);
27 service_client_ = nh.serviceClient<ServiceType>(address);
35 service_initialized_ =
true;
42 template <
class ServiceType>
45 if (!service_initialized_) {
49 return service_client_.call(srv);
56 template <
class ServiceType>
59 if (!service_initialized_) {
63 std::scoped_lock lock(mutex_service_client_);
68 while (!success && ros::ok()) {
70 success = service_client_.call(srv);
73 ROS_ERROR(
"[%s]: failed to call service to '%s'", ros::this_node::getName().c_str(), _address_.c_str());
76 if (++counter >= attempts) {
88 template <
class ServiceType>
91 if (!service_initialized_) {
95 std::scoped_lock lock(mutex_service_client_);
100 while (!success && ros::ok()) {
102 success = service_client_.call(srv);
105 ROS_ERROR(
"[%s]: failed to call service to '%s'", ros::this_node::getName().c_str(), _address_.c_str());
108 if (++counter >= attempts) {
112 ros::Duration(repeat_delay).sleep();
122 template <
class ServiceType>
126 std::scoped_lock lock(mutex_async_);
130 async_repeat_delay_ = 0;
133 return std::async(std::launch::async, &ServiceClientHandler_impl::asyncRun,
this);
140 template <
class ServiceType>
144 std::scoped_lock lock(mutex_async_);
147 async_attempts_ = attempts;
148 async_repeat_delay_ = 0;
151 return std::async(std::launch::async, &ServiceClientHandler_impl::asyncRun,
this);
158 template <
class ServiceType>
162 std::scoped_lock lock(mutex_async_);
165 async_attempts_ = attempts;
166 async_repeat_delay_ = repeat_delay;
169 return std::async(std::launch::async, &ServiceClientHandler_impl::asyncRun,
this);
176 template <
class ServiceType>
179 ServiceType async_data;
183 std::scoped_lock lock(mutex_async_);
185 async_data = async_data_;
186 async_attempts = async_attempts_;
187 async_repeat_delay_ = async_repeat_delay_;
190 call(async_data, async_attempts, async_repeat_delay_);
203 template <
class ServiceType>
206 if (
this == &other) {
211 this->impl_ = other.impl_;
221 template <
class ServiceType>
225 this->impl_ = other.impl_;
233 template <
class ServiceType>
236 impl_ = std::make_shared<ServiceClientHandler_impl<ServiceType>>(nh, address);
243 template <
class ServiceType>
246 impl_ = std::make_shared<ServiceClientHandler_impl<ServiceType>>(nh, address);
253 template <
class ServiceType>
256 return impl_->call(srv);
263 template <
class ServiceType>
266 return impl_->call(srv, attempts);
273 template <
class ServiceType>
276 return impl_->call(srv, attempts, repeat_delay);
283 template <
class ServiceType>
286 std::future<ServiceType> res = impl_->callAsync(srv);
295 template <
class ServiceType>
298 std::future<ServiceType> res = impl_->callAsync(srv, attempts);
307 template <
class ServiceType>
310 std::future<ServiceType> res = impl_->callAsync(srv, attempts, repeat_delay);
319 #endif // SERVICE_CLIENT_HANDLER_HPP