Ignition Transport

API Reference

4.0.0
RepHandler.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #ifndef IGN_TRANSPORT_REPHANDLER_HH_
19 #define IGN_TRANSPORT_REPHANDLER_HH_
20 
21 #ifdef _MSC_VER
22 #pragma warning(push, 0)
23 #endif
24 #include <google/protobuf/message.h>
25 #ifdef _MSC_VER
26 #pragma warning(pop)
27 #endif
28 
29 #if GOOGLE_PROTOBUF_VERSION > 2999999
30 #include <google/protobuf/stubs/casts.h>
31 #endif
32 
33 #include <functional>
34 #include <iostream>
35 #include <memory>
36 #include <string>
37 
38 #include "ignition/transport/Export.hh"
41 
42 namespace ignition
43 {
44  namespace transport
45  {
48  class IGNITION_TRANSPORT_VISIBLE IRepHandler
49  {
51  public: IRepHandler()
52  : hUuid(Uuid().ToString())
53  {
54  }
55 
57  public: virtual ~IRepHandler() = default;
58 
63  public: virtual bool RunLocalCallback(const transport::ProtoMsg &_msgReq,
64  transport::ProtoMsg &_msgRep) = 0;
65 
72  public: virtual bool RunCallback(const std::string &_req,
73  std::string &_rep) = 0;
74 
77  public: std::string HandlerUuid() const
78  {
79  return this->hUuid;
80  }
81 
84  public: virtual std::string ReqTypeName() const = 0;
85 
88  public: virtual std::string RepTypeName() const = 0;
89 
91  protected: std::string hUuid;
92  };
93 
98  // the service call. 'Rep' is the protobuf message type that will be filled
100  template <typename Req, typename Rep> class RepHandler
101  : public IRepHandler
102  {
103  // Documentation inherited.
104  public: RepHandler() = default;
105 
112  public: void SetCallback(
113  const std::function<bool(const Req &, Rep &)> &_cb)
114  {
115  this->cb = _cb;
116  }
117 
118  // Documentation inherited.
119  public: bool RunLocalCallback(const transport::ProtoMsg &_msgReq,
120  transport::ProtoMsg &_msgRep)
121  {
122  // Execute the callback (if existing)
123  if (!this->cb)
124  {
125  std::cerr << "RepHandler::RunLocalCallback() error: "
126  << "Callback is NULL" << std::endl;
127  return false;
128  }
129 
130 #if GOOGLE_PROTOBUF_VERSION > 2999999
131  auto msgReq = google::protobuf::down_cast<const Req*>(&_msgReq);
132  auto msgRep = google::protobuf::down_cast<Rep*>(&_msgRep);
133 #else
134  auto msgReq =
135  google::protobuf::internal::down_cast<const Req*>(&_msgReq);
136  auto msgRep = google::protobuf::internal::down_cast<Rep*>(&_msgRep);
137 #endif
138 
139  return this->cb(*msgReq, *msgRep);
140  }
141 
142  // Documentation inherited.
143  public: bool RunCallback(const std::string &_req,
144  std::string &_rep)
145  {
146  // Check if we have a callback registered.
147  if (!this->cb)
148  {
149  std::cerr << "RepHandler::RunCallback() error: "
150  << "Callback is NULL" << std::endl;
151  return false;
152  }
153 
154  // Instantiate the specific protobuf message associated to this topic.
155  auto msgReq = this->CreateMsg(_req);
156  if (!msgReq)
157  {
158  return false;
159  }
160 
161  Rep msgRep;
162  if (!this->cb(*msgReq, msgRep))
163  return false;
164 
165  if (!msgRep.SerializeToString(&_rep))
166  {
167  std::cerr << "RepHandler::RunCallback(): Error serializing the "
168  << "response" << std::endl;
169  return false;
170  }
171 
172  return true;
173  }
174 
175  // Documentation inherited.
176  public: virtual std::string ReqTypeName() const
177  {
178  return Req().GetTypeName();
179  }
180 
181  // Documentation inherited.
182  public: virtual std::string RepTypeName() const
183  {
184  return Rep().GetTypeName();
185  }
186 
190  private: std::shared_ptr<Req> CreateMsg(const std::string &_data) const
191  {
192  // Instantiate a specific protobuf message
193  std::shared_ptr<Req> msgPtr(new Req());
194 
195  // Create the message using some serialized data
196  if (!msgPtr->ParseFromString(_data))
197  {
198  std::cerr << "RepHandler::CreateMsg() error: ParseFromString failed"
199  << std::endl;
200  }
201 
202  return msgPtr;
203  }
204 
206  private: std::function<bool(const Req &, Rep &)> cb;
207  };
208  }
209 }
210 
211 #endif
bool RunLocalCallback(const transport::ProtoMsg &_msgReq, transport::ProtoMsg &_msgRep)
Executes the local callback registered for this handler.
Definition: RepHandler.hh:119
Definition: AdvertiseOptions.hh:28
STL class.
STL class.
reqHandlerPtr SetMessage & _req
Definition: Node.hh:973
google::protobuf::Message ProtoMsg
Definition: TransportTypes.hh:66
virtual std::string RepTypeName() const =0
Get the message type name used in the service response.
std::string HandlerUuid() const
Get the unique UUID of this handler.
Definition: RepHandler.hh:77
virtual bool RunCallback(const std::string &_req, std::string &_rep)=0
Executes the callback registered for this handler.
A portable class for representing a Universally Unique Identifier.
Definition: Uuid.hh:42
*brief Advertise a new service without any output parameter *In this version the callback is a free function *param[in] _topic Topic name associated to the service *param[in] _cb Callback to handle the service request with the *following void(* _cb)(const RequestT &_req)
Definition: Node.hh:527
void SetCallback(const std::function< bool(const Req &, Rep &)> &_cb)
Set the callback for this handler.
Definition: RepHandler.hh:112
virtual ~IRepHandler()=default
Destructor.
virtual std::string ReqTypeName() const
Get the message type name used in the service request.
Definition: RepHandler.hh:176
virtual std::string RepTypeName() const
Get the message type name used in the service response.
Definition: RepHandler.hh:182
virtual bool RunLocalCallback(const transport::ProtoMsg &_msgReq, transport::ProtoMsg &_msgRep)=0
Executes the local callback registered for this handler.
with the service response.
Definition: RepHandler.hh:102
std::string hUuid
Unique handler's UUID.
Definition: RepHandler.hh:91
T endl(T... args)
IRepHandler()
Constructor.
Definition: RepHandler.hh:51
reqHandlerPtr SetResponse & _rep
Definition: Node.hh:1107
Interface class used to manage a replier handler.
Definition: RepHandler.hh:49
virtual std::string ReqTypeName() const =0
Get the message type name used in the service request.
bool RunCallback(const std::string &_req, std::string &_rep)
Executes the callback registered for this handler.
Definition: RepHandler.hh:143