Message

class Message

Message class.

The message class will hold the header and payload of a message: the default mechanism to communicate in Melo.

Public Types

enum class Type

Message Type.

Please read the Protobuf documentation for more details.

Values:

enumerator kUnknown

Undefined message type.

enumerator kRequest

Request message.

enumerator kCancel

Cancel message.

enumerator kResponse

Response message.

enumerator kError

Error message.

enumerator kEvent

Event message.

Public Functions

Message() = default

Create a new empty message.

An empty message is invalid and to fill the message, the function get_buffer() and parse_buffer() should be used.

Message(Type type, node::Address src, node::Address dst, uint32_t uid, std::string payload)

Create a message.

Note

The unique ID is only applicable for Type::kRequest, Type::kCancel, Type::kResponse or Type::kError, and it should be >= 1 to be valid.

Parameters:
inline Message(Type type, node::Address src, node::Address dst, std::string payload)

Create a message.

Parameters:
  • type[in] The type of the message

  • src[in] The address of sending Node

  • dst[in] The address of the receiving Node

  • payload[in] The payload of the message

Message(Type type, node::Address src, node::Address dst, uint32_t uid, const google::protobuf::Message &msg)

Create a message from a protobuf.

Parameters:
inline Message(Type type, node::Address src, node::Address dst, const google::protobuf::Message &msg)

Create a message from a protobuf.

Parameters:
  • type[in] The type of the message

  • src[in] The address of sending Node

  • dst[in] The address of the receiving Node

  • msg[in] The protobuf message to serialize

Message(const Message &msg) = default

Copy a message.

Parameters:

msg[in] The message to copy

inline Message(Message &&msg)

Move a nessage.

Parameters:

msg[in] The message to move

inline Message &operator=(Message &&msg)

Assign a message by move.

Parameters:

msg[in] The message to move

std::span<char> get_buffer(size_t chunk_size = kDefaultHeaderSize)

Get parsing buffer.

This function is used in conjunction with parse_buffer() and is used to get a buffer to fill with data to parse.

Parameters:

chunk_size[in] The optional chunk size in bytes (clipped to maximal header size)

Returns:

a span pointing the buffer to fill.

size_t parse_buffer(size_t size, std::string &extra)

Parse from internal buffer with extra buffer.

This function is used to tell the parser that new data has been written in the internal buffer fetched with get_buffer(). The amount of data is provided by size.

If too many data have been provided, the extra parameter will be filled with the extra data not used for this message. Thus, the extra data can be used with the other parse_buffer() implementation.

When the parsing is finished, a call to is_valid() will return true.

Parameters:
  • size[in] The size of data to parse

  • extra[out] The buffer to store extra data (out of this message)

Returns:

the length in bytes of the data parsed or 0 if failed.

size_t parse_buffer(std::string &buffer)

Parse from a buffer.

This function should be used when the extra parameter of parse_buffer() has been filled with remaining data not used during parsing.

If the buffer was containing enough data for a new message, a call to is_valid() will return true and the buffer will be updated with the extra unused data. In such case, this function should called again until the buffer is empty after the function returned.

Parameters:

buffer[inout] The buffer to parse and where to store extra data (out of this message)

Returns:

the length in bytes of the data parsed or 0 if failed.

inline bool is_valid() const

Check if the message is valid.

This function should be used with parse_buffer(): when the message is fully parsed, this function will return true.

Returns:

true is the message is fully parse and valid.

inline Type get_type() const

Get node type.

Returns:

the type of the current message.

inline const node::Address &get_source() const

Get source address.

Returns:

a reference to the source address.

inline const node::Address &get_destination() const

Get destination address.

Returns:

a reference to the destination address.

inline uint32_t get_uid() const

Get message unique ID.

Note

The unique ID is only applicable for Type::kRequest, Type::kCancel, Type::kResponse or Type::kError, and it should be >= 1 to be valid.

Returns:

the unique ID of the current message or 0 if not applicable.

inline const std::string &get_header() const

Get message header.

Returns:

a reference to the message header.

inline const std::string &get_payload() const

Get message payload.

Returns:

a reference to the message payload.

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

Get message payload as protobuf.

This function will deserialize the payload to a protobuf.

Parameters:

msg[out] The message to fill with data from payload

Returns:

true if the payload has been deserialized, false otherwise.

template<class T>
inline T get_payload() const

Get message payload as protobuf.

This function will return a deserialization of the payload.

Returns:

a reference to the message payload.

bool convert_to_response(std::string payload)

Convert the message into a response.

Parameters:

payload[in] The serialized payload

Returns:

true if the message has been converted, false otherwise.

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

Convert the message into a response.

Parameters:

msg[in] The message to serialize as payload

Returns:

true if the message has been converted, false otherwise.

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

Convert the message into an error.

Parameters:
  • code[in] The error code

  • reason[in] The reason of the error

Returns:

true if the message has been converted, false otherwise.

Public Static Functions

template<class ...U>
static inline std::shared_ptr<Message> create(U&&... u)

Create a shared message.

This function can be useful to create a shared pointer of a Message in order to be shared by multiple parts of the code, such as event messages.

Parameters:

u[in] The arguments from one of the Message constructors

Returns:

a newly allocated shared pointer containing the Message instance.

Public Static Attributes

static size_t kMaxAddressSize = 256

Maximal address size.

static size_t kMinHeaderSize = 12

Minimal header size.

static size_t kMaxHeaderSize = 512 + kMinHeaderSize

Maximal header size.

static size_t kDefaultHeaderSize = 64

Default header size.