From 9bb57720cf66992813bc5060c291d950e5590500 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 8 Feb 2009 14:17:55 +0000 Subject: [PATCH] 2009-02-08 Tatsuhiro Tsujikawa Rewritten AdaptiveURISelector::mayRetryWithIncreasedTimeout() using RequestGroup::extractURIResult(). * src/AdaptiveURISelector.cc * src/RequestGroup.cc * src/RequestGroup.h * test/RequestGroupTest.cc --- ChangeLog | 9 +++++++++ src/AdaptiveURISelector.cc | 21 ++++++++++----------- src/RequestGroup.cc | 22 ++++++++++++++++++++++ src/RequestGroup.h | 4 ++++ test/RequestGroupTest.cc | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 47d660ce..78c0a686 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2009-02-08 Tatsuhiro Tsujikawa + + Rewritten AdaptiveURISelector::mayRetryWithIncreasedTimeout() + using RequestGroup::extractURIResult(). + * src/AdaptiveURISelector.cc + * src/RequestGroup.cc + * src/RequestGroup.h + * test/RequestGroupTest.cc + 2009-02-08 Tatsuhiro Tsujikawa Added tests for array_ptr. diff --git a/src/AdaptiveURISelector.cc b/src/AdaptiveURISelector.cc index cb20aad3..c4ff5b04 100644 --- a/src/AdaptiveURISelector.cc +++ b/src/AdaptiveURISelector.cc @@ -99,18 +99,17 @@ void AdaptiveURISelector::mayRetryWithIncreasedTimeout _requestGroup->setTimeout(_requestGroup->getTimeout()*2); // looking for retries - const std::deque& uriResults = _requestGroup->getURIResults(); - for (std::deque::const_iterator i = uriResults.begin(); - i != uriResults.end(); ++i) { - if ((*i).getResult() == DownloadResult::TIME_OUT) { - _logger->debug("AdaptiveURISelector: will retry server with increased" - " timeout (%d s): %s", - _requestGroup->getTimeout(), (*i).getURI().c_str()); - uris.push_back((*i).getURI()); - } + std::deque timeouts; + _requestGroup->extractURIResult(timeouts, DownloadResult::TIME_OUT); + std::transform(timeouts.begin(), timeouts.end(), std::back_inserter(uris), + std::mem_fun_ref(&URIResult::getURI)); + + for(std::deque::const_iterator i = uris.begin(); i != uris.end(); + ++i) { + _logger->debug("AdaptiveURISelector: will retry server with increased" + " timeout (%d s): %s", + _requestGroup->getTimeout(), (*i).c_str()); } - std::sort(uris.begin(), uris.end()); - uris.erase(std::unique(uris.begin(), uris.end()), uris.end()); } std::string AdaptiveURISelector::selectOne(const std::deque& uris) diff --git a/src/RequestGroup.cc b/src/RequestGroup.cc index 493e20d3..c0252032 100644 --- a/src/RequestGroup.cc +++ b/src/RequestGroup.cc @@ -1135,6 +1135,28 @@ const std::deque& RequestGroup::getURIResults() const return _uriResults; } +class FindURIResultByResult { +private: + DownloadResult::RESULT _r; +public: + FindURIResultByResult(DownloadResult::RESULT r):_r(r) {} + + bool operator()(const URIResult& uriResult) const + { + return uriResult.getResult() == _r; + } +}; + +void RequestGroup::extractURIResult +(std::deque& res, DownloadResult::RESULT r) +{ + std::deque::iterator i = + std::stable_partition(_uriResults.begin(), _uriResults.end(), + FindURIResultByResult(r)); + std::copy(_uriResults.begin(), i, std::back_inserter(res)); + _uriResults.erase(_uriResults.begin(), i); +} + void RequestGroup::setTimeout(time_t timeout) { _timeout = timeout; diff --git a/src/RequestGroup.h b/src/RequestGroup.h index 335bc37b..3c74367d 100644 --- a/src/RequestGroup.h +++ b/src/RequestGroup.h @@ -333,6 +333,10 @@ public: const std::deque& getURIResults() const; + // Extracts URIResult whose _result is r and stores them into res. + // The extracted URIResults are removed from _uriResults. + void extractURIResult(std::deque& res, DownloadResult::RESULT r); + void dependsOn(const SharedHandle& dep); bool isDependencyResolved(); diff --git a/test/RequestGroupTest.cc b/test/RequestGroupTest.cc index 1710c12b..c01323d7 100644 --- a/test/RequestGroupTest.cc +++ b/test/RequestGroupTest.cc @@ -1,5 +1,6 @@ #include "RequestGroup.h" +#include #include #include "ServerHost.h" @@ -17,6 +18,7 @@ class RequestGroupTest : public CppUnit::TestFixture { CPPUNIT_TEST(testRemoveURIWhoseHostnameIs); CPPUNIT_TEST(testGetFilePath); CPPUNIT_TEST(testCreateDownloadResult); + CPPUNIT_TEST(testExtractURIResult); CPPUNIT_TEST_SUITE_END(); private: @@ -27,6 +29,7 @@ public: void testRemoveURIWhoseHostnameIs(); void testGetFilePath(); void testCreateDownloadResult(); + void testExtractURIResult(); }; @@ -139,4 +142,33 @@ void RequestGroupTest::testCreateDownloadResult() } } +void RequestGroupTest::testExtractURIResult() +{ + Option op; + RequestGroup group(&op, std::deque()); + group.addURIResult("http://timeout/file", DownloadResult::TIME_OUT); + group.addURIResult("http://finished/file", DownloadResult::FINISHED); + group.addURIResult("http://timeout/file2", DownloadResult::TIME_OUT); + group.addURIResult("http://unknownerror/file", DownloadResult::UNKNOWN_ERROR); + + std::deque res; + group.extractURIResult(res, DownloadResult::TIME_OUT); + CPPUNIT_ASSERT_EQUAL((size_t)2, res.size()); + CPPUNIT_ASSERT_EQUAL(std::string("http://timeout/file"), res[0].getURI()); + CPPUNIT_ASSERT_EQUAL(std::string("http://timeout/file2"), res[1].getURI()); + + CPPUNIT_ASSERT_EQUAL((size_t)2, group.getURIResults().size()); + CPPUNIT_ASSERT_EQUAL(std::string("http://finished/file"), + group.getURIResults()[0].getURI()); + CPPUNIT_ASSERT_EQUAL(std::string("http://unknownerror/file"), + group.getURIResults()[1].getURI()); + + res.clear(); + + group.extractURIResult(res, DownloadResult::TIME_OUT); + CPPUNIT_ASSERT(res.empty()); + CPPUNIT_ASSERT_EQUAL((size_t)2, group.getURIResults().size()); +} + + } // namespace aria2