service_server.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote prducts derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef _ROS_SERVICE_SERVER_H_
36 #define _ROS_SERVICE_SERVER_H_
37 
38 #include "rosserial_msgs/TopicInfo.h"
39 
40 #include "ros/publisher.h"
41 #include "ros/subscriber.h"
42 
43 namespace ros
44 {
45 
46 template<typename MReq , typename MRes, typename ObjT = void>
47 class ServiceServer : public Subscriber_
48 {
49 public:
50  typedef void(ObjT::*CallbackT)(const MReq&, MRes&);
51 
52  ServiceServer(const char* topic_name, CallbackT cb, ObjT* obj) :
53  pub(topic_name, &resp, rosserial_msgs::TopicInfo::ID_SERVICE_SERVER + rosserial_msgs::TopicInfo::ID_PUBLISHER),
54  obj_(obj)
55  {
56  this->topic_ = topic_name;
57  this->cb_ = cb;
58  }
59 
60  // these refer to the subscriber
61  virtual void callback(unsigned char *data) override
62  {
63  req.deserialize(data);
64  (obj_->*cb_)(req, resp);
65  pub.publish(&resp);
66  }
67  virtual const char * getMsgType() override
68  {
69  return this->req.getType();
70  }
71  virtual const char * getMsgMD5() override
72  {
73  return this->req.getMD5();
74  }
75  virtual int getEndpointType() override
76  {
77  return rosserial_msgs::TopicInfo::ID_SERVICE_SERVER + rosserial_msgs::TopicInfo::ID_SUBSCRIBER;
78  }
79 
80  MReq req;
81  MRes resp;
82  Publisher pub;
83 private:
84  CallbackT cb_;
85  ObjT* obj_;
86 };
87 
88 template<typename MReq , typename MRes>
89 class ServiceServer<MReq, MRes, void> : public Subscriber_
90 {
91 public:
92  typedef void(*CallbackT)(const MReq&, MRes&);
93 
94  ServiceServer(const char* topic_name, CallbackT cb) :
95  pub(topic_name, &resp, rosserial_msgs::TopicInfo::ID_SERVICE_SERVER + rosserial_msgs::TopicInfo::ID_PUBLISHER)
96  {
97  this->topic_ = topic_name;
98  this->cb_ = cb;
99  }
100 
101  // these refer to the subscriber
102  virtual void callback(unsigned char *data) override
103  {
104  req.deserialize(data);
105  cb_(req, resp);
106  pub.publish(&resp);
107  }
108  virtual const char * getMsgType() override
109  {
110  return this->req.getType();
111  }
112  virtual const char * getMsgMD5() override
113  {
114  return this->req.getMD5();
115  }
116  virtual int getEndpointType() override
117  {
118  return rosserial_msgs::TopicInfo::ID_SERVICE_SERVER + rosserial_msgs::TopicInfo::ID_SUBSCRIBER;
119  }
120 
121  MReq req;
122  MRes resp;
124 private:
125  CallbackT cb_;
126 };
127 
128 }
129 
130 #endif
ros::ServiceServer< MReq, MRes, void >::pub
Publisher pub
Definition: service_server.h:123
ros::ServiceServer< MReq, MRes, void >::getEndpointType
virtual int getEndpointType() override
Definition: service_server.h:116
ros
ros::ServiceServer< MReq, MRes, void >::getMsgType
virtual const char * getMsgType() override
Definition: service_server.h:108
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
ros::Subscriber_
Definition: subscriber.h:44
ros::ServiceServer< MReq, MRes, void >::ServiceServer
ServiceServer(const char *topic_name, CallbackT cb)
Definition: service_server.h:94
ros::ServiceServer::ServiceServer
ServiceServer()
ros::ServiceServer< MReq, MRes, void >::cb_
CallbackT cb_
Definition: service_server.h:125
ros::Publisher
Definition: publisher.h:45
ros::ServiceServer< MReq, MRes, void >::req
MReq req
Definition: service_server.h:121
ros::ServiceServer
Definition: service_server.h:47
ros::ServiceServer< MReq, MRes, void >::callback
virtual void callback(unsigned char *data) override
Definition: service_server.h:102
ros::ServiceServer< MReq, MRes, void >::getMsgMD5
virtual const char * getMsgMD5() override
Definition: service_server.h:112
ros::ServiceServer< MReq, MRes, void >::resp
MRes resp
Definition: service_server.h:122


rosserial_client
Author(s): Michael Ferguson, Adam Stambler
autogenerated on Wed Mar 2 2022 00:58:01