From c91b44b02f658548bdc70a625eb54d0ea86f6737 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Mon, 8 Feb 2016 14:20:34 -0800 Subject: Add binary for smoke-testing offline downloads --- bin/offline.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ bin/offline.gypi | 44 ++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 bin/offline.cpp create mode 100644 bin/offline.gypi (limited to 'bin') 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 +#include +#include + +#include + +#include +#include + +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 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(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)' ], + }] + ], + }, + ], +} -- cgit v1.2.1