From 0ed292d3ccf93c889734960676a321d1166d3f66 Mon Sep 17 00:00:00 2001 From: Raquel Medina Date: Wed, 18 Mar 2020 23:56:31 +0100 Subject: 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 Change-Id: I3a7a6325209ddeca2293f1ac745371861a947bfb --- voice/CMakeLists.txt | 4 +- voice/voice.cpp | 105 ++++++++++++++++++++++++++++++--------------------- voice/voice.h | 15 +++----- 3 files changed, 70 insertions(+), 54 deletions(-) (limited to 'voice') diff --git a/voice/CMakeLists.txt b/voice/CMakeLists.txt index 61b8fe2..6363c3b 100644 --- a/voice/CMakeLists.txt +++ b/voice/CMakeLists.txt @@ -1,4 +1,4 @@ -add_headers(voice.h voicemessage.h +add_headers(voice.h voiceagentregistry.h voiceagentprofile.h voiceagentmodel.h) -add_sources(voice.cpp voicemessage.cpp +add_sources(voice.cpp voiceagentregistry.cpp voiceagentprofile.cpp voiceagentmodel.cpp) diff --git a/voice/voice.cpp b/voice/voice.cpp index 3c93960..5961882 100644 --- a/voice/voice.cpp +++ b/voice/voice.cpp @@ -16,9 +16,10 @@ #include #include -#include "message.h" +#include "callmessage.h" #include "responsemessage.h" -#include "voicemessage.h" +#include "eventmessage.h" +#include "messagefactory.h" #include "messageengine.h" #include "voiceagentregistry.h" #include "voice.h" @@ -46,12 +47,15 @@ Voice::~Voice() void Voice::scan() { - VoiceMessage *vmsg = new VoiceMessage(); + std::unique_ptr msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call); + if (!msg) + return; + + CallMessage *vmsg = static_cast(msg.get()); QJsonObject parameter; - vmsg->createRequest("enumerateVoiceAgents", parameter); - m_loop->sendMessage(vmsg); - delete vmsg; + vmsg->createRequest("vshl-core", "enumerateVoiceAgents", parameter); + m_loop->sendMessage(std::move(msg)); } void Voice::getCBLpair(QString id) @@ -61,41 +65,50 @@ void Voice::getCBLpair(QString id) void Voice::subscribeAgentToVshlEvents(QString id) { + std::unique_ptr msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call); + if (!msg) + return; + + CallMessage *vmsg = static_cast(msg.get()); QJsonArray events = QJsonArray::fromStringList(vshl_events); - VoiceMessage *vmsg = new VoiceMessage(); QJsonObject parameter; parameter.insert("va_id", id); parameter.insert("events", events); - vmsg->createRequest("subscribe", parameter); - m_loop->sendMessage(vmsg); - delete vmsg; + vmsg->createRequest("vshl-core", "subscribe", parameter); + m_loop->sendMessage(std::move(msg)); } void Voice::unsubscribeAgentFromVshlEvents(QString id) { + std::unique_ptr msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call); + if (!msg) + return; + + CallMessage *vmsg = static_cast(msg.get()); QJsonArray events = QJsonArray::fromStringList(vshl_events); - VoiceMessage *vmsg = new VoiceMessage(); QJsonObject parameter; parameter.insert("va_id", id); parameter.insert("events", events); - vmsg->createRequest("unsubscribe", parameter); - m_loop->sendMessage(vmsg); - delete vmsg; + vmsg->createRequest("vshl-core", "unsubscribe", parameter); + m_loop->sendMessage(std::move(msg)); } void Voice::triggerCBLProcess(QString id) { + std::unique_ptr msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call); + if (!msg) + return; + + CallMessage *vmsg = static_cast(msg.get()); QJsonArray events; - VoiceMessage *vmsg = new VoiceMessage(); QJsonObject parameter; parameter.insert("va_id", id); parameter.insert("events", events); - vmsg->createRequest("subscribeToLoginEvents", parameter); - m_loop->sendMessage(vmsg); - delete vmsg; + vmsg->createRequest("vshl-core", "subscribeToLoginEvents", parameter); + m_loop->sendMessage(std::move(msg)); } void Voice::parseAgentsList(QJsonArray agents) @@ -109,14 +122,20 @@ void Voice::parseAgentsList(QJsonArray agents) -void Voice::processEvent(VoiceMessage *vmsg) +void Voice::processEvent(std::shared_ptr msg) { - const QString str = vmsg->eventName(); - QJsonObject data = vmsg->eventData(); + std::shared_ptr emsg = std::static_pointer_cast(msg); + QString eapi = emsg->eventApi(); + + if (eapi != "vshl-core") + return; + + QString ename = emsg->eventName(); + QJsonObject data = emsg->eventData(); QString agentId = data.value("va_id").toString(); QString state = data.value("state").toString(); - if (vmsg->isAuthStateEvent()) { + if (ename.contains("voice_authstate_event")) { m_var->setAuthState( agentId, static_cast( @@ -124,47 +143,48 @@ void Voice::processEvent(VoiceMessage *vmsg) return; } - else if (vmsg->isConnectionStateEvent()) { + else if (ename.contains("voice_connectionstate_event")) { m_var->setConnectionState( agentId, static_cast( m_var->stringToEnum(state, "AgentConnectionState"))); return; } - else if (vmsg->isDialogStateEvent()) { + else if (ename.contains("voice_dialogstate_event")) { m_var->setDialogState( agentId, static_cast( m_var->stringToEnum(state, "VoiceDialogState"))); return; } - else if (vmsg->isCblEvent()) { + else if (ename.contains("cbl")) { QJsonObject payload = data.value("payload").toObject(); QString url = payload.value("url").toString(); QString code = payload.value("code").toString(); - if (str.contains("expired")) + if (ename.contains("expired")) m_var->updateLoginData(agentId, code, url, true); - else if (str.contains("received")) { + else if (ename.contains("received")) { m_var->updateLoginData(agentId, code, url, false); } else qWarning() << "Unknown cbl event"; return; } - qWarning() << "Unknown vshl event:" << str; + qWarning() << "Unknown vshl event:" << ename; } -void Voice::processReply(ResponseMessage *rmsg) +void Voice::processReply(std::shared_ptr msg) { + std::shared_ptr rmsg = std::static_pointer_cast(msg); + QString verb = rmsg->requestVerb(); + QJsonObject data = rmsg->replyData(); if (rmsg->replyStatus() == "failed") { - qWarning() << "Reply Failed received for verb:" << rmsg->requestVerb(); - } else if (rmsg->requestVerb() == "enumerateVoiceAgents") { - parseAgentsList(rmsg->replyData().value("agents").toArray()); - m_var->setDefaultId( - rmsg->replyData().value("default").toString()); + qWarning() << "Reply Failed received for verb:" << verb; + } else if (verb == "enumerateVoiceAgents") { + parseAgentsList(data.value("agents").toArray()); + m_var->setDefaultId(data.value("default").toString()); } else - qDebug() << "discarding reply received for verb:" << - rmsg->requestVerb(); + qDebug() << "discarding reply received for verb:" << verb; } void Voice::onConnected() @@ -180,13 +200,12 @@ void Voice::onDisconnected() unsubscribeAgentFromVshlEvents(*it); } -void Voice::onMessageReceived(MessageType type, Message *msg) +void Voice::onMessageReceived(std::shared_ptr msg) { - if (msg->isEvent() && type == MessageType::VoiceEventMessage) { - processEvent(qobject_cast(msg)); - } else if (msg->isReply() && (type == MessageType::ResponseRequestMessage)) { - processReply(qobject_cast(msg)); + if (msg->isEvent()) { + processEvent(msg); + } else if (msg->isReply()) { + processReply(msg); } else - qWarning() << "Received unknown message type:" << static_cast(type); - msg->deleteLater(); + qWarning() << "Received invalid inbound message"; } diff --git a/voice/voice.h b/voice/voice.h index e3132be..ea0649b 100644 --- a/voice/voice.h +++ b/voice/voice.h @@ -17,6 +17,7 @@ #ifndef VOICE_H #define VOICE_H +#include #include #include #include @@ -24,10 +25,6 @@ class VoiceAgentRegistry; class MessageEngine; class Message; -class ResponseMessage; -class VoiceMessage; - -enum class MessageType; class Voice : public QObject { @@ -51,16 +48,16 @@ class Voice : public QObject void unsubscribeAgentFromVshlEvents(QString id); void triggerCBLProcess(QString id); void parseAgentsList(QJsonArray agents); - void processVshlEvent(VoiceMessage *vmsg); - void processLoginEvent(VoiceMessage *vmsg); + void processVshlEvent(std::shared_ptr msg); + void processLoginEvent(std::shared_ptr msg); - void processEvent(VoiceMessage *vmsg); - void processReply(ResponseMessage *rmsg); + void processEvent(std::shared_ptr emsg); + void processReply(std::shared_ptr rmsg); // slots void onConnected(); void onDisconnected(); - void onMessageReceived(MessageType type, Message *msg); + void onMessageReceived(std::shared_ptr msg); const QStringList vshl_events { "voice_authstate_event", -- cgit 1.2.3-korg