Merge PR #219: Update to latest upstream QtZeroConf

This commit is contained in:
Jenkins 2019-07-05 18:17:09 +02:00
commit 9e7b516cc2
3 changed files with 27 additions and 38 deletions

@ -1 +1 @@
Subproject commit 1632db0b7f9f637b1a6325a267fa404c40fa6693 Subproject commit 1e36e430e98ecec6e15ab06f56467567f22f9e8a

View File

@ -11,7 +11,6 @@ ZeroconfDiscovery::ZeroconfDiscovery(NymeaHosts *nymeaHosts, QObject *parent) :
#ifdef WITH_ZEROCONF #ifdef WITH_ZEROCONF
// NOTE: There seem to be too many issues in QtZeroConf and IPv6. // NOTE: There seem to be too many issues in QtZeroConf and IPv6.
// See https://github.com/jbagg/QtZeroConf/issues/22 // See https://github.com/jbagg/QtZeroConf/issues/22
// IPv6 resolving is disabled completely for android in avahicore.cpp for now
// Limiting this to IPv4 for now... // Limiting this to IPv4 for now...
m_zeroconfJsonRPC = new QZeroConf(this); m_zeroconfJsonRPC = new QZeroConf(this);
@ -54,40 +53,30 @@ bool ZeroconfDiscovery::discovering() const
#ifdef WITH_ZEROCONF #ifdef WITH_ZEROCONF
void ZeroconfDiscovery::serviceEntryAdded(const QZeroConfService &entry) void ZeroconfDiscovery::serviceEntryAdded(const QZeroConfService &entry)
{ {
if (!entry.name().startsWith("nymea")) { if (!entry->name().startsWith("nymea")) {
// Skip non-nymea services altogether // Skip non-nymea services altogether
qDebug() << "Skipping Avahi entry:" << entry << entry.ip() << entry.ipv6() << entry.txt() << entry.type(); qDebug() << "Skipping Avahi entry:" << entry << entry->ip() << entry->txt() << entry->type();
return; return;
} }
if (entry.ip().isNull() && entry.ipv6().isNull()) { if (entry->ip().isNull()) {
// Skip entries that don't have an ip address at all for some reason // Skip entries that don't have an ip address at all for some reason
qDebug() << "Skipping Avahi entry:" << entry << entry.ip() << entry.ipv6() << entry.txt() << entry.type(); qDebug() << "Skipping Avahi entry:" << entry << entry->ip() << entry->txt() << entry->type();
return; return;
} }
if (entry.ip().isNull() && entry.ipv6().toString().startsWith("fe80")) { if (entry->ip().toString().startsWith("fe80")) {
// Skip link-local-IPv6-only results // Skip link-local-IPv6 results
qDebug() << "Skipping Avahi entry:" << entry << entry.ip() << entry.ipv6() << entry.txt() << entry.type(); qDebug() << "Skipping Avahi entry:" << entry << entry->ip() << entry->txt() << entry->type();
return; return;
} }
// Workaround a bug in deeper layers (I believe it's avahi, but could be QtZeroConf too): qDebug() << "zeroconf service discovered" << entry->type() << entry->name() << " IP:" << entry->ip().toString() << entry->txt();
// Sometimes the ip() field contains an IPv6 address. In that case the entry is likely garbage as
// it does not mean the host necessarily exports the services on IPv6.
bool isIPv4;
entry.ip().toIPv4Address(&isIPv4);
if (!isIPv4) {
qDebug() << "Skipping invalid Avahi entry: IPv4:" << entry.ip();
return;
}
// qDebug() << "zeroconf service discovered" << entry.type() << entry.name() << " IP:" << entry.ip() << "IPv6:" << entry.ipv6() << entry.txt();
QString uuid; QString uuid;
bool sslEnabled = false; bool sslEnabled = false;
QString serverName; QString serverName;
QString version; QString version;
foreach (const QByteArray &key, entry.txt().keys()) { foreach (const QByteArray &key, entry->txt().keys()) {
QPair<QString, QString> txtRecord = qMakePair<QString, QString>(key, entry.txt().value(key)); QPair<QString, QString> txtRecord = qMakePair<QString, QString>(key, entry->txt().value(key));
if (!sslEnabled && txtRecord.first == "sslEnabled") { if (!sslEnabled && txtRecord.first == "sslEnabled") {
sslEnabled = (txtRecord.second == "true"); sslEnabled = (txtRecord.second == "true");
} }
@ -108,37 +97,37 @@ void ZeroconfDiscovery::serviceEntryAdded(const QZeroConfService &entry)
if (!host) { if (!host) {
host = new NymeaHost(m_nymeaHosts); host = new NymeaHost(m_nymeaHosts);
host->setUuid(uuid); host->setUuid(uuid);
// qDebug() << "ZeroConf: Adding new host:" << serverName << uuid; qDebug() << "ZeroConf: Adding new host:" << serverName << uuid;
m_nymeaHosts->addHost(host); m_nymeaHosts->addHost(host);
} }
host->setName(serverName); host->setName(serverName);
host->setVersion(version); host->setVersion(version);
QUrl url; QUrl url;
// NOTE: On linux this is "_jsonrpc._tcp" while on apple systems this is "_jsonrpc._tcp." // NOTE: On linux this is "_jsonrpc._tcp" while on apple systems this is "_jsonrpc._tcp."
if (entry.type().startsWith("_jsonrpc._tcp")) { if (entry->type().startsWith("_jsonrpc._tcp")) {
url.setScheme(sslEnabled ? "nymeas" : "nymea"); url.setScheme(sslEnabled ? "nymeas" : "nymea");
} else if (entry.type().startsWith("_ws._tcp")) { } else if (entry->type().startsWith("_ws._tcp")) {
url.setScheme(sslEnabled ? "wss" : "ws"); url.setScheme(sslEnabled ? "wss" : "ws");
} }
url.setHost(!entry.ip().isNull() ? entry.ip().toString() : entry.ipv6().toString()); url.setHost(entry->ip().toString());
url.setPort(entry.port()); url.setPort(entry->port());
Connection *connection = host->connections()->find(url); Connection *connection = host->connections()->find(url);
if (!connection) { if (!connection) {
// qDebug() << "Zeroconf: Adding new connection to host:" << host->name() << url.toString(); qDebug() << "Zeroconf: Adding new connection to host:" << host->name() << url.toString();
QString displayName = QString("%1:%2").arg(url.host()).arg(url.port()); QString displayName = QString("%1:%2").arg(url.host()).arg(url.port());
Connection::BearerType bearerType = QHostAddress(url.host()).isLoopback() ? Connection::BearerTypeLoopback : Connection::BearerTypeLan; Connection::BearerType bearerType = QHostAddress(url.host()).isLoopback() ? Connection::BearerTypeLoopback : Connection::BearerTypeLan;
connection = new Connection(url, bearerType, sslEnabled, displayName); connection = new Connection(url, bearerType, sslEnabled, displayName);
connection->setOnline(true); connection->setOnline(true);
host->connections()->addConnection(connection); host->connections()->addConnection(connection);
} else { } else {
// qDebug() << "Zeroconf: Setting connection online:" << host->name() << url.toString(); qDebug() << "Zeroconf: Setting connection online:" << host->name() << url.toString();
connection->setOnline(true); connection->setOnline(true);
} }
} }
void ZeroconfDiscovery::serviceEntryRemoved(const QZeroConfService &entry) void ZeroconfDiscovery::serviceEntryRemoved(const QZeroConfService &entry)
{ {
if (!entry.name().startsWith("nymea") || (entry.ip().isNull() && entry.ipv6().isNull())) { if (!entry->name().startsWith("nymea") || entry->ip().isNull()) {
return; return;
} }
@ -146,8 +135,8 @@ void ZeroconfDiscovery::serviceEntryRemoved(const QZeroConfService &entry)
bool sslEnabled = false; bool sslEnabled = false;
QString serverName; QString serverName;
QString version; QString version;
foreach (const QByteArray &key, entry.txt().keys()) { foreach (const QByteArray &key, entry->txt().keys()) {
QPair<QString, QString> txtRecord = qMakePair<QString, QString>(key, entry.txt().value(key)); QPair<QString, QString> txtRecord = qMakePair<QString, QString>(key, entry->txt().value(key));
if (!sslEnabled && txtRecord.first == "sslEnabled") { if (!sslEnabled && txtRecord.first == "sslEnabled") {
sslEnabled = (txtRecord.second == "true"); sslEnabled = (txtRecord.second == "true");
} }
@ -162,7 +151,7 @@ void ZeroconfDiscovery::serviceEntryRemoved(const QZeroConfService &entry)
} }
} }
// qDebug() << "Zeroconf: Service entry removed" << entry.name(); // qDebug() << "Zeroconf: Service entry removed" << entry->name();
NymeaHost* host = m_nymeaHosts->find(uuid); NymeaHost* host = m_nymeaHosts->find(uuid);
if (!host) { if (!host) {
@ -171,13 +160,13 @@ void ZeroconfDiscovery::serviceEntryRemoved(const QZeroConfService &entry)
} }
QUrl url; QUrl url;
if (entry.type() == "_jsonrpc._tcp") { if (entry->type() == "_jsonrpc._tcp") {
url.setScheme(sslEnabled ? "nymeas" : "nymea"); url.setScheme(sslEnabled ? "nymeas" : "nymea");
} else { } else {
url.setScheme(sslEnabled ? "wss" : "ws"); url.setScheme(sslEnabled ? "wss" : "ws");
} }
url.setHost(!entry.ip().isNull() ? entry.ip().toString() : entry.ipv6().toString()); url.setHost(entry->ip().toString());
url.setPort(entry.port()); url.setPort(entry->port());
Connection *connection = host->connections()->find(url); Connection *connection = host->connections()->find(url);
if (!connection){ if (!connection){
// Connection url not found... // Connection url not found...

View File

@ -255,9 +255,9 @@ void DeviceManager::getConfiguredDevicesResponse(const QVariantMap &params)
device->setStateValue(stateTypeId, value); device->setStateValue(stateTypeId, value);
// qDebug() << "Set device state value:" << device->stateValue(stateTypeId) << value; // qDebug() << "Set device state value:" << device->stateValue(stateTypeId) << value;
} }
qDebug() << "Configured Device JSON:" << qUtf8Printable(QJsonDocument::fromVariant(deviceVariant).toJson(QJsonDocument::Indented)); // qDebug() << "Configured Device JSON:" << qUtf8Printable(QJsonDocument::fromVariant(deviceVariant).toJson(QJsonDocument::Indented));
devices()->addDevice(device); devices()->addDevice(device);
qDebug() << "*** Added device:" << endl << device; // qDebug() << "*** Added device:" << endl << device;
} }
} }
m_fetchingData = false; m_fetchingData = false;