Wavelength
Privacy-focused, cross-platform, and open-source communication application
Loading...
Searching...
No Matches
MessageHandler Class Referencefinal

Singleton class responsible for handling WebSocket message operations. More...

#include <message_handler.h>

Inheritance diagram for MessageHandler:
Collaboration diagram for MessageHandler:

Public Member Functions

void ClearProcessedMessages ()
 Clears the internal cache of processed message IDs.
 
bool IsMessageProcessed (const QString &message_id) const
 Checks if a message with the given ID has already been processed.
 
void MarkMessageAsProcessed (const QString &message_id)
 Marks a message ID as processed by adding it to the internal set. Also manages the size of the processed ID cache, removing older entries if the maximum size is exceeded.
 

Static Public Member Functions

static QJsonObject CreateAuthRequest (const QString &frequency, const QString &password, const QString &client_id)
 Creates a JSON object representing an authentication request.
 
static QJsonObject CreateLeaveRequest (const QString &frequency, bool is_host)
 Creates a JSON object representing a request to leave or close a frequency.
 
static QJsonObject CreateRegisterRequest (const QString &frequency, bool is_password_protected, const QString &password, const QString &host_id)
 Creates a JSON object representing a request to register a new frequency.
 
static QString GenerateMessageId ()
 Generates a unique message identifier using UUID.
 
static MessageHandlerGetInstance ()
 Gets the singleton instance of the MessageHandler.
 
static QString GetMessageContent (const QJsonObject &message_object)
 Extracts the message content string from a parsed message object.
 
static QString GetMessageFrequency (const QJsonObject &message_object)
 Extracts the frequency string from a parsed message object.
 
static QString GetMessageId (const QJsonObject &message_object)
 Extracts the message ID string from a parsed message object.
 
static QString GetMessageSenderId (const QJsonObject &message_object)
 Extracts the sender ID string from a parsed message object.
 
static QString GetMessageType (const QJsonObject &message_object)
 Extracts the message type string from a parsed message object.
 
static QJsonObject ParseMessage (const QString &message, bool *ok=nullptr)
 Parses a JSON string message into a QJsonObject.
 
static bool SendSystemCommand (QWebSocket *socket, const QString &command, const QJsonObject &params=QJsonObject())
 Sends a JSON-formatted system command over the specified WebSocket. Constructs a JSON object with the given command type and parameters, converts it to a JSON string, and sends it as a text message.
 

Private Member Functions

 MessageHandler (const MessageHandler &)=delete
 Deleted copy constructor to prevent copying.
 
 MessageHandler (QObject *parent=nullptr)
 Private constructor to enforce the singleton pattern.
 
MessageHandleroperator= (const MessageHandler &)=delete
 Deleted assignment operator to prevent assignment.
 
 ~MessageHandler () override=default
 Private destructor.
 

Private Attributes

QSet< QString > processed_message_ids_
 Set storing the IDs of messages that have already been processed to prevent duplicates.
 

Static Private Attributes

static constexpr int kMaxCachedMessageIds = 200
 Maximum number of message IDs to keep in the processed cache before removing older ones.
 

Detailed Description

Singleton class responsible for handling WebSocket message operations.

Provides static methods for creating, parsing, and extracting information from JSON-based messages used in the WebSocket communication protocol. It also manages to send system commands and keeps track of processed message IDs to prevent duplicates.

Constructor & Destructor Documentation

◆ MessageHandler() [1/2]

MessageHandler::MessageHandler ( QObject * parent = nullptr)
inlineexplicitprivate

Private constructor to enforce the singleton pattern.

Parameters
parentOptional parent QObject.
Here is the caller graph for this function:

◆ ~MessageHandler()

MessageHandler::~MessageHandler ( )
overrideprivatedefault

Private destructor.

◆ MessageHandler() [2/2]

MessageHandler::MessageHandler ( const MessageHandler & )
privatedelete

Deleted copy constructor to prevent copying.

Here is the call graph for this function:

Member Function Documentation

◆ ClearProcessedMessages()

void MessageHandler::ClearProcessedMessages ( )
inline

Clears the internal cache of processed message IDs.

◆ CreateAuthRequest()

QJsonObject MessageHandler::CreateAuthRequest ( const QString & frequency,
const QString & password,
const QString & client_id )
static

Creates a JSON object representing an authentication request.

Parameters
frequencyThe frequency the client wants to join.
passwordThe password for the frequency (if required).
client_idThe unique identifier of the client sending the request.
Returns
A QJsonObject formatted as an authentication request.

◆ CreateLeaveRequest()

QJsonObject MessageHandler::CreateLeaveRequest ( const QString & frequency,
bool is_host )
static

Creates a JSON object representing a request to leave or close a frequency.

Parameters
frequencyThe frequency to leave or close.
is_hostTrue if the client is the host (closing the frequency), false otherwise (leaving).
Returns
A QJsonObject formatted as a leave or close request.

◆ CreateRegisterRequest()

QJsonObject MessageHandler::CreateRegisterRequest ( const QString & frequency,
bool is_password_protected,
const QString & password,
const QString & host_id )
static

Creates a JSON object representing a request to register a new frequency.

Parameters
frequencyThe desired name for the new frequency.
is_password_protectedFlag indicating if the frequency should require a password.
passwordThe password to set for the frequency (if protected).
host_idThe unique identifier of the client hosting the frequency.
Returns
A QJsonObject formatted as a frequency registration request.

◆ GenerateMessageId()

static QString MessageHandler::GenerateMessageId ( )
inlinestatic

Generates a unique message identifier using UUID.

Returns
A unique QString identifier (UUID without braces).
Here is the caller graph for this function:

◆ GetInstance()

static MessageHandler * MessageHandler::GetInstance ( )
inlinestatic

Gets the singleton instance of the MessageHandler.

Returns
Pointer to the singleton MessageHandler instance.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetMessageContent()

static QString MessageHandler::GetMessageContent ( const QJsonObject & message_object)
inlinestatic

Extracts the message content string from a parsed message object.

Parameters
message_objectThe QJsonObject representing the parsed message.
Returns
The value of the "content" field as a QString.

◆ GetMessageFrequency()

static QString MessageHandler::GetMessageFrequency ( const QJsonObject & message_object)
inlinestatic

Extracts the frequency string from a parsed message object.

Parameters
message_objectThe QJsonObject representing the parsed message.
Returns
The value of the "frequency" field as a QString.
Here is the caller graph for this function:

◆ GetMessageId()

static QString MessageHandler::GetMessageId ( const QJsonObject & message_object)
inlinestatic

Extracts the message ID string from a parsed message object.

Parameters
message_objectThe QJsonObject representing the parsed message.
Returns
The value of the "messageId" field as a QString.
Here is the caller graph for this function:

◆ GetMessageSenderId()

static QString MessageHandler::GetMessageSenderId ( const QJsonObject & message_object)
inlinestatic

Extracts the sender ID string from a parsed message object.

Parameters
message_objectThe QJsonObject representing the parsed message.
Returns
The value of the "senderId" field as a QString.

◆ GetMessageType()

static QString MessageHandler::GetMessageType ( const QJsonObject & message_object)
inlinestatic

Extracts the message type string from a parsed message object.

Parameters
message_objectThe QJsonObject representing the parsed message.
Returns
The value of the "type" field as a QString.
Here is the caller graph for this function:

◆ IsMessageProcessed()

bool MessageHandler::IsMessageProcessed ( const QString & message_id) const
inline

Checks if a message with the given ID has already been processed.

Parameters
message_idThe unique identifier of the message to check.
Returns
True if the message ID exists in the processed set, false otherwise.

◆ MarkMessageAsProcessed()

void MessageHandler::MarkMessageAsProcessed ( const QString & message_id)

Marks a message ID as processed by adding it to the internal set. Also manages the size of the processed ID cache, removing older entries if the maximum size is exceeded.

Parameters
message_idThe unique identifier of the message to mark as processed.
Here is the caller graph for this function:

◆ operator=()

MessageHandler & MessageHandler::operator= ( const MessageHandler & )
privatedelete

Deleted assignment operator to prevent assignment.

Here is the call graph for this function:

◆ ParseMessage()

QJsonObject MessageHandler::ParseMessage ( const QString & message,
bool * ok = nullptr )
static

Parses a JSON string message into a QJsonObject.

Parameters
messageThe QString containing the JSON message data.
okOptional pointer to a boolean flag that will be set to true on success, false on failure.
Returns
The parsed QJsonObject on success, or an empty QJsonObject on failure.
Here is the caller graph for this function:

◆ SendSystemCommand()

bool MessageHandler::SendSystemCommand ( QWebSocket * socket,
const QString & command,
const QJsonObject & params = QJsonObject() )
static

Sends a JSON-formatted system command over the specified WebSocket. Constructs a JSON object with the given command type and parameters, converts it to a JSON string, and sends it as a text message.

Parameters
socketPointer to the QWebSocket to send the command through.
commandThe type string for the command (e.g., "auth", "register_wavelength").
paramsOptional QJsonObject containing additional parameters for the command.
Returns
True if the command was sent successfully (socket valid), false otherwise.
Here is the caller graph for this function:

Member Data Documentation

◆ kMaxCachedMessageIds

int MessageHandler::kMaxCachedMessageIds = 200
staticconstexprprivate

Maximum number of message IDs to keep in the processed cache before removing older ones.

◆ processed_message_ids_

QSet<QString> MessageHandler::processed_message_ids_
private

Set storing the IDs of messages that have already been processed to prevent duplicates.


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