diff --git a/ChangeLog b/ChangeLog index ef450c03..94ab0146 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2008-12-14 Tatsuhiro Tsujikawa + + Use BDE instead of Dictionary/List/Data. + * src/AnnounceList.cc + * src/AnnounceList.h + * test/AnnounceListTest.cc + 2008-12-14 Tatsuhiro Tsujikawa Use BDE instead of Dictionary/List/Data. diff --git a/src/AnnounceList.cc b/src/AnnounceList.cc index ffdba076..0896bdae 100644 --- a/src/AnnounceList.cc +++ b/src/AnnounceList.cc @@ -36,10 +36,9 @@ #include -#include "List.h" -#include "Data.h" #include "A2STR.h" #include "SimpleRandomizer.h" +#include "bencode.h" namespace aria2 { @@ -49,9 +48,9 @@ const std::string AnnounceList::STOPPED("stopped"); const std::string AnnounceList::COMPLETED("completed"); -AnnounceList::AnnounceList(const MetaEntry* announceListEntry): +AnnounceList::AnnounceList(const bencode::BDE& announceList): currentTrackerInitialized(false) { - reconfigure(announceListEntry); + reconfigure(announceList); } AnnounceList::AnnounceList(const AnnounceTiers& announceTiers): @@ -59,24 +58,24 @@ AnnounceList::AnnounceList(const AnnounceTiers& announceTiers): resetIterator(); } -void AnnounceList::reconfigure(const MetaEntry* announceListEntry) { - const List* l = dynamic_cast(announceListEntry); - if(l) { - for(std::deque::const_iterator itr = l->getList().begin(); - itr != l->getList().end(); itr++) { - const List* elem = dynamic_cast(*itr); - if(!elem) { +void AnnounceList::reconfigure(const bencode::BDE& announceList) +{ + if(announceList.isList()) { + for(bencode::BDE::List::const_iterator itr = announceList.listBegin(); + itr != announceList.listEnd(); ++itr) { + const bencode::BDE& elemList = *itr; + if(!elemList.isList()) { continue; } std::deque urls; - for(std::deque::const_iterator elemItr = elem->getList().begin(); - elemItr != elem->getList().end(); elemItr++) { - const Data* data = dynamic_cast(*elemItr); - if(data) { - urls.push_back(data->toString()); + for(bencode::BDE::List::const_iterator elemItr = elemList.listBegin(); + elemItr != elemList.listEnd(); ++elemItr) { + const bencode::BDE& data = *elemItr; + if(data.isString()) { + urls.push_back(data.s()); } } - if(urls.size()) { + if(!urls.empty()) { AnnounceTierHandle tier(new AnnounceTier(urls)); tiers.push_back(tier); } diff --git a/src/AnnounceList.h b/src/AnnounceList.h index 178447e4..4c7dc5ce 100644 --- a/src/AnnounceList.h +++ b/src/AnnounceList.h @@ -41,7 +41,9 @@ namespace aria2 { -class MetaEntry; +namespace bencode { +class BDE; +} // namespace bencode class AnnounceList { public: @@ -55,10 +57,10 @@ private: void setCurrentTier(const std::deque >::iterator& itr); public: AnnounceList():currentTrackerInitialized(false) {} - AnnounceList(const MetaEntry* announceListEntry); + AnnounceList(const bencode::BDE& announceList); AnnounceList(const std::deque >& tiers); - void reconfigure(const MetaEntry* announceListEntry); + void reconfigure(const bencode::BDE& announceList); void reconfigure(const std::string& url); size_t countTier() const { diff --git a/test/AnnounceListTest.cc b/test/AnnounceListTest.cc index 56d0e789..f5c98ed4 100644 --- a/test/AnnounceListTest.cc +++ b/test/AnnounceListTest.cc @@ -1,9 +1,10 @@ #include "AnnounceList.h" -#include "MetaFileUtil.h" -#include "Exception.h" -#include "List.h" + #include +#include "Exception.h" +#include "bencode.h" + namespace aria2 { class AnnounceListTest:public CppUnit::TestFixture { @@ -43,12 +44,11 @@ CPPUNIT_TEST_SUITE_REGISTRATION( AnnounceListTest ); void AnnounceListTest::testSingleElementList() { std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee"; - SharedHandle announces - (dynamic_cast(MetaFileUtil::bdecoding(peersString))); + const bencode::BDE announcesList = bencode::decode(peersString); // ANNOUNCE_LIST // [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ] - AnnounceList announceList(announces.get()); + AnnounceList announceList(announcesList); CPPUNIT_ASSERT(!announceList.allTiersFailed()); std::string url = announceList.getAnnounce(); @@ -90,12 +90,11 @@ void AnnounceListTest::testSingleElementList() { void AnnounceListTest::testMultiElementList() { std::string peersString = "ll8:tracker18:tracker28:tracker3ee"; - SharedHandle announces - (dynamic_cast(MetaFileUtil::bdecoding(peersString))); + const bencode::BDE announcesList = bencode::decode(peersString); // ANNOUNCE_LIST // [ [ tracker1, tracker2, tracker3 ] ] - AnnounceList announceList(announces.get()); + AnnounceList announceList(announcesList); CPPUNIT_ASSERT(!announceList.allTiersFailed()); std::string url = announceList.getAnnounce(); @@ -124,12 +123,11 @@ void AnnounceListTest::testMultiElementList() { void AnnounceListTest::testSingleAndMulti() { std::string peersString = "ll8:tracker18:tracker2el8:tracker3ee"; - SharedHandle announces - (dynamic_cast(MetaFileUtil::bdecoding(peersString))); + const bencode::BDE announcesList = bencode::decode(peersString); // ANNOUNCE_LIST // [ [ tracker1, tracker2 ], [ tracker3 ] ] - AnnounceList announceList(announces.get()); + AnnounceList announceList(announcesList); std::string url = announceList.getAnnounce(); CPPUNIT_ASSERT_EQUAL(std::string("tracker1"), url); @@ -151,22 +149,20 @@ void AnnounceListTest::testSingleAndMulti() { void AnnounceListTest::testNoGroup() { std::string peersString = "llee"; - SharedHandle announces - (dynamic_cast(MetaFileUtil::bdecoding(peersString))); + const bencode::BDE announcesList = bencode::decode(peersString); - AnnounceList announceList(announces.get()); + AnnounceList announceList(announcesList); CPPUNIT_ASSERT(announceList.countTier() == 0); } void AnnounceListTest::testNextEventIfAfterStarted() { std::string peersString = "ll8:tracker1ee"; - SharedHandle announces - (dynamic_cast(MetaFileUtil::bdecoding(peersString))); + const bencode::BDE announcesList = bencode::decode(peersString); // ANNOUNCE_LIST // [ [ tracker1 ] ] - AnnounceList announceList(announces.get()); + AnnounceList announceList(announcesList); announceList.setEvent(AnnounceTier::STOPPED); announceList.announceFailure(); announceList.resetTier(); @@ -182,12 +178,11 @@ void AnnounceListTest::testNextEventIfAfterStarted() { void AnnounceListTest::testEvent() { std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee"; - SharedHandle announces - (dynamic_cast(MetaFileUtil::bdecoding(peersString))); + const bencode::BDE announcesList = bencode::decode(peersString); // ANNOUNCE_LIST // [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ] - AnnounceList announceList(announces.get()); + AnnounceList announceList(announcesList); announceList.setEvent(AnnounceTier::STOPPED); announceList.announceSuccess(); @@ -207,12 +202,11 @@ void AnnounceListTest::testEvent() { void AnnounceListTest::testCountStoppedAllowedTier() { std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee"; - SharedHandle announces - (dynamic_cast(MetaFileUtil::bdecoding(peersString))); + const bencode::BDE announcesList = bencode::decode(peersString); // ANNOUNCE_LIST // [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ] - AnnounceList announceList(announces.get()); + AnnounceList announceList(announcesList); CPPUNIT_ASSERT_EQUAL((size_t)0, announceList.countStoppedAllowedTier()); announceList.setEvent(AnnounceTier::STARTED); @@ -235,12 +229,11 @@ void AnnounceListTest::testCountStoppedAllowedTier() { void AnnounceListTest::testCountCompletedAllowedTier() { std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee"; - SharedHandle announces - (dynamic_cast(MetaFileUtil::bdecoding(peersString))); + const bencode::BDE announcesList = bencode::decode(peersString); // ANNOUNCE_LIST // [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ] - AnnounceList announceList(announces.get()); + AnnounceList announceList(announcesList); CPPUNIT_ASSERT_EQUAL((size_t)0, announceList.countCompletedAllowedTier()); announceList.setEvent(AnnounceTier::STARTED);