From d5af628a52c43c1eb4c26848baea7196d35776b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Sat, 23 Jan 2016 22:55:58 +0100 Subject: [PATCH] cleanup debug add UPnP byebye message at shutdown --- libguh/devicemanager.cpp | 2 +- .../network/upnpdiscovery/upnpdiscovery.cpp | 75 +++++++++++++++++-- libguh/network/upnpdiscovery/upnpdiscovery.h | 6 +- server/logging/logengine.cpp | 2 +- server/ruleengine.cpp | 2 +- server/tcpserver.cpp | 2 +- server/webserver.cpp | 2 +- server/websocketserver.cpp | 2 +- 8 files changed, 77 insertions(+), 16 deletions(-) diff --git a/libguh/devicemanager.cpp b/libguh/devicemanager.cpp index 27718cab..f3730c81 100644 --- a/libguh/devicemanager.cpp +++ b/libguh/devicemanager.cpp @@ -238,7 +238,7 @@ DeviceManager::DeviceManager(QObject *parent) : /*! Destructor of the DeviceManager. Each loaded \l{DevicePlugin} will be deleted. */ DeviceManager::~DeviceManager() { - qCDebug(dcApplication) << "Shutting down device manager"; + qCDebug(dcApplication) << "Shutting down \"Device Manager\""; foreach (DevicePlugin *plugin, m_devicePlugins) { delete plugin; } diff --git a/libguh/network/upnpdiscovery/upnpdiscovery.cpp b/libguh/network/upnpdiscovery/upnpdiscovery.cpp index 77bf32c4..c39d1fd1 100644 --- a/libguh/network/upnpdiscovery/upnpdiscovery.cpp +++ b/libguh/network/upnpdiscovery/upnpdiscovery.cpp @@ -92,6 +92,16 @@ UpnpDiscovery::UpnpDiscovery(QObject *parent) : m_notificationTimer->start(); qCDebug(dcDeviceManager) << "--> UPnP discovery created successfully."; + sendAliveMessage(); + sendAliveMessage(); +} + +UpnpDiscovery::~UpnpDiscovery() +{ + qCDebug(dcApplication) << "Shutting down \"UPnP Server\""; + sendByeByeMessage(); + waitForBytesWritten(); + close(); } /*! Returns false, if the \l{UpnpDiscovery} resource is not available. Returns true, if a device with @@ -152,12 +162,12 @@ void UpnpDiscovery::respondToSearchRequest(QHostAddress host, int port) // http://upnp.org/specs/basic/UPnP-basic-Basic-v1-Device.pdf QByteArray rootdeviceResponseMessage = QByteArray("HTTP/1.1 200 OK\r\n" - "Cache-Control: max-age=1900\r\n" + "CACHE-CONTROL: max-age=1900\r\n" "DATE: " + QDateTime::currentDateTime().toString("ddd, dd MMM yyyy hh:mm:ss").toUtf8() + " GMT\r\n" "EXT:\r\n" "CONTENT-LENGTH:0\r\n" - "Location: " + locationString.toUtf8() + "\r\n" - "Server: guh/" + QByteArray(GUH_VERSION_STRING) + " UPnP/1.1 \r\n" + "LOCATION: " + locationString.toUtf8() + "\r\n" + "SERVER: guh/" + QByteArray(GUH_VERSION_STRING) + " UPnP/1.1 \r\n" "ST:upnp:rootdevice\r\n" "USN:uuid:" + uuid + "::urn:schemas-upnp-org:device:Basic:1\r\n" "\r\n"); @@ -173,7 +183,6 @@ void UpnpDiscovery::respondToSearchRequest(QHostAddress host, int port) /*! This method will be called to send the SSDP message \a data to the UPnP multicast.*/ void UpnpDiscovery::sendToMulticast(const QByteArray &data) { - //qCDebug(dcHardware) << "sending to multicast\n" << data; writeDatagram(data, m_host, m_port); } @@ -309,6 +318,11 @@ void UpnpDiscovery::replyFinished(QNetworkReply *reply) } void UpnpDiscovery::notificationTimeout() +{ + sendAliveMessage(); +} + +void UpnpDiscovery::sendByeByeMessage() { GuhSettings settings(GuhSettings::SettingsRoleDevices); settings.beginGroup("guhd"); @@ -337,17 +351,62 @@ void UpnpDiscovery::notificationTimeout() } // http://upnp.org/specs/basic/UPnP-basic-Basic-v1-Device.pdf - QByteArray rootdeviceResponseMessage = QByteArray("NOTIFY * HTTP/1.1\r\n" + QByteArray byebyeMessage = QByteArray("NOTIFY * HTTP/1.1\r\n" "HOST:239.255.255.250:1900\r\n" - "Cache-Control: max-age=1900\r\n" - "Location: " + locationString.toUtf8() + "\r\n" + "CACHE-CONTROL: max-age=1900\r\n" + "LOCATION: " + locationString.toUtf8() + "\r\n" + "NT:urn:schemas-upnp-org:device:Basic:1\r\n" + "USN:uuid:" + uuid + "::urn:schemas-upnp-org:device:Basic:1\r\n" + "NTS: ssdp:byebye\r\n" + "SERVER: guh/" + QByteArray(GUH_VERSION_STRING) + " UPnP/1.1 \r\n" + "\r\n"); + + sendToMulticast(byebyeMessage); + } + } + } +} + +void UpnpDiscovery::sendAliveMessage() +{ + GuhSettings settings(GuhSettings::SettingsRoleDevices); + settings.beginGroup("guhd"); + QByteArray uuid = settings.value("uuid", QVariant()).toByteArray(); + if (uuid.isEmpty()) { + uuid = QUuid::createUuid().toByteArray().replace("{", "").replace("}",""); + settings.setValue("uuid", uuid); + } + settings.endGroup(); + + GuhSettings globalSettings(GuhSettings::SettingsRoleGlobal); + globalSettings.beginGroup("WebServer"); + int port = settings.value("port", 3333).toInt(); + bool useSsl = settings.value("https", false).toBool(); + globalSettings.endGroup(); + + foreach (const QNetworkInterface &interface, QNetworkInterface::allInterfaces()) { + // listen only on IPv4 + foreach (QNetworkAddressEntry entry, interface.addressEntries()) { + if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) { + QString locationString; + if (useSsl) { + locationString = "https://" + entry.ip().toString() + ":" + QString::number(port) + "/server.xml"; + } else { + locationString = "http://" + entry.ip().toString() + ":" + QString::number(port) + "/server.xml"; + } + + // http://upnp.org/specs/basic/UPnP-basic-Basic-v1-Device.pdf + QByteArray aliveMessage = QByteArray("NOTIFY * HTTP/1.1\r\n" + "HOST:239.255.255.250:1900\r\n" + "CACHE-CONTROL: max-age=1900\r\n" + "LOCATION: " + locationString.toUtf8() + "\r\n" "NT:urn:schemas-upnp-org:device:Basic:1\r\n" "USN:uuid:" + uuid + "::urn:schemas-upnp-org:device:Basic:1\r\n" "NTS: ssdp:alive\r\n" "SERVER: guh/" + QByteArray(GUH_VERSION_STRING) + " UPnP/1.1 \r\n" "\r\n"); - sendToMulticast(rootdeviceResponseMessage); + sendToMulticast(aliveMessage); } } } diff --git a/libguh/network/upnpdiscovery/upnpdiscovery.h b/libguh/network/upnpdiscovery/upnpdiscovery.h index 137a7d1a..f1fb6c88 100644 --- a/libguh/network/upnpdiscovery/upnpdiscovery.h +++ b/libguh/network/upnpdiscovery/upnpdiscovery.h @@ -44,6 +44,8 @@ class LIBGUH_EXPORT UpnpDiscovery : public QUdpSocket Q_OBJECT public: explicit UpnpDiscovery(QObject *parent = 0); + ~UpnpDiscovery(); + bool discoverDevices(const QString &searchTarget = "ssdp:all", const QString &userAgent = "", const PluginId &pluginId = PluginId()); void sendToMulticast(const QByteArray &data); @@ -61,8 +63,6 @@ private: void requestDeviceInformation(const QNetworkRequest &networkRequest, const UpnpDeviceDescriptor &upnpDeviceDescriptor); void respondToSearchRequest(QHostAddress host, int port); -protected: - signals: void discoveryFinished(const QList &deviceDescriptorList, const PluginId & pluginId); void upnpNotify(const QByteArray ¬ifyMessage); @@ -72,6 +72,8 @@ private slots: void readData(); void replyFinished(QNetworkReply *reply); void notificationTimeout(); + void sendByeByeMessage(); + void sendAliveMessage(); void discoverTimeout(); }; diff --git a/server/logging/logengine.cpp b/server/logging/logengine.cpp index 808f5a96..5dbf65ed 100644 --- a/server/logging/logengine.cpp +++ b/server/logging/logengine.cpp @@ -147,7 +147,7 @@ LogEngine::LogEngine(QObject *parent): LogEngine::~LogEngine() { - qCDebug(dcApplication) << "Shutting down log engine"; + qCDebug(dcApplication) << "Shutting down \"Log Engine\""; m_db.close(); } diff --git a/server/ruleengine.cpp b/server/ruleengine.cpp index f7428149..8730fbd1 100644 --- a/server/ruleengine.cpp +++ b/server/ruleengine.cpp @@ -215,7 +215,7 @@ RuleEngine::RuleEngine(QObject *parent) : RuleEngine::~RuleEngine() { - qCDebug(dcApplication) << "Shutting down rule engine"; + qCDebug(dcApplication) << "Shutting down \"Rule Engine\""; } /*! Ask the Engine to evaluate all the rules for the given \a event. diff --git a/server/tcpserver.cpp b/server/tcpserver.cpp index 0824b31f..69ba8c14 100644 --- a/server/tcpserver.cpp +++ b/server/tcpserver.cpp @@ -87,7 +87,7 @@ TcpServer::TcpServer(QObject *parent) : /*! Destructor of this \l{TcpServer}. */ TcpServer::~TcpServer() { - qCDebug(dcApplication) << "Shutting down TCP server"; + qCDebug(dcApplication) << "Shutting down \"TCP Server\""; stopServer(); } diff --git a/server/webserver.cpp b/server/webserver.cpp index fb5ed88c..6a38cb12 100644 --- a/server/webserver.cpp +++ b/server/webserver.cpp @@ -131,7 +131,7 @@ WebServer::WebServer(const QSslConfiguration &sslConfiguration, QObject *parent) /*! Destructor of this \l{WebServer}. */ WebServer::~WebServer() { - qCDebug(dcApplication) << "Shutting down webserver"; + qCDebug(dcApplication) << "Shutting down \"Webserver\""; this->close(); } diff --git a/server/websocketserver.cpp b/server/websocketserver.cpp index 4a718819..250f6425 100644 --- a/server/websocketserver.cpp +++ b/server/websocketserver.cpp @@ -85,7 +85,7 @@ WebSocketServer::WebSocketServer(const QSslConfiguration &sslConfiguration, QObj /*! Destructor of this \l{WebSocketServer}. */ WebSocketServer::~WebSocketServer() { - qCDebug(dcApplication) << "Shutting down websocket server"; + qCDebug(dcApplication) << "Shutting down \"Websocket server\""; stopServer(); }