-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpeer.cpp
More file actions
216 lines (178 loc) · 5.86 KB
/
Copy pathpeer.cpp
File metadata and controls
216 lines (178 loc) · 5.86 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright © 2011 Vinícius dos Santos Oliveira
#include "peer.h"
#include "responsehandler.h"
#include <QVariantMap>
#include <qt-json/json.h>
using namespace std;
using namespace JsonRPC;
inline bool isRequestMessage(const QVariantMap &object)
{
if (object.contains("method"))
return true;
else
return false;
}
inline bool isRequestMessage(const QVariantList &objectList)
{
if (objectList.size())
return true;
else
return false;
}
inline bool isRequestMessage(const QVariant &object)
{
switch (object.type()) {
case QVariant::Map:
return isRequestMessage(object.toMap());
case QVariant::List:
return isRequestMessage(object.toList());
default:
return false;
}
}
inline bool isResponseMessage(const QVariantMap &object)
{
if (object.contains("result")
|| object.contains("error"))
return true;
else
return false;
}
inline bool isResponseMessage(const QVariantList &objectList)
{
if (objectList.size())
return true;
else
return false;
}
inline bool isResponseMessage(const QVariant &object)
{
switch (object.type()) {
case QVariant::Map:
return isResponseMessage(object.toMap());
case QVariant::List:
return isResponseMessage(object.toList());
default:
return false;
}
}
Peer::Peer(QObject *parent) :
QObject(parent)
{
}
void Peer::handleMessage(const QByteArray &json)
{
bool ok;
QVariant object = QtJson::Json::parse(QString::fromUtf8(json), ok);
if (!ok) {
emit readyResponseMessage(static_cast<QByteArray>(Error(PARSE_ERROR)));
return;
}
if (isRequestMessage(object))
handleRequest(object);
else if (isResponseMessage(object))
handleResponse(object);
else
emit readyResponseMessage(static_cast<QByteArray>(Error(INVALID_REQUEST)));
}
void Peer::handleRequest(const QVariant &json)
{
if (json.type() != QVariant::Map) {
emit readyResponseMessage(static_cast<QByteArray>(Error(INVALID_REQUEST)));
return;
}
QVariantMap object = json.toMap();
if (!object.contains("method")) {
emit readyResponseMessage(static_cast<QByteArray>(Error(INVALID_REQUEST)));
return;
}
QVariant method = object["method"];
QSharedPointer<JsonRPC::ResponseHandler> handler(new ResponseHandler(this));
if (method.type() == QVariant::String) {
if (!handler->setMethod(method.toString())) {
emit readyResponseMessage(static_cast<QByteArray>(Error(INVALID_REQUEST)));
return;
}
} else {
emit readyResponseMessage(static_cast<QByteArray>(Error(INVALID_REQUEST)));
return;
}
if (object.contains("params")) {
QVariant params = object["params"];
if (!handler->setParams(params)) {
emit readyResponseMessage(static_cast<QByteArray>(Error(INVALID_REQUEST)));
return;
}
}
if (object.contains("id")) {
QVariant id = object["id"];
if (!handler->setId(id)) {
emit readyResponseMessage(static_cast<QByteArray>(Error(INVALID_REQUEST)));
return;
}
}
emit readyRequest(handler);
}
void Peer::reply(const QVariant &json)
{
emit readyResponseMessage(QtJson::Json::serialize(json));
}
bool Peer::call(const QString &method, const QVariant ¶ms, const QVariant &id)
{
if (method.startsWith("rpc.")
|| (params.type() != QVariant::List
&& params.type() != QVariant::Map
&& !params.isNull())
|| (id.type() != QVariant::String
&& id.type() != QVariant::Int
&& id.type() != QVariant::ULongLong
&& id.type() != QVariant::LongLong
&& id.type() != QVariant::Double
&& !id.isNull()))
return false;
QVariantMap object;
object.insert("jsonrpc", "2.0");
object.insert("method", method);
if (!params.isNull())
object.insert("params", params);
object.insert("id", id);
emit readyRequestMessage(QtJson::Json::serialize(object));
return true;
}
void Peer::handleResponse(const QVariant &json)
{
QVariantList objects;
if (json.type() == QVariant::Map) {
objects.push_back(json);
} else if (json.type() == QVariant::List) {
objects = json.toList();
} else {
return;
}
Q_FOREACH (const QVariant &object, objects) {
if (object.type() == QVariant::Map) {
QVariantMap objectMap = object.toMap();
if (objectMap.contains("result")) {
emit readyResponse(objectMap.value("result"),
objectMap.value("id"));
} else if (objectMap.contains("error")
&& objectMap.value("error").type() == QVariant::Map) {
QVariantMap errorObject = objectMap["error"].toMap();
if (!errorObject.contains("code")
|| (errorObject.value("code").type() != QVariant::Int
&& errorObject.value("code").type() != QVariant::ULongLong
&& errorObject.value("code").type() != QVariant::LongLong)
|| !errorObject.contains("message")
|| errorObject.value("message").type() != QVariant::String)
continue;
int code = errorObject["code"].toInt();
QString message = errorObject["message"].toString();
QVariant data = errorObject.contains("data") ? errorObject["data"]
: QVariant();
QVariant id = objectMap.contains("id") ? objectMap["id"]
: QVariant();
emit requestError(code, message, data, id);
}
}
}
}