[downloader][mac][ios] Integrated new implementation

This commit is contained in:
Alex Zolotarev 2011-11-10 23:54:03 +03:00 committed by Alex Zolotarev
parent d689488682
commit 8b7003765f
9 changed files with 57 additions and 73 deletions

View file

@ -12,6 +12,6 @@
- (id) initWithStorage: (storage::Storage &) storage andIndex: (storage::TIndex const &) index andHeader: (NSString *) header;
- (void) OnCountryChange: (storage::TIndex const &) index;
- (void) OnDownload: (storage::TIndex const &) index withProgress: (HttpProgressT const &) progress;
- (void) OnDownload: (storage::TIndex const &) index withProgress: (pair<int64_t, int64_t> const &) progress;
@end

View file

@ -391,7 +391,7 @@ UITableViewCell * g_clickedCell = nil;
}
}
- (void) OnDownload: (TIndex const &) index withProgress: (HttpProgressT const &) progress
- (void) OnDownload: (TIndex const &) index withProgress: (pair<int64_t, int64_t> const &) progress
{
if (IsOurIndex(index, m_index))
{
@ -399,7 +399,7 @@ UITableViewCell * g_clickedCell = nil;
UITableViewCell * cell = [tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow: RowFromIndex(index) inSection: 0]];
if (cell)
cell.detailTextLabel.text = [NSString stringWithFormat: @"Downloading %qu%%, touch to cancel",
progress.m_current * 100 / progress.m_total];
progress.first * 100 / progress.second];
}
}

View file

@ -39,7 +39,7 @@ using namespace storage;
[(CountriesViewController *)controller OnCountryChange: index];
}
- (void) OnCountryDownload: (TIndex const &) index withProgress: (HttpProgressT const &) progress
- (void) OnCountryDownload: (TIndex const &) index withProgress: (pair<int64_t, int64_t> const &) progress
{
UIViewController * controller = [self ControllerByIndex:index];
if (controller && [controller respondsToSelector:@selector(OnDownload:withProgress:)])

View file

@ -147,7 +147,6 @@ bool MainWindow::winEvent(MSG * msg, long * result)
MainWindow::~MainWindow()
{
SaveState();
GetDownloadManager().CancelAllDownloads();
}
void MainWindow::SaveState()

View file

@ -303,22 +303,23 @@ namespace qt
}
void UpdateDialog::OnCountryDownloadProgress(TIndex const & index,
HttpProgressT const & progress)
pair<int64_t, int64_t> const & progress)
{
QTreeWidgetItem * item = GetTreeItemByIndex(*m_tree, index);
if (item)
{
QString speed;
if (progress.m_bytesPerSec > 1000 * 1000)
speed = QString(" %1 MB/s").arg(QString::number(static_cast<double>(progress.m_bytesPerSec) / (1000.0 * 1000.0),
'f', 1));
else if (progress.m_bytesPerSec > 1000)
speed = QString(" %1 kB/s").arg(progress.m_bytesPerSec / 1000);
else if (progress.m_bytesPerSec >= 0)
speed = QString(" %1 B/sec").arg(progress.m_bytesPerSec);
// QString speed;
// if (progress.m_bytesPerSec > 1000 * 1000)
// speed = QString(" %1 MB/s").arg(QString::number(static_cast<double>(progress.m_bytesPerSec) / (1000.0 * 1000.0),
// 'f', 1));
// else if (progress.m_bytesPerSec > 1000)
// speed = QString(" %1 kB/s").arg(progress.m_bytesPerSec / 1000);
// else if (progress.m_bytesPerSec >= 0)
// speed = QString(" %1 B/sec").arg(progress.m_bytesPerSec);
item->setText(KColumnIndexSize, QString("%1%%2").arg(progress.m_current * 100 / progress.m_total)
.arg(speed));
item->setText(KColumnIndexSize, QString("%1%").arg(progress.first * 100 / progress.second));
// item->setText(KColumnIndexSize, QString("%1%%2").arg(progress.m_current * 100 / progress.m_total)
// .arg(speed));
}
}

View file

@ -23,7 +23,7 @@ namespace qt
//@{
void OnCountryChanged(storage::TIndex const & index);
void OnCountryDownloadProgress(storage::TIndex const & index,
HttpProgressT const & progress);
pair<int64_t, int64_t> const & progress);
//@}
void ShowDialog();

View file

@ -16,7 +16,7 @@
namespace storage
{
/// Simple check - compare url size with real file size on disk
/// Simple check - if file is present on disk. Incomplete download has different file name.
bool IsFileDownloaded(CountryFile const & file)
{
uint64_t size = 0;

View file

@ -5,6 +5,8 @@
#include "../indexer/data_factory.hpp"
#include "../platform/platform.hpp"
#include "../coding/file_writer.hpp"
#include "../coding/file_reader.hpp"
#include "../coding/file_container.hpp"
@ -12,7 +14,6 @@
#include "../version/version.hpp"
#include "../std/set.hpp"
#include "../std/algorithm.hpp"
#include "../std/target_os.hpp"
#include "../std/bind.hpp"
@ -56,8 +57,7 @@ namespace storage
string Storage::UpdateBaseUrl() const
{
// we do not add server name here - it should be added automatically in Downloader Engine
return OMIM_OS_NAME "/" + strings::to_string(m_currentVersion) + "/";
return "http://svobodu404popugajam.mapswithme.com:34568/maps/" OMIM_OS_NAME "/" + strings::to_string(m_currentVersion) + "/";
}
CountriesContainerT const & NodeFromIndex(CountriesContainerT const & root, TIndex const & index)
@ -145,8 +145,8 @@ namespace storage
{
// reset total country's download progress
LocalAndRemoteSizeT const size = CountryByIndex(index).Size();
m_countryProgress.m_current = 0;
m_countryProgress.m_total = size.second;
m_countryProgress.first = 0;
m_countryProgress.second = size.second;
DownloadNextCountryFromQueue();
}
@ -183,12 +183,13 @@ namespace storage
{
if (!IsFileDownloaded(*it))
{
HttpStartParams params;
params.m_url = UpdateBaseUrl() + UrlEncode(it->GetFileWithExt());
params.m_fileToSave = GetPlatform().WritablePathForFile(it->GetFileWithExt());
params.m_finish = bind(&Storage::OnMapDownloadFinished, this, _1);
params.m_progress = bind(&Storage::OnMapDownloadProgress, this, _1);
GetDownloadManager().HttpRequest(params);
vector<string> urls;
urls.push_back(UpdateBaseUrl() + UrlEncode(it->GetFileWithExt()));
m_request.reset(downloader::HttpRequest::GetFile(urls,
GetPlatform().WritablePathForFile(it->GetFileWithExt()),
it->m_remoteSize,
bind(&Storage::OnMapDownloadFinished, this, _1),
bind(&Storage::OnMapDownloadProgress, this, _1)));
// notify GUI - new status for country, "Downloading"
if (m_observerChange)
m_observerChange(index);
@ -200,8 +201,8 @@ namespace storage
// reset total country's download progress
if (!m_queue.empty())
{
m_countryProgress.m_current = 0;
m_countryProgress.m_total = CountryByIndex(m_queue.front()).Size().second;
m_countryProgress.first = 0;
m_countryProgress.second = CountryByIndex(m_queue.front()).Size().second;
}
// and notify GUI - new status for country, "OnDisk"
if (m_observerChange)
@ -209,16 +210,6 @@ namespace storage
}
}
struct CancelDownloading
{
string const m_baseUrl;
CancelDownloading(string const & baseUrl) : m_baseUrl(baseUrl) {}
void operator()(CountryFile const & file)
{
GetDownloadManager().CancelDownload((m_baseUrl + UrlEncode(file.GetFileWithExt())).c_str());
}
};
class DeleteMap
{
string m_workingDir;
@ -261,7 +252,7 @@ namespace storage
{
if (found == m_queue.begin())
{ // stop download
for_each(country.Files().begin(), country.Files().end(), CancelDownloading(UpdateBaseUrl()));
m_request.reset();
// remove from the queue
m_queue.erase(found);
// start another download if the queue is not empty
@ -315,18 +306,18 @@ namespace storage
m_observerProgress.clear();
}
void Storage::OnMapDownloadFinished(HttpFinishedParams const & result)
void Storage::OnMapDownloadFinished(downloader::HttpRequest & request)
{
if (m_queue.empty())
{
ASSERT(false, ("Invalid url?", result.m_url));
ASSERT(false, ("Invalid url?", request.Data()));
return;
}
if (result.m_error != EHttpDownloadOk)
if (request.Status() == downloader::HttpRequest::EFailed)
{
// remove failed country from the queue
TIndex failedIndex = m_queue.front();
TIndex const failedIndex = m_queue.front();
m_queue.pop_front();
m_failedCountries.insert(failedIndex);
// notify GUI about failed country
@ -335,12 +326,12 @@ namespace storage
}
else
{
LocalAndRemoteSizeT size = CountryByIndex(m_queue.front()).Size();
LocalAndRemoteSizeT const size = CountryByIndex(m_queue.front()).Size();
if (size.second != 0)
m_countryProgress.m_current = size.first;
m_countryProgress.first = size.first;
// get file descriptor
string file = result.m_file;
string file = request.Data();
// FIXME
string::size_type const i = file.find_last_of("/\\");
@ -355,10 +346,11 @@ namespace storage
LoadMapHeader(GetPlatform().GetReader(file), header);
m_updateRect(header.GetBounds());
}
m_request.reset();
DownloadNextCountryFromQueue();
}
void Storage::OnMapDownloadProgress(HttpProgressT const & progress)
void Storage::OnMapDownloadProgress(downloader::HttpRequest & request)
{
if (m_queue.empty())
{
@ -368,9 +360,9 @@ namespace storage
if (m_observerProgress)
{
HttpProgressT p(progress);
p.m_current = m_countryProgress.m_current + progress.m_current;
p.m_total = m_countryProgress.m_total;
downloader::HttpRequest::ProgressT p = request.Progress();
p.first += m_countryProgress.first;
p.second = m_countryProgress.second;
m_observerProgress(m_queue.front(), p);
}
}

View file

@ -1,17 +1,16 @@
#pragma once
#include "../platform/download_manager.hpp"
#include "../platform/platform.hpp"
#include "../defines.hpp"
#include "../storage/country.hpp"
#include "../platform/http_request.hpp"
#include "../std/vector.hpp"
#include "../std/map.hpp"
#include "../std/list.hpp"
#include "../std/string.hpp"
#include "../std/set.hpp"
#include "../std/function.hpp"
#include "../std/scoped_ptr.hpp"
namespace storage
{
@ -26,17 +25,6 @@ namespace storage
EUnknown
};
enum TUpdateResult
{
ENoAnyUpdateAvailable = 0,
ENewBinaryAvailable = 0x01,
EBinaryCheckFailed = 0x02,
EBinaryUpdateFailed = 0x04,
ENewDataAvailable = 0x08,
EDataCheckFailed = 0x10,
EDataUpdateFailed = 0x20
};
struct TIndex
{
static int const INVALID;
@ -62,6 +50,9 @@ namespace storage
/// Can be used to store local maps and/or maps available for download
class Storage
{
/// We support only one simultaneous request at the moment
scoped_ptr<downloader::HttpRequest> m_request;
/// stores timestamp for update checks
int64_t m_currentVersion;
@ -69,8 +60,9 @@ namespace storage
typedef list<TIndex> TQueue;
TQueue m_queue;
/// used to correctly calculate total country download progress
HttpProgressT m_countryProgress;
/// used to correctly calculate total country download progress with more than 1 file
/// <current, total>
downloader::HttpRequest::ProgressT m_countryProgress;
typedef set<TIndex> TFailedCountries;
/// stores countries which download has failed recently
@ -79,7 +71,7 @@ namespace storage
/// @name Communicate with GUI
//@{
typedef function<void (TIndex const &)> TObserverChangeCountryFunction;
typedef function<void (TIndex const &, HttpProgressT const &)> TObserverProgressFunction;
typedef function<void (TIndex const &, pair<int64_t, int64_t> const &)> TObserverProgressFunction;
TObserverChangeCountryFunction m_observerChange;
TObserverProgressFunction m_observerProgress;
//@}
@ -112,8 +104,8 @@ namespace storage
/// @name Called from DownloadManager
//@{
void OnMapDownloadFinished(HttpFinishedParams const & params);
void OnMapDownloadProgress(HttpProgressT const & progress);
void OnMapDownloadFinished(downloader::HttpRequest & request);
void OnMapDownloadProgress(downloader::HttpRequest & request);
//@}
/// @name Current impl supports only one observer