diff --git a/KelvinDashboard.pro b/KelvinDashboard.pro old mode 100644 new mode 100755 index 6d654c848068c8aa655dd8e3e379d5a61938e498..395241d24f070f32aa5d33571c06487d4eab7d1b --- a/KelvinDashboard.pro +++ b/KelvinDashboard.pro @@ -10,3 +10,8 @@ KelvinDashboardGUI.depends = KelvinDashboardService BRAND = KelvinDashboard } +unix: !mac : !android { + share_target.files = debian/share/* + share_target.path = /opt/dap/$$lower($$BRAND)/share/ + INSTALLS += share_target +} diff --git a/KelvinDashboardGUI/DapClient.cpp b/KelvinDashboardGUI/DapClient.cpp deleted file mode 100644 index 7c3ba56efbfa66d3df08b04bb85bba5e1c5bd894..0000000000000000000000000000000000000000 --- a/KelvinDashboardGUI/DapClient.cpp +++ /dev/null @@ -1,156 +0,0 @@ -#include "DapClient.h" - -/// Standard Ñonstructor. -DapClient::DapClient(QObject *parent) : QObject(parent) -{ - connect(&m_localClient, &DapLocalClient::commandRecieved, this, &DapClient::identificationCommand); - connect(&m_localClient, static_cast<void(DapLocalClient::*)(DapLocalClient::LocalSocketError)>(&DapLocalClient::error), this, &DapClient::errorHandler); - connect(&m_localClient, &DapLocalClient::connected, this, [=] - { - QTimer::singleShot(1000, [&] - { - emit connectedToService(); - }); - }); -} - -/// Get an instance of a class. -/// @return Instance of a class. -DapClient &DapClient::getInstance() -{ - static DapClient instance; - return instance; -} - -/// Connect to the service. -/// @brief The name of the service connection is set from the NameSrvice property. -void DapClient::connectToService() -{ - qDebug() << "Connect to service ()" << getNameSrvice(); - m_localClient.connectToServer(getNameSrvice()); -} - -/// Connect to the service. -/// @param nameService Service connection name. -void DapClient::connectToService(const QString &nameService) -{ - setNameSrvice(nameService); - qDebug() << "Connect to servoce (nameService)"; - m_localClient.connectToServer(nameService); -} - -/// Authorize user. -/// @param password User password. -void DapClient::authorization(const QString &password) -{ - qDebug() << "Authorization " << password << endl; - DapCommand command(TypeDapCommand::Authorization, 1, { password }); - m_localClient.sendCommand(command); -} - -/// Handle connection error. -/// @param socketError Error local connection of the client with the service. -void DapClient::errorHandler(const DapLocalClient::LocalSocketError &socketError) -{ - switch (socketError) { - case DapLocalClient::PeerClosedError: - case DapLocalClient::ConnectionRefusedError: - case DapLocalClient::ServerNotFoundError: - QTimer::singleShot(1000, [&] - { - connectToService(); - emit errorConnect(); - } ); - break; - default: - break; - } -} - -/// Get the name of the service connection. -/// @return The name of the service connection. -QString DapClient::getNameSrvice() const -{ - return m_nameService; -} - -/// Set the name of the service connection. -/// @param nameService Service connection name. -void DapClient::setNameSrvice(const QString &nameService) -{ - m_nameService = nameService; - m_localClient.setNameConnect(nameService); -} - -/// Get company brand. -/// @return Brand Ñompany. -QString DapClient::getBrand() const -{ - return m_brand; -} - -/// Get app version. -/// @return Application version. -QString DapClient::getVersion() const -{ - return m_version; -} - -/// Get user authorization flag. -/// @return Returns true if the user is authorized, otherwise - false. -bool DapClient::getIsAuthorization() const -{ - return m_isAuthorization; -} - -/// Set user authorization flag. -/// @param isAuthorization The value of the user authorization flag. -void DapClient::setIsAuthorization(bool isAuthorization) -{ - m_isAuthorization = isAuthorization; - emit isAuthorizationChanged(m_isAuthorization); -} - -/// /// Identification of the command received. -/// @param command Command received. -/// @return Returns true if the command is identified, otherwise - false. -bool DapClient::identificationCommand(const DapCommand &command) -{ - qDebug() << "Identification command: " << command.getTypeCommand(); - qDebug() << "Identification command: " << command.getArguments().count(); - switch (command.getTypeCommand()) - { - case TypeDapCommand::Authorization: - if(command.getArgument(0).toBool()) - setIsAuthorization(true); - else - setIsAuthorization(false); - return true; - case TypeDapCommand::ActivateWindowClient: - emit activateWindow(); - return true; - case TypeDapCommand::CheckExistenceClient: - if(command.getArgument(0).toBool()) - emit isExistenceClient(true); - else - emit isExistenceClient(false); - return true; - case TypeDapCommand::CloseClient: - qApp->quit(); - return true; - default: - return false; - } -} - -/// Method that implements the singleton pattern for the qml layer. -/// @param engine QML application. -/// @param scriptEngine The QJSEngine class provides an environment for evaluating JavaScript code. -QObject *DapClient::singletonProvider(QQmlEngine *engine, QJSEngine *scriptEngine) -{ - Q_UNUSED(engine) - Q_UNUSED(scriptEngine) - - return &getInstance(); -} - diff --git a/KelvinDashboardGUI/DapClient.h b/KelvinDashboardGUI/DapClient.h deleted file mode 100644 index d0801e72fc63c511a997c607d82f18541c5b4a4e..0000000000000000000000000000000000000000 --- a/KelvinDashboardGUI/DapClient.h +++ /dev/null @@ -1,131 +0,0 @@ -/**************************************************************************** -** -** This file is part of the KelvinDashboardGUI application. -** -** The class implements the GUI interface of the application. Designed to -** broadcast user actions to the service. Manages the state of graphic -** elements of the GUI framework, connection with the service, identification -** of commands received from the service, etc. -** -** Class implements a singleton pattern. -** -****************************************************************************/ - -#ifndef DAPCLIENT_H -#define DAPCLIENT_H - -#include <QObject> -#include <QLocalSocket> -#include <QQmlEngine> -#include <QJSEngine> -#include <QApplication> -#include <QTimer> - -#include "DapCommand.h" -#include "DapLocalClient.h" - -class DapClient : public QObject -{ - Q_OBJECT - - /// Local client. - DapLocalClient m_localClient; - /// Brand Ñompany. - QString m_brand {DAP_BRAND}; - /// Application version. - QString m_version {DAP_VERSION}; - /// Service connection name. - QString m_nameService; - /// User authorization flag. - bool m_isAuthorization {false}; - - /// Standard Ñonstructor. - explicit DapClient(QObject *parent = nullptr); -public: - DapClient(const DapClient&) = delete; - DapClient& operator= (const DapClient &) = delete; - - /// Get an instance of a class. - /// @return Instance of a class. - Q_INVOKABLE static DapClient &getInstance(); - - ///******************************************** - /// Property - /// ******************************************* - - /// Service connection name. - Q_PROPERTY(QString NameSrvice MEMBER m_nameService READ getNameSrvice WRITE setNameSrvice NOTIFY nameSrviceChanged) - /// Brand Ñompany. - Q_PROPERTY(QString Brand MEMBER m_brand READ getBrand NOTIFY brandChanged) - /// Application version. - Q_PROPERTY(QString Version MEMBER m_version READ getVersion NOTIFY versionChanged) - /// User authorization flag. - Q_PROPERTY(bool IsAuthorization MEMBER m_isAuthorization READ getIsAuthorization WRITE setIsAuthorization NOTIFY isAuthorizationChanged) - - ///******************************************** - /// Interface - /// ******************************************* - - /// Connect to the service. - /// @brief The name of the service connection is set from the NameSrvice property. - Q_INVOKABLE void connectToService(); - /// Connect to the service. - /// @param nameService Service connection name. - Q_INVOKABLE void connectToService(const QString &nameService); - /// Authorize user. - /// @param password User password. - Q_INVOKABLE void authorization(const QString &password); - /// Handle connection error. - /// @param socketError Error local connection of the client with the service. - void errorHandler(const QLocalSocket::LocalSocketError& socketError); - /// Get the name of the service connection. - /// @return The name of the service connection. - QString getNameSrvice() const; - /// Set the name of the service connection. - /// @param nameService Service connection name. - void setNameSrvice(const QString &nameService); - /// Get company brand. - /// @return Brand Ñompany. - QString getBrand() const; - /// Get app version. - /// @return Application version. - QString getVersion() const; - /// Get user authorization flag. - /// @return Returns true if the user is authorized, otherwise - false. - bool getIsAuthorization() const; - /// Set user authorization flag. - /// @param isAuthorization The value of the user authorization flag. - void setIsAuthorization(bool isAuthorization); - -signals: - /// The signal is emitted when a successful connection to the service is established. - void connectedToService(); - /// The signal is emitted when the connection to the service is broken. - void disconnectedFromService(); - /// The signal is emitted when the name of the client's connection with the service is changed. - void nameSrviceChanged(const QString &nameService); - /// The signal is emitted when the Brand company property changes. - void brandChanged(const QString &brand); - /// The signal is emitted when the Application version property changes. - void versionChanged(const QString &version); - /// The signal is emitted when the User authorization flag property changes. - void isAuthorizationChanged(bool isAuthorization); - /// The signal is emitted when the main application window is activated. - void activateWindow(); - /// The signal is emitted when an error occurs in the connection between the client and the service. - void errorConnect(); - /// The signal is emitted when checking the existence of an already running copy of the application. - void isExistenceClient(bool isExistenceClient); - -public slots: - /// /// Identification of the command received. - /// @param command Command received. - /// @return Returns true if the command is identified, otherwise - false. - bool identificationCommand(const DapCommand &command); - /// Method that implements the singleton pattern for the qml layer. - /// @param engine QML application. - /// @param scriptEngine The QJSEngine class provides an environment for evaluating JavaScript code. - static QObject *singletonProvider(QQmlEngine *engine, QJSEngine *scriptEngine); -}; - -#endif // DAPCLIENT_H diff --git a/KelvinDashboardGUI/DapCommandController.cpp b/KelvinDashboardGUI/DapCommandController.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3e791a8bb678b402ca47ff1954a2043c9e5fc66 --- /dev/null +++ b/KelvinDashboardGUI/DapCommandController.cpp @@ -0,0 +1,53 @@ +#include "DapCommandController.h" + +/// Overloaded constructor. +/// @param apIODevice Data transfer device. +/// @param apParent Parent. +DapCommandController::DapCommandController(QIODevice *apIODevice, QObject *apParent) + : DapRpcService(apParent) +{ + // Socket initialization + m_DAPRpcSocket = new DapRpcSocket(apIODevice, this); + // Signal-slot connection initiating the execution of the method called by the service + connect(m_DAPRpcSocket, SIGNAL(messageReceived(DapRpcMessage)), SLOT(messageProcessing(DapRpcMessage))); + + addService(this); +} + +/// Process incoming message. +/// @param asMessage Incoming message. +void DapCommandController::messageProcessing(const DapRpcMessage &asMessage) +{ + DapRpcSocket *socket = static_cast<DapRpcSocket*>(sender()); + if (!socket) { + qDebug() << "Called without service socket"; + return; + } + + processMessage(socket, asMessage); +} + +/// Process the result of the command execution. +void DapCommandController::processCommandResult() +{ + DapRpcServiceReply *reply = static_cast<DapRpcServiceReply *>(sender()); + if (!reply) { + qWarning() << "Invalid response received"; + return; + } + emit sigCommandResult(reply->response().result()); +} + +/// Show or hide GUI client by clicking on the tray icon. +/// @param aIsActivated Accepts true - when requesting to +/// display a client, falso - when requesting to hide a client. +void DapCommandController::activateClient(bool aIsActivated) +{ + emit onClientActivate(aIsActivated); +} + +/// Shut down client. +void DapCommandController::closeClient() +{ + emit onClientClose(); +} diff --git a/KelvinDashboardGUI/DapCommandController.h b/KelvinDashboardGUI/DapCommandController.h new file mode 100644 index 0000000000000000000000000000000000000000..7e946d342cb248270ef016121ab4b47f38927a8f --- /dev/null +++ b/KelvinDashboardGUI/DapCommandController.h @@ -0,0 +1,52 @@ +#ifndef DAPCOMMANDCONTROLLER_H +#define DAPCOMMANDCONTROLLER_H + +#include <QObject> +#include <QIODevice> +#include <QVariantMap> +#include <QDebug> + +#include "DapRpcSocket.h" +#include "DapRpcServiceProvider.h" +#include "DapRpcService.h" + +class DapCommandController : public DapRpcService, public DapRpcServiceProvider +{ + Q_OBJECT + Q_DISABLE_COPY(DapCommandController) + Q_CLASSINFO("serviceName", "RPCClient") + + /// RPC socket. + DapRpcSocket * m_DAPRpcSocket {nullptr}; + +signals: + /// The signal is emitted after receiving a response from the service about the command execution. + void sigCommandResult(QJsonValue ); + /// The signal is emitted when the main application window is activated. + void onClientActivate(bool aIsActivated); + + void onClientClose(); + +public: + /// Overloaded constructor. + /// @param apIODevice Data transfer device. + /// @param apParent Parent. + DapCommandController(QIODevice *apIODevice, QObject *apParent = nullptr); + +private slots: + /// Process incoming message. + /// @param asMessage Incoming message. + void messageProcessing(const DapRpcMessage &asMessage); + /// Process the result of the command execution. + void processCommandResult(); + +public slots: + /// Show or hide GUI client by clicking on the tray icon. + /// @param aIsActivated Accepts true - when requesting to + /// display a client, falso - when requesting to hide a client. + void activateClient(bool aIsActivated); + /// Shut down client. + void closeClient(); +}; + +#endif // COMMANDCONTROLLER_H diff --git a/KelvinDashboardGUI/DapQmlScreenAbout.qml b/KelvinDashboardGUI/DapQmlScreenAbout.qml old mode 100644 new mode 100755 index 0d2c3fd75d224dabdc62f7bb420346508d4e1670..e4b3c93215a4a036a50330f0b1d257f947f080aa --- a/KelvinDashboardGUI/DapQmlScreenAbout.qml +++ b/KelvinDashboardGUI/DapQmlScreenAbout.qml @@ -6,8 +6,8 @@ import KelvinDashboard 1.0 DapUiQmlScreenAbout { id: dapQmlScreenAbout - textTitle.text: DapClient.Brand + textTitle.text: DapServiceController.Brand textAbout.text: "KelvinDashboard" - textVersion.text: "Version " + DapClient.Version + textVersion.text: "Version " + DapServiceController.Version textYear.text: new Date().toLocaleDateString(locale, "dd MMM yyyy") } diff --git a/KelvinDashboardGUI/DapQmlScreenLogin.qml b/KelvinDashboardGUI/DapQmlScreenLogin.qml deleted file mode 100644 index cb6c8469fa01f669515f3c1e09a68aefd88d9c7f..0000000000000000000000000000000000000000 --- a/KelvinDashboardGUI/DapQmlScreenLogin.qml +++ /dev/null @@ -1,36 +0,0 @@ -import QtQuick 2.0 -import KelvinDashboard 1.0 - -DapUiQmlScreenLogin { - id: dapQmlScreenLogin - - function acceptPassword() - { - dapScreenLogin.Password = textFieldPassword.text - DapClient.authorization(textFieldPassword.text) - console.log(textFieldPassword.text) - } - - Connections { - target: dapClient - - onIsAuthorizationChanged: { - console.log("PARAM " + isAuthorization) - textStatus.visible = true - if(isAuthorization) - { - textStatus.text = "Password confirmed" - textStatus.color = "green" - textFieldPassword.color = "green" - } - else - { - textStatus.text = "Password not verified" - textStatus.color = "red" - textFieldPassword.color = "red" - } - } - } - - buttonPassword.onClicked: acceptPassword() -} diff --git a/KelvinDashboardGUI/DapScreenDialogChangeWidget.cpp b/KelvinDashboardGUI/DapScreenDialogChangeWidget.cpp old mode 100644 new mode 100755 index ea0f064d124c6375c6fb2f9f6f317c3f13831b94..6dfe8643a271b35d1a798090e0f977aee6bd6c9d --- a/KelvinDashboardGUI/DapScreenDialogChangeWidget.cpp +++ b/KelvinDashboardGUI/DapScreenDialogChangeWidget.cpp @@ -2,7 +2,7 @@ DapScreenDialogChangeWidget::DapScreenDialogChangeWidget(QObject *parent) : QObject(parent) { - m_proxyModel = new QSortFilterProxyModel; + m_proxyModel = new QSortFilterProxyModel(this); m_proxyModel->setSourceModel(&DapUiQmlWidgetModel::getInstance()); m_proxyModel->setFilterRegExp(QRegExp("false")); m_proxyModel->setFilterRole(VisibleRole); diff --git a/KelvinDashboardGUI/DapScreenLogin.cpp b/KelvinDashboardGUI/DapScreenLogin.cpp deleted file mode 100644 index 9c1fe7ec90d6c51dcafe53ad330ff7418f869d98..0000000000000000000000000000000000000000 --- a/KelvinDashboardGUI/DapScreenLogin.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "DapScreenLogin.h" - -DapScreenLogin::DapScreenLogin(QObject *parent) : QObject(parent) -{ - connect(this, &DapScreenLogin::passwordChanged, this, &DapScreenLogin::autorization); -} - -QString DapScreenLogin::getPassword() const -{ - return m_password; -} - -void DapScreenLogin::setPassword(const QString &password) -{ - qDebug() << "Set password: " << password << endl; - m_password = password; -} - -bool DapScreenLogin::autorization(const QString &password) -{ - -} diff --git a/KelvinDashboardGUI/DapScreenLogin.h b/KelvinDashboardGUI/DapScreenLogin.h deleted file mode 100644 index 8debb16252e5b6792fb4fb17dc54671b659d07b6..0000000000000000000000000000000000000000 --- a/KelvinDashboardGUI/DapScreenLogin.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef DAPSCREENLOGIN_H -#define DAPSCREENLOGIN_H - -#include <QObject> -#include <QDebug> - -class DapScreenLogin : public QObject -{ - Q_OBJECT - - QString m_password; -public: - explicit DapScreenLogin(QObject *parent = nullptr); - - Q_PROPERTY(QString Password MEMBER m_password READ getPassword WRITE setPassword NOTIFY passwordChanged) - - QString getPassword() const; - - void setPassword(const QString &password); - - Q_INVOKABLE bool autorization(const QString& password); -signals: - - void passwordChanged(const QString& password); -public slots: -}; - -#endif // DAPSCREENLOGIN_H diff --git a/KelvinDashboardGUI/DapServiceClient.cpp b/KelvinDashboardGUI/DapServiceClient.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c387bed1205b20650d12c3736706325270cbdee --- /dev/null +++ b/KelvinDashboardGUI/DapServiceClient.cpp @@ -0,0 +1,150 @@ +#include "DapServiceClient.h" + +DapServiceClient::DapServiceClient(QObject *apParent) + : QObject(apParent) +{ + // Initialization of the service connection socket + m_pClientSocket = new DapUiSocket(this); + // Signal-slot connection broadcasting service connection error + connect(m_pClientSocket,static_cast<void(DapUiSocket::*)(DapUiSocketError)> (&DapUiSocket::error), + this, &DapServiceClient::handleSocketError); + // Signal-slot connection broadcasting signal of successful connection to the service + connect(m_pClientSocket,SIGNAL(connected()), this, SLOT(connectedToService())); + // Signal-slot connection transmitting a signal to disconnect from the service + connect(m_pClientSocket,SIGNAL(disconnected()), this, SLOT(disconnectFromService())); + // Signal-slot connection that reconnects to the service + connect(&m_reconnectTimer, SIGNAL(timeout()), this, SLOT(reconnectToService())); +} + +/// Get a socket pointer. +/// @return A pointer to a socket. +DapUiSocket *DapServiceClient::getClientSocket() const +{ + return m_pClientSocket; +} + +/// Establish a connection with the service when the latter is launched. +void DapServiceClient::onServiceStarted() +{ + qInfo() << "Service started."; + connectToService(); +} + +/// Handle event of successful connection to the service. +void DapServiceClient::connectedToService() +{ + qInfo() << "Connected to the service"; + m_launchAttemptCounter = 0; + m_isServiceConnected = true; + stopReconnectingToService(); + emit sigConnected(); +} + +/// Start the process of reconnecting to the service. +void DapServiceClient::startReconnectingToService() +{ + if(!m_reconnectTimer.isActive()) { + qInfo() << "Start trying to reconnect to service"; + m_reconnectTimer.start(RECONNECT_TIMEOUT_MS); + } +} + +/// Stop the process of reconnecting to the service. +void DapServiceClient::stopReconnectingToService() +{ + if(m_reconnectTimer.isActive()) + { + m_reconnectTimer.stop(); + qInfo() << "Reconnect timer stopped"; + } +} + +/// Handle socket error. +/// @param aSocketEror Socket error code. +void DapServiceClient::handleSocketError(DapUiSocketError aSocketEror) +{ + qDebug() << m_pClientSocket->errorString(); + startReconnectingToService(); + emit sigSocketError(aSocketEror); + emit sigSocketErrorString(m_pClientSocket->errorString()); +} + +/// Reconnect service. +void DapServiceClient::reconnectToService() +{ + ++m_launchAttemptCounter; + DapServiceError resultInit = DapServiceError::NO_ERRORS; + if(m_launchAttemptCounter == NUMBER_LAUNCH_ATTEMPTS) + { + qCritical() << "Server not running after `serviceStart` operation"; + QMessageBox::critical(Q_NULLPTR, DAP_BRAND, "Unable to start service", QMessageBox::Ok); + exit(-1); + } + else + { + resultInit = init(); + } + if(resultInit == DapServiceError::NO_ERRORS) + { + connectToService(); + } +} + +/// Initiate the service monitor. +DapServiceError DapServiceClient::init() +{ + DapServiceError result = DapServiceClientNative::init(); + + handleServiceError(result); + + return result; +} + +/// Handle service error. +/// @param aServiceEror Service error code. +void DapServiceClient::handleServiceError(DapServiceError aServiceEror) +{ + switch (aServiceEror) + { + case DapServiceError::NO_ERRORS: + break; + case DapServiceError::USER_COMMAND_ABORT: + QMessageBox::critical(Q_NULLPTR, DAP_BRAND, "User abort service comand", QMessageBox::Ok); + exit(-1); + case DapServiceError::UNABLE_START_SERVICE: + QMessageBox::critical(Q_NULLPTR, DAP_BRAND, "Start the service with administrator rights", QMessageBox::Ok); + qCritical() << "Start the service with administrator rights"; + break; + case DapServiceError::UNABLE_STOP_SERVICE: + qCritical() << "Can't stop service"; + break; + case DapServiceError::UNKNOWN_ERROR: + qCritical() << "Got unknown error"; + break; + case DapServiceError::SERVICE_NOT_FOUND: + qCritical() << "Service not found"; + break; + } + if(aServiceEror != DapServiceError::NO_ERRORS) + { + emit sigServiceError(aServiceEror); + } +} + +/// Establish a connection with the service. +void DapServiceClient::connectToService() +{ + if(m_pClientSocket->state() == QAbstractSocket::ConnectedState) + return; + + qInfo() << "with parametr: " << DAP_BRAND; + m_pClientSocket->connectToServer(DAP_BRAND); +} + +/// Handle service outage. +void DapServiceClient::disconnectFromService() +{ + m_isServiceConnected = true; + startReconnectingToService(); + emit sigDisconnected(); +} diff --git a/KelvinDashboardGUI/DapServiceClient.h b/KelvinDashboardGUI/DapServiceClient.h new file mode 100644 index 0000000000000000000000000000000000000000..8f9763363e48c1e5fedaf1b97f51a810cdd174ee --- /dev/null +++ b/KelvinDashboardGUI/DapServiceClient.h @@ -0,0 +1,85 @@ +#ifndef DAPSERVICECLIENT_H +#define DAPSERVICECLIENT_H + +#include <QObject> +#include <QTimer> +#include <QMessageBox> +#include <QLocalSocket> +#include <QLocalServer> + +#if defined(Q_OS_LINUX) +#include "DapServiceClientNativeLinux.h" +typedef class DapServiceClientNativeLinux DapServiceClientNative; +#endif + +typedef QLocalSocket DapUiSocket; +typedef QLocalServer DapUiServer; +typedef QLocalSocket::LocalSocketError DapUiSocketError; + +class DapServiceClient : public QObject, public DapServiceClientNative +{ + Q_OBJECT + Q_DISABLE_COPY(DapServiceClient) + + /// Reconnect interval. + const int RECONNECT_TIMEOUT_MS {5000}; + /// The number of attempts to restart the service. + const size_t NUMBER_LAUNCH_ATTEMPTS {3}; + /// The current number of attempts to restart the service. + size_t m_launchAttemptCounter {0}; + /// Reconnect timer. + QTimer m_reconnectTimer; + /// Service connection socket. + DapUiSocket *m_pClientSocket {nullptr}; + + bool m_isServiceConnected {false}; + +public: + explicit DapServiceClient(QObject * apParent = nullptr); + /// Get a socket pointer. + /// @return A pointer to a socket. + DapUiSocket *getClientSocket() const; + +signals: + /// The signal emitted in case of successful connection to the service. + void sigConnected(); + /// The signal emitted in case of successful disconnection from the service. + void sigDisconnected(); + /// The signal emitted in the event of an error when connecting to the service. + /// @param asErrorMessage Socket error message. + void sigSocketErrorString(const QString& asErrorMessage); + /// The signal emitted in the event of an error when connecting to the service. + /// @param aSocketEror Socket error code. + void sigSocketError(DapUiSocketError aSocketEror); + /// The signal is emitted in case of an error when trying to start the service. + /// @param aServiceEror Service error code. + void sigServiceError(DapServiceError aServiceEror); + +protected slots: + /// Establish a connection with the service when the latter is launched. + void onServiceStarted(); + /// Handle event of successful connection to the service. + void connectedToService(); + /// Start the process of reconnecting to the service. + void startReconnectingToService(); + /// Stop the process of reconnecting to the service. + void stopReconnectingToService(); + /// Handle socket error. + /// @param aSocketEror Socket error code. + void handleSocketError(DapUiSocketError aSocketEror); + /// Reconnect service. + void reconnectToService(); + /// Handle service error. + /// @param aServiceEror Service error code. + void handleServiceError(DapServiceError aServiceEror); + +public slots: + /// Initiate the service monitor. + virtual DapServiceError init() override; + /// Establish a connection with the service. + void connectToService(); + /// Handle service outage. + void disconnectFromService(); +}; + +#endif // DAPSERVICECLIENT_H diff --git a/KelvinDashboardGUI/DapServiceClientNativeAbstract.cpp b/KelvinDashboardGUI/DapServiceClientNativeAbstract.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d05bb31e7def2248337134498b49e4cca37001b --- /dev/null +++ b/KelvinDashboardGUI/DapServiceClientNativeAbstract.cpp @@ -0,0 +1,53 @@ +#include "DapServiceClientNativeAbstract.h" + +DapServiceClientNativeAbstract::DapServiceClientNativeAbstract() +{ + m_isServiceRunning = false; +} + +DapServiceError DapServiceClientNativeAbstract::init() +{ + qInfo() << "DapServiceClientNativeAbstract::init()"; + DapServiceError result = DapServiceError::NO_ERRORS; + if(!isServiceRunning()) + { + qInfo() << "Install the service in the system"; + + result = serviceInstallAndRun(); + + if(result != DapServiceError::NO_ERRORS) + return result; + + if(isServiceRunning()) + { + onServiceStarted(); + } + else + { + qCritical() << "Service not started after " + "'serviceInstallAndRun' operation!"; + } + } + else + { + onServiceStarted(); + } + return result; +} + +void DapServiceClientNativeAbstract::onServiceInstalled() +{ + qInfo() << "DapServiceClientNativeAbstract::onServiceInstalled()"; + if(isServiceRunning()) + onServiceStarted(); +} + +void DapServiceClientNativeAbstract::onServiceStarted() +{ + +} + +void DapServiceClientNativeAbstract::onServiceStopped() +{ + +} diff --git a/KelvinDashboardGUI/DapServiceClientNativeAbstract.h b/KelvinDashboardGUI/DapServiceClientNativeAbstract.h new file mode 100644 index 0000000000000000000000000000000000000000..de92eea8267afdee8a5416292b08a4849b0a2930 --- /dev/null +++ b/KelvinDashboardGUI/DapServiceClientNativeAbstract.h @@ -0,0 +1,40 @@ +#ifndef DAPSERVICECLIENTNATIVEABSTRACT_H +#define DAPSERVICECLIENTNATIVEABSTRACT_H + +#include <QTimer> +#include <QDebug> + +enum class DapServiceError { + NO_ERRORS, + USER_COMMAND_ABORT, + UNABLE_START_SERVICE, + UNABLE_STOP_SERVICE, + SERVICE_NOT_FOUND, + UNKNOWN_ERROR +}; + +class DapServiceClientNativeAbstract +{ +public: + DapServiceClientNativeAbstract(); + virtual ~DapServiceClientNativeAbstract() { } + + virtual bool isServiceRunning() = 0; + + virtual DapServiceError serviceInstallAndRun() = 0; + virtual DapServiceError serviceStart() = 0; + virtual DapServiceError serviceStop() = 0; + virtual DapServiceError serviceRestart() = 0; + +public slots: + virtual DapServiceError init(); +protected: + bool m_isServiceRunning; + +protected slots: + virtual void onServiceInstalled(); + virtual void onServiceStarted(); + virtual void onServiceStopped(); +}; + +#endif // DAPSERVICECLIENTNATIVEABSTRACT_H diff --git a/KelvinDashboardGUI/DapServiceClientNativeLinux.cpp b/KelvinDashboardGUI/DapServiceClientNativeLinux.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fbb5af195eeea9f721b6d5e0cf22e6df8a4edfb4 --- /dev/null +++ b/KelvinDashboardGUI/DapServiceClientNativeLinux.cpp @@ -0,0 +1,75 @@ +#include "DapServiceClientNativeLinux.h" + +#include <QtDebug> +#include <QMessageBox> + +DapServiceClientNativeLinux::DapServiceClientNativeLinux() +{ + QString dapServiceNameToLower = QString(DAP_SERVICE_NAME).toLower(); + QString cmd = QString("ps -C %1 > /dev/null").arg(DAP_SERVICE_NAME); + m_checkIsServiceRunningCommand = strdup(cmd.toLatin1().data()); + + m_cmdTemplate = QString("service " + dapServiceNameToLower) + " %1"; + + qDebug() << "command for check is service running: " << m_checkIsServiceRunningCommand; +} + +DapServiceClientNativeLinux::~DapServiceClientNativeLinux() +{ + delete m_checkIsServiceRunningCommand; +} + +bool DapServiceClientNativeLinux::isServiceRunning() +{ + m_isServiceRunning = (::system(m_checkIsServiceRunningCommand) == 0); + return m_isServiceRunning; +} + +DapServiceError DapServiceClientNativeLinux::serviceRestart() +{ + qDebug() << "Restart service name" << m_cmdTemplate.arg("restart").toLatin1().data(); + + int retCode = ::system(m_cmdTemplate.arg("restart").toLatin1().data()); + qDebug() << "Restart result code:" << retCode; + if(retCode != 0) { + return DapServiceError::USER_COMMAND_ABORT; + } + + return DapServiceError::NO_ERRORS; +} + +/** + * @brief SapNetworkClientNativeLinux::serviceStart + */ +DapServiceError DapServiceClientNativeLinux::serviceStart() +{ + // yes better use restart + int ret = ::system(m_cmdTemplate.arg("restart").toLatin1().data()); + qDebug() << "serviceStart Result: " << ret; + + if(ret != 0) { + return DapServiceError::USER_COMMAND_ABORT; + } + + return DapServiceError::NO_ERRORS; +} + +/** + * @brief SapServiceClientNativeLinux::serviceStop + */ +DapServiceError DapServiceClientNativeLinux::serviceStop() +{ + int ret = ::system(m_cmdTemplate.arg("stop").toLatin1().data()); + qDebug() << "serviceStop result:" << ret; + if(ret != 0) { + return DapServiceError::USER_COMMAND_ABORT; + } + return DapServiceError::NO_ERRORS; +} +/** + * @brief SapServiceClientNativeLinux::serviceInstallAndRun + */ +DapServiceError DapServiceClientNativeLinux::serviceInstallAndRun() +{ + return serviceStart(); +} diff --git a/KelvinDashboardGUI/DapServiceClientNativeLinux.h b/KelvinDashboardGUI/DapServiceClientNativeLinux.h new file mode 100644 index 0000000000000000000000000000000000000000..16068d9ef59d8450abfbe9006c228a817d250dce --- /dev/null +++ b/KelvinDashboardGUI/DapServiceClientNativeLinux.h @@ -0,0 +1,21 @@ +#ifndef DAPSERVICECLIENTNATIVELINUX_H +#define DAPSERVICECLIENTNATIVELINUX_H + +#include "DapServiceClientNativeAbstract.h" + +class DapServiceClientNativeLinux : public DapServiceClientNativeAbstract +{ + const char* m_checkIsServiceRunningCommand; + QString m_cmdTemplate; +public: + DapServiceClientNativeLinux(); + ~DapServiceClientNativeLinux() override; + bool isServiceRunning() override; + DapServiceError serviceStart() override; + DapServiceError serviceRestart() override; + + DapServiceError serviceStop() override; + DapServiceError serviceInstallAndRun() override; +}; + +#endif // DAPSERVICECLIENTNATIVELINUX_H diff --git a/KelvinDashboardGUI/DapServiceController.cpp b/KelvinDashboardGUI/DapServiceController.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d54b016469bf5e5545dba540d2a37ea666d1e49 --- /dev/null +++ b/KelvinDashboardGUI/DapServiceController.cpp @@ -0,0 +1,68 @@ +#include "DapServiceController.h" +#include "DapUiQmlWidgetModel.h" +DapServiceController::DapServiceController(QObject *apParent) + : QObject(apParent) +{ + +} + +/// Show or hide GUI client by clicking on the tray icon. +/// @param aIsActivated Accepts true - when requesting to +/// display a client, falso - when requesting to hide a client. +void DapServiceController::activateClient(bool aIsActivated) +{ + if(aIsActivated) + emit activateWindow(); +} + +/// Shut down client. +void DapServiceController::closeClient() +{ + qApp->quit(); +} + +/// Get company brand. +/// @return Brand Ñompany. +void DapServiceController::init(DapServiceClient *apDapServiceClient) +{ + m_pDapServiceClient = apDapServiceClient; + + // Creating rpc controller + m_pDapCommandController = new DapCommandController(apDapServiceClient->getClientSocket(), this); + + // Signal-slot connection that implements the client display control when you click on the tray icon + connect(m_pDapCommandController, SIGNAL(onClientActivate(bool)), SLOT(activateClient(bool))); + // Signal-slot connection that implements the client display control when you click on the tray icon + connect(m_pDapCommandController, SIGNAL(onClientClose()), SLOT(closeClient())); +} + +QString DapServiceController::getBrand() const +{ + return m_sBrand; +} + +/// Get app version. +/// @return Application version. +QString DapServiceController::getVersion() const +{ + return m_sVersion; +} + +/// Get an instance of a class. +/// @return Instance of a class. +DapServiceController &DapServiceController::getInstance() +{ + static DapServiceController instance; + return instance; +} + +/// Method that implements the singleton pattern for the qml layer. +/// @param engine QML application. +/// @param scriptEngine The QJSEngine class provides an environment for evaluating JavaScript code. +QObject *DapServiceController::singletonProvider(QQmlEngine *engine, QJSEngine *scriptEngine) +{ + Q_UNUSED(engine) + Q_UNUSED(scriptEngine) + + return &getInstance(); +} diff --git a/KelvinDashboardGUI/DapServiceController.h b/KelvinDashboardGUI/DapServiceController.h new file mode 100644 index 0000000000000000000000000000000000000000..539a9edc98d1fe67c4d96edade9b9f6d5c11164c --- /dev/null +++ b/KelvinDashboardGUI/DapServiceController.h @@ -0,0 +1,79 @@ +#ifndef DAPSERVICECONTROLLER_H +#define DAPSERVICECONTROLLER_H + +#include <QObject> +#include <QQmlEngine> +#include <QJSEngine> +#include <QApplication> +#include <QTimer> + +#include "DapCommandController.h" +#include "DapServiceClient.h" + +class DapServiceController : public QObject +{ + Q_OBJECT + Q_DISABLE_COPY(DapServiceController) + /// Brand Ñompany. + QString m_sBrand {DAP_BRAND}; + /// Application version. + QString m_sVersion {DAP_VERSION}; + + /// Service connection management service. + DapServiceClient *m_pDapServiceClient {nullptr}; + /// RPC protocol controller. + DapCommandController *m_pDapCommandController {nullptr}; + + explicit DapServiceController(QObject *apParent = nullptr); + +public: + + + /// Get an instance of a class. + /// @return Instance of a class. + Q_INVOKABLE static DapServiceController &getInstance(); + + ///******************************************** + /// Property + /// ******************************************* + + /// Brand Ñompany. + Q_PROPERTY(QString Brand MEMBER m_sBrand READ getBrand NOTIFY brandChanged) + /// Application version. + Q_PROPERTY(QString Version MEMBER m_sVersion READ getVersion NOTIFY versionChanged) + + ///******************************************** + /// Interface + /// ******************************************* + void init(DapServiceClient *apDapServiceClient); + /// Get company brand. + /// @return Brand Ñompany. + QString getBrand() const; + /// Get app version. + /// @return Application version. + QString getVersion() const; + +signals: + /// The signal is emitted when the Brand company property changes. + void brandChanged(const QString &brand); + /// The signal is emitted when the Application version property changes. + void versionChanged(const QString &version); + /// The signal is emitted when the main application window is activated. + void activateWindow(); + /// The signal is emitted when checking the existence of an already running copy of the application. + void isExistenceClient(bool isExistenceClient); + +public slots: + /// Show or hide GUI client by clicking on the tray icon. + /// @param aIsActivated Accepts true - when requesting to + /// display a client, falso - when requesting to hide a client. + void activateClient(bool aIsActivated); + /// Shut down client. + void closeClient(); + /// Method that implements the singleton pattern for the qml layer. + /// @param engine QML application. + /// @param scriptEngine The QJSEngine class provides an environment for evaluating JavaScript code. + static QObject *singletonProvider(QQmlEngine *engine, QJSEngine *scriptEngine); +}; + +#endif // DAPSERVICECONTROLLER_H diff --git a/KelvinDashboardGUI/DapUiQmlScreenChangeWidget.qml b/KelvinDashboardGUI/DapUiQmlScreenChangeWidget.qml old mode 100644 new mode 100755 index 26ffd1655711ff9e6e2acf997f0433c329f7f3cc..4ff8f4e2fdf9d14124c70534ba37e5334733a914 --- a/KelvinDashboardGUI/DapUiQmlScreenChangeWidget.qml +++ b/KelvinDashboardGUI/DapUiQmlScreenChangeWidget.qml @@ -102,7 +102,7 @@ Dialog { anchors.margins: 1 - model: DapUiQmlWidgetModel + model: dapUiQmlWidgetModel clip: true @@ -135,8 +135,8 @@ Dialog { { listViewDapWidgets.currentIndex = index var item = DapUiQmlWidgetModel.get(index) - DapUiQmlWidgetModel.set(index, DapUiQmlWidgetModel.get(index).name, DapUiQmlWidgetModel.get(index).URLpage, DapUiQmlWidgetModel.get(index).image, !item.visible) - console.log("I: " +index + " : " + DapUiQmlWidgetModel.get(index) + " value = " + !item.visible) + dapUiQmlWidgetModel.set(index, dapUiQmlWidgetModel.get(index).name, dapUiQmlWidgetModel.get(index).URLpage, dapUiQmlWidgetModel.get(index).image, !item.visible) + console.log("I: " +index + " : " + dapUiQmlWidgetModel.get(index) + " value = " + !item.visible) if(checkBoxWidget.checked) { diff --git a/KelvinDashboardGUI/DapUiQmlScreenDashboard.qml b/KelvinDashboardGUI/DapUiQmlScreenDashboard.qml old mode 100644 new mode 100755 index f4963d3b99554539cba7d87cf964a0efc38301ba..15d4eaa70025e6ce8af4ff38dc6e8c73bc60028e --- a/KelvinDashboardGUI/DapUiQmlScreenDashboard.qml +++ b/KelvinDashboardGUI/DapUiQmlScreenDashboard.qml @@ -17,10 +17,6 @@ Page { ListModel { id: listModelTabs - ListElement { - name: qsTr("Login") - page: "DapQmlScreenLogin.qml" - } ListElement { name: qsTr("Dashboard") page: "DapUiQmlScreenDialog.qml" @@ -80,7 +76,7 @@ Page { id: stackViewScreenDashboard anchors.fill: parent anchors.margins: 1 - source: "DapQmlScreenLogin.qml" + source: "DapUiQmlScreenDialog.qml" } } } diff --git a/KelvinDashboardGUI/DapUiQmlScreenLogin.ui.qml b/KelvinDashboardGUI/DapUiQmlScreenLogin.ui.qml deleted file mode 100644 index 3e03cb113f0476f1f7dce744bb4fb19a68c0de20..0000000000000000000000000000000000000000 --- a/KelvinDashboardGUI/DapUiQmlScreenLogin.ui.qml +++ /dev/null @@ -1,52 +0,0 @@ -import QtQuick 2.9 -import QtQuick.Controls 2.2 -import KelvinDashboard 1.0 - -Page { - id: dapUiQmlScreenLogin - - property alias dapScreenLogin: dapScreenLogin - property alias textFieldPassword: textFieldPassword - property alias buttonPassword: buttonPassword - property alias textStatus: textStatus - - title: qsTr("Login") - - DapScreenLogin - { - id: dapScreenLogin - } - - Column { - id: columnScreenLogin - width: parent.width - anchors.centerIn: parent - spacing: 10 - clip: true - - TextField { - id: textFieldPassword - placeholderText: "Password" - echoMode: TextInput.Password - anchors.horizontalCenter: parent.horizontalCenter - } - - Text { - id: textStatus - width: parent.width - wrapMode: Text.WordWrap - horizontalAlignment: Text.AlignHCenter - anchors.horizontalCenter: parent.horizontalCenter - visible: false - } - - Button { - id: buttonPassword - text: "Log in" - width: textFieldPassword.width - anchors.horizontalCenter: parent.horizontalCenter - focus: true - } - } - -} diff --git a/KelvinDashboardGUI/DapUiQmlWidgetModel.cpp b/KelvinDashboardGUI/DapUiQmlWidgetModel.cpp old mode 100644 new mode 100755 index 2f8cef3afc9bb545a1d73a550f6f52eca8f2f7b6..41c2b477eeb7d90cffe14421eef5328a1ebadfce --- a/KelvinDashboardGUI/DapUiQmlWidgetModel.cpp +++ b/KelvinDashboardGUI/DapUiQmlWidgetModel.cpp @@ -8,6 +8,7 @@ DapUiQmlWidgetModel::DapUiQmlWidgetModel(QObject *parent) : QAbstractListModel(p m_dapUiQmlWidgets.append(new DapUiQmlWidget( "Services share control", "DapUiQmlWidgetChainServicesShareControl.ui.qml", "qrc:/Resources/Icons/add.png")); m_dapUiQmlWidgets.append(new DapUiQmlWidget( "Settings", "DapUiQmlWidgetChainSettings.ui.qml", "qrc:/Resources/Icons/add.png")); m_dapUiQmlWidgets.append(new DapUiQmlWidget( "Wallet", "DapUiQmlWidgetChainWallet.ui.qml", "qrc:/Resources/Icons/add.png")); + m_dapUiQmlWidgets.append(new DapUiQmlWidget( "Logs", "DapUiQmlWidgetChainNodeLogs.ui.qml", "qrc:/Resources/Icons/add.png")); } DapUiQmlWidgetModel &DapUiQmlWidgetModel::getInstance() diff --git a/KelvinDashboardGUI/KelvinDashboardGUI.pro b/KelvinDashboardGUI/KelvinDashboardGUI.pro old mode 100644 new mode 100755 index e9cdca2267b0db8d0a96e273695758b5d05276aa..44407b81285ea0965335e7a8e9124b0ec4556016 --- a/KelvinDashboardGUI/KelvinDashboardGUI.pro +++ b/KelvinDashboardGUI/KelvinDashboardGUI.pro @@ -1,4 +1,4 @@ -QT += quick widgets quickwidgets +QT += qml quick widgets TEMPLATE = app CONFIG += c++11 @@ -29,6 +29,7 @@ else { # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS DEFINES += DAP_BRAND=\\\"$$BRAND\\\" +DEFINES += DAP_SERVICE_NAME=\\\"$${BRAND}Service\\\" DEFINES += DAP_VERSION=\\\"$$VERSION\\\" ICON = icon.ico # You can also make your code fail to compile if you use deprecated APIs. @@ -43,12 +44,15 @@ SOURCES += \ DapUiQmlWidgetChainNodeLogs.cpp \ DapUiQmlWidgetChainTransctions.cpp \ DapUiQmlWidgetChainOperations.cpp \ - DapScreenLogin.cpp \ - DapClient.cpp \ DapUiQmlWidgetModel.cpp \ DapUiQmlWidget.cpp \ DapScreenDialog.cpp \ - DapScreenDialogChangeWidget.cpp + DapScreenDialogChangeWidget.cpp \ + DapServiceClient.cpp \ + DapServiceController.cpp \ + DapCommandController.cpp \ + DapServiceClientNativeAbstract.cpp \ + DapServiceClientNativeLinux.cpp RESOURCES += qml.qrc @@ -70,15 +74,19 @@ HEADERS += \ DapUiQmlWidgetChainTransctions.h \ DapUiQmlScreenDashboard.h \ DapUiQmlWidgetChainOperations.h \ - DapScreenLogin.h \ - DapClient.h \ DapUiQmlWidgetModel.h \ DapUiQmlWidget.h \ DapScreenDialog.h \ - DapScreenDialogChangeWidget.h + DapScreenDialogChangeWidget.h \ + DapServiceClient.h \ + DapServiceController.h \ + DapCommandController.h \ + DapServiceClientNativeAbstract.h \ + DapServiceClientNativeLinux.h include (../libKelvinDashboardCommon/libKelvinDashboardCommon.pri) +include (../DapRPCProtocol/DapRPCProtocol.pri) INCLUDEPATH += $$_PRO_FILE_PWD_/../libKelvinDashboardCommon/ $$_PRO_FILE_PWD_/../libdap/ diff --git a/KelvinDashboardGUI/main.cpp b/KelvinDashboardGUI/main.cpp old mode 100644 new mode 100755 index ac42f11850839b16bdb0772d9ed9cd75ce745b12..faa83fbbc37dd196a6ef8fde91fe7295948457ca --- a/KelvinDashboardGUI/main.cpp +++ b/KelvinDashboardGUI/main.cpp @@ -8,30 +8,16 @@ #include <QSharedMemory> #include "DapHalper.h" -#include "DapClient.h" -#include "DapScreenLogin.h" #include "DapScreenDialog.h" #include "DapScreenDialogChangeWidget.h" #include "DapUiQmlWidgetModel.h" #include "DapSettings.h" #include "DapSettingsCipher.h" +#include "DapServiceClient.h" +#include "DapServiceController.h" int main(int argc, char *argv[]) { - // Creating a semaphore for locking external resources, as well as initializing an external resource-memory - QSystemSemaphore systemSemaphore(QString("systemSemaphore for %1").arg("KelvinDashboardGUI"), 1); -#ifndef Q_OS_WIN - QSharedMemory memmoryAppBagFix(QString("memmory for %1").arg("KelvinDashboardGUI")); -#endif - QSharedMemory memmoryApp(QString("memmory for %1").arg("KelvinDashboardGUI")); - // Check for the existence of a running instance of the program - bool isRunning = DapHalper::getInstance().checkExistenceRunningInstanceApp(systemSemaphore, memmoryApp, memmoryAppBagFix); - - if(isRunning) - { - return 1; - } - QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication app(argc, argv); @@ -40,29 +26,31 @@ int main(int argc, char *argv[]) app.setApplicationName("Kelvin Client"); app.setWindowIcon(QIcon(":/Resources/Icons/icon.ico")); - // Set the name of the service connection - DapClient::getInstance().setNameSrvice(app.applicationName()); - // We establish a connection with the service - DapClient::getInstance().connectToService("Kelvin Client"); + /// Local client. + DapServiceClient dapServiceClient; + // Creating a service controller + DapServiceController &controller = DapServiceController::getInstance(); + controller.init(&dapServiceClient); + dapServiceClient.init(); - qmlRegisterType<DapScreenLogin>("KelvinDashboard", 1, 0, "DapScreenLogin"); qmlRegisterType<DapScreenDialog>("KelvinDashboard", 1, 0, "DapScreenDialog"); qmlRegisterType<DapScreenDialogChangeWidget>("KelvinDashboard", 1, 0, "DapScreenDialogChangeWidget"); - qmlRegisterSingletonType<DapClient>("KelvinDashboard", 1, 0, "DapClient", DapClient::singletonProvider); + qmlRegisterSingletonType<DapServiceController>("KelvinDashboard", 1, 0, "DapServiceController", DapServiceController::singletonProvider); qmlRegisterSingletonType<DapUiQmlWidgetModel>("KelvinDashboard", 1, 0, "DapUiQmlWidgetModel", DapUiQmlWidgetModel::singletonProvider); QQmlApplicationEngine engine; - engine.rootContext()->setContextProperty("dapClient", &DapClient::getInstance()); + engine.rootContext()->setContextProperty("dapServiceController", &DapServiceController::getInstance()); + engine.rootContext()->setContextProperty("dapUiQmlWidgetModel", &DapUiQmlWidgetModel::getInstance()); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); - DapSettings &settings = DapSettings::getInstance("Settings.json"); - DapSettingsCipher &set = DapSettingsCipher::getInstance(settings); - qDebug() << "Settings file name: " << set.getFileName(); - set.setKeyValue("user", "Vasy"); - bool f = false; - set.setGroupPropertyValue("widgets", "name", "Services client", "visible", f); - qDebug() << set.getGroupPropertyValue("widgets", "name", "Services client", "visible").toBool(); - qDebug() << set.getKeyValue("user"); +// DapSettings &settings = DapSettings::getInstance("Settings.json"); +// DapSettingsCipher &set = DapSettingsCipher::getInstance(settings); +// qDebug() << "Settings file name: " << set.getFileName(); +// set.setKeyValue("user", "Vasy"); +// bool f = false; +// set.setGroupPropertyValue("widgets", "name", "Services client", "visible", f); +// qDebug() << set.getGroupPropertyValue("widgets", "name", "Services client", "visible").toBool(); +// qDebug() << set.getKeyValue("user"); if (engine.rootObjects().isEmpty()) return -1; diff --git a/KelvinDashboardGUI/main.qml b/KelvinDashboardGUI/main.qml old mode 100644 new mode 100755 index f80a6edc2f9f9b2bb33c64aa46af5cf9405b524f..fd7fede5a69c6da3425bd26d827f1dcca7e31f7b --- a/KelvinDashboardGUI/main.qml +++ b/KelvinDashboardGUI/main.qml @@ -13,11 +13,12 @@ ApplicationWindow { height: 480 onClosing: { + console.log("Close") window.hide() } Connections { - target: dapClient + target: dapServiceController onActivateWindow: { if(window.visibility === Window.Hidden) { @@ -29,19 +30,19 @@ ApplicationWindow { } } - onErrorConnect: { - imageNetwork.visible = false - if(imageErrorNetwork.visible) - imageErrorNetwork.visible = false - else - imageErrorNetwork.visible = true - } +// onErrorConnect: { +// imageNetwork.visible = false +// if(imageErrorNetwork.visible) +// imageErrorNetwork.visible = false +// else +// imageErrorNetwork.visible = true +// } - onConnectedToService: { - imageNetwork.visible = true - imageErrorNetwork.visible = false - console.log("Connected") - } +// onConnectedToService: { +// imageNetwork.visible = true +// imageErrorNetwork.visible = false +// console.log("Connected") +// } } @@ -103,7 +104,7 @@ ApplicationWindow { ListView { id: listViewMenu anchors.fill: parent - model: DapUiQmlWidgetModel + model: dapUiQmlWidgetModel delegate: Component { diff --git a/KelvinDashboardGUI/qml.qrc b/KelvinDashboardGUI/qml.qrc old mode 100644 new mode 100755 index 259fa3884c435b408c2f2d83a6f9938429ff0fd2..9bc4dd25ee4b7621dbf660c8c7d5ac634ef773a1 --- a/KelvinDashboardGUI/qml.qrc +++ b/KelvinDashboardGUI/qml.qrc @@ -4,7 +4,6 @@ <file>DapUiQmlScreenDashboard.qml</file> <file>DapUiQmlScreenMainWindow.ui.qml</file> <file>KelvinDashboardGUI.conf</file> - <file>DapUiQmlScreenLogin.ui.qml</file> <file>DapUiQmlWidgetChainBlockExplorer.ui.qml</file> <file>DapUiQmlWidgetChainExchanges.ui.qml</file> <file>DapUiQmlWidgetChainServicesClient.ui.qml</file> @@ -15,7 +14,6 @@ <file>Resources/Icons/icon.png</file> <file>Resources/Icons/icon.ico</file> <file>Resources/Icons/add.png</file> - <file>DapQmlScreenLogin.qml</file> <file>DapUiQmlScreenAbout.ui.qml</file> <file>DapQmlScreenAbout.qml</file> <file>Resources/Icons/iconErrorNetwork.png</file> @@ -23,5 +21,6 @@ <file>DapUiQmlWidgetDelegate.qml</file> <file>DapUiQmlWidgetDelegateForm.ui.qml</file> <file>DapUiQmlScreenChangeWidget.qml</file> + <file>DapUiQmlWidgetChainNodeLogs.ui.qml</file> </qresource> </RCC> diff --git a/KelvinDashboardService/DapChainDashboardAuth.cpp b/KelvinDashboardService/DapChainDashboardAuth.cpp deleted file mode 100644 index f9d3392ae85768c74a8ebd69ccbf0bec1e5600b2..0000000000000000000000000000000000000000 --- a/KelvinDashboardService/DapChainDashboardAuth.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include "DapChainDashboardAuth.h" - -/// Standart constructor. -DapChainDashboardAuth::DapChainDashboardAuth(QObject *parent) : QObject(parent) -{ - -} - -/// Get user password. -/// @return User password. -QString DapChainDashboardAuth::getPassword() const -{ - return m_password; -} - -/// Set user password. -/// @param password User password. -void DapChainDashboardAuth::setPassword(const QString &password) -{ - m_password = password; -} - -/// Execute the command. -/// @param command Executable command. -void DapChainDashboardAuth::runCommand(const DapCommand &command) -{ - qDebug() << "Run command authorization"; - DapCommand *answerCommand = new DapCommand(TypeDapCommand::Authorization); - answerCommand->setCountArguments(1); - if(getPassword() == command.getArgument(0).toString()) - answerCommand->addArgument(true); - else - answerCommand->addArgument(false); - emit onCommandCompleted(*answerCommand); -} - - - diff --git a/KelvinDashboardService/DapChainDashboardAuth.h b/KelvinDashboardService/DapChainDashboardAuth.h deleted file mode 100644 index 9adf631821311c666c99632ad063e7f7e85b8f78..0000000000000000000000000000000000000000 --- a/KelvinDashboardService/DapChainDashboardAuth.h +++ /dev/null @@ -1,48 +0,0 @@ -/**************************************************************************** -** -** This file is part of the KelvinDashboardService application. -** -** The class implements the authorization object. Stores the user password, -** provides a user authorization mechanism in the service. -** -****************************************************************************/ - -#ifndef DAPCHAINDASHBOARDAUTH_H -#define DAPCHAINDASHBOARDAUTH_H - -#include <QObject> - -#include "DapCommand.h" - -class DapChainDashboardAuth : public QObject -{ - Q_OBJECT - -protected: - /// User password. - QString m_password = "123"; - -public: - /// Standart constructor. - explicit DapChainDashboardAuth(QObject *parent = nullptr); - - ///******************************************** - /// Interface - /// ******************************************* - /// Get user password. - /// @return User password. - QString getPassword() const; - /// Set user password. - /// @param password User password. - void setPassword(const QString &password); - /// Execute the command. - /// @param command Executable command. - void runCommand(const DapCommand& command); - -signals: - /// The signal is emitted when the command is successful. - /// @param command The executed command. - void onCommandCompleted(const DapCommand& command); -}; - -#endif // DAPCHAINDASHBOARDAUTH_H diff --git a/KelvinDashboardService/DapChainDashboardService.cpp b/KelvinDashboardService/DapChainDashboardService.cpp old mode 100644 new mode 100755 index 618a4b397abd59fb71376d64ff68295bbf92a1ac..5e8841333b6356a0f4ccb73da5eaf5041158ca37 --- a/KelvinDashboardService/DapChainDashboardService.cpp +++ b/KelvinDashboardService/DapChainDashboardService.cpp @@ -1,49 +1,44 @@ #include "DapChainDashboardService.h" -DapChainDashboardService::DapChainDashboardService(QObject *parent) : QObject(parent) +DapChainDashboardService::DapChainDashboardService() : DapRpcService(nullptr) { - // Creating a local server to establish connection with the GUI client and then connect to it. - m_dapLocalServer = new DapLocalServer("Kelvin Client"); - m_dapLocalServer->connectWithClient(); - - // Signal-slot connection that triggers the identification of a received command. - connect(m_dapLocalServer, &DapLocalServer::commandRecieved, this, &DapChainDashboardService::identificationCommand); - // Signal-slot connection, starts processing the user authorization command. - connect(&m_dapChainDashboardAuth, &DapChainDashboardAuth::onCommandCompleted, m_dapLocalServer, &DapLocalServer::sendCommand); + connect(this, &DapChainDashboardService::onNewClientConnected, [=] { + qDebug() << "New client"; + }); } -/// Identification of the command received. -/// @param command Command received -/// @return Returns true if the command is identified, otherwise - false. -bool DapChainDashboardService::identificationCommand(const DapCommand &command) +bool DapChainDashboardService::start() { - qDebug() << "Identification command: " << command.getTypeCommand(); - switch (command.getTypeCommand()) + qInfo() << "DapChainDashboardService::start()"; + + m_pSocketService = new DapUiSocketServer(); + m_pServer = new DapUiService(); + if(m_pServer->listen(DAP_BRAND)) { - // User authorization - case TypeDapCommand::Authorization: - m_dapChainDashboardAuth.runCommand(command); - return true; - default: + connect(m_pServer, SIGNAL(onClientConnected()), SIGNAL(onNewClientConnected())); + m_pServer->addService(this); + } + else + { + qCritical() << QString("Can't listen on %1").arg(DAP_BRAND); return false; } + return true; } + /// Activate the main client window by double-clicking the application icon in the system tray. /// @param reason Type of action on the icon in the system tray. -void DapChainDashboardService::clientActivated(const QSystemTrayIcon::ActivationReason& reason) +void DapChainDashboardService::activateClient(const QSystemTrayIcon::ActivationReason& reason) { - qDebug() << "Client activated"; + qInfo() << "DapChainDashboardService::activateClient()"; switch (reason) { case QSystemTrayIcon::Trigger: { - if(m_dapLocalServer->isClientExist()) - { - qDebug() << "Send command activated"; - DapCommand *command = new DapCommand(TypeDapCommand::ActivateWindowClient, {true}); - m_dapLocalServer->sendCommand(*command); - } + QJsonArray arguments; + arguments.append(true); + m_pServer->notifyConnectedClients("RPCClient.activateClient", arguments); } break; default: @@ -54,8 +49,11 @@ void DapChainDashboardService::clientActivated(const QSystemTrayIcon::Activation /// Shut down client. void DapChainDashboardService::closeClient() { - DapCommand *command = new DapCommand(TypeDapCommand::CloseClient, {true}); - m_dapLocalServer->sendCommand(*command); + qDebug() << "Close client"; + QJsonArray arguments; + m_pServer->notifyConnectedClients("RPCClient.closeClient", arguments); + // Signal-slot connection shutting down the service after closing the client + connect(m_pServer, SIGNAL(onClientDisconnected()), qApp, SLOT(quit())); } /// System tray initialization. @@ -75,11 +73,10 @@ void DapChainDashboardService::initTray() connect(quitAction, &QAction::triggered, this, [=] { closeClient(); - qApp->quit(); }); // With a double click on the icon in the system tray, // we send a command to the client to activate the main window connect(trayIconKelvinDashboard, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), - this, SLOT(clientActivated(QSystemTrayIcon::ActivationReason))); + this, SLOT(activateClient(QSystemTrayIcon::ActivationReason))); } diff --git a/KelvinDashboardService/DapChainDashboardService.h b/KelvinDashboardService/DapChainDashboardService.h old mode 100644 new mode 100755 index ef4d75b9af12732caf65f567a8b1080726370d5f..06cc40a8b6c27d9b6248301eac01a2f2049c2ff9 --- a/KelvinDashboardService/DapChainDashboardService.h +++ b/KelvinDashboardService/DapChainDashboardService.h @@ -18,29 +18,35 @@ #include <QAction> #include <QApplication> -#include "DapLocalServer.h" -#include "DapChainDashboardAuth.h" +#include "DapRpcAbstractServer.h" +#include "DapRpcLocalServer.h" +#include "DapRpcTCPServer.h" +#include "DapRpcService.h" -class DapChainDashboardService : public QObject +#include <QLocalServer> +typedef class DapRpcLocalServer DapUiService; +typedef class QLocalServer DapUiSocketServer; + +class DapChainDashboardService : public DapRpcService { Q_OBJECT - - /// The object responsible for authorization of the user in the application. - DapChainDashboardAuth m_dapChainDashboardAuth; - /// Local server for establishing connection with GUI client. - DapLocalServer *m_dapLocalServer; + Q_CLASSINFO("serviceName", "RPCServer") + DapUiService * m_pServer {nullptr}; + DapUiSocketServer * m_pSocketService {nullptr}; public: /// Standard Ñonstructor. - explicit DapChainDashboardService(QObject *parent = nullptr); + explicit DapChainDashboardService(); + + bool start(); + +signals: + /// The signal is emitted in case of successful connection of a new client. + void onNewClientConnected(); public slots: - /// Identification of the command received. - /// @param command Command received. - /// @return Returns true if the command is identified, otherwise - false. - bool identificationCommand(const DapCommand &command); /// Activate the main client window by double-clicking the application icon in the system tray. /// @param reason Type of action on the icon in the system tray. - void clientActivated(const QSystemTrayIcon::ActivationReason& reason); + void activateClient(const QSystemTrayIcon::ActivationReason& reason); /// Shut down client. void closeClient(); /// System tray initialization. diff --git a/KelvinDashboardService/KelvinDashboardService.pro b/KelvinDashboardService/KelvinDashboardService.pro old mode 100644 new mode 100755 index 314e893af5e0f84ebc67912fa3817d139e38e1a2..212499b4e86ff73915a21f297608f762d8a0db21 --- a/KelvinDashboardService/KelvinDashboardService.pro +++ b/KelvinDashboardService/KelvinDashboardService.pro @@ -1,4 +1,4 @@ -QT += network widgets +QT += core network widgets QT += gui CONFIG += c++11 @@ -8,6 +8,7 @@ CONFIG -= app_bundle # Default brand BRAND = KelvinDashboard } +DEFINES += DAP_BRAND=\\\"$$BRAND\\\" TARGET = $${BRAND}Service @@ -38,17 +39,16 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp \ DapChainDashboardService.cpp \ - DapChainDashboardAuth.cpp \ DapChainNode.cpp \ DapChainNodeCache.cpp HEADERS += \ DapChainDashboardService.h \ - DapChainDashboardAuth.h \ DapChainNode.h \ DapChainNodeCache.h include (../libKelvinDashboardCommon/libKelvinDashboardCommon.pri) +include (../DapRPCProtocol/DapRPCProtocol.pri) INCLUDEPATH += $$_PRO_FILE_PWD_/../libKelvinDashboardCommon/ $$_PRO_FILE_PWD_/../libdap/ diff --git a/KelvinDashboardService/main.cpp b/KelvinDashboardService/main.cpp old mode 100644 new mode 100755 index d6308ba276fc2bd038afd35211f1f65b796ce928..ac6fd0ccdd4cf52fac9c5162dd076afbfaeef475 --- a/KelvinDashboardService/main.cpp +++ b/KelvinDashboardService/main.cpp @@ -28,6 +28,7 @@ int main(int argc, char *argv[]) // Creating the main application object DapChainDashboardService service; + service.start(); // Initialization of the application in the system tray service.initTray(); diff --git a/libKelvinDashboardCommon/DapCommand.cpp b/libKelvinDashboardCommon/DapCommand.cpp deleted file mode 100644 index 37294267ce97b8bf3fbfcb2852f13364470a099a..0000000000000000000000000000000000000000 --- a/libKelvinDashboardCommon/DapCommand.cpp +++ /dev/null @@ -1,124 +0,0 @@ -#include "DapCommand.h" - -/// Redefined Ñonstructor. -/// @param byteArray Interpreting a Ñommand in a byte array. -DapCommand::DapCommand(const QByteArray &byteArray) -{ - this->deserialize(byteArray); -} - -/// Add argument. -/// @param argument The argument. -void DapCommand::addArgument(const QVariant &argument) -{ - m_arguments.push_back(argument); -} - -/// Get the number of arguments. -/// @return Number of arguments. -unsigned DapCommand::getCountArguments() const -{ - return m_countArguments; -} - -/// Set the number of arguments. -/// @return Number of arguments. -void DapCommand::setCountArguments(const unsigned &countArguments) -{ - m_countArguments = countArguments; -} - -/// Get the type of command. -/// @return The type of command. -const TypeDapCommand& DapCommand::getTypeCommand() const -{ - return m_typeCommand; -} - -/// Set the type of command. -/// @param The type of command. -void DapCommand::setTypeCommand(const TypeDapCommand &typeCommand) -{ - m_typeCommand = typeCommand; -} - -/// Get an argument. -/// @param pos The position of the argument in the collection of arguments. -/// @return The argument. -QVariant DapCommand::getArgument(int pos) const -{ - if(!m_arguments.isEmpty() && pos >=0 && pos < m_arguments.size()) - return m_arguments.at(pos); - return QVariant(); -} - -/// Get a collection of arguments. -/// @return Сollection of arguments. -const QVector<QVariant>& DapCommand::getArguments() const -{ - return m_arguments; -} - -/// Set a collection of arguments. -/// @return arguments Сollection of arguments. -void DapCommand::setArguments(const std::initializer_list<QVariant> &arguments) -{ - m_arguments.append(arguments); -} - -/// Set a collection of arguments. -/// @return arguments Сollection of arguments. -void DapCommand::setArguments(const QVector<QVariant> &arguments) -{ - m_arguments.append(arguments); -} - -/// Serialize a command to a byte array. -/// @return A serialized command interpreted as a byte array. -const QByteArray &DapCommand::serialize() -{ - QDataStream output( &m_byteArrayBuffer, QIODevice::WriteOnly ); - output << (*this); - return m_byteArrayBuffer; -} - -/// Deserialization of a command from a byte array. -/// @param byteArray A serialized command interpreted as a byte array. -void DapCommand::deserialize(const QByteArray &byteArray) -{ - QDataStream input(byteArray); - input>>(*this); -} - -/// Overloaded version of stream output. -/// @param ds Binary data stream. -/// @param dapCommand The command. -QDataStream& operator <<(QDataStream &dataStream, const DapCommand &dapCommand) -{ - dataStream << dapCommand.getTypeCommand() << dapCommand.getCountArguments(); - for(const QVariant& argument : dapCommand.getArguments()) - { - dataStream << argument; - } - return dataStream; -} - -/// Overloaded version of streaming input. -/// @param ds Binary data stream. -/// @param dapCommand The command. -QDataStream& operator >>(QDataStream &dataStream, DapCommand &dapCommand) -{ - int typeCommand{0}; - unsigned countArguments{0}; - dataStream >> typeCommand >> countArguments; - dapCommand.setTypeCommand(static_cast<TypeDapCommand>(typeCommand)); - dapCommand.setCountArguments(countArguments); - for(register unsigned a{0}; a < countArguments; ++a) - { - QVariant argument; - dataStream >> argument; - dapCommand.addArgument(argument); - } - - return dataStream; -} diff --git a/libKelvinDashboardCommon/DapCommand.h b/libKelvinDashboardCommon/DapCommand.h deleted file mode 100644 index b326121686f2506597ade3c3d7db2d7e4178d8f3..0000000000000000000000000000000000000000 --- a/libKelvinDashboardCommon/DapCommand.h +++ /dev/null @@ -1,123 +0,0 @@ -/**************************************************************************** -** -** This file is part of the libKelvinDashboardClient library. -** -** The class implements the essence of the command. The signature of the -** command includes: the type of the command, the number of arguments in -** the command, a collection of arguments of the type QVariant. -** -****************************************************************************/ - -#ifndef DAPCOMMAND_H -#define DAPCOMMAND_H - -#include <QVector> -#include <QString> -#include <QByteArray> -#include <QIODevice> -#include <QDataStream> -#include <QDebug> -#include <initializer_list> - -/**************************************************************************** -** -** This file is part of the libKelvinDashboardClient library. -** -** The enum type implements the nomenclature of commands used in client-server -** communication. -** -****************************************************************************/ - -enum TypeDapCommand -{ - None, // Type is undefined - Authorization, // User authorization - ActivateWindowClient, // Activate the main application window if minimized to the system tray - CheckExistenceClient, // Checking the existence of a client connected to the server - CloseClient // Shut down the client -}; - -class DapCommand -{ - /// Overloaded version of stream output. - /// @param ds Binary data stream. - /// @param dapCommand The command. - friend QDataStream& operator<<(QDataStream &ds, const DapCommand &dapCommand); - /// Overloaded version of streaming input. - /// @param ds Binary data stream. - /// @param dapCommand The command. - friend QDataStream& operator>>(QDataStream &ds, DapCommand &dapCommand); - -protected: - /// Type of the command. - TypeDapCommand m_typeCommand {TypeDapCommand::None}; - /// Number of arguments. - unsigned m_countArguments{0}; - /// Collection of the command. - QVector<QVariant> m_arguments; - /// Interpreting a Ñommand in a byte array. - QByteArray m_byteArrayBuffer; - -public: - /// Standart constructor. - DapCommand() = default; - /// Redefined Ñonstructor. - /// @param byteArray Interpreting a Ñommand in a byte array. - DapCommand(const QByteArray& byteArray); - /// Redefined Ñonstructor. - /// @param typeCommand The type of command. - DapCommand(const TypeDapCommand &typeCommand) : m_typeCommand(typeCommand) { } - /// Redefined Ñonstructor. - /// @param typeCommand The type of command. - /// @param countArguments Number of arguments. - /// @param args Сollection of arguments. - DapCommand(const TypeDapCommand &typeCommand, const unsigned& countArguments, const std::initializer_list<QVariant> &args) - : m_typeCommand(typeCommand), m_countArguments(countArguments), m_arguments(args) { } - /// Redefined Ñonstructor. - /// @param typeCommand The type of command. - /// @param args Сollection of arguments. - DapCommand(const TypeDapCommand &typeCommand, const std::initializer_list<QVariant> &args) - : m_typeCommand(typeCommand), m_arguments(args) { } - - ///******************************************** - /// Interface - /// ******************************************* - - /// Get the type of command. - /// @return The type of command. - const TypeDapCommand &getTypeCommand() const; - /// Set the type of command. - /// @param The type of command. - void setTypeCommand(const TypeDapCommand &typeCommand); - /// Get an argument. - /// @param pos The position of the argument in the collection of arguments. - /// @return The argument. - QVariant getArgument(int pos) const; - /// Get a collection of arguments. - /// @return Сollection of arguments. - const QVector<QVariant> &getArguments() const; - /// Set a collection of arguments. - /// @param arguments Сollection of arguments. - void setArguments(const std::initializer_list<QVariant> &arguments); - /// Set a collection of arguments. - /// @return arguments Сollection of arguments. - void setArguments(const QVector<QVariant> &arguments); - /// Serialize a command to a byte array. - /// @return A serialized command interpreted as a byte array. - const QByteArray &serialize(); - /// Deserialization of a command from a byte array. - /// @param byteArray A serialized command interpreted as a byte array. - void deserialize(const QByteArray &byteArray); - /// Get the number of arguments. - /// @return Number of arguments. - unsigned getCountArguments() const; - /// Set the number of arguments. - /// @return Number of arguments. - void setCountArguments(const unsigned& countArguments); - /// Add argument. - /// @param argument The argument. - void addArgument(const QVariant &argument); -}; - - -#endif // DAPCOMMAND_H diff --git a/libKelvinDashboardCommon/DapLocalClient.cpp b/libKelvinDashboardCommon/DapLocalClient.cpp deleted file mode 100644 index d6c16f84bde40d48859b16c271ce64f802888801..0000000000000000000000000000000000000000 --- a/libKelvinDashboardCommon/DapLocalClient.cpp +++ /dev/null @@ -1,77 +0,0 @@ -#include "DapLocalClient.h" - -/// Redefined Ñonstructor. -/// @param nameConnect Name of the connection. Default is empty. -/// @param parent The default parent is empty. -DapLocalClient::DapLocalClient(const QString &nameConnect, QObject *parent) : QLocalSocket(parent) -{ - Q_UNUSED(nameConnect); - - // Initializing fields - m_nameConnect = nameConnect; - - // A signal-slot connection that starts receiving a command when the socket is - // switched to readyRead mode - connect(this, &DapLocalClient::readyRead, this, &DapLocalClient::recieveCommand); -} - -/// Get the name of the connection. -/// @return Name of the connection. -const QString &DapLocalClient::getNameConnect() const -{ - return m_nameConnect; -} - -/// Set connection name. -/// @param nameConnect Name of the connection. -void DapLocalClient::setNameConnect(const QString &nameConnect) -{ - m_nameConnect = nameConnect; -} - -/// Connect to the server. -/// @details As the name of the connection, the NameConnect property -/// initialized through the constructor will be used. -void DapLocalClient::connectToServer() -{ - QLocalSocket::connectToServer(getNameConnect()); -} - -/// Connect to the server. -/// @details The name of the connection sets the parameter. -/// @param nameConnect Name of the connection. -void DapLocalClient::connectToServer(const QString &nameConnect) -{ - QLocalSocket::connectToServer(nameConnect); -} - -/// Get the command. -/// @details When the command is received, the commandRecieved signal is emitted -/// @return The received command. -const DapCommand &DapLocalClient::recieveCommand() -{ - DapCommand *command = new DapCommand(this->readAll()); - emit commandRecieved(*command); - return *command; -} - -/// Set command. -/// @details If the command is successfully sent, the commandSent signal -/// is emitted. -/// @param command Submitted command. -/// @return Returns true if the command was successfully sent, -/// false - the command was not sent. -bool DapLocalClient::sendCommand(const DapCommand &command) -{ - if(this->isWritable()) - { - this->write(const_cast<DapCommand &>(command).serialize()); - - if(this->flush()) - { - emit commandSent(command); - return true; - } - } - return false; -} diff --git a/libKelvinDashboardCommon/DapLocalClient.h b/libKelvinDashboardCommon/DapLocalClient.h deleted file mode 100644 index 10989f6405f4e6fa2a09f3ef9aa1c211687c0d5a..0000000000000000000000000000000000000000 --- a/libKelvinDashboardCommon/DapLocalClient.h +++ /dev/null @@ -1,82 +0,0 @@ -/**************************************************************************** -** -** This file is part of the libKelvinDashboardClient library. -** -** The class extends the QLocalSocket to implement the client side. It is -** intended for transfer of commands of type Dapsomommand between the client -** and the server. -** -****************************************************************************/ - -#ifndef DAPLOCALCLIENT_H -#define DAPLOCALCLIENT_H - -#include <QObject> -#include <QLocalSocket> - -#include "DapCommand.h" - -class DapLocalClient : public QLocalSocket -{ - Q_OBJECT - -protected: - /// Name of client-server connection. - QString m_nameConnect; - -public: - /// Redefined Ñonstructor. - /// @param nameConnect Name of the connection. Default is empty. - /// @param parent The default parent is empty. - explicit DapLocalClient(const QString& nameConnect = "", QObject *parent = nullptr); - - ///******************************************** - /// Properties - /// ******************************************* - - /// Name of client-server connection. - Q_PROPERTY(QString NameConnect MEMBER m_nameConnect READ getNameConnect WRITE setNameConnect NOTIFY nameConnectChanged) - - ///******************************************** - /// Interface - /// ******************************************* - - /// Get the name of the connection. - /// @return Name of the connection. - const QString& getNameConnect() const; - /// Set connection name. - /// @param nameConnect Name of the connection. - void setNameConnect(const QString &nameConnect); - /// Connect to the server. - /// @details As the name of the connection, the NameConnect property - /// initialized through the constructor will be used. - void connectToServer(); - /// Connect to the server. - /// @details The name of the connection sets the parameter. - /// @param nameConnect Name of the connection. - void connectToServer(const QString &nameConnect); - /// Get the command. - /// @details When the command is received, the commandRecieved signal is emitted - /// @return The received command. - const DapCommand& recieveCommand(); - /// Set command. - /// @details If the command is successfully sent, the commandSent signal - /// is emitted. - /// @param command Submitted command. - /// @return Returns true if the command was successfully sent, - /// false - the command was not sent. - bool sendCommand(const DapCommand &command); - -signals: - /// The signal is emitted when the NameConnect property changes. - /// @param nameConnect Name of the connection. - void nameConnectChanged(const QString &nameConnect); - /// The signal is emitted when a command is received. - /// @param command The received command. - void commandRecieved(const DapCommand &command); - /// The signal is emitted when sending a command. - /// @param command Submitted command. - void commandSent(const DapCommand &command); -}; - -#endif // DAPLOCALCLIENT_H diff --git a/libKelvinDashboardCommon/DapLocalServer.cpp b/libKelvinDashboardCommon/DapLocalServer.cpp deleted file mode 100644 index febc832eb74815b2411f26609088a3f9396da438..0000000000000000000000000000000000000000 --- a/libKelvinDashboardCommon/DapLocalServer.cpp +++ /dev/null @@ -1,107 +0,0 @@ -#include "DapLocalServer.h" - -/// Redefined Ñonstructor. -/// @param nameConnect Name of the connection. Default is empty. -/// @param parent The default parent is empty. -DapLocalServer::DapLocalServer(const QString &nameConnect, QObject *parent) : QLocalServer(parent) -{ - Q_UNUSED(nameConnect); - - // Initializing fields - m_nameConnect = nameConnect; -} - -/// Get the name of the connection. -/// @return Name of the connection. -const QString &DapLocalServer::getNameConnect() const -{ - return m_nameConnect; -} - -/// Set connection name. -/// @param nameConnect Name of the connection. -void DapLocalServer::setNameConnect(const QString &nameConnect) -{ - m_nameConnect = nameConnect; -} - -/// Connect with client. -/// @details As the name of the connection, the NameConnect property -/// initialized through the constructor will be used. -void DapLocalServer::connectWithClient() -{ - connectWithClient(getNameConnect()); -} - -/// Connect with client. -/// @details The name of the connection sets the parameter. -/// @param nameConnect Name of the connection. -void DapLocalServer::connectWithClient(const QString &nameConnect) -{ - this->setSocketOptions(QLocalServer::WorldAccessOption); - - if(this->listen(nameConnect)) - { - connect(this, &QLocalServer::newConnection, [=] - { - m_client = this->nextPendingConnection(); - { - qDebug() << "Cient connected"; - connect(m_client, &QLocalSocket::readyRead, [=] - { - QByteArray clientCommand = m_client->readAll(); - - while (true) { - if(clientCommand.isEmpty()) - break; - - recieveCommand(clientCommand); - break; - } - }); - } - }); - } -} - -/// Get the command. -/// @details When the command is received, the commandRecieved signal is emitted. -/// @param byteArrayCommand Accepted command in byte representation. -/// @return The received command. -const DapCommand& DapLocalServer::recieveCommand(const QByteArray &byteArrayCommand) -{ - DapCommand *command = new DapCommand(byteArrayCommand); - emit commandRecieved(*command); - return *command; -} - -/// Set command. -/// @details If the command is successfully sent, the commandSent signal -/// is emitted. -/// @param command Submitted command. -/// @return Returns true if the command was successfully sent, -/// false - the command was not sent. -bool DapLocalServer::sendCommand(const DapCommand &command) -{ - if(m_client->isWritable()) - { - qDebug() << "Send command by: " << const_cast<DapCommand &>(command).serialize(); - qDebug() << "Send command: " << command.getArguments().at(0); - m_client->write(const_cast<DapCommand &>(command).serialize()); - - if(m_client->flush()) - { - emit commandSent(command); - return true; - } - } - return false; -} - -/// Check the existence of the connected client. -/// @return Returns three if the client is connected to the server, -/// false is not connected. -bool DapLocalServer::isClientExist() -{ - return m_client ? true : false; -} diff --git a/libKelvinDashboardCommon/DapLocalServer.h b/libKelvinDashboardCommon/DapLocalServer.h deleted file mode 100644 index 33a9349615702a46292832f164a33ac59412d722..0000000000000000000000000000000000000000 --- a/libKelvinDashboardCommon/DapLocalServer.h +++ /dev/null @@ -1,93 +0,0 @@ -/**************************************************************************** -** -** This file is part of the libKelvinDashboardClient library. -** -** The class extends QLocalServer to implement the server side. It is -** intended for transfer of commands of type Dapsomommand between the server -** and the client. -** -****************************************************************************/ - -#ifndef DAPLOCALSERVER_H -#define DAPLOCALSERVER_H - -#include <QObject> -#include <QString> -#include <QLocalServer> -#include <QLocalSocket> -#include <QDebug> - -#include "DapCommand.h" - -class DapLocalServer : public QLocalServer -{ - Q_OBJECT - -protected: - /// Name of client-server connection. - QString m_nameConnect; - /// Client connected to the server. - QLocalSocket *m_client {nullptr}; - -public: - /// Redefined Ñonstructor. - /// @param nameConnect Name of the connection. Default is empty. - /// @param parent The default parent is empty. - explicit DapLocalServer(const QString& nameConnect, QObject *parent = nullptr); - - ///******************************************** - /// Properties - /// ******************************************* - - /// Name of client-server connection. - Q_PROPERTY(QString NameConnect MEMBER m_nameConnect READ getNameConnect WRITE setNameConnect NOTIFY nameConnectChanged) - - - ///******************************************** - /// Interface - /// ******************************************* - - /// Get the name of the connection. - /// @return Name of the connection. - const QString& getNameConnect() const; - /// Set connection name. - /// @param nameConnect Name of the connection. - void setNameConnect(const QString &nameConnect); - /// Connect with client. - /// @details As the name of the connection, the NameConnect property - /// initialized through the constructor will be used. - void connectWithClient(); - /// Connect with client. - /// @details The name of the connection sets the parameter. - /// @param nameConnect Name of the connection. - void connectWithClient(const QString &nameConnect); - /// Get the command. - /// @details When the command is received, the commandRecieved signal is emitted. - /// @param byteArrayCommand Accepted command in byte representation. - /// @return The received command. - const DapCommand &recieveCommand(const QByteArray& byteArrayCommand); - /// Set command. - /// @details If the command is successfully sent, the commandSent signal - /// is emitted. - /// @param command Submitted command. - /// @return Returns true if the command was successfully sent, - /// false - the command was not sent. - bool sendCommand(const DapCommand &command); - /// Check the existence of the connected client. - /// @return Returns three if the client is connected to the server, - /// false is not connected. - Q_INVOKABLE bool isClientExist(); - -signals: - /// The signal is emitted when the NameConnect property changes. - /// @param nameConnect Name of the connection. - void nameConnectChanged(const QString &nameConnect); - /// The signal is emitted when a command is received. - /// @param command The received command. - void commandRecieved(const DapCommand &command); - /// The signal is emitted when sending a command. - /// @param command Submitted command. - void commandSent(const DapCommand &command); -}; - -#endif // DAPLOCALSERVER_H diff --git a/libKelvinDashboardCommon/libKelvinDashboardCommon.pri b/libKelvinDashboardCommon/libKelvinDashboardCommon.pri old mode 100644 new mode 100755 index 7c81a6284c6ab35731f946a61fbe872fa1432c93..3dfc99342e9488d181c765703c467353d6512265 --- a/libKelvinDashboardCommon/libKelvinDashboardCommon.pri +++ b/libKelvinDashboardCommon/libKelvinDashboardCommon.pri @@ -12,17 +12,11 @@ QT += quick quickwidgets SOURCES +=\ - $$PWD/DapCommand.cpp \ - $$PWD/DapLocalServer.cpp \ - $$PWD/DapLocalClient.cpp \ $$PWD/DapHalper.cpp \ $$PWD/DapSettings.cpp \ $$PWD/DapSettingsCipher.cpp HEADERS +=\ - $$PWD/DapCommand.h \ - $$PWD/DapLocalServer.h \ - $$PWD/DapLocalClient.h \ $$PWD/DapHalper.h \ $$PWD/DapSettings.h \ $$PWD/DapSettingsCipher.h