[downloader] Add comments, minor changes.

This commit is contained in:
vng 2012-05-26 00:58:30 +03:00 committed by Alex Zolotarev
parent 08ff3dda47
commit e1a36441a1
3 changed files with 15 additions and 13 deletions

View file

@ -1,7 +1,5 @@
#include "chunks_download_strategy.hpp"
#include "../defines.hpp"
#include "../coding/file_writer.hpp"
#include "../coding/file_reader.hpp"
@ -40,7 +38,6 @@ ChunksDownloadStrategy::GetChunk(RangeT const & range)
void ChunksDownloadStrategy::InitChunks(int64_t fileSize, int64_t chunkSize, ChunkStatusT status)
{
// init chunks which should be downloaded
m_chunks.reserve(fileSize / chunkSize + 2);
for (int64_t i = 0; i < fileSize; i += chunkSize)
m_chunks.push_back(ChunkT(i, status));
@ -70,7 +67,7 @@ void ChunksDownloadStrategy::SaveChunks(string const & fName)
{
try
{
FileWriter w(fName + RESUME_FILE_EXTENSION);
FileWriter w(fName);
w.Write(&m_chunks[0], sizeof(ChunkT) * m_chunks.size());
return;
}
@ -81,7 +78,7 @@ void ChunksDownloadStrategy::SaveChunks(string const & fName)
}
// Delete if no chunks or some error occured.
(void)FileWriter::DeleteFileX(fName + RESUME_FILE_EXTENSION);
(void)FileWriter::DeleteFileX(fName);
}
int64_t ChunksDownloadStrategy::LoadOrInitChunks( string const & fName,
@ -92,7 +89,7 @@ int64_t ChunksDownloadStrategy::LoadOrInitChunks( string const & fName,
try
{
FileReader r(fName + RESUME_FILE_EXTENSION);
FileReader r(fName);
// Load chunks.
size_t const count = r.Size() / sizeof(ChunkT);
@ -106,7 +103,7 @@ int64_t ChunksDownloadStrategy::LoadOrInitChunks( string const & fName,
if (m_chunks[i].m_status != CHUNK_COMPLETE)
m_chunks[i].m_status = CHUNK_FREE;
else
downloadedSize += m_chunks[i+1].m_pos - m_chunks[i].m_pos;
downloadedSize += (m_chunks[i+1].m_pos - m_chunks[i].m_pos);
}
return downloadedSize;

View file

@ -54,13 +54,17 @@ private:
public:
ChunksDownloadStrategy(vector<string> const & urls);
/// Init chunks vector for fileSize.
void InitChunks(int64_t fileSize, int64_t chunkSize, ChunkStatusT status = CHUNK_FREE);
/// Used in unit tests only!
void AddChunk(RangeT const & range, ChunkStatusT status);
void SaveChunks(string const & fName);
/// @return Already downloaded size.
int64_t LoadOrInitChunks(string const & fName, int64_t fileSize, int64_t chunkSize);
/// Should be called for every completed chunk (no matter successful or not).
void ChunkFinished(bool success, RangeT const & range);
enum ResultT

View file

@ -182,7 +182,7 @@ class FileHttpRequest : public HttpRequest, public IHttpThreadCallback
// save information for download resume
++m_goodChunksCount;
if (m_status != ECompleted && m_goodChunksCount % 10 == 0)
m_strategy.SaveChunks(m_filePath);
m_strategy.SaveChunks(m_filePath + RESUME_FILE_EXTENSION);
}
if (m_status != EInProgress)
@ -195,12 +195,12 @@ class FileHttpRequest : public HttpRequest, public IHttpThreadCallback
my::DeleteFileX(m_filePath + RESUME_FILE_EXTENSION);
// Rename finished file to it's original name.
CHECK(my::RenameFileX((m_filePath + DOWNLOADING_FILE_EXTENSION).c_str(), m_filePath.c_str()), ());
CHECK(my::RenameFileX(m_filePath + DOWNLOADING_FILE_EXTENSION, m_filePath), ());
DisableBackupForFile(m_filePath.c_str());
DisableBackupForFile(m_filePath);
}
else // or save "chunks left" otherwise
m_strategy.SaveChunks(m_filePath);
m_strategy.SaveChunks(m_filePath + RESUME_FILE_EXTENSION);
m_onFinish(*this);
}
@ -216,7 +216,8 @@ public:
{
ASSERT ( !urls.empty(), () );
m_progress.first = m_strategy.LoadOrInitChunks(m_filePath, fileSize, chunkSize);
m_progress.first = m_strategy.LoadOrInitChunks(m_filePath + RESUME_FILE_EXTENSION,
fileSize, chunkSize);
m_progress.second = fileSize;
#ifdef OMIM_OS_IPHONE
@ -239,7 +240,7 @@ public:
if (m_doCleanProgressFiles)
{
CHECK(my::DeleteFileX(m_filePath + DOWNLOADING_FILE_EXTENSION), ());
my::DeleteFileX(m_filePath + DOWNLOADING_FILE_EXTENSION);
my::DeleteFileX(m_filePath + RESUME_FILE_EXTENSION);
}
}