forked from frida/cryptoshark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.cpp
More file actions
111 lines (90 loc) · 3.24 KB
/
router.cpp
File metadata and controls
111 lines (90 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "router.h"
#include "models.h"
#include <QMetaMethod>
Router *Router::s_instance = nullptr;
Router::Router(QObject *parent) :
QObject(parent),
m_agent(nullptr),
m_nextRequestId(1)
{
}
Router::~Router()
{
s_instance = nullptr;
}
Router *Router::instance()
{
if (s_instance == nullptr)
s_instance = new Router();
return s_instance;
}
void Router::attach(QObject *agent)
{
auto agentMeta = agent->metaObject();
m_agent = agent;
m_postMethod = agentMeta->method(agentMeta->indexOfMethod("post(QJsonObject)"));
auto agentMessageSignal = agentMeta->method(agentMeta->indexOfSignal("message(ScriptInstance*,QJsonObject,QByteArray)"));
auto routerMeta = metaObject();
auto routerOnMessageSlot = routerMeta->method(routerMeta->indexOfSlot("onMessage(ScriptInstance*,QJsonObject,QByteArray)"));
connect(agent, agentMessageSignal, this, routerOnMessageSlot);
}
Request *Router::request(QString name, QJsonObject payload)
{
auto id = QString("r") + QString::number(m_nextRequestId++);
auto request = new Request(this);
m_requests[id] = request;
auto message = QJsonObject();
message[QStringLiteral("id")] = id;
message[QStringLiteral("name")] = name;
message[QStringLiteral("payload")] = payload;
m_postMethod.invoke(m_agent, Q_ARG(QJsonObject, message));
return request;
}
void Router::onMessage(ScriptInstance *sender, QJsonObject object, QByteArray data)
{
Q_UNUSED(sender);
Q_UNUSED(data);
bool handled = false;
if (object[QStringLiteral("type")] == QStringLiteral("send")) {
auto stanza = object[QStringLiteral("payload")].toObject();
auto idValue = stanza[QStringLiteral("id")];
if (!idValue.isNull()) {
auto id = idValue.toString();
auto request = m_requests[id];
if (request != nullptr) {
m_requests.remove(id);
request->complete(stanza[QStringLiteral("payload")]);
handled = true;
}
}
if (!handled) {
auto name = stanza[QStringLiteral("name")];
if (name == QStringLiteral("modules:update")) {
Models::instance()->modules()->update(stanza[QStringLiteral("payload")].toArray());
handled = true;
} else if (name == QStringLiteral("thread:summary")) {
auto update = stanza[QStringLiteral("payload")].toObject();
auto summary = update[QStringLiteral("summary")].toObject();
Models::instance()->functions()->addCalls(summary);
handled = true;
} else if (name == QStringLiteral("function:log")) {
auto entry = stanza[QStringLiteral("payload")].toObject();
auto id = entry[QStringLiteral("id")].toInt();
auto message = entry[QStringLiteral("message")].toString();
Models::instance()->functions()->addLogMessage(id, message);
handled = true;
}
}
}
if (!handled)
emit message(object);
}
Request::Request(QObject *parent) :
QObject(parent)
{
connect(this, &Request::completed, this, &Request::deleteLater);
}
void Request::complete(QJsonValue result)
{
emit completed(result);
}