Moved ConnectionStarted from DeviceLink to LanDeviceLink

This commit is contained in:
Albert Vaca 2016-01-10 07:12:13 -08:00
parent 1d98bd3cbe
commit aa4150f0c9
11 changed files with 51 additions and 37 deletions

View file

@ -22,11 +22,10 @@
#include "kdeconnectconfig.h"
#include "linkprovider.h"
DeviceLink::DeviceLink(const QString& deviceId, LinkProvider* parent, ConnectionStarted connectionSource)
DeviceLink::DeviceLink(const QString& deviceId, LinkProvider* parent)
: QObject(parent)
, mPrivateKey(KdeConnectConfig::instance()->privateKey())
, mDeviceId(deviceId)
, mConnectionSource(connectionSource)
, mLinkProvider(parent)
, mPairStatus(NotPaired)
{

View file

@ -38,9 +38,8 @@ class DeviceLink
public:
enum PairStatus : bool { NotPaired, Paired };
enum ConnectionStarted : bool { Locally, Remotely };
DeviceLink(const QString& deviceId, LinkProvider* parent, ConnectionStarted connectionSource);
DeviceLink(const QString& deviceId, LinkProvider* parent);
virtual ~DeviceLink() { };
virtual QString name() = 0;
@ -54,12 +53,12 @@ public:
virtual void userRequestsPair() = 0;
virtual void userRequestsUnpair() = 0;
ConnectionStarted connectionSource() const { return mConnectionSource; } //TODO: Move this down to landevicelink and create an abstraction like "bool keepConnectionOpen()" here.
void setConnectionSource(ConnectionStarted source) { mConnectionSource = source; }
PairStatus pairStatus() const { return mPairStatus; }
virtual void setPairStatus(PairStatus status);
//The daemon will periodically destroy unpaired links if this returns false
virtual bool linkShouldBeKeptAlive() { return false; }
Q_SIGNALS:
void receivedPackage(const NetworkPackage& np);
void pairStatusChanged(DeviceLink::PairStatus status);
@ -70,7 +69,6 @@ protected:
private:
const QString mDeviceId;
ConnectionStarted mConnectionSource;
LinkProvider* mLinkProvider;
PairStatus mPairStatus;

View file

@ -30,13 +30,13 @@
#include "lanlinkprovider.h"
LanDeviceLink::LanDeviceLink(const QString& deviceId, LinkProvider* parent, QSslSocket* socket, ConnectionStarted connectionSource)
: DeviceLink(deviceId, parent, connectionSource)
: DeviceLink(deviceId, parent)
, mSocketLineReader(nullptr)
{
reset(socket, connectionSource);
}
void LanDeviceLink::reset(QSslSocket* socket, DeviceLink::ConnectionStarted connectionSource)
void LanDeviceLink::reset(QSslSocket* socket, ConnectionStarted connectionSource)
{
if (mSocketLineReader) {
delete mSocketLineReader;
@ -53,7 +53,7 @@ void LanDeviceLink::reset(QSslSocket* socket, DeviceLink::ConnectionStarted conn
//destroyed as well
socket->setParent(this);
setConnectionSource(connectionSource);
mConnectionSource = connectionSource;
QString certString = KdeConnectConfig::instance()->getDeviceProperty(deviceId(), "certificate");
DeviceLink::setPairStatus(certString.isEmpty()? PairStatus::NotPaired : PairStatus::Paired);
@ -148,3 +148,8 @@ void LanDeviceLink::setPairStatus(PairStatus status)
}
}
bool LanDeviceLink::linkShouldBeKeptAlive() {
//We keep the remotely initiated connections, since the remotes require them if they want to request
//pairing to us, or connections that are already paired. TODO: Keep connections in the process of pairing
return (mConnectionSource == ConnectionStarted::Remotely || pairStatus() == Paired);
}

View file

@ -37,6 +37,8 @@ class LanDeviceLink
Q_OBJECT
public:
enum ConnectionStarted : bool { Locally, Remotely };
LanDeviceLink(const QString& deviceId, LinkProvider* parent, QSslSocket* socket, ConnectionStarted connectionSource);
void reset(QSslSocket* socket, ConnectionStarted connectionSource);
@ -49,11 +51,14 @@ public:
virtual void setPairStatus(PairStatus status) override;
virtual bool linkShouldBeKeptAlive() override;
private Q_SLOTS:
void dataReceived();
private:
SocketLineReader* mSocketLineReader;
ConnectionStarted mConnectionSource;
};
#endif

View file

@ -235,7 +235,7 @@ void LanLinkProvider::connected()
return; // Return statement prevents from deleting received package, needed in slot "encrypted"
} else {
qWarning() << "Incompatible protocol version, this won't work";
addLink(deviceId, socket, receivedPackage, DeviceLink::Remotely);
addLink(deviceId, socket, receivedPackage, LanDeviceLink::Remotely);
}
} else {
@ -264,7 +264,7 @@ void LanLinkProvider::encrypted()
const QString& deviceId = receivedPackage->get<QString>("deviceId");
//qCDebug(KDECONNECT_CORE) << "Connected" << socket->isWritable();
addLink(deviceId, socket, receivedPackage, DeviceLink::Remotely);
addLink(deviceId, socket, receivedPackage, LanDeviceLink::Remotely);
// Copied from connected slot, now delete received package
delete receivedPackage;
@ -378,7 +378,7 @@ void LanLinkProvider::dataReceived()
socket->startClientEncryption();
return;
} else {
addLink(deviceId, socket, np, DeviceLink::Locally);
addLink(deviceId, socket, np, LanDeviceLink::Locally);
}
delete np;
@ -431,7 +431,7 @@ void LanLinkProvider::configureSocket(QSslSocket* socket)
}
void LanLinkProvider::addLink(const QString& deviceId, QSslSocket* socket, NetworkPackage* receivedPackage, DeviceLink::ConnectionStarted connectionOrigin)
void LanLinkProvider::addLink(const QString& deviceId, QSslSocket* socket, NetworkPackage* receivedPackage, LanDeviceLink::ConnectionStarted connectionOrigin)
{
// Socket disconnection will now be handled by LanDeviceLink
disconnect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));

View file

@ -67,7 +67,7 @@ private:
LanPairingHandler* createPairingHandler(DeviceLink* link);
void onNetworkConfigurationChanged(const QNetworkConfiguration &config);
void addLink(const QString& deviceId, QSslSocket* socket, NetworkPackage* receivedPackage, DeviceLink::ConnectionStarted connectionOrigin);
void addLink(const QString& deviceId, QSslSocket* socket, NetworkPackage* receivedPackage, LanDeviceLink::ConnectionStarted connectionOrigin);
Server* mServer;
QUdpSocket* mUdpServer;

View file

@ -24,14 +24,14 @@
#include "loopbackpairinghandler.h"
LoopbackDeviceLink::LoopbackDeviceLink(const QString& deviceId, LoopbackLinkProvider* provider)
: DeviceLink(deviceId, provider, Remotely)
: DeviceLink(deviceId, provider)
{
}
QString LoopbackDeviceLink::name()
{
return "LoopbackLink"; // Should be similar to android
return "LoopbackLink";
}
bool LoopbackDeviceLink::sendPackage(NetworkPackage& input)

View file

@ -123,7 +123,12 @@ void Daemon::removeDevice(Device* device)
void Daemon::cleanDevices()
{
Q_FOREACH(Device* device, d->mDevices) {
if (!device->isTrusted() && device->connectionSource() == DeviceLink::ConnectionStarted::Remotely) {
if (device->isTrusted()) {
continue;
}
device->cleanUnneededLinks();
//If there are no links remaining
if (!device->isReachable()) {
removeDevice(device);
}
}
@ -178,8 +183,7 @@ void Daemon::onNewDeviceLink(const NetworkPackage& identityPackage, DeviceLink*
Device* device = new Device(this, identityPackage, dl);
//we discard the connections that we created but it's not paired.
//we keep the remotely initiated ones, since the remotes require them
if (!isDiscoveringDevices() && !device->isTrusted() && dl->connectionSource() == DeviceLink::ConnectionStarted::Locally) {
if (!isDiscoveringDevices() && !device->isTrusted() && !dl->linkShouldBeKeptAlive()) {
device->deleteLater();
} else {
connect(device, SIGNAL(reachableStatusChanged()), this, SLOT(onDeviceStatusChanged()));

View file

@ -345,18 +345,6 @@ bool Device::isTrusted() const
return KdeConnectConfig::instance()->trustedDevices().contains(id());
}
DeviceLink::ConnectionStarted Device::connectionSource() const
{
DeviceLink::ConnectionStarted ret = DeviceLink::Remotely;
Q_FOREACH(DeviceLink* link, m_deviceLinks) {
if(link->connectionSource() == DeviceLink::ConnectionStarted::Locally) {
ret = DeviceLink::ConnectionStarted::Locally;
break;
}
}
return ret;
}
QStringList Device::availableLinks() const
{
QStringList sl;
@ -366,6 +354,21 @@ QStringList Device::availableLinks() const
return sl;
}
void Device::cleanUnneededLinks() {
if (isTrusted()) {
return;
}
for(int i = 0; i < m_deviceLinks.size(); ) {
DeviceLink* dl = m_deviceLinks[i];
if (!dl->linkShouldBeKeptAlive()) {
dl->deleteLater();
m_deviceLinks.remove(i);
} else {
i++;
}
}
}
Device::DeviceType Device::str2type(const QString &deviceType) {
if (deviceType == "desktop") return Desktop;
if (deviceType == "laptop") return Laptop;
@ -439,7 +442,6 @@ bool Device::isPluginEnabled(const QString& pluginName) const
: PluginLoader::instance()->getPluginInfo(pluginName).isEnabledByDefault());
}
//HACK
QString Device::encryptionInfo() const
{
QString result;

View file

@ -107,7 +107,7 @@ public:
void setPluginEnabled(const QString& pluginName, bool enabled);
bool isPluginEnabled(const QString& pluginName) const;
DeviceLink::ConnectionStarted connectionSource() const;
void cleanUnneededLinks();
public Q_SLOTS:
///sends a @p np package to the device

View file

@ -75,7 +75,7 @@ void DeviceTest::testPairedDevice()
// Add link
LanLinkProvider linkProvider;
QSslSocket socket;
LanDeviceLink* link = new LanDeviceLink(deviceId, &linkProvider, &socket, DeviceLink::Locally);
LanDeviceLink* link = new LanDeviceLink(deviceId, &linkProvider, &socket, LanDeviceLink::Locally);
device.addLink(*identityPackage, link);
QCOMPARE(device.isReachable(), true);
@ -96,7 +96,7 @@ void DeviceTest::testUnpairedDevice()
{
LanLinkProvider linkProvider;
QSslSocket socket;
LanDeviceLink* link = new LanDeviceLink(deviceId, &linkProvider, &socket, DeviceLink::Locally);
LanDeviceLink* link = new LanDeviceLink(deviceId, &linkProvider, &socket, LanDeviceLink::Locally);
Device device(this, *identityPackage, link);
@ -120,6 +120,7 @@ void DeviceTest::cleanupTestCase()
{
delete identityPackage;
}
QTEST_GUILESS_MAIN(DeviceTest)
#include "devicetest.moc"