summaryrefslogtreecommitdiffstats
path: root/voice/voice.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 /voice/voice.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 'voice/voice.cpp')
-rw-r--r--voice/voice.cpp105
1 files changed, 62 insertions, 43 deletions
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 <QDebug>
#include <QStringList>
-#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<Message> msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call);
+ if (!msg)
+ return;
+
+ CallMessage *vmsg = static_cast<CallMessage*>(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<Message> msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call);
+ if (!msg)
+ return;
+
+ CallMessage *vmsg = static_cast<CallMessage*>(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<Message> msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call);
+ if (!msg)
+ return;
+
+ CallMessage *vmsg = static_cast<CallMessage*>(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<Message> msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call);
+ if (!msg)
+ return;
+
+ CallMessage *vmsg = static_cast<CallMessage*>(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<Message> msg)
{
- const QString str = vmsg->eventName();
- QJsonObject data = vmsg->eventData();
+ std::shared_ptr<EventMessage> emsg = std::static_pointer_cast<EventMessage>(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<VoiceAgentRegistry::ServiceAuthState>(
@@ -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<VoiceAgentRegistry::AgentConnectionState>(
m_var->stringToEnum(state, "AgentConnectionState")));
return;
}
- else if (vmsg->isDialogStateEvent()) {
+ else if (ename.contains("voice_dialogstate_event")) {
m_var->setDialogState(
agentId,
static_cast<VoiceAgentRegistry::VoiceDialogState>(
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<Message> msg)
{
+ std::shared_ptr<ResponseMessage> rmsg = std::static_pointer_cast<ResponseMessage>(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<Message> msg)
{
- if (msg->isEvent() && type == MessageType::VoiceEventMessage) {
- processEvent(qobject_cast<VoiceMessage*>(msg));
- } else if (msg->isReply() && (type == MessageType::ResponseRequestMessage)) {
- processReply(qobject_cast<ResponseMessage*>(msg));
+ if (msg->isEvent()) {
+ processEvent(msg);
+ } else if (msg->isReply()) {
+ processReply(msg);
} else
- qWarning() << "Received unknown message type:" << static_cast<double>(type);
- msg->deleteLater();
+ qWarning() << "Received invalid inbound message";
}