From 8b7003765f15dccf14d06eb1193a904c3aa0dd1a Mon Sep 17 00:00:00 2001 From: Alex Zolotarev Date: Thu, 10 Nov 2011 23:54:03 +0300 Subject: [PATCH] [downloader][mac][ios] Integrated new implementation --- .../Maps/Settings/CountriesViewController.h | 2 +- .../Maps/Settings/CountriesViewController.mm | 4 +- iphone/Maps/Settings/SettingsManager.mm | 2 +- qt/mainwindow.cpp | 1 - qt/update_dialog.cpp | 23 +++---- qt/update_dialog.hpp | 2 +- storage/country.cpp | 2 +- storage/storage.cpp | 62 ++++++++----------- storage/storage.hpp | 32 ++++------ 9 files changed, 57 insertions(+), 73 deletions(-) diff --git a/iphone/Maps/Settings/CountriesViewController.h b/iphone/Maps/Settings/CountriesViewController.h index 1a71ae4316..4bbe6d1366 100644 --- a/iphone/Maps/Settings/CountriesViewController.h +++ b/iphone/Maps/Settings/CountriesViewController.h @@ -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 const &) progress; @end diff --git a/iphone/Maps/Settings/CountriesViewController.mm b/iphone/Maps/Settings/CountriesViewController.mm index 12b98eee31..1b8519feaa 100644 --- a/iphone/Maps/Settings/CountriesViewController.mm +++ b/iphone/Maps/Settings/CountriesViewController.mm @@ -391,7 +391,7 @@ UITableViewCell * g_clickedCell = nil; } } -- (void) OnDownload: (TIndex const &) index withProgress: (HttpProgressT const &) progress +- (void) OnDownload: (TIndex const &) index withProgress: (pair 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]; } } diff --git a/iphone/Maps/Settings/SettingsManager.mm b/iphone/Maps/Settings/SettingsManager.mm index edfaaa5258..63a6952025 100644 --- a/iphone/Maps/Settings/SettingsManager.mm +++ b/iphone/Maps/Settings/SettingsManager.mm @@ -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 const &) progress { UIViewController * controller = [self ControllerByIndex:index]; if (controller && [controller respondsToSelector:@selector(OnDownload:withProgress:)]) diff --git a/qt/mainwindow.cpp b/qt/mainwindow.cpp index cb73d15e39..501e9f968d 100644 --- a/qt/mainwindow.cpp +++ b/qt/mainwindow.cpp @@ -147,7 +147,6 @@ bool MainWindow::winEvent(MSG * msg, long * result) MainWindow::~MainWindow() { SaveState(); - GetDownloadManager().CancelAllDownloads(); } void MainWindow::SaveState() diff --git a/qt/update_dialog.cpp b/qt/update_dialog.cpp index 6ae1c3944d..cda4080b8c 100644 --- a/qt/update_dialog.cpp +++ b/qt/update_dialog.cpp @@ -303,22 +303,23 @@ namespace qt } void UpdateDialog::OnCountryDownloadProgress(TIndex const & index, - HttpProgressT const & progress) + pair 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(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(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)); } } diff --git a/qt/update_dialog.hpp b/qt/update_dialog.hpp index 36c5274aec..ae44adbcf7 100644 --- a/qt/update_dialog.hpp +++ b/qt/update_dialog.hpp @@ -23,7 +23,7 @@ namespace qt //@{ void OnCountryChanged(storage::TIndex const & index); void OnCountryDownloadProgress(storage::TIndex const & index, - HttpProgressT const & progress); + pair const & progress); //@} void ShowDialog(); diff --git a/storage/country.cpp b/storage/country.cpp index 3a3969ea6c..0d8bc770f7 100644 --- a/storage/country.cpp +++ b/storage/country.cpp @@ -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; diff --git a/storage/storage.cpp b/storage/storage.cpp index 413116068a..8c67e8cf49 100644 --- a/storage/storage.cpp +++ b/storage/storage.cpp @@ -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 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); } } diff --git a/storage/storage.hpp b/storage/storage.hpp index ecda86e23b..568ea50549 100644 --- a/storage/storage.hpp +++ b/storage/storage.hpp @@ -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 m_request; + /// stores timestamp for update checks int64_t m_currentVersion; @@ -69,8 +60,9 @@ namespace storage typedef list 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 + /// + downloader::HttpRequest::ProgressT m_countryProgress; typedef set TFailedCountries; /// stores countries which download has failed recently @@ -79,7 +71,7 @@ namespace storage /// @name Communicate with GUI //@{ typedef function TObserverChangeCountryFunction; - typedef function TObserverProgressFunction; + typedef function 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