summaryrefslogtreecommitdiffstats
path: root/messageengine.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 /messageengine.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 'messageengine.cpp')
-rw-r--r--messageengine.cpp130
1 files changed, 21 insertions, 109 deletions
diff --git a/messageengine.cpp b/messageengine.cpp
index 6f31d0f..2633545 100644
--- a/messageengine.cpp
+++ b/messageengine.cpp
@@ -14,24 +14,13 @@
* limitations under the License.
*/
+#include <QJsonArray>
+#include <QDebug>
+
#include "message.h"
+#include "messagefactory.h"
#include "messageengine.h"
-#include "bluetoothmessage.h"
-#include "guimetadatamessage.h"
-#include "hvacmessage.h"
-#include "mapmessage.h"
-#include "mediaplayermessage.h"
-#include "navigationmessage.h"
-#include "networkmessage.h"
-#include "pbapmessage.h"
-#include "radiomessage.h"
-#include "responsemessage.h"
-#include "signalcomposermessage.h"
-#include "telephonymessage.h"
-#include "weathermessage.h"
-#include "voicemessage.h"
-#include <QJsonArray>
MessageEngine::MessageEngine(const QUrl &url, QObject *parent) :
QObject(parent),
@@ -44,31 +33,17 @@ MessageEngine::MessageEngine(const QUrl &url, QObject *parent) :
m_websocket.open(url);
}
-unsigned int MessageEngine::requestCallId()
-{
- int callid;
-
- m_mutex.lock();
- callid = ++m_callid;
- m_mutex.unlock();
-
- return callid;
-}
-
-bool MessageEngine::sendMessage(Message *message)
+bool MessageEngine::sendMessage(std::unique_ptr<Message> msg)
{
- if (!message->isValid())
+ if (!msg)
return false;
- auto callid = requestCallId();
- message->setCallId(callid);
-
- QByteArray data = message->toJson().data();
- qint64 size = m_websocket.sendTextMessage(data);
- if (size == 0)
+ unsigned int callid = m_callid++;
+ QByteArray forkeeps = msg->send(m_websocket, callid);
+ if (forkeeps.isEmpty())
return false;
- m_calls.insert(callid, data);
+ m_calls.insert(callid, forkeeps);
return true;
}
@@ -94,83 +69,20 @@ void MessageEngine::onTextMessageReceived(QString jsonStr)
return;
}
- QJsonArray msg = jdoc.array();
- int msgid = msg[0].toInt();
+ MessageId id = Message::isValid(jdoc);
+ if (id == MessageId::Invalid) {
+ qWarning() << "Received unknown message, discarding";
+ return;
+ }
- Message *message;
- MessageType type;
+ std::shared_ptr<Message> message = MessageFactory::getInstance().createInboundMessage(id, jdoc);
- switch (msgid) {
- case RetOk:
- case RetErr: {
- auto callid = msg[1].toString().toInt();
- message = new ResponseMessage(m_calls[callid]);
- type = MessageType::ResponseRequestMessage;
+ unsigned int callid;
+ if (message->isReply() && message->getCallId(&callid)) {
+ message->setAdditionalData(m_calls[callid]);
m_calls.remove(callid);
- break;
- }
- case Event: {
- QStringList api_str_list = msg[1].toString().split(QRegExp("/"));
- QString api = api_str_list[0].toLower();
-
- // FIXME: This should be rewritten using a factory class with a
- // parser parameter to remove API specific handling here
- if (api == "bluetooth-manager") {
- message = new BluetoothMessage;
- type = MessageType::BluetoothEventMessage;
- } else if (api == "bluetooth-pbap") {
- message = new PbapMessage;
- type = MessageType::PbapEventMessage;
- } else if (api == "telephony") {
- message = new TelephonyMessage;
- type = MessageType::TelephonyEventMessage;
- } else if (api == "weather") {
- message = new WeatherMessage;
- type = MessageType::WeatherEventMessage;
- } else if (api == "mediaplayer") {
- message = new MediaplayerMessage;
- type = MessageType::MediaplayerEventMessage;
- } else if (api == "navigation") {
- message = new NavigationMessage;
- type = MessageType::NavigationEventMessage;
- } else if (api == "network-manager") {
- message = new NetworkMessage;
- type = MessageType::NetworkEventMessage;
- } else if (api == "radio") {
- message = new RadioMessage;
- type = MessageType::RadioEventMessage;
- } else if (api == "bluetooth-map") {
- message = new MapMessage;
- type = MessageType::MapEventMessage;
- } else if (api == "vshl-core" ) {
- message = new VoiceMessage;
- type = MessageType::VoiceEventMessage;
- } else if (api == "vshl-capabilities" ) {
- // NOTE: Will need to look at event name to differentiate
- // capabilities if more support (e.g. navigation or
- // local media control) is added.
- message = new GuiMetadataCapabilityMessage;
- type = MessageType::GuiMetadataCapabilityEventMessage;
- } else if (api == "signal-composer") {
- message = new SignalComposerMessage;
- type = MessageType::SignalComposerEventMessage;
- } else if (api == "hvac") {
- message = new HVACMessage;
- type = MessageType::HVACEventMessage;
- } else {
- message = new Message;
- type = MessageType::GenericMessage;
- }
- break;
- }
- default:
- break;
- }
-
- if (message->fromJDoc(jdoc) == false) {
- delete message;
- return;
}
- emit messageReceived(type, message);
+ if (message->isComplete())
+ emit messageReceived(message);
}