diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 8 | ||||
-rw-r--r-- | bin/offline.cpp | 76 | ||||
-rw-r--r-- | bin/offline.gypi | 44 | ||||
-rw-r--r-- | gyp/osx.gyp | 1 | ||||
-rw-r--r-- | include/mbgl/storage/response.hpp | 2 | ||||
-rw-r--r-- | src/mbgl/storage/response.cpp | 22 | ||||
-rw-r--r-- | test/storage/storage.hpp | 17 |
8 files changed, 150 insertions, 21 deletions
diff --git a/.gitignore b/.gitignore index f750e2f876..17531a3c3d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.actual.png *.diff.png *.pyc +offline.db /platform/android/debug /platform/android/sdk /platform/android/**/.classpath @@ -116,11 +116,11 @@ ifeq ($(BUILD),osx) xtest: ; $(RUN) HOST=osx HOST_VERSION=x86_64 Xcode/test endif -.PHONY: render xrender +.PHONY: render render: ; $(RUN) Makefile/mbgl-render -ifeq ($(BUILD),osx) -xrender: ; $(RUN) HOST=osx HOST_VERSION=x86_64 Xcode/mbgl-render -endif + +.PHONY: offline +offline: ; $(RUN) Makefile/mbgl-offline ##### Maintenace operations #################################################### diff --git a/bin/offline.cpp b/bin/offline.cpp new file mode 100644 index 0000000000..79a091a534 --- /dev/null +++ b/bin/offline.cpp @@ -0,0 +1,76 @@ +#include <mbgl/util/run_loop.hpp> +#include <mbgl/util/string.hpp> +#include <mbgl/util/io.hpp> + +#include <mbgl/storage/default_file_source.hpp> + +#include <cstdlib> +#include <iostream> + +using namespace std::literals::chrono_literals; + +int main(int, char * []) { + using namespace mbgl; + + util::deleteFile("offline.db"); + + util::RunLoop loop; + DefaultFileSource fileSource("offline.db", "."); + + fileSource.setAccessToken(getenv("MAPBOX_ACCESS_TOKEN")); + + LatLngBounds bayArea = LatLngBounds::hull(LatLng(37.2, -122.8), LatLng(38.1, -121.7)); + OfflineTilePyramidRegionDefinition definition("mapbox://styles/mapbox/streets-v8", bayArea, 0, 15, 1.0); + OfflineRegionMetadata metadata; + + class Observer : public OfflineRegionObserver { + public: + Observer(util::RunLoop& loop_) + : loop(loop_), + start(SystemClock::now()) { + } + + void statusChanged(OfflineRegionStatus status) override { + std::string bytesPerSecond = "-"; + + auto elapsedSeconds = (SystemClock::now() - start) / 1s; + if (elapsedSeconds != 0) { + bytesPerSecond = util::toString(status.completedResourceSize / elapsedSeconds); + } + + std::cout << status.completedResourceCount << " / " << status.requiredResourceCount + << " resources" + << (status.requiredResourceCountIsIndeterminate ? " (indeterminate); " : "; ") + << status.completedResourceSize << " bytes downloaded" + << " (" << bytesPerSecond << " bytes/sec)" + << std::endl; + + if (status.complete()) { + std::cout << "Finished" << std::endl; + loop.stop(); + } + } + + void responseError(Response::Error error) override { + std::cerr << error.reason << " downloading resource: " << error.message << std::endl; + } + + util::RunLoop& loop; + SystemTimePoint start; + }; + + fileSource.createOfflineRegion(definition, metadata, [&] (std::exception_ptr error, optional<OfflineRegion> region) { + if (error) { + std::cerr << "Error creating region: " << util::toString(error) << std::endl; + loop.stop(); + exit(1); + } else { + assert(region); + fileSource.setOfflineRegionObserver(*region, std::make_unique<Observer>(loop)); + fileSource.setOfflineRegionDownloadState(*region, OfflineRegionDownloadState::Active); + } + }); + + loop.run(); + return 0; +} diff --git a/bin/offline.gypi b/bin/offline.gypi new file mode 100644 index 0000000000..b5980ae76d --- /dev/null +++ b/bin/offline.gypi @@ -0,0 +1,44 @@ +{ + 'includes': [ + '../gyp/common.gypi', + ], + 'targets': [ + { 'target_name': 'mbgl-offline', + 'product_name': 'mbgl-offline', + 'type': 'executable', + + 'dependencies': [ + 'mbgl.gyp:core', + 'mbgl.gyp:platform-<(platform_lib)', + 'mbgl.gyp:headless-<(headless_lib)', + 'mbgl.gyp:http-<(http_lib)', + 'mbgl.gyp:asset-<(asset_lib)', + 'mbgl.gyp:copy_certificate_bundle', + ], + + 'include_dirs': [ + '../src', + ], + + 'sources': [ + './offline.cpp', + ], + + 'variables' : { + 'cflags_cc': [ + '<@(boost_cflags)', + ], + }, + + 'conditions': [ + ['OS == "mac"', { + 'xcode_settings': { + 'OTHER_CPLUSPLUSFLAGS': [ '<@(cflags_cc)' ], + } + }, { + 'cflags_cc': [ '<@(cflags_cc)' ], + }] + ], + }, + ], +} diff --git a/gyp/osx.gyp b/gyp/osx.gyp index 7501dd8ba0..4a5accda6e 100644 --- a/gyp/osx.gyp +++ b/gyp/osx.gyp @@ -6,5 +6,6 @@ '../platform/linux/mapboxgl-app.gypi', '../test/test.gypi', '../bin/render.gypi', + '../bin/offline.gypi', ], } diff --git a/include/mbgl/storage/response.hpp b/include/mbgl/storage/response.hpp index 162a272948..bec1efe6b1 100644 --- a/include/mbgl/storage/response.hpp +++ b/include/mbgl/storage/response.hpp @@ -53,6 +53,8 @@ public: Error(Reason, const std::string& = ""); }; +std::ostream& operator<<(std::ostream&, Response::Error::Reason); + } // namespace mbgl #endif diff --git a/src/mbgl/storage/response.cpp b/src/mbgl/storage/response.cpp index 09c43c8a6a..644d73d286 100644 --- a/src/mbgl/storage/response.cpp +++ b/src/mbgl/storage/response.cpp @@ -1,6 +1,9 @@ #include <mbgl/storage/response.hpp> #include <mbgl/util/chrono.hpp> +#include <iostream> +#include <cassert> + namespace mbgl { Response::Response(const Response& res) { @@ -22,4 +25,23 @@ Response::Error::Error(Reason reason_, const std::string& message_) : reason(reason_), message(message_) { } +std::ostream& operator<<(std::ostream& os, Response::Error::Reason r) { + switch (r) { + case Response::Error::Reason::Success: + return os << "Response::Error::Reason::NotFound"; + case Response::Error::Reason::NotFound: + return os << "Response::Error::Reason::NotFound"; + case Response::Error::Reason::Server: + return os << "Response::Error::Reason::Server"; + case Response::Error::Reason::Connection: + return os << "Response::Error::Reason::Connection"; + case Response::Error::Reason::Other: + return os << "Response::Error::Reason::Other"; + } + + // The above switch is exhaustive, but placate GCC nonetheless: + assert(false); + return os; +} + } // namespace mbgl diff --git a/test/storage/storage.hpp b/test/storage/storage.hpp index a4bb1bfbb7..3dc13d0d9f 100644 --- a/test/storage/storage.hpp +++ b/test/storage/storage.hpp @@ -3,7 +3,6 @@ #include "../fixtures/util.hpp" #include <mbgl/storage/response.hpp> -#include <iostream> #include <memory> class Storage : public testing::Test { @@ -15,20 +14,4 @@ protected: static std::unique_ptr<mbgl::test::Server> server; }; -namespace mbgl { - -inline std::ostream& operator<<(std::ostream& os, Response::Error::Reason r) { - // Special case - if (uint8_t(r) == 1) return os << "Response::Error::Reason::Success"; - switch (r) { - case Response::Error::Reason::NotFound: return os << "Response::Error::Reason::NotFound"; - case Response::Error::Reason::Server: return os << "Response::Error::Reason::Server"; - case Response::Error::Reason::Connection: return os << "Response::Error::Reason::Connection"; - case Response::Error::Reason::Other: return os << "Response::Error::Reason::Other"; - default: return os << "<Unknown>"; - } -} - -} // namespace mbgl - #endif |