Ignition Transport

API Reference

10.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/config.hh"
39 #include "ignition/transport/Export.hh"
42 
43 namespace ignition
44 {
45  namespace transport
46  {
47  // Inline bracket to help doxygen filtering.
48  inline namespace IGNITION_TRANSPORT_VERSION_NAMESPACE {
49  //
52  class IGNITION_TRANSPORT_VISIBLE IRepHandler
53  {
55  public: IRepHandler()
56  : hUuid(Uuid().ToString())
57  {
58  }
59 
61  public: virtual ~IRepHandler() = default;
62 
67  public: virtual bool RunLocalCallback(const transport::ProtoMsg &_msgReq,
68  transport::ProtoMsg &_msgRep) = 0;
69 
76  public: virtual bool RunCallback(const std::string &_req,
77  std::string &_rep) = 0;
78 
81  public: std::string HandlerUuid() const
82  {
83  return this->hUuid;
84  }
85 
88  public: virtual std::string ReqTypeName() const = 0;
89 
92  public: virtual std::string RepTypeName() const = 0;
93 
94 #ifdef _WIN32
95 // Disable warning C4251 which is triggered by
96 // std::string
97 #pragma warning(push)
98 #pragma warning(disable: 4251)
99 #endif
100  protected: std::string hUuid;
102 #ifdef _WIN32
103 #pragma warning(pop)
104 #endif
105  };
106 
111  // the service call. 'Rep' is the protobuf message type that will be filled
113  template <typename Req, typename Rep> class RepHandler
114  : public IRepHandler
115  {
116  // Documentation inherited.
117  public: RepHandler() = default;
118 
125  public: void SetCallback(
126  const std::function<bool(const Req &, Rep &)> &_cb)
127  {
128  this->cb = _cb;
129  }
130 
131  // Documentation inherited.
132  public: bool RunLocalCallback(const transport::ProtoMsg &_msgReq,
133  transport::ProtoMsg &_msgRep)
134  {
135  // Execute the callback (if existing)
136  if (!this->cb)
137  {
138  std::cerr << "RepHandler::RunLocalCallback() error: "
139  << "Callback is NULL" << std::endl;
140  return false;
141  }
142 
143 #if GOOGLE_PROTOBUF_VERSION > 2999999
144  auto msgReq = google::protobuf::down_cast<const Req*>(&_msgReq);
145  auto msgRep = google::protobuf::down_cast<Rep*>(&_msgRep);
146 #else
147  auto msgReq =
148  google::protobuf::internal::down_cast<const Req*>(&_msgReq);
149  auto msgRep = google::protobuf::internal::down_cast<Rep*>(&_msgRep);
150 #endif
151 
152  return this->cb(*msgReq, *msgRep);
153  }
154 
155  // Documentation inherited.
156  public: bool RunCallback(const std::string &_req,
157  std::string &_rep)
158  {
159  // Check if we have a callback registered.
160  if (!this->cb)
161  {
162  std::cerr << "RepHandler::RunCallback() error: "
163  << "Callback is NULL" << std::endl;
164  return false;
165  }
166 
167  // Instantiate the specific protobuf message associated to this topic.
168  auto msgReq = this->CreateMsg(_req);
169  if (!msgReq)
170  {
171  return false;
172  }
173 
174  Rep msgRep;
175  if (!this->cb(*msgReq, msgRep))
176  return false;
177 
178  if (!msgRep.SerializeToString(&_rep))
179  {
180  std::cerr << "RepHandler::RunCallback(): Error serializing the "
181  << "response" << std::endl;
182  return false;
183  }
184 
185  return true;
186  }
187 
188  // Documentation inherited.
189  public: virtual std::string ReqTypeName() const
190  {
191  return Req().GetTypeName();
192  }
193 
194  // Documentation inherited.
195  public: virtual std::string RepTypeName() const
196  {
197  return Rep().GetTypeName();
198  }
199 
203  private: std::shared_ptr<Req> CreateMsg(const std::string &_data) const
204  {
205  // Instantiate a specific protobuf message
206  std::shared_ptr<Req> msgPtr(new Req());
207 
208  // Create the message using some serialized data
209  if (!msgPtr->ParseFromString(_data))
210  {
211  std::cerr << "RepHandler::CreateMsg() error: ParseFromString failed"
212  << std::endl;
213  }
214 
215  return msgPtr;
216  }
217 
220  };
221  }
222  }
223 }
224 
225 #endif
Interface class used to manage a replier handler.
Definition: RepHandler.hh:52
T endl(T... args)
std::string HandlerUuid() const
Get the unique UUID of this handler.
Definition: RepHandler.hh:81
IRepHandler()
Constructor.
Definition: RepHandler.hh:55
STL class.
A portable class for representing a Universally Unique Identifier.
Definition: Uuid.hh:45
virtual std::string ReqTypeName() const
Get the message type name used in the service request.
Definition: RepHandler.hh:189
void SetCallback(const std::function< bool(const Req &, Rep &)> &_cb)
Set the callback for this handler.
Definition: RepHandler.hh:125
virtual std::string RepTypeName() const
Get the message type name used in the service response.
Definition: RepHandler.hh:195
bool RunCallback(const std::string &_req, std::string &_rep)
Executes the callback registered for this handler.
Definition: RepHandler.hh:156
with the service response.
Definition: RepHandler.hh:113
bool RunLocalCallback(const transport::ProtoMsg &_msgReq, transport::ProtoMsg &_msgRep)
Executes the local callback registered for this handler.
Definition: RepHandler.hh:132
google::protobuf::Message ProtoMsg
Definition: TransportTypes.hh:70
Definition: AdvertiseOptions.hh:28