diff --git a/server/tcpserver.cpp b/server/tcpserver.cpp index 69df4a58..8c6b9432 100644 --- a/server/tcpserver.cpp +++ b/server/tcpserver.cpp @@ -54,7 +54,10 @@ TcpServer::TcpServer(const QHostAddress &host, const uint &port, QObject *parent m_host(host), m_port(port) { - +#ifndef TESTING_ENABLED + m_avahiService = new QtAvahiService(this); + connect(m_avahiService, &QtAvahiService::serviceStateChanged, this, &TcpServer::onAvahiServiceStateChanged); +#endif } /*! Destructor of this \l{TcpServer}. */ @@ -134,6 +137,13 @@ void TcpServer::onError(QAbstractSocket::SocketError error) stopServer(); } +void TcpServer::onAvahiServiceStateChanged(const QtAvahiService::QtAvahiServiceState &state) +{ + if (state == QtAvahiService::QtAvahiServiceStateEstablished) { + qCDebug(dcAvahi()) << "Service" << m_avahiService->name() << m_avahiService->serviceType() << "established successfully"; + } +} + bool TcpServer::reconfigureServer(const QHostAddress &address, const uint &port) { @@ -175,6 +185,10 @@ bool TcpServer::startServer() return false; } +#ifndef TESTING_ENABLED + m_avahiService->registerService("guhIO", m_port, "_jsonrpc._tcp"); +#endif + qCDebug(dcConnection) << "Started Tcp server on" << m_server->serverAddress().toString() << m_server->serverPort(); connect(m_server, SIGNAL(newConnection()), SLOT(onClientConnected())); return true; @@ -186,6 +200,11 @@ bool TcpServer::startServer() */ bool TcpServer::stopServer() { +#ifndef TESTING_ENABLED + if (m_avahiService) + m_avahiService->resetService(); +#endif + if (!m_server) return true; diff --git a/server/tcpserver.h b/server/tcpserver.h index ac107c0b..e9d53c63 100644 --- a/server/tcpserver.h +++ b/server/tcpserver.h @@ -30,6 +30,7 @@ #include #include "transportinterface.h" +#include "network/avahi/qtavahiservice.h" namespace guhserver { @@ -46,6 +47,8 @@ public: private: QTimer *m_timer; + QtAvahiService *m_avahiService; + QTcpServer * m_server; QHash m_clientList; @@ -58,6 +61,8 @@ private slots: void readPackage(); void onError(QAbstractSocket::SocketError error); + void onAvahiServiceStateChanged(const QtAvahiService::QtAvahiServiceState &state); + public slots: bool reconfigureServer(const QHostAddress &address, const uint &port); bool startServer() override; diff --git a/server/webserver.cpp b/server/webserver.cpp index 714e5646..78a3179f 100644 --- a/server/webserver.cpp +++ b/server/webserver.cpp @@ -104,11 +104,14 @@ WebServer::WebServer(const QHostAddress &host, const uint &port, const QString & m_useSsl(false), m_enabled(false) { - // Create avahi service + if (QCoreApplication::instance()->organizationName() == "guh-test") { + m_webinterfaceDir = QDir(QCoreApplication::applicationDirPath()); + qCWarning(dcWebServer) << "Using public folder" << m_webinterfaceDir.path(); + } + #ifndef TESTING_ENABLED m_avahiService = new QtAvahiService(this); connect(m_avahiService, &QtAvahiService::serviceStateChanged, this, &WebServer::onAvahiServiceStateChanged); - m_avahiService->registerService("guhIO", m_port); #endif } @@ -513,7 +516,7 @@ void WebServer::onError(QAbstractSocket::SocketError error) void WebServer::onAvahiServiceStateChanged(const QtAvahiService::QtAvahiServiceState &state) { if (state == QtAvahiService::QtAvahiServiceStateEstablished) { - qCDebug(dcAvahi()) << "Service" << m_avahiService->name() << "established successfully"; + qCDebug(dcAvahi()) << "Service" << m_avahiService->name() << m_avahiService->serviceType() << "established successfully"; } } @@ -556,6 +559,10 @@ bool WebServer::startServer() } } +#ifndef TESTING_ENABLED + m_avahiService->registerService("guhIO", m_port); +#endif + m_enabled = true; return true; } @@ -563,6 +570,11 @@ bool WebServer::startServer() /*! Returns true if this \l{WebServer} stopped successfully. */ bool WebServer::stopServer() { +#ifndef TESTING_ENABLED + if (m_avahiService) + m_avahiService->resetService(); +#endif + foreach (QSslSocket *client, m_clientList.values()) client->close(); diff --git a/server/websocketserver.cpp b/server/websocketserver.cpp index 13a355b1..f294d900 100644 --- a/server/websocketserver.cpp +++ b/server/websocketserver.cpp @@ -68,7 +68,10 @@ WebSocketServer::WebSocketServer(const QHostAddress &address, const uint &port, m_useSsl(sslEnabled), m_enabled(false) { - +#ifndef TESTING_ENABLED + m_avahiService = new QtAvahiService(this); + connect(m_avahiService, &QtAvahiService::serviceStateChanged, this, &WebSocketServer::onAvahiServiceStateChanged); +#endif } /*! Destructor of this \l{WebSocketServer}. */ @@ -168,6 +171,13 @@ void WebSocketServer::onPing(quint64 elapsedTime, const QByteArray &payload) qCDebug(dcWebSocketServer) << "ping response" << client->peerAddress() << elapsedTime << payload; } +void WebSocketServer::onAvahiServiceStateChanged(const QtAvahiService::QtAvahiServiceState &state) +{ + if (state == QtAvahiService::QtAvahiServiceStateEstablished) { + qCDebug(dcAvahi()) << "Service" << m_avahiService->name() << m_avahiService->serviceType() << "established successfully"; + } +} + bool WebSocketServer::reconfigureServer(const QHostAddress &address, const uint &port) { if (m_host == address && m_port == (qint16)port && m_server->isListening()) @@ -225,6 +235,11 @@ bool WebSocketServer::startServer() } else { qCDebug(dcConnection) << "Started websocket server" << m_server->serverName() << QString("on wss://%1:%2").arg(m_server->serverAddress().toString()).arg(m_port); } + +#ifndef TESTING_ENABLED + m_avahiService->registerService("guhIO", m_port, "_ws._tcp"); +#endif + return true; } @@ -234,6 +249,11 @@ bool WebSocketServer::startServer() */ bool WebSocketServer::stopServer() { +#ifndef TESTING_ENABLED + if (m_avahiService) + m_avahiService->resetService(); +#endif + foreach (QWebSocket *client, m_clientList.values()) { client->close(QWebSocketProtocol::CloseCodeNormal, "Stop server"); } diff --git a/server/websocketserver.h b/server/websocketserver.h index ead728f9..351bd3b2 100644 --- a/server/websocketserver.h +++ b/server/websocketserver.h @@ -28,6 +28,7 @@ #include #include +#include "network/avahi/qtavahiservice.h" #include "transportinterface.h" // Note: WebSocket Protocol from the Internet Engineering Task Force (IETF) -> RFC6455 V13: @@ -51,6 +52,8 @@ private: QWebSocketServer *m_server; QHash m_clientList; + QtAvahiService *m_avahiService; + QHostAddress m_host; qint16 m_port; @@ -68,6 +71,8 @@ private slots: void onServerError(QAbstractSocket::SocketError error); void onPing(quint64 elapsedTime, const QByteArray & payload); + void onAvahiServiceStateChanged(const QtAvahiService::QtAvahiServiceState &state); + public slots: bool reconfigureServer(const QHostAddress &address, const uint &port); bool startServer() override;