From fa7c40c37fc2078b181f77afc808fdc4cd1baae9 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Mon, 7 Mar 2016 21:50:24 -0300 Subject: [PATCH] [core] Add client defined network status This API will let the client force a network status. If set to Offline, we won't make network requests. When set make to Online, it will trigger the pending requests and try to fetch tiles from the network. --- include/mbgl/storage/network_status.hpp | 10 ++++++++++ src/mbgl/storage/network_status.cpp | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/include/mbgl/storage/network_status.hpp b/include/mbgl/storage/network_status.hpp index 2a0ab78ed..420279366 100644 --- a/include/mbgl/storage/network_status.hpp +++ b/include/mbgl/storage/network_status.hpp @@ -1,6 +1,7 @@ #ifndef MBGL_STORAGE_NETWORK_STATUS #define MBGL_STORAGE_NETWORK_STATUS +#include #include #include @@ -12,12 +13,21 @@ class AsyncTask; class NetworkStatus { public: + enum class Status : uint8_t { + Online, + Offline, + }; + + Status Get(); + void Set(Status); + static void Reachable(); static void Subscribe(util::AsyncTask* async); static void Unsubscribe(util::AsyncTask* async); private: + static std::atomic online; static std::mutex mtx; static std::set observers; }; diff --git a/src/mbgl/storage/network_status.cpp b/src/mbgl/storage/network_status.cpp index 796cc5d27..1ef5619bd 100644 --- a/src/mbgl/storage/network_status.cpp +++ b/src/mbgl/storage/network_status.cpp @@ -9,9 +9,27 @@ namespace mbgl { +std::atomic NetworkStatus::online(true); std::mutex NetworkStatus::mtx; std::set NetworkStatus::observers; +NetworkStatus::Status NetworkStatus::Get() { + if (online) { + return Status::Online; + } else { + return Status::Offline; + } +} + +void NetworkStatus::Set(Status status) { + if (status == Status::Online) { + online = true; + Reachable(); + } else { + online = false; + } +} + void NetworkStatus::Subscribe(util::AsyncTask *async) { std::lock_guard lock(NetworkStatus::mtx); observers.insert(async); @@ -23,6 +41,10 @@ void NetworkStatus::Unsubscribe(util::AsyncTask *async) { } void NetworkStatus::Reachable() { + if (!online) { + return; + } + std::lock_guard lock(NetworkStatus::mtx); for (auto async : observers) { async->send();