From 2d53d3dc839d325d473a3463c5a3be04281ef489 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Tue, 3 Oct 2017 11:13:30 +0200 Subject: [PATCH] fix houskeeping of ruleengine --- libguh-core/guhcore.cpp | 18 ++++++++---------- libguh-core/ruleengine.cpp | 4 ++-- libguh-core/tcpserver.cpp | 9 +++++++-- libguh-core/tcpserver.h | 2 ++ libguh-core/webserver.cpp | 15 ++++++++------- libguh-core/webserver.h | 2 ++ libguh-core/websocketserver.cpp | 19 ++++++++++--------- libguh-core/websocketserver.h | 2 ++ libguh/network/avahi/qtavahiservice.cpp | 4 +++- 9 files changed, 44 insertions(+), 31 deletions(-) diff --git a/libguh-core/guhcore.cpp b/libguh-core/guhcore.cpp index de5fd565..63337953 100644 --- a/libguh-core/guhcore.cpp +++ b/libguh-core/guhcore.cpp @@ -647,16 +647,14 @@ void GuhCore::deviceManagerLoaded() } } - // FIXME: this removes all timedescriptors - - // foreach (const DeviceId &deviceId, m_ruleEngine->devicesInRules()) { - // if (!m_deviceManager->findConfiguredDevice(deviceId)) { - // qCDebug(dcApplication()) << "Cleaning stale rule entries for device id" << deviceId; - // foreach (const RuleId &ruleId, m_ruleEngine->findRules(deviceId)) { - // m_ruleEngine->removeDeviceFromRule(ruleId, deviceId); - // } - // } - // } + foreach (const DeviceId &deviceId, m_ruleEngine->devicesInRules()) { + if (!m_deviceManager->findConfiguredDevice(deviceId)) { + qCDebug(dcApplication()) << "Cleaning stale rule entries for device id" << deviceId; + foreach (const RuleId &ruleId, m_ruleEngine->findRules(deviceId)) { + m_ruleEngine->removeDeviceFromRule(ruleId, deviceId); + } + } + } qCDebug(dcApplication()) << "Housekeeping done in" << startTime.msecsTo(QDateTime::currentDateTime()) << "ms."; } diff --git a/libguh-core/ruleengine.cpp b/libguh-core/ruleengine.cpp index f5c365fe..c9f1c8cc 100644 --- a/libguh-core/ruleengine.cpp +++ b/libguh-core/ruleengine.cpp @@ -900,12 +900,12 @@ QList RuleEngine::devicesInRules() const QList tmp; foreach (const Rule &rule, m_rules) { foreach (const EventDescriptor &descriptor, rule.eventDescriptors()) { - if (!tmp.contains(descriptor.deviceId())) { + if (!tmp.contains(descriptor.deviceId()) && !descriptor.deviceId().isNull()) { tmp.append(descriptor.deviceId()); } } foreach (const DeviceId &deviceId, rule.stateEvaluator().containedDevices()) { - if (!tmp.contains(deviceId)) { + if (!tmp.contains(deviceId) && !deviceId.isNull()) { tmp.append(deviceId); } } diff --git a/libguh-core/tcpserver.cpp b/libguh-core/tcpserver.cpp index da4602c4..84a2c1d3 100644 --- a/libguh-core/tcpserver.cpp +++ b/libguh-core/tcpserver.cpp @@ -57,10 +57,15 @@ TcpServer::TcpServer(const ServerConfiguration &configuration, const QSslConfigu /*! Destructor of this \l{TcpServer}. */ TcpServer::~TcpServer() { - qCDebug(dcApplication) << "Shutting down \"TCP Server\"" << QString("%1://%2:%3").arg((configuration().sslEnabled ? "guhs" : "guh")).arg(configuration().address.toString()).arg(configuration().port); + qCDebug(dcApplication) << "Shutting down \"TCP Server\"" << serverUrl().toString(); stopServer(); } +QUrl TcpServer::serverUrl() const +{ + return QUrl(QString("%1://%2:%3").arg((configuration().sslEnabled ? "guhs" : "guh")).arg(configuration().address.toString()).arg(configuration().port)); +} + /*! Sending \a data to a list of \a clients.*/ void TcpServer::sendData(const QList &clients, const QByteArray &data) { @@ -160,7 +165,7 @@ bool TcpServer::startServer() qCWarning(dcTcpServer()) << "Could not register avahi service for" << configuration(); } - qCDebug(dcConnection) << "Started Tcp server on" << m_server->serverAddress().toString() << m_server->serverPort(); + qCDebug(dcConnection) << "Started Tcp server" << serverUrl().toString(); connect(m_server, SIGNAL(clientConnected(QSslSocket *)), SLOT(onClientConnected(QSslSocket *))); connect(m_server, SIGNAL(clientDisconnected(QSslSocket *)), SLOT(onClientDisconnected(QSslSocket *))); connect(m_server, &SslServer::dataAvailable, this, &TcpServer::onDataAvailable); diff --git a/libguh-core/tcpserver.h b/libguh-core/tcpserver.h index 8a03463b..b2b6508b 100644 --- a/libguh-core/tcpserver.h +++ b/libguh-core/tcpserver.h @@ -75,6 +75,8 @@ public: explicit TcpServer(const ServerConfiguration &configuration, const QSslConfiguration &sslConfiguration, QObject *parent = 0); ~TcpServer(); + QUrl serverUrl() const; + void sendData(const QUuid &clientId, const QByteArray &data) override; void sendData(const QList &clients, const QByteArray &data) override; diff --git a/libguh-core/webserver.cpp b/libguh-core/webserver.cpp index d5ac5feb..a9708700 100644 --- a/libguh-core/webserver.cpp +++ b/libguh-core/webserver.cpp @@ -115,11 +115,16 @@ WebServer::WebServer(const WebServerConfiguration &configuration, const QSslConf /*! Destructor of this \l{WebServer}. */ WebServer::~WebServer() { - qCDebug(dcApplication) << "Shutting down \"Webserver\"" << QString("%1://%2:%3").arg((m_configuration.sslEnabled ? "https" : "http")).arg(m_configuration.address.toString()).arg(m_configuration.port); + qCDebug(dcApplication) << "Shutting down \"Webserver\"" << serverUrl().toString(); this->close(); } +QUrl WebServer::serverUrl() const +{ + return QUrl(QString("%1://%2:%3").arg((m_configuration.sslEnabled ? "https" : "http")).arg(m_configuration.address.toString()).arg(m_configuration.port)); +} + /*! Send the given \a reply map to the corresponding client. * * \sa HttpReply @@ -503,16 +508,12 @@ void WebServer::reconfigureServer(const WebServerConfiguration &config) bool WebServer::startServer() { if (!listen(m_configuration.address, m_configuration.port)) { - qCWarning(dcWebServer()) << "Webserver could not listen on" << m_configuration.address.toString() << m_configuration.port << errorString(); + qCWarning(dcWebServer()) << "Webserver could not listen on" << serverUrl().toString() << errorString(); m_enabled = false; return false; } - if (m_configuration.sslEnabled) { - qCDebug(dcWebServer()) << "Started webserver on" << QString("https://%1:%2").arg(m_configuration.address.toString()).arg(m_configuration.port); - } else { - qCDebug(dcWebServer()) << "Started webserver on" << QString("http://%1:%2").arg(m_configuration.address.toString()).arg(m_configuration.port); - } + qCDebug(dcConnection()) << "Started web server on" << serverUrl().toString(); // Note: reversed order QHash txt; diff --git a/libguh-core/webserver.h b/libguh-core/webserver.h index 71c9f86f..b6ff9807 100644 --- a/libguh-core/webserver.h +++ b/libguh-core/webserver.h @@ -77,6 +77,8 @@ public: explicit WebServer(const WebServerConfiguration &configuration, const QSslConfiguration &sslConfiguration, QObject *parent = 0); ~WebServer(); + QUrl serverUrl() const; + void sendHttpReply(HttpReply *reply); private: diff --git a/libguh-core/websocketserver.cpp b/libguh-core/websocketserver.cpp index 407362db..e1e4b509 100644 --- a/libguh-core/websocketserver.cpp +++ b/libguh-core/websocketserver.cpp @@ -73,10 +73,15 @@ WebSocketServer::WebSocketServer(const ServerConfiguration &configuration, const /*! Destructor of this \l{WebSocketServer}. */ WebSocketServer::~WebSocketServer() { - qCDebug(dcApplication) << "Shutting down \"Websocket server\"" << QString("%1://%2:%3").arg((configuration().sslEnabled ? "wss" : "ws")).arg(configuration().address.toString()).arg(configuration().port); + qCDebug(dcApplication) << "Shutting down \"Websocket server\"" << serverUrl().toString(); stopServer(); } +QUrl WebSocketServer::serverUrl() const +{ + return QUrl(QString("%1://%2:%3").arg((configuration().sslEnabled ? "wss" : "ws")).arg(configuration().address.toString()).arg(configuration().port)); +} + /*! Send the given \a data map to the client with the given \a clientId. * * \sa TransportInterface::sendData() @@ -206,15 +211,11 @@ bool WebSocketServer::startServer() connect (m_server, &QWebSocketServer::acceptError, this, &WebSocketServer::onServerError); if (!m_server->listen(configuration().address, configuration().port)) { - qCWarning(dcConnection) << "Websocket server" << m_server->serverName() << QString("could not listen on %1:%2").arg(m_server->serverAddress().toString()).arg(configuration().port); + qCWarning(dcConnection) << "Websocket server" << m_server->serverName() << "could not listen on" << serverUrl().toString(); return false; } - if (m_server->secureMode() == QWebSocketServer::NonSecureMode) { - qCDebug(dcConnection) << "Started websocket server" << m_server->serverName() << QString("on ws://%1:%2").arg(m_server->serverAddress().toString()).arg(configuration().port); - } else { - qCDebug(dcConnection) << "Started websocket server" << m_server->serverName() << QString("on wss://%1:%2").arg(m_server->serverAddress().toString()).arg(configuration().port); - } + qCDebug(dcConnection()) << "Started websocket server" << m_server->serverName() << "on" << serverUrl().toString(); // Note: reversed order QHash txt; @@ -224,8 +225,8 @@ bool WebSocketServer::startServer() txt.insert("uuid", GuhCore::instance()->configuration()->serverUuid().toString()); txt.insert("name", GuhCore::instance()->configuration()->serverName()); txt.insert("sslEnabled", configuration().sslEnabled ? "true" : "false"); - if (m_avahiService->registerService(QString("guhIO-ws-%1").arg(configuration().id), configuration().port, "_ws._tcp", txt)) { - qCWarning(dcTcpServer()) << "Could not register avahi service for" << configuration(); + if (!m_avahiService->registerService(QString("guhIO-ws-%1").arg(configuration().id), configuration().port, "_ws._tcp", txt)) { + qCWarning(dcWebServer()) << "Could not register avahi service for" << configuration(); } return true; diff --git a/libguh-core/websocketserver.h b/libguh-core/websocketserver.h index a71ce3bb..388cd0e2 100644 --- a/libguh-core/websocketserver.h +++ b/libguh-core/websocketserver.h @@ -45,6 +45,8 @@ public: explicit WebSocketServer(const ServerConfiguration &configuration, const QSslConfiguration &sslConfiguration, QObject *parent = 0); ~WebSocketServer(); + QUrl serverUrl() const; + void sendData(const QUuid &clientId, const QByteArray &data) override; void sendData(const QList &clients, const QByteArray &data) override; diff --git a/libguh/network/avahi/qtavahiservice.cpp b/libguh/network/avahi/qtavahiservice.cpp index 280956f2..e6538e8b 100644 --- a/libguh/network/avahi/qtavahiservice.cpp +++ b/libguh/network/avahi/qtavahiservice.cpp @@ -106,8 +106,10 @@ QtAvahiService::QtAvahiServiceState QtAvahiService::state() const bool QtAvahiService::registerService(const QString &name, const quint16 &port, const QString &serviceType, const QHash &txtRecords) { // Check if the client is running - if (!d_ptr->client->client || AVAHI_CLIENT_S_RUNNING != avahi_client_get_state(d_ptr->client->client)) + if (!d_ptr->client->client || AVAHI_CLIENT_S_RUNNING != avahi_client_get_state(d_ptr->client->client)) { + qCWarning(dcAvahi()) << "Could not register service" << name << port << serviceType << ". The client is not available."; return false; + } d_ptr->name = name; d_ptr->port = port;