diff --git a/libguh-core/cloudmanager.cpp b/libguh-core/cloudmanager.cpp index cf9f47d1..535e2547 100644 --- a/libguh-core/cloudmanager.cpp +++ b/libguh-core/cloudmanager.cpp @@ -55,6 +55,7 @@ void CloudManager::setDeviceId(const QUuid &deviceId) void CloudManager::setDeviceName(const QString &name) { + qCDebug(dcAWS()) << "Set device name" << name; m_deviceName = name; m_awsConnector->setDeviceName(name); } diff --git a/libguh-core/guhcore.cpp b/libguh-core/guhcore.cpp index 7f4bc677..32ad385d 100644 --- a/libguh-core/guhcore.cpp +++ b/libguh-core/guhcore.cpp @@ -459,6 +459,7 @@ void GuhCore::init() { connect(m_configuration, &GuhConfiguration::localeChanged, this, &GuhCore::onLocaleChanged); connect(m_configuration, &GuhConfiguration::cloudEnabledChanged, m_cloudManager, &CloudManager::setEnabled); connect(m_configuration, &GuhConfiguration::serverNameChanged, m_cloudManager, &CloudManager::setDeviceName); + connect(m_configuration, &GuhConfiguration::serverNameChanged, m_serverManager, &ServerManager::setServerName); connect(m_deviceManager, &DeviceManager::pluginConfigChanged, this, &GuhCore::pluginConfigChanged); connect(m_deviceManager, &DeviceManager::eventTriggered, this, &GuhCore::gotEvent); diff --git a/libguh-core/servermanager.cpp b/libguh-core/servermanager.cpp index 48a058e4..617030b2 100644 --- a/libguh-core/servermanager.cpp +++ b/libguh-core/servermanager.cpp @@ -123,7 +123,6 @@ ServerManager::ServerManager(GuhConfiguration* configuration, QObject *parent) : m_webServers.insert(config.id, webServer); } - connect(configuration, &GuhConfiguration::serverNameChanged, this, &ServerManager::onServerNameChanged); connect(configuration, &GuhConfiguration::tcpServerConfigurationChanged, this, &ServerManager::tcpServerConfigurationChanged); connect(configuration, &GuhConfiguration::tcpServerConfigurationRemoved, this, &ServerManager::tcpServerConfigurationRemoved); connect(configuration, &GuhConfiguration::webSocketServerConfigurationChanged, this, &ServerManager::webSocketServerConfigurationChanged); @@ -154,19 +153,6 @@ MockTcpServer *ServerManager::mockTcpServer() const return m_mockTcpServer; } -void ServerManager::onServerNameChanged() -{ - qCDebug(dcConnection()) << "Server name changed"; - - foreach (WebSocketServer *websocketServer, m_webSocketServers.values()) { - websocketServer->resetAvahiService(); - } - - foreach (WebServer *webServer, m_webServers.values()) { - webServer->resetAvahiService(); - } -} - void ServerManager::tcpServerConfigurationChanged(const QString &id) { ServerConfiguration config = GuhCore::instance()->configuration()->tcpServerConfigurations().value(id); @@ -275,4 +261,21 @@ bool ServerManager::loadCertificate(const QString &certificateKeyFileName, const return true; } +void ServerManager::setServerName(const QString &serverName) +{ + qCDebug(dcConnection()) << "Server name changed" << serverName; + + foreach (WebSocketServer *websocketServer, m_webSocketServers.values()) { + websocketServer->setServerName(serverName); + } + + foreach (WebServer *webServer, m_webServers.values()) { + webServer->setServerName(serverName); + } + + foreach (TcpServer *tcpServer, m_tcpServers.values()) { + tcpServer->setServerName(serverName); + } +} + } diff --git a/libguh-core/servermanager.h b/libguh-core/servermanager.h index 1400e165..9838aea5 100644 --- a/libguh-core/servermanager.h +++ b/libguh-core/servermanager.h @@ -52,7 +52,6 @@ public: MockTcpServer *mockTcpServer() const; private slots: - void onServerNameChanged(); void tcpServerConfigurationChanged(const QString &id); void tcpServerConfigurationRemoved(const QString &id); void webSocketServerConfigurationChanged(const QString &id); @@ -77,6 +76,10 @@ private: QSslCertificate m_certificate; bool loadCertificate(const QString &certificateKeyFileName, const QString &certificateFileName); + +public slots: + void setServerName(const QString &serverName); + }; } diff --git a/libguh-core/tcpserver.cpp b/libguh-core/tcpserver.cpp index 51ed5e57..35c70475 100644 --- a/libguh-core/tcpserver.cpp +++ b/libguh-core/tcpserver.cpp @@ -49,7 +49,7 @@ TcpServer::TcpServer(const ServerConfiguration &configuration, const QSslConfigu TransportInterface(configuration, parent), m_server(NULL), m_sslConfig(sslConfiguration) -{ +{ m_avahiService = new QtAvahiService(this); connect(m_avahiService, &QtAvahiService::serviceStateChanged, this, &TcpServer::onAvahiServiceStateChanged); } @@ -126,7 +126,20 @@ void TcpServer::onAvahiServiceStateChanged(const QtAvahiService::QtAvahiServiceS void TcpServer::resetAvahiService() { + if (m_avahiService) + m_avahiService->resetService(); + // Note: reversed order + QHash txt; + txt.insert("jsonrpcVersion", JSON_PROTOCOL_VERSION); + txt.insert("serverVersion", GUH_VERSION_STRING); + txt.insert("manufacturer", "guh GmbH"); + 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-tcp-%1").arg(configuration().id), configuration().port, "_jsonrpc._tcp", txt)) { + qCWarning(dcTcpServer()) << "Could not register avahi service for" << configuration(); + } } @@ -145,6 +158,12 @@ void TcpServer::reconfigureServer(const ServerConfiguration &config) startServer(); } +void TcpServer::setServerName(const QString &serverName) +{ + m_serverName = serverName; + resetAvahiService(); +} + /*! Returns true if this \l{TcpServer} started successfully. * * \sa TransportInterface::startServer() diff --git a/libguh-core/tcpserver.h b/libguh-core/tcpserver.h index 48273abf..139b2a39 100644 --- a/libguh-core/tcpserver.h +++ b/libguh-core/tcpserver.h @@ -98,11 +98,11 @@ private slots: void onEncrypted(); void onAvahiServiceStateChanged(const QtAvahiService::QtAvahiServiceState &state); - + void resetAvahiService(); public slots: - void resetAvahiService(); void reconfigureServer(const ServerConfiguration &configuration); + void setServerName(const QString &serverName) override; bool startServer() override; bool stopServer() override; }; diff --git a/libguh-core/transportinterface.cpp b/libguh-core/transportinterface.cpp index 50ba82ea..1fdf82c7 100644 --- a/libguh-core/transportinterface.cpp +++ b/libguh-core/transportinterface.cpp @@ -95,6 +95,11 @@ ServerConfiguration TransportInterface::configuration() const return m_config; } +void TransportInterface::setServerName(const QString &serverName) +{ + m_serverName = serverName; +} + /*! Virtual destructor for \l{TransportInterface}. */ TransportInterface::~TransportInterface() { diff --git a/libguh-core/transportinterface.h b/libguh-core/transportinterface.h index 5a012f2b..33ea075c 100644 --- a/libguh-core/transportinterface.h +++ b/libguh-core/transportinterface.h @@ -43,12 +43,16 @@ public: void setConfiguration(const ServerConfiguration &config); ServerConfiguration configuration() const; +protected: + QString m_serverName; + signals: void clientConnected(const QUuid &clientId); void clientDisconnected(const QUuid &clientId); void dataAvailable(const QUuid &clientId, const QByteArray &data); public slots: + virtual void setServerName(const QString &serverName); virtual bool startServer() = 0; virtual bool stopServer() = 0; diff --git a/libguh-core/webserver.cpp b/libguh-core/webserver.cpp index 72c5e431..b1a816ee 100644 --- a/libguh-core/webserver.cpp +++ b/libguh-core/webserver.cpp @@ -490,8 +490,10 @@ void WebServer::onAvahiServiceStateChanged(const QtAvahiService::QtAvahiServiceS void WebServer::resetAvahiService() { - if (m_avahiService) - m_avahiService->resetService(); + if (!m_avahiService) + return; + + m_avahiService->resetService(); // Note: reversed order QHash txt; @@ -523,6 +525,12 @@ void WebServer::reconfigureServer(const WebServerConfiguration &config) startServer(); } +void WebServer::setServerName(const QString &serverName) +{ + m_serverName = serverName; + resetAvahiService(); +} + /*! Returns true if this \l{WebServer} started successfully. */ bool WebServer::startServer() { diff --git a/libguh-core/webserver.h b/libguh-core/webserver.h index 5030648a..a6e896fd 100644 --- a/libguh-core/webserver.h +++ b/libguh-core/webserver.h @@ -87,8 +87,7 @@ private: QHash m_incompleteRequests; QtAvahiService *m_avahiService; - - + QString m_serverName; WebServerConfiguration m_configuration; QSslConfiguration m_sslConfiguration; @@ -115,10 +114,11 @@ private slots: void onError(QAbstractSocket::SocketError error); void onAvahiServiceStateChanged(const QtAvahiService::QtAvahiServiceState &state); + void resetAvahiService(); public slots: - void resetAvahiService(); void reconfigureServer(const WebServerConfiguration &config); + void setServerName(const QString &serverName); bool startServer(); bool stopServer(); diff --git a/libguh-core/websocketserver.cpp b/libguh-core/websocketserver.cpp index 90350305..4fedfa01 100644 --- a/libguh-core/websocketserver.cpp +++ b/libguh-core/websocketserver.cpp @@ -193,8 +193,19 @@ void WebSocketServer::onAvahiServiceStateChanged(const QtAvahiService::QtAvahiSe void WebSocketServer::resetAvahiService() { - if (m_avahiService) - m_avahiService->resetService(); + if (!m_avahiService) + return; + + m_avahiService->resetService(); + + // Note: reversed order + QHash txt; + txt.insert("jsonrpcVersion", JSON_PROTOCOL_VERSION); + txt.insert("serverVersion", GUH_VERSION_STRING); + txt.insert("manufacturer", "guh GmbH"); + 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", createTxtRecord())) { qCWarning(dcWebServer()) << "Could not register avahi service for" << configuration(); @@ -219,6 +230,12 @@ void WebSocketServer::reconfigureServer(const ServerConfiguration &config) startServer(); } +void WebSocketServer::setServerName(const QString &serverName) +{ + m_serverName = serverName; + resetAvahiService(); +} + /*! Returns true if this \l{WebSocketServer} started successfully. * * \sa TransportInterface::startServer() diff --git a/libguh-core/websocketserver.h b/libguh-core/websocketserver.h index 02bda3c5..e9a42848 100644 --- a/libguh-core/websocketserver.h +++ b/libguh-core/websocketserver.h @@ -69,10 +69,11 @@ private slots: void onPing(quint64 elapsedTime, const QByteArray & payload); void onAvahiServiceStateChanged(const QtAvahiService::QtAvahiServiceState &state); + void resetAvahiService(); public slots: - void resetAvahiService(); void reconfigureServer(const ServerConfiguration &config); + void setServerName(const QString &serverName) override; bool startServer() override; bool stopServer() override; };