From 7920c97767164714175fa1cb3edb8fecf585bf3c Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Sun, 24 Dec 2017 18:23:18 +0100 Subject: [PATCH] Fix the map configurations for Mars Include hotfix for limiting the number of concurrent downloads --- .../scene/solarsystem/planets/mars/mars.asset | 2 +- include/openspace/util/httprequest.h | 12 ++++++++++++ src/util/httprequest.cpp | 16 ++++++++++++---- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/data/assets/scene/solarsystem/planets/mars/mars.asset b/data/assets/scene/solarsystem/planets/mars/mars.asset index 39547dd358..20235b93e6 100644 --- a/data/assets/scene/solarsystem/planets/mars/mars.asset +++ b/data/assets/scene/solarsystem/planets/mars/mars.asset @@ -14,7 +14,7 @@ local textures = asset.syncedResource({ local marsRadii = { 3396190.0, 3396190.0, 3376200.0 } -local mapServiceConfigs = asset.localResource("mapServiceConfigs") +local mapServiceConfigs = asset.localResource("map_service_configs") local color_layers = { { diff --git a/include/openspace/util/httprequest.h b/include/openspace/util/httprequest.h index 7c4cc2942d..e55a6f3b81 100644 --- a/include/openspace/util/httprequest.h +++ b/include/openspace/util/httprequest.h @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -174,6 +175,7 @@ protected: void markAsStarted(); void markAsSuccessful(); void markAsFailed(); + private: ProgressCallback _onProgress; bool _started = false; @@ -187,6 +189,7 @@ public: SyncHttpDownload(SyncHttpDownload&& d) = default; virtual ~SyncHttpDownload() = default; void download(HttpRequest::RequestOptions opt); + protected: HttpRequest _httpRequest; }; @@ -199,8 +202,10 @@ public: void start(HttpRequest::RequestOptions opt); void cancel(); void wait(); + protected: void download(HttpRequest::RequestOptions opt); + private: HttpRequest _httpRequest; std::thread _downloadThread; @@ -218,16 +223,21 @@ public: HttpFileDownload(std::string destination, Overwrite = Overwrite::No); HttpFileDownload(HttpFileDownload&& d) = default; virtual ~HttpFileDownload() = default; + protected: bool initDownload() override; bool deinitDownload() override; size_t handleData(HttpRequest::Data d) override; static std::mutex _directoryCreationMutex; + private: std::string _destination; bool _overwrite; std::ofstream _file; + + static const int MaxFilehandles = 35; + static std::atomic_int nCurrentFilehandles; }; class HttpMemoryDownload : public virtual HttpDownload { @@ -236,10 +246,12 @@ public: HttpMemoryDownload(HttpMemoryDownload&& d) = default; virtual ~HttpMemoryDownload() = default; const std::vector& downloadedData(); + protected: bool initDownload() override; bool deinitDownload() override; size_t handleData(HttpRequest::Data d) override; + private: std::vector _downloadedData; }; diff --git a/src/util/httprequest.cpp b/src/util/httprequest.cpp index 348b0773e1..0298fabe22 100644 --- a/src/util/httprequest.cpp +++ b/src/util/httprequest.cpp @@ -51,7 +51,7 @@ namespace { constexpr const long StatusCodeOk = 200; constexpr const char* _loggerCat = "HttpRequest"; -} +} // namespace namespace openspace { @@ -298,9 +298,11 @@ const std::vector& HttpMemoryDownload::downloadedData() { bool HttpMemoryDownload::initDownload() { return true; } + bool HttpMemoryDownload::deinitDownload() { return true; } + size_t HttpMemoryDownload::handleData(HttpRequest::Data d) { _downloadedData.insert(_downloadedData.end(), d.buffer, d.buffer + d.size); return d.size; @@ -315,7 +317,7 @@ HttpFileDownload::HttpFileDownload( bool HttpFileDownload::initDownload() { if (!_overwrite && FileSys.fileExists(_destination)) { - LERROR("File " << _destination << " already exists!"); + LWARNING("File " << _destination << " already exists!"); return false; } @@ -329,6 +331,12 @@ bool HttpFileDownload::initDownload() { } } + while (nCurrentFilehandles >= MaxFilehandles) { + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + } + + + ++nCurrentFilehandles; _file = std::ofstream(_destination, std::ofstream::binary); if (_file.fail()) { @@ -348,8 +356,6 @@ bool HttpFileDownload::initDownload() { #endif if (errno) { #if defined(__unix__) - // We use strerror_r because it's threadsafe. - // GNU's strerror_r returns a string and may ignore buffer completely. char buffer[255]; LERROR( "Cannot open file " << destinationFile << ": " << @@ -373,6 +379,7 @@ bool HttpFileDownload::initDownload() { bool HttpFileDownload::deinitDownload() { _file.close(); + --nCurrentFilehandles; return _file.good(); } @@ -381,6 +388,7 @@ size_t HttpFileDownload::handleData(HttpRequest::Data d) { return d.size; } +std::atomic_int HttpFileDownload::nCurrentFilehandles = 0; std::mutex HttpFileDownload::_directoryCreationMutex; SyncHttpMemoryDownload::SyncHttpMemoryDownload(std::string url)