managed_serviceserver.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2018, University of Luxembourg
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of University of Luxembourg nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * Author: Maciej Zurad
36  *********************************************************************/
46 #ifndef ROBOT_ACTIVITY_RESOURCE_MANAGED_SERVICESERVER_H
47 #define ROBOT_ACTIVITY_RESOURCE_MANAGED_SERVICESERVER_H
48 
49 #include <ros/ros.h>
50 #include <ros/console.h>
51 
53 
54 #include <string>
55 
56 namespace robot_activity
57 {
58 namespace resource
59 {
60 
71 class ManagedServiceServer : public Managed<ManagedServiceServer, ros::ServiceServer>
72 {
73 public:
90 
102  void advertiseService(const ros::NodeHandlePtr& node_handle)
103  {
104  acquire(node_handle);
105  }
106 
113  void shutdown()
114  {
115  release();
116  }
117 
123  template <typename ...Args>
124  using ServiceCallback = boost::function<bool(Args...)>;
125 
141  template<class MReq, class MRes>
143  const std::string& service,
144  const ServiceCallback<MReq&, MRes&>& callback,
145  const ros::VoidConstPtr& tracked_object = ros::VoidConstPtr()) const
146  {
147  ROS_DEBUG("makeLazyAcquirer ServiceCallback<MReq&, MRes&>&");
148  return [ = ](const ros::NodeHandlePtr & nh) -> ros::ServiceServer
149  {
150  ROS_DEBUG("Advertising...");
151  return nh->advertiseService(
152  service,
153  static_cast<ServiceCallback<MReq&, MRes&>>(wrapServiceCallback(callback)),
154  tracked_object);
155  };
156  }
157 
172  template<class ServiceEvent>
174  const std::string& service,
175  const ServiceCallback<ServiceEvent&>& callback,
176  const ros::VoidConstPtr& tracked_object = ros::VoidConstPtr()) const
177  {
178  ROS_DEBUG("makeLazyAcquirer ServiceEventCallback<ServiceEvent&>&");
179  return [ = ](const ros::NodeHandlePtr & nh) -> ros::ServiceServer
180  {
181  ROS_DEBUG("Advertising...");
182  return nh->advertiseService(
183  service,
184  static_cast<ServiceCallback<ServiceEvent&>>(wrapServiceCallback(callback)),
185  tracked_object);
186  };
187  }
188 
205  template<class T, class MReq, class MRes>
207  const std::string& service,
208  bool(T::*srv_func)(MReq&, MRes&),
209  T *obj) const
210  {
211  ServiceCallback<MReq&, MRes&> callback = boost::bind(srv_func, obj, _1, _2);
212  return makeLazyAcquirer(service, callback);
213  }
214 
232  template<class T, class MReq, class MRes>
234  const std::string& service,
235  bool(T::*srv_func)(ros::ServiceEvent<MReq, MRes>&),
236  T *obj) const
237  {
239  = boost::bind(srv_func, obj, _1);
240  return makeLazyAcquirer(service, callback);
241  }
242 
262  template<class T, class MReq, class MRes>
264  const std::string& service,
265  bool(T::*srv_func)(MReq &, MRes &),
266  const boost::shared_ptr<T>& obj) const
267  {
268  ServiceCallback<MReq&, MRes&> callback = boost::bind(srv_func, obj.get(), _1, _2);
269  return makeLazyAcquirer(service, callback, obj);
270  }
271 
291  template<class T, class MReq, class MRes>
293  const std::string& service,
294  bool(T::*srv_func)(ros::ServiceEvent<MReq, MRes>&),
295  const boost::shared_ptr<T>& obj) const
296  {
298  = boost::bind(srv_func, obj.get(), _1);
299  return makeLazyAcquirer(service, callback, obj);
300  }
301 
317  template<class MReq, class MRes>
319  const std::string& service,
320  bool(*srv_func)(MReq&, MRes&)) const
321  {
322  ServiceCallback<MReq&, MRes&> callback = boost::bind(srv_func);
323  return makeLazyAcquirer(service, callback);
324  }
325 
340  template<class MReq, class MRes>
342  const std::string& service,
343  bool(*srv_func)(ros::ServiceEvent<MReq, MRes>&)) const
344  {
346  = boost::bind(srv_func);
347  return makeLazyAcquirer(service, callback);
348  }
349 
350 
351  /* TODO - remove if unneccessary
352  template<class MReq, class MRes>
353  LazyAcquirer makeLazyAcquirer(
354  const std::string& service,
355  const boost::function<bool(MReq&, MRes&)>& callback,
356  const ros::VoidConstPtr& tracked_object = ros::VoidConstPtr())
357  {
358  AdvertiseServiceOptions ops;
359  ops.template init<MReq, MRes>(service, callback);
360  ops.tracked_object = tracked_object;
361  return makeLazyAcquirer(ops);
362  }
363 
364  template<class S>
365  LazyAcquirer makeLazyAcquirer(
366  const std::string& service,
367  const boost::function<bool(S&)>& callback,
368  const ros::VoidConstPtr& tracked_object = ros::VoidConstPtr())
369  {
370  AdvertiseServiceOptions ops;
371  ops.template initBySpecType<S>(service, callback);
372  ops.tracked_object = tracked_object;
373  return makeLazyAcquirer(ops);
374  }
375  */
376 private:
385  template <typename ...Args>
387  const ServiceCallback<Args...>& callback) const
388  {
389  return [this, &callback](Args ... args) -> bool
390  {
391  if (paused_)
392  {
393  ROS_DEBUG("service is paused!");
394  return false;
395  }
396  return callback(std::forward<Args>(args)...);
397  };
398  }
399 };
400 
401 } // namespace resource
402 } // namespace robot_activity
403 
404 #endif // ROBOT_ACTIVITY_RESOURCE_MANAGED_SERVICESERVER_H
ServiceCallback< Args... > wrapServiceCallback(const ServiceCallback< Args... > &callback) const
Wraps the service callback.
std::atomic< bool > paused_
Atomic flag specifing whether the resource is paused or not.
Implementation of Managed class for ros::ServiceServer.
LazyAcquirer makeLazyAcquirer(const std::string &service, bool(T::*srv_func)(ros::ServiceEvent< MReq, MRes > &), T *obj) const
Creates a function that advertises a ROS service when called.
std::function< ros::ServiceServer(const ros::NodeHandlePtr &)> LazyAcquirer
LazyAcquirer makeLazyAcquirer(const std::string &service, bool(T::*srv_func)(ros::ServiceEvent< MReq, MRes > &), const boost::shared_ptr< T > &obj) const
Creates a function that advertises a ROS service when called.
LazyAcquirer makeLazyAcquirer(const std::string &service, const ServiceCallback< MReq &, MRes & > &callback, const ros::VoidConstPtr &tracked_object=ros::VoidConstPtr()) const
Creates a function that advertises a ROS service when called.
Wrapper around ROS resources, such as ros::Subscriber and ros::ServiceServer.
void release()
Releases the resource if it&#39;s already acquired. shutdown() method in case of ros::Subscriber and ros:...
LazyAcquirer makeLazyAcquirer(const std::string &service, const ServiceCallback< ServiceEvent & > &callback, const ros::VoidConstPtr &tracked_object=ros::VoidConstPtr()) const
Creates a function that advertises a ROS service when called.
boost::function< bool(Args...)> ServiceCallback
Typedef for ROS service callbacks.
LazyAcquirer makeLazyAcquirer(const std::string &service, bool(*srv_func)(ros::ServiceEvent< MReq, MRes > &)) const
Creates a function that advertises a ROS service when called.
LazyAcquirer makeLazyAcquirer(const std::string &service, bool(*srv_func)(MReq &, MRes &)) const
Creates a function that advertises a ROS service when called.
LazyAcquirer makeLazyAcquirer(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj) const
Creates a function that advertises a ROS service when called.
void advertiseService(const ros::NodeHandlePtr &node_handle)
Advertises the ROS service given a node handle.
LazyAcquirer makeLazyAcquirer(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), const boost::shared_ptr< T > &obj) const
Creates a function that advertises a ROS service when called.
void acquire(const ros::NodeHandlePtr &node_handle)
Acquires the resource if it&#39;s not already acquired.
Managed<Derived,R> class implements a base class which manages ROS resources, such as ros::Subscriber...
#define ROS_DEBUG(...)


robot_activity
Author(s): Maciej ZURAD
autogenerated on Mon Jun 10 2019 14:33:22