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

Singleton service responsible for sending messages and files over WebSocket connections. More...

#include <message_service.h>

Inheritance diagram for MessageService:
Collaboration diagram for MessageService:

Public Slots

void HandleSendJsonViaSocket (const QString &json_message, const QString &frequency, const QString &progress_message_id)
 Slot connected to the sendJsonViaSocket signal. Performs the actual sending of a JSON message. Finds the socket for the frequency and sends the message. Updates the progress message to indicate success or failure. This runs on the main thread.
 
void UpdateProgressMessage (const QString &progress_message_id, const QString &message)
 Slot to update a progress message associated with a file transfer. Emits the progressMessageUpdated signal.
 

Signals

void audioDataReceived (QString frequency, const QByteArray &audio_data)
 Emitted when raw audio data is received via a binary WebSocket message. Relayed from WavelengthMessageProcessor.
 
void messageSent (QString frequency, const QString &formatted_message)
 Emitted immediately after a text message is successfully sent via the socket.
 
void progressMessageUpdated (const QString &message_id, const QString &message)
 Emitted during file processing and sending to update the status display.
 
void pttDenied (QString frequency, QString reason)
 Emitted when the server denies permission to transmit audio (Push-to-Talk). Relayed from WavelengthMessageProcessor.
 
void pttGranted (QString frequency)
 Emitted when the server grants permission to transmit audio (Push-to-Talk). Relayed from WavelengthMessageProcessor.
 
void pttStartReceiving (QString frequency, QString sender_id)
 Emitted when another user starts transmitting audio on the frequency. Relayed from WavelengthMessageProcessor.
 
void pttStopReceiving (QString frequency)
 Emitted when the currently transmitting user stops sending audio. Relayed from WavelengthMessageProcessor.
 
void remoteAudioAmplitudeUpdate (QString frequency, qreal amplitude)
 Emitted when the server sends an update about the remote audio amplitude (optional). Relayed from WavelengthMessageProcessor.
 
void removeProgressMessage (const QString &message_id)
 Emitted after a file transfer is complete (success or failure) to remove the progress message.
 
void sendJsonViaSocket (const QString &json_message, QString frequency, const QString &progress_message_id)
 Internal signal emitted by the background file processing task when the JSON message is ready to be sent. Connected to the HandleSendJsonViaSocket slot.
 

Public Member Functions

void ClearSentMessageCache ()
 Clears the internal cache of sent message contents.
 
QString GetClientId () const
 Gets the client ID currently associated with this service instance.
 
QMap< QString, QString > * GetSentMessageCache ()
 Gets a pointer to the internal cache of sent message contents. The cache maps message ID to message content.
 
 MessageService (const MessageService &)=delete
 Deleted copy constructor to prevent copying.
 
MessageServiceoperator= (const MessageService &)=delete
 Deleted assignment operator to prevent assignment.
 
bool SendFile (const QString &file_path, const QString &progress_message_id=QString())
 Initiates the process of sending a file to the currently active frequency. Reads the file, encodes it to base64, determines a file type, and constructs a JSON message of a type "send_file" containing the metadata and encoded data. This entire process (reading, encoding) is offloaded to a background thread using AttachmentQueueManager. Emits progressMessageUpdated signals to track the process. Finally, emits sendJsonViaSocket to trigger the actual sending on the main thread.
 
bool SendFileToServer (const QString &json_message, const QString &frequency, const QString &progress_message_id)
 Sends a pre-formatted JSON message (typically containing file data) to the server. This method is intended to be called internally or via signals after file processing. Finds the socket for the given frequency and sends the JSON string. Updates the progress message to indicate success or failure. Schedules removal of the progress message.
 
bool SendTextMessage (const QString &message)
 Sends a text message to the currently active frequency. Retrieves the active frequency and socket from WavelengthRegistry. Constructs a JSON message of type "send_message" including content, sender ID, timestamp, and a unique message ID. Sends the message via the socket. Caches sent message content locally.
 
void SetClientId (const QString &clientId)
 Sets the client ID for this service instance.
 

Static Public Member Functions

static MessageServiceGetInstance ()
 Gets the singleton instance of the MessageService.
 
static bool SendAudioData (const QString &frequency, const QByteArray &audio_data)
 Sends raw audio data as a binary message for the specified frequency. Used for transmitting audio during an active PTT session.
 
static bool SendPttRelease (const QString &frequency)
 Sends a Push-to-Talk (PTT) release message for the specified frequency. Constructs a JSON message of type "release_ptt" and sends it via the frequency's socket.
 
static bool SendPttRequest (const QString &frequency)
 Sends a Push-to-Talk (PTT) request message for the specified frequency. Constructs a JSON message of type "request_ptt" and sends it via the frequency's socket.
 

Private Member Functions

 MessageService (QObject *parent=nullptr)
 Private constructor to enforce the singleton pattern. Connects the internal sendJsonViaSocket signal to the HandleSendJsonViaSocket slot.
 
 ~MessageService () override=default
 Private destructor.
 

Static Private Member Functions

static QWebSocket * GetSocketForFrequency (const QString &frequency)
 Helper function to retrieve the active WebSocket connection for a given frequency. Uses WavelengthRegistry to look up the socket. Includes checks for validity.
 

Private Attributes

QString client_id_
 Stores the client ID associated with this service instance.
 
QMap< QString, QString > sent_messages_
 Cache storing the content of recently sent text messages, mapped by message ID.
 

Detailed Description

Singleton service responsible for sending messages and files over WebSocket connections.

This class manages the sending of text messages, files (as base64 encoded JSON), Push-to-Talk (PTT) requests/releases, and raw audio data for specific frequencies (wavelengths). It interacts with WavelengthRegistry to find the appropriate WebSocket connection for a given frequency. File sending is handled asynchronously using AttachmentQueueManager to avoid blocking the main thread. It provides signals for tracking message sending progress and status, as well as PTT and audio events.

Constructor & Destructor Documentation

◆ MessageService() [1/2]

MessageService::MessageService ( const MessageService & )
delete

Deleted copy constructor to prevent copying.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MessageService() [2/2]

MessageService::MessageService ( QObject * parent = nullptr)
explicitprivate

Private constructor to enforce the singleton pattern. Connects the internal sendJsonViaSocket signal to the HandleSendJsonViaSocket slot.

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

◆ ~MessageService()

MessageService::~MessageService ( )
overrideprivatedefault

Private destructor.

Member Function Documentation

◆ audioDataReceived

void MessageService::audioDataReceived ( QString frequency,
const QByteArray & audio_data )
signal

Emitted when raw audio data is received via a binary WebSocket message. Relayed from WavelengthMessageProcessor.

Parameters
frequencyThe frequency the audio data belongs to.
audio_dataThe raw audio data bytes.
Here is the caller graph for this function:

◆ ClearSentMessageCache()

void MessageService::ClearSentMessageCache ( )
inline

Clears the internal cache of sent message contents.

◆ GetClientId()

QString MessageService::GetClientId ( ) const
inlinenodiscard

Gets the client ID currently associated with this service instance.

Returns
The client ID string.

◆ GetInstance()

static MessageService * MessageService::GetInstance ( )
inlinestatic

Gets the singleton instance of the MessageService.

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

◆ GetSentMessageCache()

QMap< QString, QString > * MessageService::GetSentMessageCache ( )
inline

Gets a pointer to the internal cache of sent message contents. The cache maps message ID to message content.

Returns
Pointer to the QMap storing sent message contents.

◆ GetSocketForFrequency()

QWebSocket * MessageService::GetSocketForFrequency ( const QString & frequency)
staticprivate

Helper function to retrieve the active WebSocket connection for a given frequency. Uses WavelengthRegistry to look up the socket. Includes checks for validity.

Parameters
frequencyThe frequency whose socket is needed.
Returns
Pointer to the QWebSocket if found and valid, nullptr otherwise.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ HandleSendJsonViaSocket

void MessageService::HandleSendJsonViaSocket ( const QString & json_message,
const QString & frequency,
const QString & progress_message_id )
slot

Slot connected to the sendJsonViaSocket signal. Performs the actual sending of a JSON message. Finds the socket for the frequency and sends the message. Updates the progress message to indicate success or failure. This runs on the main thread.

Parameters
json_messageThe JSON string message to send (usually containing file data).
frequencyThe target frequency.
progress_message_idThe ID associated with the progress message for this transfer.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ messageSent

void MessageService::messageSent ( QString frequency,
const QString & formatted_message )
signal

Emitted immediately after a text message is successfully sent via the socket.

Parameters
frequencyThe frequency the message was sent to.
formatted_messageThe HTML-formatted version of the sent message (for local display).
Here is the caller graph for this function:

◆ operator=()

MessageService & MessageService::operator= ( const MessageService & )
delete

Deleted assignment operator to prevent assignment.

Here is the call graph for this function:

◆ progressMessageUpdated

void MessageService::progressMessageUpdated ( const QString & message_id,
const QString & message )
signal

Emitted during file processing and sending to update the status display.

Parameters
message_idThe unique ID associated with this file transfer's progress messages.
messageThe HTML-formatted status update message.
Here is the caller graph for this function:

◆ pttDenied

void MessageService::pttDenied ( QString frequency,
QString reason )
signal

Emitted when the server denies permission to transmit audio (Push-to-Talk). Relayed from WavelengthMessageProcessor.

Parameters
frequencyThe frequency for which PTT was denied.
reasonThe reason provided for the denial.
Here is the caller graph for this function:

◆ pttGranted

void MessageService::pttGranted ( QString frequency)
signal

Emitted when the server grants permission to transmit audio (Push-to-Talk). Relayed from WavelengthMessageProcessor.

Parameters
frequencyThe frequency for which PTT was granted.
Here is the caller graph for this function:

◆ pttStartReceiving

void MessageService::pttStartReceiving ( QString frequency,
QString sender_id )
signal

Emitted when another user starts transmitting audio on the frequency. Relayed from WavelengthMessageProcessor.

Parameters
frequencyThe frequency where transmission started.
sender_idThe ID of the user transmitting.
Here is the caller graph for this function:

◆ pttStopReceiving

void MessageService::pttStopReceiving ( QString frequency)
signal

Emitted when the currently transmitting user stops sending audio. Relayed from WavelengthMessageProcessor.

Parameters
frequencyThe frequency where transmission stopped.
Here is the caller graph for this function:

◆ remoteAudioAmplitudeUpdate

void MessageService::remoteAudioAmplitudeUpdate ( QString frequency,
qreal amplitude )
signal

Emitted when the server sends an update about the remote audio amplitude (optional). Relayed from WavelengthMessageProcessor.

Parameters
frequencyThe frequency the amplitude update is for.
amplitudeThe current amplitude level (typically 0.0 to 1.0).
Here is the caller graph for this function:

◆ removeProgressMessage

void MessageService::removeProgressMessage ( const QString & message_id)
signal

Emitted after a file transfer is complete (success or failure) to remove the progress message.

Parameters
message_idThe unique ID of the progress message to remove.
Here is the caller graph for this function:

◆ SendAudioData()

bool MessageService::SendAudioData ( const QString & frequency,
const QByteArray & audio_data )
static

Sends raw audio data as a binary message for the specified frequency. Used for transmitting audio during an active PTT session.

Parameters
frequencyThe frequency the audio data belongs to.
audio_dataThe raw audio data bytes.
Returns
True if the binary message was sent successfully (socket valid and data sent), false otherwise.
Here is the call graph for this function:

◆ SendFile()

bool MessageService::SendFile ( const QString & file_path,
const QString & progress_message_id = QString() )

Initiates the process of sending a file to the currently active frequency. Reads the file, encodes it to base64, determines a file type, and constructs a JSON message of a type "send_file" containing the metadata and encoded data. This entire process (reading, encoding) is offloaded to a background thread using AttachmentQueueManager. Emits progressMessageUpdated signals to track the process. Finally, emits sendJsonViaSocket to trigger the actual sending on the main thread.

Parameters
file_pathThe local path to the file to be sent.
progress_message_idOptional unique ID to associate with progress update messages. If empty, a new one is generated.
Returns
True if the file sending a task was successfully queued, false otherwise (e.g., empty path).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SendFileToServer()

bool MessageService::SendFileToServer ( const QString & json_message,
const QString & frequency,
const QString & progress_message_id )

Sends a pre-formatted JSON message (typically containing file data) to the server. This method is intended to be called internally or via signals after file processing. Finds the socket for the given frequency and sends the JSON string. Updates the progress message to indicate success or failure. Schedules removal of the progress message.

Parameters
json_messageThe JSON string message to send.
frequencyThe target frequency.
progress_message_idThe ID associated with the progress message for this file transfer.
Returns
True if the message was sent successfully, false if the socket is invalid.
Deprecated
This method seems redundant with HandleSendJsonViaSocket and might be removed.
Here is the call graph for this function:

◆ sendJsonViaSocket

void MessageService::sendJsonViaSocket ( const QString & json_message,
QString frequency,
const QString & progress_message_id )
signal

Internal signal emitted by the background file processing task when the JSON message is ready to be sent. Connected to the HandleSendJsonViaSocket slot.

Parameters
json_messageThe complete JSON message string containing file metadata and base64 data.
frequencyThe target frequency.
progress_message_idThe ID associated with the progress message for this transfer.
Here is the caller graph for this function:

◆ SendPttRelease()

bool MessageService::SendPttRelease ( const QString & frequency)
static

Sends a Push-to-Talk (PTT) release message for the specified frequency. Constructs a JSON message of type "release_ptt" and sends it via the frequency's socket.

Parameters
frequencyThe frequency for which PTT is being released.
Returns
True if the release message was sent successfully (socket valid), false otherwise.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SendPttRequest()

bool MessageService::SendPttRequest ( const QString & frequency)
static

Sends a Push-to-Talk (PTT) request message for the specified frequency. Constructs a JSON message of type "request_ptt" and sends it via the frequency's socket.

Parameters
frequencyThe frequency for which PTT is requested.
Returns
True if the request was sent successfully (socket valid), false otherwise.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SendTextMessage()

bool MessageService::SendTextMessage ( const QString & message)

Sends a text message to the currently active frequency. Retrieves the active frequency and socket from WavelengthRegistry. Constructs a JSON message of type "send_message" including content, sender ID, timestamp, and a unique message ID. Sends the message via the socket. Caches sent message content locally.

Parameters
messageThe text content of the message to send.
Returns
True if the message was sent successfully, false if no active frequency, socket invalid, etc.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetClientId()

void MessageService::SetClientId ( const QString & clientId)
inline

Sets the client ID for this service instance.

Parameters
clientIdThe client ID string to set.

◆ UpdateProgressMessage

void MessageService::UpdateProgressMessage ( const QString & progress_message_id,
const QString & message )
inlineslot

Slot to update a progress message associated with a file transfer. Emits the progressMessageUpdated signal.

Parameters
progress_message_idThe ID of the progress message to update.
messageThe new HTML-formatted status message.
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ client_id_

QString MessageService::client_id_
private

Stores the client ID associated with this service instance.

◆ sent_messages_

QMap<QString, QString> MessageService::sent_messages_
private

Cache storing the content of recently sent text messages, mapped by message ID.


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