Request

class Request

Request class.

This class will wrap a message of type Message::Type::kRequest and is used to provide the request to the rest of the application. This class will also take care of the Message::Type::kResponse or Message::Type::kError to return to the sender.

Public Types

using CompleteCallback = std::function<void(Message msg)>

Completion callback.

This function is called when the request is completed with a Message::Type::kResponse message or canceled with a Message::Type::kCancel message.

Param msg:

[in] The message returned from the request

Public Functions

~Request()

Request destructor.

If the request is not yet completed, the request will be marked as aborted and completion callback will be called.

inline const node::Address &get_source() const

Get source address.

Returns:

the source address of the request.

inline const node::Address &get_destination() const

Get destination address.

Returns:

the destination address of the request.

inline const std::string &get_payload() const

Get payload.

This function can be used to get current payload of a request.

Returns:

a reference to the current payload.

inline bool get_payload(google::protobuf::Message &msg) const

Get payload as protobuf.

This function can be used to deserialize the payload of a request.

Parameters:

msg[out] The protobug message to fill

Returns:

true if the payload has been deserialized, false otherwise.

template<class T>
inline T get_payload() const

Get payload as protobuf.

This function will return a deserialization of the payload.

Returns:

a reference to the message payload.

bool complete(std::string payload)

Complete the request.

This function will complete the request by accepting the response payload and forwarding it to the source node of the request. If a response or an error is already set, the function will return false.

Note

This function will call immediately the complete callback registered by the source node, thus locks should be avoided.

Parameters:

payload[in] The payload to set as a response

Returns:

true if the request has been completed, false otherwise.

bool complete(const google::protobuf::Message &msg)

Complete the request.

This function will complete the request by accepting the response message and forwarding it to the source node of the request. If a response or an error is already set, the function will return false.

Note

This function will call immediately the complete callback registered by the source node, thus locks should be avoided.

Parameters:

msg[in] The message to serialize as payload

Returns:

true if the request has been completed, false otherwise.

bool abort(uint32_t code, std::string reason)

Abort the request.

This function will abort the request and any further call to set a response will be ignored. If a response or an error is already set, the function will return false.

Note

This function will call immediately the complete callback registered by the source node, thus locks should be avoided.

Parameters:
  • code[in] The error code

  • reason[in] The reason of the error

Returns:

true if the request has been aborted, false otherwise.

inline bool is_completed() const

Check if request is already completed.

Returns:

true if the request has been completed, false otherwise.

Public Static Functions

static inline std::shared_ptr<Request> create(Message msg, CompleteCallback cb)

Create a new request from a message.

If the provided message is not a Message::Type::kRequest, this function will fail.

Parameters:
  • msg[in] The message to wrap

  • cb[in] The function to call when request is completed / aborted

Returns:

the newly created request or nullptr.