Message
Message
#include <icy/symple/message.h>Inherits:
Value,IPacketSubclassed by:Command,Event,Presence
Base Symple protocol message with addressing, status, data, and notes.
Public Methods
| Return | Name | Description |
|---|---|---|
Message | Constructs a new message with a random ID and type set to "message". | |
Message | Constructs a message from a JSON value; sets missing id/type fields. | |
Message | Copy constructor; preserves or generates id/type fields. | |
std::unique_ptr< IPacket > | clone virtual const | Returns a heap-allocated copy of this message. |
bool | valid virtual const | Returns true if the message has both type and id fields. |
void | clear virtual | Clears all JSON fields from this message. |
void | clearData virtual | Clears the data sub-object. |
void | clearNotes virtual | Clears the notes array. |
std::string | type const | Returns the message type string (defaults to "message"). |
std::string | id const | Returns the message ID string. |
Address | to const | Returns the recipient address parsed from the to field. |
Address | from const | Returns the sender address parsed from the from field. |
std::string | toUser const | Returns the user component of the to address without constructing an Address. |
std::string | toId const | Returns the id component of the to address without constructing an Address. |
std::string | fromUser const | Returns the user component of the from address without constructing an Address. |
std::string | fromId const | Returns the id component of the from address without constructing an Address. |
int | status const | Returns the HTTP status code, or -1 if not set. |
void | setType | Sets the message type field. |
void | setTo | Sets the to field from a peer's address. |
void | setTo | Sets the to field from an address object. |
void | setTo | Sets the to field from an address string. |
void | setFrom | Sets the from field from a peer's address. |
void | setFrom | Sets the from field from an address object. |
void | setFrom | Sets the from field from an address string. |
void | setStatus | HTTP status codes are used to describe the message response. Throws std::invalid_argument if code is outside [101, 504]. |
json::Value & | notes | Returns a reference to the notes JSON array. |
void | setNote | Replaces all notes with a single note. |
void | addNote | Appends a note to the notes array. |
json::Value | data const | Returns a copy of the named data field. |
json::Value & | data | Returns a reference to the named data field (creates it if absent). |
json::Value & | setData | Creates or replaces a named data field; returns a reference to it. |
void | setData | Sets a named data field to a C-string value. |
void | setData | Sets a named data field to a string value. |
void | setData | Sets a named data field to a JSON value. |
void | setData | Sets a named data field to an integer value. |
void | removeData | Removes a named field from the data sub-object. |
bool | hasData | Returns true if the named field exists in the data sub-object. |
ssize_t | read virtual | Deserialises the message from a raw buffer. |
ssize_t | read virtual | Deserialises the message from a JSON string. |
void | write virtual const | Serialises the message as JSON into a buffer. |
bool | isRequest const | Returns true if no status code has been set (i.e. status() == -1). |
size_t | size virtual const | Returns the serialised JSON size in bytes. |
void | print virtual const | Pretty-prints the message JSON to the given stream. |
const char * | className virtual const inline | Returns the class name of this packet type for logging and diagnostics. |
char * | data virtual const inline | The packet data pointer for buffered packets. |
bool | hasData virtual const inline | Returns 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
rootJSON object to initialise from.
Message
Message(const Message & root)Copy constructor; preserves or generates id/type fields.
Parameters
rootSource message.
clone
virtual const
virtual std::unique_ptr< IPacket > clone() constReturns a heap-allocated copy of this message.
valid
virtual const
virtual bool valid() constReturns 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() constReturns the message type string (defaults to "message").
id
const
std::string id() constReturns the message ID string.
to
const
Address to() constReturns the recipient address parsed from the to field.
from
const
Address from() constReturns the sender address parsed from the from field.
toUser
const
std::string toUser() constReturns the user component of the to address without constructing an Address.
toId
const
std::string toId() constReturns the id component of the to address without constructing an Address.
fromUser
const
std::string fromUser() constReturns the user component of the from address without constructing an Address.
fromId
const
std::string fromId() constReturns the id component of the from address without constructing an Address.
status
const
int status() constReturns the HTTP status code, or -1 if not set.
setType
void setType(std::string_view type)Sets the message type field.
Parameters
typeType string (e.g. "message", "command").
setTo
void setTo(const Peer & to)Sets the to field from a peer's address.
Parameters
toDestination peer.
setTo
void setTo(const Address & to)Sets the to field from an address object.
Parameters
toDestination address.
setTo
void setTo(std::string_view to)Sets the to field from an address string.
Parameters
toDestination address string.
setFrom
void setFrom(const Peer & from)Sets the from field from a peer's address.
Parameters
fromSender peer.
setFrom
void setFrom(const Address & from)Sets the from field from an address object.
Parameters
fromSender address.
setFrom
void setFrom(std::string_view from)Sets the from field from an address string.
Parameters
fromSender 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
codeHTTP 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
typeNote severity:info,warn, orerror.textNote message text.
addNote
void addNote(std::string_view type, std::string_view text)Appends a note to the notes array.
Parameters
typeNote severity:info,warn, orerror.textNote message text.
data
const
json::Value data(std::string_view name) constReturns a copy of the named data field.
Parameters
nameField name withindata.
data
json::Value & data(std::string_view name)Returns a reference to the named data field (creates it if absent).
Parameters
nameField name withindata.
setData
json::Value & setData(std::string_view name)Creates or replaces a named data field; returns a reference to it.
Parameters
nameField name withindata.
setData
void setData(std::string_view name, const char * data)Sets a named data field to a C-string value.
Parameters
nameField name withindata.dataString value to assign.
setData
void setData(std::string_view name, std::string_view data)Sets a named data field to a string value.
Parameters
nameField name withindata.dataString value to assign.
setData
void setData(std::string_view name, const json::Value & data)Sets a named data field to a JSON value.
Parameters
nameField name withindata.dataJSON value to assign.
setData
void setData(std::string_view name, int data)Sets a named data field to an integer value.
Parameters
nameField name withindata.dataInteger value to assign.
removeData
void removeData(std::string_view name)Removes a named field from the data sub-object.
Parameters
nameField name to remove.
hasData
bool hasData(std::string_view name)Returns true if the named field exists in the data sub-object.
Parameters
nameField name to look up.
read
virtual
virtual ssize_t read(const ConstBuffer & buf)Deserialises the message from a raw buffer.
Parameters
bufBuffer 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
rootJSON string to parse.
Returns
Number of characters consumed.
write
virtual const
virtual void write(Buffer & buf) constSerialises the message as JSON into a buffer.
Parameters
bufBuffer to append to.
isRequest
const
bool isRequest() constReturns true if no status code has been set (i.e. status() == -1).
size
virtual const
virtual size_t size() constReturns the serialised JSON size in bytes.
virtual const
virtual void print(std::ostream & os) constPretty-prints the message JSON to the given stream.
Parameters
osOutput stream.
className
virtual const inline
virtual inline const char * className() constReturns the class name of this packet type for logging and diagnostics.
data
virtual const inline
virtual inline char * data() constThe packet data pointer for buffered packets.
hasData
virtual const inline
virtual inline bool hasData() constReturns true if the packet has a non-null data pointer.
