-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResourceProviderNetworkServicePolicy.cpp
272 lines (237 loc) · 8.49 KB
/
ResourceProviderNetworkServicePolicy.cpp
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <QString>
#include <QCoreApplication>
#include <QStandardPaths>
#include <QDir>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <algorithm>
#include <QUrl>
#include "Commons.h"
#include "ResourceProviderNetworkServicePolicy.h"
ResourceProviderNetworkServicePolicy::ResourceProviderNetworkServicePolicy() :
m_url_management("http://127.0.0.1:8080") {
QString appDir = qApp->applicationDirPath();
static const QString urlFile = "url.cfg";
QString cfgPath = appDir + QDir::separator() + urlFile;
QFile cfgF(cfgPath);
do {
if (!cfgF.open(QFile::ReadOnly))
break;
QString tmp = cfgF.readAll();
QUrl turl;
if (turl.isEmpty() || !turl.isValid())
break;
m_url_management = tmp;
} while (0);
}
ResourceProviderNetworkServicePolicy::~ResourceProviderNetworkServicePolicy() {
}
///////////////////////////////////////////////////////
ResourceProviderNetworkServicePolicy&
ResourceProviderNetworkServicePolicy::Instance() {
static ResourceProviderNetworkServicePolicy intance_;
return intance_;
}
///////////////////////////////////////////////////////
size_t
ResourceProviderNetworkServicePolicy::updateResWriteFunc(void *ptr,
size_t size,
size_t nmemb,
std::string* data) {
data->append(static_cast<char*>(ptr), size * nmemb);
return size * nmemb;
}
///////////////////////////////////////////////////////
size_t
ResourceProviderNetworkServicePolicy::downloadImgWriteFunc(void *ptr,
size_t size,
size_t nmemb,
void *stream) {
size_t written = std::fwrite(ptr, size, nmemb, static_cast<std::FILE*>(stream));
return written;
}
///////////////////////////////////////////////////////
void
ResourceProviderNetworkServicePolicy::updateListOfResourcesNetworkImpl() {
const QString urlStr(QString("%1/api/resources").arg(m_url_management));
CURL *hCurl = curl_easy_init();
if (!hCurl) {
//!todo log
return;
}
Defer curlCleanup(std::bind([&hCurl](){curl_easy_cleanup(hCurl);}));
curl_easy_setopt(hCurl, CURLOPT_URL, urlStr.toStdString().c_str());
curl_easy_setopt(hCurl, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(hCurl, CURLOPT_USERAGENT, "curl/7.42.0");
curl_easy_setopt(hCurl, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(hCurl, CURLOPT_TCP_KEEPALIVE, 1L);
std::string strResponse;
curl_easy_setopt(hCurl, CURLOPT_WRITEFUNCTION, updateResWriteFunc);
curl_easy_setopt(hCurl, CURLOPT_WRITEDATA, &strResponse);
long rc;
curl_easy_getinfo(hCurl, CURLINFO_RESPONSE_CODE, &rc);
curl_easy_perform(hCurl);
if (rc != CURLE_OK) {
return;
}
QByteArray arr(strResponse.c_str(), strResponse.size());
QJsonDocument doc = QJsonDocument::fromJson(arr);
if (doc.isNull() || doc.isEmpty() || !doc.isArray()) {
//!todo log at least
return;
}
QJsonArray jsonArr = doc.array();
std::vector<InternetResource> lstResources;
for (auto it : jsonArr) {
if (!it.isObject())
continue;
QJsonObject obj = it.toObject();
InternetResource ir;
if (!parseReply(obj, ir))
continue;
ir.ix = lstResources.size();
lstResources.push_back(ir);
} //for it : jsonArr
m_lstResources = lstResources;
}
///////////////////////////////////////////////////////
void
ResourceProviderNetworkServicePolicy::updateListOfResourcesLocalImpl() {
QString appDir = qApp->applicationDirPath();
static const QString urlFile = "res.cfg";
QString cfgPath = appDir + QDir::separator() + urlFile;
QFile cfgF(cfgPath);
if (!cfgF.open(QFile::ReadOnly))
return;
QByteArray data = cfgF.readAll();
QJsonDocument doc = QJsonDocument::fromJson(data);
if (doc.isNull() || doc.isEmpty() || !doc.isArray()) {
//!todo log at least
return;
}
QJsonArray jsonArr = doc.array();
std::vector<InternetResource> lstResources;
for (auto it : jsonArr) {
if (!it.isObject())
continue;
QJsonObject obj = it.toObject();
InternetResource ir;
if (!parseLocal(obj, ir))
continue;
ir.ix = lstResources.size();
lstResources.push_back(ir);
} //for it : jsonArr
m_lstResources = lstResources;
}
///////////////////////////////////////////////////////
QString
ResourceProviderNetworkServicePolicy::downloadImgToTemp(const QString &imgUrlStr,
bool &success) {
success = false;
QString tmpPathDir = QCoreApplication::instance()->applicationDirPath();
QStringList lstTempLocations = QStandardPaths::standardLocations(QStandardPaths::TempLocation);
if (!lstTempLocations.empty())
tmpPathDir = lstTempLocations[0];
QString fileName = imgUrlStr;
fileName.replace("\\", "_").replace("/", "_").replace(":", "_");
fileName = QString("img_%1.pix").arg(fileName);
QString fullFilePath = tmpPathDir + QDir::separator() + fileName;
if (QFile::exists(fullFilePath)) {
success = true;
return fullFilePath;
}
CURL *hCurl;
std::FILE *fImg;
/* init the curl session */
hCurl = curl_easy_init();
if (!hCurl) {
//!todo log
success = false;
return "";
}
Defer curlCleanup(std::bind([&hCurl](){curl_easy_cleanup(hCurl);}));
fImg = std::fopen(fullFilePath.toStdString().c_str(), "wb");
if (!fImg) {
//!todo log
success = false;
return "";
}
Defer imgClose(std::bind([&fImg](){std::fclose(fImg);}));
curl_easy_setopt(hCurl, CURLOPT_URL, imgUrlStr.toStdString().c_str());
curl_easy_setopt(hCurl, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(hCurl, CURLOPT_USERAGENT, "curl/7.42.0");
curl_easy_setopt(hCurl, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(hCurl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(hCurl, CURLOPT_TCP_KEEPALIVE, 1L);
curl_easy_setopt(hCurl, CURLOPT_WRITEFUNCTION, downloadImgWriteFunc);
curl_easy_setopt(hCurl, CURLOPT_WRITEDATA, fImg);
long rc;
curl_easy_getinfo(hCurl, CURLINFO_RESPONSE_CODE, &rc);
curl_easy_perform(hCurl);
if (rc != CURLE_OK) {
success = false;
return "";
}
success = true;
return fullFilePath;
}
///////////////////////////////////////////////////////
bool
ResourceProviderNetworkServicePolicy::parseReply(const QJsonObject &obj,
InternetResource &ir) {
if (obj.find("url") == obj.end())
return false;
ir.url = obj["url"].toString();
if (obj.find("skip_hostname_verification") != obj.end())
ir.skip_hostname_verification = obj["skip_hostname_verification"].toInt(0) != 0;
if (obj.find("skip_peer_verification") != obj.end())
ir.skip_peer_verification = obj["skip_peer_verification"].toInt(0) != 0;
if (obj.find("timeout") != obj.end())
ir.timeout_ms = static_cast<uint32_t>(obj["timeout"].toInt(3000));
if (obj.find("title") != obj.end())
ir.name = obj["title"].toString();
do {
if (obj.find("picture") == obj.end())
break;
bool d = false;
QString path = Instance().downloadImgToTemp(obj["picture"].toString(), d);
if (!d)
break;
ir.img_path = path;
} while (0);
return true;
}
bool
ResourceProviderNetworkServicePolicy::parseLocal(const QJsonObject &obj,
InternetResource &ir) {
if (obj.find("url") == obj.end())
return false;
//if resource is not active just skip it
if (obj.find("active") != obj.end()) {
if (!obj["active"].toBool())
return false;
}
ir.url = obj["url"].toString();
if (obj.find("skip_hostname_verification") != obj.end())
ir.skip_hostname_verification = obj["skip_hostname_verification"].toInt(0) != 0;
if (obj.find("skip_peer_verification") != obj.end())
ir.skip_peer_verification = obj["skip_peer_verification"].toInt(0) != 0;
if (obj.find("timeout") != obj.end())
ir.timeout_ms = static_cast<uint32_t>(obj["timeout"].toInt(3000));
if (obj.find("title") != obj.end())
ir.name = obj["title"].toString();
if (obj.find("picture") != obj.end())
ir.img_path = obj["picture"].toString();
return true;
}
///////////////////////////////////////////////////////
std::vector<InternetResource>
ResourceProviderNetworkServicePolicy::updateListOfResources() {
Instance().updateListOfResourcesNetworkImpl();
if (Instance().m_lstResources.empty()) {
Instance().updateListOfResourcesLocalImpl();
}
return Instance().m_lstResources; //copy of vector
}
///////////////////////////////////////////////////////