aboutsummaryrefslogtreecommitdiffstats
path: root/app/taskmanager.cpp
diff options
context:
space:
mode:
authorVitaly Wool <vitaly.wool@konsulko.com>2018-12-03 21:18:49 +0000
committerVitaly Wool <vitaly.wool@konsulko.com>2018-12-03 21:20:42 +0000
commit2e03aeb732646f30c4a946a894e29eefb256b586 (patch)
treebd8ddd5148a0b2a20c22333ebe31d2de2b67ad8c /app/taskmanager.cpp
parentd466bf191f581f4e3a3d1988845bffad01f7b3e1 (diff)
taskmanager: comunicate using libqtappfw
Use libqtappfw to avoid dealing with websocket protocol directly. Change-Id: I67813b38ffba3e54d52e61f60644ee101426e38a Signed-off-by: Vitaly Wool <vitaly.wool@konsulko.com>
Diffstat (limited to 'app/taskmanager.cpp')
-rw-r--r--app/taskmanager.cpp79
1 files changed, 39 insertions, 40 deletions
diff --git a/app/taskmanager.cpp b/app/taskmanager.cpp
index be95fd7..03a39bb 100644
--- a/app/taskmanager.cpp
+++ b/app/taskmanager.cpp
@@ -5,69 +5,68 @@
#include <unistd.h>
#include <iostream>
#include <QtCore>
+#include <message.h>
#include "taskmanager.h"
-TaskManager::TaskManager(QObject* parent) : QObject(parent) {
- connect(&m_socket, SIGNAL(textMessageReceived(QString)), this, SLOT(onSocketTextReceived(QString)));
+TaskManager::TaskManager(QObject* parent) : QObject(parent), m_loop(nullptr) {
}
-void TaskManager::open(const QUrl &bindingAddress) {
- m_socket.open(bindingAddress);
+TaskManager::~TaskManager() {
+ delete m_loop;
+}
+
+void TaskManager::open(const QUrl &bindingAddress)
+{
+ m_loop = new MessageEngine(bindingAddress);
+ QObject::connect(m_loop, &MessageEngine::connected, this, &TaskManager::onConnected);
+ QObject::connect(m_loop, &MessageEngine::messageReceived, this, &TaskManager::onMessageReceived);
+}
+
+void TaskManager::onConnected()
+{
+ query(); // let's not wait 3 seconds and issue the first call directly
timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(query()));
timer->start(3000);
}
-
void TaskManager::kill(int tid) {
- callService(QString("kill_process"), QJsonValue(tid));
+ callService("kill_process", QJsonValue(tid));
}
void TaskManager::query() {
- callService(QString("get_process_list"), QJsonValue());
+ callService("get_process_list",
+ QJsonValue(QJsonObject({{"processes", QJsonValue()}})));
}
void TaskManager::callService(const QString& command, QJsonValue value) {
- QJsonArray msg;
- msg.append(2); // Call
- msg.append(QString::number(m_nextCallId));
- msg.append(QString("taskmanager/") + command);
- msg.append(value);
- m_nextCallId++;
-
- QJsonDocument jsonDoc;
- jsonDoc.setArray(msg);
-
- m_socket.sendTextMessage(jsonDoc.toJson(QJsonDocument::Compact));
+ Message *msg = new Message();
+ msg->createRequest("taskmanager", command, value);
+ qDebug() << "sending message " << msg->toJson();
+ m_loop->sendMessage(msg);
+ delete msg;
}
-void TaskManager::onSocketTextReceived(QString msg)
+void TaskManager::onMessageReceived(MessageType type, Message *message)
{
-
- QJsonDocument doc = QJsonDocument::fromJson(msg.toUtf8());
- QJsonArray arr = doc.array();
-
- switch(arr[0].toInt()) {
- case 3: // RetOK
- case 4: // RetErr
- ProcessResponse(arr[0].toInt() == 3, arr[2]);
- break;
- }
+ if (type == ResponseRequestMessage)
+ ProcessResponse(message);
}
-void TaskManager::ProcessResponse(bool r, const QJsonValue& val)
+void TaskManager::ProcessResponse(Message *message)
{
std::vector<ProcInfo> procs;
-
- if (r) {
- QJsonObject ret_val = val.toObject();
- QJsonObject response = ret_val["response"].toObject();
- QJsonArray processes = response["processes"].toArray();
- for(auto it = processes.constBegin(); it != processes.constEnd(); ++it)
- {
- ProcInfo Proc(it->toObject());
- procs.push_back(Proc);
- }
+ qDebug() << "got message " << message->toJson();
+ QJsonArray processes = message->replyData()["processes"].toArray();
+
+ if (processes.size() == 0) {
+ // this is not a valid process list response
+ return;
+ }
+
+ for(auto it = processes.constBegin(); it != processes.constEnd(); ++it) {
+ ProcInfo Proc(it->toObject());
+ procs.push_back(Proc);
}
int flag;