-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilerequests.cc
More file actions
313 lines (231 loc) · 7.26 KB
/
filerequests.cc
File metadata and controls
313 lines (231 loc) · 7.26 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <filerequests.hh>
#include <QDir>
#include <QFileInfo>
#include <QFile>
#include <QtCrypto>
#include <QDebug>
FileRequests::FileRequests(const QString& my_name)
{
me = my_name;
timerDuration = 1000;
timer.start(timerDuration);
downloadTimer.start(timerDuration);
QObject::connect(&timer, SIGNAL(timeout()),
this, SLOT(processTimeout()));
QObject::connect(&downloadTimer, SIGNAL(timeout()),
this, SLOT(processDownloadTimeout()));
}
void
FileRequests::newDownload(const QString& fileName, const QString&destination, const QByteArray& masterBlock)
{
if(!pendingDownloads.contains(masterBlock)){
QPair<QString, QString> val(fileName, destination);
pendingDownloads[masterBlock] = val;
QMap<QString, QVariant> req;
req["BlockRequest"] = masterBlock;
qDebug() << masterBlock;
qDebug() << masterBlock.size();
emit sendDownloadMsg(req, destination);
}
}
void
FileRequests::processBlockReply(const QMap<QString, QVariant> & msg)
{
QByteArray blockReply = msg["BlockReply"].toByteArray();
QString origin = msg["Origin"].toString();
QByteArray data = msg["Data"].toByteArray();
qDebug() << "Got something!";
if (verifyData(blockReply, data)){
qDebug() << "Verified!";
// Reply contains the hash meta-list
if(pendingDownloads.contains(blockReply)){
qDebug() << "FileRequests: got the hash-list";
if (!invertBlockHashes.contains(blockReply)){
QList<QByteArray> hashList;
int size = data.count();
char *buf = data.data();
int count = 0;
for(int i = 0; i < size; i += HASH_SIZE){
QByteArray temp(&buf[i], HASH_SIZE);
hashList.append(temp);
blockHashes[temp] = blockReply;
QMap<QString, QVariant> msg;
msg["BlockRequest"] = temp;
emit sendDownloadMsg(msg, origin);
++count;
}
qDebug() << "Expect " << count << " blocks";
invertBlockHashes[blockReply] = hashList;
}
}
// Reply contains a block of data
else if (blockHashes.contains(blockReply)){
qDebug() << "FileRequests: got a data block!";
if (!pendingDownloadData.contains(blockReply)){
pendingDownloadData[blockReply] = data;
QByteArray master = blockHashes[blockReply];
if(downloadCompleted(master)){
qDebug() << "Download completed!";
writeFile(master);
}
}
}
}
}
void
FileRequests::writeFile(const QByteArray &hash)
{
QDir downloadDir;
if (!downloadDir.exists("Downloads"))
downloadDir.mkdir("Downloads");
downloadDir.cd("Downloads");
QString fileName = pendingDownloads[hash].first;
QFileInfo fileinf(downloadDir, fileName);
QString bigName = fileinf.absoluteFilePath();
QFile file(bigName);
if (file.exists())
file.remove();
file.open(QIODevice::WriteOnly);
QByteArray fileData;
QList<QByteArray> hashes = invertBlockHashes[hash];
for(int i = 0; i < hashes.count(); ++i)
fileData += pendingDownloadData[hashes[i]];
file.write(fileData);
file.flush();
}
void
FileRequests::destroyDownloadData(const QByteArray &hash)
{
}
bool
FileRequests::downloadCompleted(const QByteArray &hash)
{
QList<QByteArray> hashes = invertBlockHashes[hash];
int size = hashes.count();
for(int i = 0; i < size; ++i){
if (!pendingDownloadData.contains(hashes[i]))
return false;
}
return true;
}
bool
FileRequests::verifyData(const QByteArray &hash, const QByteArray &data)
{
QCA::Hash shaHash("sha256");
const char *dataBuf = data.data();
quint32 size = data.size();
shaHash.update(dataBuf, size);
QByteArray computedHash = shaHash.final().toByteArray();
return computedHash == hash;
}
void
FileRequests::newSearch(const QString& queryString)
{
// Check if we are already searching for this query string.
if (!pendingSearches.contains(queryString)){
// If not, initiate the search sequence and keep an identifier
// around (query string itself).
QMap<QString, QVariant> msg;
msg["Origin"] = me;
msg["Search"] = queryString;
msg["Budget"] = START_BUDGET;
pendingSearches[queryString] = START_BUDGET;
emit broadcastRequest(msg);
}
}
void
FileRequests::processReply(const QMap<QString, QVariant>&msg)
{
if (msg.contains("SearchReply") &&
msg.contains("Dest") &&
msg.contains("Origin") &&
msg.contains("HopLimit") &&
msg.contains("MatchNames") &&
msg.contains("MatchIDs")){
qDebug() << "FileRequests: Got search reply for " << msg["SearchReply"].toString();
processSearchReply(msg);
}
else if (msg.contains("Dest") &&
msg.contains("Origin") &&
msg.contains("HopLimit") &&
msg.contains("BlockReply") &&
msg.contains("Data")){
processBlockReply(msg);
}
}
void
FileRequests::processSearchReply(const QMap<QString, QVariant>& msg)
{
//Check if we have a legitimate reply.
QString searchReply = msg["SearchReply"].toString();
if (pendingSearches.contains(searchReply)){
// CAUTION: names and ids might not have the same
// size always, make sure you account for that!
QList<QVariant> names = msg["MatchNames"].toList();
QList<QVariant> ids = msg["MatchIDs"].toList();
for(int i = 0; i < names.count() && i < ids.count(); ++i){
QMap<QString, QVariant> temp;
temp["Name"] = names[i].toString();
temp["ID"] = ids[i].toByteArray();
temp["Origin"] = msg["Origin"].toString();
// Keep the responses around and signal the UI.
searchResponses.insertMulti(searchReply, temp);
emit newResponse(searchReply, temp);
}
// We've exceeded the threshold, stop requesting.
if (searchResponses.values(searchReply).count() >= NUM_MATCHES)
destroyRequest(searchReply);
}
}
void
FileRequests::destroyRequest(const QString& queryString)
{
pendingSearches.remove(queryString);
searchResponses.remove(queryString);
qDebug() << "Destroyed search request " << queryString;
}
void
FileRequests::processDownloadTimeout()
{
downloadTimer.stop();
QList<QByteArray> masters = pendingDownloadData.keys();
for(int i = 0; i < masters.count(); ++i){
if (!invertBlockHashes.contains(masters[i])){
QString destination = pendingDownloads[masters[i]].second;
QMap<QString, QVariant> msg;
msg["BlockRequest"] = masters[i];
emit sendDownloadMsg(msg, destination);
}
}
QList<QByteArray> keys = blockHashes.keys();
for(int i = 0; i < keys.count(); ++i){
if (!pendingDownloadData.contains(keys[i])){
QString destination = pendingDownloads[blockHashes[keys[i]]].second;
QMap<QString, QVariant> msg;
msg["BlockRequest"] = keys[i];
emit sendDownloadMsg(msg, destination);
}
}
downloadTimer.start();
}
void
FileRequests::processTimeout()
{
timer.stop();
QList<QString> keys = pendingSearches.keys();
for(int i = 0; i < keys.count(); ++i){
quint32 currentBudget = pendingSearches[keys[i]];
if (currentBudget > MAX_BUDGET){
destroyRequest(keys[i]);
}
else{
QMap<QString, QVariant> msg;
msg["Origin"] = me;
msg["Search"] = keys[i];
msg["Budget"] = currentBudget * 2;
pendingSearches.insert(keys[i], currentBudget * 2);
emit broadcastRequest(msg);
}
}
timer.start(timerDuration);
}