diff options
author | Raquel Medina <raquel.medina@konsulko.com> | 2020-03-18 23:56:31 +0100 |
---|---|---|
committer | Raquel Medina <raquel.medina@konsulko.com> | 2020-03-23 14:02:28 +0100 |
commit | 0ed292d3ccf93c889734960676a321d1166d3f66 (patch) | |
tree | b8d64d1685d2f4bf599ab3867a0d3ff557ff0479 /signal-composer | |
parent | 5c750385d02116a92fa4c120ccc26abb8267bc97 (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 'signal-composer')
-rw-r--r-- | signal-composer/CMakeLists.txt | 4 | ||||
-rw-r--r-- | signal-composer/signalcomposer.cpp | 74 | ||||
-rw-r--r-- | signal-composer/signalcomposer.h | 5 |
3 files changed, 45 insertions, 38 deletions
diff --git a/signal-composer/CMakeLists.txt b/signal-composer/CMakeLists.txt index 18f3a0a..f517d27 100644 --- a/signal-composer/CMakeLists.txt +++ b/signal-composer/CMakeLists.txt @@ -1,2 +1,2 @@ -add_headers(signalcomposer.h signalcomposermessage.h) -add_sources(signalcomposer.cpp signalcomposermessage.cpp) +add_headers(signalcomposer.h) +add_sources(signalcomposer.cpp) diff --git a/signal-composer/signalcomposer.cpp b/signal-composer/signalcomposer.cpp index 59395f5..1296162 100644 --- a/signal-composer/signalcomposer.cpp +++ b/signal-composer/signalcomposer.cpp @@ -16,8 +16,9 @@ #include <QDebug> -#include "message.h" -#include "signalcomposermessage.h" +#include "callmessage.h" +#include "eventmessage.h" +#include "messagefactory.h" #include "messageengine.h" #include "signalcomposer.h" @@ -40,56 +41,63 @@ SignalComposer::~SignalComposer() void SignalComposer::onConnected() { QStringListIterator eventIterator(events); - SignalComposerMessage *tmsg; while (eventIterator.hasNext()) { - tmsg = new SignalComposerMessage(); + std::unique_ptr<Message> msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call); + if (!msg) + return; + + CallMessage* tmsg = static_cast<CallMessage*>(msg.get()); QJsonObject parameter; parameter.insert("signal", eventIterator.next()); - tmsg->createRequest("subscribe", parameter); - m_mloop->sendMessage(tmsg); - delete tmsg; + tmsg->createRequest("signal-composer", "subscribe", parameter); + m_mloop->sendMessage(std::move(msg)); } } void SignalComposer::onDisconnected() { QStringListIterator eventIterator(events); - SignalComposerMessage *tmsg; while (eventIterator.hasNext()) { - tmsg = new SignalComposerMessage(); + std::unique_ptr<Message> msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call); + if (!msg) + return; + + CallMessage* tmsg = static_cast<CallMessage*>(msg.get()); QJsonObject parameter; parameter.insert("signal", eventIterator.next()); - tmsg->createRequest("unsubscribe", parameter); - m_mloop->sendMessage(tmsg); - delete tmsg; + tmsg->createRequest("signal-composer", "unsubscribe", parameter); + m_mloop->sendMessage(std::move(msg)); } } -void SignalComposer::onMessageReceived(MessageType type, Message *message) +void SignalComposer::onMessageReceived(std::shared_ptr<Message> msg) { - if (type == MessageType::SignalComposerEventMessage) { - SignalComposerMessage *tmsg = qobject_cast<SignalComposerMessage*>(message); + if (!msg) + return; + + if (msg->isEvent()) { + std::shared_ptr<EventMessage> emsg = std::static_pointer_cast<EventMessage>(msg); + if (emsg->eventApi() != "signal-composer") + return; - if (tmsg->isEvent()) { - QString uid = tmsg->eventData().value("uid").toString(); - QVariant v = tmsg->eventData().value("value").toVariant(); - QString value; - if(v.canConvert(QMetaType::QString)) - value = v.toString(); - else - qWarning() << "Unconvertible value type for uid " << uid; - QString units = tmsg->eventData().value("unit").toString(); - v = tmsg->eventData().value("timestamp").toVariant(); - quint64 timestamp = 0; - if(v.canConvert(QMetaType::ULongLong)) - timestamp = v.toULongLong(); - else - qWarning() << "Unconvertible timestamp type for uid " << uid; + QJsonObject data = emsg->eventData(); + QString uid = data.value("uid").toString(); + QVariant v = data.value("value").toVariant(); + QString value; + if(v.canConvert(QMetaType::QString)) + value = v.toString(); + else + qWarning() << "Unconvertible value type for uid " << uid; + QString units = data.value("unit").toString(); + v = data.value("timestamp").toVariant(); + quint64 timestamp = 0; + if(v.canConvert(QMetaType::ULongLong)) + timestamp = v.toULongLong(); + else + qWarning() << "Unconvertible timestamp type for uid " << uid; - emit signalEvent(uid, value, units, timestamp); - } + emit signalEvent(uid, value, units, timestamp); } - message->deleteLater(); } diff --git a/signal-composer/signalcomposer.h b/signal-composer/signalcomposer.h index 13d26c6..382b6e9 100644 --- a/signal-composer/signalcomposer.h +++ b/signal-composer/signalcomposer.h @@ -17,14 +17,13 @@ #ifndef SIGNALCOMPOSER_H #define SIGNALCOMPOSER_H +#include <memory> #include <QObject> #include <QJsonArray> class MessageEngine; class Message; -enum class MessageType; - class SignalComposer : public QObject { Q_OBJECT @@ -41,7 +40,7 @@ class SignalComposer : public QObject void onConnected(); void onDisconnected(); - void onMessageReceived(MessageType, Message*); + void onMessageReceived(std::shared_ptr<Message>); const QStringList events { "event.vehicle.speed", |