Add proxy monitor

This commit is contained in:
Simon Stürz 2018-08-15 23:12:10 +02:00
parent 84b964f501
commit 3625e6e46c
11 changed files with 204 additions and 9 deletions

View File

@ -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;

View File

@ -7,6 +7,7 @@
#include <QSslConfiguration>
#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);

View File

@ -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

View File

@ -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")

View File

@ -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

View File

@ -0,0 +1,96 @@
#include "monitorserver.h"
#include "loggingcategories.h"
#include <QJsonDocument>
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<QLocalSocket *>(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;
}
}

View File

@ -0,0 +1,38 @@
#ifndef MONITORSERVER_H
#define MONITORSERVER_H
#include <QTimer>
#include <QObject>
#include <QLocalServer>
#include <QLocalSocket>
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<QLocalSocket *> 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

36
monitor/main.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <QUrl>
#include <QCoreApplication>
#include <QLoggingCategory>
#include <QCommandLineParser>
#include <QCommandLineOption>
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 <simon.stuerz@guh.io>\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();
}

12
monitor/monitor.pro Normal file
View File

@ -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

View File

@ -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}")

View File

@ -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;