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_; }
102  M_string& getConnectionHeader() const { return *connection_header_; }
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 
123  static bool call(const CallbackType& cb, ServiceSpecCallParams<RequestType, ResponseType>& params)
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 
157  ServiceCallbackHelperT(const Callback& callback,
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:
188  Callback callback_;
189  ReqCreateFunction create_req_;
190  ResCreateFunction create_res_;
191 };
192 
193 }
194 
195 #endif // ROSCPP_SERVICE_MESSAGE_HELPER_H
boost::shared_ptr< RequestType > RequestPtr
boost::function< RequestPtr()> ReqCreateFunction
const std::string & getCallerName() const
Returns the name of the node which called this service.
ResponseType & getResponse() const
Returns a non-const reference to the response.
static bool call(const CallbackType &cb, ServiceSpecCallParams< RequestType, ResponseType > &params)
boost::function< bool(ServiceEvent< RequestType, ResponseType > &)> CallbackType
boost::shared_ptr< ServiceCallbackHelper > ServiceCallbackHelperPtr
boost::shared_ptr< MReq > request
boost::function< bool(RequestType &, ResponseType &)> CallbackType
bool call(const std::string &service_name, MReq &req, MRes &res)
Invoke an RPC service.
Definition: service.h:65
boost::shared_ptr< M_string > connection_header
boost::shared_ptr< M_string > connection_header
virtual bool call(ServiceCallbackHelperCallParams &params)
Abstract base class used by service servers to deal with concrete message types through a common inte...
boost::shared_ptr< ResponseType > response_
std::map< std::string, std::string > M_string
const RequestType & getRequest() const
Returns a const-reference to the request.
ROSCPP_DECL bool ok()
Check whether it&#39;s time to exit.
Definition: init.cpp:573
Concrete generic implementation of ServiceCallbackHelper for any normal service type.
boost::shared_ptr< M_string > connection_header_
boost::shared_ptr< RequestType const > request_
boost::shared_ptr< MRes > response
Event type for services, ros::ServiceEvent<MReq, MRes>& can be used in your callback instead of MReq&...
ServiceEvent(const boost::shared_ptr< MReq const > &req, const boost::shared_ptr< MRes > &res, const boost::shared_ptr< M_string > &connection_header)
boost::function< ResponsePtr()> ResCreateFunction
boost::shared_ptr< M > defaultServiceCreateFunction()
boost::shared_ptr< ResponseType > ResponsePtr
M_string & getConnectionHeader() const
Returns a reference to the connection header.
static bool call(const CallbackType &cb, ServiceSpecCallParams< RequestType, ResponseType > &params)
boost::shared_ptr< RequestType > RequestPtr
ServiceCallbackHelperT(const Callback &callback, const ReqCreateFunction &create_req=static_cast< RequestPtr(*)()>(defaultServiceCreateFunction< RequestType >), const ResCreateFunction &create_res=static_cast< ResponsePtr(*)()>(defaultServiceCreateFunction< ResponseType >))
boost::shared_ptr< ResponseType > ResponsePtr


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim
autogenerated on Wed Mar 21 2018 07:13:27