managed_serviceserver.h
Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2018, University of Luxembourg
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of University of Luxembourg nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  * Author: Maciej Zurad
00036  *********************************************************************/
00046 #ifndef ROBOT_ACTIVITY_RESOURCE_MANAGED_SERVICESERVER_H
00047 #define ROBOT_ACTIVITY_RESOURCE_MANAGED_SERVICESERVER_H
00048 
00049 #include <ros/ros.h>
00050 #include <ros/console.h>
00051 
00052 #include <robot_activity/resource/managed_resource.h>
00053 
00054 #include <string>
00055 
00056 namespace robot_activity
00057 {
00058 namespace resource
00059 {
00060 
00071 class ManagedServiceServer : public Managed<ManagedServiceServer, ros::ServiceServer>
00072 {
00073 public:
00089   using Managed<ManagedServiceServer, ros::ServiceServer>::Managed;
00090 
00102   void advertiseService(const ros::NodeHandlePtr& node_handle)
00103   {
00104     acquire(node_handle);
00105   }
00106 
00113   void shutdown()
00114   {
00115     release();
00116   }
00117 
00123   template <typename ...Args>
00124   using ServiceCallback = boost::function<bool(Args...)>;
00125 
00141   template<class MReq, class MRes>
00142   LazyAcquirer makeLazyAcquirer(
00143     const std::string& service,
00144     const ServiceCallback<MReq&, MRes&>& callback,
00145     const ros::VoidConstPtr& tracked_object = ros::VoidConstPtr()) const
00146   {
00147     ROS_DEBUG("makeLazyAcquirer ServiceCallback<MReq&, MRes&>&");
00148     return [ = ](const ros::NodeHandlePtr & nh) -> ros::ServiceServer
00149     {
00150       ROS_DEBUG("Advertising...");
00151       return nh->advertiseService(
00152         service,
00153         static_cast<ServiceCallback<MReq&, MRes&>>(wrapServiceCallback(callback)),
00154         tracked_object);
00155     };
00156   }
00157 
00172   template<class ServiceEvent>
00173   LazyAcquirer makeLazyAcquirer(
00174     const std::string& service,
00175     const ServiceCallback<ServiceEvent&>& callback,
00176     const ros::VoidConstPtr& tracked_object = ros::VoidConstPtr()) const
00177   {
00178     ROS_DEBUG("makeLazyAcquirer ServiceEventCallback<ServiceEvent&>&");
00179     return [ = ](const ros::NodeHandlePtr & nh) -> ros::ServiceServer
00180     {
00181       ROS_DEBUG("Advertising...");
00182       return nh->advertiseService(
00183         service,
00184         static_cast<ServiceCallback<ServiceEvent&>>(wrapServiceCallback(callback)),
00185         tracked_object);
00186     };
00187   }
00188 
00205   template<class T, class MReq, class MRes>
00206   LazyAcquirer makeLazyAcquirer(
00207     const std::string& service,
00208     bool(T::*srv_func)(MReq&, MRes&),
00209     T *obj) const
00210   {
00211     ServiceCallback<MReq&, MRes&> callback = boost::bind(srv_func, obj, _1, _2);
00212     return makeLazyAcquirer(service, callback);
00213   }
00214 
00232   template<class T, class MReq, class MRes>
00233   LazyAcquirer makeLazyAcquirer(
00234     const std::string& service,
00235     bool(T::*srv_func)(ros::ServiceEvent<MReq, MRes>&),
00236     T *obj) const
00237   {
00238     ServiceCallback<ros::ServiceEvent<MReq, MRes>&> callback
00239       = boost::bind(srv_func, obj, _1);
00240     return makeLazyAcquirer(service, callback);
00241   }
00242 
00262   template<class T, class MReq, class MRes>
00263   LazyAcquirer makeLazyAcquirer(
00264     const std::string& service,
00265     bool(T::*srv_func)(MReq &, MRes &),
00266     const boost::shared_ptr<T>& obj) const
00267   {
00268     ServiceCallback<MReq&, MRes&> callback = boost::bind(srv_func, obj.get(), _1, _2);
00269     return makeLazyAcquirer(service, callback, obj);
00270   }
00271 
00291   template<class T, class MReq, class MRes>
00292   LazyAcquirer makeLazyAcquirer(
00293     const std::string& service,
00294     bool(T::*srv_func)(ros::ServiceEvent<MReq, MRes>&),
00295     const boost::shared_ptr<T>& obj) const
00296   {
00297     ServiceCallback<ros::ServiceEvent<MReq, MRes>&> callback
00298       = boost::bind(srv_func, obj.get(), _1);
00299     return makeLazyAcquirer(service, callback, obj);
00300   }
00301 
00317   template<class MReq, class MRes>
00318   LazyAcquirer makeLazyAcquirer(
00319     const std::string& service,
00320     bool(*srv_func)(MReq&, MRes&)) const
00321   {
00322     ServiceCallback<MReq&, MRes&> callback = boost::bind(srv_func);
00323     return makeLazyAcquirer(service, callback);
00324   }
00325 
00340   template<class MReq, class MRes>
00341   LazyAcquirer makeLazyAcquirer(
00342     const std::string& service,
00343     bool(*srv_func)(ros::ServiceEvent<MReq, MRes>&)) const
00344   {
00345     ServiceCallback<ros::ServiceEvent<MReq, MRes>&> callback
00346       = boost::bind(srv_func);
00347     return makeLazyAcquirer(service, callback);
00348   }
00349 
00350 
00351   /* TODO - remove if unneccessary
00352   template<class MReq, class MRes>
00353   LazyAcquirer makeLazyAcquirer(
00354     const std::string& service,
00355     const boost::function<bool(MReq&, MRes&)>& callback,
00356     const ros::VoidConstPtr& tracked_object = ros::VoidConstPtr())
00357   {
00358     AdvertiseServiceOptions ops;
00359     ops.template init<MReq, MRes>(service, callback);
00360     ops.tracked_object = tracked_object;
00361     return makeLazyAcquirer(ops);
00362   }
00363 
00364   template<class S>
00365   LazyAcquirer makeLazyAcquirer(
00366     const std::string& service,
00367     const boost::function<bool(S&)>& callback,
00368     const ros::VoidConstPtr& tracked_object = ros::VoidConstPtr())
00369   {
00370     AdvertiseServiceOptions ops;
00371     ops.template initBySpecType<S>(service, callback);
00372     ops.tracked_object = tracked_object;
00373     return makeLazyAcquirer(ops);
00374   }
00375   */
00376 private:
00385   template <typename ...Args>
00386   ServiceCallback<Args...> wrapServiceCallback(
00387     const ServiceCallback<Args...>& callback) const
00388   {
00389     return [this, &callback](Args ... args) -> bool
00390     {
00391       if (paused_)
00392       {
00393         ROS_DEBUG("service is paused!");
00394         return false;
00395       }
00396       return callback(std::forward<Args>(args)...);
00397     };
00398   }
00399 };
00400 
00401 }  // namespace resource
00402 }  // namespace robot_activity
00403 
00404 #endif  // ROBOT_ACTIVITY_RESOURCE_MANAGED_SERVICESERVER_H


robot_activity
Author(s): Maciej ZURAD
autogenerated on Thu Jun 6 2019 18:10:04