cleanup debug

add UPnP byebye message at shutdown
This commit is contained in:
Simon Stürz 2016-01-23 22:55:58 +01:00 committed by Michael Zanetti
parent 03381cc25c
commit d5af628a52
8 changed files with 77 additions and 16 deletions

View File

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

View File

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

View File

@ -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<UpnpDeviceDescriptor> &deviceDescriptorList, const PluginId & pluginId);
void upnpNotify(const QByteArray &notifyMessage);
@ -72,6 +72,8 @@ private slots:
void readData();
void replyFinished(QNetworkReply *reply);
void notificationTimeout();
void sendByeByeMessage();
void sendAliveMessage();
void discoverTimeout();
};

View File

@ -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();
}

View File

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

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}