diff --git a/libnymea-remoteproxy/engine.cpp b/libnymea-remoteproxy/engine.cpp index d19e6af..7aaab15 100644 --- a/libnymea-remoteproxy/engine.cpp +++ b/libnymea-remoteproxy/engine.cpp @@ -59,6 +59,9 @@ void Engine::start(ProxyConfiguration *configuration) qCDebug(dcEngine()) << "Starting proxy server"; m_proxyServer->startServer(); + m_monitorServer = new MonitorServer("/tmp/nymea-remoteproxy-monitor.socket", this); + m_monitorServer->startServer(); + // Set tunning true in the next event loop QMetaObject::invokeMethod(this, QString("setRunning").toLatin1().data(), Qt::QueuedConnection, Q_ARG(bool, true)); } @@ -95,12 +98,9 @@ void Engine::setAuthenticator(Authenticator *authenticator) if (m_authenticator) { qCDebug(dcEngine()) << "There is already an authenticator registered. Unregister default authenticator"; m_authenticator = nullptr; - // FIXME: do unconnect } m_authenticator = authenticator; - - // FIXME: connect } void Engine::setDeveloperModeEnabled(bool enabled) @@ -136,6 +136,12 @@ Engine::~Engine() void Engine::clean() { + if (m_monitorServer) { + m_monitorServer->startServer(); + delete m_monitorServer; + m_monitorServer = nullptr; + } + if (m_proxyServer) { m_proxyServer->stopServer(); delete m_proxyServer; diff --git a/libnymea-remoteproxy/engine.h b/libnymea-remoteproxy/engine.h index fed4e98..25354f5 100644 --- a/libnymea-remoteproxy/engine.h +++ b/libnymea-remoteproxy/engine.h @@ -7,6 +7,7 @@ #include #include "proxyserver.h" +#include "monitorserver.h" #include "websocketserver.h" #include "proxyconfiguration.h" #include "authentication/authenticator.h" @@ -49,6 +50,7 @@ private: Authenticator *m_authenticator = nullptr; ProxyServer *m_proxyServer = nullptr; WebSocketServer *m_webSocketServer = nullptr; + MonitorServer *m_monitorServer = nullptr; signals: void runningChanged(bool running); diff --git a/libnymea-remoteproxy/libnymea-remoteproxy.pro b/libnymea-remoteproxy/libnymea-remoteproxy.pro index 462cc69..125d5b3 100644 --- a/libnymea-remoteproxy/libnymea-remoteproxy.pro +++ b/libnymea-remoteproxy/libnymea-remoteproxy.pro @@ -27,7 +27,8 @@ HEADERS += \ proxyconfiguration.h \ tunnelconnection.h \ authentication/authenticationprocess.h \ - authentication/dummyauthenticator.h + authentication/dummyauthenticator.h \ + monitorserver.h SOURCES += \ engine.cpp \ @@ -47,7 +48,8 @@ SOURCES += \ proxyconfiguration.cpp \ tunnelconnection.cpp \ authentication/authenticationprocess.cpp \ - authentication/dummyauthenticator.cpp + authentication/dummyauthenticator.cpp \ + monitorserver.cpp # install header file with relative subdirectory diff --git a/libnymea-remoteproxy/loggingcategories.cpp b/libnymea-remoteproxy/loggingcategories.cpp index 60acc03..8da1417 100644 --- a/libnymea-remoteproxy/loggingcategories.cpp +++ b/libnymea-remoteproxy/loggingcategories.cpp @@ -10,4 +10,5 @@ Q_LOGGING_CATEGORY(dcAuthentication, "Authentication") Q_LOGGING_CATEGORY(dcAuthenticationProcess, "AuthenticationProcess") Q_LOGGING_CATEGORY(dcProxyServer, "ProxyServer") Q_LOGGING_CATEGORY(dcProxyServerTraffic, "ProxyServerTraffic") +Q_LOGGING_CATEGORY(dcMonitorServer, "MonitorServer") diff --git a/libnymea-remoteproxy/loggingcategories.h b/libnymea-remoteproxy/loggingcategories.h index b1a57ee..4528953 100644 --- a/libnymea-remoteproxy/loggingcategories.h +++ b/libnymea-remoteproxy/loggingcategories.h @@ -14,5 +14,6 @@ Q_DECLARE_LOGGING_CATEGORY(dcAuthentication) Q_DECLARE_LOGGING_CATEGORY(dcAuthenticationProcess) Q_DECLARE_LOGGING_CATEGORY(dcProxyServer) Q_DECLARE_LOGGING_CATEGORY(dcProxyServerTraffic) +Q_DECLARE_LOGGING_CATEGORY(dcMonitorServer) #endif // LOGGINGCATEGORIES_H diff --git a/libnymea-remoteproxy/monitorserver.cpp b/libnymea-remoteproxy/monitorserver.cpp new file mode 100644 index 0000000..5ee58cd --- /dev/null +++ b/libnymea-remoteproxy/monitorserver.cpp @@ -0,0 +1,96 @@ +#include "monitorserver.h" +#include "loggingcategories.h" + +#include + +namespace remoteproxy { + +MonitorServer::MonitorServer(const QString &serverName, QObject *parent) : + QObject(parent), + m_serverName(serverName) +{ + m_timer = new QTimer(this); + m_timer->setInterval(1000); + m_timer->setSingleShot(false); + connect(m_timer, &QTimer::timeout, this, &MonitorServer::onTimeout); +} + +QVariantMap MonitorServer::createMonitorData() +{ + QVariantMap monitorData; + + return monitorData; +} + +void MonitorServer::sendMonitorData(QLocalSocket *clientConnection, const QVariantMap &dataMap) +{ + clientConnection->write(QJsonDocument::fromVariant(dataMap).toJson(QJsonDocument::Compact) + '\n'); + clientConnection->flush(); +} + +void MonitorServer::onTimeout() +{ + // If no client left or no server running, stop the timer + if (m_clients.isEmpty() || !m_server) + m_timer->stop(); + + QVariantMap dataMap = createMonitorData(); + + // Send each monitor the current data + foreach (QLocalSocket *clientConnection, m_clients) { + sendMonitorData(clientConnection, dataMap); + } +} + +void MonitorServer::onMonitorConnected() +{ + QLocalSocket *clientConnection = m_server->nextPendingConnection(); + connect(clientConnection, &QLocalSocket::disconnected, this, &MonitorServer::onMonitorDisconnected); + connect(clientConnection, &QLocalSocket::readyRead, this, &MonitorServer::onMonitorDisconnected); + m_clients.append(clientConnection); + + if (!m_timer->isActive()) { + // Send the data right the way + onTimeout(); + m_timer->start(); + } +} + +void MonitorServer::onMonitorDisconnected() +{ + qCDebug(dcMonitorServer()) << "Monitor disconnected."; + QLocalSocket *clientConnection = static_cast(sender()); + m_clients.removeAll(clientConnection); + clientConnection->deleteLater(); +} + +void MonitorServer::startServer() +{ + m_server = new QLocalServer(this); + if (!m_server->listen(m_serverName)) { + qCWarning(dcMonitorServer()) << "Could not start local server for monitor on" << m_serverName; + delete m_server; + m_server = nullptr; + return; + } + + connect(m_server, &QLocalServer::newConnection, this, &MonitorServer::onMonitorConnected); + qCDebug(dcMonitorServer()) << "Started successfully on" << m_serverName; +} + +void MonitorServer::stopServer() +{ + if (!m_server) + return; + + qCDebug(dcMonitorServer()) << "Stopping server" << m_serverName; + foreach (QLocalSocket *clientConnection, m_clients) { + clientConnection->close(); + } + + m_server->close(); + m_server->deleteLater(); + m_server = nullptr; +} + +} diff --git a/libnymea-remoteproxy/monitorserver.h b/libnymea-remoteproxy/monitorserver.h new file mode 100644 index 0000000..6db6ff4 --- /dev/null +++ b/libnymea-remoteproxy/monitorserver.h @@ -0,0 +1,38 @@ +#ifndef MONITORSERVER_H +#define MONITORSERVER_H + +#include +#include +#include +#include + +namespace remoteproxy { + +class MonitorServer : public QObject +{ + Q_OBJECT +public: + explicit MonitorServer(const QString &serverName, QObject *parent = nullptr); + +private: + QString m_serverName; + QLocalServer *m_server = nullptr; + QTimer *m_timer = nullptr; + QList m_clients; + + QVariantMap createMonitorData(); + void sendMonitorData(QLocalSocket *clientConnection, const QVariantMap &dataMap); + +private slots: + void onTimeout(); + void onMonitorConnected(); + void onMonitorDisconnected(); + +public slots: + void startServer(); + void stopServer(); +}; + +} + +#endif // MONITORSERVER_H diff --git a/monitor/main.cpp b/monitor/main.cpp new file mode 100644 index 0000000..4012f9b --- /dev/null +++ b/monitor/main.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + + QCoreApplication application(argc, argv); + application.setApplicationName(SERVER_NAME_STRING); + application.setOrganizationName("nymea"); + application.setApplicationVersion(SERVER_VERSION_STRING); + + QCommandLineParser parser; + parser.addHelpOption(); + parser.addVersionOption(); + parser.setApplicationDescription(QString("\nThe nymea remote proxy server client application. This client allowes to test " + "a server application as client perspective.\n\n" + "Server version: %1\n" + "API version: %2\n\n" + "Copyright %3 2018 Simon Stürz \n") + .arg(SERVER_VERSION_STRING) + .arg(API_VERSION_STRING) + .arg(QChar(0xA9))); + + + + + QCommandLineOption tokenOption(QStringList() << "s" << "socket", "The AWS token for authentication.", "socket"); + parser.addOption(tokenOption); + + parser.process(application); + + return application.exec(); +} diff --git a/monitor/monitor.pro b/monitor/monitor.pro new file mode 100644 index 0000000..128c63c --- /dev/null +++ b/monitor/monitor.pro @@ -0,0 +1,12 @@ +include(../nymea-remoteproxy.pri) + +TARGET = nymea-remoteproxy-monitor +TEMPLATE = app + +SOURCES += main.cpp + +target.path = /usr/bin +INSTALLS += target + +HEADERS += \ + proxyclient.h diff --git a/nymea-remoteproxy.pro b/nymea-remoteproxy.pro index 3b3fc8b..2857702 100644 --- a/nymea-remoteproxy.pro +++ b/nymea-remoteproxy.pro @@ -1,11 +1,11 @@ include(nymea-remoteproxy.pri) TEMPLATE=subdirs -SUBDIRS += server client libnymea-remoteproxy libnymea-remoteproxyclient tests +SUBDIRS += server client monitor libnymea-remoteproxy libnymea-remoteproxyclient tests server.depends = libnymea-remoteproxy -tests.depends = libnymea-remoteproxy libnymea-remoteproxyclient client.depends = libnymea-remoteproxyclient +tests.depends = libnymea-remoteproxy libnymea-remoteproxyclient message("----------------------------------------------------------") message("Building nymea-remoteproxy $${SERVER_VERSION}") diff --git a/server/main.cpp b/server/main.cpp index a8880bb..0cfe70f 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -89,7 +89,7 @@ int main(int argc, char *argv[]) QCoreApplication application(argc, argv); application.setApplicationName(SERVER_NAME_STRING); - application.setOrganizationName("guh"); + application.setOrganizationName("nymea"); application.setApplicationVersion(SERVER_VERSION_STRING); s_loggingFilters.insert("Application", true); @@ -98,6 +98,7 @@ int main(int argc, char *argv[]) s_loggingFilters.insert("WebSocketServer", true); s_loggingFilters.insert("Authentication", true); s_loggingFilters.insert("ProxyServer", true); + s_loggingFilters.insert("MonitorServer", true); // Only with verbose enabled s_loggingFilters.insert("JsonRpcTraffic", false); @@ -105,7 +106,7 @@ int main(int argc, char *argv[]) s_loggingFilters.insert("AuthenticationProcess", false); s_loggingFilters.insert("WebSocketServerTraffic", false); - QString configFile = QStandardPaths::standardLocations(QStandardPaths::ConfigLocation).first() + "/nymea/nymea-remoteproxy.conf"; + QString configFile = "/etc/nymea/nymea-remoteproxy.conf"; // command line parser QCommandLineParser parser;