From 3e2f82e5a6275ab8262ef7637d9e07aa262b0611 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Tue, 18 Jun 2019 17:58:59 +0200 Subject: [PATCH] Also handle loopback connections in bearer management --- .../connection/discovery/nymeadiscovery.cpp | 4 ++ .../connection/discovery/upnpdiscovery.cpp | 3 +- .../discovery/zeroconfdiscovery.cpp | 3 +- .../connection/nymeaconnection.cpp | 53 +++++++++++++------ .../connection/nymeaconnection.h | 2 + libnymea-app-core/connection/nymeahost.h | 1 + libnymea-app-core/connection/nymeahosts.cpp | 3 ++ nymea-app/ui/MainPage.qml | 2 + 8 files changed, 53 insertions(+), 18 deletions(-) diff --git a/libnymea-app-core/connection/discovery/nymeadiscovery.cpp b/libnymea-app-core/connection/discovery/nymeadiscovery.cpp index 274adb6b..21332053 100644 --- a/libnymea-app-core/connection/discovery/nymeadiscovery.cpp +++ b/libnymea-app-core/connection/discovery/nymeadiscovery.cpp @@ -124,6 +124,10 @@ void NymeaDiscovery::cacheHost(NymeaHost *host) if (remoteConnection) { connections.append(remoteConnection); } + Connection *loopbackConnection = host->connections()->bestMatch(Connection::BearerTypeLoopback); + if (loopbackConnection) { + connections.append(loopbackConnection); + } Connection *lanConnection = host->connections()->bestMatch(Connection::BearerTypeLan); if (lanConnection) { connections.append(lanConnection); diff --git a/libnymea-app-core/connection/discovery/upnpdiscovery.cpp b/libnymea-app-core/connection/discovery/upnpdiscovery.cpp index 90ef777e..03dffc62 100644 --- a/libnymea-app-core/connection/discovery/upnpdiscovery.cpp +++ b/libnymea-app-core/connection/discovery/upnpdiscovery.cpp @@ -254,7 +254,8 @@ void UpnpDiscovery::networkReplyFinished(QNetworkReply *reply) qDebug() << "UPnP: Adding new connection to host:" << device->name() << url; bool sslEnabled = url.scheme() == "nymeas" || url.scheme() == "wss"; QString displayName = QString("%1:%2").arg(url.host()).arg(url.port()); - Connection *conn = new Connection(url, Connection::BearerTypeLan, sslEnabled, displayName); + Connection::BearerType bearerType = QHostAddress(url.host()).isLoopback() ? Connection::BearerTypeLoopback : Connection::BearerTypeLan; + Connection *conn = new Connection(url, bearerType, sslEnabled, displayName); conn->setOnline(true); device->connections()->addConnection(conn); } diff --git a/libnymea-app-core/connection/discovery/zeroconfdiscovery.cpp b/libnymea-app-core/connection/discovery/zeroconfdiscovery.cpp index 51337163..4b1f8f90 100644 --- a/libnymea-app-core/connection/discovery/zeroconfdiscovery.cpp +++ b/libnymea-app-core/connection/discovery/zeroconfdiscovery.cpp @@ -126,7 +126,8 @@ void ZeroconfDiscovery::serviceEntryAdded(const QZeroConfService &entry) if (!connection) { // qDebug() << "Zeroconf: Adding new connection to host:" << host->name() << url.toString(); QString displayName = QString("%1:%2").arg(url.host()).arg(url.port()); - connection = new Connection(url, Connection::BearerTypeLan, sslEnabled, displayName); + Connection::BearerType bearerType = QHostAddress(url.host()).isLoopback() ? Connection::BearerTypeLoopback : Connection::BearerTypeLan; + connection = new Connection(url, bearerType, sslEnabled, displayName); connection->setOnline(true); host->connections()->addConnection(connection); } else { diff --git a/libnymea-app-core/connection/nymeaconnection.cpp b/libnymea-app-core/connection/nymeaconnection.cpp index 85191214..65561dee 100644 --- a/libnymea-app-core/connection/nymeaconnection.cpp +++ b/libnymea-app-core/connection/nymeaconnection.cpp @@ -387,10 +387,10 @@ void NymeaConnection::onDisconnected() void NymeaConnection::updateActiveBearers() { NymeaConnection::BearerTypes availableBearerTypes; - QList configs = m_networkConfigManager->allConfigurations(QNetworkConfiguration::Active); -// qDebug() << "Network configuations:" << configs.count(); + QList configs = m_networkConfigManager->allConfigurations(); + qDebug() << "Network configuations:" << configs.count(); foreach (const QNetworkConfiguration &config, configs) { -// qDebug() << "Candidate network config:" << config.name() << config.bearerTypeFamily() << config.bearerTypeName(); + qDebug() << "Candidate network config:" << config.name() << config.bearerTypeFamily() << config.bearerTypeName(); // NOTE: iOS doesn't correctly report bearer types. It'll be Unknown all the time. Let's hardcode it to WiFi for that... #if defined(Q_OS_IOS) @@ -399,7 +399,6 @@ void NymeaConnection::updateActiveBearers() availableBearerTypes.setFlag(qBearerTypeToNymeaBearerType(config.bearerType())); #endif } -// qDebug() << "Available bearers:" << availableBearerTypes; if (m_availableBearerTypes != availableBearerTypes) { qDebug() << "Available Bearer Types changed:" << availableBearerTypes; m_availableBearerTypes = availableBearerTypes; @@ -487,21 +486,21 @@ void NymeaConnection::connect(NymeaHost *nymeaHost, Connection *connection) void NymeaConnection::connectInternal(NymeaHost *host) { - qDebug() << "Connecting. Available bearer types:" << m_availableBearerTypes; - if (m_availableBearerTypes == NymeaConnection::BearerTypeNone) { - qDebug() << "No available bearer. Not connecting..."; - m_connectionStatus = ConnectionStatusNoBearerAvailable; - emit connectionStatusChanged(); - return; - } - if (m_preferredConnection) { - qDebug() << "Preferred connection is set. Using" << m_preferredConnection->url(); - connectInternal(m_preferredConnection); - return; + if (isConnectionBearerAvailable(m_preferredConnection->bearerType())) { + qDebug() << "Preferred connection is set. Using" << m_preferredConnection->url(); + connectInternal(m_preferredConnection); + return; + } + qDebug() << "Preferred connection set but no bearer available for it."; } - if (m_availableBearerTypes.testFlag(NymeaConnection::BearerTypeWiFi) + Connection *loopbackConnection = host->connections()->bestMatch(Connection::BearerTypeLoopback); + if (loopbackConnection) { + qDebug() << "Best candidate Loopback connection:" << loopbackConnection->url(); + connectInternal(loopbackConnection); + + } else if (m_availableBearerTypes.testFlag(NymeaConnection::BearerTypeWiFi) || m_availableBearerTypes.testFlag(NymeaConnection::BearerTypeEthernet)) { Connection* lanConnection = host->connections()->bestMatch(Connection::BearerTypeLan | Connection::BearerTypeWan); if (lanConnection) { @@ -510,6 +509,7 @@ void NymeaConnection::connectInternal(NymeaHost *host) } else { qDebug() << "No available LAN/WAN connection to" << host->name(); } + } else if (m_availableBearerTypes.testFlag(NymeaConnection::BearerTypeMobileData)) { Connection* wanConnection = host->connections()->bestMatch(Connection::BearerTypeWan); if (wanConnection) { @@ -597,6 +597,27 @@ NymeaConnection::BearerType NymeaConnection::qBearerTypeToNymeaBearerType(QNetwo return BearerTypeAll; } +bool NymeaConnection::isConnectionBearerAvailable(Connection::BearerType connectionBearerType) const +{ + switch (connectionBearerType) { + case Connection::BearerTypeLan: + return m_availableBearerTypes.testFlag(BearerTypeEthernet) + || m_availableBearerTypes.testFlag(BearerTypeWiFi); + case Connection::BearerTypeWan: + case Connection::BearerTypeCloud: + return m_availableBearerTypes.testFlag(BearerTypeEthernet) + || m_availableBearerTypes.testFlag(BearerTypeWiFi) + || m_availableBearerTypes.testFlag(BearerTypeMobileData); + case Connection::BearerTypeBluetooth: + return m_availableBearerTypes.testFlag(BearerTypeBluetooth); + case Connection::BearerTypeUnknown: + return true; + case Connection::BearerTypeNone: + return false; + } + return false; +} + void NymeaConnection::disconnect() { setCurrentHost(nullptr); diff --git a/libnymea-app-core/connection/nymeaconnection.h b/libnymea-app-core/connection/nymeaconnection.h index e5718a94..34bbf4ba 100644 --- a/libnymea-app-core/connection/nymeaconnection.h +++ b/libnymea-app-core/connection/nymeaconnection.h @@ -98,6 +98,8 @@ private: NymeaConnection::BearerType qBearerTypeToNymeaBearerType(QNetworkConfiguration::BearerType type) const; + bool isConnectionBearerAvailable(Connection::BearerType connectionBearerType) const; + private: ConnectionStatus m_connectionStatus = ConnectionStatusUnconnected; QNetworkConfigurationManager *m_networkConfigManager = nullptr; diff --git a/libnymea-app-core/connection/nymeahost.h b/libnymea-app-core/connection/nymeahost.h index cd1faae4..d2fed740 100644 --- a/libnymea-app-core/connection/nymeahost.h +++ b/libnymea-app-core/connection/nymeahost.h @@ -46,6 +46,7 @@ public: BearerTypeWan = 0x02, BearerTypeCloud = 0x04, BearerTypeBluetooth = 0x08, + BearerTypeLoopback = 0x10, BearerTypeUnknown = 0xFF, BearerTypeAll = 0xFF }; diff --git a/libnymea-app-core/connection/nymeahosts.cpp b/libnymea-app-core/connection/nymeahosts.cpp index f90c338e..e9d9098e 100644 --- a/libnymea-app-core/connection/nymeahosts.cpp +++ b/libnymea-app-core/connection/nymeahosts.cpp @@ -91,6 +91,9 @@ NymeaHost *NymeaHosts::createCloudHost(const QString &name, const QUrl &url) NymeaHost *NymeaHosts::createLanHost(const QString &name, const QUrl &url) { + if (QHostAddress(url.host()).isLoopback()) { + return createHost(name, url, Connection::BearerTypeLoopback); + } return createHost(name, url, Connection::BearerTypeLan); } diff --git a/nymea-app/ui/MainPage.qml b/nymea-app/ui/MainPage.qml index ee387cb1..0a48cfd8 100644 --- a/nymea-app/ui/MainPage.qml +++ b/nymea-app/ui/MainPage.qml @@ -26,6 +26,8 @@ Page { return "../images/network-wifi.svg"; case Connection.BearerTypeCloud: return "../images/cloud.svg" + case Connection.BearerTypeLoopback: + return "qrc:/styles/%1/logo.svg".arg(styleController.currentStyle) } return "" }