diff options
author | Vitaly Wool <vitaly.wool@konsulko.com> | 2018-12-03 21:18:49 +0000 |
---|---|---|
committer | Vitaly Wool <vitaly.wool@konsulko.com> | 2018-12-03 21:20:42 +0000 |
commit | 2e03aeb732646f30c4a946a894e29eefb256b586 (patch) | |
tree | bd8ddd5148a0b2a20c22333ebe31d2de2b67ad8c | |
parent | d466bf191f581f4e3a3d1988845bffad01f7b3e1 (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>
-rw-r--r-- | app/taskmanager.cpp | 79 | ||||
-rw-r--r-- | app/taskmanager.h | 19 |
2 files changed, 49 insertions, 49 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; diff --git a/app/taskmanager.h b/app/taskmanager.h index da4f725..4049f9b 100644 --- a/app/taskmanager.h +++ b/app/taskmanager.h @@ -4,7 +4,7 @@ #include <QStringList> #include <QVector> #include <QtCore> -#include <QWebSocket> +#include <messageengine.h> #include "procinfo.h" #ifndef TASKMANAGER_H @@ -15,11 +15,12 @@ class TaskManager : public QObject Q_OBJECT public: - explicit TaskManager(QObject* parent = nullptr); + explicit TaskManager(QObject* parent = nullptr); + virtual ~TaskManager(); - Q_INVOKABLE void open(const QUrl& url); - Q_INVOKABLE void kill(int tid); - QTimer *timer; + Q_INVOKABLE void open(const QUrl& url); + Q_INVOKABLE void kill(int tid); + QTimer *timer; signals: void updateProcess(const QString& cmd_, int tid_, int euid_, double scpu_, double ucpu_, double resident_memory_, const QString& state_); @@ -29,14 +30,14 @@ signals: private slots: void query(); void callService(const QString& ccommand, QJsonValue value); - void onSocketTextReceived(QString msg); + void onConnected(); + void onMessageReceived(MessageType type, Message *message); private: - QWebSocket m_socket; - int m_nextCallId; + MessageEngine *m_loop; std::vector<ProcInfo> m_procinfos; - void ProcessResponse(bool r, const QJsonValue &val); + void ProcessResponse(Message *message); }; #endif // TASKMANAGER_H |