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.
This commit is contained in:
Rob Emery 2023-10-01 10:39:59 +00:00 committed by Albert Vaca Cintora
parent 2d770b780a
commit ad75b438cb
8 changed files with 29 additions and 1 deletions

View file

@ -37,6 +37,11 @@ public:
return QStringLiteral("BluetoothLinkProvider"); return QStringLiteral("BluetoothLinkProvider");
} }
int priority() override
{
return 10;
}
public Q_SLOTS: public Q_SLOTS:
virtual void onNetworkChange() override; virtual void onNetworkChange() override;
virtual void onStart() override; virtual void onStart() override;

View file

@ -14,6 +14,7 @@ DeviceLink::DeviceLink(const QString &deviceId, LinkProvider *parent)
connect(this, &QObject::destroyed, [this, deviceId, parent]() { connect(this, &QObject::destroyed, [this, deviceId, parent]() {
parent->onLinkDestroyed(deviceId, this); parent->onLinkDestroyed(deviceId, this);
}); });
this->priorityFromProvider = parent->priority();
} }
#include "moc_devicelink.cpp" #include "moc_devicelink.cpp"

View file

@ -25,10 +25,18 @@ public:
return deviceInfo().id; return deviceInfo().id;
} }
int priority() const
{
return priorityFromProvider;
}
virtual bool sendPacket(NetworkPacket &np) = 0; virtual bool sendPacket(NetworkPacket &np) = 0;
virtual DeviceInfo deviceInfo() const = 0; virtual DeviceInfo deviceInfo() const = 0;
private:
int priorityFromProvider;
Q_SIGNALS: Q_SIGNALS:
void receivedPacket(const NetworkPacket &np); void receivedPacket(const NetworkPacket &np);
}; };

View file

@ -43,6 +43,11 @@ public:
return QStringLiteral("LanLinkProvider"); return QStringLiteral("LanLinkProvider");
} }
int priority() override
{
return 20;
}
void sendUdpIdentityPacket(const QList<QHostAddress> &addresses); void sendUdpIdentityPacket(const QList<QHostAddress> &addresses);
static void configureSslSocket(QSslSocket *socket, const QString &deviceId, bool isDeviceTrusted); static void configureSslSocket(QSslSocket *socket, const QString &deviceId, bool isDeviceTrusted);

View file

@ -22,6 +22,7 @@ public:
LinkProvider(); LinkProvider();
virtual QString name() = 0; virtual QString name() = 0;
virtual int priority() = 0;
public Q_SLOTS: public Q_SLOTS:
virtual void onStart() = 0; virtual void onStart() = 0;

View file

@ -22,6 +22,10 @@ public:
{ {
return QStringLiteral("LoopbackLinkProvider"); return QStringLiteral("LoopbackLinkProvider");
} }
int priority() override
{
return 0;
}
void onStart() override; void onStart() override;
void onStop() override; void onStop() override;

View file

@ -158,7 +158,7 @@ void Daemon::onNewDeviceLink(DeviceLink *link)
{ {
QString id = link->deviceId(); 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)) { if (d->m_devices.contains(id)) {
qCDebug(KDECONNECT_CORE) << "It is a known device" << link->deviceInfo().name; qCDebug(KDECONNECT_CORE) << "It is a known device" << link->deviceInfo().name;

View file

@ -276,6 +276,10 @@ void Device::addLink(DeviceLink *link)
d->m_deviceLinks.append(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, &QObject::destroyed, this, &Device::linkDestroyed);
connect(link, &DeviceLink::receivedPacket, this, &Device::privateReceivedPacket); connect(link, &DeviceLink::receivedPacket, this, &Device::privateReceivedPacket);