From ad75b438cb99f29157760e8250d70bf37f4877b1 Mon Sep 17 00:00:00 2001 From: Rob Emery Date: Sun, 1 Oct 2023 10:39:59 +0000 Subject: [PATCH] Implementing link priorities Now that devices can potentially be connected via both network and bluetooth simultaneously we should prioritise connections over the highest performing link (probably wifi/network). To this end the m_deviceLinks are now sorted based on priority with the fastest links first; this means that when Device::sendPacket is scheduling to send a packet, it should always use the fastest link first. --- core/backends/bluetooth/bluetoothlinkprovider.h | 5 +++++ core/backends/devicelink.cpp | 1 + core/backends/devicelink.h | 8 ++++++++ core/backends/lan/lanlinkprovider.h | 5 +++++ core/backends/linkprovider.h | 1 + core/backends/loopback/loopbacklinkprovider.h | 4 ++++ core/daemon.cpp | 2 +- core/device.cpp | 4 ++++ 8 files changed, 29 insertions(+), 1 deletion(-) diff --git a/core/backends/bluetooth/bluetoothlinkprovider.h b/core/backends/bluetooth/bluetoothlinkprovider.h index 91103b6b7..b831c926b 100644 --- a/core/backends/bluetooth/bluetoothlinkprovider.h +++ b/core/backends/bluetooth/bluetoothlinkprovider.h @@ -37,6 +37,11 @@ public: return QStringLiteral("BluetoothLinkProvider"); } + int priority() override + { + return 10; + } + public Q_SLOTS: virtual void onNetworkChange() override; virtual void onStart() override; diff --git a/core/backends/devicelink.cpp b/core/backends/devicelink.cpp index 7fa5b2571..8d7126438 100644 --- a/core/backends/devicelink.cpp +++ b/core/backends/devicelink.cpp @@ -14,6 +14,7 @@ DeviceLink::DeviceLink(const QString &deviceId, LinkProvider *parent) connect(this, &QObject::destroyed, [this, deviceId, parent]() { parent->onLinkDestroyed(deviceId, this); }); + this->priorityFromProvider = parent->priority(); } #include "moc_devicelink.cpp" diff --git a/core/backends/devicelink.h b/core/backends/devicelink.h index f01ec5a8f..3eb2596e9 100644 --- a/core/backends/devicelink.h +++ b/core/backends/devicelink.h @@ -25,10 +25,18 @@ public: return deviceInfo().id; } + int priority() const + { + return priorityFromProvider; + } + virtual bool sendPacket(NetworkPacket &np) = 0; virtual DeviceInfo deviceInfo() const = 0; +private: + int priorityFromProvider; + Q_SIGNALS: void receivedPacket(const NetworkPacket &np); }; diff --git a/core/backends/lan/lanlinkprovider.h b/core/backends/lan/lanlinkprovider.h index aae13eb10..08b3b07bc 100644 --- a/core/backends/lan/lanlinkprovider.h +++ b/core/backends/lan/lanlinkprovider.h @@ -43,6 +43,11 @@ public: return QStringLiteral("LanLinkProvider"); } + int priority() override + { + return 20; + } + void sendUdpIdentityPacket(const QList &addresses); static void configureSslSocket(QSslSocket *socket, const QString &deviceId, bool isDeviceTrusted); diff --git a/core/backends/linkprovider.h b/core/backends/linkprovider.h index 9f407ad46..eeddd1d07 100644 --- a/core/backends/linkprovider.h +++ b/core/backends/linkprovider.h @@ -22,6 +22,7 @@ public: LinkProvider(); virtual QString name() = 0; + virtual int priority() = 0; public Q_SLOTS: virtual void onStart() = 0; diff --git a/core/backends/loopback/loopbacklinkprovider.h b/core/backends/loopback/loopbacklinkprovider.h index b33e657e5..d6e82b888 100644 --- a/core/backends/loopback/loopbacklinkprovider.h +++ b/core/backends/loopback/loopbacklinkprovider.h @@ -22,6 +22,10 @@ public: { return QStringLiteral("LoopbackLinkProvider"); } + int priority() override + { + return 0; + } void onStart() override; void onStop() override; diff --git a/core/daemon.cpp b/core/daemon.cpp index 94a92f98c..fb6b3d3a7 100644 --- a/core/daemon.cpp +++ b/core/daemon.cpp @@ -158,7 +158,7 @@ void Daemon::onNewDeviceLink(DeviceLink *link) { QString id = link->deviceId(); - // qCDebug(KDECONNECT_CORE) << "Device discovered" << id << "via" << dl->name(); + qCDebug(KDECONNECT_CORE) << "Device discovered" << id << "via link with priority" << link->priority(); if (d->m_devices.contains(id)) { qCDebug(KDECONNECT_CORE) << "It is a known device" << link->deviceInfo().name; diff --git a/core/device.cpp b/core/device.cpp index a11ca659b..c9bb1fa3b 100644 --- a/core/device.cpp +++ b/core/device.cpp @@ -276,6 +276,10 @@ void Device::addLink(DeviceLink *link) d->m_deviceLinks.append(link); + std::sort(d->m_deviceLinks.begin(), d->m_deviceLinks.end(), [](DeviceLink *a, DeviceLink *b) { + return a->priority() > b->priority(); + }); + connect(link, &QObject::destroyed, this, &Device::linkDestroyed); connect(link, &DeviceLink::receivedPacket, this, &Device::privateReceivedPacket);