Commit 7d4143b9 authored by Albert Vaca Cintora's avatar Albert Vaca Cintora

First and untested implementation of payload transfer in LAN backend

Encryption not implemented yet for transfers, but should be easy to do
parent 42e0b4a0
......@@ -4,7 +4,8 @@ set(kded_kdeconnect_SRCS
backends/lan/lanlinkprovider.cpp
backends/lan/landevicelink.cpp
#landevicelinktransfer.cpp
backends/lan/uploadjob.cpp
backends/lan/downloadjob.cpp
PARENT_SCOPE
)
/*
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "downloadjob.h"
DownloadJob::DownloadJob(QHostAddress address, QVariantMap transferInfo): KJob()
{
mPort = transferInfo["port"].toInt();
mSocket = new QTcpSocket(this);
mAddress = address;
}
void DownloadJob::start()
{
mSocket->connectToHost(mAddress, mPort, QIODevice::ReadOnly);
connect(mSocket, SIGNAL(disconnected()), this, SLOT(disconnected()));
}
void DownloadJob::disconnected()
{
emitResult();
}
QIODevice* DownloadJob::getPayload()
{
return mSocket;
}
/*
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DOWNLOADJOB_H
#define DOWNLOADJOB_H
#include <KJob>
#include <QIODevice>
#include <QVariantMap>
#include <QHostAddress>
#include <QTcpSocket>
class DownloadJob
: public KJob
{
Q_OBJECT
public:
DownloadJob(QHostAddress address, QVariantMap transferInfo);
virtual void start();
QIODevice* getPayload();
private:
QIODevice* mOutput;
QTcpSocket* mSocket;
QHostAddress mAddress;
qint16 mPort;
private Q_SLOTS:
void disconnected();
};
#endif // UPLOADJOB_H
......@@ -26,6 +26,8 @@
#include <netdb.h>
#include "../linkprovider.h"
#include "uploadjob.h"
#include "downloadjob.h"
LanDeviceLink::LanDeviceLink(const QString& d, LinkProvider* a, QTcpSocket* socket)
: DeviceLink(d, a)
......@@ -53,13 +55,26 @@ LanDeviceLink::LanDeviceLink(const QString& d, LinkProvider* a, QTcpSocket* sock
bool LanDeviceLink::sendPackageEncrypted(QCA::PublicKey& key, NetworkPackage& np)
{
if (np.hasPayload()) {
UploadJob* job = new UploadJob(np.payload());
job->start();
np.setPayloadTransferInfo(job->getTransferInfo());
}
np.encrypt(key);
int written = mSocket->write(np.serialize());
return (written != -1);
}
bool LanDeviceLink::sendPackage(NetworkPackage& np)
{
if (np.hasPayload()) {
UploadJob* job = new UploadJob(np.payload());
job->start();
np.setPayloadTransferInfo(job->getTransferInfo());
}
int written = mSocket->write(np.serialize());
return (written != -1);
}
......@@ -85,6 +100,13 @@ void LanDeviceLink::dataReceived()
NetworkPackage decrypted(QString::null);
np.decrypt(mPrivateKey, &decrypted);
if (np.hasPayloadTransferInfo()) {
DownloadJob* job = new DownloadJob(mSocket->peerAddress(), np.payloadTransferInfo());
job->start();
np.setPayload(job->getPayload());
}
Q_EMIT receivedPackage(decrypted);
} else {
......@@ -95,3 +117,8 @@ void LanDeviceLink::dataReceived()
}
}
void LanDeviceLink::readyRead()
{
}
......@@ -42,6 +42,7 @@ public:
private Q_SLOTS:
void dataReceived();
void readyRead();
private:
QTcpSocket* mSocket;
......
/*
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <qalgorithms.h>
#include "uploadjob.h"
UploadJob::UploadJob(QIODevice* source): KJob()
{
mInput = source;
mServer = new QTcpServer(this);
mSocket = 0;
}
void UploadJob::start()
{
mPort = 1739;
while(!mServer->listen(QHostAddress::Any, mPort)) {
mPort++;
if (mPort > 1764) { //No ports available?
mPort = 0;
return;
}
}
connect(mServer, SIGNAL(newConnection()), this, SLOT(newConnection()));
}
void UploadJob::newConnection()
{
if (mSocket || !mServer->hasPendingConnections()) return;
mSocket = mServer->nextPendingConnection();
connect(mInput, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(mInput, SIGNAL(aboutToClose()), this, SLOT(aboutToClose()));
if (!mInput->open(QIODevice::ReadOnly)) {
return; //TODO: Handle error, clean up...
}
}
void UploadJob::readyRead()
{
qint64 bytes = qMax(mInput->bytesAvailable(), (qint64)4096);
mSocket->write(mInput->read(bytes));
}
void UploadJob::aboutToClose()
{
mSocket->close();
mSocket->disconnectFromHost();
emitResult();
}
QVariantMap UploadJob::getTransferInfo()
{
QVariantMap ret;
ret["port"] = mPort;
return ret;
}
/*
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef UPLOADJOB_H
#define UPLOADJOB_H
#include <KJob>
#include <QIODevice>
#include <QVariantMap>
#include <QTcpServer>
#include <QTcpSocket>
class UploadJob
: public KJob
{
Q_OBJECT
public:
UploadJob(QIODevice* source);
virtual void start();
QVariantMap getTransferInfo();
private:
QIODevice* mInput;
QTcpServer* mServer;
QTcpSocket* mSocket;
qint16 mPort;
private Q_SLOTS:
void readyRead();
void newConnection();
void aboutToClose();
};
#endif // UPLOADJOB_H
......@@ -68,6 +68,7 @@ public:
template<typename T> void set(const QString& key, const T& value) { mBody[key] = QVariant(value); }
bool has(const QString& key) const { return mBody.contains(key); }
//TODO: Change to a shared pointer
QIODevice* payload() const { return mPayload; }
void setPayload(QIODevice* device) { mPayload = device; }
bool hasPayload() const { return (mPayload != 0); }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment