Gazebo Transport

API Reference

8.5.0
gz/transport/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 GZ_TRANSPORT_REPHANDLER_HH_
19 #define GZ_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 && GOOGLE_PROTOBUF_VERSION < 4022000
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 "gz/transport/config.hh"
39 #include "gz/transport/Export.hh"
41 #include "gz/transport/Uuid.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 >= 4022000
144  auto msgReq =
145  google::protobuf::internal::DownCast<const Req*>(&_msgReq);
146  auto msgRep = google::protobuf::internal::DownCast<Rep*>(&_msgRep);
147 #elif GOOGLE_PROTOBUF_VERSION > 2999999
148  auto msgReq = google::protobuf::down_cast<const Req*>(&_msgReq);
149  auto msgRep = google::protobuf::down_cast<Rep*>(&_msgRep);
150 #else
151  auto msgReq =
152  google::protobuf::internal::down_cast<const Req*>(&_msgReq);
153  auto msgRep = google::protobuf::internal::down_cast<Rep*>(&_msgRep);
154 #endif
155 
156  return this->cb(*msgReq, *msgRep);
157  }
158 
159  // Documentation inherited.
160  public: bool RunCallback(const std::string &_req,
161  std::string &_rep)
162  {
163  // Check if we have a callback registered.
164  if (!this->cb)
165  {
166  std::cerr << "RepHandler::RunCallback() error: "
167  << "Callback is NULL" << std::endl;
168  return false;
169  }
170 
171  // Instantiate the specific protobuf message associated to this topic.
172  auto msgReq = this->CreateMsg(_req);
173  if (!msgReq)
174  {
175  return false;
176  }
177 
178  Rep msgRep;
179  if (!this->cb(*msgReq, msgRep))
180  return false;
181 
182  if (!msgRep.SerializeToString(&_rep))
183  {
184  std::cerr << "RepHandler::RunCallback(): Error serializing the "
185  << "response" << std::endl;
186  return false;
187  }
188 
189  return true;
190  }
191 
192  // Documentation inherited.
193  public: virtual std::string ReqTypeName() const
194  {
195  return Req().GetTypeName();
196  }
197 
198  // Documentation inherited.
199  public: virtual std::string RepTypeName() const
200  {
201  return Rep().GetTypeName();
202  }
203 
207  private: std::shared_ptr<Req> CreateMsg(const std::string &_data) const
208  {
209  // Instantiate a specific protobuf message
210  std::shared_ptr<Req> msgPtr(new Req());
211 
212  // Create the message using some serialized data
213  if (!msgPtr->ParseFromString(_data))
214  {
215  std::cerr << "RepHandler::CreateMsg() error: ParseFromString failed"
216  << std::endl;
217  }
218 
219  return msgPtr;
220  }
221 
223  private: std::function<bool(const Req &, Rep &)> cb;
224  };
225  }
226  }
227 }
228 
229 #endif
bool RunLocalCallback(const transport::ProtoMsg &_msgReq, transport::ProtoMsg &_msgRep)
Executes the local callback registered for this handler.
Definition: gz/transport/RepHandler.hh:132
Definition: gz/transport/AdvertiseOptions.hh:28
STL class.
STL class.
google::protobuf::Message ProtoMsg
Definition: gz/transport/TransportTypes.hh:70
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: gz/transport/RepHandler.hh:81
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: gz/transport/Uuid.hh:45
void SetCallback(const std::function< bool(const Req &, Rep &)> &_cb)
Set the callback for this handler.
Definition: gz/transport/RepHandler.hh:125
virtual ~IRepHandler()=default
Destructor.
virtual std::string ReqTypeName() const
Get the message type name used in the service request.
Definition: gz/transport/RepHandler.hh:193
virtual std::string RepTypeName() const
Get the message type name used in the service response.
Definition: gz/transport/RepHandler.hh:199
virtual bool RunLocalCallback(const transport::ProtoMsg &_msgReq, transport::ProtoMsg &_msgRep)=0
Executes the local callback registered for this handler.
with the service response.
Definition: gz/transport/RepHandler.hh:113
std::string hUuid
Unique handler's UUID.
Definition: gz/transport/RepHandler.hh:101
T endl(T... args)
IRepHandler()
Constructor.
Definition: gz/transport/RepHandler.hh:55
Interface class used to manage a replier handler.
Definition: gz/transport/RepHandler.hh:52
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: gz/transport/RepHandler.hh:160