Symple module

Message

Base Symple protocol message with addressing, status, data, and notes.

Message

#include <icy/symple/message.h>

Inherits: Value, IPacket Subclassed by: Command, Event, Presence

Base Symple protocol message with addressing, status, data, and notes.

Public Methods

ReturnNameDescription
MessageConstructs a new message with a random ID and type set to "message".
MessageConstructs a message from a JSON value; sets missing id/type fields.
MessageCopy constructor; preserves or generates id/type fields.
std::unique_ptr< IPacket >clone virtual constReturns a heap-allocated copy of this message.
boolvalid virtual constReturns true if the message has both type and id fields.
voidclear virtualClears all JSON fields from this message.
voidclearData virtualClears the data sub-object.
voidclearNotes virtualClears the notes array.
std::stringtype constReturns the message type string (defaults to "message").
std::stringid constReturns the message ID string.
Addressto constReturns the recipient address parsed from the to field.
Addressfrom constReturns the sender address parsed from the from field.
std::stringtoUser constReturns the user component of the to address without constructing an Address.
std::stringtoId constReturns the id component of the to address without constructing an Address.
std::stringfromUser constReturns the user component of the from address without constructing an Address.
std::stringfromId constReturns the id component of the from address without constructing an Address.
intstatus constReturns the HTTP status code, or -1 if not set.
voidsetTypeSets the message type field.
voidsetToSets the to field from a peer's address.
voidsetToSets the to field from an address object.
voidsetToSets the to field from an address string.
voidsetFromSets the from field from a peer's address.
voidsetFromSets the from field from an address object.
voidsetFromSets the from field from an address string.
voidsetStatusHTTP status codes are used to describe the message response. Throws std::invalid_argument if code is outside [101, 504].
json::Value &notesReturns a reference to the notes JSON array.
voidsetNoteReplaces all notes with a single note.
voidaddNoteAppends a note to the notes array.
json::Valuedata constReturns a copy of the named data field.
json::Value &dataReturns a reference to the named data field (creates it if absent).
json::Value &setDataCreates or replaces a named data field; returns a reference to it.
voidsetDataSets a named data field to a C-string value.
voidsetDataSets a named data field to a string value.
voidsetDataSets a named data field to a JSON value.
voidsetDataSets a named data field to an integer value.
voidremoveDataRemoves a named field from the data sub-object.
boolhasDataReturns true if the named field exists in the data sub-object.
ssize_tread virtualDeserialises the message from a raw buffer.
ssize_tread virtualDeserialises the message from a JSON string.
voidwrite virtual constSerialises the message as JSON into a buffer.
boolisRequest constReturns true if no status code has been set (i.e. status() == -1).
size_tsize virtual constReturns the serialised JSON size in bytes.
voidprint virtual constPretty-prints the message JSON to the given stream.
const char *className virtual const inlineReturns the class name of this packet type for logging and diagnostics.
char *data virtual const inlineThe packet data pointer for buffered packets.
boolhasData virtual const inlineReturns true if the packet has a non-null data pointer.

Message

Message()

Constructs a new message with a random ID and type set to "message".


Message

Message(const json::Value & root)

Constructs a message from a JSON value; sets missing id/type fields.

Parameters

  • root JSON object to initialise from.

Message

Message(const Message & root)

Copy constructor; preserves or generates id/type fields.

Parameters

  • root Source message.

clone

virtual const

virtual std::unique_ptr< IPacket > clone() const

Returns a heap-allocated copy of this message.


valid

virtual const

virtual bool valid() const

Returns true if the message has both type and id fields.


clear

virtual

virtual void clear()

Clears all JSON fields from this message.


clearData

virtual

virtual void clearData()

Clears the data sub-object.


clearNotes

virtual

virtual void clearNotes()

Clears the notes array.


type

const

std::string type() const

Returns the message type string (defaults to "message").


id

const

std::string id() const

Returns the message ID string.


to

const

Address to() const

Returns the recipient address parsed from the to field.


from

const

Address from() const

Returns the sender address parsed from the from field.


toUser

const

std::string toUser() const

Returns the user component of the to address without constructing an Address.


toId

const

std::string toId() const

Returns the id component of the to address without constructing an Address.


fromUser

const

std::string fromUser() const

Returns the user component of the from address without constructing an Address.


fromId

const

std::string fromId() const

Returns the id component of the from address without constructing an Address.


status

const

int status() const

Returns the HTTP status code, or -1 if not set.


setType

void setType(std::string_view type)

Sets the message type field.

Parameters

  • type Type string (e.g. "message", "command").

setTo

void setTo(const Peer & to)

Sets the to field from a peer's address.

Parameters

  • to Destination peer.

setTo

void setTo(const Address & to)

Sets the to field from an address object.

Parameters

  • to Destination address.

setTo

void setTo(std::string_view to)

Sets the to field from an address string.

Parameters

  • to Destination address string.

setFrom

void setFrom(const Peer & from)

Sets the from field from a peer's address.

Parameters

  • from Sender peer.

setFrom

void setFrom(const Address & from)

Sets the from field from an address object.

Parameters

  • from Sender address.

setFrom

void setFrom(std::string_view from)

Sets the from field from an address string.

Parameters

  • from Sender address string.

setStatus

void setStatus(int code)

HTTP status codes are used to describe the message response. Throws std::invalid_argument if code is outside [101, 504].

Parameters

  • code HTTP status code.

See also: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html


notes

json::Value & notes()

Returns a reference to the notes JSON array.


setNote

void setNote(std::string_view type, std::string_view text)

Replaces all notes with a single note.

Parameters

  • type Note severity: info, warn, or error.

  • text Note message text.


addNote

void addNote(std::string_view type, std::string_view text)

Appends a note to the notes array.

Parameters

  • type Note severity: info, warn, or error.

  • text Note message text.


data

const

json::Value data(std::string_view name) const

Returns a copy of the named data field.

Parameters

  • name Field name within data.

data

json::Value & data(std::string_view name)

Returns a reference to the named data field (creates it if absent).

Parameters

  • name Field name within data.

setData

json::Value & setData(std::string_view name)

Creates or replaces a named data field; returns a reference to it.

Parameters

  • name Field name within data.

setData

void setData(std::string_view name, const char * data)

Sets a named data field to a C-string value.

Parameters

  • name Field name within data.

  • data String value to assign.


setData

void setData(std::string_view name, std::string_view data)

Sets a named data field to a string value.

Parameters

  • name Field name within data.

  • data String value to assign.


setData

void setData(std::string_view name, const json::Value & data)

Sets a named data field to a JSON value.

Parameters

  • name Field name within data.

  • data JSON value to assign.


setData

void setData(std::string_view name, int data)

Sets a named data field to an integer value.

Parameters

  • name Field name within data.

  • data Integer value to assign.


removeData

void removeData(std::string_view name)

Removes a named field from the data sub-object.

Parameters

  • name Field name to remove.

hasData

bool hasData(std::string_view name)

Returns true if the named field exists in the data sub-object.

Parameters

  • name Field name to look up.

read

virtual

virtual ssize_t read(const ConstBuffer & buf)

Deserialises the message from a raw buffer.

Parameters

  • buf Buffer containing the JSON payload.

Returns

Number of bytes consumed.


read

virtual

virtual ssize_t read(const std::string & root)

Deserialises the message from a JSON string.

Parameters

  • root JSON string to parse.

Returns

Number of characters consumed.


write

virtual const

virtual void write(Buffer & buf) const

Serialises the message as JSON into a buffer.

Parameters

  • buf Buffer to append to.

isRequest

const

bool isRequest() const

Returns true if no status code has been set (i.e. status() == -1).


size

virtual const

virtual size_t size() const

Returns the serialised JSON size in bytes.


print

virtual const

virtual void print(std::ostream & os) const

Pretty-prints the message JSON to the given stream.

Parameters

  • os Output stream.

className

virtual const inline

virtual inline const char * className() const

Returns the class name of this packet type for logging and diagnostics.


data

virtual const inline

virtual inline char * data() const

The packet data pointer for buffered packets.


hasData

virtual const inline

virtual inline bool hasData() const

Returns true if the packet has a non-null data pointer.