From e86897cfe3902adb87351dede14662a6f7c4b995 Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Fri, 20 Sep 2013 15:54:30 +0200 Subject: [PATCH] Fixed file transfer, added file size to protocol (optional) Increased protocol version to 5 Updated README --- README | 22 +++++-- kded/backends/lan/downloadjob.cpp | 1 + kded/backends/lan/landevicelink.cpp | 2 +- kded/backends/lan/uploadjob.cpp | 2 + kded/backends/loopback/loopbackdevicelink.cpp | 4 +- kded/networkpackage.cpp | 21 ++++--- kded/networkpackage.h | 7 ++- kded/plugins/filetransfer/filetransferjob.cpp | 57 +++++++++++++++---- kded/plugins/filetransfer/filetransferjob.h | 4 +- .../filetransfer/filetransferplugin.cpp | 17 +++--- .../notificationsdbusinterface.cpp | 2 + 11 files changed, 99 insertions(+), 40 deletions(-) diff --git a/README b/README index 1c6445fec..0a4b2ec2c 100644 --- a/README +++ b/README @@ -15,11 +15,11 @@ Class diagram Daemon instantiates Backends -Backends manage to create DeviceLinks with the devices they can reach, and Q_EMIT them to Daemon. +Backends manage to create DeviceLinks with the devices they can reach, and Q_EMIT them to the Daemon. -When Daemon receives a DeviceLink from a backend it: - - If he already knows the Device, adds the DeviceLink to the Device - - If not, it creates a new Device. +When the Daemon receives a DeviceLink from a backend: + - If it already knows the Device, adds the new DeviceLink to the existing Device, as a new way to reach it. + - If not, it creates a new untrusted (yet to be paired) Device. Devices contain a list of DeviceLinks, plus a list of Plugins (instantiated automatically) @@ -45,10 +45,22 @@ The basic structure of a NetworkPackage is the following: "body": { }, - "version": 1 + "version": 5 } Each type of package defines what it should contain inside its "body", so only the emisor Plugin and receiver Plugin of this type of package need agree about its content. +If the package has a payload attached, it will also contain two more fields: + "payloadSize": The size of the file, or -1 if it is a stream without known end + "payloadTransferInfo": Another JSON Object containing the information that the + backend wants to send to the peer backend, to actually + transfer the payload. + +Encrypted network packages have the following format: + +//TODO + + +//TODO: Document the possible contents written for each plugin in the body part diff --git a/kded/backends/lan/downloadjob.cpp b/kded/backends/lan/downloadjob.cpp index cc04697b4..d86801579 100644 --- a/kded/backends/lan/downloadjob.cpp +++ b/kded/backends/lan/downloadjob.cpp @@ -32,6 +32,7 @@ void DownloadJob::start() qDebug() << "start"; mSocket->connectToHost(mAddress, mPort, QIODevice::ReadOnly); connect(mSocket, SIGNAL(disconnected()), this, SLOT(disconnected())); + //TODO: Implement payload encryption somehow (create an intermediate iodevice to encrypt the payload here?) } void DownloadJob::disconnected() diff --git a/kded/backends/lan/landevicelink.cpp b/kded/backends/lan/landevicelink.cpp index 1b159c24b..3704696dc 100644 --- a/kded/backends/lan/landevicelink.cpp +++ b/kded/backends/lan/landevicelink.cpp @@ -101,7 +101,7 @@ void LanDeviceLink::dataReceived() qDebug() << "HasPayloadTransferInfo"; DownloadJob* job = new DownloadJob(mSocket->peerAddress(), decrypted.payloadTransferInfo()); job->start(); - decrypted.setPayload(job->getPayload()); + decrypted.setPayload(job->getPayload(), decrypted.payloadSize()); } Q_EMIT receivedPackage(decrypted); diff --git a/kded/backends/lan/uploadjob.cpp b/kded/backends/lan/uploadjob.cpp index b77ad9582..af8009cbb 100644 --- a/kded/backends/lan/uploadjob.cpp +++ b/kded/backends/lan/uploadjob.cpp @@ -62,6 +62,8 @@ void UploadJob::newConnection() void UploadJob::readyRead() { + //TODO: Implement payload encryption + qint64 bytes = qMax(mInput->bytesAvailable(), (qint64)4096); mSocket->write(mInput->read(bytes)); } diff --git a/kded/backends/loopback/loopbackdevicelink.cpp b/kded/backends/loopback/loopbackdevicelink.cpp index d5e4cbab6..c16a02e19 100644 --- a/kded/backends/loopback/loopbackdevicelink.cpp +++ b/kded/backends/loopback/loopbackdevicelink.cpp @@ -47,7 +47,7 @@ bool LoopbackDeviceLink::sendPackageEncrypted(QCA::PublicKey& key, NetworkPackag //LoopbackDeviceLink does not need deviceTransferInfo if (input.hasPayload()) { QIODevice* device = input.payload(); - output.setPayload(device); + output.setPayload(device, input.payloadSize()); } Q_EMIT receivedPackage(output); @@ -63,7 +63,7 @@ bool LoopbackDeviceLink::sendPackage(NetworkPackage& input) //LoopbackDeviceLink does not need deviceTransferInfo if (input.hasPayload()) { QIODevice* device = input.payload(); - output.setPayload(device); + output.setPayload(device, input.payloadSize()); } Q_EMIT receivedPackage(output); diff --git a/kded/networkpackage.cpp b/kded/networkpackage.cpp index eeb78465c..2f20220eb 100644 --- a/kded/networkpackage.cpp +++ b/kded/networkpackage.cpp @@ -34,7 +34,7 @@ #include const QCA::EncryptionAlgorithm NetworkPackage::EncryptionAlgorithm = QCA::EME_PKCS1v15; -const int NetworkPackage::ProtocolVersion = 4; +const int NetworkPackage::ProtocolVersion = 5; NetworkPackage::NetworkPackage(const QString& type) { @@ -42,6 +42,7 @@ NetworkPackage::NetworkPackage(const QString& type) mType = type; mBody = QVariantMap(); mPayload = 0; + mPayloadSize = 0; } void NetworkPackage::createIdentityPackage(NetworkPackage* np) @@ -51,6 +52,7 @@ void NetworkPackage::createIdentityPackage(NetworkPackage* np) np->mId = QString::number(QDateTime::currentMSecsSinceEpoch()); np->mType = PACKAGE_TYPE_IDENTITY; np->mPayload = 0; + np->mPayloadSize = 0; np->set("deviceId", id); np->set("deviceName", QHostInfo::localHostName()); np->set("protocolVersion", NetworkPackage::ProtocolVersion); @@ -69,6 +71,7 @@ QByteArray NetworkPackage::serialize() const if (hasPayload()) { //qDebug() << "Serializing payloadTransferInfo"; + variant["payloadSize"] = 0; variant["payloadTransferInfo"] = mPayloadTransferInfo; } @@ -110,10 +113,8 @@ bool NetworkPackage::unserialize(const QByteArray& a, NetworkPackage* np) qDebug() << "Unserialize: " << a; } - if (variant.contains("payloadTransferInfo")) { - //qDebug() << "Unserializing payloadTransferInfo"; - np->mPayloadTransferInfo = variant["payloadTransferInfo"].toMap(); - } + np->mPayloadSize = variant["payloadSize"].toInt(); //Will return 0 if was not present, which is ok + np->mPayloadTransferInfo = variant["payloadTransferInfo"].toMap(); //Will return an empty qvariantmap if was not present, which is ok return true; @@ -122,7 +123,6 @@ bool NetworkPackage::unserialize(const QByteArray& a, NetworkPackage* np) void NetworkPackage::encrypt(QCA::PublicKey& key) { - //TODO: Implement payload encryption somehow (create an intermediate iodevice to encrypt the payload here?) QByteArray serialized = serialize(); int chunkSize = key.maximumEncryptSize(NetworkPackage::EncryptionAlgorithm); @@ -160,12 +160,15 @@ bool NetworkPackage::decrypt(QCA::PrivateKey& key, NetworkPackage* out) const decryptedJson.append(decryptedChunk.toByteArray()); } - //TODO: Implement payload encryption somehow (create an intermediate iodevice to decrypt the payload here?) + bool success = unserialize(decryptedJson, out); + + if (!success) return false; + if (hasPayload()) { - out->setPayload(mPayload); + out->mPayload = mPayload; } - return unserialize(decryptedJson, out); + return true; } diff --git a/kded/networkpackage.h b/kded/networkpackage.h index 4556c4de7..61e83306c 100644 --- a/kded/networkpackage.h +++ b/kded/networkpackage.h @@ -71,8 +71,9 @@ public: //TODO: Change to a shared pointer QIODevice* payload() const { return mPayload; } - void setPayload(QIODevice* device) { mPayload = device; } - bool hasPayload() const { return (mPayload != 0); } + void setPayload(QIODevice* device, int payloadSize) { mPayload = device; mPayloadSize = payloadSize; Q_ASSERT(mPayloadSize >= -1); } + bool hasPayload() const { return (mPayloadSize != 0); } + int payloadSize() const { return mPayloadSize; } //-1 means it is an endless stream //To be called by a particular DeviceLink QVariantMap payloadTransferInfo() const { return mPayloadTransferInfo; } @@ -84,12 +85,14 @@ private: void setId(const QString& id) { mId = id; } void setType(const QString& t) { mType = t; } void setBody(const QVariantMap& b) { mBody = b; } + void setPayloadSize(int s) { mPayloadSize = s; } QString mId; QString mType; QVariantMap mBody; QIODevice* mPayload; + int mPayloadSize; QVariantMap mPayloadTransferInfo; }; diff --git a/kded/plugins/filetransfer/filetransferjob.cpp b/kded/plugins/filetransfer/filetransferjob.cpp index 5535f104c..876b81740 100644 --- a/kded/plugins/filetransfer/filetransferjob.cpp +++ b/kded/plugins/filetransfer/filetransferjob.cpp @@ -20,13 +20,17 @@ #include "filetransferjob.h" +#include + #include #include -FileTransferJob::FileTransferJob(QIODevice* origin, const KUrl& destination): KJob() +FileTransferJob::FileTransferJob(QIODevice* origin, int size, const KUrl& destination): KJob() { mDestination = destination; mOrigin = origin; + mSize = size; + mWritten = 0; } void FileTransferJob::start() @@ -46,51 +50,80 @@ void FileTransferJob::start() void FileTransferJob::open(KIO::Job* job) { Q_UNUSED(job); - + + qDebug() << "open"; + + if (!mOrigin) { + qDebug() << "FileTransferJob: Origin is null"; + return; + } + //Open source file - mOrigin->open(QIODevice::ReadOnly); - Q_ASSERT(mOrigin->isOpen()); connect(mOrigin, SIGNAL(readyRead()),this, SLOT(readyRead())); - connect(mOrigin, SIGNAL(aboutToClose()),this, SLOT(sourceFinished())); + connect(mOrigin, SIGNAL(disconnected()),this, SLOT(sourceFinished())); if (mOrigin->bytesAvailable() > 0) readyRead(); } void FileTransferJob::readyRead() { + qDebug() << "readyRead"; + //Copy a chunk of data int bytes = qMin(qint64(4096), mOrigin->bytesAvailable()); QByteArray data = mOrigin->read(bytes); mTempDestination->write(data); + mWritten += bytes; - if(!mOrigin->atEnd()) + if (mSize > -1) { + setPercent((mWritten*100)/mSize); + } + + qDebug() << mSize << mWritten << bytes; + + if (mSize > -1 && mWritten >= mSize) { //At the end or expected size reached + qDebug() << "No more data to read"; + disconnect(mOrigin, SIGNAL(readyRead()),this, SLOT(readyRead())); + mOrigin->close(); + } else if (mOrigin->bytesAvailable() > 0) { QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection); + } } void FileTransferJob::sourceFinished() { qDebug() << "sourceFinished"; - readyRead(); //Read the last chunk of data, if any + //Make sure we do not enter this function again + disconnect(mOrigin, SIGNAL(aboutToClose()),this, SLOT(sourceFinished())); - qDebug() << "Finished" << mTempDestination->url(); + //TODO: MD5 check the file + if (mSize > -1 && mWritten != mSize) { + qDebug() << "Received incomplete file"; + setError(1); + setErrorText(i18n("Received incomplete file")); + emitResult(); + } + + qDebug() << "Finished" << mTempDestination->url() << mDestination; KIO::FileCopyJob* job = KIO::file_move(mTempDestination->url(), mDestination); connect(job, SIGNAL(result(KJob*)), this, SLOT(moveResult(KJob*))); job->start(); - delete mOrigin; //TODO: Use shared pointers + //delete mOrigin; //TODO: Use shared pointers } void FileTransferJob::moveResult(KJob* job) { //TODO: Error handling, cleanup - qDebug() << "Move result"; - qDebug() << job->errorString(); - qDebug() << job->errorText(); + qDebug() << "Move finished"; + if (job->error()) { + qDebug() << job->errorText(); + } emitResult(); } diff --git a/kded/plugins/filetransfer/filetransferjob.h b/kded/plugins/filetransfer/filetransferjob.h index 5cf365f1c..d38d0feaf 100644 --- a/kded/plugins/filetransfer/filetransferjob.h +++ b/kded/plugins/filetransfer/filetransferjob.h @@ -35,7 +35,7 @@ class FileTransferJob : public KJob Q_OBJECT public: - FileTransferJob(QIODevice* origin, const KUrl& destination); + FileTransferJob(QIODevice* origin, int size, const KUrl& destination); virtual void start(); KUrl destination() { return mDestination; } @@ -48,7 +48,9 @@ public Q_SLOTS: private: KIO::FileJob* mTempDestination; KUrl mDestination; + int mSize; QIODevice* mOrigin; + int mWritten; }; diff --git a/kded/plugins/filetransfer/filetransferplugin.cpp b/kded/plugins/filetransfer/filetransferplugin.cpp index 097eab98b..7533f7388 100644 --- a/kded/plugins/filetransfer/filetransferplugin.cpp +++ b/kded/plugins/filetransfer/filetransferplugin.cpp @@ -40,41 +40,42 @@ FileTransferPlugin::FileTransferPlugin(QObject* parent, const QVariantList& args //TODO: Use downloads user path //TODO: Be able to change this from config mDestinationDir = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation); + if (!mDestinationDir.endsWith('/')) mDestinationDir.append('/'); } bool FileTransferPlugin::receivePackage(const NetworkPackage& np) { //TODO: Move this code to a test and do a diff between files - //if (np.type() == PACKAGE_TYPE_PING) { + if (np.type() == PACKAGE_TYPE_PING) { qDebug() << "sending file" << (QDesktopServices::storageLocation(QDesktopServices::HomeLocation) + "/.bashrc"); NetworkPackage out(PACKAGE_TYPE_FILETRANSFER); - out.set("filename", mDestinationDir + "/itworks.txt"); + out.set("filename", mDestinationDir + "itworks.txt"); //TODO: Use shared pointers AutoClosingQFile* file = new AutoClosingQFile(QDesktopServices::storageLocation(QDesktopServices::HomeLocation) + "/.bashrc"); //Test file to transfer - out.setPayload(file); + out.setPayload(file, file->size()); device()->sendPackage(out); return true; - //} + } if (np.type() != PACKAGE_TYPE_FILETRANSFER) return false; - - qDebug() << "file transfer"; + qDebug() << "File transfer"; if (np.hasPayload()) { qDebug() << "receiving file"; - QString filename = np.get("filename"); + QString filename = np.get("filename", mDestinationDir + QString::number(QDateTime::currentMSecsSinceEpoch())); QIODevice* incoming = np.payload(); - FileTransferJob* job = new FileTransferJob(incoming,filename); + FileTransferJob* job = new FileTransferJob(incoming, np.payloadSize(), filename); connect(job,SIGNAL(result(KJob*)), this, SLOT(finished(KJob*))); job->start(); } + return true; } diff --git a/kded/plugins/notifications/notificationsdbusinterface.cpp b/kded/plugins/notifications/notificationsdbusinterface.cpp index 24466a403..a62e2d127 100644 --- a/kded/plugins/notifications/notificationsdbusinterface.cpp +++ b/kded/plugins/notifications/notificationsdbusinterface.cpp @@ -51,6 +51,8 @@ void NotificationsDbusInterface::processPackage(const NetworkPackage& np) } else { Notification* noti = new Notification(np, this); + //TODO: Store the app icon if any under tmp with the app name as filename (so we only store one per app) and export the path to that file to dbus inside Notification + //Do not show updates to existent notification nor answers to a initialization request if (!mInternalIdToPublicId.contains(noti->internalId()) && !np.get("requestAnswer", false)) { KNotification* notification = new KNotification("notification");