00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
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 }
00402 }
00403
00404 #endif // ROBOT_ACTIVITY_RESOURCE_MANAGED_SERVICESERVER_H