Feature/sync cleanup (#535)

* Cleanup in Sync module
 * Correctly name mar097 asset name
This commit is contained in:
Alexander Bock
2018-02-24 14:18:14 -05:00
committed by GitHub
parent ee7e1b9b96
commit 1193e14c14
9 changed files with 207 additions and 232 deletions

View File

@@ -47,27 +47,44 @@ namespace {
constexpr const char* QueryKeyFileVersion = "file_version";
constexpr const char* QueryKeyApplicationVersion = "application_version";
constexpr const int ApplicationVersion = 1;
}
} // namespace
namespace openspace {
HttpSynchronization::HttpSynchronization(
const ghoul::Dictionary& dict,
const std::string& synchronizationRoot,
const std::vector<std::string>& synchronizationRepositories
documentation::Documentation HttpSynchronization::Documentation() {
using namespace openspace::documentation;
return {
"HttpSynchronization",
"http_synchronization",
{
{
KeyIdentifier,
new StringVerifier,
Optional::No,
"A unique identifier for this resource"
},
{
KeyVersion,
new IntVerifier,
Optional::No,
"The version of this resource"
}
}
};
}
HttpSynchronization::HttpSynchronization(const ghoul::Dictionary& dict,
const std::string& synchronizationRoot,
const std::vector<std::string>& synchronizationRepositories
)
: openspace::ResourceSynchronization(dict)
, _nTotalBytesKnown(false)
, _nTotalBytes(0)
, _nSynchronizedBytes(0)
, _shouldCancel(false)
, _synchronizationRoot(synchronizationRoot)
, _synchronizationRepositories(synchronizationRepositories)
{
documentation::testSpecificationAndThrow(
Documentation(),
dict,
"HttpSynchroniztion"
"HttpSynchronization"
);
_identifier = dict.value<std::string>(KeyIdentifier);
@@ -81,28 +98,6 @@ HttpSynchronization::~HttpSynchronization() {
}
}
documentation::Documentation HttpSynchronization::Documentation() {
using namespace openspace::documentation;
return {
"HttpSynchronization",
"http_synchronization",
{
{
KeyIdentifier,
new StringVerifier,
Optional::No,
"A unique identifier for this resource"
},
{
KeyVersion,
new IntVerifier,
Optional::No,
"The version of this resource"
}
}
};
}
std::string HttpSynchronization::directory() {
ghoul::filesystem::Directory d(
_synchronizationRoot +
@@ -128,10 +123,13 @@ void HttpSynchronization::start() {
return;
}
std::vector<std::string> listUrls = fileListUrls();
_syncThread = std::thread([this, listUrls] {
for (const auto& url : listUrls) {
if (trySyncFromUrl(url)) {
std::string query = std::string("?") + QueryKeyIdentifier + "=" + _identifier +
"&" + QueryKeyFileVersion + "=" + std::to_string(_version) +
"&" + QueryKeyApplicationVersion + "=" + std::to_string(ApplicationVersion);
_syncThread = std::thread([this](const std::string& query) {
for (const std::string& url : _synchronizationRepositories) {
if (trySyncFromUrl(url + query)) {
createSyncFile();
resolve();
return;
@@ -140,7 +138,7 @@ void HttpSynchronization::start() {
if (!_shouldCancel) {
reject();
}
});
}, query);
}
void HttpSynchronization::cancel() {
@@ -153,17 +151,27 @@ void HttpSynchronization::clear() {
// TODO: Remove all files from directory.
}
std::vector<std::string> HttpSynchronization::fileListUrls() {
std::string query = std::string("?") + QueryKeyIdentifier + "=" + _identifier +
"&" + QueryKeyFileVersion + "=" + std::to_string(_version) +
"&" + QueryKeyApplicationVersion + "=" + std::to_string(ApplicationVersion);
size_t HttpSynchronization::nSynchronizedBytes() {
return _nSynchronizedBytes;
}
std::vector<std::string> urls;
for (const auto& repoUrl : _synchronizationRepositories) {
urls.push_back(repoUrl + query);
}
size_t HttpSynchronization::nTotalBytes() {
return _nTotalBytes;
}
return urls;
bool HttpSynchronization::nTotalBytesIsKnown() {
return _nTotalBytesKnown;
}
void HttpSynchronization::createSyncFile() {
std::string directoryName = directory();
std::string filepath = directoryName + ".ossync";
FileSys.createDirectory(directoryName, ghoul::filesystem::Directory::Recursive::Yes);
std::ofstream syncFile(filepath, std::ofstream::out);
syncFile << "Synchronized";
syncFile.close();
}
bool HttpSynchronization::hasSyncFile() {
@@ -175,9 +183,9 @@ bool HttpSynchronization::trySyncFromUrl(std::string listUrl) {
HttpRequest::RequestOptions opt;
opt.requestTimeoutSeconds = 0;
SyncHttpMemoryDownload fileListDownload(listUrl);
fileListDownload.onProgress([this](HttpRequest::Progress) {
return !_shouldCancel;
SyncHttpMemoryDownload fileListDownload(std::move(listUrl));
fileListDownload.onProgress([&c = _shouldCancel](HttpRequest::Progress) {
return !c;
});
fileListDownload.download(opt);
@@ -246,7 +254,7 @@ bool HttpSynchronization::trySyncFromUrl(std::string listUrl) {
startedAllDownloads = true;
bool failed = false;
for (auto& d : downloads) {
for (std::unique_ptr<AsyncHttpFileDownload>& d : downloads) {
d->wait();
if (!d->hasSucceeded()) {
failed = true;
@@ -255,32 +263,10 @@ bool HttpSynchronization::trySyncFromUrl(std::string listUrl) {
if (!failed) {
return true;
}
for (auto& d : downloads) {
for (std::unique_ptr<AsyncHttpFileDownload>& d : downloads) {
d->cancel();
}
return false;
}
void HttpSynchronization::createSyncFile() {
std::string dir = directory();
std::string filepath = dir + ".ossync";
FileSys.createDirectory(dir, ghoul::filesystem::Directory::Recursive::Yes);
std::ofstream syncFile(filepath, std::ofstream::out);
syncFile << "Synchronized";
syncFile.close();
}
size_t HttpSynchronization::nSynchronizedBytes() {
return _nSynchronizedBytes;
}
size_t HttpSynchronization::nTotalBytes() {
return _nTotalBytes;
}
bool HttpSynchronization::nTotalBytesIsKnown() {
return _nTotalBytesKnown;
}
} // namespace openspace