CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
cmlabs::JSONM Class Reference

JSONM: a JSON document with attached binary chunks in one contiguous buffer. More...

#include <NetworkProtocols.h>

Collaboration diagram for cmlabs::JSONM:
[legend]

Public Member Functions

 JSONM ()
 JSONM (const char *data, uint64 size)
 Parse a complete JSONM container from raw bytes.
 JSONM (const char *json)
 Create a container from JSON text only (no attachments yet).
 ~JSONM ()
uint64 getSize ()
uint32 getCount ()
bool isValid ()
std::string getJSON ()
const char * getData (const char *name, uint64 &size)
 Get an attachment's bytes by name.
const char * getData (uint32 chunk, uint64 &size)
 Get an attachment's bytes by chunk index.
std::string getDataType (const char *name)
std::string getDataType (uint32 chunk)
std::string getDataName (uint32 chunk)
bool setJSON (const char *json)
 Replace the JSON text portion (multipart metadata is regenerated).
uint32 addData (const char *name, const char *data, uint64 size, const char *type)
 Append a binary attachment.
std::string getInfoString ()
DataMessageconvertToMessage ()
 Convert this container into a binary DataMessage.

Public Attributes

char * jmData
 Owned serialised container buffer (JSON + attachments).
uint64 jmSize
 Total size of ::jmData in bytes.
uint64 mOffset
 Byte offset where the binary multipart section begins.
std::map< uint32, JSONMEntryentries
 Attachment metadata by chunk index.

Detailed Description

JSONM: a JSON document with attached binary chunks in one contiguous buffer.

Wire format used to move JSON plus binary payloads (images, audio, etc.) as a single blob: the JSON text comes first and describes the attachments (name, size, MIME type); the raw binary chunks follow at recorded offsets. Construct from received bytes to parse, or build up with setJSON() + addData() to send. Internally serialised into ::jmData.

Note
Guarded by an internal mutex, so concurrent readers/writers on the same object are safe, but typical usage is single-threaded.

Definition at line 354 of file NetworkProtocols.h.

Constructor & Destructor Documentation

◆ JSONM() [1/3]

cmlabs::JSONM::JSONM ( )

Definition at line 2460 of file NetworkProtocols.cpp.

References jmData, jmSize, and mOffset.

◆ JSONM() [2/3]

cmlabs::JSONM::JSONM ( const char * data,
uint64 size )

Parse a complete JSONM container from raw bytes.

Parameters
dataContainer bytes (copied).
sizeTotal size in bytes.

Definition at line 2465 of file NetworkProtocols.cpp.

References jmData, jmSize, and mOffset.

◆ JSONM() [3/3]

cmlabs::JSONM::JSONM ( const char * json)

Create a container from JSON text only (no attachments yet).

Parameters
jsonNUL-terminated JSON text (copied).

Definition at line 2477 of file NetworkProtocols.cpp.

References jmData, jmSize, and setJSON().

◆ ~JSONM()

cmlabs::JSONM::~JSONM ( )

Definition at line 2486 of file NetworkProtocols.cpp.

References jmData, jmSize, and mOffset.

Member Function Documentation

◆ addData()

uint32 cmlabs::JSONM::addData ( const char * name,
const char * data,
uint64 size,
const char * type )

Append a binary attachment.

Parameters
nameAttachment name.
dataBytes (copied).
sizeByte count.
typeMIME type.
Returns
The new chunk index, or 0 on failure.

Definition at line 2675 of file NetworkProtocols.cpp.

References cmlabs::JSONMEntry::chunk, entries, getCount(), getData(), jmData, jmSize, cmlabs::JSONMEntry::name, cmlabs::JSONMEntry::offset, setJSON(), cmlabs::JSONMEntry::size, and cmlabs::JSONMEntry::type.

Referenced by cmlabs::RequestGateway::replyToClient(), and cmlabs::TestWebSocketRequestClient::run().

◆ convertToMessage()

DataMessage * cmlabs::JSONM::convertToMessage ( )

◆ getCount()

uint32 cmlabs::JSONM::getCount ( )
Returns
Number of binary attachment chunks.

Definition at line 2501 of file NetworkProtocols.cpp.

References entries.

Referenced by addData(), and getInfoString().

◆ getData() [1/2]

const char * cmlabs::JSONM::getData ( const char * name,
uint64 & size )

Get an attachment's bytes by name.

Parameters
nameAttachment name.
sizeOut: attachment size.
Returns
Pointer into the internal buffer (valid while this object lives), or NULL.

Definition at line 2538 of file NetworkProtocols.cpp.

References entries, jmData, jmSize, LOG_NETWORK, LogPrint, mOffset, and stricmp.

Referenced by addData().

◆ getData() [2/2]

const char * cmlabs::JSONM::getData ( uint32 chunk,
uint64 & size )

Get an attachment's bytes by chunk index.

Parameters
chunkChunk index.
sizeOut: attachment size.
Returns
Pointer into the internal buffer, or NULL.

Definition at line 2616 of file NetworkProtocols.cpp.

References entries, jmData, jmSize, LOG_NETWORK, LogPrint, and mOffset.

◆ getDataName()

std::string cmlabs::JSONM::getDataName ( uint32 chunk)
Parameters
chunkChunk index.
Returns
Its attachment name, or empty.

Definition at line 2597 of file NetworkProtocols.cpp.

References entries, jmData, and jmSize.

◆ getDataType() [1/2]

std::string cmlabs::JSONM::getDataType ( const char * name)
Parameters
nameAttachment name.
Returns
Its MIME type, or empty.

Definition at line 2564 of file NetworkProtocols.cpp.

References entries, jmData, jmSize, and stricmp.

◆ getDataType() [2/2]

std::string cmlabs::JSONM::getDataType ( uint32 chunk)
Parameters
chunkChunk index.
Returns
Its MIME type, or empty.

Definition at line 2582 of file NetworkProtocols.cpp.

References entries, jmData, and jmSize.

◆ getInfoString()

std::string cmlabs::JSONM::getInfoString ( )
Returns
Multi-line human-readable summary of all chunks.

Definition at line 2704 of file NetworkProtocols.cpp.

References cmlabs::utils::BytifySize(), entries, getCount(), jmData, jmSize, mOffset, and cmlabs::utils::StringFormat().

◆ getJSON()

std::string cmlabs::JSONM::getJSON ( )
Returns
The JSON text portion of the container.

Definition at line 2528 of file NetworkProtocols.cpp.

References jmData, jmSize, and cmlabs::utils::laststrstr().

Referenced by convertToMessage(), and cmlabs::RequestGateway::receiveWebsocketData().

◆ getSize()

uint64 cmlabs::JSONM::getSize ( )
Returns
Total serialised size (JSON plus all attachments) in bytes.

Definition at line 2495 of file NetworkProtocols.cpp.

References jmSize.

Referenced by cmlabs::TestWebSocketRequestClient::run().

◆ isValid()

bool cmlabs::JSONM::isValid ( )
Returns
true when the container holds parseable JSON and consistent chunk metadata.

Definition at line 2508 of file NetworkProtocols.cpp.

References entries, jmData, jmSize, cmlabs::utils::laststrstr(), LOG_NETWORK, and LogPrint.

Referenced by cmlabs::RequestGateway::receiveWebsocketData(), and cmlabs::TestWebSocketRequestClient::run().

◆ setJSON()

bool cmlabs::JSONM::setJSON ( const char * json)

Replace the JSON text portion (multipart metadata is regenerated).

Parameters
jsonNUL-terminated JSON text.
Returns
true on success.

Definition at line 2665 of file NetworkProtocols.cpp.

Referenced by addData(), JSONM(), cmlabs::RequestGateway::replyToClient(), and cmlabs::TestWebSocketRequestClient::run().

Member Data Documentation

◆ entries

std::map<uint32, JSONMEntry> cmlabs::JSONM::entries

◆ jmData

char* cmlabs::JSONM::jmData

◆ jmSize

◆ mOffset

uint64 cmlabs::JSONM::mOffset

Byte offset where the binary multipart section begins.

Definition at line 405 of file NetworkProtocols.h.

Referenced by convertToMessage(), getData(), getData(), getInfoString(), JSONM(), JSONM(), and ~JSONM().


The documentation for this class was generated from the following files: