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()
: identityPackage(PACKAGE_TYPE_IDENTITY)
{
loopbackDeviceLink = 0;
NetworkPackage::createIdentityPackage(&identityPackage);
}
@ -55,7 +54,6 @@ void LoopbackLinkProvider::onStop()
{
if (loopbackDeviceLink) {
delete loopbackDeviceLink;
loopbackDeviceLink = 0;
}
}

View file

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

View file

@ -44,6 +44,7 @@ struct DaemonPrivate
//Every known device
QMap<QString, Device*> mDevices;
bool discoveryMode = false;
};
Daemon* Daemon::instance()
@ -80,8 +81,8 @@ Daemon::Daemon(QObject *parent, bool testMode)
Q_FOREACH (LinkProvider* a, d->mLinkProviders) {
connect(a, SIGNAL(onConnectionReceived(NetworkPackage,DeviceLink*)),
this, SLOT(onNewDeviceLink(NetworkPackage,DeviceLink*)));
a->onStart();
}
setDiscoveryEnabled(true);
//Register on DBus
QDBusConnection::sessionBus().registerService("org.kde.kdeconnect");
@ -92,11 +93,36 @@ Daemon::Daemon(QObject *parent, bool testMode)
void Daemon::setDiscoveryEnabled(bool b)
{
Q_FOREACH (LinkProvider* a, d->mLinkProviders) {
if (b)
a->onStart();
else
a->onStop();
// qDebug() << "setting discover..." << b;
if (b == d->discoveryMode)
return;
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);
}
} else {
Device* device = new Device(this, identityPackage, dl);
//qCDebug(KDECONNECT_CORE) << "It is a new device";
Device* device = new Device(this, identityPackage, dl);
connect(device, SIGNAL(reachableStatusChanged()), this, SLOT(onDeviceStatusChanged()));
connect(device, SIGNAL(pairingChanged(bool)), this, SLOT(onDeviceStatusChanged()));
d->mDevices[id] = device;
if (d->discoveryMode && !device->isPaired()) {
device->deleteLater();
} else {
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()
{
Device* device = (Device*)sender();
QString id = device->id();
qCDebug(KDECONNECT_CORE) << "Device" << device->name() << "status changed. Reachable:" << device->isReachable() << ". Paired: " << device->isPaired();
if (!device->isReachable() && !device->isPaired()) {
qCDebug(KDECONNECT_CORE) << "Destroying device" << device->name();
d->mDevices.remove(id);
device->deleteLater();
Q_EMIT deviceRemoved(id);
removeDevice(device);
} 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_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.daemon")
Q_PROPERTY(bool discoveryEnabled READ isDiscoveryEnabled WRITE setDiscoveryEnabled)
public:
explicit Daemon(QObject *parent, bool testMode = false);
@ -49,8 +50,9 @@ public:
*/
static Daemon* instance();
//After calling this, signal deviceDiscovered will be triggered for each device
Q_SCRIPTABLE void setDiscoveryEnabled(bool b);
bool isDiscoveryEnabled() const;
void setDiscoveryEnabled(bool b);
QList<Device*> devicesList() const;
virtual void requestPairing(Device *d) = 0;
@ -78,6 +80,9 @@ private Q_SLOTS:
void onDeviceStatusChanged();
private:
void removeDevice(Device* d);
void cleanDevices();
QScopedPointer<struct DaemonPrivate> d;
};

View file

@ -93,7 +93,7 @@ Device::Device(QObject* parent, const NetworkPackage& identityPackage, DeviceLin
Device::~Device()
{
qDeleteAll(m_deviceLinks);
}
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()),
this, SLOT(renameShow()));
daemon->setDiscoveryEnabled(true);
}
void KdeConnectKcm::renameShow()
@ -133,6 +134,7 @@ void KdeConnectKcm::setRenameMode(bool b) {
KdeConnectKcm::~KdeConnectKcm()
{
daemon->setDiscoveryEnabled(false);
delete kcmUi;
}