diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2009-06-17 11:51:44 +0000 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2009-06-17 11:51:44 +0000 |
| commit | eaf5217de78792ea1e49fd489e48488d6d297317 (patch) | |
| tree | d77c839e5e4759865de3affc1438f8a323877c02 /src | |
| parent | 5e835ae885fa1d2ade805ee58b99e3abf72ba125 (diff) | |
2009-06-17 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
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
Diffstat (limited to 'src')
| -rw-r--r-- | src/BtRegistry.cc | 103 | ||||
| -rw-r--r-- | src/BtRegistry.h | 98 | ||||
| -rw-r--r-- | src/BtSetup.cc | 15 | ||||
| -rw-r--r-- | src/ConsoleStatCalc.cc | 2 | ||||
| -rw-r--r-- | src/PeerInteractionCommand.cc | 2 | ||||
| -rw-r--r-- | src/PeerReceiveHandshakeCommand.cc | 12 | ||||
| -rw-r--r-- | src/ReceiverMSEHandshakeCommand.cc | 5 | ||||
| -rw-r--r-- | src/RequestGroup.cc | 26 | ||||
| -rw-r--r-- | src/XmlRpcMethodImpl.cc | 4 |
9 files changed, 98 insertions, 169 deletions
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) -{ - return peerStorageMap.getHandle(key); -} - -void BtRegistry::registerPeerStorage(const std::string& key, - const PeerStorageHandle& peerStorage) -{ - peerStorageMap.registerHandle(key, peerStorage); -} - -PieceStorageHandle -BtRegistry::getPieceStorage(const std::string& key) -{ - return pieceStorageMap.getHandle(key); -} - -void -BtRegistry::registerPieceStorage(const std::string& key, - const PieceStorageHandle& pieceStorage) -{ - pieceStorageMap.registerHandle(key, pieceStorage); -} - -BtRuntimeHandle BtRegistry::getBtRuntime(const std::string& key) -{ - return btRuntimeMap.getHandle(key); -} - -void -BtRegistry::registerBtRuntime(const std::string& key, - const BtRuntimeHandle& btRuntime) +SharedHandle<BtContext> +BtRegistry::getBtContext(const std::string& infoHash) const { - btRuntimeMap.registerHandle(key, btRuntime); + return get(infoHash)._btContext; } -BtAnnounceHandle BtRegistry::getBtAnnounce(const std::string& key) +void BtRegistry::put(const std::string& infoHash, const BtObject& obj) { - return btAnnounceMap.getHandle(key); + remove(infoHash); + std::map<std::string, BtObject>::value_type p(infoHash, obj); + _pool.insert(p); } -void -BtRegistry::registerBtAnnounce(const std::string& key, - const BtAnnounceHandle& btAnnounce) +BtObject BtRegistry::get(const std::string& infoHash) const { - btAnnounceMap.registerHandle(key, btAnnounce); + std::map<std::string, BtObject>::const_iterator i = _pool.find(infoHash); + if(i == _pool.end()) { + return BtObject(); + } else { + return (*i).second; + } } -BtProgressInfoFileHandle BtRegistry::getBtProgressInfoFile(const std::string& key) +bool BtRegistry::remove(const std::string& infoHash) { - return btProgressInfoFileMap.getHandle(key); + return _pool.erase(infoHash); } -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<SharedHandle<BtContext> > 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 <string> #include <map> +#include <vector> #include "SharedHandle.h" -#include "HandleRegistry.h" namespace aria2 { @@ -52,54 +52,62 @@ class BtRuntime; class BtProgressInfoFile; class BtContext; -typedef HandleRegistry<std::string, PeerStorage> PeerStorageMap; -typedef HandleRegistry<std::string, PieceStorage> PieceStorageMap; -typedef HandleRegistry<std::string, BtAnnounce> BtAnnounceMap; -typedef HandleRegistry<std::string, BtRuntime> BtRuntimeMap; -typedef HandleRegistry<std::string, BtProgressInfoFile> BtProgressInfoFileMap; -typedef HandleRegistry<std::string, BtContext> BtContextMap; +struct BtObject { + SharedHandle<BtContext> _btContext; + SharedHandle<PieceStorage> _pieceStorage; + SharedHandle<PeerStorage> _peerStorage; + SharedHandle<BtAnnounce> _btAnnounce; + SharedHandle<BtRuntime> _btRuntime; + SharedHandle<BtProgressInfoFile> _btProgressInfoFile; + + BtObject(const SharedHandle<BtContext>& btContext, + const SharedHandle<PieceStorage>& pieceStorage, + const SharedHandle<PeerStorage>& peerStorage, + const SharedHandle<BtAnnounce>& btAnnounce, + const SharedHandle<BtRuntime>& btRuntime, + const SharedHandle<BtProgressInfoFile>& 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<std::string, BtObject> _pool; public: - BtRegistry(); - - SharedHandle<BtContext> getBtContext(const std::string& key); - void registerBtContext(const std::string& key, - const SharedHandle<BtContext>& btContext); - - SharedHandle<PeerStorage> getPeerStorage(const std::string& key); - void registerPeerStorage(const std::string& key, - const SharedHandle<PeerStorage>& peer); - - SharedHandle<PieceStorage> getPieceStorage(const std::string& key); - void registerPieceStorage(const std::string& key, - const SharedHandle<PieceStorage>& pieceStorage); - - SharedHandle<BtRuntime> getBtRuntime(const std::string& key); - void registerBtRuntime(const std::string& key, - const SharedHandle<BtRuntime>& btRuntime); - - SharedHandle<BtAnnounce> getBtAnnounce(const std::string& key); - void registerBtAnnounce(const std::string& key, - const SharedHandle<BtAnnounce>& btAnnounce); - - SharedHandle<BtProgressInfoFile> - getBtProgressInfoFile(const std::string& key); - void registerBtProgressInfoFile(const std::string& key, - const SharedHandle<BtProgressInfoFile>& file); - - std::deque<SharedHandle<BtContext> > getAllBtContext(); - - void unregisterAll(); - - void unregister(const std::string& key); + SharedHandle<BtContext> getBtContext(const std::string& infoHash) const; + + void put(const std::string& infoHash, const BtObject& obj); + + BtObject get(const std::string& infoHash) const; + + template<typename OutputIterator> + void getAllBtContext(OutputIterator dest) + { + for(std::map<std::string, BtObject>::const_iterator i = _pool.begin(); + i != _pool.end(); ++i) { + *dest++ = (*i).second._btContext; + } + } + + void removeAll(); + + 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<Command*>& commands, if(btContext.isNull()) { return; } - SharedHandle<BtRegistry> btRegistry = e->getBtRegistry(); - SharedHandle<PieceStorage> pieceStorage = - btRegistry->getPieceStorage(btContext->getInfoHashAsString()); - SharedHandle<PeerStorage> peerStorage = - btRegistry->getPeerStorage(btContext->getInfoHashAsString()); - SharedHandle<BtRuntime> btRuntime = - btRegistry->getBtRuntime(btContext->getInfoHashAsString()); - SharedHandle<BtAnnounce> btAnnounce = - btRegistry->getBtAnnounce(btContext->getInfoHashAsString()); - + BtObject btObject = e->getBtRegistry()->get(btContext->getInfoHashAsString()); + SharedHandle<PieceStorage> pieceStorage = btObject._pieceStorage; + SharedHandle<PeerStorage> peerStorage = btObject._peerStorage; + SharedHandle<BtRuntime> btRuntime = btObject._btRuntime; + SharedHandle<BtAnnounce> 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<PeerStorage> ps = - e->getBtRegistry()->getPeerStorage(btctx->getInfoHashAsString()); + e->getBtRegistry()->get(btctx->getInfoHashAsString())._peerStorage; std::deque<SharedHandle<Peer> > 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> btRegistry = e->getBtRegistry(); SharedHandle<PeerStorage> peerStorage = - btRegistry->getPeerStorage(_btContext->getInfoHashAsString()); + btRegistry->get(_btContext->getInfoHashAsString())._peerStorage; SharedHandle<ExtensionMessageRegistry> 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> btRegistry = e->getBtRegistry(); - SharedHandle<BtContext> btContext = btRegistry->getBtContext(infoHash); - SharedHandle<BtRuntime> btRuntime = btRegistry->getBtRuntime(infoHash); - SharedHandle<PieceStorage> pieceStorage = - btRegistry->getPieceStorage(infoHash); - SharedHandle<PeerStorage> peerStorage = - btRegistry->getPeerStorage(infoHash); + BtObject btObject = e->getBtRegistry()->get(infoHash); + SharedHandle<BtContext> btContext = btObject._btContext; + SharedHandle<BtRuntime> btRuntime = btObject._btRuntime; + SharedHandle<PieceStorage> pieceStorage = btObject._pieceStorage; + SharedHandle<PeerStorage> 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<SharedHandle<BtContext> > 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<Command*>& 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<Command*>& 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<Command*>& 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> peerStorage = - btreg->getPeerStorage(btctx->getInfoHashAsString()); + btreg->get(btctx->getInfoHashAsString())._peerStorage; assert(!peerStorage.isNull()); std::deque<SharedHandle<Peer> > peers; @@ -460,7 +460,7 @@ BDE GetPeersXmlRpcMethod::process if(!btctx.isNull()) { SharedHandle<BtRegistry> btreg = e->getBtRegistry(); SharedHandle<PeerStorage> peerStorage = - btreg->getPeerStorage(btctx->getInfoHashAsString()); + btreg->get(btctx->getInfoHashAsString())._peerStorage; assert(!peerStorage.isNull()); BDE entry = BDE::dict(); gatherPeer(peers, peerStorage); |
