Go to the documentation of this file.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 #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 };
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 }
00122 #endif // SWRI_ROSCPP_TOPIC_SERVICE_SERVER_H_