forked from organicmaps/organicmaps
[downloader] Fix issue when url download failed. Use default server urls.
This commit is contained in:
parent
98efcc314a
commit
74a22a60ad
11 changed files with 126 additions and 106 deletions
|
@ -10,6 +10,7 @@
|
|||
|
||||
#include "../../../../../platform/platform.hpp"
|
||||
#include "../../../../../platform/http_request.hpp"
|
||||
#include "../../../../../platform/servers_list.hpp"
|
||||
|
||||
#include "../../../../../base/logging.hpp"
|
||||
#include "../../../../../base/string_utils.hpp"
|
||||
|
@ -186,36 +187,28 @@ extern "C"
|
|||
glbTotal, glbProgress);
|
||||
}
|
||||
|
||||
typedef downloader::HttpRequest::CallbackT CallbackT;
|
||||
|
||||
void DownloadURLListFinished(downloader::HttpRequest & req,
|
||||
downloader::HttpRequest::CallbackT const & onFinish,
|
||||
downloader::HttpRequest::CallbackT const & onProgress)
|
||||
CallbackT const & onFinish, CallbackT const & onProgress)
|
||||
{
|
||||
if (req.Status() == downloader::HttpRequest::EFailed)
|
||||
onFinish(req);
|
||||
else
|
||||
FileToDownload & curFile = g_filesToDownload.back();
|
||||
|
||||
LOG(LINFO, ("Finished URL list download for", curFile.m_fileName));
|
||||
|
||||
downloader::GetServerListFromRequest(req, curFile.m_urls);
|
||||
|
||||
storage::Storage const & storage = g_framework->Storage();
|
||||
for (size_t i = 0; i < curFile.m_urls.size(); ++i)
|
||||
{
|
||||
FileToDownload & curFile = g_filesToDownload.back();
|
||||
|
||||
LOG(LDEBUG, ("finished URL list download for", curFile.m_fileName));
|
||||
|
||||
downloader::ParseServerList(req.Data(), curFile.m_urls);
|
||||
|
||||
for (size_t i = 0; i < curFile.m_urls.size(); ++i)
|
||||
{
|
||||
curFile.m_urls[i] = curFile.m_urls[i] + OMIM_OS_NAME "/"
|
||||
+ strings::to_string(g_framework->Storage().GetCurrentVersion()) + "/"
|
||||
+ UrlEncode(curFile.m_fileName);
|
||||
LOG(LDEBUG, (curFile.m_urls[i]));
|
||||
}
|
||||
|
||||
g_currentRequest.reset(downloader::HttpRequest::GetFile(curFile.m_urls,
|
||||
curFile.m_pathOnSdcard,
|
||||
curFile.m_fileSize,
|
||||
onFinish,
|
||||
onProgress,
|
||||
64 * 1024,
|
||||
false));
|
||||
curFile.m_urls[i] = storage.GetFileDownloadUrl(curFile.m_urls[i], curFile.m_fileName);
|
||||
LOG(LINFO, (curFile.m_urls[i]));
|
||||
}
|
||||
|
||||
g_currentRequest.reset(downloader::HttpRequest::GetFile(
|
||||
curFile.m_urls, curFile.m_pathOnSdcard, curFile.m_fileSize,
|
||||
onFinish, onProgress,
|
||||
64 * 1024, false));
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
|
@ -236,14 +229,12 @@ extern "C"
|
|||
|
||||
LOG(LDEBUG, ("downloading", curFile.m_fileName, "sized", curFile.m_fileSize, "bytes"));
|
||||
|
||||
downloader::HttpRequest::CallbackT onFinish(bind(&DownloadFileFinished, jni::make_global_ref(observer), _1));
|
||||
downloader::HttpRequest::CallbackT onProgress(bind(&DownloadFileProgress, jni::make_global_ref(observer), _1));
|
||||
CallbackT onFinish(bind(&DownloadFileFinished, jni::make_global_ref(observer), _1));
|
||||
CallbackT onProgress(bind(&DownloadFileProgress, jni::make_global_ref(observer), _1));
|
||||
|
||||
g_currentRequest.reset(downloader::HttpRequest::PostJson(GetPlatform().MetaServerUrl(),
|
||||
curFile.m_fileName,
|
||||
bind(&DownloadURLListFinished, _1,
|
||||
onFinish,
|
||||
onProgress)));
|
||||
g_currentRequest.reset(downloader::HttpRequest::PostJson(
|
||||
GetPlatform().MetaServerUrl(), curFile.m_fileName,
|
||||
bind(&DownloadURLListFinished, _1, onFinish, onProgress)));
|
||||
|
||||
return ERR_FILE_IN_PROGRESS;
|
||||
}
|
||||
|
|
|
@ -3,9 +3,8 @@
|
|||
#include "../std/string.hpp"
|
||||
#include "../std/vector.hpp"
|
||||
#include "../std/utility.hpp"
|
||||
#include "../std/set.hpp"
|
||||
#include "../std/stdint.hpp"
|
||||
#include "../std/cstdio.hpp"
|
||||
|
||||
|
||||
namespace downloader
|
||||
{
|
||||
|
|
|
@ -8,17 +8,13 @@
|
|||
#include "../base/thread.hpp"
|
||||
#endif
|
||||
|
||||
#include "../base/std_serialization.hpp"
|
||||
#include "../coding/internal/file_data.hpp"
|
||||
#include "../coding/file_writer.hpp"
|
||||
|
||||
#include "../base/logging.hpp"
|
||||
|
||||
#include "../coding/file_writer_stream.hpp"
|
||||
#include "../coding/file_reader_stream.hpp"
|
||||
#include "../coding/internal/file_data.hpp"
|
||||
|
||||
#include "../std/scoped_ptr.hpp"
|
||||
|
||||
#include "../3party/jansson/myjansson.hpp"
|
||||
|
||||
|
||||
#ifdef OMIM_OS_IPHONE
|
||||
#include <sys/xattr.h>
|
||||
|
@ -169,6 +165,8 @@ class FileHttpRequest : public HttpRequest, public IHttpThreadCallback
|
|||
if (m_onProgress)
|
||||
m_onProgress(*this);
|
||||
}
|
||||
else
|
||||
LOG(LWARNING, (m_filePath, "HttpRequest error:", httpCode));
|
||||
|
||||
ChunksDownloadStrategy::ResultT const result = StartThreads();
|
||||
|
||||
|
@ -281,25 +279,4 @@ HttpRequest * HttpRequest::GetFile(vector<string> const & urls, string const & f
|
|||
return new FileHttpRequest(urls, filePath, fileSize, onFinish, onProgress, chunkSize, doCleanProgressFiles);
|
||||
}
|
||||
|
||||
bool ParseServerList(string const & jsonStr, vector<string> & outUrls)
|
||||
{
|
||||
outUrls.clear();
|
||||
try
|
||||
{
|
||||
my::Json root(jsonStr.c_str());
|
||||
for (size_t i = 0; i < json_array_size(root); ++i)
|
||||
{
|
||||
char const * url = json_string_value(json_array_get(root, i));
|
||||
if (url)
|
||||
outUrls.push_back(url);
|
||||
}
|
||||
}
|
||||
catch (std::exception const & e)
|
||||
{
|
||||
LOG(LERROR, ("Can't parse server list json", e.what(), jsonStr));
|
||||
}
|
||||
return !outUrls.empty();
|
||||
}
|
||||
|
||||
|
||||
} // namespace downloader
|
||||
|
|
|
@ -56,6 +56,4 @@ public:
|
|||
bool doCleanProgressFiles = true);
|
||||
};
|
||||
|
||||
bool ParseServerList(string const & jsonStr, vector<string> & outUrls);
|
||||
|
||||
} // namespace downloader
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include "../coding/sha2.hpp"
|
||||
#include "../coding/base64.hpp"
|
||||
|
||||
|
||||
string Platform::ReadPathForFile(string const & file) const
|
||||
{
|
||||
string fullPath = m_writableDir + file;
|
||||
|
@ -28,3 +29,18 @@ string Platform::HashUniqueID(string const & s)
|
|||
return base64::encode(xoredHash);
|
||||
}
|
||||
|
||||
string Platform::MetaServerUrl() const
|
||||
{
|
||||
if (IsFeatureSupported("search"))
|
||||
return "http://active.servers.url";
|
||||
else
|
||||
return "http://active.servers.url";
|
||||
}
|
||||
|
||||
string Platform::DefaultUrlsJSON() const
|
||||
{
|
||||
if (IsFeatureSupported("search"))
|
||||
return "[\"http://1st.default.server/\",\"http://2nd.default.server/\",\"http://3rd.default.server/\"]";
|
||||
else
|
||||
return "[\"http://1st.default.server/\",\"http://2nd.default.server/\",\"http://3rd.default.server/\"]";
|
||||
}
|
||||
|
|
|
@ -119,22 +119,10 @@ public:
|
|||
|
||||
/// @return url for clients to download maps
|
||||
/// Different urls are returned for versions with and without search support
|
||||
inline string MetaServerUrl() const
|
||||
{
|
||||
if (IsFeatureSupported("search"))
|
||||
return "http://active.servers.url";
|
||||
else
|
||||
return "http://active.servers.url";
|
||||
}
|
||||
string MetaServerUrl() const;
|
||||
|
||||
/// @return JSON-encoded list of urls if metaserver is unreachable
|
||||
inline string DefaultUrlsJSON() const
|
||||
{
|
||||
if (IsFeatureSupported("search"))
|
||||
return "[\"http://1st.default.server/\",\"http://2nd.default.server/\",\"http://3rd.default.server/\"]";
|
||||
else
|
||||
return "[\"http://1st.default.server/\",\"http://2nd.default.server/\",\"http://3rd.default.server/\"]";
|
||||
}
|
||||
string DefaultUrlsJSON() const;
|
||||
};
|
||||
|
||||
extern "C" Platform & GetPlatform();
|
||||
|
|
|
@ -60,6 +60,7 @@ HEADERS += \
|
|||
http_request.hpp \
|
||||
http_thread_callback.hpp \
|
||||
chunks_download_strategy.hpp \
|
||||
servers_list.hpp \
|
||||
|
||||
SOURCES += \
|
||||
preferred_languages.cpp \
|
||||
|
@ -67,5 +68,5 @@ SOURCES += \
|
|||
video_timer.cpp \
|
||||
http_request.cpp \
|
||||
chunks_download_strategy.cpp \
|
||||
platform.cpp
|
||||
|
||||
platform.cpp \
|
||||
servers_list.cpp \
|
||||
|
|
54
platform/servers_list.cpp
Normal file
54
platform/servers_list.cpp
Normal file
|
@ -0,0 +1,54 @@
|
|||
#include "servers_list.hpp"
|
||||
#include "http_request.hpp"
|
||||
#include "settings.hpp"
|
||||
#include "platform.hpp"
|
||||
|
||||
#include "../base/logging.hpp"
|
||||
#include "../base/assert.hpp"
|
||||
|
||||
#include "../3party/jansson/myjansson.hpp"
|
||||
|
||||
|
||||
#define SETTINGS_SERVERS_KEY "LastBaseUrls"
|
||||
|
||||
|
||||
namespace downloader
|
||||
{
|
||||
|
||||
bool ParseServerList(string const & jsonStr, vector<string> & outUrls)
|
||||
{
|
||||
outUrls.clear();
|
||||
try
|
||||
{
|
||||
my::Json root(jsonStr.c_str());
|
||||
for (size_t i = 0; i < json_array_size(root); ++i)
|
||||
{
|
||||
char const * url = json_string_value(json_array_get(root, i));
|
||||
if (url)
|
||||
outUrls.push_back(url);
|
||||
}
|
||||
}
|
||||
catch (std::exception const & e)
|
||||
{
|
||||
LOG(LERROR, ("Can't parse server list json", e.what(), jsonStr));
|
||||
}
|
||||
return !outUrls.empty();
|
||||
}
|
||||
|
||||
void GetServerListFromRequest(HttpRequest const & request, vector<string> & urls)
|
||||
{
|
||||
if (request.Status() == HttpRequest::ECompleted &&
|
||||
ParseServerList(request.Data(), urls))
|
||||
{
|
||||
Settings::Set(SETTINGS_SERVERS_KEY, request.Data());
|
||||
}
|
||||
else
|
||||
{
|
||||
string serverList;
|
||||
if (!Settings::Get(SETTINGS_SERVERS_KEY, serverList))
|
||||
serverList = GetPlatform().DefaultUrlsJSON();
|
||||
VERIFY ( ParseServerList(serverList, urls), () );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
12
platform/servers_list.hpp
Normal file
12
platform/servers_list.hpp
Normal file
|
@ -0,0 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
#include "../std/vector.hpp"
|
||||
#include "../std/string.hpp"
|
||||
|
||||
|
||||
namespace downloader
|
||||
{
|
||||
class HttpRequest;
|
||||
|
||||
void GetServerListFromRequest(HttpRequest const & request, vector<string> & urls);
|
||||
}
|
|
@ -2,20 +2,20 @@
|
|||
|
||||
#include "../defines.hpp"
|
||||
|
||||
#include "../base/logging.hpp"
|
||||
#include "../base/string_utils.hpp"
|
||||
|
||||
#include "../indexer/data_factory.hpp"
|
||||
#include "../indexer/search_index_builder.hpp"
|
||||
|
||||
#include "../platform/platform.hpp"
|
||||
#include "../platform/settings.hpp"
|
||||
#include "../platform/servers_list.hpp"
|
||||
|
||||
#include "../coding/file_writer.hpp"
|
||||
#include "../coding/file_reader.hpp"
|
||||
#include "../coding/file_container.hpp"
|
||||
#include "../coding/url_encode.hpp"
|
||||
|
||||
#include "../base/logging.hpp"
|
||||
#include "../base/string_utils.hpp"
|
||||
|
||||
#include "../version/version.hpp"
|
||||
|
||||
#include "../std/algorithm.hpp"
|
||||
|
@ -23,9 +23,6 @@
|
|||
#include "../std/bind.hpp"
|
||||
#include "../std/sstream.hpp"
|
||||
|
||||
#include "../3party/jansson/myjansson.hpp"
|
||||
|
||||
#define SETTINGS_SERVERS_KEY "LastBaseUrls"
|
||||
|
||||
using namespace downloader;
|
||||
|
||||
|
@ -468,21 +465,11 @@ namespace storage
|
|||
CountryFile const & file = CountryByIndex(m_queue.front()).Files().front();
|
||||
|
||||
vector<string> urls;
|
||||
if (request.Status() == HttpRequest::ECompleted
|
||||
&& ParseServerList(request.Data(), urls))
|
||||
Settings::Set(SETTINGS_SERVERS_KEY, request.Data());
|
||||
else
|
||||
{
|
||||
string serverList;
|
||||
if (!Settings::Get(SETTINGS_SERVERS_KEY, serverList))
|
||||
serverList = GetPlatform().DefaultUrlsJSON();
|
||||
VERIFY(ParseServerList(serverList, urls), ());
|
||||
}
|
||||
GetServerListFromRequest(request, urls);
|
||||
|
||||
// append actual version and file name
|
||||
for (size_t i = 0; i < urls.size(); ++i)
|
||||
urls[i] = urls[i] + OMIM_OS_NAME "/"
|
||||
+ strings::to_string(m_currentVersion) + "/" + UrlEncode(file.GetFileWithExt());
|
||||
urls[i] = GetFileDownloadUrl(urls[i], file.GetFileWithExt());
|
||||
|
||||
m_request.reset(HttpRequest::GetFile(urls,
|
||||
GetPlatform().WritablePathForFile(file.GetFileWithExt()),
|
||||
|
@ -491,9 +478,9 @@ namespace storage
|
|||
bind(&Storage::OnMapDownloadProgress, this, _1)));
|
||||
}
|
||||
|
||||
int64_t Storage::GetCurrentVersion() const
|
||||
string Storage::GetFileDownloadUrl(string const & baseUrl, string const & fName) const
|
||||
{
|
||||
return m_currentVersion;
|
||||
return baseUrl + OMIM_OS_NAME "/" + strings::to_string(m_currentVersion) + "/" + UrlEncode(fName);
|
||||
}
|
||||
|
||||
TIndex const Storage::FindIndexByName(string const & name) const
|
||||
|
|
|
@ -131,7 +131,6 @@ namespace storage
|
|||
void ReportProgress(TIndex const & index, pair<int64_t, int64_t> const & p);
|
||||
|
||||
public:
|
||||
|
||||
Storage();
|
||||
|
||||
void Init(TAddMapFunction addFunc,
|
||||
|
@ -170,8 +169,6 @@ namespace storage
|
|||
|
||||
void NotifyStatusChanged(TIndex const & index) const;
|
||||
|
||||
int64_t GetCurrentVersion() const;
|
||||
string GetFileDownloadUrl(string const & baseUrl, string const & fName) const;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue