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

Manages communication and interaction between multiple instances of the application. More...

#include <app_instance_manager.h>

Inheritance diagram for AppInstanceManager:
Collaboration diagram for AppInstanceManager:

Signals

void instanceConnected (QString instance_id)
 Emitted when a new client instance connects (on the server) or when connection to the server is established (on the client).
 
void instanceDisconnected (QString instance_id)
 Emitted when a client instance disconnects (on the server) or when the connection to the server is lost (on the client).
 
void otherInstancePositionHasChanged (QString instance_id, const QPointF &blob_center, const QPoint &window_position)
 Emitted when position data from another instance is received.
 

Public Member Functions

 AppInstanceManager (QMainWindow *window, BlobAnimation *blob, QObject *parent=nullptr)
 Constructs an AppInstanceManager.
 
QString GetInstanceId () const
 Gets the unique identifier for this instance.
 
bool IsCreator () const
 Checks if this instance is the creator (server - was opened first).
 
bool Start ()
 Starts the instance manager.
 
 ~AppInstanceManager () override
 Destructor. Cleans up the resources.
 

Static Public Member Functions

static bool IsAnotherInstanceRunning ()
 Statically checks if another instance of the application is already running.
 

Private Types

enum  MessageType : quint8 { kPositionUpdate = 1 , kIdentify = 5 , kIdentifyResponse = 6 }
 Defines the types of messages exchanged between instances. More...
 

Private Slots

void clientDisconnected ()
 Slot called when a connected socket disconnects. Handles cleanup and potential role change (the client becoming creator).
 
void onNewConnection ()
 Slot called when a new client attempts to connect to the server.
 
void readData ()
 Slot called when data can be read from a connected socket.
 
void sendPositionUpdate ()
 Slot called periodically by position_timer_ to send the current instance's position data.
 

Private Member Functions

void ApplyAttractionForce (const QPointF &target_position)
 Applies a force to the client window, pulling it towards the target position (creator window).
 
void ConnectToServer ()
 Establishes a connection to the server instance for a client instance.
 
QByteArray CreatePositionMessage () const
 Creates a QByteArray message containing the current instance's position data.
 
void FinalizeAbsorption ()
 Finalizes the absorption process by quitting the client application.
 
void InitAttractionThread ()
 Initializes and starts the background thread responsible for attraction logic.
 
bool ProcessMessage (const QByteArray &message, QLocalSocket *sender=nullptr)
 Processes an incoming message received from a socket.
 
void SendToAllClients (const QByteArray &message)
 Sends a message to all connected clients (only applicable for the creator instance).
 
void SetupServer ()
 Sets up the QLocalServer for the creator instance.
 
void StartAbsorptionAnimation ()
 Starts the animation sequence for absorbing a client instance. This involves moving the client window to the creator's position and fading it out.
 

Private Attributes

QTimer absorption_check_timer_
 Timer potentially used for absorption checks (currently seems unused).
 
std::unique_ptr< std::thread > attraction_thread_
 Background thread for attraction logic.
 
BlobAnimationblob_
 Pointer to the BlobAnimation object.
 
QHash< QLocalSocket *, QString > client_ids_
 Maps client sockets to their instance IDs (used only by the creator).
 
QVector< InstanceInfoconnected_instances_
 List of currently known connected instances. Protected by instances_mutex_.
 
QString instance_id_
 Unique identifier for this application instance.
 
QMutex instances_mutex_
 Mutex to protect access to connected_instances_.
 
std::atomic< bool > is_being_absorbed_ = false
 Atomic flag indicating if this client instance is currently being absorbed.
 
std::atomic< bool > is_creator_ {false}
 Atomic flag indicating if this instance is the creator.
 
std::atomic< bool > is_thread_running {false}
 Atomic flag to control the attraction thread's loop.
 
QMainWindow * main_window_
 Pointer to the main application window.
 
QTimer position_timer_
 Timer to periodically send position updates.
 
QLocalServer * server_ = nullptr
 Local server instance (used only by the creator).
 
QLocalSocket * socket_ = nullptr
 Local socket instance (used only by clients).
 

Static Private Attributes

static constexpr double kAbsorptionDistance = 50.0
 Distance (in pixels) at which absorption starts.
 
static constexpr double kAttractionForce = 0.5
 Deprecated attraction force constant (logic now uses kSmoothForce).
 
static const QString kServerName = "pk4-projekt-blob-animation"
 Name used for the local server discovery.
 
static constexpr int kUpdateIntervalMs = 50
 Interval (in ms) for sending position updates.
 

Detailed Description

Manages communication and interaction between multiple instances of the application.

This class handles the detection of existing instances, establishing a local server/client connection and exchanging position data between instances. It also implements an attraction and absorption mechanism where client instances are drawn towards the creator instance and eventually absorbed (closed).

Member Enumeration Documentation

◆ MessageType

enum AppInstanceManager::MessageType : quint8
private

Defines the types of messages exchanged between instances.

Enumerator
kPositionUpdate 

Message containing position and size data.

kIdentify 

Message sent by a client to identify itself to the server.

kIdentifyResponse 

Message sent by the server in response to an Identify message.

Constructor & Destructor Documentation

◆ AppInstanceManager()

AppInstanceManager::AppInstanceManager ( QMainWindow * window,
BlobAnimation * blob,
QObject * parent = nullptr )
explicit

Constructs an AppInstanceManager.

Parameters
windowPointer to the main application window.
blobPointer to the BlobAnimation object associated with this instance.
parentOptional parent QObject.
Here is the call graph for this function:

◆ ~AppInstanceManager()

AppInstanceManager::~AppInstanceManager ( )
override

Destructor. Cleans up the resources.

Member Function Documentation

◆ ApplyAttractionForce()

void AppInstanceManager::ApplyAttractionForce ( const QPointF & target_position)
private

Applies a force to the client window, pulling it towards the target position (creator window).

Parameters
target_positionThe global center position of the creator instance's window.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ clientDisconnected

void AppInstanceManager::clientDisconnected ( )
privateslot

Slot called when a connected socket disconnects. Handles cleanup and potential role change (the client becoming creator).

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

◆ ConnectToServer()

void AppInstanceManager::ConnectToServer ( )
private

Establishes a connection to the server instance for a client instance.

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

◆ CreatePositionMessage()

QByteArray AppInstanceManager::CreatePositionMessage ( ) const
nodiscardprivate

Creates a QByteArray message containing the current instance's position data.

Returns
A QByteArray ready to be sent over the socket.
Here is the caller graph for this function:

◆ FinalizeAbsorption()

void AppInstanceManager::FinalizeAbsorption ( )
private

Finalizes the absorption process by quitting the client application.

Here is the caller graph for this function:

◆ GetInstanceId()

QString AppInstanceManager::GetInstanceId ( ) const
inlinenodiscard

Gets the unique identifier for this instance.

Returns
The instance ID as a QString.

◆ InitAttractionThread()

void AppInstanceManager::InitAttractionThread ( )
private

Initializes and starts the background thread responsible for attraction logic.

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

◆ instanceConnected

void AppInstanceManager::instanceConnected ( QString instance_id)
signal

Emitted when a new client instance connects (on the server) or when connection to the server is established (on the client).

Parameters
instance_idThe ID of the connected instance.
Here is the caller graph for this function:

◆ instanceDisconnected

void AppInstanceManager::instanceDisconnected ( QString instance_id)
signal

Emitted when a client instance disconnects (on the server) or when the connection to the server is lost (on the client).

Parameters
instance_idThe ID of the disconnected instance.
Here is the caller graph for this function:

◆ IsAnotherInstanceRunning()

bool AppInstanceManager::IsAnotherInstanceRunning ( )
static

Statically checks if another instance of the application is already running.

Returns
True if another instance is detected, false otherwise.
Here is the caller graph for this function:

◆ IsCreator()

bool AppInstanceManager::IsCreator ( ) const
inlinenodiscard

Checks if this instance is the creator (server - was opened first).

Returns
True if this instance is the creator, false otherwise.

◆ onNewConnection

void AppInstanceManager::onNewConnection ( )
privateslot

Slot called when a new client attempts to connect to the server.

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

◆ otherInstancePositionHasChanged

void AppInstanceManager::otherInstancePositionHasChanged ( QString instance_id,
const QPointF & blob_center,
const QPoint & window_position )
signal

Emitted when position data from another instance is received.

Parameters
instance_idThe ID of the instance whose position changed.
blob_centerThe new center position of the blob in the other instance's window.
window_positionThe new global position of the other instance's window.
Here is the caller graph for this function:

◆ ProcessMessage()

bool AppInstanceManager::ProcessMessage ( const QByteArray & message,
QLocalSocket * sender = nullptr )
private

Processes an incoming message received from a socket.

Parameters
messageThe raw byte array containing the message data.
senderPointer to the socket that sent the message (used by the server to identify clients). Null for client processing.
Returns
True if the message was processed successfully, false otherwise.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readData

void AppInstanceManager::readData ( )
privateslot

Slot called when data can be read from a connected socket.

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

◆ sendPositionUpdate

void AppInstanceManager::sendPositionUpdate ( )
privateslot

Slot called periodically by position_timer_ to send the current instance's position data.

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

◆ SendToAllClients()

void AppInstanceManager::SendToAllClients ( const QByteArray & message)
private

Sends a message to all connected clients (only applicable for the creator instance).

Parameters
messageThe message to send.
Here is the caller graph for this function:

◆ SetupServer()

void AppInstanceManager::SetupServer ( )
private

Sets up the QLocalServer for the creator instance.

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

◆ Start()

bool AppInstanceManager::Start ( )

Starts the instance manager.

Determines if another instance is running. If so, connect as a client. Otherwise, starts a local server and becomes the creator instance. Initializes timers and the attraction thread.

Returns
True if startup was successful, false otherwise (though currently always returns true).
Here is the call graph for this function:

◆ StartAbsorptionAnimation()

void AppInstanceManager::StartAbsorptionAnimation ( )
private

Starts the animation sequence for absorbing a client instance. This involves moving the client window to the creator's position and fading it out.

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

Member Data Documentation

◆ absorption_check_timer_

QTimer AppInstanceManager::absorption_check_timer_
private

Timer potentially used for absorption checks (currently seems unused).

◆ attraction_thread_

std::unique_ptr<std::thread> AppInstanceManager::attraction_thread_
private

Background thread for attraction logic.

◆ blob_

BlobAnimation* AppInstanceManager::blob_
private

Pointer to the BlobAnimation object.

◆ client_ids_

QHash<QLocalSocket *, QString> AppInstanceManager::client_ids_
private

Maps client sockets to their instance IDs (used only by the creator).

◆ connected_instances_

QVector<InstanceInfo> AppInstanceManager::connected_instances_
private

List of currently known connected instances. Protected by instances_mutex_.

◆ instance_id_

QString AppInstanceManager::instance_id_
private

Unique identifier for this application instance.

◆ instances_mutex_

QMutex AppInstanceManager::instances_mutex_
private

Mutex to protect access to connected_instances_.

◆ is_being_absorbed_

std::atomic<bool> AppInstanceManager::is_being_absorbed_ = false
private

Atomic flag indicating if this client instance is currently being absorbed.

◆ is_creator_

std::atomic<bool> AppInstanceManager::is_creator_ {false}
private

Atomic flag indicating if this instance is the creator.

◆ is_thread_running

std::atomic<bool> AppInstanceManager::is_thread_running {false}
private

Atomic flag to control the attraction thread's loop.

◆ kAbsorptionDistance

double AppInstanceManager::kAbsorptionDistance = 50.0
staticconstexprprivate

Distance (in pixels) at which absorption starts.

◆ kAttractionForce

double AppInstanceManager::kAttractionForce = 0.5
staticconstexprprivate

Deprecated attraction force constant (logic now uses kSmoothForce).

◆ kServerName

const QString AppInstanceManager::kServerName = "pk4-projekt-blob-animation"
staticprivate

Name used for the local server discovery.

◆ kUpdateIntervalMs

int AppInstanceManager::kUpdateIntervalMs = 50
staticconstexprprivate

Interval (in ms) for sending position updates.

◆ main_window_

QMainWindow* AppInstanceManager::main_window_
private

Pointer to the main application window.

◆ position_timer_

QTimer AppInstanceManager::position_timer_
private

Timer to periodically send position updates.

◆ server_

QLocalServer* AppInstanceManager::server_ = nullptr
private

Local server instance (used only by the creator).

◆ socket_

QLocalSocket* AppInstanceManager::socket_ = nullptr
private

Local socket instance (used only by clients).


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