update avahi service if server name changes

This commit is contained in:
Simon Stürz 2017-11-22 22:46:46 +01:00 committed by Michael Zanetti
parent 39c7d9b0a1
commit fff63c2b18
12 changed files with 88 additions and 26 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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<QString, QString> 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()

View File

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

View File

@ -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()
{

View File

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

View File

@ -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<QString, QString> 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()
{

View File

@ -87,8 +87,7 @@ private:
QHash<QSslSocket *, HttpRequest> 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();

View File

@ -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<QString, QString> 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()

View File

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