/
fetcher.cc
635 lines (579 loc) · 18.4 KB
/
fetcher.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#include "fetcher.h"
#include <QDir>
using std::pair;
using std::vector;
static const char * kNonResumeModeFileName = "NON_RESUMABLE";
namespace {
struct AscendingStartIndex {
inline bool operator() (const Segment& a, const Segment& b) {
return a.first < b.first;
}
};
void Sort(std::vector<Segment>* segments) {
std::sort(segments->begin(), segments->end(), AscendingStartIndex());
}
qint64 CountBytes(const std::vector<Segment>& segments) {
qint64 sum = 0;
for (const Segment& segment : segments) {
sum += segment.second - segment.first + 1;
}
return sum;
}
}
FetcherWorker::FetcherWorker(int worker_id,
qint64 pre_downloaded,
const vector<Segment>& segments,
const QUrl& url,
const QString& work_dir,
bool non_resume_mode)
: worker_id_(worker_id),
pre_downloaded_(pre_downloaded),
segments_(segments),
url_(url),
work_dir_(work_dir),
downloaded_(0),
seg_bytes_received_(0),
allocation_size_(CountBytes(segments)),
current_segment_(segments_.begin()),
current_request_(url),
non_resume_mode_(non_resume_mode) {
is_done_ = false;
is_in_error_ = false;
progress_updater_ = new QTimer(this);
connect(progress_updater_, SIGNAL(timeout()), this, SLOT(UpdateProgress()));
current_request_.setRawHeader("connection", "Keep-Alive");
current_request_.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute,
true);
}
FetcherWorker::~FetcherWorker() {
if (current_reply_ != nullptr) {
Stop();
}
}
void FetcherWorker::UpdateProgress() {
emit Progress(GetTotalDownloadedBytes());
}
void FetcherWorker::Start() {
network_.reset(new QNetworkAccessManager());
progress_updater_->start(kProgressUpdateInterval);
if (!StartNextSegment() && !IsInError()) {
is_done_ = true;
progress_updater_->stop();
emit Completed();
}
// qDebug() << "Worker " << worker_id_ << " started.";
}
// TODO(ogaro): Confirm content length?
bool FetcherWorker::StartNextSegment() {
if (!non_resume_mode_ && current_segment_ == segments_.end()) {
return false;
}
QString fname;
if (non_resume_mode_) {
fname = JoinPath(work_dir_, kNonResumeModeFileName);
} else {
fname = MakeShardPath(work_dir_, *current_segment_);
}
current_file_.reset(new QFile(fname));
if (!current_file_->open(QIODevice::WriteOnly)) {
// TODO(ogaro): Emit an error to controller.
DIE() << "Worker " << worker_id_ << " failed to open shard file "
<< fname;
is_in_error_ = true;
return false;
}
seg_bytes_received_ = 0;
if (!non_resume_mode_) {
QString range_header = QString("bytes=%1-%2")
.arg(current_segment_->first)
.arg(current_segment_->second);
current_request_.setRawHeader("range", range_header.toUtf8());
}
current_reply_.reset(network_->get(current_request_));
connect(current_reply_.get(), SIGNAL(finished()),
this, SLOT(OnSegmentFinished()));
connect(current_reply_.get(), SIGNAL(downloadProgress(qint64, qint64)),
this, SLOT(OnDownloadProgress(qint64, qint64)));
//connect(current_reply_.get(), SIGNAL(error(QNetworkReply::NetworkError)),
// this, SLOT(OnError(QNetworkReply::NetworkError)));
return true;
}
void FetcherWorker::Stop() {
disconnect(current_reply_.get(), 0, 0, 0);
current_reply_->abort();
if (seg_bytes_received_ > 0) {
MaybeRenameCurrentShard();
} else {
current_file_->remove(); // Nothing downloaded for this segment.
}
current_file_->close();
current_file_.reset();
current_reply_.reset();
progress_updater_->stop();
UpdateProgress();
emit Stopped();
}
void FetcherWorker::MaybeRenameCurrentShard() {
MaybeRenameShard(seg_bytes_received_, current_file_.get());
/*qint64 start = current_segment_->first;
if (seg_bytes_received_ <= 0) {
CHECK(current_file_->remove());
return;
}
Segment done_so_far = Segment(start, start + seg_bytes_received_ - 1);
if (done_so_far.second == current_segment_->second) {
// No rename necessary.
return;
}
QString new_shard_path = MakeShardPath(work_dir_, done_so_far);
if (QFile::exists(new_shard_path)) {
QFile::remove(new_shard_path);
}
if (!current_file_->rename(new_shard_path)) {
DIE() << "Shard rename to " << new_shard_path << " failed";
}*/
}
void FetcherWorker::OnSegmentFinished() {
disconnect(current_reply_.get(), 0, 0, 0);
if (non_resume_mode_) {
// Rename file so it can be merged later.
QString new_shard_path = MakeShardPath(
work_dir_, Segment(0, downloaded_ - 1));
if (!current_file_->rename(new_shard_path)) {
DIE() << "Shard rename to " << new_shard_path << " failed";
}
is_done_ = true;
}
if (!non_resume_mode_) {
MaybeRenameCurrentShard();
current_file_->close();
current_file_.reset();
current_reply_.reset();
++current_segment_;
if (!StartNextSegment() && !IsInError()) {
is_done_ = true;
}
} else {
current_file_->close();
current_file_.reset();
current_reply_.reset();
}
if (is_done_) {
progress_updater_->stop();
UpdateProgress();
emit Completed();
}
}
// TODO(ogaro): Double-check whether it is ok to assume that bytesReceived
// should always add up to the reported size of a file.
void FetcherWorker::OnDownloadProgress(qint64 bytesReceived,
qint64 bytesTotal) {
if (bytesReceived < seg_bytes_received_) {
DIE() << "Bytes received was " << bytesReceived
<< " but seg bytes received was" << seg_bytes_received_;
}
qint64 delta = bytesReceived - seg_bytes_received_;
seg_bytes_received_ += delta;
downloaded_ += delta;
if (current_file_ == nullptr) {
// TODO(ogaro): Emit error.
DIE()<< "OnDownloadProgress encountered null file pointer.";
return;
}
current_file_->write(current_reply_->readAll());
}
void FetcherWorker::OnError(QNetworkReply::NetworkError code) {
qDebug() << "Worker " << worker_id_ << " encountered error " << code;
// TODO(ogaro): Write a function that converts the code to string. Don't
// know if calling current_reply_->errorString() will be appropriate here.
is_in_error_ = true;
/*
QString err;
if (current_reply_ != nullptr) {
err = current_reply_->errorString();
}*/
progress_updater_->stop();
emit Error(worker_id_, code);
}
WorkerUnit::WorkerUnit(FetcherWorker* worker)
: worker_(worker),
worker_id_(worker->GetId()),
thread_(new QThread()),
is_done_(false),
byte_allocation_(worker->GetTotalAllocatedBytes()),
total_downloaded_(worker->GetPreDownloaded()) {
is_stopped_ = false;
worker_->moveToThread(thread_);
// connect(worker_, SIGNAL(Error(QNetworkReply::NetworkError)),
// this, SLOT(OnError(QNetworkReply::NetworkError)));
connect(this, SIGNAL(StopRequested()), worker_, SLOT(Stop()));
connect(worker_, SIGNAL(Progress(qint64)), this, SLOT(WorkerProgress(qint64)));
connect(worker_, SIGNAL(Stopped()), this, SLOT(OnWorkerStopped()));
connect(thread_, SIGNAL(started()), worker_, SLOT(Start()));
connect(worker_, SIGNAL(Completed()), this, SLOT(Completed()));
// connect(worker_, SIGNAL(Completed()), thread_, SLOT(quit()));
connect(thread_, SIGNAL(finished()), thread_, SLOT(deleteLater()));
connect(worker_, SIGNAL(Completed()), worker_, SLOT(deleteLater()));
}
WorkerUnit::~WorkerUnit() {
if (!is_done_) {
// worker_->deleteLater();
// thread_->deleteLater();
}
}
FetcherWorker* WorkerUnit::GetWorker() {
return worker_;
}
void WorkerUnit::WorkerProgress(qint64 total_downloaded) {
total_downloaded_ = total_downloaded;
// qDebug() << "Worker " << worker_id_ << " has downloaded " << total_downloaded;
}
void WorkerUnit::Start() {
thread_->start();
}
void WorkerUnit::Stop() {
if (!worker_->IsDone() || !worker_->IsInError()) {
emit StopRequested();
}
}
void WorkerUnit::OnWorkerStopped() {
thread_->quit();
thread_->deleteLater();
worker_->deleteLater();
is_stopped_ = true;
emit WorkerStopped(worker_id_);
}
bool WorkerUnit::IsDone() {
return is_done_;
}
bool WorkerUnit::IsStopped() {
return is_stopped_;
}
qint64 WorkerUnit::Allocation() {
return byte_allocation_;
}
void WorkerUnit::Completed() {
is_done_ = true;
emit Completed(worker_id_);
}
/*
void WorkerUnit::OnError(QNetworkReply::NetworkError code) {
emit Error(worker_id_, code);
thread_->quit();
}
*/
Fetcher::Fetcher(const QUrl& url, qint64 file_size, const QString& save_as)
: url_(url),
file_size_(file_size),
save_as_(save_as),
num_connections_(0),
work_dir_("") {
is_in_error_ = false;
waiting_for_all_workers_stopped_ = false;
CHECK(!save_as.isEmpty());
// Preconditions: Overwrite save-as if preferences say so, or raise an
// error.
}
Fetcher::~Fetcher() {
ClearWorkerUnits();
}
const QString& Fetcher::WorkDir() {
return work_dir_;
}
void Fetcher::Resume(const QString& work_dir, int num_connections) {
work_dir_ = work_dir;
Resume(num_connections);
}
void Fetcher::Start(int num_connections) {
int appended = 0;
QString new_save_as = MaybeAppendSuffix(save_as_, &appended);
if (appended > 0) { // Avoid overwriting existing files.
save_as_ = new_save_as;
emit ChangeSaveAs(new_save_as);
}
QString download_dir = DirName(save_as_);
work_dir_ = save_as_ + ".qaccelerator";
int i = 0;
QString fname = FileName(save_as_);
QFileInfo finfo = QFileInfo(fname);
QString prefix = finfo.baseName();
QString suffix = finfo.suffix();
while (QFileInfo(work_dir_).exists()) {
++i;
save_as_ = JoinPath(
download_dir,
QString("%1 (%2)%3%4")
.arg(prefix)
.arg(i)
.arg(suffix.isEmpty() ? "" : ".")
.arg(suffix));
work_dir_ = save_as_ + ".qaccelerator";
}
if (i > 0) {
emit ChangeSaveAs(save_as_);
}
Resume(num_connections);
}
void Fetcher::Resume(int num_connections) {
CHECK(!work_dir_.isEmpty());
if (file_size_ < 1) {
CHECK(num_connections == 1);
}
num_connections_ = num_connections;
QDir dir(work_dir_);
if (file_size_ < 1) { // Unknown file size.
if (dir.exists()) {
CHECK(dir.removeRecursively());
}
CHECK(dir.mkpath("."));
} else {
if (!dir.exists()) {
CHECK(dir.mkpath("."));
// qDebug() << "Work dir " << work_dir_ << " created.";
}
}
PrepareThreads();
for (WorkerUnit* unit : worker_units_) {
unit->Start();
}
}
void Fetcher::PrepareThreads() {
qDebug() << "File size is " << file_size_;
std::vector<Segment> pre_downloaded_segments;
GetDownloadedSegments(work_dir_, &pre_downloaded_segments);
std::vector<std::vector<Segment> > allocations;
if (file_size_ > 0) {
CalculateAllocations(&pre_downloaded_segments, file_size_, num_connections_,
&allocations);
} else {
vector<Segment> empty_alloc = { Segment(0, 0) };
allocations.push_back(empty_alloc);
}
qint64 pre_downloaded_bytes = CountBytes(pre_downloaded_segments);
qint64 pre_downloaded_per_worker = pre_downloaded_bytes / num_connections_;
for (int i = 0; i < num_connections_; ++i) {
qint64 pre_downloaded_for_worker = pre_downloaded_per_worker;
if (i == num_connections_ - 1) {
pre_downloaded_for_worker += pre_downloaded_bytes % num_connections_;
}
FetcherWorker* worker = new FetcherWorker(
i,
pre_downloaded_for_worker,
allocations[i],
url_,
work_dir_,
file_size_ <= 0);
// () << "Worker is at " << reinterpret_cast<qint64>(worker);
WorkerUnit* worker_unit = new WorkerUnit(worker);
// qDebug() << "Worker unit is at " << reinterpret_cast<qint64>(worker_unit);
connect(worker_unit, SIGNAL(WorkerStopped(int)), this, SLOT(OnWorkerStopped(int)));
connect(worker_unit, SIGNAL(Completed(int)),
this, SLOT(RegisterCompletion(int)));
connect(worker, SIGNAL(Error(int, QNetworkReply::NetworkError)),
this, SLOT(HandleError(int, QNetworkReply::NetworkError)));
worker_units_.append(worker_unit);
}
}
void Fetcher::HandleError(int worker_id, QNetworkReply::NetworkError code) {
qDebug() << "Thread " << worker_id << " encountered " << code;
if (!is_in_error_) {
is_in_error_ = true;
emit Error(code); // Only need to emit an error once.
}
}
void Fetcher::Stop() {
waiting_for_all_workers_stopped_ = true;
for (const auto& unit : worker_units_) {
if (!unit->IsDone()) {
unit->Stop();
}
}
}
void Fetcher::OnWorkerStopped(int worker_id) {
bool all_workers_stopped = true;
for (const auto& unit : worker_units_) {
if (!unit->IsDone() && !unit->IsStopped()) {
all_workers_stopped = false;
break;
}
}
if (all_workers_stopped) {
ClearWorkerUnits();
emit Paused();
waiting_for_all_workers_stopped_ = false;
}
}
void Fetcher::RemoveWorkDir() {
if (work_dir_.isEmpty()) {
return;
}
QDir(work_dir_).removeRecursively();
}
void Fetcher::ClearWorkerUnits() {
for (WorkerUnit* worker_unit : worker_units_) {
worker_unit->deleteLater();
}
worker_units_.clear();
}
bool Fetcher::GetProgress(qint64* overall_downloaded,
std::vector<std::pair<qint64, qint64> >* thread_stats) {
*overall_downloaded = 0;
for (WorkerUnit* unit : worker_units_) {
qint64 downloaded = unit->TotalDownloaded();
// Note: This will be 1 if file size is unknown.
qint64 allocated = unit->Allocation();
if (downloaded < 0) {
DIE() << "Running worker " << unit->GetWorker()->GetId()
<< " had negative downloaded value "
<< downloaded;
} else if (file_size_ > 0 && downloaded > allocated) {
qDebug() << downloaded << " (downloaded) is greater than "
<< allocated << " (allocated).";
return false;
}
*overall_downloaded += downloaded;
if (*overall_downloaded < 0) {
DIE() << "Overall downloaded fell to less than zero.";
}
thread_stats->push_back(std::make_pair(downloaded, allocated));
}
return true;
}
void Fetcher::RegisterCompletion(int worker_id) {
// TODO(ogaro): QMutexLocker?
mutex_.lock();
bool all_completed = true;
int num_completed = 0; // TODO(ogaro): Remove this counter;
for (WorkerUnit* unit : worker_units_) {
if (!unit->IsDone()) {
all_completed = false;
break;
}
++num_completed;
}
if (all_completed) {
qint64 overall_downloaded = 0;
std::vector<std::pair<qint64, qint64> > thread_stats;
GetProgress(&overall_downloaded, &thread_stats);
if (overall_downloaded < file_size_) {
qDebug() << "Sending paused signal.";
ClearWorkerUnits();
emit Paused();
} else {
MergeFiles();
// TODO(ogaro): At this point, not all QThreads may have been destroyed.
emit Completed();
}
}
mutex_.unlock();
}
void Fetcher::MergeFiles() {
std::vector<Segment> downloaded_segments;
GetDownloadedSegments(work_dir_, &downloaded_segments);
Sort(&downloaded_segments);
// TODO(ogaro): Check here that the segments are valid.
QFile merged(save_as_);
if (!merged.open(QIODevice::WriteOnly)) {
// TODO(ogaro): Send an error.
qDebug() << "Failed to open file " << save_as_;
return;
}
int buffer_size = 2048;
for (const Segment& segment : downloaded_segments) {
QString shard_path = MakeShardPath(work_dir_, segment);
QFile shard(shard_path);
if (!shard.open(QIODevice::ReadOnly)) {
// TODO(ogaro): Send an error.
qDebug() << "Failed to open file " << shard_path;
return;
}
while(!shard.atEnd()) {
QByteArray buffer = shard.read(buffer_size);
if (buffer.size() == 0) {
// TODO(ogaro): Log error (shard.errorString());
continue;
}
if (merged.write(buffer) == -1) {
// TODO(ogaro): Log error (merged.errorString());
}
}
shard.close();
}
QDir(work_dir_).removeRecursively();
}
void Fetcher::GetDownloadedSegments(const QString& work_dir,
std::vector<Segment>* segments) {
QStringList fnames = QDir(work_dir).entryList(
QDir::NoDotAndDotDot | QDir::Files);
foreach(const QString& fname, fnames) {
Segment segment;
if (!ParseSegment(fname, &segment)) {
continue;
}
segments->push_back(segment);
}
}
void Fetcher::CalculateAllocations(std::vector<Segment>* downloaded,
qint64 file_size, int num_connections,
std::vector<std::vector<Segment> >* allocations) {
qint64 undownloaded_size = file_size - CountBytes(*downloaded);
for (const Segment& segment : *downloaded) {
if (segment.first > segment.second) {
DIE() << "Segment (" << segment.first << ", " << segment.second
<< ") is invalid.";
}
}
if (undownloaded_size < 0) {
DIE() << __FUNCTION__ << "File size is " << file_size
<< " but number of undownloaded is " << undownloaded_size;
}
Sort(downloaded);
// Get the boundaries of the undownloaded segments of the file.
std::deque<Segment> undownloaded;
qint64 start = 0;
qint64 end = 0;
for (int i = 0; i < downloaded->size(); ++i) {
end = downloaded->at(i).first - 1;
if (end - start + 1 > 0) {
undownloaded.push_back(Segment(start, end));
}
start = downloaded->at(i).second + 1;
}
end = file_size - 1;
if (end - start + 1 > 0) {
undownloaded.push_back(Segment(start, end));
}
qint64 min_thread_load = undownloaded_size / num_connections;
for (int i = 0; i < num_connections; ++i) {
qint64 thread_load = min_thread_load;
std::vector<Segment> allocation;
if (i == num_connections - 1) {
thread_load += undownloaded_size % num_connections;
}
if (thread_load == 0) {
allocations->push_back(allocation);
continue;
}
qint64 capacity_left = thread_load;
while (undownloaded.size() > 0) {
Segment segment = undownloaded.at(0);
undownloaded.pop_front();
qint64 start = segment.first;
qint64 end = segment.second;
qint64 segment_size = end - start + 1;
if (segment_size < capacity_left) {
allocation.push_back(segment);
capacity_left -= segment_size;
} else if (segment_size == capacity_left) {
allocation.push_back(segment);
break; // We are done allocating for thread i.
} else {
// Split segment.
allocation.push_back(Segment(start, start + capacity_left - 1));
undownloaded.push_front(Segment(start + capacity_left, end));
break;
}
}
allocations->push_back(allocation);
}
}