Added a payload field to networkpackage, protocol version increased to 4

It will be used to attach files to packages when devicelinks support it
This commit is contained in:
Albert Vaca 2013-09-09 18:30:44 +02:00
parent 40944cf944
commit e1c749e33d
2 changed files with 40 additions and 20 deletions

View file

@ -22,6 +22,7 @@
#include <KSharedConfig>
#include <KConfigGroup>
#include <QByteArray>
#include <QDataStream>
#include <QHostInfo>
@ -33,13 +34,29 @@
#include <qjson/qobjecthelper.h>
const QCA::EncryptionAlgorithm NetworkPackage::EncryptionAlgorithm = QCA::EME_PKCS1v15;
const int NetworkPackage::ProtocolVersion = 3;
const int NetworkPackage::ProtocolVersion = 4;
NetworkPackage::NetworkPackage(const QString& type)
{
mId = QString::number(QDateTime::currentMSecsSinceEpoch());
mType = type;
mBody = QVariantMap();
mPayload = 0;
}
void NetworkPackage::createIdentityPackage(NetworkPackage* np)
{
KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
QString id = config->group("myself").readEntry<QString>("id","");
np->mId = QString::number(QDateTime::currentMSecsSinceEpoch());
np->mType = PACKAGE_TYPE_IDENTITY;
np->mPayload = 0;
np->set("deviceId", id);
np->set("deviceName", QHostInfo::localHostName());
np->set("protocolVersion", NetworkPackage::ProtocolVersion);
//qDebug() << "createIdentityPackage" << np->serialize();
}
QByteArray NetworkPackage::serialize() const
@ -51,6 +68,10 @@ QByteArray NetworkPackage::serialize() const
//variant["body"] = mBody;
QVariantMap variant = QJson::QObjectHelper::qobject2qvariant(this);
if (hasPayload()) {
variant["payloadTransferInfo"] = mPayloadTransferInfo;
}
//QVariant -> json
bool ok;
QJson::Serializer serializer;
@ -81,6 +102,10 @@ bool NetworkPackage::unserialize(const QByteArray& a, NetworkPackage* np)
//QVariant -> Object
QJson::QObjectHelper::qvariant2qobject(variant,np);
if (variant.contains("payloadTransferInfo")) {
np->mPayloadTransferInfo = variant["payloadTransferInfo"].toMap();
}
return true;
}
@ -129,19 +154,3 @@ bool NetworkPackage::decrypt (QCA::PrivateKey& key, NetworkPackage* out) const
}
void NetworkPackage::createIdentityPackage(NetworkPackage* np)
{
KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
QString id = config->group("myself").readEntry<QString>("id","");
np->mId = QString::number(QDateTime::currentMSecsSinceEpoch());
np->mType = PACKAGE_TYPE_IDENTITY;
np->set("deviceId", id);
np->set("deviceName", QHostInfo::localHostName());
np->set("protocolVersion", NetworkPackage::ProtocolVersion);
//qDebug() << "createIdentityPackage" << np->serialize();
}

View file

@ -24,9 +24,11 @@
#include "networkpackagetypes.h"
#include <QObject>
#include <QDebug>
#include <QString>
#include <QVariant>
#include <QStringList>
#include <QIODevice>
#include <QtCrypto>
#include <qjson/parser.h>
@ -66,7 +68,16 @@ 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); }
QIODevice* payload() const { return mPayload; }
void setPayload(QIODevice* device) { mPayload = device;}
bool hasPayload() const { return (mPayload != 0); }
//To be called by a particular DeviceLink
QVariantMap payloadTransferInfo() const { return mPayloadTransferInfo; }
void setPayloadTransferInfo(const QVariantMap& map) { mPayloadTransferInfo = map; }
private:
void setId(const QString& id) { mId = id; }
void setType(const QString& t) { mType = t; }
void setBody(const QVariantMap& b) { mBody = b; }
@ -75,9 +86,9 @@ private:
QString mType;
QVariantMap mBody;
QIODevice* mPayload;
QVariantMap mPayloadTransferInfo;
};
//Set specialization need this awesome-to-the-max syntax:
//template<> inline void NetworkPackage::set<QStringList>(const QString& key, const QStringList& value) { mBody[key] = QVariant(value); }
#endif // NETWORKPACKAGE_H