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_INCLUDED__
19 #define __IGN_TRANSPORT_REPHANDLER_HH_INCLUDED__
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 #include <functional>
30 #include <iostream>
31 #include <memory>
32 #include <string>
33 
37 
38 namespace ignition
39 {
40  namespace transport
41  {
45  {
47  public: IRepHandler()
48  : hUuid(Uuid().ToString())
49  {
50  }
51 
53  public: virtual ~IRepHandler() = default;
54 
59  public: virtual void RunLocalCallback(const transport::ProtoMsg &_msgReq,
60  transport::ProtoMsg &_msgRep,
61  bool &_result) = 0;
62 
69  public: virtual void RunCallback(const std::string &_req,
70  std::string &_rep,
71  bool &_result) = 0;
72 
75  public: std::string HandlerUuid() const
76  {
77  return this->hUuid;
78  }
79 
82  public: virtual std::string ReqTypeName() const = 0;
83 
86  public: virtual std::string RepTypeName() const = 0;
87 
89  protected: std::string hUuid;
90  };
91 
96  // the service call. 'Rep' is the protobuf message type that will be filled
98  template <typename Req, typename Rep> class RepHandler
99  : public IRepHandler
100  {
101  // Documentation inherited.
102  public: RepHandler() = default;
103 
110  public: void SetCallback(const std::function
111  <void(const Req &, Rep &, bool &)> &_cb)
112  {
113  this->cb = _cb;
114  }
115 
116  // Documentation inherited.
117  public: void RunLocalCallback(const transport::ProtoMsg &_msgReq,
118  transport::ProtoMsg &_msgRep,
119  bool &_result)
120  {
121  // Execute the callback (if existing)
122  if (this->cb)
123  {
124  auto msgReq = google::protobuf::down_cast<const Req*>(&_msgReq);
125  auto msgRep = google::protobuf::down_cast<Rep*>(&_msgRep);
126 
127  this->cb(*msgReq, *msgRep, _result);
128  }
129  else
130  {
131  std::cerr << "RepHandler::RunLocalCallback() error: "
132  << "Callback is NULL" << std::endl;
133  _result = false;
134  }
135  }
136 
137  // Documentation inherited.
138  public: void RunCallback(const std::string &_req,
139  std::string &_rep,
140  bool &_result)
141  {
142  // Check if we have a callback registered.
143  if (!this->cb)
144  {
145  std::cerr << "RepHandler::RunCallback() error: "
146  << "Callback is NULL" << std::endl;
147  _result = false;
148  return;
149  }
150 
151  // Instantiate the specific protobuf message associated to this topic.
152  auto msgReq = this->CreateMsg(_req);
153  if (!msgReq)
154  {
155  _result = false;
156  return;
157  }
158 
159  Rep msgRep;
160  this->cb(*msgReq, msgRep, _result);
161 
162  if (!msgRep.SerializeToString(&_rep))
163  {
164  std::cerr << "RepHandler::RunCallback(): Error serializing the "
165  << "response" << std::endl;
166  _result = false;
167  return;
168  }
169  }
170 
171  // Documentation inherited.
172  public: virtual std::string ReqTypeName() const
173  {
174  return Req().GetTypeName();
175  }
176 
177  // Documentation inherited.
178  public: virtual std::string RepTypeName() const
179  {
180  return Rep().GetTypeName();
181  }
182 
186  private: std::shared_ptr<Req> CreateMsg(const std::string &_data) const
187  {
188  // Instantiate a specific protobuf message
189  std::shared_ptr<Req> msgPtr(new Req());
190 
191  // Create the message using some serialized data
192  if (!msgPtr->ParseFromString(_data))
193  {
194  std::cerr << "RepHandler::CreateMsg() error: ParseFromString failed"
195  << std::endl;
196  }
197 
198  return msgPtr;
199  }
200 
202  private: std::function<void(const Req &, Rep &, bool &)> cb;
203  };
204  }
205 }
206 
207 #endif
std::string HandlerUuid() const
Get the unique UUID of this handler.
Definition: RepHandler.hh:75
Interface class used to manage a replier handler.
Definition: RepHandler.hh:44
#define IGNITION_VISIBLE
Use to represent "symbol visible" if supported.
Definition: Helpers.hh:56
std::string hUuid
Unique handler&#39;s UUID.
Definition: RepHandler.hh:89
virtual std::string ReqTypeName() const
Get the message type name used in the service request.
Definition: RepHandler.hh:172
IRepHandler()
Constructor.
Definition: RepHandler.hh:47
void RunCallback(const std::string &_req, std::string &_rep, bool &_result)
Executes the callback registered for this handler.
Definition: RepHandler.hh:138
A portable class for representing a Universally Unique Identifier.
Definition: Uuid.hh:41
virtual std::string RepTypeName() const
Get the message type name used in the service response.
Definition: RepHandler.hh:178
google::protobuf::Message ProtoMsg
Definition: TransportTypes.hh:62
void RunLocalCallback(const transport::ProtoMsg &_msgReq, transport::ProtoMsg &_msgRep, bool &_result)
Executes the local callback registered for this handler.
Definition: RepHandler.hh:117
with the service response.
Definition: RepHandler.hh:98
void SetCallback(const std::function< void(const Req &, Rep &, bool &)> &_cb)
Set the callback for this handler.
Definition: RepHandler.hh:110
Definition: AdvertiseOptions.hh:25