From 150f1fef3f91228d1c0bcc6c6cafe790ab731b12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Sun, 12 Feb 2023 23:36:32 +0100 Subject: [PATCH] Add transports and counts for the monitor --- libnymea-remoteproxy/engine.cpp | 5 +++++ .../server/tcpsocketserver.cpp | 10 ++++++++++ libnymea-remoteproxy/server/tcpsocketserver.h | 3 +++ .../server/transportinterface.h | 3 +++ .../server/unixsocketserver.cpp | 10 ++++++++++ .../server/unixsocketserver.h | 3 +++ .../server/websocketserver.cpp | 10 ++++++++++ libnymea-remoteproxy/server/websocketserver.h | 3 +++ .../tunnelproxy/tunnelproxyserver.cpp | 7 ++++++- monitor/main.cpp | 5 ++++- monitor/monitor.cpp | 13 +++++++++---- monitor/monitor.h | 3 ++- monitor/monitorclient.cpp | 10 +++++++--- monitor/monitorclient.h | 3 ++- monitor/noninteractivemonitor.cpp | 19 +++++++++++++------ 15 files changed, 90 insertions(+), 17 deletions(-) diff --git a/libnymea-remoteproxy/engine.cpp b/libnymea-remoteproxy/engine.cpp index 19fd336..cea1334 100644 --- a/libnymea-remoteproxy/engine.cpp +++ b/libnymea-remoteproxy/engine.cpp @@ -294,6 +294,11 @@ void Engine::onTimerTick() serverStatistics.value("proxyStatistic").toMap().value("troughput").toInt()); m_currentTimeCounter = 0; + + + if (m_tunnelProxyServer) { + m_tunnelProxyServer->tick(); + } } } diff --git a/libnymea-remoteproxy/server/tcpsocketserver.cpp b/libnymea-remoteproxy/server/tcpsocketserver.cpp index a7af670..19db242 100644 --- a/libnymea-remoteproxy/server/tcpsocketserver.cpp +++ b/libnymea-remoteproxy/server/tcpsocketserver.cpp @@ -70,6 +70,16 @@ void TcpSocketServer::killClientConnection(const QUuid &clientId, const QString client->close(); } +QString TcpSocketServer::name() const +{ + return "TcpSocketServer"; +} + +uint TcpSocketServer::connectionsCount() const +{ + return m_clientList.count(); +} + bool TcpSocketServer::running() const { if (!m_server) diff --git a/libnymea-remoteproxy/server/tcpsocketserver.h b/libnymea-remoteproxy/server/tcpsocketserver.h index 2f4e684..0a239cc 100644 --- a/libnymea-remoteproxy/server/tcpsocketserver.h +++ b/libnymea-remoteproxy/server/tcpsocketserver.h @@ -75,6 +75,9 @@ public: void sendData(const QUuid &clientId, const QByteArray &data) override; void killClientConnection(const QUuid &clientId, const QString &killReason) override; + QString name() const override; + uint connectionsCount() const override; + bool running() const override; private: diff --git a/libnymea-remoteproxy/server/transportinterface.h b/libnymea-remoteproxy/server/transportinterface.h index b507fcd..693f958 100644 --- a/libnymea-remoteproxy/server/transportinterface.h +++ b/libnymea-remoteproxy/server/transportinterface.h @@ -46,6 +46,9 @@ public: virtual void sendData(const QUuid &clientId, const QByteArray &data) = 0; virtual void killClientConnection(const QUuid &clientId, const QString &killReason) = 0; + virtual QString name() const = 0; + virtual uint connectionsCount() const = 0; + QUrl serverUrl() const; void setServerUrl(const QUrl &serverUrl); diff --git a/libnymea-remoteproxy/server/unixsocketserver.cpp b/libnymea-remoteproxy/server/unixsocketserver.cpp index 588523f..84be632 100644 --- a/libnymea-remoteproxy/server/unixsocketserver.cpp +++ b/libnymea-remoteproxy/server/unixsocketserver.cpp @@ -73,6 +73,16 @@ void UnixSocketServer::killClientConnection(const QUuid &clientId, const QString } } +QString UnixSocketServer::name() const +{ + return "UnixSocketServer"; +} + +uint UnixSocketServer::connectionsCount() const +{ + return m_clientList.count(); +} + bool UnixSocketServer::running() const { if (!m_server) diff --git a/libnymea-remoteproxy/server/unixsocketserver.h b/libnymea-remoteproxy/server/unixsocketserver.h index 5dc672d..97e91ba 100644 --- a/libnymea-remoteproxy/server/unixsocketserver.h +++ b/libnymea-remoteproxy/server/unixsocketserver.h @@ -47,6 +47,9 @@ public: void sendData(const QUuid &clientId, const QByteArray &data) override; void killClientConnection(const QUuid &clientId, const QString &killReason) override; + QString name() const override; + uint connectionsCount() const override; + bool running() const override; public slots: diff --git a/libnymea-remoteproxy/server/websocketserver.cpp b/libnymea-remoteproxy/server/websocketserver.cpp index 63ab552..18393d9 100644 --- a/libnymea-remoteproxy/server/websocketserver.cpp +++ b/libnymea-remoteproxy/server/websocketserver.cpp @@ -81,6 +81,16 @@ void WebSocketServer::killClientConnection(const QUuid &clientId, const QString client->close(QWebSocketProtocol::CloseCodeBadOperation, killReason); } +QString WebSocketServer::name() const +{ + return "WebSocketServer"; +} + +uint WebSocketServer::connectionsCount() const +{ + return m_clientList.count(); +} + void WebSocketServer::onClientConnected() { // Got a new client connected diff --git a/libnymea-remoteproxy/server/websocketserver.h b/libnymea-remoteproxy/server/websocketserver.h index c6d172d..01872c7 100644 --- a/libnymea-remoteproxy/server/websocketserver.h +++ b/libnymea-remoteproxy/server/websocketserver.h @@ -53,6 +53,9 @@ public: void sendData(const QUuid &clientId, const QByteArray &data) override; void killClientConnection(const QUuid &clientId, const QString &killReason) override; + QString name() const override; + uint connectionsCount() const override; + private: QWebSocketServer *m_server = nullptr; bool m_sslEnabled; diff --git a/libnymea-remoteproxy/tunnelproxy/tunnelproxyserver.cpp b/libnymea-remoteproxy/tunnelproxy/tunnelproxyserver.cpp index 518931f..735942b 100644 --- a/libnymea-remoteproxy/tunnelproxy/tunnelproxyserver.cpp +++ b/libnymea-remoteproxy/tunnelproxy/tunnelproxyserver.cpp @@ -201,6 +201,11 @@ QVariantMap TunnelProxyServer::currentStatistics() statisticsMap.insert("totalClientCount", m_proxyClients.count()); statisticsMap.insert("serverConnectionsCount", m_tunnelProxyServerConnections.count()); statisticsMap.insert("clientConnectionsCount", m_tunnelProxyClientConnections.count()); + QVariantMap transports; + foreach (TransportInterface *transportInterface, m_transportInterfaces) { + transports.insert(transportInterface->name(), transportInterface->connectionsCount()); + } + statisticsMap.insert("transports", transports); statisticsMap.insert("troughput", m_troughput); QVariantList tunnelConnections; @@ -267,8 +272,8 @@ void TunnelProxyServer::tick() void TunnelProxyServer::onClientConnected(const QUuid &clientId, const QHostAddress &address) { TransportInterface *interface = static_cast(sender()); - qCDebug(dcTunnelProxyServer()) << "New client connected" << interface->serverName() << clientId.toString() << address.toString(); + TunnelProxyClient *tunnelProxyClient = new TunnelProxyClient(interface, clientId, address, this); m_proxyClients.insert(clientId, tunnelProxyClient); m_jsonRpcServer->registerClient(tunnelProxyClient); diff --git a/monitor/main.cpp b/monitor/main.cpp index a0ad617..6d311b6 100644 --- a/monitor/main.cpp +++ b/monitor/main.cpp @@ -64,6 +64,9 @@ int main(int argc, char *argv[]) QCommandLineOption noninteractiveOption(QStringList() << "n" << "non-interactive", "Connect to the server, list all data and close the connection. This works only for the tunnelproxy."); parser.addOption(noninteractiveOption); + QCommandLineOption jsonOption(QStringList() << "j" << "json", "Connect to the server and print the raw json data."); + parser.addOption(jsonOption); + parser.process(application); // Check socket file @@ -82,7 +85,7 @@ int main(int argc, char *argv[]) NonInteractiveMonitor *monitor = new NonInteractiveMonitor(parser.value(socketOption)); Q_UNUSED(monitor); } else { - Monitor *monitor = new Monitor(parser.value(socketOption)); + Monitor *monitor = new Monitor(parser.value(socketOption), parser.isSet(jsonOption)); Q_UNUSED(monitor); } diff --git a/monitor/monitor.cpp b/monitor/monitor.cpp index ee006e5..58cf366 100644 --- a/monitor/monitor.cpp +++ b/monitor/monitor.cpp @@ -26,10 +26,13 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include "monitor.h" +#include -Monitor::Monitor(const QString &serverName, QObject *parent) : QObject(parent) +Monitor::Monitor(const QString &serverName, bool jsonMode, QObject *parent) : + QObject(parent), + m_jsonMode(jsonMode) { - m_monitorClient = new MonitorClient(serverName, this); + m_monitorClient = new MonitorClient(serverName, jsonMode, this); connect(m_monitorClient, &MonitorClient::connected, this, &Monitor::onConnected); connect(m_monitorClient, &MonitorClient::disconnected, this, &Monitor::onDisconnected); @@ -38,8 +41,10 @@ Monitor::Monitor(const QString &serverName, QObject *parent) : QObject(parent) void Monitor::onConnected() { - m_terminal = new TerminalWindow(this); - connect(m_monitorClient, &MonitorClient::dataReady, m_terminal, &TerminalWindow::refreshWindow); + if (!m_jsonMode) { + m_terminal = new TerminalWindow(this); + connect(m_monitorClient, &MonitorClient::dataReady, m_terminal, &TerminalWindow::refreshWindow); + } } void Monitor::onDisconnected() diff --git a/monitor/monitor.h b/monitor/monitor.h index 48dd7ff..6bd199c 100644 --- a/monitor/monitor.h +++ b/monitor/monitor.h @@ -37,11 +37,12 @@ class Monitor : public QObject { Q_OBJECT public: - explicit Monitor(const QString &serverName, QObject *parent = nullptr); + explicit Monitor(const QString &serverName, bool jsonMode, QObject *parent = nullptr); private: TerminalWindow *m_terminal = nullptr; MonitorClient *m_monitorClient = nullptr; + bool m_jsonMode = false; private slots: void onConnected(); diff --git a/monitor/monitorclient.cpp b/monitor/monitorclient.cpp index aa4c9aa..3b9ab8d 100644 --- a/monitor/monitorclient.cpp +++ b/monitor/monitorclient.cpp @@ -29,9 +29,10 @@ #include -MonitorClient::MonitorClient(const QString &serverName, QObject *parent) : +MonitorClient::MonitorClient(const QString &serverName, bool jsonMode, QObject *parent) : QObject(parent), - m_serverName(serverName) + m_serverName(serverName), + m_jsonMode(jsonMode) { m_socket = new QLocalSocket(this); @@ -65,7 +66,10 @@ void MonitorClient::onReadyRead() return; } - //qDebug() << qUtf8Printable(jsonDoc.toJson(QJsonDocument::Indented)); + if (m_jsonMode) { + qDebug() << qUtf8Printable(jsonDoc.toJson(QJsonDocument::Indented)); + return; + } QVariantMap dataMap = jsonDoc.toVariant().toMap(); emit dataReady(dataMap); diff --git a/monitor/monitorclient.h b/monitor/monitorclient.h index 82e638e..07c2d46 100644 --- a/monitor/monitorclient.h +++ b/monitor/monitorclient.h @@ -37,11 +37,12 @@ class MonitorClient : public QObject { Q_OBJECT public: - explicit MonitorClient(const QString &serverName, QObject *parent = nullptr); + explicit MonitorClient(const QString &serverName, bool jsonMode, QObject *parent = nullptr); private: QString m_serverName; QLocalSocket *m_socket = nullptr; + bool m_jsonMode = false; signals: void connected(); diff --git a/monitor/noninteractivemonitor.cpp b/monitor/noninteractivemonitor.cpp index 4766373..6d56743 100644 --- a/monitor/noninteractivemonitor.cpp +++ b/monitor/noninteractivemonitor.cpp @@ -33,7 +33,7 @@ NonInteractiveMonitor::NonInteractiveMonitor(const QString &serverName, QObject *parent) : QObject{parent} { - m_monitorClient = new MonitorClient(serverName, this); + m_monitorClient = new MonitorClient(serverName, false, this); connect(m_monitorClient, &MonitorClient::connected, this, &NonInteractiveMonitor::onConnected); m_monitorClient->connectMonitor(); @@ -42,17 +42,24 @@ NonInteractiveMonitor::NonInteractiveMonitor(const QString &serverName, QObject void NonInteractiveMonitor::onConnected() { connect(m_monitorClient, &MonitorClient::dataReady, this, [](const QVariantMap &dataMap){ + + QVariantMap tunnelProxyMap = dataMap.value("tunnelProxyStatistic").toMap(); + qInfo().noquote() << "---------------------------------------------------------------------"; qInfo().noquote() << "Server name:" << dataMap.value("serverName", "-").toString(); qInfo().noquote() << "Server version:" << dataMap.value("serverVersion", "-").toString(); qInfo().noquote() << "API version:" << dataMap.value("apiVersion", "-").toString(); - qInfo().noquote() << "Total client count:" << dataMap.value("tunnelProxyStatistic").toMap().value("totalClientCount", 0).toInt(); - qInfo().noquote() << "Server connections:" << dataMap.value("tunnelProxyStatistic").toMap().value("serverConnectionsCount", 0).toInt(); - qInfo().noquote() << "Client connections:" << dataMap.value("tunnelProxyStatistic").toMap().value("clientConnectionsCount", 0).toInt(); - qInfo().noquote() << "Data troughput:" << Utils::humanReadableTraffic(dataMap.value("tunnelProxyStatistic").toMap().value("troughput", 0).toInt()) + " / s"; + qInfo().noquote() << "Total client count:" << tunnelProxyMap.value("totalClientCount", 0).toInt(); + qInfo().noquote() << "Server connections:" << tunnelProxyMap.value("serverConnectionsCount", 0).toInt(); + qInfo().noquote() << "Client connections:" << tunnelProxyMap.value("clientConnectionsCount", 0).toInt(); + qInfo().noquote() << "Data troughput:" << Utils::humanReadableTraffic(tunnelProxyMap.value("troughput", 0).toInt()) + " / s"; + qInfo().noquote() << "---------------------------------------------------------------------"; + QVariantMap transportsMap = tunnelProxyMap.value("transports").toMap(); + foreach(const QString &transportInterface, transportsMap.keys()) { + qInfo().noquote().nospace() << "Connections on " << transportInterface << ": " << transportsMap.value(transportInterface).toInt(); + } qInfo().noquote() << "---------------------------------------------------------------------"; - QVariantMap tunnelProxyMap = dataMap.value("tunnelProxyStatistic").toMap(); foreach (const QVariant &serverVariant, tunnelProxyMap.value("tunnelConnections").toList()) { QVariantMap serverMap = serverVariant.toMap(); QVariantList clientList = serverMap.value("clientConnections").toList();