diff --git a/ChangeLog b/ChangeLog index dcb61e52..fb432001 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2009-06-17 Tatsuhiro Tsujikawa + + Rewritten BtRegistry + * src/BtRegistry.cc + * src/BtRegistry.h + * src/BtSetup.cc + * src/ConsoleStatCalc.cc + * src/PeerInteractionCommand.cc + * src/PeerReceiveHandshakeCommand.cc + * src/ReceiverMSEHandshakeCommand.cc + * src/RequestGroup.cc + * src/XmlRpcMethodImpl.cc + * test/BtRegistryTest.cc + 2009-06-14 Tatsuhiro Tsujikawa Used array_ptr for savedInfoHash, savedBitfield and pieceBitfield. diff --git a/src/BtRegistry.cc b/src/BtRegistry.cc index 13583dcb..0c90a20c 100644 --- a/src/BtRegistry.cc +++ b/src/BtRegistry.cc @@ -43,103 +43,36 @@ namespace aria2 { -BtRegistry::BtRegistry() {} - -PeerStorageHandle BtRegistry::getPeerStorage(const std::string& key) +SharedHandle +BtRegistry::getBtContext(const std::string& infoHash) const { - return peerStorageMap.getHandle(key); + return get(infoHash)._btContext; } -void BtRegistry::registerPeerStorage(const std::string& key, - const PeerStorageHandle& peerStorage) +void BtRegistry::put(const std::string& infoHash, const BtObject& obj) { - peerStorageMap.registerHandle(key, peerStorage); -} - -PieceStorageHandle -BtRegistry::getPieceStorage(const std::string& key) -{ - return pieceStorageMap.getHandle(key); + remove(infoHash); + std::map::value_type p(infoHash, obj); + _pool.insert(p); } -void -BtRegistry::registerPieceStorage(const std::string& key, - const PieceStorageHandle& pieceStorage) +BtObject BtRegistry::get(const std::string& infoHash) const { - pieceStorageMap.registerHandle(key, pieceStorage); + std::map::const_iterator i = _pool.find(infoHash); + if(i == _pool.end()) { + return BtObject(); + } else { + return (*i).second; + } } -BtRuntimeHandle BtRegistry::getBtRuntime(const std::string& key) +bool BtRegistry::remove(const std::string& infoHash) { - return btRuntimeMap.getHandle(key); + return _pool.erase(infoHash); } -void -BtRegistry::registerBtRuntime(const std::string& key, - const BtRuntimeHandle& btRuntime) -{ - btRuntimeMap.registerHandle(key, btRuntime); -} - -BtAnnounceHandle BtRegistry::getBtAnnounce(const std::string& key) -{ - return btAnnounceMap.getHandle(key); -} - -void -BtRegistry::registerBtAnnounce(const std::string& key, - const BtAnnounceHandle& btAnnounce) -{ - btAnnounceMap.registerHandle(key, btAnnounce); -} - -BtProgressInfoFileHandle BtRegistry::getBtProgressInfoFile(const std::string& key) -{ - return btProgressInfoFileMap.getHandle(key); -} - -void -BtRegistry::registerBtProgressInfoFile(const std::string& key, - const BtProgressInfoFileHandle& btProgressInfoFile) -{ - btProgressInfoFileMap.registerHandle(key, btProgressInfoFile); -} - -BtContextHandle -BtRegistry::getBtContext(const std::string& key) -{ - return btContextMap.getHandle(key); -} - -void -BtRegistry::registerBtContext(const std::string& key, - const BtContextHandle& btContext) -{ - btContextMap.registerHandle(key, btContext); -} - -std::deque > BtRegistry::getAllBtContext() -{ - return btContextMap.getAll(); -} - -void BtRegistry::unregisterAll() { - btContextMap.clear(); - peerStorageMap.clear(); - pieceStorageMap.clear(); - btAnnounceMap.clear(); - btRuntimeMap.clear(); - btProgressInfoFileMap.clear(); -} - -void BtRegistry::unregister(const std::string& key) -{ - btContextMap.unregisterHandle(key); - peerStorageMap.unregisterHandle(key); - pieceStorageMap.unregisterHandle(key); - btAnnounceMap.unregisterHandle(key); - btRuntimeMap.unregisterHandle(key); - btProgressInfoFileMap.unregisterHandle(key); +void BtRegistry::removeAll() { + _pool.clear(); } } // namespace aria2 diff --git a/src/BtRegistry.h b/src/BtRegistry.h index c5f83089..36242469 100644 --- a/src/BtRegistry.h +++ b/src/BtRegistry.h @@ -39,9 +39,9 @@ #include #include +#include #include "SharedHandle.h" -#include "HandleRegistry.h" namespace aria2 { @@ -52,54 +52,62 @@ class BtRuntime; class BtProgressInfoFile; class BtContext; -typedef HandleRegistry PeerStorageMap; -typedef HandleRegistry PieceStorageMap; -typedef HandleRegistry BtAnnounceMap; -typedef HandleRegistry BtRuntimeMap; -typedef HandleRegistry BtProgressInfoFileMap; -typedef HandleRegistry BtContextMap; +struct BtObject { + SharedHandle _btContext; + SharedHandle _pieceStorage; + SharedHandle _peerStorage; + SharedHandle _btAnnounce; + SharedHandle _btRuntime; + SharedHandle _btProgressInfoFile; + + BtObject(const SharedHandle& btContext, + const SharedHandle& pieceStorage, + const SharedHandle& peerStorage, + const SharedHandle& btAnnounce, + const SharedHandle& btRuntime, + const SharedHandle& btProgressInfoFile): + _btContext(btContext), + _pieceStorage(pieceStorage), + _peerStorage(peerStorage), + _btAnnounce(btAnnounce), + _btRuntime(btRuntime), + _btProgressInfoFile(btProgressInfoFile) {} + + BtObject() {} + + bool isNull() const + { + return _btContext.isNull() && + _pieceStorage.isNull() && + _peerStorage.isNull() && + _btAnnounce.isNull() && + _btRuntime.isNull() && + _btProgressInfoFile.isNull(); + } +}; class BtRegistry { private: - BtContextMap btContextMap; - PeerStorageMap peerStorageMap; - PieceStorageMap pieceStorageMap; - BtAnnounceMap btAnnounceMap; - BtRuntimeMap btRuntimeMap; - BtProgressInfoFileMap btProgressInfoFileMap; + std::map _pool; public: - BtRegistry(); - - SharedHandle getBtContext(const std::string& key); - void registerBtContext(const std::string& key, - const SharedHandle& btContext); + SharedHandle getBtContext(const std::string& infoHash) const; - SharedHandle getPeerStorage(const std::string& key); - void registerPeerStorage(const std::string& key, - const SharedHandle& peer); - - SharedHandle getPieceStorage(const std::string& key); - void registerPieceStorage(const std::string& key, - const SharedHandle& pieceStorage); + void put(const std::string& infoHash, const BtObject& obj); - SharedHandle getBtRuntime(const std::string& key); - void registerBtRuntime(const std::string& key, - const SharedHandle& btRuntime); + BtObject get(const std::string& infoHash) const; - SharedHandle getBtAnnounce(const std::string& key); - void registerBtAnnounce(const std::string& key, - const SharedHandle& btAnnounce); + template + void getAllBtContext(OutputIterator dest) + { + for(std::map::const_iterator i = _pool.begin(); + i != _pool.end(); ++i) { + *dest++ = (*i).second._btContext; + } + } - SharedHandle - getBtProgressInfoFile(const std::string& key); - void registerBtProgressInfoFile(const std::string& key, - const SharedHandle& file); + void removeAll(); - std::deque > getAllBtContext(); - - void unregisterAll(); - - void unregister(const std::string& key); + bool remove(const std::string& infoHash); }; } // namespace aria2 diff --git a/src/BtSetup.cc b/src/BtSetup.cc index 033e9b6a..26472d98 100644 --- a/src/BtSetup.cc +++ b/src/BtSetup.cc @@ -72,16 +72,11 @@ void BtSetup::setup(std::deque& commands, if(btContext.isNull()) { return; } - SharedHandle btRegistry = e->getBtRegistry(); - SharedHandle pieceStorage = - btRegistry->getPieceStorage(btContext->getInfoHashAsString()); - SharedHandle peerStorage = - btRegistry->getPeerStorage(btContext->getInfoHashAsString()); - SharedHandle btRuntime = - btRegistry->getBtRuntime(btContext->getInfoHashAsString()); - SharedHandle btAnnounce = - btRegistry->getBtAnnounce(btContext->getInfoHashAsString()); - + BtObject btObject = e->getBtRegistry()->get(btContext->getInfoHashAsString()); + SharedHandle pieceStorage = btObject._pieceStorage; + SharedHandle peerStorage = btObject._peerStorage; + SharedHandle btRuntime = btObject._btRuntime; + SharedHandle btAnnounce = btObject._btAnnounce; // commands { TrackerWatcherCommand* c = diff --git a/src/ConsoleStatCalc.cc b/src/ConsoleStatCalc.cc index 25da156b..c6c94631 100644 --- a/src/ConsoleStatCalc.cc +++ b/src/ConsoleStatCalc.cc @@ -116,7 +116,7 @@ static void printProgress #ifdef ENABLE_BITTORRENT if(!btctx.isNull()) { SharedHandle ps = - e->getBtRegistry()->getPeerStorage(btctx->getInfoHashAsString()); + e->getBtRegistry()->get(btctx->getInfoHashAsString())._peerStorage; std::deque > peers; ps->getActivePeers(peers); o << " " << "SEED:" diff --git a/src/PeerInteractionCommand.cc b/src/PeerInteractionCommand.cc index d6f7c256..c3bbc1b9 100644 --- a/src/PeerInteractionCommand.cc +++ b/src/PeerInteractionCommand.cc @@ -100,7 +100,7 @@ PeerInteractionCommand::PeerInteractionCommand SharedHandle btRegistry = e->getBtRegistry(); SharedHandle peerStorage = - btRegistry->getPeerStorage(_btContext->getInfoHashAsString()); + btRegistry->get(_btContext->getInfoHashAsString())._peerStorage; SharedHandle exMsgRegistry (new ExtensionMessageRegistry()); diff --git a/src/PeerReceiveHandshakeCommand.cc b/src/PeerReceiveHandshakeCommand.cc index 47382520..20df4391 100644 --- a/src/PeerReceiveHandshakeCommand.cc +++ b/src/PeerReceiveHandshakeCommand.cc @@ -93,13 +93,11 @@ bool PeerReceiveHandshakeCommand::executeInternal() // check info_hash std::string infoHash = Util::toHex(&data[28], INFO_HASH_LENGTH); - SharedHandle btRegistry = e->getBtRegistry(); - SharedHandle btContext = btRegistry->getBtContext(infoHash); - SharedHandle btRuntime = btRegistry->getBtRuntime(infoHash); - SharedHandle pieceStorage = - btRegistry->getPieceStorage(infoHash); - SharedHandle peerStorage = - btRegistry->getPeerStorage(infoHash); + BtObject btObject = e->getBtRegistry()->get(infoHash); + SharedHandle btContext = btObject._btContext; + SharedHandle btRuntime = btObject._btRuntime; + SharedHandle pieceStorage = btObject._pieceStorage; + SharedHandle peerStorage = btObject._peerStorage; if(btContext.isNull() || !btRuntime->ready()) { throw DL_ABORT_EX diff --git a/src/ReceiverMSEHandshakeCommand.cc b/src/ReceiverMSEHandshakeCommand.cc index 72ee26de..defe2a22 100644 --- a/src/ReceiverMSEHandshakeCommand.cc +++ b/src/ReceiverMSEHandshakeCommand.cc @@ -135,8 +135,9 @@ bool ReceiverMSEHandshakeCommand::executeInternal() break; } case RECEIVER_RECEIVE_PAD_C_LENGTH: { - if(_mseHandshake->receiveReceiverHashAndPadCLength - (e->getBtRegistry()->getAllBtContext())) { + std::deque > btContexts; + e->getBtRegistry()->getAllBtContext(std::back_inserter(btContexts)); + if(_mseHandshake->receiveReceiverHashAndPadCLength(btContexts)) { _sequence = RECEIVER_RECEIVE_PAD_C; } break; diff --git a/src/RequestGroup.cc b/src/RequestGroup.cc index 7a40e6df..99fca57e 100644 --- a/src/RequestGroup.cc +++ b/src/RequestGroup.cc @@ -243,20 +243,10 @@ void RequestGroup::createInitialCommand(std::deque& commands, progressInfoFile(new DefaultBtProgressInfoFile(_downloadContext, _pieceStorage, _option.get())); - - btRegistry->registerBtContext(btContext->getInfoHashAsString(), - btContext); - btRegistry->registerPieceStorage(btContext->getInfoHashAsString(), - _pieceStorage); - btRegistry->registerBtProgressInfoFile(btContext->getInfoHashAsString(), - progressInfoFile); - - + BtRuntimeHandle btRuntime(new BtRuntime()); btRuntime->setListenPort(_option->getAsInt(PREF_LISTEN_PORT)); btRuntime->setMaxPeers(_option->getAsInt(PREF_BT_MAX_PEERS)); - btRegistry->registerBtRuntime(btContext->getInfoHashAsString(), - btRuntime); _btRuntime = btRuntime; progressInfoFile->setBtRuntime(btRuntime); @@ -264,8 +254,6 @@ void RequestGroup::createInitialCommand(std::deque& commands, (new DefaultPeerStorage(btContext, _option.get())); peerStorage->setBtRuntime(btRuntime); peerStorage->setPieceStorage(_pieceStorage); - btRegistry->registerPeerStorage(btContext->getInfoHashAsString(), - peerStorage); _peerStorage = peerStorage; progressInfoFile->setPeerStorage(peerStorage); @@ -276,10 +264,16 @@ void RequestGroup::createInitialCommand(std::deque& commands, btAnnounce->setPeerStorage(peerStorage); btAnnounce->setUserDefinedInterval (_option->getAsInt(PREF_BT_TRACKER_INTERVAL)); - btRegistry->registerBtAnnounce(btContext->getInfoHashAsString(), - btAnnounce); btAnnounce->shuffleAnnounce(); + btRegistry->put(btContext->getInfoHashAsString(), + BtObject(btContext, + _pieceStorage, + peerStorage, + btAnnounce, + btRuntime, + progressInfoFile)); + // Remove the control file if download file doesn't exist if(progressInfoFile->exists() && !_pieceStorage->getDiskAdaptor()->fileExists()) { progressInfoFile->removeFile(); @@ -828,7 +822,7 @@ void RequestGroup::releaseRuntimeResource(DownloadEngine* e) if(!btContextInReg.isNull() && btContextInReg->getOwnerRequestGroup()->getGID() == btContext->getOwnerRequestGroup()->getGID()) { - btRegistry->unregister(btContext->getInfoHashAsString()); + btRegistry->remove(btContext->getInfoHashAsString()); if(!DHTRegistry::_peerAnnounceStorage.isNull()) { DHTRegistry::_peerAnnounceStorage-> removeLocalPeerAnnounce(btContext->getInfoHash()); diff --git a/src/XmlRpcMethodImpl.cc b/src/XmlRpcMethodImpl.cc index a29c2448..176f18c0 100644 --- a/src/XmlRpcMethodImpl.cc +++ b/src/XmlRpcMethodImpl.cc @@ -292,7 +292,7 @@ static void gatherProgressBitTorrent entryDict["infoHash"] = btctx->getInfoHashAsString(); SharedHandle peerStorage = - btreg->getPeerStorage(btctx->getInfoHashAsString()); + btreg->get(btctx->getInfoHashAsString())._peerStorage; assert(!peerStorage.isNull()); std::deque > peers; @@ -460,7 +460,7 @@ BDE GetPeersXmlRpcMethod::process if(!btctx.isNull()) { SharedHandle btreg = e->getBtRegistry(); SharedHandle peerStorage = - btreg->getPeerStorage(btctx->getInfoHashAsString()); + btreg->get(btctx->getInfoHashAsString())._peerStorage; assert(!peerStorage.isNull()); BDE entry = BDE::dict(); gatherPeer(peers, peerStorage); diff --git a/test/BtRegistryTest.cc b/test/BtRegistryTest.cc index 6d8fc4d0..61b63d40 100644 --- a/test/BtRegistryTest.cc +++ b/test/BtRegistryTest.cc @@ -17,21 +17,17 @@ class BtRegistryTest:public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(BtRegistryTest); CPPUNIT_TEST(testGetBtContext); - CPPUNIT_TEST(testGetPeerStorage); - CPPUNIT_TEST(testGetPieceStorage); - CPPUNIT_TEST(testGetBtRuntime); - CPPUNIT_TEST(testGetBtAnnounce); - CPPUNIT_TEST(testGetBtProgressInfoFile); + CPPUNIT_TEST(testGetAllBtContext); + CPPUNIT_TEST(testRemove); + CPPUNIT_TEST(testRemoveAll); CPPUNIT_TEST_SUITE_END(); private: public: void testGetBtContext(); - void testGetPeerStorage(); - void testGetPieceStorage(); - void testGetBtRuntime(); - void testGetBtAnnounce(); - void testGetBtProgressInfoFile(); + void testGetAllBtContext(); + void testRemove(); + void testRemoveAll(); }; @@ -42,64 +38,51 @@ void BtRegistryTest::testGetBtContext() BtRegistry btRegistry; CPPUNIT_ASSERT(btRegistry.getBtContext("test").isNull()); SharedHandle btContext(new MockBtContext()); - btRegistry.registerBtContext("test", btContext); + BtObject btObject; + btObject._btContext = btContext; + btRegistry.put("test", btObject); CPPUNIT_ASSERT_EQUAL(btContext.get(), btRegistry.getBtContext("test").get()); } -void BtRegistryTest::testGetPeerStorage() { - BtRegistry btRegistry; - CPPUNIT_ASSERT(!btRegistry.getPeerStorage("test").get()); - - SharedHandle peerStorage(new MockPeerStorage()); - - btRegistry.registerPeerStorage("test", peerStorage); - CPPUNIT_ASSERT_EQUAL(peerStorage.get(), - btRegistry.getPeerStorage("test").get()); +static void addTwoBtContext(BtRegistry& btRegistry) +{ + SharedHandle btContext1(new MockBtContext()); + SharedHandle btContext2(new MockBtContext()); + BtObject btObject1; + btObject1._btContext = btContext1; + BtObject btObject2; + btObject2._btContext = btContext2; + btRegistry.put("ctx1", btObject1); + btRegistry.put("ctx2", btObject2); } -void BtRegistryTest::testGetPieceStorage() { +void BtRegistryTest::testGetAllBtContext() +{ BtRegistry btRegistry; - CPPUNIT_ASSERT(!btRegistry.getPieceStorage("test").get()); + addTwoBtContext(btRegistry); - SharedHandle pieceStorage(new MockPieceStorage()); - - btRegistry.registerPieceStorage("test", pieceStorage); - CPPUNIT_ASSERT_EQUAL(pieceStorage.get(), - btRegistry.getPieceStorage("test").get()); + std::vector > result; + btRegistry.getAllBtContext(std::back_inserter(result)); + CPPUNIT_ASSERT_EQUAL((size_t)2, result.size()); } -void BtRegistryTest::testGetBtRuntime() { +void BtRegistryTest::testRemove() +{ BtRegistry btRegistry; - CPPUNIT_ASSERT(!btRegistry.getBtRuntime("test").get()); - - SharedHandle runtime; - - btRegistry.registerBtRuntime("test", runtime); - CPPUNIT_ASSERT_EQUAL(runtime.get(), - btRegistry.getBtRuntime("test").get()); + addTwoBtContext(btRegistry); + CPPUNIT_ASSERT(btRegistry.remove("ctx1")); + CPPUNIT_ASSERT(btRegistry.get("ctx1").isNull()); + CPPUNIT_ASSERT(!btRegistry.get("ctx2").isNull()); } -void BtRegistryTest::testGetBtAnnounce() { +void BtRegistryTest::testRemoveAll() +{ BtRegistry btRegistry; - CPPUNIT_ASSERT(!btRegistry.getBtAnnounce("test").get()); - - SharedHandle btAnnounce(new MockBtAnnounce()); - - btRegistry.registerBtAnnounce("test", btAnnounce); - CPPUNIT_ASSERT_EQUAL(btAnnounce.get(), - btRegistry.getBtAnnounce("test").get()); -} - -void BtRegistryTest::testGetBtProgressInfoFile() { - BtRegistry btRegistry; - CPPUNIT_ASSERT(!btRegistry.getBtProgressInfoFile("test").get()); - - SharedHandle btProgressInfoFile(new MockBtProgressInfoFile()); - - btRegistry.registerBtProgressInfoFile("test", btProgressInfoFile); - CPPUNIT_ASSERT_EQUAL(btProgressInfoFile.get(), - btRegistry.getBtProgressInfoFile("test").get()); + addTwoBtContext(btRegistry); + btRegistry.removeAll(); + CPPUNIT_ASSERT(btRegistry.get("ctx1").isNull()); + CPPUNIT_ASSERT(btRegistry.get("ctx2").isNull()); } } // namespace aria2