Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[offline] Update offline utility
Browse files Browse the repository at this point in the history
  • Loading branch information
alexshalamov committed Oct 10, 2019
1 parent 1214374 commit 04ca71c
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions bin/offline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include <mbgl/util/string.hpp>
#include <mbgl/util/geojson.hpp>

#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/database_file_source.hpp>
#include <mbgl/storage/file_source_manager.hpp>
#include <mbgl/storage/resource_options.hpp>

#include <args.hxx>

Expand Down Expand Up @@ -161,17 +163,16 @@ int main(int argc, char *argv[]) {


util::RunLoop loop;
DefaultFileSource fileSource(output, ".");
std::unique_ptr<OfflineRegion> region;
std::shared_ptr<DatabaseFileSource> fileSource =
std::static_pointer_cast<DatabaseFileSource>(FileSourceManager::get()->getFileSource(
FileSourceType::Database,
ResourceOptions().withAccessToken(token).withBaseURL(apiBaseURL).withCachePath(output)));

fileSource.setAccessToken(token);
fileSource.setAPIBaseURL(apiBaseURL);
std::unique_ptr<OfflineRegion> region;

if (inputDb && mergePath) {
DefaultFileSource inputSource(*inputDb, ".");
inputSource.setAccessToken(token);
inputSource.setAPIBaseURL(apiBaseURL);

DatabaseFileSource inputSource(ResourceOptions().withCachePath(*inputDb));

int retCode = 0;
std::cout << "Start Merge" << std::endl;
inputSource.mergeOfflineRegions(*mergePath, [&] (mbgl::expected<std::vector<OfflineRegion>, std::exception_ptr> result) {
Expand All @@ -193,13 +194,15 @@ int main(int argc, char *argv[]) {

class Observer : public OfflineRegionObserver {
public:
Observer(OfflineRegion& region_, DefaultFileSource& fileSource_, util::RunLoop& loop_, mbgl::optional<std::string> mergePath_)
Observer(OfflineRegion& region_,
std::shared_ptr<DatabaseFileSource> fileSource_,
util::RunLoop& loop_,
mbgl::optional<std::string> mergePath_)
: region(region_),
fileSource(fileSource_),
fileSource(std::move(fileSource_)),
loop(loop_),
mergePath(std::move(mergePath_)),
start(util::now()) {
}
start(util::now()) {}

void statusChanged(OfflineRegionStatus status) override {
if (status.downloadState == OfflineRegionDownloadState::Inactive) {
Expand All @@ -215,14 +218,11 @@ int main(int argc, char *argv[]) {
bytesPerSecond = util::toString(status.completedResourceSize / elapsedSeconds);
}

std::cout << status.completedResourceCount << " / " << status.requiredResourceCount
<< " resources"
<< status.completedTileCount << " / " << status.requiredTileCount
<< "tiles"
<< (status.requiredResourceCountIsPrecise ? "; " : " (indeterminate); ")
std::cout << status.completedResourceCount << " / " << status.requiredResourceCount << " resources | "
<< status.completedTileCount << " / " << status.requiredTileCount << " tiles"
<< (status.requiredResourceCountIsPrecise ? " | " : " (indeterminate); ")
<< status.completedResourceSize << " bytes downloaded"
<< " (" << bytesPerSecond << " bytes/sec)"
<< std::endl;
<< " (" << bytesPerSecond << " bytes/sec)" << std::endl;

if (status.complete()) {
std::cout << "Finished Download" << std::endl;
Expand All @@ -239,7 +239,7 @@ int main(int argc, char *argv[]) {
}

OfflineRegion& region;
DefaultFileSource& fileSource;
std::shared_ptr<DatabaseFileSource> fileSource;
util::RunLoop& loop;
mbgl::optional<std::string> mergePath;
Timestamp start;
Expand All @@ -248,24 +248,26 @@ int main(int argc, char *argv[]) {
static auto stop = [&] {
if (region) {
std::cout << "Stopping download... ";
fileSource.setOfflineRegionDownloadState(*region, OfflineRegionDownloadState::Inactive);
fileSource->setOfflineRegionDownloadState(*region, OfflineRegionDownloadState::Inactive);
}
};

std::signal(SIGINT, [] (int) { stop(); });

fileSource.createOfflineRegion(definition, metadata, [&] (mbgl::expected<OfflineRegion, std::exception_ptr> region_) {
if (!region_) {
std::cerr << "Error creating region: " << util::toString(region_.error()) << std::endl;
loop.stop();
exit(1);
} else {
assert(region_);
region = std::make_unique<OfflineRegion>(std::move(*region_));
fileSource.setOfflineRegionObserver(*region, std::make_unique<Observer>(*region, fileSource, loop, mergePath));
fileSource.setOfflineRegionDownloadState(*region, OfflineRegionDownloadState::Active);
}
});
fileSource->createOfflineRegion(
definition, metadata, [&](mbgl::expected<OfflineRegion, std::exception_ptr> region_) {
if (!region_) {
std::cerr << "Error creating region: " << util::toString(region_.error()) << std::endl;
loop.stop();
exit(1);
} else {
assert(region_);
region = std::make_unique<OfflineRegion>(std::move(*region_));
fileSource->setOfflineRegionObserver(*region,
std::make_unique<Observer>(*region, fileSource, loop, mergePath));
fileSource->setOfflineRegionDownloadState(*region, OfflineRegionDownloadState::Active);
}
});

loop.run();
return 0;
Expand Down

0 comments on commit 04ca71c

Please sign in to comment.