diff options
author | Mike Morris <michael.patrick.morris@gmail.com> | 2014-11-25 14:18:47 -0500 |
---|---|---|
committer | Mike Morris <michael.patrick.morris@gmail.com> | 2014-12-03 12:25:11 -0500 |
commit | dac76a0f9269a580706e52b730a98f9b1430bcdf (patch) | |
tree | b7c592b648a443ef456479ddd2e21fe66d22abad /src | |
parent | d3ab8951530b760afb19e685078b1480babc2209 (diff) | |
download | qtlocation-mapboxgl-dac76a0f9269a580706e52b730a98f9b1430bcdf.tar.gz |
break out FileSource as an abstract class
add CachingHTTPFileSource implementation
Diffstat (limited to 'src')
-rw-r--r-- | src/map/map.cpp | 35 | ||||
-rw-r--r-- | src/storage/file_source.cpp | 106 |
2 files changed, 16 insertions, 125 deletions
diff --git a/src/map/map.cpp b/src/map/map.cpp index cc22e048f7..a33200fe0b 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -85,13 +85,14 @@ const static bool sqlite_version_check = []() { using namespace mbgl; -Map::Map(View& view_) +Map::Map(View& view_, FileSource& fileSource_) : loop(util::make_unique<uv::loop>()), view(view_), #ifndef NDEBUG mainThread(uv_thread_self()), #endif transform(view_), + fileSource(fileSource_), glyphAtlas(1024, 1024), spriteAtlas(512, 512), texturePool(std::make_shared<TexturePool>()), @@ -115,7 +116,6 @@ Map::~Map() { glyphStore.reset(); style.reset(); texturePool.reset(); - fileSource.reset(); workers.reset(); uv_run(**loop, UV_RUN_DEFAULT); @@ -145,7 +145,6 @@ void Map::start() { // Remove all of these to make sure they are destructed in the correct thread. glyphStore.reset(); - fileSource.reset(); style.reset(); workers.reset(); activeSources.clear(); @@ -286,11 +285,9 @@ void Map::terminate() { void Map::setReachability(bool reachable) { // Note: This function may be called from *any* thread. if (reachable) { - if (fileSource) { - fileSource->prepare([&]() { - fileSource->retryAllPending(); - }); - } + fileSource.prepare([&]() { + fileSource.retryAllPending(); + }); } } @@ -317,11 +314,11 @@ void Map::setStyleJSON(std::string newStyleJSON, const std::string &base) { style = std::make_shared<Style>(); } style->loadJSON((const uint8_t *)styleJSON.c_str()); - if (!fileSource) { - fileSource = std::make_shared<FileSource>(**loop, platform::defaultCacheDatabase()); - glyphStore = std::make_shared<GlyphStore>(*fileSource); + if (!fileSource.hasLoop()) { + fileSource.setLoop(**loop); + glyphStore = std::make_shared<GlyphStore>(fileSource); } - fileSource->setBase(base); + fileSource.setBase(base); glyphStore->setURL(util::mapbox::normalizeGlyphsURL(style->glyph_url, getAccessToken())); update(); } @@ -343,7 +340,7 @@ util::ptr<Sprite> Map::getSprite() { const float pixelRatio = state.getPixelRatio(); const std::string &sprite_url = style->getSpriteURL(); if (!sprite || sprite->pixelRatio != pixelRatio) { - sprite = Sprite::Create(sprite_url, pixelRatio, *fileSource); + sprite = Sprite::Create(sprite_url, pixelRatio, fileSource); } return sprite; @@ -554,7 +551,7 @@ void Map::updateSources() { if (source->enabled) { if (!source->source) { source->source = std::make_shared<Source>(source->info); - source->source->load(*this, *fileSource); + source->source->load(*this, fileSource); } } else { source->source.reset(); @@ -588,20 +585,20 @@ void Map::updateTiles() { source->source->update(*this, getWorker(), style, glyphAtlas, *glyphStore, spriteAtlas, getSprite(), - *texturePool, *fileSource, [this](){ update(); }); + *texturePool, fileSource, [this](){ update(); }); } } void Map::prepare() { - if (!fileSource) { - fileSource = std::make_shared<FileSource>(**loop, platform::defaultCacheDatabase()); - glyphStore = std::make_shared<GlyphStore>(*fileSource); + if (!fileSource.hasLoop()) { + fileSource.setLoop(**loop); + glyphStore = std::make_shared<GlyphStore>(fileSource); } if (!style) { style = std::make_shared<Style>(); - fileSource->request(ResourceType::JSON, styleURL)->onload([&](const Response &res) { + fileSource.request(ResourceType::JSON, styleURL)->onload([&](const Response &res) { if (res.code == 200) { // Calculate the base const size_t pos = styleURL.rfind('/'); diff --git a/src/storage/file_source.cpp b/src/storage/file_source.cpp deleted file mode 100644 index d1e4c8f38d..0000000000 --- a/src/storage/file_source.cpp +++ /dev/null @@ -1,106 +0,0 @@ -#include <mbgl/storage/file_source.hpp> -#include <mbgl/storage/file_request.hpp> -#include <mbgl/storage/http_request.hpp> -#include <mbgl/storage/sqlite_store.hpp> -#include <mbgl/util/uv-messenger.h> -#include <mbgl/util/std.hpp> - -#include <uv.h> - -namespace mbgl { - -FileSource::FileSource(uv_loop_t *loop_, const std::string &path) - : thread_id(uv_thread_self()), - store(!path.empty() ? util::ptr<SQLiteStore>(new SQLiteStore(loop_, path)) : nullptr), - loop(loop_), - queue(new uv_messenger_t) { - - uv_messenger_init(loop, queue, [](void *ptr) { - std::unique_ptr<std::function<void()>> fn { reinterpret_cast<std::function<void()> *>(ptr) }; - (*fn)(); - }); - uv_unref((uv_handle_t *)&queue->async); -} - -FileSource::~FileSource() { - assert(thread_id == uv_thread_self()); - uv_messenger_stop(queue, [](uv_messenger_t *msgr) { - delete msgr; - }); - - util::ptr<BaseRequest> req; - - // Send a cancel() message to all requests that we are still holding. - for (const std::pair<std::string, std::weak_ptr<BaseRequest>> &pair : pending) { - if ((req = pair.second.lock())) { - req->cancel(); - } - } -} - -void FileSource::setBase(const std::string &value) { - assert(thread_id == uv_thread_self()); - base = value; -} - -const std::string &FileSource::getBase() const { - assert(thread_id == uv_thread_self()); - return base; -} - -std::unique_ptr<Request> FileSource::request(ResourceType type, const std::string &url) { - assert(thread_id == uv_thread_self()); - - // Make URL absolute. - const std::string absoluteURL = [&]() -> std::string { - const size_t separator = url.find("://"); - if (separator == std::string::npos) { - // Relative URL. - return base + url; - } else { - return url; - } - }(); - - util::ptr<BaseRequest> req; - - // First, try to find an existing Request object. - auto it = pending.find(absoluteURL); - if (it != pending.end()) { - req = it->second.lock(); - } - - if (!req) { - if (absoluteURL.substr(0, 7) == "file://") { - req = std::make_shared<FileRequest>(absoluteURL.substr(7), loop); - } else { - req = std::make_shared<HTTPRequest>(type, absoluteURL, loop, store); - } - - pending.emplace(absoluteURL, req); - } - - return util::make_unique<Request>(req); -} - -void FileSource::prepare(std::function<void()> fn) { - if (thread_id == uv_thread_self()) { - fn(); - } else { - uv_messenger_send(queue, new std::function<void()>(std::move(fn))); - } -} - -void FileSource::retryAllPending() { - assert(thread_id == uv_thread_self()); - - util::ptr<BaseRequest> req; - for (const std::pair<std::string, std::weak_ptr<BaseRequest>> &pair : pending) { - if ((req = pair.second.lock())) { - req->retryImmediately(); - } - } - -} - -}
\ No newline at end of file |