From 1a72dfb10bb0189e842c754b3550795538a79493 Mon Sep 17 00:00:00 2001 From: Javier Date: Tue, 31 Mar 2015 01:42:07 +0200 Subject: allow non-json message payloads --- saltoqd/fmsmanager.cpp | 2 +- saltoqd/storagemanager.cpp | 4 ++-- saltoqd/systemmanager.cpp | 3 ++- saltoqd/toqconnection.cpp | 39 +++++++++++++++++++++++++++------------ saltoqd/toqconnection.h | 9 ++++++--- saltoqd/toqmanager.cpp | 8 ++++---- saltoqd/versionmanager.cpp | 2 +- 7 files changed, 43 insertions(+), 24 deletions(-) (limited to 'saltoqd') diff --git a/saltoqd/fmsmanager.cpp b/saltoqd/fmsmanager.cpp index 6abf0b6..cdb9ee4 100644 --- a/saltoqd/fmsmanager.cpp +++ b/saltoqd/fmsmanager.cpp @@ -39,7 +39,7 @@ void FmsManager::handleMessage(const ToqConnection::Message &msg) Q_ASSERT(msg.destination == ToqConnection::FMSEndpoint); switch (msg.type) { case 0x4001: // Reply to file transfer - handleTransferResult(msg.payload.object()); + handleTransferResult(msg.toJson().object()); break; default: qWarning() << "Unknown message type" << msg.type; diff --git a/saltoqd/storagemanager.cpp b/saltoqd/storagemanager.cpp index eea309b..c88f36c 100644 --- a/saltoqd/storagemanager.cpp +++ b/saltoqd/storagemanager.cpp @@ -31,7 +31,7 @@ void StorageManager::handleMessage(const ToqConnection::Message &msg) void StorageManager::handleGetStoreStatusMessage(const ToqConnection::Message &msg) { - QString storeName = msg.payload.object()["store"].toString(); + QString storeName = msg.toJson().object()["store"].toString(); QJsonObject reply; if (storeName.contains('*')) { QJsonArray results; @@ -66,7 +66,7 @@ void StorageManager::handleGetStoreStatusMessage(const ToqConnection::Message &m void StorageManager::handleGetStoreMessage(const ToqConnection::Message &msg) { - QString storeName = msg.payload.object()["store"].toString(); + QString storeName = msg.toJson().object()["store"].toString(); QJsonObject reply; if (_stores.contains(storeName)) { const Store &store = _stores[storeName]; diff --git a/saltoqd/systemmanager.cpp b/saltoqd/systemmanager.cpp index bd5a563..cf80008 100644 --- a/saltoqd/systemmanager.cpp +++ b/saltoqd/systemmanager.cpp @@ -51,12 +51,13 @@ void SystemManager::handleGetTimeMessage(const ToqConnection::Message &msg) void SystemManager::handleSilenceMessage(const ToqConnection::Message &msg) { + QJsonObject obj = msg.toJson().object(); QJsonObject reply; reply.insert("result", int(0)); reply.insert("description", QLatin1String("Set to Silence Mode Request received")); - VoiceCallManager::setSilentMode(msg.payload.object()["silence_mode"].toInt()); + VoiceCallManager::setSilentMode(obj["silence_mode"].toInt()); _toq->sendReply(msg, 0x4007, reply); } diff --git a/saltoqd/toqconnection.cpp b/saltoqd/toqconnection.cpp index 6c1035e..8ffd02f 100644 --- a/saltoqd/toqconnection.cpp +++ b/saltoqd/toqconnection.cpp @@ -21,6 +21,26 @@ ToqConnection::ToqConnection(QObject *parent) : _reconnectTimer->setSingleShot(true); } +ToqConnection::Message::Message(Endpoint source, Endpoint destination, quint16 transactionId, quint32 type, const QJsonDocument &payload) + : source(source), destination(destination), transactionId(transactionId), type(type), + payload(payload.toJson(QJsonDocument::Compact)) +{ + +} + +QJsonDocument ToqConnection::Message::toJson() const +{ + QJsonDocument doc; + QJsonParseError error; + + doc = QJsonDocument::fromJson(payload, &error); + if (error.error) { + qWarning() << "Failure while parsing message JSON payload: " << error.errorString(); + } + + return doc; +} + QString ToqConnection::nameOfEndpoint(Endpoint ep) { int index = staticMetaObject.indexOfEnumerator("CoreEndpoints"); @@ -96,32 +116,27 @@ ToqConnection::Message ToqConnection::unpackMessage(const QByteArray &data) msg.destination = header[1]; msg.transactionId = qFromBigEndian(&header[4]); msg.type = qFromBigEndian(&header[6]); - - if (!data.isEmpty()) { - QJsonParseError error; - msg.payload = QJsonDocument::fromJson(data.mid(HEADER_LENGTH), &error); - if (error.error) { - qWarning() << "Failure while parsing message JSON payload: " << error.errorString(); - } - } + msg.payload = data.mid(HEADER_LENGTH); return msg; } QByteArray ToqConnection::packMessage(const Message &msg) { - QByteArray payload = msg.payload.toJson(QJsonDocument::Compact); uchar header[HEADER_LENGTH]; header[0] = msg.source; header[1] = msg.destination; - qToBigEndian(payload.length() + 4, &header[2]); + qToBigEndian(msg.payload.length() + 4, &header[2]); qToBigEndian(msg.transactionId, &header[4]); qToBigEndian(msg.type, &header[6]); - payload.prepend(reinterpret_cast(&header[0]), HEADER_LENGTH); + QByteArray data; + data.reserve(HEADER_LENGTH + msg.payload.length()); + data.append(reinterpret_cast(&header[0]), HEADER_LENGTH); + data.append(msg.payload); - return payload; + return data; } void ToqConnection::tryConnect() diff --git a/saltoqd/toqconnection.h b/saltoqd/toqconnection.h index 4d8ecc6..765c3f3 100644 --- a/saltoqd/toqconnection.h +++ b/saltoqd/toqconnection.h @@ -40,13 +40,16 @@ public: struct Message { Message(); - Message(Endpoint source, Endpoint destination, quint16 transactionId, quint32 type, QJsonDocument payload); + Message(Endpoint source, Endpoint destination, quint16 transactionId, quint32 type, const QByteArray &payload); + Message(Endpoint source, Endpoint destination, quint16 transactionId, quint32 type, const QJsonDocument &payload); Endpoint source; Endpoint destination; quint16 transactionId; quint32 type; - QJsonDocument payload; + QByteArray payload; + + QJsonDocument toJson() const; }; static QString nameOfEndpoint(Endpoint ep); @@ -91,7 +94,7 @@ inline ToqConnection::Message::Message() { } -inline ToqConnection::Message::Message(Endpoint source, Endpoint destination, quint16 transactionId, quint32 type, QJsonDocument payload) +inline ToqConnection::Message::Message(Endpoint source, Endpoint destination, quint16 transactionId, quint32 type, const QByteArray &payload) : source(source), destination(destination), transactionId(transactionId), type(type), payload(payload) { } diff --git a/saltoqd/toqmanager.cpp b/saltoqd/toqmanager.cpp index c8ba0e2..331baaf 100644 --- a/saltoqd/toqmanager.cpp +++ b/saltoqd/toqmanager.cpp @@ -51,8 +51,8 @@ void ToqManager::setEndpointListener(ToqConnection::Endpoint ep, EndpointHandler void ToqManager::sendMessage(const ToqConnection::Message &msg) { if (PROTO_DEBUG) { - QString json = QString::fromUtf8(msg.payload.toJson(QJsonDocument::Compact)); - qDebug() << "Sending message to" << ToqConnection::nameOfEndpoint(msg.destination) << "from" << ToqConnection::nameOfEndpoint(msg.source) << "type" << msg.type << json; + QString content = QString::fromUtf8(msg.payload); + qDebug() << "Sending message to" << ToqConnection::nameOfEndpoint(msg.destination) << "from" << ToqConnection::nameOfEndpoint(msg.source) << "type" << msg.type << content; } _conn->sendMessage(msg); } @@ -84,8 +84,8 @@ void ToqManager::handleToqMessage(const ToqConnection::Message &msg) EndpointHandler *handler = _handlers.value(msg.destination, 0); if (PROTO_DEBUG) { - QString json = QString::fromUtf8(msg.payload.toJson(QJsonDocument::Compact)); - qDebug() << "Received message to" << ToqConnection::nameOfEndpoint(msg.destination) << "from" << ToqConnection::nameOfEndpoint(msg.destination) << "type" << msg.type << json; + QString content = QString::fromUtf8(msg.payload); + qDebug() << "Received message to" << ToqConnection::nameOfEndpoint(msg.destination) << "from" << ToqConnection::nameOfEndpoint(msg.destination) << "type" << msg.type << content; } if (handler) { diff --git a/saltoqd/versionmanager.cpp b/saltoqd/versionmanager.cpp index 8c88992..2b7d692 100644 --- a/saltoqd/versionmanager.cpp +++ b/saltoqd/versionmanager.cpp @@ -21,7 +21,7 @@ void VersionManager::handleMessage(const ToqConnection::Message &msg) void VersionManager::handleVersionMessage(const ToqConnection::Message &msg) { - QJsonObject root = msg.payload.object(); + QJsonObject root = msg.toJson().object(); qDebug() << "Remote AlohaVersion: " << root["AlohaVersion"].toString(); QJsonObject reply; -- cgit v1.2.3