-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsurvey_data.cc
134 lines (116 loc) · 3.47 KB
/
survey_data.cc
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
#include "survey_data.h"
#include <QJsonObject>
#include <QJsonArray>
QJsonValue SurveyTarget::toJson() const {
return QJsonObject{
{"name", name},
{"url", url}
};
}
bool SurveyTarget::fromJson(const QJsonValue& v) {
auto ob = v.toObject();
QJsonValue n = ob.value("name");
QJsonValue u = ob.value("url");
name = n.toString();
url = u.toString();
return n.isString() && u.isString();
}
QJsonValue SurveyResolver::toJson() const {
return QJsonObject{
{"name", name},
{"url", url}
};
}
bool SurveyResolver::fromJson(const QJsonValue& v) {
auto ob = v.toObject();
QJsonValue n = ob.value("name");
QJsonValue u = ob.value("url");
name = n.toString();
url = u.toString();
return n.isString() && u.isString();
}
SurveyResult::SurveyResult(QString t, QString r) : target(t), resolver(r), rttA(0), rttAAAA(0), rttFetch(0) {}
QJsonValue SurveyResult::toJson() const {
QJsonArray addressList;
for (auto &ip : addresses) {
addressList.append(ip.toString());
}
return QJsonObject{
{"target", target},
{"resolver", resolver},
{"addresses", addressList},
{"rtt_a", static_cast<double>(rttA)},
{"rtt_aaaa", static_cast<double>(rttAAAA)},
{"rtt_content", static_cast<double>(rttFetch)},
{"content_size", responseSize},
{"content_error", responseError}
};
}
bool SurveyResult::fromJson(const QJsonValue& v) {
auto ob = v.toObject();
QJsonArray addressList = ob.value("addresses").toArray();
for (auto ad : addressList) {
QHostAddress a(ad.toString());
addresses.append(a);
}
target = ob.value("target").toString();
resolver = ob.value("resolver").toString();
rttA = ob.value("rtt_a").toDouble();
rttAAAA = ob.value("rtt_aaaa").toDouble();
rttFetch = ob.value("rtt_content").toDouble();
responseSize = ob.value("content_size").toInt();
responseError = ob.value("content_error").toInt();
return true;
}
QJsonValue SurveyData::toJson() const {
QJsonObject response;
response.insert("meta", QJsonObject::fromVariantMap(meta));
QJsonArray targetList;
for (auto &v : targets) {
targetList.append(v.toJson());
}
response.insert("targets", targetList);
QJsonArray resolverList;
for (auto &v : resolvers) {
resolverList.append(v.toJson());
}
response.insert("resolvers", resolverList);
QJsonArray resultList;
for (auto &v : results) {
resultList.append(v.toJson());
}
response.insert("results", resultList);
return response;
}
bool SurveyData::fromJson(const QJsonValue &v) {
clear();
auto ob = v.toObject();
meta = ob.value("meta").toVariant().toMap();
auto targetList = ob.value("targets").toArray();
for (auto tv : targetList) {
SurveyTarget target;
if (!target.fromJson(tv)) {
return false;
}
targets.append(target);
targetName.insert(target.url, target.name);
}
auto resolverList = ob.value("resolvers").toArray();
for (auto rv : resolverList) {
SurveyResolver resolver;
if (!resolver.fromJson(rv)) {
return false;
}
resolvers.append(resolver);
resolverName.insert(resolver.url, resolver.name);
}
return true;
}
void SurveyData::clear() {
meta.clear();
targets.clear();
resolvers.clear();
results.clear();
resolverName.clear();
targetName.clear();
}