Only keep connections alive with unpaired devices when discovery is enabled

At the moment, we were keeping the connection alive with every reachable
device. While this works optimally for most use-cases, on networks with
several devices with KDE Connect, the amount of connections grows
exponentially.

Reviewed by Albert Vaca

CCBUG: 352424
This commit is contained in:
Aleix Pol 2015-09-08 09:30:55 +02:00
parent 774893d3f9
commit 4023bf0599
6 changed files with 55 additions and 22 deletions

View file

@ -25,7 +25,6 @@
LoopbackLinkProvider::LoopbackLinkProvider() LoopbackLinkProvider::LoopbackLinkProvider()
: identityPackage(PACKAGE_TYPE_IDENTITY) : identityPackage(PACKAGE_TYPE_IDENTITY)
{ {
loopbackDeviceLink = 0;
NetworkPackage::createIdentityPackage(&identityPackage); NetworkPackage::createIdentityPackage(&identityPackage);
} }
@ -55,7 +54,6 @@ void LoopbackLinkProvider::onStop()
{ {
if (loopbackDeviceLink) { if (loopbackDeviceLink) {
delete loopbackDeviceLink; delete loopbackDeviceLink;
loopbackDeviceLink = 0;
} }
} }

View file

@ -23,6 +23,7 @@
#include "../linkprovider.h" #include "../linkprovider.h"
#include "loopbackdevicelink.h" #include "loopbackdevicelink.h"
#include <QPointer>
class LoopbackLinkProvider class LoopbackLinkProvider
: public LinkProvider : public LinkProvider
@ -40,7 +41,7 @@ public:
virtual void onNetworkChange(); virtual void onNetworkChange();
private: private:
LoopbackDeviceLink* loopbackDeviceLink; QPointer<LoopbackDeviceLink> loopbackDeviceLink;
NetworkPackage identityPackage; NetworkPackage identityPackage;
}; };

View file

@ -44,6 +44,7 @@ struct DaemonPrivate
//Every known device //Every known device
QMap<QString, Device*> mDevices; QMap<QString, Device*> mDevices;
bool discoveryMode = false;
}; };
Daemon* Daemon::instance() Daemon* Daemon::instance()
@ -80,8 +81,8 @@ Daemon::Daemon(QObject *parent, bool testMode)
Q_FOREACH (LinkProvider* a, d->mLinkProviders) { Q_FOREACH (LinkProvider* a, d->mLinkProviders) {
connect(a, SIGNAL(onConnectionReceived(NetworkPackage,DeviceLink*)), connect(a, SIGNAL(onConnectionReceived(NetworkPackage,DeviceLink*)),
this, SLOT(onNewDeviceLink(NetworkPackage,DeviceLink*))); this, SLOT(onNewDeviceLink(NetworkPackage,DeviceLink*)));
a->onStart();
} }
setDiscoveryEnabled(true);
//Register on DBus //Register on DBus
QDBusConnection::sessionBus().registerService("org.kde.kdeconnect"); QDBusConnection::sessionBus().registerService("org.kde.kdeconnect");
@ -92,11 +93,36 @@ Daemon::Daemon(QObject *parent, bool testMode)
void Daemon::setDiscoveryEnabled(bool b) void Daemon::setDiscoveryEnabled(bool b)
{ {
Q_FOREACH (LinkProvider* a, d->mLinkProviders) { // qDebug() << "setting discover..." << b;
if (b) if (b == d->discoveryMode)
a->onStart(); return;
else
a->onStop(); d->discoveryMode = b;
if (b) {
forceOnNetworkChange();
} else {
cleanDevices();
}
}
bool Daemon::isDiscoveryEnabled() const
{
return d->discoveryMode;
}
void Daemon::removeDevice(Device* device)
{
d->mDevices.remove(device->id());
device->deleteLater();
Q_EMIT deviceRemoved(device->id());
}
void Daemon::cleanDevices()
{
Q_FOREACH(Device* device, d->mDevices) {
if (!device->isPaired()) {
removeDevice(device);
}
} }
} }
@ -134,31 +160,32 @@ void Daemon::onNewDeviceLink(const NetworkPackage& identityPackage, DeviceLink*
Q_EMIT deviceVisibilityChanged(id, true); Q_EMIT deviceVisibilityChanged(id, true);
} }
} else { } else {
Device* device = new Device(this, identityPackage, dl);
//qCDebug(KDECONNECT_CORE) << "It is a new device"; //qCDebug(KDECONNECT_CORE) << "It is a new device";
Device* device = new Device(this, identityPackage, dl); if (d->discoveryMode && !device->isPaired()) {
connect(device, SIGNAL(reachableStatusChanged()), this, SLOT(onDeviceStatusChanged())); device->deleteLater();
connect(device, SIGNAL(pairingChanged(bool)), this, SLOT(onDeviceStatusChanged())); } else {
d->mDevices[id] = device; connect(device, SIGNAL(reachableStatusChanged()), this, SLOT(onDeviceStatusChanged()));
connect(device, SIGNAL(pairingChanged(bool)), this, SLOT(onDeviceStatusChanged()));
d->mDevices[id] = device;
Q_EMIT deviceAdded(id); Q_EMIT deviceAdded(id);
}
} }
} }
void Daemon::onDeviceStatusChanged() void Daemon::onDeviceStatusChanged()
{ {
Device* device = (Device*)sender(); Device* device = (Device*)sender();
QString id = device->id();
qCDebug(KDECONNECT_CORE) << "Device" << device->name() << "status changed. Reachable:" << device->isReachable() << ". Paired: " << device->isPaired(); qCDebug(KDECONNECT_CORE) << "Device" << device->name() << "status changed. Reachable:" << device->isReachable() << ". Paired: " << device->isPaired();
if (!device->isReachable() && !device->isPaired()) { if (!device->isReachable() && !device->isPaired()) {
qCDebug(KDECONNECT_CORE) << "Destroying device" << device->name(); qCDebug(KDECONNECT_CORE) << "Destroying device" << device->name();
d->mDevices.remove(id); removeDevice(device);
device->deleteLater();
Q_EMIT deviceRemoved(id);
} else { } else {
Q_EMIT deviceVisibilityChanged(id, device->isReachable()); Q_EMIT deviceVisibilityChanged(device->id(), device->isReachable());
} }
} }

View file

@ -37,6 +37,7 @@ class KDECONNECTCORE_EXPORT Daemon
{ {
Q_OBJECT Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.daemon") Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.daemon")
Q_PROPERTY(bool discoveryEnabled READ isDiscoveryEnabled WRITE setDiscoveryEnabled)
public: public:
explicit Daemon(QObject *parent, bool testMode = false); explicit Daemon(QObject *parent, bool testMode = false);
@ -49,8 +50,9 @@ public:
*/ */
static Daemon* instance(); static Daemon* instance();
//After calling this, signal deviceDiscovered will be triggered for each device bool isDiscoveryEnabled() const;
Q_SCRIPTABLE void setDiscoveryEnabled(bool b); void setDiscoveryEnabled(bool b);
QList<Device*> devicesList() const; QList<Device*> devicesList() const;
virtual void requestPairing(Device *d) = 0; virtual void requestPairing(Device *d) = 0;
@ -78,6 +80,9 @@ private Q_SLOTS:
void onDeviceStatusChanged(); void onDeviceStatusChanged();
private: private:
void removeDevice(Device* d);
void cleanDevices();
QScopedPointer<struct DaemonPrivate> d; QScopedPointer<struct DaemonPrivate> d;
}; };

View file

@ -93,7 +93,7 @@ Device::Device(QObject* parent, const NetworkPackage& identityPackage, DeviceLin
Device::~Device() Device::~Device()
{ {
qDeleteAll(m_deviceLinks);
} }
bool Device::hasPlugin(const QString& name) const bool Device::hasPlugin(const QString& name) const

View file

@ -104,6 +104,7 @@ KdeConnectKcm::KdeConnectKcm(QWidget *parent, const QVariantList&)
connect(kcmUi->renameShow_button,SIGNAL(clicked()), connect(kcmUi->renameShow_button,SIGNAL(clicked()),
this, SLOT(renameShow())); this, SLOT(renameShow()));
daemon->setDiscoveryEnabled(true);
} }
void KdeConnectKcm::renameShow() void KdeConnectKcm::renameShow()
@ -133,6 +134,7 @@ void KdeConnectKcm::setRenameMode(bool b) {
KdeConnectKcm::~KdeConnectKcm() KdeConnectKcm::~KdeConnectKcm()
{ {
daemon->setDiscoveryEnabled(false);
delete kcmUi; delete kcmUi;
} }