summaryrefslogtreecommitdiffstats
path: root/messageengine.cpp
diff options
context:
space:
mode:
authorMatt Ranostay <matt.ranostay@konsulko.com>2018-05-01 23:15:58 -0700
committerMatt Ranostay <matt.ranostay@konsulko.com>2018-05-13 17:43:33 -0700
commitbb1aee701bc3516c2072a79d3129ee83918df7fd (patch)
tree150edc50142429ee566edf629a8d3f5dfb3a8557 /messageengine.cpp
parent6465961f28f40ff5a2da31876f1c23ea3c7f7cec (diff)
message: add response message support
Add support for callid sequencing, and request data being included with the response message. Bug-AGL: SPEC-1404 Change-Id: I259ae3da68e55de17eafe9db534258db5fbc3ef8 Signed-off-by: Matt Ranostay <matt.ranostay@konsulko.com>
Diffstat (limited to 'messageengine.cpp')
-rw-r--r--messageengine.cpp77
1 files changed, 57 insertions, 20 deletions
diff --git a/messageengine.cpp b/messageengine.cpp
index 4cc5bf3..87c2630 100644
--- a/messageengine.cpp
+++ b/messageengine.cpp
@@ -18,6 +18,7 @@
#include "messageengine.h"
#include "bluetoothmessage.h"
#include "mediaplayermessage.h"
+#include "responsemessage.h"
#include "telephonymessage.h"
#include "weathermessage.h"
@@ -25,6 +26,7 @@
MessageEngine::MessageEngine(const QUrl &url, QObject *parent) :
QObject(parent),
+ m_callid(0),
m_url(url)
{
connect(&m_websocket, &QWebSocket::connected, this, &MessageEngine::onConnected);
@@ -33,15 +35,32 @@ 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)
{
if (!message->isValid())
return false;
- qint64 size = m_websocket.sendTextMessage(message->toJson().data());
+ auto callid = requestCallId();
+ message->setCallId(callid);
+
+ QByteArray data = message->toJson().data();
+ qint64 size = m_websocket.sendTextMessage(data);
if (size == 0)
return false;
+ m_calls.insert(callid, data);
+
return true;
}
@@ -67,28 +86,46 @@ void MessageEngine::onTextMessageReceived(QString jsonStr)
}
QJsonArray msg = jdoc.array();
- QStringList api_str_list = msg[1].toString().split(QRegExp("/"));
- QString api = api_str_list[0];
+ int msgid = msg[0].toInt();
Message *message;
MessageType type;
- // 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 = BluetoothEventMessage;
- } else if (api == "telephony") {
- message = new TelephonyMessage;
- type = TelephonyEventMessage;
- } else if (api == "weather") {
- message = new WeatherMessage;
- type = WeatherEventMessage;
- } else if (api == "mediaplayer") {
- message = new MediaplayerMessage;
- type = MediaplayerEventMessage;
- } else {
- message = new Message;
- type = GenericMessage;
+
+ switch (msgid) {
+ case RetOk:
+ case RetErr: {
+ auto callid = msg[1].toString().toInt();
+ message = new ResponseMessage(m_calls[callid]);
+ type = ResponseRequestMessage;
+ m_calls.remove(callid);
+ break;
+ }
+ case Event: {
+ QStringList api_str_list = msg[1].toString().split(QRegExp("/"));
+ QString api = api_str_list[0];
+
+ // 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 = BluetoothEventMessage;
+ } else if (api == "telephony") {
+ message = new TelephonyMessage;
+ type = TelephonyEventMessage;
+ } else if (api == "weather") {
+ message = new WeatherMessage;
+ type = WeatherEventMessage;
+ } else if (api == "mediaplayer") {
+ message = new MediaplayerMessage;
+ type = MediaplayerEventMessage;
+ } else {
+ message = new Message;
+ type = GenericMessage;
+ }
+ break;
+ }
+ default:
+ break;
}
if (message->fromJDoc(jdoc) == false) {