fix webserver test config and add avahi service to interfaces

This commit is contained in:
Simon Stürz 2016-07-12 00:10:09 +02:00 committed by Michael Zanetti
parent 69a31b00c6
commit 5ce8e78db5
5 changed files with 66 additions and 5 deletions

View File

@ -54,7 +54,10 @@ TcpServer::TcpServer(const QHostAddress &host, const uint &port, QObject *parent
m_host(host), m_host(host),
m_port(port) 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}. */ /*! Destructor of this \l{TcpServer}. */
@ -134,6 +137,13 @@ void TcpServer::onError(QAbstractSocket::SocketError error)
stopServer(); 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) bool TcpServer::reconfigureServer(const QHostAddress &address, const uint &port)
{ {
@ -175,6 +185,10 @@ bool TcpServer::startServer()
return false; 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(); qCDebug(dcConnection) << "Started Tcp server on" << m_server->serverAddress().toString() << m_server->serverPort();
connect(m_server, SIGNAL(newConnection()), SLOT(onClientConnected())); connect(m_server, SIGNAL(newConnection()), SLOT(onClientConnected()));
return true; return true;
@ -186,6 +200,11 @@ bool TcpServer::startServer()
*/ */
bool TcpServer::stopServer() bool TcpServer::stopServer()
{ {
#ifndef TESTING_ENABLED
if (m_avahiService)
m_avahiService->resetService();
#endif
if (!m_server) if (!m_server)
return true; return true;

View File

@ -30,6 +30,7 @@
#include <QTimer> #include <QTimer>
#include "transportinterface.h" #include "transportinterface.h"
#include "network/avahi/qtavahiservice.h"
namespace guhserver { namespace guhserver {
@ -46,6 +47,8 @@ public:
private: private:
QTimer *m_timer; QTimer *m_timer;
QtAvahiService *m_avahiService;
QTcpServer * m_server; QTcpServer * m_server;
QHash<QUuid, QTcpSocket *> m_clientList; QHash<QUuid, QTcpSocket *> m_clientList;
@ -58,6 +61,8 @@ private slots:
void readPackage(); void readPackage();
void onError(QAbstractSocket::SocketError error); void onError(QAbstractSocket::SocketError error);
void onAvahiServiceStateChanged(const QtAvahiService::QtAvahiServiceState &state);
public slots: public slots:
bool reconfigureServer(const QHostAddress &address, const uint &port); bool reconfigureServer(const QHostAddress &address, const uint &port);
bool startServer() override; bool startServer() override;

View File

@ -104,11 +104,14 @@ WebServer::WebServer(const QHostAddress &host, const uint &port, const QString &
m_useSsl(false), m_useSsl(false),
m_enabled(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 #ifndef TESTING_ENABLED
m_avahiService = new QtAvahiService(this); m_avahiService = new QtAvahiService(this);
connect(m_avahiService, &QtAvahiService::serviceStateChanged, this, &WebServer::onAvahiServiceStateChanged); connect(m_avahiService, &QtAvahiService::serviceStateChanged, this, &WebServer::onAvahiServiceStateChanged);
m_avahiService->registerService("guhIO", m_port);
#endif #endif
} }
@ -513,7 +516,7 @@ void WebServer::onError(QAbstractSocket::SocketError error)
void WebServer::onAvahiServiceStateChanged(const QtAvahiService::QtAvahiServiceState &state) void WebServer::onAvahiServiceStateChanged(const QtAvahiService::QtAvahiServiceState &state)
{ {
if (state == QtAvahiService::QtAvahiServiceStateEstablished) { 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; m_enabled = true;
return true; return true;
} }
@ -563,6 +570,11 @@ bool WebServer::startServer()
/*! Returns true if this \l{WebServer} stopped successfully. */ /*! Returns true if this \l{WebServer} stopped successfully. */
bool WebServer::stopServer() bool WebServer::stopServer()
{ {
#ifndef TESTING_ENABLED
if (m_avahiService)
m_avahiService->resetService();
#endif
foreach (QSslSocket *client, m_clientList.values()) foreach (QSslSocket *client, m_clientList.values())
client->close(); client->close();

View File

@ -68,7 +68,10 @@ WebSocketServer::WebSocketServer(const QHostAddress &address, const uint &port,
m_useSsl(sslEnabled), m_useSsl(sslEnabled),
m_enabled(false) 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}. */ /*! 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; 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) bool WebSocketServer::reconfigureServer(const QHostAddress &address, const uint &port)
{ {
if (m_host == address && m_port == (qint16)port && m_server->isListening()) if (m_host == address && m_port == (qint16)port && m_server->isListening())
@ -225,6 +235,11 @@ bool WebSocketServer::startServer()
} else { } else {
qCDebug(dcConnection) << "Started websocket server" << m_server->serverName() << QString("on wss://%1:%2").arg(m_server->serverAddress().toString()).arg(m_port); 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; return true;
} }
@ -234,6 +249,11 @@ bool WebSocketServer::startServer()
*/ */
bool WebSocketServer::stopServer() bool WebSocketServer::stopServer()
{ {
#ifndef TESTING_ENABLED
if (m_avahiService)
m_avahiService->resetService();
#endif
foreach (QWebSocket *client, m_clientList.values()) { foreach (QWebSocket *client, m_clientList.values()) {
client->close(QWebSocketProtocol::CloseCodeNormal, "Stop server"); client->close(QWebSocketProtocol::CloseCodeNormal, "Stop server");
} }

View File

@ -28,6 +28,7 @@
#include <QWebSocket> #include <QWebSocket>
#include <QWebSocketServer> #include <QWebSocketServer>
#include "network/avahi/qtavahiservice.h"
#include "transportinterface.h" #include "transportinterface.h"
// Note: WebSocket Protocol from the Internet Engineering Task Force (IETF) -> RFC6455 V13: // Note: WebSocket Protocol from the Internet Engineering Task Force (IETF) -> RFC6455 V13:
@ -51,6 +52,8 @@ private:
QWebSocketServer *m_server; QWebSocketServer *m_server;
QHash<QUuid, QWebSocket *> m_clientList; QHash<QUuid, QWebSocket *> m_clientList;
QtAvahiService *m_avahiService;
QHostAddress m_host; QHostAddress m_host;
qint16 m_port; qint16 m_port;
@ -68,6 +71,8 @@ private slots:
void onServerError(QAbstractSocket::SocketError error); void onServerError(QAbstractSocket::SocketError error);
void onPing(quint64 elapsedTime, const QByteArray & payload); void onPing(quint64 elapsedTime, const QByteArray & payload);
void onAvahiServiceStateChanged(const QtAvahiService::QtAvahiServiceState &state);
public slots: public slots:
bool reconfigureServer(const QHostAddress &address, const uint &port); bool reconfigureServer(const QHostAddress &address, const uint &port);
bool startServer() override; bool startServer() override;