2024-05-21 01:14:38 +08:00
|
|
|
// Copyright (c) Itay Grudev 2023
|
2016-07-05 01:19:48 +08:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
2023-03-24 18:09:12 +08:00
|
|
|
// Permission is not granted to use this software or any of the associated files
|
|
|
|
// as sample data for the purposes of building machine learning models.
|
|
|
|
//
|
2016-07-05 01:19:48 +08:00
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
2015-02-27 03:00:11 +08:00
|
|
|
#ifndef SINGLE_APPLICATION_H
|
|
|
|
#define SINGLE_APPLICATION_H
|
2016-05-05 04:05:59 +08:00
|
|
|
|
2016-03-05 02:15:13 +08:00
|
|
|
#include <QtCore/QtGlobal>
|
2016-08-10 09:42:46 +08:00
|
|
|
#include <QtNetwork/QLocalSocket>
|
2012-12-23 06:12:38 +08:00
|
|
|
|
2016-04-06 01:31:05 +08:00
|
|
|
#ifndef QAPPLICATION_CLASS
|
2016-05-10 00:05:38 +08:00
|
|
|
#define QAPPLICATION_CLASS QCoreApplication
|
2016-04-06 01:31:05 +08:00
|
|
|
#endif
|
|
|
|
|
2016-03-05 02:15:13 +08:00
|
|
|
#include QT_STRINGIFY(QAPPLICATION_CLASS)
|
2015-06-06 05:26:41 +08:00
|
|
|
|
|
|
|
class SingleApplicationPrivate;
|
2012-12-23 06:12:38 +08:00
|
|
|
|
|
|
|
/**
|
2022-04-06 05:04:31 +08:00
|
|
|
* @brief Handles multiple instances of the same
|
2016-08-10 09:42:46 +08:00
|
|
|
* Application
|
|
|
|
* @see QCoreApplication
|
2012-12-23 06:12:38 +08:00
|
|
|
*/
|
2015-06-09 22:29:20 +08:00
|
|
|
class SingleApplication : public QAPPLICATION_CLASS
|
2012-12-23 06:12:38 +08:00
|
|
|
{
|
2016-03-05 02:15:13 +08:00
|
|
|
Q_OBJECT
|
|
|
|
|
2020-05-17 22:50:27 +08:00
|
|
|
using app_t = QAPPLICATION_CLASS;
|
2016-03-05 02:15:13 +08:00
|
|
|
|
2012-12-23 06:12:38 +08:00
|
|
|
public:
|
2024-05-21 01:14:38 +08:00
|
|
|
// If you change this enum, make sure to update read validation code in message_decoder.cpp
|
|
|
|
enum MessageType : quint8 {
|
|
|
|
Acknowledge,
|
|
|
|
NewInstance,
|
|
|
|
InstanceMessage,
|
|
|
|
};
|
|
|
|
Q_ENUM( MessageType )
|
|
|
|
|
|
|
|
struct Message {
|
|
|
|
MessageType type;
|
|
|
|
quint16 instanceId;
|
|
|
|
QByteArray content;
|
|
|
|
};
|
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
/**
|
2022-04-06 05:04:31 +08:00
|
|
|
* @brief Mode of operation of `SingleApplication`.
|
2016-08-10 09:42:46 +08:00
|
|
|
* Whether the block should be user-wide or system-wide and whether the
|
|
|
|
* primary instance should be notified when a secondary instance had been
|
|
|
|
* started.
|
|
|
|
* @note Operating system can restrict the shared memory blocks to the same
|
|
|
|
* user, in which case the User/System modes will have no effect and the
|
|
|
|
* block will be user wide.
|
|
|
|
*/
|
|
|
|
enum Mode {
|
2022-04-06 05:04:31 +08:00
|
|
|
/** The `SingleApplication` block should apply user wide
|
|
|
|
* (this adds user specific data to the key used for the shared memory and server name)
|
|
|
|
* */
|
|
|
|
User = 1 << 0,
|
|
|
|
/**
|
|
|
|
* The `SingleApplication` block applies system-wide.
|
|
|
|
*/
|
|
|
|
System = 1 << 1,
|
|
|
|
/**
|
|
|
|
* Whether to trigger `instanceStarted()` even whenever secondary instances are started
|
|
|
|
*/
|
|
|
|
SecondaryNotification = 1 << 2,
|
|
|
|
/**
|
|
|
|
* Excludes the application version from the server name (and memory block) hash
|
|
|
|
*/
|
|
|
|
ExcludeAppVersion = 1 << 3,
|
|
|
|
/**
|
|
|
|
* Excludes the application path from the server name (and memory block) hash
|
|
|
|
*/
|
|
|
|
ExcludeAppPath = 1 << 4
|
2016-08-10 09:42:46 +08:00
|
|
|
};
|
|
|
|
Q_DECLARE_FLAGS(Options, Mode)
|
|
|
|
|
|
|
|
/**
|
2022-04-06 05:04:31 +08:00
|
|
|
* @brief Intitializes a `SingleApplication` instance with argc command line
|
2016-08-10 09:42:46 +08:00
|
|
|
* arguments in argv
|
2022-04-06 05:04:31 +08:00
|
|
|
* @arg argc - Number of arguments in argv
|
|
|
|
* @arg argv - Supplied command line arguments
|
|
|
|
* @arg allowSecondary - Whether to start the instance as secondary
|
2016-08-10 09:42:46 +08:00
|
|
|
* if there is already a primary instance.
|
2022-04-06 05:04:31 +08:00
|
|
|
* @arg mode - Whether for the `SingleApplication` block to be applied
|
2016-08-10 09:42:46 +08:00
|
|
|
* User wide or System wide.
|
2022-04-06 05:04:31 +08:00
|
|
|
* @arg timeout - Timeout to wait in milliseconds.
|
2016-08-10 09:42:46 +08:00
|
|
|
* @note argc and argv may be changed as Qt removes arguments that it
|
|
|
|
* recognizes
|
2022-04-06 05:04:31 +08:00
|
|
|
* @note `Mode::SecondaryNotification` only works if set on both the primary
|
2016-08-10 09:42:46 +08:00
|
|
|
* instance and the secondary instance.
|
|
|
|
* @note The timeout is just a hint for the maximum time of blocking
|
2022-04-06 05:04:31 +08:00
|
|
|
* operations. It does not guarantee that the `SingleApplication`
|
2016-08-10 09:42:46 +08:00
|
|
|
* initialisation will be completed in given time, though is a good hint.
|
|
|
|
* Usually 4*timeout would be the worst case (fail) scenario.
|
2022-04-06 05:04:31 +08:00
|
|
|
* @see See the corresponding `QAPPLICATION_CLASS` constructor for reference
|
2016-08-10 09:42:46 +08:00
|
|
|
*/
|
2020-12-22 01:07:36 +08:00
|
|
|
explicit SingleApplication( int &argc, char *argv[], bool allowSecondary = false, Options options = Mode::User, int timeout = 1000, const QString &userData = {} );
|
2020-05-17 22:50:27 +08:00
|
|
|
~SingleApplication() override;
|
2012-12-23 06:12:38 +08:00
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
/**
|
2022-04-06 05:04:31 +08:00
|
|
|
* @brief Checks if the instance is primary instance
|
|
|
|
* @returns `true` if the instance is primary
|
2016-08-10 09:42:46 +08:00
|
|
|
*/
|
2021-01-25 06:16:37 +08:00
|
|
|
bool isPrimary() const;
|
2016-08-10 09:42:46 +08:00
|
|
|
|
|
|
|
/**
|
2022-04-06 05:04:31 +08:00
|
|
|
* @brief Checks if the instance is a secondary instance
|
|
|
|
* @returns `true` if the instance is secondary
|
2016-08-10 09:42:46 +08:00
|
|
|
*/
|
2021-01-25 06:16:37 +08:00
|
|
|
bool isSecondary() const;
|
2016-05-05 02:59:07 +08:00
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
/**
|
|
|
|
* @brief Returns a unique identifier for the current instance
|
2022-04-06 05:04:31 +08:00
|
|
|
* @returns instance id
|
2016-08-10 09:42:46 +08:00
|
|
|
*/
|
2021-01-25 06:16:37 +08:00
|
|
|
quint32 instanceId() const;
|
2012-12-23 06:12:38 +08:00
|
|
|
|
2017-10-02 19:17:41 +08:00
|
|
|
/**
|
|
|
|
* @brief Returns the process ID (PID) of the primary instance
|
2022-04-06 05:04:31 +08:00
|
|
|
* @returns pid
|
2017-10-02 19:17:41 +08:00
|
|
|
*/
|
2021-01-25 06:16:37 +08:00
|
|
|
qint64 primaryPid() const;
|
2017-10-02 19:17:41 +08:00
|
|
|
|
2020-03-03 09:22:54 +08:00
|
|
|
/**
|
|
|
|
* @brief Returns the username of the user running the primary instance
|
2022-04-06 05:04:31 +08:00
|
|
|
* @returns user name
|
2020-03-03 09:22:54 +08:00
|
|
|
*/
|
2021-01-25 06:16:37 +08:00
|
|
|
QString primaryUser() const;
|
2020-03-03 09:22:54 +08:00
|
|
|
|
2020-03-27 15:00:14 +08:00
|
|
|
/**
|
|
|
|
* @brief Returns the username of the current user
|
2022-04-06 05:04:31 +08:00
|
|
|
* @returns user name
|
2020-03-27 15:00:14 +08:00
|
|
|
*/
|
2021-01-25 06:16:37 +08:00
|
|
|
QString currentUser() const;
|
2020-03-27 15:00:14 +08:00
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
/**
|
2022-04-06 05:04:31 +08:00
|
|
|
* @brief Sends a message to the primary instance
|
|
|
|
* @param message data to send
|
|
|
|
* @param timeout timeout for connecting
|
|
|
|
* @param sendMode - Mode of operation
|
|
|
|
* @returns `true` on success
|
|
|
|
* @note sendMessage() will return false if invoked from the primary instance
|
2016-08-10 09:42:46 +08:00
|
|
|
*/
|
2024-05-21 01:14:38 +08:00
|
|
|
bool sendMessage( const QByteArray &message, int timeout = 100 );
|
2016-08-10 09:42:46 +08:00
|
|
|
|
2020-12-11 08:18:00 +08:00
|
|
|
/**
|
|
|
|
* @brief Get the set user data.
|
2022-04-06 05:04:31 +08:00
|
|
|
* @returns user data
|
2020-12-11 08:18:00 +08:00
|
|
|
*/
|
2021-01-25 06:16:37 +08:00
|
|
|
QStringList userData() const;
|
2020-12-11 08:18:00 +08:00
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
Q_SIGNALS:
|
2022-04-06 05:04:31 +08:00
|
|
|
/**
|
|
|
|
* @brief Triggered whenever a new instance had been started,
|
|
|
|
* except for secondary instances if the `Mode::SecondaryNotification` flag is not specified
|
|
|
|
*/
|
2016-08-10 09:42:46 +08:00
|
|
|
void instanceStarted();
|
2022-04-06 05:04:31 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Triggered whenever there is a message received from a secondary instance
|
|
|
|
*/
|
2016-08-10 09:42:46 +08:00
|
|
|
void receivedMessage( quint32 instanceId, QByteArray message );
|
2015-02-27 03:00:11 +08:00
|
|
|
|
2012-12-23 06:12:38 +08:00
|
|
|
private:
|
2016-03-05 02:15:13 +08:00
|
|
|
SingleApplicationPrivate *d_ptr;
|
2016-08-10 09:42:46 +08:00
|
|
|
Q_DECLARE_PRIVATE(SingleApplication)
|
2012-12-23 06:12:38 +08:00
|
|
|
};
|
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
Q_DECLARE_OPERATORS_FOR_FLAGS(SingleApplication::Options)
|
|
|
|
|
2015-02-27 03:00:11 +08:00
|
|
|
#endif // SINGLE_APPLICATION_H
|