topic_service_server.h
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2018, Southwest Research Institute® (SwRI®)
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //     * Redistributions of source code must retain the above copyright
00009 //       notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //     * Neither the name of Southwest Research Institute® (SwRI®) nor the
00014 //       names of its contributors may be used to endorse or promote products
00015 //       derived from this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 // ARE DISCLAIMED. IN NO EVENT SHALL SOUTHWEST RESEARCH INSTITUTE BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //
00028 // *****************************************************************************
00029 #ifndef SWRI_ROSCPP_TOPIC_SERVICE_SERVER_H_
00030 #define SWRI_ROSCPP_TOPIC_SERVICE_SERVER_H_
00031 
00032 #include <ros/node_handle.h>
00033 
00034 namespace swri
00035 {
00036 class ImplRoot
00037 {
00038 public:
00039   virtual ~ImplRoot()
00040   {
00041 
00042   }
00043 };
00044 
00045 template<class MReq, class MRes, class T>
00046 class TopicServiceServerImpl: public ImplRoot
00047 {
00048   ros::Subscriber request_sub_;
00049   ros::Publisher response_pub_;
00050 
00051   bool(T::*callback_)(const MReq &, MRes &);
00052   T* obj_;
00053 
00054 public:
00055   void initialize(ros::NodeHandle &nh,
00056               const std::string &service,
00057               bool(T::*srv_func)(const MReq &, MRes &),
00058               T *obj)
00059   {
00060     callback_ = srv_func;
00061     obj_ = obj;
00062 
00063     ros::NodeHandle pnh("~");
00064 
00065     std::string rservice = nh.resolveName(service);
00066 
00067     response_pub_ = nh.advertise<MRes>(rservice + "/response", 10);
00068     request_sub_ = nh.subscribe(rservice + "/request", 10, &TopicServiceServerImpl<MReq, MRes, T>::request_callback, this);
00069   }
00070 
00071 private:
00072 
00073   void request_callback(const MReq& message)
00074   {
00075     ROS_DEBUG("Got request from %s with sequence %i", message.srv_header.sender.c_str(), message.srv_header.sequence);
00076 
00077     MRes response;
00078 
00079     bool result = (obj_->*callback_)(message, response);
00080     response.srv_header.result = result;
00081     response.srv_header.sequence = message.srv_header.sequence;
00082     response.srv_header.sender = message.srv_header.sender;
00083     response_pub_.publish(response);
00084   }
00085 };
00086 
00087 class TopicServiceServer
00088 {
00089  private:
00090 
00091   boost::shared_ptr<ImplRoot> impl_;
00092 
00093  public:
00094   TopicServiceServer();
00095 
00096   template<class MReq, class MRes, class T>
00097   void initialize(ros::NodeHandle &nh,
00098                 const std::string &service,
00099                 bool(T::*srv_func)(const MReq &, MRes &),
00100                 T *obj);
00101 };  // class TopicServiceServer
00102 
00103 inline
00104 TopicServiceServer::TopicServiceServer()
00105 {
00106 
00107 }
00108 
00109 template<class MReq, class MRes, class T>
00110 inline
00111 void TopicServiceServer::initialize(ros::NodeHandle &nh,
00112                           const std::string &service,
00113                           bool(T::*srv_func)(const MReq &, MRes &),
00114                           T *obj)
00115 {
00116   TopicServiceServerImpl<MReq, MRes, T>* impl = new TopicServiceServerImpl<MReq, MRes, T>();
00117   impl->initialize(nh, service, srv_func, obj);
00118   impl_ = boost::shared_ptr<ImplRoot>(impl);
00119 }
00120 
00121 }  // namespace swri
00122 #endif  // SWRI_ROSCPP_TOPIC_SERVICE_SERVER_H_


swri_roscpp
Author(s):
autogenerated on Thu Jun 6 2019 20:34:47