summaryrefslogtreecommitdiffstats
path: root/responsemessage.cpp
diff options
context:
space:
mode:
authorRaquel Medina <raquel.medina@konsulko.com>2020-03-18 23:56:31 +0100
committerRaquel Medina <raquel.medina@konsulko.com>2020-03-23 14:02:28 +0100
commit0ed292d3ccf93c889734960676a321d1166d3f66 (patch)
treeb8d64d1685d2f4bf599ab3867a0d3ff557ff0479 /responsemessage.cpp
parent5c750385d02116a92fa4c120ccc26abb8267bc97 (diff)
rework message hierarchy
Rework message hierarchy with the final objective of splitting libqtappfw into several libraries. This commit carries the following changes: - Simplify message hierarchy, keeping abstract Message class, adding specialization for call and event messages, keeping ResponseMessage, and removing all module specific specializations. - Add MessageFactory class to create message objects. - Change messages life cycle: using smart pointers and removing QObject from message hierarchy (a Message is not a QObject anymore and thus 'deleteLater()' is not available). - Adapt all modules to use new message hierarchy. - Keep ResponseMessage original constructor to avoid breaking TaskManager. - Message constructors have been kept public, but will go private on a follow-up patch (once TaskManager class has been modified to use new MessageFactory). Bug-AGL: SPEC-3112 Signed-off-by: Raquel Medina <raquel.medina@konsulko.com> Change-Id: I3a7a6325209ddeca2293f1ac745371861a947bfb
Diffstat (limited to 'responsemessage.cpp')
-rw-r--r--responsemessage.cpp111
1 files changed, 94 insertions, 17 deletions
diff --git a/responsemessage.cpp b/responsemessage.cpp
index 0e2ad5a..96dab97 100644
--- a/responsemessage.cpp
+++ b/responsemessage.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2018 Konsulko Group
+ * Copyright (C) 2018-2020 Konsulko Group
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,28 +22,105 @@
#include "responsemessage.h"
+//deprecated method call new constructor and setAdditionalData() instead:
ResponseMessage::ResponseMessage(QByteArray request)
{
- QJsonDocument jdoc(QJsonDocument::fromJson(request));
+ QJsonDocument jdoc(QJsonDocument::fromJson(request));
- if (!jdoc.isArray()) {
- qWarning("Invalid appfw message: not an array");
- return;
- }
+ if (!jdoc.isArray()) {
+ qWarning("Invalid appfw message: not an array");
+ return;
+ }
- QJsonArray msg = jdoc.array();
+ QJsonArray msg = jdoc.array();
- if (msg.size() != 4) {
- qWarning("Invalid appfw message: invalid array size");
- return;
- }
+ if (msg.size() != 4) {
+ qWarning("Invalid appfw message: invalid array size");
+ return;
+ }
- QStringList api_str_list = msg[2].toString().split(QRegExp("/"));
+ QStringList api_str_list = msg[2].toString().split(QRegExp("/"));
- m_request["msgid"] = msg.at(0);
- m_request["callid"] = msg.at(1);
- m_request["api"] = api_str_list[0];
- m_request["verb"] = api_str_list[1];
- m_request["parameter"] = msg.at(3);
+ m_request["msgid"] = msg.at(0);
+ m_request["callid"] = msg.at(1);
+ m_request["api"] = api_str_list[0];
+ m_request["verb"] = api_str_list[1];
+ m_request["parameter"] = msg.at(3);
+}
+
+ResponseMessage::ResponseMessage(QJsonDocument content)
+{
+ QJsonArray msg = content.array();
+ if (!msg[2].isObject()) {
+ qWarning("Invalid appfw payload: no JSON object");
+ return;
+ }
+
+ //deserialize:
+ auto callid = msg[1].toString().toInt();
+ QJsonObject payload = msg[2].toObject();
+
+ auto request_iter = payload.find("request");
+ auto request = request_iter.value().toObject();
+ if (request.empty()) {
+ qWarning("Invalid appfw reply message: empty request data");
+ return;
+ }
+
+ auto status_iter = request.find("status");
+ auto info_iter = request.find("info");
+ auto response_iter = payload.find("response");
+ auto response = response_iter.value().toObject();
+ m_reply_status = status_iter.value().toString();
+ m_reply_info = info_iter.value().toString();
+ m_reply_data = response;
+ m_reply_callid = callid;
+ m_init = false; //not complete yet, missing matching request data
+}
+
+bool ResponseMessage::setAdditionalData(QByteArray data)
+{
+ QJsonDocument jdoc(QJsonDocument::fromJson(data));
+ if (!jdoc.isArray()) {
+ qWarning("Invalid data: not an array");
+ return false;
+ }
+
+ QJsonArray content = jdoc.array();
+ if (content.size() != 4) {
+ qWarning("Invalid data: invalid array size");
+ return false;
+ }
+
+ QStringList api_str_list = content[2].toString().split(QRegExp("/"));
+ m_request["msgid"] = content.at(0);
+ m_request["callid"] = content.at(1);
+ m_request["api"] = api_str_list[0];
+ m_request["verb"] = api_str_list[1];
+ m_request["parameter"] = content.at(3);
+ m_init = true;
+ return true;
+}
+
+bool ResponseMessage::copyCallId(unsigned int *id)
+{
+ *id = m_reply_callid;
+ return true;
+}
+
+QByteArray ResponseMessage::serialize(QJsonDocument::JsonFormat format)
+{
+ QJsonArray array;
+ (m_reply_status == "failed")?
+ array.append(static_cast<int>(MessageId::RetErr)) :
+ array.append(static_cast<int>(MessageId::RetOk));
+ array.append(static_cast<int>(m_reply_callid));
+ array.append(m_request["api"].toString() + "/" + m_request["verb"].toString());
+ array.append(m_reply_data);
+
+ QJsonDocument jdoc;
+ jdoc.setArray(array);
+
+ return jdoc.toJson(format).data();
}