Manages communication and interaction between multiple instances of the application.
More...
#include <app_instance_manager.h>
|
| 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.
|
| |
|
| 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.
|
| |
|
| 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.
|
| |
|
| 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.
|
| |
| BlobAnimation * | blob_ |
| | Pointer to the BlobAnimation object.
|
| |
| QHash< QLocalSocket *, QString > | client_ids_ |
| | Maps client sockets to their instance IDs (used only by the creator).
|
| |
| QVector< InstanceInfo > | connected_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 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.
|
| |
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).
◆ MessageType
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.
|
◆ AppInstanceManager()
| AppInstanceManager::AppInstanceManager |
( |
QMainWindow * | window, |
|
|
BlobAnimation * | blob, |
|
|
QObject * | parent = nullptr ) |
|
explicit |
Constructs an AppInstanceManager.
- Parameters
-
| window | Pointer to the main application window. |
| blob | Pointer to the BlobAnimation object associated with this instance. |
| parent | Optional parent QObject. |
◆ ~AppInstanceManager()
| AppInstanceManager::~AppInstanceManager |
( |
| ) |
|
|
override |
Destructor. Cleans up the resources.
◆ 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_position | The global center position of the creator instance's window. |
◆ clientDisconnected
| void AppInstanceManager::clientDisconnected |
( |
| ) |
|
|
privateslot |
Slot called when a connected socket disconnects. Handles cleanup and potential role change (the client becoming creator).
◆ ConnectToServer()
| void AppInstanceManager::ConnectToServer |
( |
| ) |
|
|
private |
Establishes a connection to the server instance for a client instance.
◆ 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.
◆ FinalizeAbsorption()
| void AppInstanceManager::FinalizeAbsorption |
( |
| ) |
|
|
private |
Finalizes the absorption process by quitting the client application.
◆ 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.
◆ 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_id | The ID of the connected instance. |
◆ 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_id | The ID of the disconnected instance. |
◆ 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.
◆ 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.
◆ 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_id | The ID of the instance whose position changed. |
| blob_center | The new center position of the blob in the other instance's window. |
| window_position | The new global position of the other instance's window. |
◆ ProcessMessage()
| bool AppInstanceManager::ProcessMessage |
( |
const QByteArray & | message, |
|
|
QLocalSocket * | sender = nullptr ) |
|
private |
Processes an incoming message received from a socket.
- Parameters
-
| message | The raw byte array containing the message data. |
| sender | Pointer 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.
◆ readData
| void AppInstanceManager::readData |
( |
| ) |
|
|
privateslot |
Slot called when data can be read from a connected socket.
◆ sendPositionUpdate
| void AppInstanceManager::sendPositionUpdate |
( |
| ) |
|
|
privateslot |
Slot called periodically by position_timer_ to send the current instance's position data.
◆ SendToAllClients()
| void AppInstanceManager::SendToAllClients |
( |
const QByteArray & | message | ) |
|
|
private |
Sends a message to all connected clients (only applicable for the creator instance).
- Parameters
-
| message | The message to send. |
◆ SetupServer()
| void AppInstanceManager::SetupServer |
( |
| ) |
|
|
private |
Sets up the QLocalServer for the creator instance.
◆ 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).
◆ 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.
◆ 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_
◆ 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: