service_callback_helper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009, Willow Garage, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef ROSCPP_SERVICE_MESSAGE_HELPER_H
29 #define ROSCPP_SERVICE_MESSAGE_HELPER_H
30 
31 #include "ros/forwards.h"
32 #include "ros/common.h"
33 #include "ros/message.h"
34 #include "ros/message_traits.h"
35 #include "ros/service_traits.h"
36 #include "ros/serialization.h"
37 
38 #include <boost/type_traits/is_base_of.hpp>
39 #include <boost/utility/enable_if.hpp>
40 
41 namespace ros
42 {
44 {
48 };
49 
50 template<typename M>
52 {
53  return boost::make_shared<M>();
54 }
55 
56 template<typename MReq, typename MRes>
58 {
62 };
63 
69 template<typename MReq, typename MRes>
71 {
72 public:
73  typedef MReq RequestType;
74  typedef MRes ResponseType;
77  typedef boost::function<bool(ServiceEvent<RequestType, ResponseType>&)> CallbackType;
78 
80  {
82  return cb(event);
83  }
84 
86  : request_(req)
87  , response_(res)
88  , connection_header_(connection_header)
89  {}
90 
94  const RequestType& getRequest() const { return *request_; }
98  ResponseType& getResponse() const { return *response_; }
103 
107  const std::string& getCallerName() const { return (*connection_header_)["callerid"]; }
108 private:
112 };
113 
114 template<typename MReq, typename MRes>
116 {
117  typedef MReq RequestType;
118  typedef MRes ResponseType;
121  typedef boost::function<bool(RequestType&, ResponseType&)> CallbackType;
122 
124  {
125  return cb(*params.request, *params.response);
126  }
127 };
128 
134 class ROSCPP_DECL ServiceCallbackHelper
135 {
136 public:
138  virtual bool call(ServiceCallbackHelperCallParams& params) = 0;
139 };
141 
145 template<typename Spec>
147 {
148 public:
149  typedef typename Spec::RequestType RequestType;
150  typedef typename Spec::ResponseType ResponseType;
151  typedef typename Spec::RequestPtr RequestPtr;
152  typedef typename Spec::ResponsePtr ResponsePtr;
153  typedef typename Spec::CallbackType Callback;
154  typedef boost::function<RequestPtr()> ReqCreateFunction;
155  typedef boost::function<ResponsePtr()> ResCreateFunction;
156 
158  const ReqCreateFunction& create_req =
159  // these static casts are legally unnecessary, but
160  // here to keep clang 2.8 from getting confused
161  static_cast<RequestPtr(*)()>(defaultServiceCreateFunction<RequestType>),
162  const ResCreateFunction& create_res =
163  static_cast<ResponsePtr(*)()>(defaultServiceCreateFunction<ResponseType>))
164  : callback_(callback)
165  , create_req_(create_req)
166  , create_res_(create_res)
167  {
168  }
169 
170  virtual bool call(ServiceCallbackHelperCallParams& params)
171  {
172  namespace ser = serialization;
173  RequestPtr req(create_req_());
174  ResponsePtr res(create_res_());
175 
176  ser::deserializeMessage(params.request, *req);
177 
179  call_params.request = req;
180  call_params.response = res;
181  call_params.connection_header = params.connection_header;
182  bool ok = Spec::call(callback_, call_params);
183  params.response = ser::serializeServiceResponse(ok, *res);
184  return ok;
185  }
186 
187 private:
191 };
192 
193 }
194 
195 #endif // ROSCPP_SERVICE_MESSAGE_HELPER_H
ros::ServiceCallbackHelperCallParams::response
SerializedMessage response
Definition: service_callback_helper.h:46
ros::ServiceEvent::connection_header_
boost::shared_ptr< M_string > connection_header_
Definition: service_callback_helper.h:111
ros::ServiceEvent::call
static bool call(const CallbackType &cb, ServiceSpecCallParams< RequestType, ResponseType > &params)
Definition: service_callback_helper.h:79
ros::SerializedMessage
ros::ServiceSpec::CallbackType
boost::function< bool(RequestType &, ResponseType &)> CallbackType
Definition: service_callback_helper.h:121
ros::ServiceCallbackHelperCallParams::request
SerializedMessage request
Definition: service_callback_helper.h:45
ros::ServiceSpec::call
static bool call(const CallbackType &cb, ServiceSpecCallParams< RequestType, ResponseType > &params)
Definition: service_callback_helper.h:123
ros::service::call
bool call(const std::string &service_name, MReq &req, MRes &res)
Invoke an RPC service.
Definition: service.h:65
ros::ServiceCallbackHelper::~ServiceCallbackHelper
virtual ~ServiceCallbackHelper()
Definition: service_callback_helper.h:137
ros::ServiceCallbackHelperT::ServiceCallbackHelperT
ServiceCallbackHelperT(const Callback &callback, const ReqCreateFunction &create_req=static_cast< RequestPtr(*)()>(defaultServiceCreateFunction< RequestType >), const ResCreateFunction &create_res=static_cast< ResponsePtr(*)()>(defaultServiceCreateFunction< ResponseType >))
Definition: service_callback_helper.h:157
ros::ServiceCallbackHelperT::ReqCreateFunction
boost::function< RequestPtr()> ReqCreateFunction
Definition: service_callback_helper.h:154
boost::shared_ptr< M_string >
ros::ServiceCallbackHelperT::create_req_
ReqCreateFunction create_req_
Definition: service_callback_helper.h:189
forwards.h
ros::ServiceCallbackHelperT::RequestType
Spec::RequestType RequestType
Definition: service_callback_helper.h:149
ros::ServiceEvent::getRequest
const RequestType & getRequest() const
Returns a const-reference to the request.
Definition: service_callback_helper.h:94
ros::ServiceSpec::ResponsePtr
boost::shared_ptr< ResponseType > ResponsePtr
Definition: service_callback_helper.h:120
ros::ServiceSpec::RequestPtr
boost::shared_ptr< RequestType > RequestPtr
Definition: service_callback_helper.h:119
ros
ros::ServiceEvent::ServiceEvent
ServiceEvent(const boost::shared_ptr< MReq const > &req, const boost::shared_ptr< MRes > &res, const boost::shared_ptr< M_string > &connection_header)
Definition: service_callback_helper.h:85
ros::ServiceSpecCallParams::connection_header
boost::shared_ptr< M_string > connection_header
Definition: service_callback_helper.h:61
ros::ServiceCallbackHelperT::call
virtual bool call(ServiceCallbackHelperCallParams &params)
Definition: service_callback_helper.h:170
ros::ServiceEvent::ResponsePtr
boost::shared_ptr< ResponseType > ResponsePtr
Definition: service_callback_helper.h:76
ros::ServiceEvent::request_
boost::shared_ptr< RequestType const > request_
Definition: service_callback_helper.h:109
ros::ServiceCallbackHelperT
Concrete generic implementation of ServiceCallbackHelper for any normal service type.
Definition: service_callback_helper.h:146
ros::ok
ROSCPP_DECL bool ok()
Check whether it's time to exit.
Definition: init.cpp:603
ros::ServiceEvent::getResponse
ResponseType & getResponse() const
Returns a non-const reference to the response.
Definition: service_callback_helper.h:98
ros::ServiceSpecCallParams::request
boost::shared_ptr< MReq > request
Definition: service_callback_helper.h:59
ros::defaultServiceCreateFunction
boost::shared_ptr< M > defaultServiceCreateFunction()
Definition: service_callback_helper.h:51
ros::ServiceSpecCallParams
Definition: service_callback_helper.h:57
ros::ServiceEvent::ResponseType
MRes ResponseType
Definition: service_callback_helper.h:74
ros::ServiceEvent::response_
boost::shared_ptr< ResponseType > response_
Definition: service_callback_helper.h:110
message.h
message_traits.h
ros::ServiceSpecCallParams::response
boost::shared_ptr< MRes > response
Definition: service_callback_helper.h:60
ros::ServiceCallbackHelperT::create_res_
ResCreateFunction create_res_
Definition: service_callback_helper.h:190
serialization.h
ros::ServiceSpec::ResponseType
MRes ResponseType
Definition: service_callback_helper.h:118
ros::ServiceEvent::RequestType
MReq RequestType
Definition: service_callback_helper.h:73
ros::ServiceCallbackHelperT::RequestPtr
Spec::RequestPtr RequestPtr
Definition: service_callback_helper.h:151
ros::ServiceEvent::getCallerName
const std::string & getCallerName() const
Returns the name of the node which called this service.
Definition: service_callback_helper.h:107
ros::ServiceCallbackHelperT::callback_
Callback callback_
Definition: service_callback_helper.h:188
ros::ServiceCallbackHelperT::ResponsePtr
Spec::ResponsePtr ResponsePtr
Definition: service_callback_helper.h:152
ros::ServiceCallbackHelper
Abstract base class used by service servers to deal with concrete message types through a common inte...
Definition: service_callback_helper.h:134
ros::ServiceCallbackHelperPtr
boost::shared_ptr< ServiceCallbackHelper > ServiceCallbackHelperPtr
Definition: service_callback_helper.h:140
ros::ServiceEvent::RequestPtr
boost::shared_ptr< RequestType > RequestPtr
Definition: service_callback_helper.h:75
ros::ServiceSpec
Definition: service_callback_helper.h:115
ros::ServiceCallbackHelperCallParams
Definition: service_callback_helper.h:43
ros::ServiceCallbackHelperT::ResCreateFunction
boost::function< ResponsePtr()> ResCreateFunction
Definition: service_callback_helper.h:155
ros::ServiceEvent::getConnectionHeader
M_string & getConnectionHeader() const
Returns a reference to the connection header.
Definition: service_callback_helper.h:102
ros::ServiceCallbackHelperT::Callback
Spec::CallbackType Callback
Definition: service_callback_helper.h:153
ros::ServiceEvent::CallbackType
boost::function< bool(ServiceEvent< RequestType, ResponseType > &)> CallbackType
Definition: service_callback_helper.h:77
ros::ServiceSpec::RequestType
MReq RequestType
Definition: service_callback_helper.h:117
ros::ServiceCallbackHelperT::ResponseType
Spec::ResponseType ResponseType
Definition: service_callback_helper.h:150
ros::ServiceEvent
Event type for services, ros::ServiceEvent<MReq, MRes>& can be used in your callback instead of MReq&...
Definition: service_callback_helper.h:70
ros::ServiceCallbackHelperCallParams::connection_header
boost::shared_ptr< M_string > connection_header
Definition: service_callback_helper.h:47
ros::M_string
std::map< std::string, std::string > M_string
service_traits.h


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas , Jacob Perron
autogenerated on Sat Sep 14 2024 02:59:35