diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2016-03-07 21:09:46 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2016-03-07 21:09:46 -0800 |
commit | 12993531c057aafa1e8f3b137594f049fcea7202 (patch) | |
tree | 140ed30a25667d545e3480ff545e6fbbfb0d0c35 /bin | |
parent | a3791b2325a4a2e7c38b9a41404f231faf6c3ed5 (diff) | |
download | qtlocation-mapboxgl-12993531c057aafa1e8f3b137594f049fcea7202.tar.gz |
[bin] Add explicit ^C handling to bin/offline, to test deactivation
Diffstat (limited to 'bin')
-rw-r--r-- | bin/offline.cpp | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/bin/offline.cpp b/bin/offline.cpp index a6a135df5f..68b36529d4 100644 --- a/bin/offline.cpp +++ b/bin/offline.cpp @@ -7,6 +7,8 @@ #include <cstdlib> #include <iostream> +#include <csignal> +#include <atomic> #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunknown-pragmas" @@ -54,7 +56,16 @@ int main(int argc, char *argv[]) { util::RunLoop loop; - DefaultFileSource fileSource(output, "."); + static DefaultFileSource fileSource(output, "."); + static std::unique_ptr<OfflineRegion> region; + + std::signal(SIGINT, [] (int) { + if (region) { + std::cout << "Stopping download... "; + fileSource.setOfflineRegionDownloadState(*region, OfflineRegionDownloadState::Inactive); + } + }); + fileSource.setAccessToken(token); LatLngBounds boundingBox = LatLngBounds::hull(LatLng(north, west), LatLng(south, east)); @@ -63,12 +74,20 @@ int main(int argc, char *argv[]) { class Observer : public OfflineRegionObserver { public: - Observer(util::RunLoop& loop_) - : loop(loop_), + Observer(OfflineRegion& region_, DefaultFileSource& fileSource_, util::RunLoop& loop_) + : region(region_), + fileSource(fileSource_), + loop(loop_), start(SystemClock::now()) { } void statusChanged(OfflineRegionStatus status) override { + if (status.downloadState == OfflineRegionDownloadState::Inactive) { + std::cout << "stopped" << std::endl; + loop.stop(); + return; + } + std::string bytesPerSecond = "-"; auto elapsedSeconds = (SystemClock::now() - start) / 1s; @@ -97,18 +116,21 @@ int main(int argc, char *argv[]) { std::cerr << "Error: reached limit of " << limit << " offline tiles" << std::endl; } + OfflineRegion& region; + DefaultFileSource& fileSource; util::RunLoop& loop; SystemTimePoint start; }; - fileSource.createOfflineRegion(definition, metadata, [&] (std::exception_ptr error, optional<OfflineRegion> region) { + 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)); + assert(region_); + region = std::make_unique<OfflineRegion>(std::move(*region_)); + fileSource.setOfflineRegionObserver(*region, std::make_unique<Observer>(*region, fileSource, loop)); fileSource.setOfflineRegionDownloadState(*region, OfflineRegionDownloadState::Active); } }); |