18 #ifndef __IGN_TRANSPORT_REQHANDLER_HH_INCLUDED__ 19 #define __IGN_TRANSPORT_REQHANDLER_HH_INCLUDED__ 22 #pragma warning(push, 0) 24 #include <google/protobuf/message.h> 29 #include <condition_variable> 51 hUuid(
Uuid().ToString()),
67 public:
virtual void NotifyResult(
const std::string &_rep,
68 const bool _result) = 0;
95 return this->requested;
102 this->requested = _value;
108 public:
virtual bool Serialize(std::string &_buffer)
const = 0;
124 public:
template<
typename Lock>
bool WaitUntil(Lock &_lock,
125 const unsigned int _timeout)
127 auto now = std::chrono::system_clock::now();
128 return this->condition.wait_until(_lock,
129 now + std::chrono::milliseconds(_timeout),
132 return this->repAvailable;
138 public:
virtual std::string ReqTypeName()
const = 0;
142 public:
virtual std::string RepTypeName()
const = 0;
149 protected: std::string
rep;
158 private: std::string nUuid;
162 private:
bool requested;
187 public: std::shared_ptr<Rep>
CreateMsg(
const std::string &_data)
const 190 std::shared_ptr<Rep> msgPtr(
new Rep());
193 if (!msgPtr->ParseFromString(_data))
195 std::cerr <<
"ReqHandler::CreateMsg() error: ParseFromString failed" 208 const Rep &_rep,
const bool _result)> &_cb)
218 this->reqMsg = _reqMsg;
224 if (!this->reqMsg.SerializeToString(&_buffer))
226 std::cerr <<
"ReqHandler::Serialize(): Error serializing the request" 241 auto msg = this->CreateMsg(_rep);
243 this->cb(*msg, _result);
248 this->result = _result;
251 this->repAvailable =
true;
252 this->condition.notify_one();
258 return Req().GetTypeName();
264 return Rep().GetTypeName();
275 private: std::function<void(const Rep &_rep, const bool _result)> cb;
#define IGNITION_VISIBLE
Use to represent "symbol visible" if supported.
Definition: Helpers.hh:56
std::string rep
Stores the service response as raw bytes.
Definition: ReqHandler.hh:149
ReqHandler(const std::string &_nUuid)
Definition: ReqHandler.hh:179
void SetMessage(const Req &_reqMsg)
Set the REQ protobuf message for this handler.
Definition: ReqHandler.hh:216
It creates a reply handler for the specific protobuf messages used.
Definition: ReqHandler.hh:175
std::condition_variable_any condition
Condition variable used to wait until a service call REP is available.
Definition: ReqHandler.hh:146
std::string HandlerUuid() const
Returns the unique handler UUID.
Definition: ReqHandler.hh:112
bool repAvailable
When there is a blocking service call request, the call can be unlocked when a service call REP is av...
Definition: ReqHandler.hh:167
bool Serialize(std::string &_buffer) const
Serialize the Req protobuf message stored.
Definition: ReqHandler.hh:222
std::shared_ptr< Rep > CreateMsg(const std::string &_data) const
Create a specific protobuf message given its serialized data.
Definition: ReqHandler.hh:187
virtual std::string RepTypeName() const
Get the message type name used in the service response.
Definition: ReqHandler.hh:262
std::string hUuid
Unique handler's UUID.
Definition: ReqHandler.hh:155
A portable class for representing a Universally Unique Identifier.
Definition: Uuid.hh:41
Interface class used to manage a request handler.
Definition: ReqHandler.hh:44
void NotifyResult(const std::string &_rep, const bool _result)
Executes the callback registered for this handler and notify a potential requester waiting on a block...
Definition: ReqHandler.hh:235
bool WaitUntil(Lock &_lock, const unsigned int _timeout)
Block the current thread until the response to the service request is available or until the timeout ...
Definition: ReqHandler.hh:124
bool Result() const
Get the result of the service response.
Definition: ReqHandler.hh:86
void SetCallback(const std::function< void( const Rep &_rep, const bool _result)> &_cb)
Set the callback for this handler.
Definition: ReqHandler.hh:207
void Requested(const bool _value)
Mark the service call as requested (or not).
Definition: ReqHandler.hh:100
bool Requested() const
Returns if this service call request has already been requested.
Definition: ReqHandler.hh:93
std::string NodeUuid() const
Get the node UUID.
Definition: ReqHandler.hh:72
virtual std::string ReqTypeName() const
Get the message type name used in the service request.
Definition: ReqHandler.hh:256
std::string Response() const
Get the service response as raw bytes.
Definition: ReqHandler.hh:79
Definition: AdvertiseOptions.hh:25
IReqHandler(const std::string &_nUuid)
Constructor.
Definition: ReqHandler.hh:48
bool result
Stores the result of the service call.
Definition: ReqHandler.hh:152