Improve implementation and interface of http downloads

This commit is contained in:
Emil Axelsson
2017-11-13 14:59:39 +01:00
parent 62c5506250
commit 27f36209fa
9 changed files with 289 additions and 122 deletions

View File

@@ -112,14 +112,16 @@ void HttpSynchronization::start() {
return;
}
// TODO: Do this in a new thread!
std::vector<std::string> listUrls = fileListUrls();
for (const auto& url : listUrls) {
if (trySyncFromUrl(url)) {
resolve();
return;
_syncThread = std::thread([this, listUrls] {
for (const auto& url : listUrls) {
if (trySyncFromUrl(url)) {
resolve();
return;
}
}
}
//fail();
});
}
void HttpSynchronization::cancel() {
@@ -150,9 +152,11 @@ bool HttpSynchronization::trySyncFromUrl(std::string listUrl) {
HttpRequest::RequestOptions opt;
opt.requestTimeoutSeconds = 0;
HttpMemoryDownload fileListDownload(listUrl);
SyncHttpMemoryDownload fileListDownload(listUrl);
fileListDownload.download(opt);
// ...
const std::vector<char>& buffer = fileListDownload.downloadedData();
std::istringstream fileList(std::string(buffer.begin(), buffer.end()));
@@ -168,7 +172,7 @@ bool HttpSynchronization::trySyncFromUrl(std::string listUrl) {
filename;
std::thread t([opt, line, fileDestination]() {
HttpFileDownload fileDownload(line, fileDestination);
SyncHttpFileDownload fileDownload(line, fileDestination);
fileDownload.download(opt);
});
downloadThreads.push_back(std::move(t));
@@ -176,6 +180,7 @@ bool HttpSynchronization::trySyncFromUrl(std::string listUrl) {
for (auto& t : downloadThreads) {
t.join();
}
createSyncFile();
return true;
}