From e2e711fba8b888ba3ce8b9e89c10399dba6d134b Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 31 Oct 2011 21:53:24 +0900 Subject: [PATCH] Reuse in-flight and pooled URIs when a download is paused. --- src/DownloadContext.cc | 1 + src/FileEntry.cc | 19 +++++++++++++++++++ src/FileEntry.h | 3 +++ test/FileEntryTest.cc | 21 +++++++++++++++++++++ 4 files changed, 44 insertions(+) diff --git a/src/DownloadContext.cc b/src/DownloadContext.cc index 3a47ed53..7f90627c 100644 --- a/src/DownloadContext.cc +++ b/src/DownloadContext.cc @@ -185,6 +185,7 @@ void DownloadContext::releaseRuntimeResource() { for(std::vector >::const_iterator i = fileEntries_.begin(), eoi = fileEntries_.end(); i != eoi; ++i) { + (*i)->putBackRequest(); (*i)->releaseRuntimeResource(); } } diff --git a/src/FileEntry.cc b/src/FileEntry.cc index 7d04e2dd..23721e7a 100644 --- a/src/FileEntry.cc +++ b/src/FileEntry.cc @@ -452,6 +452,25 @@ void FileEntry::releaseRuntimeResource() inFlightRequests_.clear(); } +namespace { +template +void putBackUri +(std::deque& uris, + InputIterator first, + InputIterator last) +{ + for(; first != last; ++first) { + uris.push_front((*first)->getUri()); + } +} +} // namespace + +void FileEntry::putBackRequest() +{ + putBackUri(uris_, requestPool_.begin(), requestPool_.end()); + putBackUri(uris_, inFlightRequests_.begin(), inFlightRequests_.end()); +} + namespace { template InputIterator findRequestByUri diff --git a/src/FileEntry.h b/src/FileEntry.h index 029bd3e1..c77ca47a 100644 --- a/src/FileEntry.h +++ b/src/FileEntry.h @@ -231,6 +231,9 @@ public: void releaseRuntimeResource(); + // Push URIs in pooled or in-flight requests to the front of uris_. + void putBackRequest(); + void setOriginalName(const std::string& originalName); const std::string& getOriginalName() const diff --git a/test/FileEntryTest.cc b/test/FileEntryTest.cc index bcfe819d..3510409d 100644 --- a/test/FileEntryTest.cc +++ b/test/FileEntryTest.cc @@ -20,6 +20,7 @@ class FileEntryTest : public CppUnit::TestFixture { CPPUNIT_TEST(testAddUris); CPPUNIT_TEST(testInsertUri); CPPUNIT_TEST(testRemoveUri); + CPPUNIT_TEST(testPutBackRequest); CPPUNIT_TEST_SUITE_END(); public: void setUp() {} @@ -34,6 +35,7 @@ public: void testAddUris(); void testInsertUri(); void testRemoveUri(); + void testPutBackRequest(); }; @@ -292,4 +294,23 @@ void FileEntryTest::testRemoveUri() CPPUNIT_ASSERT(!file.removeUri("http://example.net")); } +void FileEntryTest::testPutBackRequest() +{ + SharedHandle fileEntry = createFileEntry(); + SharedHandle selector(new InorderURISelector()); + std::vector > usedHosts; + SharedHandle req1 = + fileEntry->getRequest(selector, false, usedHosts); + SharedHandle req2 = + fileEntry->getRequest(selector, false, usedHosts); + CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntry->getRemainingUris().size()); + fileEntry->poolRequest(req2); + fileEntry->putBackRequest(); + const std::deque& uris = fileEntry->getRemainingUris(); + CPPUNIT_ASSERT_EQUAL((size_t)3, uris.size()); + CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/aria2.zip"), uris[0]); + CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/aria2.zip"), uris[1]); + CPPUNIT_ASSERT_EQUAL(std::string("ftp://localhost/aria2.zip"), uris[2]); +} + } // namespace aria2