From 7cf589e78483e6ca7daff2ddd1a30af8091a4448 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 3 Nov 2008 06:49:02 +0000 Subject: [PATCH] 2008-11-03 Tatsuhiro Tsujikawa Made BtRegistry non-static object. Now DownloadEngine has a reference to its object. * src/AbstractBtMessage.cc * src/ActivePeerConnectionCommand.cc * src/ActivePeerConnectionCommand.h * src/BtContextAwareCommand.cc * src/BtContextAwareCommand.h * src/BtExtendedMessage.cc * src/BtExtendedMessage.h * src/BtInterestedMessage.cc * src/BtInterestedMessage.h * src/BtNotInterestedMessage.cc * src/BtNotInterestedMessage.h * src/BtRegistry.cc * src/BtRegistry.h * src/BtSeederStateChoke.cc * src/BtSetup.cc * src/DHTGetPeersCommand.cc * src/DHTGetPeersCommand.h * src/DHTPeerAnnounceEntry.cc * src/DHTPeerAnnounceEntry.h * src/DHTPeerAnnounceStorage.cc * src/DHTPeerAnnounceStorage.h * src/DHTPeerLookupTask.cc * src/DHTPeerLookupTask.h * src/DHTSetup.cc * src/DHTSetup.h * src/DHTTaskFactory.h * src/DHTTaskFactoryImpl.cc * src/DHTTaskFactoryImpl.h * src/DefaultBtAnnounce.cc * src/DefaultBtInteractive.cc * src/DefaultBtInteractive.h * src/DefaultBtMessageDispatcher.cc * src/DefaultBtMessageDispatcher.h * src/DefaultBtMessageFactory.cc * src/DefaultBtMessageFactory.h * src/DefaultBtMessageReceiver.cc * src/DefaultBtProgressInfoFile.cc * src/DefaultBtProgressInfoFile.h * src/DefaultBtRequestFactory.cc * src/DefaultBtRequestFactory.h * src/DefaultExtensionMessageFactory.cc * src/DefaultExtensionMessageFactory.h * src/DefaultPeerStorage.cc * src/DefaultPeerStorage.h * src/DownloadEngine.cc * src/DownloadEngine.h * src/InitiatorMSEHandshakeCommand.cc * src/InitiatorMSEHandshakeCommand.h * src/MSEHandshake.cc * src/MSEHandshake.h * src/Makefile.am * src/Makefile.in * src/PeerChokeCommand.cc * src/PeerChokeCommand.h * src/PeerInitiateConnectionCommand.cc * src/PeerInitiateConnectionCommand.h * src/PeerInteractionCommand.cc * src/PeerInteractionCommand.h * src/PeerObject.h * src/PeerReceiveHandshakeCommand.cc * src/PeerReceiveHandshakeCommand.h * src/ReceiverMSEHandshakeCommand.cc * src/RequestGroup.cc * src/RequestGroup.h * src/RequestGroupMan.cc * src/RequestGroupMan.h * src/SeedCheckCommand.cc * src/SeedCheckCommand.h * src/ShareRatioSeedCriteria.h * src/SimpleBtMessage.cc * src/TrackerWatcherCommand.cc * src/TrackerWatcherCommand.h * src/UTPexExtensionMessage.cc * src/UTPexExtensionMessage.h * test/BtCancelMessageTest.cc * test/BtChokeMessageTest.cc * test/BtExtendedMessageTest.cc * test/BtInterestedMessageTest.cc * test/BtNotInterestedMessageTest.cc * test/BtPieceMessageTest.cc * test/BtPortMessageTest.cc * test/BtRegistryTest.cc * test/BtRejectMessageTest.cc * test/BtRequestMessageTest.cc * test/DHTPeerAnnounceEntryTest.cc * test/DHTPeerAnnounceStorageTest.cc * test/DefaultBtMessageDispatcherTest.cc * test/DefaultBtMessageFactoryTest.cc * test/DefaultBtProgressInfoFileTest.cc * test/DefaultBtRequestFactoryTest.cc * test/DefaultExtensionMessageFactoryTest.cc * test/MSEHandshakeTest.cc * test/MockDHTTaskFactory.h * test/ShareRatioSeedCriteriaTest.cc * test/UTPexExtensionMessageTest.cc --- ChangeLog | 100 ++++++++++++++ src/AbstractBtMessage.cc | 2 - src/ActivePeerConnectionCommand.cc | 79 +++++++---- src/ActivePeerConnectionCommand.h | 22 ++- src/BtContextAwareCommand.cc | 56 -------- src/BtContextAwareCommand.h | 67 --------- src/BtExtendedMessage.cc | 11 +- src/BtExtendedMessage.h | 10 +- src/BtInterestedMessage.cc | 5 - src/BtInterestedMessage.h | 1 + src/BtNotInterestedMessage.cc | 9 +- src/BtNotInterestedMessage.h | 5 + src/BtRegistry.cc | 36 +---- src/BtRegistry.h | 120 +++++------------ src/BtSeederStateChoke.cc | 24 ++-- src/BtSetup.cc | 96 +++++++++---- src/DHTGetPeersCommand.cc | 22 ++- src/DHTGetPeersCommand.h | 16 ++- src/DHTPeerAnnounceEntry.cc | 27 ++-- src/DHTPeerAnnounceEntry.h | 12 +- src/DHTPeerAnnounceStorage.cc | 27 ++-- src/DHTPeerAnnounceStorage.h | 9 +- src/DHTPeerLookupTask.cc | 15 ++- src/DHTPeerLookupTask.h | 4 + src/DHTSetup.cc | 9 +- src/DHTSetup.h | 2 +- src/DHTTaskFactory.h | 6 +- src/DHTTaskFactoryImpl.cc | 8 +- src/DHTTaskFactoryImpl.h | 4 +- src/DefaultBtAnnounce.cc | 6 +- src/DefaultBtInteractive.cc | 113 ++++++++++------ src/DefaultBtInteractive.h | 51 +++++-- src/DefaultBtMessageDispatcher.cc | 27 ++-- src/DefaultBtMessageDispatcher.h | 8 +- src/DefaultBtMessageFactory.cc | 53 ++++++-- src/DefaultBtMessageFactory.h | 16 ++- src/DefaultBtMessageReceiver.cc | 5 +- src/DefaultBtProgressInfoFile.cc | 29 ++-- src/DefaultBtProgressInfoFile.h | 9 ++ src/DefaultBtRequestFactory.cc | 18 ++- src/DefaultBtRequestFactory.h | 4 +- src/DefaultExtensionMessageFactory.cc | 12 +- src/DefaultExtensionMessageFactory.h | 5 + src/DefaultPeerStorage.cc | 22 ++- src/DefaultPeerStorage.h | 13 +- src/DownloadEngine.cc | 29 +++- src/DownloadEngine.h | 20 ++- src/InitiatorMSEHandshakeCommand.cc | 56 +++++--- src/InitiatorMSEHandshakeCommand.h | 18 ++- src/MSEHandshake.cc | 11 +- src/MSEHandshake.h | 7 +- src/Makefile.am | 1 - src/Makefile.in | 8 +- src/PeerChokeCommand.cc | 25 +++- src/PeerChokeCommand.h | 18 ++- src/PeerInitiateConnectionCommand.cc | 80 ++++++----- src/PeerInitiateConnectionCommand.h | 20 ++- src/PeerInteractionCommand.cc | 123 +++++++++++------ src/PeerInteractionCommand.h | 20 ++- src/PeerObject.h | 63 --------- src/PeerReceiveHandshakeCommand.cc | 52 ++++--- src/PeerReceiveHandshakeCommand.h | 16 +-- src/ReceiverMSEHandshakeCommand.cc | 10 +- src/RequestGroup.cc | 89 ++++++------ src/RequestGroup.h | 16 ++- src/RequestGroupMan.cc | 14 +- src/RequestGroupMan.h | 2 +- src/SeedCheckCommand.cc | 25 +++- src/SeedCheckCommand.h | 13 +- src/ShareRatioSeedCriteria.h | 33 +++-- src/SimpleBtMessage.cc | 1 - src/TrackerWatcherCommand.cc | 77 ++++++++--- src/TrackerWatcherCommand.h | 26 +++- src/UTPexExtensionMessage.cc | 12 +- src/UTPexExtensionMessage.h | 14 +- test/BtCancelMessageTest.cc | 27 +--- test/BtChokeMessageTest.cc | 19 +-- test/BtExtendedMessageTest.cc | 51 ++----- test/BtInterestedMessageTest.cc | 8 +- test/BtNotInterestedMessageTest.cc | 9 +- test/BtPieceMessageTest.cc | 47 +++---- test/BtPortMessageTest.cc | 7 +- test/BtRegistryTest.cc | 78 ++++------- test/BtRejectMessageTest.cc | 48 +++---- test/BtRequestMessageTest.cc | 98 +++++++------- test/DHTPeerAnnounceEntryTest.cc | 25 +--- test/DHTPeerAnnounceStorageTest.cc | 23 ++-- test/DefaultBtMessageDispatcherTest.cc | 96 +++++-------- test/DefaultBtMessageFactoryTest.cc | 62 +++------ test/DefaultBtProgressInfoFileTest.cc | 95 +++++++------ test/DefaultBtRequestFactoryTest.cc | 149 ++++++++++----------- test/DefaultExtensionMessageFactoryTest.cc | 57 ++++---- test/MSEHandshakeTest.cc | 17 +-- test/MockDHTTaskFactory.h | 26 ++-- test/ShareRatioSeedCriteriaTest.cc | 33 ++--- test/UTPexExtensionMessageTest.cc | 63 ++++----- 96 files changed, 1709 insertions(+), 1423 deletions(-) delete mode 100644 src/BtContextAwareCommand.cc delete mode 100644 src/BtContextAwareCommand.h delete mode 100644 src/PeerObject.h diff --git a/ChangeLog b/ChangeLog index cd94c52d..070562a9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,103 @@ +2008-11-03 Tatsuhiro Tsujikawa + + Made BtRegistry non-static object. Now DownloadEngine has a reference to + its object. + * src/AbstractBtMessage.cc + * src/ActivePeerConnectionCommand.cc + * src/ActivePeerConnectionCommand.h + * src/BtContextAwareCommand.cc + * src/BtContextAwareCommand.h + * src/BtExtendedMessage.cc + * src/BtExtendedMessage.h + * src/BtInterestedMessage.cc + * src/BtInterestedMessage.h + * src/BtNotInterestedMessage.cc + * src/BtNotInterestedMessage.h + * src/BtRegistry.cc + * src/BtRegistry.h + * src/BtSeederStateChoke.cc + * src/BtSetup.cc + * src/DHTGetPeersCommand.cc + * src/DHTGetPeersCommand.h + * src/DHTPeerAnnounceEntry.cc + * src/DHTPeerAnnounceEntry.h + * src/DHTPeerAnnounceStorage.cc + * src/DHTPeerAnnounceStorage.h + * src/DHTPeerLookupTask.cc + * src/DHTPeerLookupTask.h + * src/DHTSetup.cc + * src/DHTSetup.h + * src/DHTTaskFactory.h + * src/DHTTaskFactoryImpl.cc + * src/DHTTaskFactoryImpl.h + * src/DefaultBtAnnounce.cc + * src/DefaultBtInteractive.cc + * src/DefaultBtInteractive.h + * src/DefaultBtMessageDispatcher.cc + * src/DefaultBtMessageDispatcher.h + * src/DefaultBtMessageFactory.cc + * src/DefaultBtMessageFactory.h + * src/DefaultBtMessageReceiver.cc + * src/DefaultBtProgressInfoFile.cc + * src/DefaultBtProgressInfoFile.h + * src/DefaultBtRequestFactory.cc + * src/DefaultBtRequestFactory.h + * src/DefaultExtensionMessageFactory.cc + * src/DefaultExtensionMessageFactory.h + * src/DefaultPeerStorage.cc + * src/DefaultPeerStorage.h + * src/DownloadEngine.cc + * src/DownloadEngine.h + * src/InitiatorMSEHandshakeCommand.cc + * src/InitiatorMSEHandshakeCommand.h + * src/MSEHandshake.cc + * src/MSEHandshake.h + * src/Makefile.am + * src/Makefile.in + * src/PeerChokeCommand.cc + * src/PeerChokeCommand.h + * src/PeerInitiateConnectionCommand.cc + * src/PeerInitiateConnectionCommand.h + * src/PeerInteractionCommand.cc + * src/PeerInteractionCommand.h + * src/PeerObject.h + * src/PeerReceiveHandshakeCommand.cc + * src/PeerReceiveHandshakeCommand.h + * src/ReceiverMSEHandshakeCommand.cc + * src/RequestGroup.cc + * src/RequestGroup.h + * src/RequestGroupMan.cc + * src/RequestGroupMan.h + * src/SeedCheckCommand.cc + * src/SeedCheckCommand.h + * src/ShareRatioSeedCriteria.h + * src/SimpleBtMessage.cc + * src/TrackerWatcherCommand.cc + * src/TrackerWatcherCommand.h + * src/UTPexExtensionMessage.cc + * src/UTPexExtensionMessage.h + * test/BtCancelMessageTest.cc + * test/BtChokeMessageTest.cc + * test/BtExtendedMessageTest.cc + * test/BtInterestedMessageTest.cc + * test/BtNotInterestedMessageTest.cc + * test/BtPieceMessageTest.cc + * test/BtPortMessageTest.cc + * test/BtRegistryTest.cc + * test/BtRejectMessageTest.cc + * test/BtRequestMessageTest.cc + * test/DHTPeerAnnounceEntryTest.cc + * test/DHTPeerAnnounceStorageTest.cc + * test/DefaultBtMessageDispatcherTest.cc + * test/DefaultBtMessageFactoryTest.cc + * test/DefaultBtProgressInfoFileTest.cc + * test/DefaultBtRequestFactoryTest.cc + * test/DefaultExtensionMessageFactoryTest.cc + * test/MSEHandshakeTest.cc + * test/MockDHTTaskFactory.h + * test/ShareRatioSeedCriteriaTest.cc + * test/UTPexExtensionMessageTest.cc + 2008-10-27 Tatsuhiro Tsujikawa Fixed compiler warning. diff --git a/src/AbstractBtMessage.cc b/src/AbstractBtMessage.cc index feb9e770..51777769 100644 --- a/src/AbstractBtMessage.cc +++ b/src/AbstractBtMessage.cc @@ -36,7 +36,6 @@ #include "Peer.h" #include "BtContext.h" #include "PieceStorage.h" -#include "BtRegistry.h" #include "BtEventListener.h" #include "BtMessageValidator.h" #include "LogFactory.h" @@ -99,7 +98,6 @@ SharedHandle AbstractBtMessage::getBtMessageValidator() cons void AbstractBtMessage::setBtContext(const SharedHandle& btContext) { this->btContext = btContext; - this->pieceStorage = PIECE_STORAGE(btContext); } SharedHandle AbstractBtMessage::getBtContext() const diff --git a/src/ActivePeerConnectionCommand.cc b/src/ActivePeerConnectionCommand.cc index 36a7416f..1941b2a8 100644 --- a/src/ActivePeerConnectionCommand.cc +++ b/src/ActivePeerConnectionCommand.cc @@ -52,19 +52,21 @@ namespace aria2 { -ActivePeerConnectionCommand::ActivePeerConnectionCommand(int cuid, - RequestGroup* requestGroup, - DownloadEngine* e, - const BtContextHandle& btContext, - time_t interval) - :Command(cuid), - BtContextAwareCommand(btContext), - RequestGroupAware(requestGroup), - interval(interval), - e(e), - _thresholdSpeed(e->option->getAsInt(PREF_BT_REQUEST_PEER_SPEED_LIMIT)), - _maxUploadSpeedLimit(e->option->getAsInt(PREF_MAX_UPLOAD_LIMIT)), - _numNewConnection(5) +ActivePeerConnectionCommand::ActivePeerConnectionCommand +(int cuid, + RequestGroup* requestGroup, + DownloadEngine* e, + const BtContextHandle& btContext, + time_t interval) + : + Command(cuid), + RequestGroupAware(requestGroup), + _btContext(btContext), + interval(interval), + e(e), + _thresholdSpeed(e->option->getAsInt(PREF_BT_REQUEST_PEER_SPEED_LIMIT)), + _maxUploadSpeedLimit(e->option->getAsInt(PREF_MAX_UPLOAD_LIMIT)), + _numNewConnection(5) { unsigned int maxDownloadSpeed = e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT); if(maxDownloadSpeed > 0) { @@ -75,32 +77,32 @@ ActivePeerConnectionCommand::ActivePeerConnectionCommand(int cuid, ActivePeerConnectionCommand::~ActivePeerConnectionCommand() {} bool ActivePeerConnectionCommand::execute() { - if(btRuntime->isHalt()) { + if(_btRuntime->isHalt()) { return true; } if(checkPoint.elapsed(interval)) { checkPoint.reset(); TransferStat tstat = _requestGroup->calculateStat(); if(// for seeder state - (pieceStorage->downloadFinished() && btRuntime->lessThanMaxPeers() && + (_pieceStorage->downloadFinished() && _btRuntime->lessThanMaxPeers() && (_maxUploadSpeedLimit == 0 || tstat.getUploadSpeed() < _maxUploadSpeedLimit*0.8)) || // for leecher state - (!pieceStorage->downloadFinished() && + (!_pieceStorage->downloadFinished() && (tstat.getDownloadSpeed() < _thresholdSpeed || - btRuntime->lessThanMinPeers()))) { - unsigned int numConnection = pieceStorage->downloadFinished() ? + _btRuntime->lessThanMinPeers()))) { + unsigned int numConnection = _pieceStorage->downloadFinished() ? std::min(_numNewConnection, - BtRuntime::MAX_PEERS-btRuntime->getConnections()) : + BtRuntime::MAX_PEERS-_btRuntime->getConnections()) : _numNewConnection; for(unsigned int numAdd = numConnection; - numAdd > 0 && peerStorage->isPeerAvailable(); --numAdd) { - PeerHandle peer = peerStorage->getUnusedPeer(); + numAdd > 0 && _peerStorage->isPeerAvailable(); --numAdd) { + PeerHandle peer = _peerStorage->getUnusedPeer(); connectToPeer(peer); } - if(btRuntime->getConnections() == 0 && - !pieceStorage->downloadFinished()) { - btAnnounce->overrideMinInterval(BtAnnounce::DEFAULT_ANNOUNCE_INTERVAL); + if(_btRuntime->getConnections() == 0 && + !_pieceStorage->downloadFinished()) { + _btAnnounce->overrideMinInterval(BtAnnounce::DEFAULT_ANNOUNCE_INTERVAL); } } } @@ -115,10 +117,37 @@ void ActivePeerConnectionCommand::connectToPeer(const PeerHandle& peer) } peer->usedBy(CUIDCounterSingletonHolder::instance()->newID()); PeerInitiateConnectionCommand* command = - new PeerInitiateConnectionCommand(peer->usedBy(), _requestGroup, peer, e, btContext); + new PeerInitiateConnectionCommand(peer->usedBy(), _requestGroup, peer, e, + _btContext, _btRuntime); + command->setPeerStorage(_peerStorage); + command->setPieceStorage(_pieceStorage); e->commands.push_back(command); logger->info(MSG_CONNECTING_TO_PEER, cuid, peer->ipaddr.c_str()); } +void ActivePeerConnectionCommand::setBtRuntime +(const SharedHandle& btRuntime) +{ + _btRuntime = btRuntime; +} + +void ActivePeerConnectionCommand::setPieceStorage +(const SharedHandle& pieceStorage) +{ + _pieceStorage = pieceStorage; +} + +void ActivePeerConnectionCommand::setPeerStorage +(const SharedHandle& peerStorage) +{ + _peerStorage = peerStorage; +} + +void ActivePeerConnectionCommand::setBtAnnounce +(const SharedHandle& btAnnounce) +{ + _btAnnounce = btAnnounce; +} + } // namespace aria2 diff --git a/src/ActivePeerConnectionCommand.h b/src/ActivePeerConnectionCommand.h index 6204ad76..77b6a6f1 100644 --- a/src/ActivePeerConnectionCommand.h +++ b/src/ActivePeerConnectionCommand.h @@ -36,7 +36,7 @@ #define _D_ACTIVE_PEER_CONNECTION_COMMAND_H_ #include "Command.h" -#include "BtContextAwareCommand.h" +#include "SharedHandle.h" #include "RequestGroupAware.h" #include "TimeA2.h" @@ -44,12 +44,22 @@ namespace aria2 { class DownloadEngine; class Peer; +class BtContext; +class BtRuntime; +class PieceStorage; +class PeerStorage; +class BtAnnounce; class ActivePeerConnectionCommand : public Command, - public BtContextAwareCommand, public RequestGroupAware { private: + SharedHandle _btContext; + SharedHandle _btRuntime; + SharedHandle _pieceStorage; + SharedHandle _peerStorage; + SharedHandle _btAnnounce; + time_t interval; // UNIT: sec DownloadEngine* e; Time checkPoint; @@ -73,6 +83,14 @@ public: { _numNewConnection = numNewConnection; } + + void setBtRuntime(const SharedHandle& btRuntime); + + void setPieceStorage(const SharedHandle& pieceStorage); + + void setPeerStorage(const SharedHandle& peerStorage); + + void setBtAnnounce(const SharedHandle& btAnnounce); }; } // namespace aria2 diff --git a/src/BtContextAwareCommand.cc b/src/BtContextAwareCommand.cc deleted file mode 100644 index 8d88138f..00000000 --- a/src/BtContextAwareCommand.cc +++ /dev/null @@ -1,56 +0,0 @@ -/* */ -#include "BtContextAwareCommand.h" -#include "BtRegistry.h" -#include "BtContext.h" -#include "BtRuntime.h" -#include "PieceStorage.h" -#include "PeerStorage.h" -#include "BtAnnounce.h" -#include "BtProgressInfoFile.h" - -namespace aria2 { - -BtContextAwareCommand::BtContextAwareCommand(const BtContextHandle& btContext): - btContext(btContext), - btRuntime(BT_RUNTIME(btContext)), - pieceStorage(PIECE_STORAGE(btContext)), - peerStorage(PEER_STORAGE(btContext)), - btAnnounce(BT_ANNOUNCE(btContext)), - btProgressInfoFile(BT_PROGRESS_INFO_FILE(btContext)) {} - -BtContextAwareCommand::~BtContextAwareCommand() {} - -} // namespace aria2 diff --git a/src/BtContextAwareCommand.h b/src/BtContextAwareCommand.h deleted file mode 100644 index bdef4f0d..00000000 --- a/src/BtContextAwareCommand.h +++ /dev/null @@ -1,67 +0,0 @@ -/* */ -#ifndef _D_BT_CONTEXT_AWARE_COMMAND_H_ -#define _D_BT_CONTEXT_AWARE_COMMAND_H_ - -#include "common.h" -#include "SharedHandle.h" - -namespace aria2 { - -class BtContext; -class BtRuntime; -class PieceStorage; -class PeerStorage; -class BtAnnounce; -class BtProgressInfoFile; - -class BtContextAwareCommand -{ -protected: - SharedHandle btContext; - SharedHandle btRuntime; - SharedHandle pieceStorage; - SharedHandle peerStorage; - SharedHandle btAnnounce; - SharedHandle btProgressInfoFile; -public: - BtContextAwareCommand(const SharedHandle& btContext); - - virtual ~BtContextAwareCommand(); -}; - -} // namespace aria2 - -#endif // _D_BT_CONTEXT_AWARE_COMMAND_H_ diff --git a/src/BtExtendedMessage.cc b/src/BtExtendedMessage.cc index b576bd4d..1eac1117 100644 --- a/src/BtExtendedMessage.cc +++ b/src/BtExtendedMessage.cc @@ -33,13 +33,6 @@ */ /* copyright --> */ #include "BtExtendedMessage.h" -#include "BtRegistry.h" -#include "PeerObject.h" -#include "BtMessageFactory.h" -#include "BtMessageReceiver.h" -#include "BtMessageDispatcher.h" -#include "BtRequestFactory.h" -#include "PeerConnection.h" #include "ExtensionMessage.h" #include "ExtensionMessageFactory.h" #include "PeerMessageUtil.h" @@ -96,7 +89,7 @@ std::string BtExtendedMessage::toString() const { } BtExtendedMessageHandle -BtExtendedMessage::create(const BtContextHandle& btContext, +BtExtendedMessage::create(const SharedHandle& factory, const PeerHandle& peer, const unsigned char* data, size_t dataLength) { @@ -109,8 +102,6 @@ BtExtendedMessage::create(const BtContextHandle& btContext, throw DlAbortEx (StringFormat(EX_INVALID_BT_MESSAGE_ID, id, "extended", ID).str()); } - ExtensionMessageFactoryHandle factory = EXTENSION_MESSAGE_FACTORY(btContext, - peer); assert(!factory.isNull()); ExtensionMessageHandle extmsg = factory->createMessage(data+1, dataLength-1); diff --git a/src/BtExtendedMessage.h b/src/BtExtendedMessage.h index a2b9cc8a..9dfbb8a1 100644 --- a/src/BtExtendedMessage.h +++ b/src/BtExtendedMessage.h @@ -39,6 +39,7 @@ namespace aria2 { class ExtensionMessage; +class ExtensionMessageFactory; class BtExtendedMessage; typedef SharedHandle BtExtendedMessageHandle; @@ -59,10 +60,11 @@ public: static const uint8_t ID = 20; - static BtExtendedMessageHandle create(const SharedHandle& btContext, - const SharedHandle& peer, - const unsigned char* data, - size_t dataLength); + static BtExtendedMessageHandle create + (const SharedHandle& factory, + const SharedHandle& peer, + const unsigned char* data, + size_t dataLength); virtual uint8_t getId() { return ID; } diff --git a/src/BtInterestedMessage.cc b/src/BtInterestedMessage.cc index aab9a612..0e44d9d3 100644 --- a/src/BtInterestedMessage.cc +++ b/src/BtInterestedMessage.cc @@ -37,9 +37,7 @@ #include "DlAbortEx.h" #include "message.h" #include "Peer.h" -#include "BtRegistry.h" #include "BtContext.h" -#include "PeerStorage.h" #include "StringFormat.h" namespace aria2 { @@ -60,9 +58,6 @@ BtInterestedMessageHandle BtInterestedMessage::create(const unsigned char* data, void BtInterestedMessage::doReceivedAction() { peer->peerInterested(true); - if(!peer->amChoking()) { - PEER_STORAGE(btContext)->executeChoke(); - } } bool BtInterestedMessage::sendPredicate() const { diff --git a/src/BtInterestedMessage.h b/src/BtInterestedMessage.h index 82e03314..9088b259 100644 --- a/src/BtInterestedMessage.h +++ b/src/BtInterestedMessage.h @@ -48,6 +48,7 @@ private: unsigned char* msg; static size_t MESSAGE_LENGTH; + public: BtInterestedMessage():msg(0) {} diff --git a/src/BtNotInterestedMessage.cc b/src/BtNotInterestedMessage.cc index f9548e45..8db23d4f 100644 --- a/src/BtNotInterestedMessage.cc +++ b/src/BtNotInterestedMessage.cc @@ -37,7 +37,6 @@ #include "DlAbortEx.h" #include "message.h" #include "Peer.h" -#include "BtRegistry.h" #include "BtContext.h" #include "PeerStorage.h" #include "StringFormat.h" @@ -61,7 +60,7 @@ BtNotInterestedMessageHandle BtNotInterestedMessage::create(const unsigned char* void BtNotInterestedMessage::doReceivedAction() { peer->peerInterested(false); if(!peer->amChoking()) { - PEER_STORAGE(btContext)->executeChoke(); + _peerStorage->executeChoke(); } } @@ -97,4 +96,10 @@ std::string BtNotInterestedMessage::toString() const { return NOT_INTERESTED; } +void BtNotInterestedMessage::setPeerStorage +(const SharedHandle& peerStorage) +{ + _peerStorage = peerStorage; +} + } // namespace aria2 diff --git a/src/BtNotInterestedMessage.h b/src/BtNotInterestedMessage.h index 993422f0..ba37c9d6 100644 --- a/src/BtNotInterestedMessage.h +++ b/src/BtNotInterestedMessage.h @@ -39,6 +39,7 @@ namespace aria2 { +class PeerStorage; class BtNotInterestedMessage; typedef SharedHandle BtNotInterestedMessageHandle; @@ -47,6 +48,8 @@ class BtNotInterestedMessage : public SimpleBtMessage { private: unsigned char* msg; + SharedHandle _peerStorage; + static size_t MESSAGE_LENGTH; public: BtNotInterestedMessage():msg(0) {} @@ -72,6 +75,8 @@ public: virtual bool sendPredicate() const; virtual void onSendComplete(); + + void setPeerStorage(const SharedHandle& peerStorage); }; } // namespace aria2 diff --git a/src/BtRegistry.cc b/src/BtRegistry.cc index 1a9b334f..13583dcb 100644 --- a/src/BtRegistry.cc +++ b/src/BtRegistry.cc @@ -40,23 +40,10 @@ #include "BtAnnounce.h" #include "BtRuntime.h" #include "BtProgressInfoFile.h" -#include "PeerObject.h" -#include "BtMessageFactory.h" -#include "BtRequestFactory.h" -#include "BtMessageDispatcher.h" -#include "BtMessageReceiver.h" -#include "PeerConnection.h" -#include "ExtensionMessageFactory.h" namespace aria2 { -BtContextMap BtRegistry::btContextMap; -PeerStorageMap BtRegistry::peerStorageMap; -PieceStorageMap BtRegistry::pieceStorageMap; -BtAnnounceMap BtRegistry::btAnnounceMap; -BtRuntimeMap BtRegistry::btRuntimeMap; -BtProgressInfoFileMap BtRegistry::btProgressInfoFileMap; -PeerObjectClusterRegistry BtRegistry::peerObjectClusterRegistry; +BtRegistry::BtRegistry() {} PeerStorageHandle BtRegistry::getPeerStorage(const std::string& key) { @@ -136,25 +123,6 @@ std::deque > BtRegistry::getAllBtContext() return btContextMap.getAll(); } -PeerObjectClusterHandle -BtRegistry::getPeerObjectCluster(const std::string& key) -{ - return peerObjectClusterRegistry.getHandle(key); -} - -void -BtRegistry::registerPeerObjectCluster(const std::string& key, - const PeerObjectClusterHandle& cluster) -{ - peerObjectClusterRegistry.registerHandle(key, cluster); -} - -void -BtRegistry::unregisterPeerObjectCluster(const std::string& key) -{ - peerObjectClusterRegistry.unregisterHandle(key); -} - void BtRegistry::unregisterAll() { btContextMap.clear(); peerStorageMap.clear(); @@ -162,7 +130,6 @@ void BtRegistry::unregisterAll() { btAnnounceMap.clear(); btRuntimeMap.clear(); btProgressInfoFileMap.clear(); - peerObjectClusterRegistry.clear(); } void BtRegistry::unregister(const std::string& key) @@ -173,7 +140,6 @@ void BtRegistry::unregister(const std::string& key) btAnnounceMap.unregisterHandle(key); btRuntimeMap.unregisterHandle(key); btProgressInfoFileMap.unregisterHandle(key); - peerObjectClusterRegistry.unregisterHandle(key); } } // namespace aria2 diff --git a/src/BtRegistry.h b/src/BtRegistry.h index 2ef55d47..c5f83089 100644 --- a/src/BtRegistry.h +++ b/src/BtRegistry.h @@ -36,11 +36,13 @@ #define _D_BT_REGISTRY_H_ #include "common.h" -#include "SharedHandle.h" -#include "HandleRegistry.h" + #include #include +#include "SharedHandle.h" +#include "HandleRegistry.h" + namespace aria2 { class PeerStorage; @@ -49,7 +51,6 @@ class BtAnnounce; class BtRuntime; class BtProgressInfoFile; class BtContext; -class PeerObject; typedef HandleRegistry PeerStorageMap; typedef HandleRegistry PieceStorageMap; @@ -58,104 +59,49 @@ typedef HandleRegistry BtRuntimeMap; typedef HandleRegistry BtProgressInfoFileMap; typedef HandleRegistry BtContextMap; -// for BtMessageFactory -typedef HandleRegistry PeerObjectCluster; -typedef SharedHandle PeerObjectClusterHandle; -typedef HandleRegistry PeerObjectClusterRegistry; - class BtRegistry { private: - BtRegistry() {} - - static BtContextMap btContextMap; - static PeerStorageMap peerStorageMap; - static PieceStorageMap pieceStorageMap; - static BtAnnounceMap btAnnounceMap; - static BtRuntimeMap btRuntimeMap; - static BtProgressInfoFileMap btProgressInfoFileMap; - static PeerObjectClusterRegistry peerObjectClusterRegistry; + BtContextMap btContextMap; + PeerStorageMap peerStorageMap; + PieceStorageMap pieceStorageMap; + BtAnnounceMap btAnnounceMap; + BtRuntimeMap btRuntimeMap; + BtProgressInfoFileMap btProgressInfoFileMap; public: - static SharedHandle getBtContext(const std::string& key); - static void registerBtContext(const std::string& key, - const SharedHandle& btContext); + BtRegistry(); + + SharedHandle getBtContext(const std::string& key); + void registerBtContext(const std::string& key, + const SharedHandle& btContext); - static SharedHandle getPeerStorage(const std::string& key); - static void registerPeerStorage(const std::string& key, - const SharedHandle& peer); + SharedHandle getPeerStorage(const std::string& key); + void registerPeerStorage(const std::string& key, + const SharedHandle& peer); - static SharedHandle getPieceStorage(const std::string& key); - static void registerPieceStorage(const std::string& key, - const SharedHandle& pieceStorage); + SharedHandle getPieceStorage(const std::string& key); + void registerPieceStorage(const std::string& key, + const SharedHandle& pieceStorage); - static SharedHandle getBtRuntime(const std::string& key); - static void registerBtRuntime(const std::string& key, - const SharedHandle& btRuntime); + SharedHandle getBtRuntime(const std::string& key); + void registerBtRuntime(const std::string& key, + const SharedHandle& btRuntime); - static SharedHandle getBtAnnounce(const std::string& key); - static void registerBtAnnounce(const std::string& key, + SharedHandle getBtAnnounce(const std::string& key); + void registerBtAnnounce(const std::string& key, const SharedHandle& btAnnounce); - static SharedHandle getBtProgressInfoFile(const std::string& key); - static void registerBtProgressInfoFile(const std::string& key, - const SharedHandle& btProgressInfoFile); + SharedHandle + getBtProgressInfoFile(const std::string& key); + void registerBtProgressInfoFile(const std::string& key, + const SharedHandle& file); - static std::deque > getAllBtContext(); + std::deque > getAllBtContext(); - // for PeerObject - static PeerObjectClusterHandle - getPeerObjectCluster(const std::string& key); + void unregisterAll(); - static void - registerPeerObjectCluster(const std::string& key, - const PeerObjectClusterHandle& cluster); - - static void - unregisterPeerObjectCluster(const std::string& key); - - static void unregisterAll(); - - static void unregister(const std::string& key); + void unregister(const std::string& key); }; -#define PEER_STORAGE(btContext) \ -BtRegistry::getPeerStorage(btContext->getInfoHashAsString()) - -#define PIECE_STORAGE(btContext) \ -BtRegistry::getPieceStorage(btContext->getInfoHashAsString()) - -#define BT_ANNOUNCE(btContext) \ -BtRegistry::getBtAnnounce(btContext->getInfoHashAsString()) - -#define BT_RUNTIME(btContext) \ -BtRegistry::getBtRuntime(btContext->getInfoHashAsString()) - -#define BT_PROGRESS_INFO_FILE(btContext) \ -BtRegistry::getBtProgressInfoFile(btContext->getInfoHashAsString()) - -#define PEER_OBJECT_CLUSTER(btContext) \ -BtRegistry::getPeerObjectCluster(btContext->getInfoHashAsString()) - -#define PEER_OBJECT(btContext, peer) \ -PEER_OBJECT_CLUSTER(btContext)->getHandle(peer->getID()) - -#define BT_MESSAGE_DISPATCHER(btContext, peer) \ -PEER_OBJECT(btContext, peer)->btMessageDispatcher - -#define BT_MESSAGE_RECEIVER(btContext, peer) \ -PEER_OBJECT(btContext, peer)->btMessageReceiver - -#define BT_MESSAGE_FACTORY(btContext, peer) \ -PEER_OBJECT(btContext, peer)->btMessageFactory - -#define BT_REQUEST_FACTORY(btContext, peer) \ -PEER_OBJECT(btContext, peer)->btRequestFactory - -#define PEER_CONNECTION(btContext, peer) \ -PEER_OBJECT(btContext, peer)->peerConnection - -#define EXTENSION_MESSAGE_FACTORY(btContext, peer) \ -PEER_OBJECT(btContext, peer)->extensionMessageFactory - } // namespace aria2 #endif // _D_BT_REGISTRY_H_ diff --git a/src/BtSeederStateChoke.cc b/src/BtSeederStateChoke.cc index 3e45d344..cabc65e6 100644 --- a/src/BtSeederStateChoke.cc +++ b/src/BtSeederStateChoke.cc @@ -33,10 +33,11 @@ */ /* copyright --> */ #include "BtSeederStateChoke.h" + +#include + #include "BtContext.h" #include "Peer.h" -#include "BtRegistry.h" -#include "PeerObject.h" #include "BtMessageDispatcher.h" #include "BtMessageFactory.h" #include "BtRequestFactory.h" @@ -46,7 +47,6 @@ #include "Logger.h" #include "LogFactory.h" #include "a2time.h" -#include namespace aria2 { @@ -70,19 +70,19 @@ public: bool operator()(Peer* left, Peer* right) const { - size_t leftUpload = BT_MESSAGE_DISPATCHER(_btContext, left)->countOutstandingRequest(); - size_t rightUpload = BT_MESSAGE_DISPATCHER(_btContext, right)->countOutstandingRequest(); - if(leftUpload && !rightUpload) { - return true; - } else if(!leftUpload && rightUpload) { - return false; - } + // TODO Should peer have the reference to message dispatcher? +// size_t leftUpload = BT_MESSAGE_DISPATCHER(_btContext, left)->countOutstandingUpload(); +// size_t rightUpload = BT_MESSAGE_DISPATCHER(_btContext, right)->countOutstandingUpload(); +// if(leftUpload && !rightUpload) { +// return true; +// } else if(!leftUpload && rightUpload) { +// return false; +// } const int TIME_FRAME = 20; if(!left->getLastAmUnchoking().elapsed(TIME_FRAME) && left->getLastAmUnchoking().isNewer(right->getLastAmUnchoking())) { return true; - } else if(!right->getLastAmUnchoking().elapsed(TIME_FRAME) && - right->getLastAmUnchoking().isNewer(left->getLastAmUnchoking())) { + } else if(!right->getLastAmUnchoking().elapsed(TIME_FRAME)) { return false; } else { return left->calculateUploadSpeed(_now) > right->calculateUploadSpeed(_now); diff --git a/src/BtSetup.cc b/src/BtSetup.cc index eeb8be9b..f7cf3e78 100644 --- a/src/BtSetup.cc +++ b/src/BtSetup.cc @@ -56,6 +56,8 @@ #include "DHTPeerAnnounceStorage.h" #include "DHTSetup.h" #include "DHTRegistry.h" +#include "BtProgressInfoFile.h" +#include "BtAnnounce.h" namespace aria2 { @@ -70,26 +72,64 @@ void BtSetup::setup(std::deque& commands, if(btContext.isNull()) { return; } - // commands - commands.push_back(new TrackerWatcherCommand(CUIDCounterSingletonHolder::instance()->newID(), - requestGroup, - e, - btContext)); - commands.push_back(new PeerChokeCommand(CUIDCounterSingletonHolder::instance()->newID(), - e, - btContext)); + 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()); - commands.push_back(new ActivePeerConnectionCommand(CUIDCounterSingletonHolder::instance()->newID(), - requestGroup, e, btContext, 10)); + // commands + { + TrackerWatcherCommand* c = + new TrackerWatcherCommand(CUIDCounterSingletonHolder::instance()->newID(), + requestGroup, + e, + btContext); + c->setPeerStorage(peerStorage); + c->setPieceStorage(pieceStorage); + c->setBtRuntime(btRuntime); + c->setBtAnnounce(btAnnounce); + + commands.push_back(c); + } + { + PeerChokeCommand* c = + new PeerChokeCommand(CUIDCounterSingletonHolder::instance()->newID(), + e, + btContext); + c->setPeerStorage(peerStorage); + c->setBtRuntime(btRuntime); + + commands.push_back(c); + } + { + ActivePeerConnectionCommand* c = + new ActivePeerConnectionCommand(CUIDCounterSingletonHolder::instance()->newID(), + requestGroup, e, btContext, 10); + c->setBtRuntime(btRuntime); + c->setPieceStorage(pieceStorage); + c->setPeerStorage(peerStorage); + c->setBtAnnounce(btAnnounce); + + commands.push_back(c); + } if(!btContext->isPrivate() && DHTSetup::initialized()) { - DHTRegistry::_peerAnnounceStorage->addPeerAnnounce(btContext); - DHTGetPeersCommand* command = new DHTGetPeersCommand(CUIDCounterSingletonHolder::instance()->newID(), - requestGroup, - e, - btContext); + DHTRegistry::_peerAnnounceStorage->addPeerAnnounce(btContext->getInfoHash(), + peerStorage); + DHTGetPeersCommand* command = + new DHTGetPeersCommand(CUIDCounterSingletonHolder::instance()->newID(), + requestGroup, + e, + btContext); command->setTaskQueue(DHTRegistry::_taskQueue); command->setTaskFactory(DHTRegistry::_taskFactory); + command->setBtRuntime(btRuntime); + command->setPeerStorage(peerStorage); commands.push_back(command); } SharedHandle unionCri(new UnionSeedCriteria()); @@ -100,16 +140,26 @@ void BtSetup::setup(std::deque& commands, { double ratio = option->getAsDouble(PREF_SEED_RATIO); if(ratio > 0.0) { - SharedHandle cri(new ShareRatioSeedCriteria(option->getAsDouble(PREF_SEED_RATIO), btContext)); + SharedHandle cri + (new ShareRatioSeedCriteria(option->getAsDouble(PREF_SEED_RATIO), + btContext)); + cri->setPieceStorage(pieceStorage); + cri->setPeerStorage(peerStorage); + cri->setBtRuntime(btRuntime); + unionCri->addSeedCriteria(cri); } } if(unionCri->getSeedCriterion().size() > 0) { - commands.push_back(new SeedCheckCommand(CUIDCounterSingletonHolder::instance()->newID(), - requestGroup, - e, - btContext, - unionCri)); + SeedCheckCommand* c = + new SeedCheckCommand(CUIDCounterSingletonHolder::instance()->newID(), + requestGroup, + e, + btContext, + unionCri); + c->setPieceStorage(pieceStorage); + c->setBtRuntime(btRuntime); + commands.push_back(c); } if(PeerListenCommand::getNumInstance() == 0) { @@ -117,7 +167,7 @@ void BtSetup::setup(std::deque& commands, IntSequence seq = Util::parseIntRange(option->get(PREF_LISTEN_PORT)); uint16_t port; if(listenCommand->bindPort(port, seq)) { - BT_RUNTIME(btContext)->setListenPort(port); + btRuntime->setListenPort(port); commands.push_back(listenCommand); } else { _logger->error(_("Errors occurred while binding port.\n")); @@ -125,7 +175,7 @@ void BtSetup::setup(std::deque& commands, } } - BT_RUNTIME(btContext)->setReady(true); + btRuntime->setReady(true); } } // namespace aria2 diff --git a/src/DHTGetPeersCommand.cc b/src/DHTGetPeersCommand.cc index 498befb8..7981e1c9 100644 --- a/src/DHTGetPeersCommand.cc +++ b/src/DHTGetPeersCommand.cc @@ -54,8 +54,8 @@ DHTGetPeersCommand::DHTGetPeersCommand(int32_t cuid, DownloadEngine* e, const BtContextHandle& ctx): Command(cuid), - BtContextAwareCommand(ctx), RequestGroupAware(requestGroup), + _btContext(ctx), _e(e), _numRetry(0), _lastGetPeerTime(0) @@ -65,19 +65,21 @@ DHTGetPeersCommand::~DHTGetPeersCommand() {} bool DHTGetPeersCommand::execute() { - if(btRuntime->isHalt()) { + if(_btRuntime->isHalt()) { return true; } if(_task.isNull() && ((_numRetry > 0 && _lastGetPeerTime.elapsed(RETRY_INTERVAL)) || _lastGetPeerTime.elapsed(GET_PEER_INTERVAL))) { logger->debug("Issuing PeerLookup for infoHash=%s", - btContext->getInfoHashAsString().c_str()); - _task = dynamic_pointer_cast(_taskFactory->createPeerLookupTask(btContext)); + _btContext->getInfoHashAsString().c_str()); + _task = dynamic_pointer_cast + (_taskFactory->createPeerLookupTask(_btContext, _btRuntime, + _peerStorage)); _taskQueue->addPeriodicTask2(_task); } else if(!_task.isNull() && _task->finished()) { _lastGetPeerTime.reset(); - if(_numRetry < MAX_RETRIES && btRuntime->lessThanMinPeers()) { + if(_numRetry < MAX_RETRIES && _btRuntime->lessThanMinPeers()) { ++_numRetry; } else { _numRetry = 0; @@ -99,4 +101,14 @@ void DHTGetPeersCommand::setTaskFactory(const SharedHandle& task _taskFactory = taskFactory; } +void DHTGetPeersCommand::setBtRuntime(const SharedHandle& btRuntime) +{ + _btRuntime = btRuntime; +} + +void DHTGetPeersCommand::setPeerStorage(const SharedHandle& ps) +{ + _peerStorage = ps; +} + } // namespace aria2 diff --git a/src/DHTGetPeersCommand.h b/src/DHTGetPeersCommand.h index 8ff5be6d..fc44f48d 100644 --- a/src/DHTGetPeersCommand.h +++ b/src/DHTGetPeersCommand.h @@ -36,8 +36,8 @@ #define _D_DHT_GET_PEERS_COMMAND_H_ #include "Command.h" -#include "BtContextAwareCommand.h" #include "RequestGroupAware.h" +#include "SharedHandle.h" #include "TimeA2.h" namespace aria2 { @@ -47,9 +47,11 @@ class DHTTaskFactory; class DHTPeerLookupTask; class DownloadEngine; class RequestGroup; +class BtContext; +class BtRuntime; +class PeerStorage; class DHTGetPeersCommand:public Command, - public BtContextAwareCommand, public RequestGroupAware { private: @@ -59,6 +61,12 @@ private: static const size_t MAX_RETRIES = 10; + SharedHandle _btContext; + + SharedHandle _btRuntime; + + SharedHandle _peerStorage; + DownloadEngine* _e; SharedHandle _taskQueue; @@ -82,6 +90,10 @@ public: void setTaskQueue(const SharedHandle& taskQueue); void setTaskFactory(const SharedHandle& taskFactory); + + void setBtRuntime(const SharedHandle& btRuntime); + + void setPeerStorage(const SharedHandle& peerStorage); }; } // namespace aria2 diff --git a/src/DHTPeerAnnounceEntry.cc b/src/DHTPeerAnnounceEntry.cc index 7986b02a..4a092f4c 100644 --- a/src/DHTPeerAnnounceEntry.cc +++ b/src/DHTPeerAnnounceEntry.cc @@ -33,13 +33,13 @@ */ /* copyright --> */ #include "DHTPeerAnnounceEntry.h" -#include "BtContext.h" -#include "Peer.h" -#include "BtRegistry.h" -#include "PeerStorage.h" + #include #include +#include "Peer.h" +#include "PeerStorage.h" + namespace aria2 { DHTPeerAnnounceEntry::DHTPeerAnnounceEntry(const unsigned char* infoHash) @@ -60,9 +60,15 @@ void DHTPeerAnnounceEntry::addPeerAddrEntry(const PeerAddrEntry& entry) notifyUpdate(); } -void DHTPeerAnnounceEntry::setBtContext(const SharedHandle& btCtx) +void DHTPeerAnnounceEntry::clearLocal() { - _btCtx = btCtx; + _peerStorage.reset(); +} + +void DHTPeerAnnounceEntry::setPeerStorage +(const SharedHandle& peerStorage) +{ + _peerStorage = peerStorage; } size_t DHTPeerAnnounceEntry::countPeerAddrEntry() const @@ -99,7 +105,7 @@ void DHTPeerAnnounceEntry::removeStalePeerAddrEntry(time_t timeout) bool DHTPeerAnnounceEntry::empty() const { - return _peerAddrEntries.empty() && _btCtx.isNull(); + return _peerAddrEntries.empty() && _peerStorage.isNull(); } void DHTPeerAnnounceEntry::getPeers(std::deque >& peers) const @@ -109,11 +115,8 @@ void DHTPeerAnnounceEntry::getPeers(std::deque >& peers) cons SharedHandle peer(new Peer((*i).getIPAddress(), (*i).getPort())); peers.push_back(peer); } - if(!_btCtx.isNull()) { - SharedHandle peerStorage = PEER_STORAGE(_btCtx); - if(!peerStorage.isNull()) { - peerStorage->getActivePeers(peers); - } + if(!_peerStorage.isNull()) { + _peerStorage->getActivePeers(peers); } } diff --git a/src/DHTPeerAnnounceEntry.h b/src/DHTPeerAnnounceEntry.h index c91fdef2..f40f1555 100644 --- a/src/DHTPeerAnnounceEntry.h +++ b/src/DHTPeerAnnounceEntry.h @@ -36,16 +36,18 @@ #define _D_DHT_PEER_ANNOUNCE_ENTRY_H_ #include "common.h" + +#include + #include "SharedHandle.h" #include "DHTConstants.h" #include "PeerAddrEntry.h" #include "TimeA2.h" -#include namespace aria2 { class Peer; -class BtContext; +class PeerStorage; class DHTPeerAnnounceEntry { private: @@ -53,7 +55,7 @@ private: std::deque _peerAddrEntries; - SharedHandle _btCtx; + SharedHandle _peerStorage; Time _lastUpdated; public: @@ -65,7 +67,9 @@ public: // if it already exists, update "Last Updated" property. void addPeerAddrEntry(const PeerAddrEntry& entry); - void setBtContext(const SharedHandle& btCtx); + void clearLocal(); + + void setPeerStorage(const SharedHandle& peerStorage); size_t countPeerAddrEntry() const; diff --git a/src/DHTPeerAnnounceStorage.cc b/src/DHTPeerAnnounceStorage.cc index 18162b54..f2e54dd1 100644 --- a/src/DHTPeerAnnounceStorage.cc +++ b/src/DHTPeerAnnounceStorage.cc @@ -33,9 +33,13 @@ */ /* copyright --> */ #include "DHTPeerAnnounceStorage.h" + +#include +#include + #include "DHTPeerAnnounceEntry.h" #include "Peer.h" -#include "BtContext.h" +#include "PeerStorage.h" #include "DHTConstants.h" #include "DHTTaskQueue.h" #include "DHTTaskFactory.h" @@ -44,8 +48,6 @@ #include "Logger.h" #include "Util.h" #include "a2functional.h" -#include -#include namespace aria2 { @@ -91,24 +93,27 @@ DHTPeerAnnounceStorage::addPeerAnnounce(const unsigned char* infoHash, } // add peer announce as localhost downloading the content -void DHTPeerAnnounceStorage::addPeerAnnounce(const BtContextHandle& ctx) +void DHTPeerAnnounceStorage::addPeerAnnounce +(const unsigned char* infoHash, const SharedHandle& peerStorage) { _logger->debug("Adding localhost to peer announce list: infoHash=%s", - ctx->getInfoHashAsString().c_str()); - getPeerAnnounceEntry(ctx->getInfoHash())->setBtContext(ctx); + Util::toHex(infoHash, DHT_ID_LENGTH).c_str()); + SharedHandle entry = + getPeerAnnounceEntry(infoHash); + entry->setPeerStorage(peerStorage); } -void DHTPeerAnnounceStorage::removePeerAnnounce(const BtContextHandle& ctx) +void DHTPeerAnnounceStorage::removeLocalPeerAnnounce +(const unsigned char* infoHash) { - SharedHandle entry(new DHTPeerAnnounceEntry( - ctx->getInfoHash())); + SharedHandle entry(new DHTPeerAnnounceEntry(infoHash)); std::deque >::iterator i = std::lower_bound(_entries.begin(), _entries.end(), entry, InfoHashLess()); if(i != _entries.end() && - memcmp(ctx->getInfoHash(), (*i)->getInfoHash(), DHT_ID_LENGTH) == 0) { - (*i)->setBtContext(SharedHandle()); + memcmp(infoHash, (*i)->getInfoHash(), DHT_ID_LENGTH) == 0) { + (*i)->clearLocal(); if((*i)->empty()) { _entries.erase(i); } diff --git a/src/DHTPeerAnnounceStorage.h b/src/DHTPeerAnnounceStorage.h index d62d612a..0d9ce87c 100644 --- a/src/DHTPeerAnnounceStorage.h +++ b/src/DHTPeerAnnounceStorage.h @@ -41,7 +41,7 @@ namespace aria2 { -class BtContext; +class PeerStorage; class Peer; class DHTPeerAnnounceEntry; class DHTTaskQueue; @@ -68,11 +68,12 @@ public: const std::string& ipaddr, uint16_t port); // add peer announce as localhost downloading the content - void addPeerAnnounce(const SharedHandle& ctx); + void addPeerAnnounce(const unsigned char* infoHash, + const SharedHandle& peerStorage); - // give 0 to DHTPeerAnnounceEntry::setBtContext(). + // Call DHTPeerAnnounceEntry::clearLocal() for infoHash if found. // If DHTPeerAnnounceEntry is empty, it is erased. - void removePeerAnnounce(const SharedHandle& ctx); + void removeLocalPeerAnnounce(const unsigned char* infoHash); bool contains(const unsigned char* infoHash) const; diff --git a/src/DHTPeerLookupTask.cc b/src/DHTPeerLookupTask.cc index a2fe6836..392d4a1d 100644 --- a/src/DHTPeerLookupTask.cc +++ b/src/DHTPeerLookupTask.cc @@ -41,7 +41,6 @@ #include "DHTNodeLookupEntry.h" #include "DHTMessageDispatcher.h" #include "DHTMessageCallback.h" -#include "BtRegistry.h" #include "PeerStorage.h" #include "BtRuntime.h" #include "BtContext.h" @@ -52,9 +51,7 @@ namespace aria2 { DHTPeerLookupTask::DHTPeerLookupTask(const SharedHandle& btContext): DHTAbstractNodeLookupTask(btContext->getInfoHash()), - _ctx(btContext), - _peerStorage(PEER_STORAGE(btContext)), - _btRuntime(BT_RUNTIME(btContext)) {} + _ctx(btContext) {} void DHTPeerLookupTask::getNodesFromMessage(std::deque >& nodes, @@ -110,4 +107,14 @@ const std::deque >& DHTPeerLookupTask::getPeers() const return _peers; } +void DHTPeerLookupTask::setBtRuntime(const SharedHandle& btRuntime) +{ + _btRuntime = btRuntime; +} + +void DHTPeerLookupTask::setPeerStorage(const SharedHandle& ps) +{ + _peerStorage = ps; +} + } // namespace aria2 diff --git a/src/DHTPeerLookupTask.h b/src/DHTPeerLookupTask.h index 3a9cf2e2..b01df29c 100644 --- a/src/DHTPeerLookupTask.h +++ b/src/DHTPeerLookupTask.h @@ -69,6 +69,10 @@ public: virtual void onFinish(); const std::deque >& getPeers() const; + + void setBtRuntime(const SharedHandle& btRuntime); + + void setPeerStorage(const SharedHandle& peerStorage); }; } // namespace aria2 diff --git a/src/DHTSetup.cc b/src/DHTSetup.cc index cead3e80..4dc6faca 100644 --- a/src/DHTSetup.cc +++ b/src/DHTSetup.cc @@ -33,6 +33,10 @@ */ /* copyright --> */ #include "DHTSetup.h" + +#include +#include + #include "LogFactory.h" #include "Logger.h" #include "Util.h" @@ -66,12 +70,11 @@ #include "DlAbortEx.h" #include "RecoverableException.h" #include "a2functional.h" -#include -#include namespace aria2 { -size_t DHTSetup::_initialized = 0; +// TODO DownloadEngine should hold this flag. +bool DHTSetup::_initialized = false; DHTSetup::DHTSetup():_logger(LogFactory::getInstance()) {} diff --git a/src/DHTSetup.h b/src/DHTSetup.h index 29339ca1..6cf9376b 100644 --- a/src/DHTSetup.h +++ b/src/DHTSetup.h @@ -46,7 +46,7 @@ class Command; class DHTSetup { private: - static size_t _initialized; + static bool _initialized; Logger* _logger; diff --git a/src/DHTTaskFactory.h b/src/DHTTaskFactory.h index c0149729..b5c8dcf9 100644 --- a/src/DHTTaskFactory.h +++ b/src/DHTTaskFactory.h @@ -41,6 +41,8 @@ namespace aria2 { class BtContext; +class BtRuntime; +class PeerStorage; class DHTTask; class DHTNode; class DHTBucket; @@ -59,7 +61,9 @@ public: virtual SharedHandle createBucketRefreshTask() = 0; virtual SharedHandle - createPeerLookupTask(const SharedHandle& ctx) = 0; + createPeerLookupTask(const SharedHandle& ctx, + const SharedHandle& btRuntime, + const SharedHandle& peerStorage) = 0; virtual SharedHandle createPeerAnnounceTask(const unsigned char* infoHash) = 0; diff --git a/src/DHTTaskFactoryImpl.cc b/src/DHTTaskFactoryImpl.cc index 9e399cd1..d4e93725 100644 --- a/src/DHTTaskFactoryImpl.cc +++ b/src/DHTTaskFactoryImpl.cc @@ -84,9 +84,15 @@ DHTTaskFactoryImpl::createBucketRefreshTask() } SharedHandle -DHTTaskFactoryImpl::createPeerLookupTask(const SharedHandle& ctx) +DHTTaskFactoryImpl::createPeerLookupTask +(const SharedHandle& ctx, + const SharedHandle& btRuntime, + const SharedHandle& peerStorage) { SharedHandle task(new DHTPeerLookupTask(ctx)); + // TODO these may be not freed by RequestGroup::releaseRuntimeResource() + task->setBtRuntime(btRuntime); + task->setPeerStorage(peerStorage); setCommonProperty(task); return task; } diff --git a/src/DHTTaskFactoryImpl.h b/src/DHTTaskFactoryImpl.h index d35e5323..9d8c1649 100644 --- a/src/DHTTaskFactoryImpl.h +++ b/src/DHTTaskFactoryImpl.h @@ -77,7 +77,9 @@ public: virtual SharedHandle createBucketRefreshTask(); virtual SharedHandle - createPeerLookupTask(const SharedHandle& ctx); + createPeerLookupTask(const SharedHandle& ctx, + const SharedHandle& btRuntime, + const SharedHandle& peerStorage); virtual SharedHandle createPeerAnnounceTask(const unsigned char* infoHash); diff --git a/src/DefaultBtAnnounce.cc b/src/DefaultBtAnnounce.cc index 6b9b9784..3d16b892 100644 --- a/src/DefaultBtAnnounce.cc +++ b/src/DefaultBtAnnounce.cc @@ -33,7 +33,6 @@ */ /* copyright --> */ #include "DefaultBtAnnounce.h" -#include "BtRegistry.h" #include "LogFactory.h" #include "Logger.h" #include "MetaFileUtil.h" @@ -68,10 +67,7 @@ DefaultBtAnnounce::DefaultBtAnnounce(const BtContextHandle& btContext, announceList(btContext->getAnnounceTiers()), option(option), logger(LogFactory::getInstance()), - _randomizer(SimpleRandomizer::getInstance()), - btRuntime(BT_RUNTIME(btContext)), - pieceStorage(PIECE_STORAGE(btContext)), - peerStorage(PEER_STORAGE(btContext)) + _randomizer(SimpleRandomizer::getInstance()) { prevAnnounceTime.setTimeInSec(0); generateKey(); diff --git a/src/DefaultBtInteractive.cc b/src/DefaultBtInteractive.cc index 776271d6..a32a6c3a 100644 --- a/src/DefaultBtInteractive.cc +++ b/src/DefaultBtInteractive.cc @@ -33,6 +33,9 @@ */ /* copyright --> */ #include "DefaultBtInteractive.h" + +#include + #include "prefs.h" #include "message.h" #include "BtHandshakeMessage.h" @@ -47,9 +50,7 @@ #include "HandshakeExtensionMessage.h" #include "UTPexExtensionMessage.h" #include "DefaultExtensionMessageFactory.h" -#include "BtRegistry.h" #include "DHTNode.h" -#include "PeerObject.h" #include "Peer.h" #include "Piece.h" #include "BtContext.h" @@ -61,17 +62,17 @@ #include "BtMessageFactory.h" #include "BtRequestFactory.h" #include "PeerConnection.h" -#include "BtRegistry.h" #include "Logger.h" #include "LogFactory.h" #include "StringFormat.h" #include "RequestGroup.h" -#include namespace aria2 { -DefaultBtInteractive::DefaultBtInteractive(const SharedHandle& btContext, const SharedHandle& peer): - BtContextAwareCommand(btContext), +DefaultBtInteractive::DefaultBtInteractive +(const SharedHandle& btContext, const SharedHandle& peer) + : + _btContext(btContext), peer(peer), logger(LogFactory::getInstance()), allowedFastSetSize(10), @@ -84,11 +85,10 @@ DefaultBtInteractive::DefaultBtInteractive(const SharedHandle& btCont DefaultBtInteractive::~DefaultBtInteractive() {} - void DefaultBtInteractive::initiateHandshake() { SharedHandle message = - messageFactory->createHandshakeMessage(btContext->getInfoHash(), - btContext->getPeerId()); + messageFactory->createHandshakeMessage(_btContext->getInfoHash(), + _btContext->getPeerId()); dispatcher->addMessageToQueue(message); dispatcher->sendMessages(); } @@ -99,8 +99,11 @@ BtMessageHandle DefaultBtInteractive::receiveHandshake(bool quickReply) { if(message.isNull()) { return SharedHandle(); } - if(memcmp(message->getPeerId(), btContext->getPeerId(), PEER_ID_LENGTH) == 0) { - throw DlAbortEx(StringFormat("CUID#%d - Drop connection from the same Peer ID", cuid).str()); + if(memcmp(message->getPeerId(), _btContext->getPeerId(), + PEER_ID_LENGTH) == 0) { + throw DlAbortEx + (StringFormat + ("CUID#%d - Drop connection from the same Peer ID", cuid).str()); } peer->setPeerId(message->getPeerId()); @@ -111,12 +114,9 @@ BtMessageHandle DefaultBtInteractive::receiveHandshake(bool quickReply) { } if(message->isExtendedMessagingEnabled()) { peer->setExtendedMessagingEnabled(true); - DefaultExtensionMessageFactoryHandle factory - (new DefaultExtensionMessageFactory(btContext, peer)); if(!_utPexEnabled) { - factory->removeExtension("ut_pex"); + _extensionMessageFactory->removeExtension("ut_pex"); } - PEER_OBJECT(btContext, peer)->extensionMessageFactory = factory; logger->info(MSG_EXTENDED_MESSAGING_ENABLED, cuid); } if(message->isDHTEnabled()) { @@ -152,7 +152,8 @@ void DefaultBtInteractive::doPostHandshakeProcessing() { void DefaultBtInteractive::addPortMessageToQueue() { - dispatcher->addMessageToQueue(messageFactory->createPortMessage(_localNode->getPort())); + dispatcher->addMessageToQueue + (messageFactory->createPortMessage(_localNode->getPort())); } void DefaultBtInteractive::addHandshakeExtendedMessageToQueue() @@ -160,8 +161,8 @@ void DefaultBtInteractive::addHandshakeExtendedMessageToQueue() static const std::string CLIENT_ARIA2("aria2"); HandshakeExtensionMessageHandle m(new HandshakeExtensionMessage()); m->setClientVersion(CLIENT_ARIA2); - m->setTCPPort(btRuntime->getListenPort()); - m->setExtensions(EXTENSION_MESSAGE_FACTORY(btContext, peer)->getExtensions()); + m->setTCPPort(_btRuntime->getListenPort()); + m->setExtensions(_extensionMessageFactory->getExtensions()); SharedHandle msg = messageFactory->createBtExtendedMessage(m); dispatcher->addMessageToQueue(msg); @@ -169,15 +170,15 @@ void DefaultBtInteractive::addHandshakeExtendedMessageToQueue() void DefaultBtInteractive::addBitfieldMessageToQueue() { if(peer->isFastExtensionEnabled()) { - if(pieceStorage->allDownloadFinished()) { + if(_pieceStorage->allDownloadFinished()) { dispatcher->addMessageToQueue(messageFactory->createHaveAllMessage()); - } else if(pieceStorage->getCompletedLength() > 0) { + } else if(_pieceStorage->getCompletedLength() > 0) { dispatcher->addMessageToQueue(messageFactory->createBitfieldMessage()); } else { dispatcher->addMessageToQueue(messageFactory->createHaveNoneMessage()); } } else { - if(pieceStorage->getCompletedLength() > 0) { + if(_pieceStorage->getCompletedLength() > 0) { dispatcher->addMessageToQueue(messageFactory->createBitfieldMessage()); } } @@ -186,10 +187,11 @@ void DefaultBtInteractive::addBitfieldMessageToQueue() { void DefaultBtInteractive::addAllowedFastMessageToQueue() { if(peer->isFastExtensionEnabled()) { std::deque fastSet; - btContext->computeFastSet(fastSet, peer->ipaddr, allowedFastSetSize); + _btContext->computeFastSet(fastSet, peer->ipaddr, allowedFastSetSize); for(std::deque::const_iterator itr = fastSet.begin(); itr != fastSet.end(); itr++) { - dispatcher->addMessageToQueue(messageFactory->createAllowedFastMessage(*itr)); + dispatcher->addMessageToQueue + (messageFactory->createAllowedFastMessage(*itr)); } } } @@ -208,16 +210,17 @@ void DefaultBtInteractive::decideChoking() { void DefaultBtInteractive::checkHave() { std::deque indexes; - pieceStorage->getAdvertisedPieceIndexes(indexes, cuid, haveCheckPoint); + _pieceStorage->getAdvertisedPieceIndexes(indexes, cuid, haveCheckPoint); haveCheckPoint.reset(); if(indexes.size() >= 20) { - if(peer->isFastExtensionEnabled() && pieceStorage->allDownloadFinished()) { + if(peer->isFastExtensionEnabled() && _pieceStorage->allDownloadFinished()) { dispatcher->addMessageToQueue(messageFactory->createHaveAllMessage()); } else { dispatcher->addMessageToQueue(messageFactory->createBitfieldMessage()); } } else { - for(std::deque::iterator itr = indexes.begin(); itr != indexes.end(); itr++) { + for(std::deque::iterator itr = indexes.begin(); + itr != indexes.end(); itr++) { dispatcher->addMessageToQueue(messageFactory->createHaveMessage(*itr)); } } @@ -235,7 +238,7 @@ size_t DefaultBtInteractive::receiveMessages() { size_t msgcount = 0; for(int i = 0; i < 50; i++) { if(maxDownloadSpeedLimit > 0) { - TransferStat stat = btContext->getOwnerRequestGroup()->calculateStat(); + TransferStat stat = _btContext->getOwnerRequestGroup()->calculateStat(); if(maxDownloadSpeedLimit < stat.downloadSpeed) { break; } @@ -274,7 +277,7 @@ size_t DefaultBtInteractive::receiveMessages() { } void DefaultBtInteractive::decideInterest() { - if(pieceStorage->hasMissingPiece(peer)) { + if(_pieceStorage->hasMissingPiece(peer)) { if(!peer->amInterested()) { logger->debug(MSG_PEER_INTERESTED, cuid); dispatcher-> @@ -290,7 +293,7 @@ void DefaultBtInteractive::decideInterest() { } void DefaultBtInteractive::fillPiece(size_t maxMissingBlock) { - if(pieceStorage->hasMissingPiece(peer)) { + if(_pieceStorage->hasMissingPiece(peer)) { size_t numMissingBlock = btRequestFactory->countMissingBlock(); @@ -300,7 +303,7 @@ void DefaultBtInteractive::fillPiece(size_t maxMissingBlock) { btRequestFactory->getTargetPieceIndexes(excludedIndexes); while(numMissingBlock < maxMissingBlock) { SharedHandle piece = - pieceStorage->getMissingFastPiece(peer, excludedIndexes); + _pieceStorage->getMissingFastPiece(peer, excludedIndexes); if(piece.isNull()) { break; } else { @@ -315,7 +318,7 @@ void DefaultBtInteractive::fillPiece(size_t maxMissingBlock) { btRequestFactory->getTargetPieceIndexes(excludedIndexes); while(numMissingBlock < maxMissingBlock) { SharedHandle piece = - pieceStorage->getMissingPiece(peer, excludedIndexes); + _pieceStorage->getMissingPiece(peer, excludedIndexes); if(piece.isNull()) { break; } else { @@ -344,7 +347,7 @@ void DefaultBtInteractive::addRequests() { 0 : MAX_PENDING_REQUEST-dispatcher->countOutstandingRequest(); if(reqNumToCreate > 0) { BtMessages requests; - if(pieceStorage->isEndGame()) { + if(_pieceStorage->isEndGame()) { btRequestFactory->createRequestMessagesOnEndGame(requests, reqNumToCreate); } else { btRequestFactory->createRequestMessages(requests, reqNumToCreate); @@ -383,7 +386,8 @@ void DefaultBtInteractive::checkActiveInteraction() inactiveCheckPoint.elapsed(interval)) { // TODO change the message throw DlAbortEx - (StringFormat("Disconnect peer because we are not interested each other after %u second(s).", interval).str()); + (StringFormat("Disconnect peer because we are not interested each other" + " after %u second(s).", interval).str()); } } // Since the peers which are *just* connected and do nothing to improve @@ -403,7 +407,7 @@ void DefaultBtInteractive::addPeerExchangeMessage() if(_pexCheckPoint.elapsed(UTPexExtensionMessage::DEFAULT_INTERVAL)) { UTPexExtensionMessageHandle m (new UTPexExtensionMessage(peer->getExtensionMessageID("ut_pex"))); - const Peers& peers = peerStorage->getPeers(); + const Peers& peers = _peerStorage->getPeers(); { for(std::deque >::const_iterator i = peers.begin(); i != peers.end() && !m->freshPeersAreFull(); ++i) { @@ -445,7 +449,7 @@ void DefaultBtInteractive::doInteractionProcessing() { btRequestFactory->removeCompletedPiece(); decideInterest(); - if(!pieceStorage->downloadFinished()) { + if(!_pieceStorage->downloadFinished()) { addRequests(); } @@ -481,32 +485,61 @@ size_t DefaultBtInteractive::countOutstandingRequest() return dispatcher->countOutstandingRequest(); } +void DefaultBtInteractive::setBtRuntime +(const SharedHandle& btRuntime) +{ + _btRuntime = btRuntime; +} + +void DefaultBtInteractive::setPieceStorage +(const SharedHandle& pieceStorage) +{ + _pieceStorage = pieceStorage; +} + +void DefaultBtInteractive::setPeerStorage +(const SharedHandle& peerStorage) +{ + _peerStorage = peerStorage; +} + void DefaultBtInteractive::setPeer(const SharedHandle& peer) { this->peer = peer; } -void DefaultBtInteractive::setBtMessageReceiver(const BtMessageReceiverWeakHandle& receiver) +void DefaultBtInteractive::setBtMessageReceiver +(const SharedHandle& receiver) { this->btMessageReceiver = receiver; } -void DefaultBtInteractive::setDispatcher(const BtMessageDispatcherWeakHandle& dispatcher) +void DefaultBtInteractive::setDispatcher +(const SharedHandle& dispatcher) { this->dispatcher = dispatcher; } -void DefaultBtInteractive::setBtRequestFactory(const BtRequestFactoryWeakHandle& factory) +void DefaultBtInteractive::setBtRequestFactory +(const SharedHandle& factory) { this->btRequestFactory = factory; } -void DefaultBtInteractive::setPeerConnection(const PeerConnectionWeakHandle& peerConnection) +void DefaultBtInteractive::setPeerConnection +(const SharedHandle& peerConnection) { this->peerConnection = peerConnection; } -void DefaultBtInteractive::setBtMessageFactory(const BtMessageFactoryWeakHandle& factory) +void DefaultBtInteractive::setExtensionMessageFactory +(const SharedHandle& factory) +{ + _extensionMessageFactory = factory; +} + +void DefaultBtInteractive::setBtMessageFactory +(const SharedHandle& factory) { this->messageFactory = factory; } diff --git a/src/DefaultBtInteractive.h b/src/DefaultBtInteractive.h index f7f47a33..98f9d2e3 100644 --- a/src/DefaultBtInteractive.h +++ b/src/DefaultBtInteractive.h @@ -36,12 +36,17 @@ #define _D_DEFAULT_BT_INTERACTIVE_H_ #include "BtInteractive.h" -#include "BtContextAwareCommand.h" -#include "TimeA2.h" + #include +#include "TimeA2.h" + namespace aria2 { +class BtContext; +class BtRuntime; +class PieceStorage; +class PeerStorage; class Peer; class BtMessage; class BtMessageReceiver; @@ -49,6 +54,7 @@ class BtMessageDispatcher; class BtMessageFactory; class BtRequestFactory; class PeerConnection; +class ExtensionMessageFactory; class DHTNode; class Logger; @@ -85,16 +91,26 @@ public: } }; -class DefaultBtInteractive : public BtInteractive, public BtContextAwareCommand { +class DefaultBtInteractive : public BtInteractive { private: int32_t cuid; + + SharedHandle _btContext; + + SharedHandle _btRuntime; + + SharedHandle _pieceStorage; + + SharedHandle _peerStorage; + SharedHandle peer; - WeakHandle btMessageReceiver; - WeakHandle dispatcher; - WeakHandle btRequestFactory; - WeakHandle peerConnection; - WeakHandle messageFactory; + SharedHandle btMessageReceiver; + SharedHandle dispatcher; + SharedHandle btRequestFactory; + SharedHandle peerConnection; + SharedHandle messageFactory; + SharedHandle _extensionMessageFactory; WeakHandle _localNode; @@ -163,17 +179,26 @@ public: this->cuid = cuid; } + void setBtRuntime(const SharedHandle& btRuntime); + + void setPieceStorage(const SharedHandle& pieceStorage); + + void setPeerStorage(const SharedHandle& peerStorage); + void setPeer(const SharedHandle& peer); - void setBtMessageReceiver(const WeakHandle& receiver); + void setBtMessageReceiver(const SharedHandle& receiver); - void setDispatcher(const WeakHandle& dispatcher); + void setDispatcher(const SharedHandle& dispatcher); - void setBtRequestFactory(const WeakHandle& factory); + void setBtRequestFactory(const SharedHandle& factory); - void setPeerConnection(const WeakHandle& peerConnection); + void setPeerConnection(const SharedHandle& peerConnection); - void setBtMessageFactory(const WeakHandle& factory); + void setBtMessageFactory(const SharedHandle& factory); + + void setExtensionMessageFactory + (const SharedHandle& factory); void setKeepAliveInterval(time_t keepAliveInterval) { this->keepAliveInterval = keepAliveInterval; diff --git a/src/DefaultBtMessageDispatcher.cc b/src/DefaultBtMessageDispatcher.cc index 55980bc0..07d54722 100644 --- a/src/DefaultBtMessageDispatcher.cc +++ b/src/DefaultBtMessageDispatcher.cc @@ -33,6 +33,9 @@ */ /* copyright --> */ #include "DefaultBtMessageDispatcher.h" + +#include + #include "prefs.h" #include "BtAbortOutstandingRequestEvent.h" #include "BtCancelSendingPieceEvent.h" @@ -44,13 +47,11 @@ #include "PeerStorage.h" #include "PieceStorage.h" #include "BtMessage.h" -#include "BtRegistry.h" #include "Peer.h" #include "Piece.h" #include "LogFactory.h" #include "Logger.h" #include "a2functional.h" -#include namespace aria2 { @@ -86,7 +87,7 @@ void DefaultBtMessageDispatcher::sendMessages() { messageQueue.pop_front(); if(maxUploadSpeedLimit > 0 && msg->isUploading() && !msg->isSendingInProgress()) { - TransferStat stat = peerStorage->calculateStat(); + TransferStat stat = _peerStorage->calculateStat(); if(maxUploadSpeedLimit < stat.getUploadSpeed()) { tempQueue.push_back(msg); continue; @@ -228,7 +229,7 @@ public: void DefaultBtMessageDispatcher::doChokedAction() { std::for_each(requestSlots.begin(), requestSlots.end(), - ProcessChokedRequestSlot(cuid, peer, pieceStorage)); + ProcessChokedRequestSlot(cuid, peer, _pieceStorage)); requestSlots.erase(std::remove_if(requestSlots.begin(), requestSlots.end(), FindChokedRequestSlot(peer)), @@ -332,13 +333,13 @@ void DefaultBtMessageDispatcher::checkRequestSlotAndDoNecessaryThing() std::for_each(requestSlots.begin(), requestSlots.end(), ProcessStaleRequestSlot(cuid, peer, - pieceStorage, + _pieceStorage, this, messageFactory, now, requestTimeout)); requestSlots.erase(std::remove_if(requestSlots.begin(), requestSlots.end(), - FindStaleRequestSlot(pieceStorage, + FindStaleRequestSlot(_pieceStorage, now, requestTimeout)), requestSlots.end()); @@ -437,8 +438,18 @@ void DefaultBtMessageDispatcher::setPeer(const SharedHandle& peer) void DefaultBtMessageDispatcher::setBtContext(const BtContextHandle& btContext) { this->btContext = btContext; - this->pieceStorage = PIECE_STORAGE(btContext); - this->peerStorage = PEER_STORAGE(btContext); +} + +void DefaultBtMessageDispatcher::setPieceStorage +(const SharedHandle& pieceStorage) +{ + _pieceStorage = pieceStorage; +} + +void DefaultBtMessageDispatcher::setPeerStorage +(const SharedHandle& peerStorage) +{ + _peerStorage = peerStorage; } void DefaultBtMessageDispatcher::setBtMessageFactory(const WeakHandle& factory) diff --git a/src/DefaultBtMessageDispatcher.h b/src/DefaultBtMessageDispatcher.h index bfd3925a..678bfa5f 100644 --- a/src/DefaultBtMessageDispatcher.h +++ b/src/DefaultBtMessageDispatcher.h @@ -55,8 +55,8 @@ private: std::deque > messageQueue; std::deque requestSlots; SharedHandle btContext; - SharedHandle peerStorage; - SharedHandle pieceStorage; + SharedHandle _peerStorage; + SharedHandle _pieceStorage; WeakHandle messageFactory; SharedHandle peer; unsigned int maxUploadSpeedLimit; @@ -111,6 +111,10 @@ public: void setBtContext(const SharedHandle& btContext); + void setPieceStorage(const SharedHandle& pieceStorage); + + void setPeerStorage(const SharedHandle& peerStorage); + void setBtMessageFactory(const WeakHandle& factory); void setCuid(int32_t cuid) { diff --git a/src/DefaultBtMessageFactory.cc b/src/DefaultBtMessageFactory.cc index b35f8df2..6cba336f 100644 --- a/src/DefaultBtMessageFactory.cc +++ b/src/DefaultBtMessageFactory.cc @@ -65,10 +65,11 @@ #include "ExtensionMessage.h" #include "Peer.h" #include "Piece.h" -#include "BtRegistry.h" #include "BtContext.h" #include "PieceStorage.h" +#include "PeerStorage.h" #include "StringFormat.h" +#include "ExtensionMessageFactory.h" namespace aria2 { @@ -98,7 +99,12 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL msg = BtInterestedMessage::create(data, dataLength); break; case BtNotInterestedMessage::ID: - msg = BtNotInterestedMessage::create(data, dataLength); + { + SharedHandle m = + BtNotInterestedMessage::create(data, dataLength); + m->setPeerStorage(_peerStorage); + msg = m; + } break; case BtHaveMessage::ID: msg = BtHaveMessage::create(data, dataLength); @@ -123,7 +129,7 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL BtMessageValidatorHandle validator (new BtRequestMessageValidator(temp.get(), btContext->getNumPieces(), - pieceStorage->getPieceLength(temp->getIndex()))); + _pieceStorage->getPieceLength(temp->getIndex()))); temp->setBtMessageValidator(validator); msg = temp; break; @@ -133,7 +139,7 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL BtMessageValidatorHandle validator (new BtCancelMessageValidator(temp.get(), btContext->getNumPieces(), - pieceStorage->getPieceLength(temp->getIndex()))); + _pieceStorage->getPieceLength(temp->getIndex()))); temp->setBtMessageValidator(validator); msg = temp; break; @@ -143,7 +149,7 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL BtMessageValidatorHandle validator (new BtPieceMessageValidator(temp.get(), btContext->getNumPieces(), - pieceStorage->getPieceLength(temp->getIndex()))); + _pieceStorage->getPieceLength(temp->getIndex()))); temp->setBtMessageValidator(validator); msg = temp; break; @@ -159,7 +165,7 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL BtMessageValidatorHandle validator (new BtRejectMessageValidator(temp.get(), btContext->getNumPieces(), - pieceStorage->getPieceLength(temp->getIndex()))); + _pieceStorage->getPieceLength(temp->getIndex()))); temp->setBtMessageValidator(validator); msg = temp; break; @@ -193,7 +199,8 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL } case BtExtendedMessage::ID: { if(peer->isExtendedMessagingEnabled()) { - msg = BtExtendedMessage::create(btContext, peer, data, dataLength); + msg = BtExtendedMessage::create(_extensionMessageFactory, + peer, data, dataLength); } else { throw DlAbortEx("Received extended message from peer during a session with extended messaging disabled."); } @@ -211,6 +218,7 @@ void DefaultBtMessageFactory::setCommonProperty(const AbstractBtMessageHandle& m msg->setCuid(cuid); msg->setPeer(peer); msg->setBtContext(btContext); + msg->setPieceStorage(_pieceStorage); msg->setBtMessageDispatcher(dispatcher); msg->setBtRequestFactory(requestFactory); msg->setBtMessageFactory(WeakHandle(this)); @@ -254,7 +262,7 @@ DefaultBtMessageFactory::createRequestMessage(const PieceHandle& piece, size_t b BtMessageValidatorHandle validator (new BtRequestMessageValidator(msg.get(), btContext->getNumPieces(), - pieceStorage->getPieceLength(msg->getIndex()))); + _pieceStorage->getPieceLength(msg->getIndex()))); msg->setBtMessageValidator(validator); setCommonProperty(msg); return msg; @@ -267,7 +275,7 @@ DefaultBtMessageFactory::createCancelMessage(size_t index, uint32_t begin, size_ BtMessageValidatorHandle validator (new BtCancelMessageValidator(msg.get(), btContext->getNumPieces(), - pieceStorage->getPieceLength(index))); + _pieceStorage->getPieceLength(index))); msg->setBtMessageValidator(validator); setCommonProperty(msg); return msg; @@ -280,7 +288,7 @@ DefaultBtMessageFactory::createPieceMessage(size_t index, uint32_t begin, size_t BtMessageValidatorHandle validator (new BtPieceMessageValidator(msg.get(), btContext->getNumPieces(), - pieceStorage->getPieceLength(index))); + _pieceStorage->getPieceLength(index))); msg->setBtMessageValidator(validator); setCommonProperty(msg); return msg; @@ -334,8 +342,8 @@ BtMessageHandle DefaultBtMessageFactory::createBitfieldMessage() { BtBitfieldMessageHandle msg - (new BtBitfieldMessage(pieceStorage->getBitfield(), - pieceStorage->getBitfieldLength())); + (new BtBitfieldMessage(_pieceStorage->getBitfield(), + _pieceStorage->getBitfieldLength())); SharedHandle v (new BtBitfieldMessageValidator(msg.get(), btContext->getNumPieces())); @@ -375,7 +383,7 @@ DefaultBtMessageFactory::createRejectMessage(size_t index, uint32_t begin, size_ BtMessageValidatorHandle validator (new BtRejectMessageValidator(msg.get(), btContext->getNumPieces(), - pieceStorage->getPieceLength(index))); + _pieceStorage->getPieceLength(index))); msg->setBtMessageValidator(validator); setCommonProperty(msg); return msg; @@ -427,7 +435,18 @@ void DefaultBtMessageFactory::setPeer(const SharedHandle& peer) void DefaultBtMessageFactory::setBtContext(const SharedHandle& btContext) { this->btContext = btContext; - this->pieceStorage = PIECE_STORAGE(btContext); +} + +void DefaultBtMessageFactory::setPieceStorage +(const SharedHandle& pieceStorage) +{ + _pieceStorage = pieceStorage; +} + +void DefaultBtMessageFactory::setPeerStorage +(const SharedHandle& peerStorage) +{ + _peerStorage = peerStorage; } void DefaultBtMessageFactory::setBtMessageDispatcher(const WeakHandle& dispatcher) @@ -435,6 +454,12 @@ void DefaultBtMessageFactory::setBtMessageDispatcher(const WeakHandledispatcher = dispatcher; } +void DefaultBtMessageFactory::setExtensionMessageFactory +(const SharedHandle& factory) +{ + _extensionMessageFactory = factory; +} + void DefaultBtMessageFactory::setLocalNode(const WeakHandle& localNode) { _localNode = localNode; diff --git a/src/DefaultBtMessageFactory.h b/src/DefaultBtMessageFactory.h index d0de2dd4..c2ef06fc 100644 --- a/src/DefaultBtMessageFactory.h +++ b/src/DefaultBtMessageFactory.h @@ -40,12 +40,14 @@ namespace aria2 { class BtContext; +class PieceStorage; +class PeerStorage; class Peer; class AbstractBtMessage; class BtMessageDispatcher; class BtRequestFactory; class PeerConnection; -class PieceStorage; +class ExtensionMessageFactory; class DHTNode; class DHTRoutingTable; class DHTTaskQueue; @@ -55,7 +57,8 @@ class DefaultBtMessageFactory : public BtMessageFactory { private: int32_t cuid; SharedHandle btContext; - SharedHandle pieceStorage; + SharedHandle _pieceStorage; + SharedHandle _peerStorage; SharedHandle peer; bool _dhtEnabled; @@ -66,6 +69,8 @@ private: WeakHandle peerConnection; + SharedHandle _extensionMessageFactory; + WeakHandle _localNode; WeakHandle _routingTable; @@ -131,6 +136,10 @@ public: void setBtContext(const SharedHandle& btContext); + void setPieceStorage(const SharedHandle& pieceStorage); + + void setPeerStorage(const SharedHandle& peerStorage); + void setCuid(int32_t cuid) { this->cuid = cuid; } @@ -145,6 +154,9 @@ public: void setPeerConnection(const WeakHandle& connection); + void setExtensionMessageFactory + (const SharedHandle& factory); + void setLocalNode(const WeakHandle& localNode); void setRoutingTable(const WeakHandle& routingTable); diff --git a/src/DefaultBtMessageReceiver.cc b/src/DefaultBtMessageReceiver.cc index 973beaf3..d6afbb70 100644 --- a/src/DefaultBtMessageReceiver.cc +++ b/src/DefaultBtMessageReceiver.cc @@ -33,17 +33,18 @@ */ /* copyright --> */ #include "DefaultBtMessageReceiver.h" + +#include + #include "BtHandshakeMessage.h" #include "message.h" #include "BtContext.h" -#include "BtRegistry.h" #include "Peer.h" #include "PeerConnection.h" #include "BtMessageDispatcher.h" #include "BtMessageFactory.h" #include "Logger.h" #include "LogFactory.h" -#include namespace aria2 { diff --git a/src/DefaultBtProgressInfoFile.cc b/src/DefaultBtProgressInfoFile.cc index eb93a3ed..31286de9 100644 --- a/src/DefaultBtProgressInfoFile.cc +++ b/src/DefaultBtProgressInfoFile.cc @@ -33,6 +33,11 @@ */ /* copyright --> */ #include "DefaultBtProgressInfoFile.h" + +#include +#include +#include + #include "BtContext.h" #include "PieceStorage.h" #include "Piece.h" @@ -41,7 +46,6 @@ #include "BitfieldMan.h" #include "Option.h" #include "TransferStat.h" -#include "BtRegistry.h" #include "LogFactory.h" #include "Logger.h" #include "prefs.h" @@ -52,9 +56,6 @@ #include "a2io.h" #include "DownloadFailureException.h" #include "StringFormat.h" -#include -#include -#include namespace aria2 { @@ -86,7 +87,7 @@ void DefaultBtProgressInfoFile::updateFilename() bool DefaultBtProgressInfoFile::isTorrentDownload() { - return !dynamic_pointer_cast(_dctx).isNull(); + return !_btRuntime.isNull(); } // Since version 0001, Integers are saved in binary form, network byte order. @@ -138,8 +139,7 @@ void DefaultBtProgressInfoFile::save() { // uploadLength: 64 bits uint64_t uploadLengthNL = 0; if(torrentDownload) { - BtContextHandle btContext(dynamic_pointer_cast(_dctx)); - TransferStat stat = PEER_STORAGE(btContext)->calculateStat(); + TransferStat stat = _peerStorage->calculateStat(); uploadLengthNL = hton64(stat.getAllTimeUploadLength()); } o.write(reinterpret_cast(&uploadLengthNL), @@ -267,8 +267,7 @@ void DefaultBtProgressInfoFile::load() uploadLength = ntoh64(uploadLength); } if(isTorrentDownload()) { - BT_RUNTIME(dynamic_pointer_cast(_dctx))-> - setUploadLengthAtStartup(uploadLength); + _btRuntime->setUploadLengthAtStartup(uploadLength); } // TODO implement the conversion mechanism between different piece length. @@ -401,4 +400,16 @@ bool DefaultBtProgressInfoFile::exists() } } +void DefaultBtProgressInfoFile::setPeerStorage +(const SharedHandle& peerStorage) +{ + _peerStorage = peerStorage; +} + +void DefaultBtProgressInfoFile::setBtRuntime +(const SharedHandle& btRuntime) +{ + _btRuntime = btRuntime; +} + } // namespace aria2 diff --git a/src/DefaultBtProgressInfoFile.h b/src/DefaultBtProgressInfoFile.h index 72757387..8ea84df2 100644 --- a/src/DefaultBtProgressInfoFile.h +++ b/src/DefaultBtProgressInfoFile.h @@ -41,6 +41,8 @@ namespace aria2 { class DownloadContext; class PieceStorage; +class PeerStorage; +class BtRuntime; class Logger; class Option; @@ -48,6 +50,9 @@ class DefaultBtProgressInfoFile : public BtProgressInfoFile { private: SharedHandle _dctx; SharedHandle _pieceStorage; + SharedHandle _peerStorage; + SharedHandle _btRuntime; + const Option* _option; Logger* _logger; std::string _filename; @@ -76,6 +81,10 @@ public: // re-set filename using current _dctx. virtual void updateFilename(); + // for torrents + void setPeerStorage(const SharedHandle& peerStorage); + + void setBtRuntime(const SharedHandle& btRuntime); }; } // namespace aria2 diff --git a/src/DefaultBtRequestFactory.cc b/src/DefaultBtRequestFactory.cc index 6227b799..87fbd411 100644 --- a/src/DefaultBtRequestFactory.cc +++ b/src/DefaultBtRequestFactory.cc @@ -33,6 +33,9 @@ */ /* copyright --> */ #include "DefaultBtRequestFactory.h" + +#include + #include "LogFactory.h" #include "Logger.h" #include "Piece.h" @@ -42,9 +45,7 @@ #include "BtMessageDispatcher.h" #include "BtMessageFactory.h" #include "BtMessage.h" -#include "BtRegistry.h" #include "a2functional.h" -#include namespace aria2 { @@ -93,7 +94,7 @@ void DefaultBtRequestFactory::removeTargetPiece(const PieceHandle& piece) { pieces.erase(std::remove(pieces.begin(), pieces.end(), piece), pieces.end()); dispatcher->doAbortOutstandingRequestAction(piece); - pieceStorage->cancelPiece(piece); + _pieceStorage->cancelPiece(piece); } class ProcessChokedPiece { @@ -129,7 +130,7 @@ public: void DefaultBtRequestFactory::doChokedAction() { std::for_each(pieces.begin(), pieces.end(), - ProcessChokedPiece(peer, pieceStorage)); + ProcessChokedPiece(peer, _pieceStorage)); pieces.erase(std::remove_if(pieces.begin(), pieces.end(), FindChokedPiece(peer)), pieces.end()); @@ -138,7 +139,7 @@ void DefaultBtRequestFactory::doChokedAction() void DefaultBtRequestFactory::removeAllTargetPiece() { for(Pieces::iterator itr = pieces.begin(); itr != pieces.end(); ++itr) { dispatcher->doAbortOutstandingRequestAction(*itr); - pieceStorage->cancelPiece(*itr); + _pieceStorage->cancelPiece(*itr); } pieces.clear(); } @@ -225,7 +226,12 @@ std::deque >& DefaultBtRequestFactory::getTargetPieces() void DefaultBtRequestFactory::setBtContext(const SharedHandle& btContext) { this->btContext = btContext; - this->pieceStorage = PIECE_STORAGE(btContext); +} + +void DefaultBtRequestFactory::setPieceStorage +(const SharedHandle& pieceStorage) +{ + _pieceStorage = pieceStorage; } void DefaultBtRequestFactory::setPeer(const SharedHandle& peer) diff --git a/src/DefaultBtRequestFactory.h b/src/DefaultBtRequestFactory.h index 5067138d..89e1096c 100644 --- a/src/DefaultBtRequestFactory.h +++ b/src/DefaultBtRequestFactory.h @@ -51,7 +51,7 @@ class DefaultBtRequestFactory : public BtRequestFactory { private: int32_t cuid; SharedHandle btContext; - SharedHandle pieceStorage; + SharedHandle _pieceStorage; SharedHandle peer; WeakHandle dispatcher; WeakHandle messageFactory; @@ -95,6 +95,8 @@ public: void setBtContext(const SharedHandle& btContext); + void setPieceStorage(const SharedHandle& pieceStorage); + void setPeer(const SharedHandle& peer); void setBtMessageDispatcher(const WeakHandle& dispatcher); diff --git a/src/DefaultExtensionMessageFactory.cc b/src/DefaultExtensionMessageFactory.cc index 2295a70e..b8ccee7e 100644 --- a/src/DefaultExtensionMessageFactory.cc +++ b/src/DefaultExtensionMessageFactory.cc @@ -40,8 +40,8 @@ #include "UTPexExtensionMessage.h" #include "LogFactory.h" #include "Logger.h" -#include "BtRegistry.h" #include "StringFormat.h" +#include "PeerStorage.h" namespace aria2 { @@ -76,8 +76,8 @@ DefaultExtensionMessageFactory::createMessage(const unsigned char* data, size_t if(extensionName == "ut_pex") { // uTorrent compatible Peer-Exchange UTPexExtensionMessageHandle m = - UTPexExtensionMessage::create(_btContext, data, length); - m->setBtContext(_btContext); + UTPexExtensionMessage::create(data, length); + m->setPeerStorage(_peerStorage); return m; } else { throw DlAbortEx @@ -92,6 +92,12 @@ void DefaultExtensionMessageFactory::setBtContext(const BtContextHandle& btConte _btContext = btContext; } +void DefaultExtensionMessageFactory::setPeerStorage +(const SharedHandle& peerStorage) +{ + _peerStorage = peerStorage; +} + void DefaultExtensionMessageFactory::setPeer(const PeerHandle& peer) { _peer = peer; diff --git a/src/DefaultExtensionMessageFactory.h b/src/DefaultExtensionMessageFactory.h index 6bbc45ef..e1cef02f 100644 --- a/src/DefaultExtensionMessageFactory.h +++ b/src/DefaultExtensionMessageFactory.h @@ -40,6 +40,7 @@ namespace aria2 { class BtContext; +class PeerStorage; class Peer; class Logger; @@ -47,6 +48,8 @@ class DefaultExtensionMessageFactory:public ExtensionMessageFactory { private: SharedHandle _btContext; + SharedHandle _peerStorage; + SharedHandle _peer; Logger* _logger; @@ -64,6 +67,8 @@ public: void setBtContext(const SharedHandle& btContext); + void setPeerStorage(const SharedHandle& peerStorage); + void setPeer(const SharedHandle& peer); }; diff --git a/src/DefaultPeerStorage.cc b/src/DefaultPeerStorage.cc index 3167c1e0..90b9fad0 100644 --- a/src/DefaultPeerStorage.cc +++ b/src/DefaultPeerStorage.cc @@ -33,9 +33,11 @@ */ /* copyright --> */ #include "DefaultPeerStorage.h" + +#include + #include "LogFactory.h" #include "Logger.h" -#include "BtRegistry.h" #include "message.h" #include "a2time.h" #include "Peer.h" @@ -44,7 +46,6 @@ #include "BtSeederStateChoke.h" #include "BtLeecherStateChoke.h" #include "PieceStorage.h" -#include namespace aria2 { @@ -53,7 +54,6 @@ DefaultPeerStorage::DefaultPeerStorage(const BtContextHandle& btContext, btContext(btContext), option(option), logger(LogFactory::getInstance()), - btRuntime(BT_RUNTIME(btContext)), maxPeerListSize(BtRuntime::MAX_PEERS+(BtRuntime::MAX_PEERS >> 2)), removedPeerSessionDownloadLength(0), removedPeerSessionUploadLength(0), @@ -206,7 +206,7 @@ TransferStat DefaultPeerStorage::calculateStat() { TransferStat stat = std::for_each(peers.begin(), peers.end(), CalculateStat()).getTransferStat(); stat.sessionDownloadLength += removedPeerSessionDownloadLength; stat.sessionUploadLength += removedPeerSessionUploadLength; - stat.setAllTimeUploadLength(btRuntime->getUploadLengthAtStartup()+ + stat.setAllTimeUploadLength(_btRuntime->getUploadLengthAtStartup()+ stat.getSessionUploadLength()); return stat; } @@ -252,7 +252,7 @@ void DefaultPeerStorage::returnPeer(const PeerHandle& peer) bool DefaultPeerStorage::chokeRoundIntervalElapsed() { const time_t CHOKE_ROUND_INTERVAL = 10; - if(PIECE_STORAGE(btContext)->downloadFinished()) { + if(_pieceStorage->downloadFinished()) { return _seederStateChoke->getLastRound().elapsed(CHOKE_ROUND_INTERVAL); } else { return _leecherStateChoke->getLastRound().elapsed(CHOKE_ROUND_INTERVAL); @@ -263,11 +263,21 @@ void DefaultPeerStorage::executeChoke() { std::deque > activePeers; getActivePeers(activePeers); - if(PIECE_STORAGE(btContext)->downloadFinished()) { + if(_pieceStorage->downloadFinished()) { return _seederStateChoke->executeChoke(activePeers); } else { return _leecherStateChoke->executeChoke(activePeers); } } +void DefaultPeerStorage::setPieceStorage(const SharedHandle& ps) +{ + _pieceStorage = ps; +} + +void DefaultPeerStorage::setBtRuntime(const SharedHandle& btRuntime) +{ + _btRuntime = btRuntime; +} + } // namespace aria2 diff --git a/src/DefaultPeerStorage.h b/src/DefaultPeerStorage.h index b8d6c9bb..228c9144 100644 --- a/src/DefaultPeerStorage.h +++ b/src/DefaultPeerStorage.h @@ -45,14 +45,16 @@ class Logger; class BtRuntime; class BtSeederStateChoke; class BtLeecherStateChoke; +class PieceStorage; class DefaultPeerStorage : public PeerStorage { private: SharedHandle btContext; + SharedHandle _btRuntime; + SharedHandle _pieceStorage; const Option* option; std::deque > peers; Logger* logger; - SharedHandle btRuntime; size_t maxPeerListSize; uint64_t removedPeerSessionDownloadLength; uint64_t removedPeerSessionUploadLength; @@ -67,12 +69,6 @@ public: virtual ~DefaultPeerStorage(); - void setBtRuntime(const SharedHandle& btRuntime) { - this->btRuntime = btRuntime; - } - - SharedHandle getBtRuntime() const { return btRuntime; } - virtual bool addPeer(const SharedHandle& peer); size_t countPeer() const; @@ -107,6 +103,9 @@ public: void onReturningPeer(const SharedHandle& peer); + void setPieceStorage(const SharedHandle& pieceStorage); + + void setBtRuntime(const SharedHandle& btRuntime); }; } // namespace aria2 diff --git a/src/DownloadEngine.cc b/src/DownloadEngine.cc index 1ea23f07..1d6247eb 100644 --- a/src/DownloadEngine.cc +++ b/src/DownloadEngine.cc @@ -33,6 +33,14 @@ */ /* copyright --> */ #include "DownloadEngine.h" + +#include + +#include +#include +#include +#include + #ifdef ENABLE_ASYNC_DNS #include "AsyncNameResolver.h" #endif // ENABLE_ASYNC_DNS @@ -54,11 +62,14 @@ #include "ServerStatMan.h" #include "CookieStorage.h" #include "A2STR.h" -#include -#include -#include -#include -#include + +#include "BtRegistry.h" +#include "BtContext.h" +#include "PeerStorage.h" +#include "PieceStorage.h" +#include "BtAnnounce.h" +#include "BtRuntime.h" +#include "BtProgressInfoFile.h" namespace aria2 { @@ -399,7 +410,8 @@ void AsyncNameResolverEntry::process(fd_set* rfdsPtr, fd_set* wfdsPtr) DownloadEngine::DownloadEngine():logger(LogFactory::getInstance()), _haltRequested(false), _noWait(false), - _cookieStorage(new CookieStorage()) + _cookieStorage(new CookieStorage()), + _btRegistry(new BtRegistry()) { #ifdef HAVE_EPOLL @@ -1016,6 +1028,11 @@ DownloadEngine::popPooledSocket return s; } +SharedHandle DownloadEngine::getBtRegistry() const +{ + return _btRegistry; +} + DownloadEngine::SocketPoolEntry::SocketPoolEntry (const SharedHandle& socket, const std::map& options, diff --git a/src/DownloadEngine.h b/src/DownloadEngine.h index 8a20493e..21421e22 100644 --- a/src/DownloadEngine.h +++ b/src/DownloadEngine.h @@ -36,6 +36,15 @@ #define _D_DOWNLOAD_ENGINE_H_ #include "common.h" + +#ifdef HAVE_EPOLL_CREATE +# include +#endif // HAVE_EPOLL_CREATE + +#include +#include +#include + #include "SharedHandle.h" #include "Command.h" #include "a2netcompat.h" @@ -44,12 +53,6 @@ #ifdef ENABLE_ASYNC_DNS # include "AsyncNameResolver.h" #endif // ENABLE_ASYNC_DNS -#include -#include -#include -#ifdef HAVE_EPOLL -# include -#endif // HAVE_EPOLL namespace aria2 { @@ -61,6 +64,7 @@ class StatCalc; class CheckIntegrityMan; class SocketCore; class CookieStorage; +class BtRegistry; class CommandEvent { @@ -292,6 +296,8 @@ private: SharedHandle _cookieStorage; + SharedHandle _btRegistry; + void shortSleep() const; /** @@ -405,6 +411,8 @@ public: uint16_t port); SharedHandle getCookieStorage() const; + + SharedHandle getBtRegistry() const; }; typedef SharedHandle DownloadEngineHandle; diff --git a/src/InitiatorMSEHandshakeCommand.cc b/src/InitiatorMSEHandshakeCommand.cc index eb3511aa..00174d2a 100644 --- a/src/InitiatorMSEHandshakeCommand.cc +++ b/src/InitiatorMSEHandshakeCommand.cc @@ -46,10 +46,8 @@ #include "PeerConnection.h" #include "BtContext.h" #include "BtRuntime.h" -#include "PieceStorage.h" #include "PeerStorage.h" -#include "BtAnnounce.h" -#include "BtProgressInfoFile.h" +#include "PieceStorage.h" #include "Option.h" #include "MSEHandshake.h" #include "ARC4Encryptor.h" @@ -63,11 +61,13 @@ InitiatorMSEHandshakeCommand::InitiatorMSEHandshakeCommand const SharedHandle& p, DownloadEngine* e, const SharedHandle& btContext, + const SharedHandle& btRuntime, const SharedHandle& s): PeerAbstractCommand(cuid, p, e, s), - BtContextAwareCommand(btContext), RequestGroupAware(requestGroup), + _btContext(btContext), + _btRuntime(btRuntime), _sequence(INITIATOR_SEND_KEY), _mseHandshake(new MSEHandshake(cuid, socket, e->option)) { @@ -75,12 +75,12 @@ InitiatorMSEHandshakeCommand::InitiatorMSEHandshakeCommand setWriteCheckSocket(socket); setTimeout(e->option->getAsInt(PREF_PEER_CONNECTION_TIMEOUT)); - btRuntime->increaseConnections(); + _btRuntime->increaseConnections(); } InitiatorMSEHandshakeCommand::~InitiatorMSEHandshakeCommand() { - btRuntime->decreaseConnections(); + _btRuntime->decreaseConnections(); delete _mseHandshake; } @@ -112,7 +112,7 @@ bool InitiatorMSEHandshakeCommand::executeInternal() { break; case INITIATOR_WAIT_KEY: { if(_mseHandshake->receivePublicKey()) { - _mseHandshake->initCipher(btContext->getInfoHash()); + _mseHandshake->initCipher(_btContext->getInfoHash()); if(_mseHandshake->sendInitiatorStep2()) { _sequence = INITIATOR_FIND_VC_MARKER; } else { @@ -148,11 +148,13 @@ bool InitiatorMSEHandshakeCommand::executeInternal() { peerConnection->enableEncryption(_mseHandshake->getEncryptor(), _mseHandshake->getDecryptor()); } - Command* c = - new PeerInteractionCommand(cuid, _requestGroup, peer, e, btContext, + PeerInteractionCommand* c = + new PeerInteractionCommand(cuid, _requestGroup, peer, e, _btContext, + _btRuntime, _pieceStorage, socket, PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE, peerConnection); + c->setPeerStorage(_peerStorage); e->commands.push_back(c); return true; } @@ -167,21 +169,25 @@ bool InitiatorMSEHandshakeCommand::prepareForNextPeer(time_t wait) { if(e->option->getAsBool(PREF_BT_REQUIRE_CRYPTO)) { logger->info("CUID#%d - Establishing connection using legacy BitTorrent handshake is disabled by preference.", cuid); - if(peerStorage->isPeerAvailable() && btRuntime->lessThanEqMinPeers()) { - SharedHandle peer = peerStorage->getUnusedPeer(); + if(_peerStorage->isPeerAvailable() && _btRuntime->lessThanEqMinPeers()) { + SharedHandle peer = _peerStorage->getUnusedPeer(); peer->usedBy(CUIDCounterSingletonHolder::instance()->newID()); - Command* command = - new PeerInitiateConnectionCommand(peer->usedBy(), _requestGroup, peer, e, - btContext); + PeerInitiateConnectionCommand* command = + new PeerInitiateConnectionCommand(peer->usedBy(), _requestGroup, peer, + e, _btContext, _btRuntime); + command->setPeerStorage(_peerStorage); + command->setPieceStorage(_pieceStorage); e->commands.push_back(command); } return true; } else { // try legacy BitTorrent handshake logger->info("CUID#%d - Retry using legacy BitTorrent handshake.", cuid); - Command* command = - new PeerInitiateConnectionCommand(cuid, _requestGroup, peer, e, btContext, - false); + PeerInitiateConnectionCommand* command = + new PeerInitiateConnectionCommand(cuid, _requestGroup, peer, e, + _btContext, _btRuntime, false); + command->setPeerStorage(_peerStorage); + command->setPieceStorage(_pieceStorage); e->commands.push_back(command); return true; } @@ -190,13 +196,25 @@ bool InitiatorMSEHandshakeCommand::prepareForNextPeer(time_t wait) void InitiatorMSEHandshakeCommand::onAbort() { if(e->option->getAsBool(PREF_BT_REQUIRE_CRYPTO)) { - peerStorage->returnPeer(peer); + _peerStorage->returnPeer(peer); } } bool InitiatorMSEHandshakeCommand::exitBeforeExecute() { - return btRuntime->isHalt(); + return _btRuntime->isHalt(); +} + +void InitiatorMSEHandshakeCommand::setPeerStorage +(const SharedHandle& peerStorage) +{ + _peerStorage = peerStorage; +} + +void InitiatorMSEHandshakeCommand::setPieceStorage +(const SharedHandle& pieceStorage) +{ + _pieceStorage = pieceStorage; } } // namespace aria2 diff --git a/src/InitiatorMSEHandshakeCommand.h b/src/InitiatorMSEHandshakeCommand.h index a9304d6d..4db8dd4a 100644 --- a/src/InitiatorMSEHandshakeCommand.h +++ b/src/InitiatorMSEHandshakeCommand.h @@ -37,14 +37,16 @@ #include "PeerAbstractCommand.h" #include "RequestGroupAware.h" -#include "BtContextAwareCommand.h" namespace aria2 { +class BtContext; +class PeerStorage; +class PieceStorage; +class BtRuntime; class MSEHandshake; class InitiatorMSEHandshakeCommand : public PeerAbstractCommand, - public BtContextAwareCommand, public RequestGroupAware { public: @@ -58,6 +60,14 @@ public: INITIATOR_RECEIVE_PAD_D, }; private: + SharedHandle _btContext; + + SharedHandle _peerStorage; + + SharedHandle _pieceStorage; + + SharedHandle _btRuntime; + Seq _sequence; MSEHandshake* _mseHandshake; protected: @@ -71,10 +81,14 @@ public: const SharedHandle& peer, DownloadEngine* e, const SharedHandle& btContext, + const SharedHandle& btRuntime, const SharedHandle& s); virtual ~InitiatorMSEHandshakeCommand(); + void setPeerStorage(const SharedHandle& peerStorage); + + void setPieceStorage(const SharedHandle& pieceStorage); }; } // namespace aria2 diff --git a/src/MSEHandshake.cc b/src/MSEHandshake.cc index 3cee3ef1..7eed5928 100644 --- a/src/MSEHandshake.cc +++ b/src/MSEHandshake.cc @@ -33,6 +33,10 @@ */ /* copyright --> */ #include "MSEHandshake.h" + +#include +#include + #include "message.h" #include "DlAbortEx.h" #include "LogFactory.h" @@ -46,13 +50,10 @@ #include "MessageDigestHelper.h" #include "SimpleRandomizer.h" #include "Util.h" -#include "BtRegistry.h" #include "BtContext.h" #include "prefs.h" #include "Option.h" #include "StringFormat.h" -#include -#include namespace aria2 { @@ -424,14 +425,14 @@ bool MSEHandshake::findReceiverHashMarker() return true; } -bool MSEHandshake::receiveReceiverHashAndPadCLength() +bool MSEHandshake::receiveReceiverHashAndPadCLength +(const std::deque >& btContexts) { size_t r = 20+VC_LENGTH+CRYPTO_BITFIELD_LENGTH+2/*PadC length*/-_rbufLength; if(r > receiveNBytes(r)) { return false; } // resolve info hash - std::deque > btContexts = BtRegistry::getAllBtContext(); // pointing to the position of HASH('req2', SKEY) xor HASH('req3', S) unsigned char* rbufptr = _rbuf; SharedHandle btContext; diff --git a/src/MSEHandshake.h b/src/MSEHandshake.h index 5b381c9f..6d4817f8 100644 --- a/src/MSEHandshake.h +++ b/src/MSEHandshake.h @@ -36,6 +36,9 @@ #define _D_MSE_HANDSHAKE_H_ #include "common.h" + +#include + #include "SharedHandle.h" #include "BtConstants.h" #include "SocketBuffer.h" @@ -48,6 +51,7 @@ class SocketCore; class DHKeyExchange; class ARC4Encryptor; class ARC4Decryptor; +class BtContext; class MSEHandshake { public: @@ -153,7 +157,8 @@ public: bool findReceiverHashMarker(); - bool receiveReceiverHashAndPadCLength(); + bool receiveReceiverHashAndPadCLength + (const std::deque >& btContexts); bool receiveReceiverIALength(); diff --git a/src/Makefile.am b/src/Makefile.am index 221ccbd3..cd8d3b59 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -252,7 +252,6 @@ SRCS += MetaEntry.h\ BtAnnounce.cc BtAnnounce.h\ DefaultBtAnnounce.cc DefaultBtAnnounce.h\ BtRuntime.h\ - BtContextAwareCommand.cc BtContextAwareCommand.h\ BtMessage.h\ AbstractBtMessage.cc AbstractBtMessage.h\ SimpleBtMessage.cc SimpleBtMessage.h\ diff --git a/src/Makefile.in b/src/Makefile.in index 258fdb97..83360def 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -81,7 +81,6 @@ bin_PROGRAMS = aria2c$(EXEEXT) @ENABLE_BITTORRENT_TRUE@ BtAnnounce.cc BtAnnounce.h\ @ENABLE_BITTORRENT_TRUE@ DefaultBtAnnounce.cc DefaultBtAnnounce.h\ @ENABLE_BITTORRENT_TRUE@ BtRuntime.h\ -@ENABLE_BITTORRENT_TRUE@ BtContextAwareCommand.cc BtContextAwareCommand.h\ @ENABLE_BITTORRENT_TRUE@ BtMessage.h\ @ENABLE_BITTORRENT_TRUE@ AbstractBtMessage.cc AbstractBtMessage.h\ @ENABLE_BITTORRENT_TRUE@ SimpleBtMessage.cc SimpleBtMessage.h\ @@ -446,9 +445,8 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \ DefaultBtContext.h PeerStorage.h DefaultPeerStorage.cc \ DefaultPeerStorage.h BtAnnounce.cc BtAnnounce.h \ DefaultBtAnnounce.cc DefaultBtAnnounce.h BtRuntime.h \ - BtContextAwareCommand.cc BtContextAwareCommand.h BtMessage.h \ - AbstractBtMessage.cc AbstractBtMessage.h SimpleBtMessage.cc \ - SimpleBtMessage.h BtAllowedFastMessage.cc \ + BtMessage.h AbstractBtMessage.cc AbstractBtMessage.h \ + SimpleBtMessage.cc SimpleBtMessage.h BtAllowedFastMessage.cc \ BtAllowedFastMessage.h BtBitfieldMessage.cc \ BtBitfieldMessage.h BtCancelMessage.cc BtCancelMessage.h \ BtChokeMessage.cc BtChokeMessage.h BtHaveAllMessage.cc \ @@ -608,7 +606,6 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \ @ENABLE_BITTORRENT_TRUE@ DefaultPeerStorage.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ BtAnnounce.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ DefaultBtAnnounce.$(OBJEXT) \ -@ENABLE_BITTORRENT_TRUE@ BtContextAwareCommand.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ AbstractBtMessage.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ SimpleBtMessage.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ BtAllowedFastMessage.$(OBJEXT) \ @@ -1261,7 +1258,6 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BtCheckIntegrityEntry.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BtChokeMessage.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BtContext.Po@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BtContextAwareCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BtDependency.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BtExtendedMessage.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BtFileAllocationEntry.Po@am__quote@ diff --git a/src/PeerChokeCommand.cc b/src/PeerChokeCommand.cc index d9205805..8799605f 100644 --- a/src/PeerChokeCommand.cc +++ b/src/PeerChokeCommand.cc @@ -42,23 +42,34 @@ namespace aria2 { PeerChokeCommand::PeerChokeCommand(int32_t cuid, DownloadEngine* e, - const BtContextHandle& btContext): + const SharedHandle& btContext): Command(cuid), - BtContextAwareCommand(btContext), - e(e) -{} + e(e), + _btContext(btContext) {} PeerChokeCommand::~PeerChokeCommand() {} bool PeerChokeCommand::execute() { - if(btRuntime->isHalt()) { + if(_btRuntime->isHalt()) { return true; } - if(peerStorage->chokeRoundIntervalElapsed()) { - peerStorage->executeChoke(); + if(_peerStorage->chokeRoundIntervalElapsed()) { + _peerStorage->executeChoke(); } e->commands.push_back(this); return false; } +void PeerChokeCommand::setBtRuntime +(const SharedHandle& btRuntime) +{ + _btRuntime = btRuntime; +} + +void PeerChokeCommand::setPeerStorage +(const SharedHandle& peerStorage) +{ + _peerStorage = peerStorage; +} + } // namespace aria2 diff --git a/src/PeerChokeCommand.h b/src/PeerChokeCommand.h index 23bb8764..b81781f3 100644 --- a/src/PeerChokeCommand.h +++ b/src/PeerChokeCommand.h @@ -36,18 +36,24 @@ #define _D_PEER_CHOKE_COMMAND_H_ #include "Command.h" -#include "BtContextAwareCommand.h" +#include "SharedHandle.h" namespace aria2 { class DownloadEngine; +class BtContext; +class BtRuntime; +class PeerStorage; -class PeerChokeCommand : public Command, - public BtContextAwareCommand -{ +class PeerChokeCommand : public Command { private: DownloadEngine* e; + SharedHandle _btContext; + + SharedHandle _peerStorage; + + SharedHandle _btRuntime; public: PeerChokeCommand(int32_t cuid, DownloadEngine* e, @@ -56,6 +62,10 @@ public: virtual ~PeerChokeCommand(); virtual bool execute(); + + void setPeerStorage(const SharedHandle& peerStorage); + + void setBtRuntime(const SharedHandle& btRuntime); }; } // namespace aria2 diff --git a/src/PeerInitiateConnectionCommand.cc b/src/PeerInitiateConnectionCommand.cc index a9570250..1c74eba5 100644 --- a/src/PeerInitiateConnectionCommand.cc +++ b/src/PeerInitiateConnectionCommand.cc @@ -43,33 +43,35 @@ #include "Socket.h" #include "Logger.h" #include "Peer.h" -#include "PeerConnection.h" #include "BtContext.h" #include "BtRuntime.h" -#include "PieceStorage.h" #include "PeerStorage.h" -#include "BtAnnounce.h" -#include "BtProgressInfoFile.h" +#include "PieceStorage.h" +#include "PeerConnection.h" namespace aria2 { -PeerInitiateConnectionCommand::PeerInitiateConnectionCommand(int cuid, - RequestGroup* requestGroup, - const PeerHandle& peer, - DownloadEngine* e, - const BtContextHandle& btContext, - bool mseHandshakeEnabled) - :PeerAbstractCommand(cuid, peer, e), - BtContextAwareCommand(btContext), - RequestGroupAware(requestGroup), - _mseHandshakeEnabled(mseHandshakeEnabled) +PeerInitiateConnectionCommand::PeerInitiateConnectionCommand +(int cuid, + RequestGroup* requestGroup, + const PeerHandle& peer, + DownloadEngine* e, + const SharedHandle& btContext, + const SharedHandle& btRuntime, + bool mseHandshakeEnabled) + : + PeerAbstractCommand(cuid, peer, e), + RequestGroupAware(requestGroup), + _btContext(btContext), + _btRuntime(btRuntime), + _mseHandshakeEnabled(mseHandshakeEnabled) { - btRuntime->increaseConnections(); + _btRuntime->increaseConnections(); } PeerInitiateConnectionCommand::~PeerInitiateConnectionCommand() { - btRuntime->decreaseConnections(); + _btRuntime->decreaseConnections(); } bool PeerInitiateConnectionCommand::executeInternal() { @@ -77,40 +79,58 @@ bool PeerInitiateConnectionCommand::executeInternal() { peer->port); socket.reset(new SocketCore()); socket->establishConnection(peer->ipaddr, peer->port); - Command* command; if(_mseHandshakeEnabled) { - command = - new InitiatorMSEHandshakeCommand(cuid, _requestGroup, peer, e, btContext, - socket); + InitiatorMSEHandshakeCommand* c = + new InitiatorMSEHandshakeCommand(cuid, _requestGroup, peer, e, _btContext, + _btRuntime, socket); + c->setPeerStorage(_peerStorage); + c->setPieceStorage(_pieceStorage); + e->commands.push_back(c); } else { - command = - new PeerInteractionCommand(cuid, _requestGroup, peer, e, btContext, socket, - PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE); + PeerInteractionCommand* command = + new PeerInteractionCommand + (cuid, _requestGroup, peer, e, _btContext, _btRuntime, _pieceStorage, + socket, PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE); + command->setPeerStorage(_peerStorage); + e->commands.push_back(command); } - e->commands.push_back(command); return true; } // TODO this method removed when PeerBalancerCommand is implemented bool PeerInitiateConnectionCommand::prepareForNextPeer(time_t wait) { - if(peerStorage->isPeerAvailable() && btRuntime->lessThanEqMinPeers()) { - PeerHandle peer = peerStorage->getUnusedPeer(); + if(_peerStorage->isPeerAvailable() && _btRuntime->lessThanEqMinPeers()) { + PeerHandle peer = _peerStorage->getUnusedPeer(); peer->usedBy(CUIDCounterSingletonHolder::instance()->newID()); - Command* command = + PeerInitiateConnectionCommand* command = new PeerInitiateConnectionCommand(peer->usedBy(), _requestGroup, peer, e, - btContext); + _btContext, _btRuntime); + command->setPeerStorage(_peerStorage); + command->setPieceStorage(_pieceStorage); e->commands.push_back(command); } return true; } void PeerInitiateConnectionCommand::onAbort() { - peerStorage->returnPeer(peer); + _peerStorage->returnPeer(peer); } bool PeerInitiateConnectionCommand::exitBeforeExecute() { - return btRuntime->isHalt(); + return _btRuntime->isHalt(); +} + +void PeerInitiateConnectionCommand::setPeerStorage +(const SharedHandle& peerStorage) +{ + _peerStorage = peerStorage; +} + +void PeerInitiateConnectionCommand::setPieceStorage +(const SharedHandle& pieceStorage) +{ + _pieceStorage = pieceStorage; } } // namespace aria2 diff --git a/src/PeerInitiateConnectionCommand.h b/src/PeerInitiateConnectionCommand.h index 04b58cc1..7a1eced0 100644 --- a/src/PeerInitiateConnectionCommand.h +++ b/src/PeerInitiateConnectionCommand.h @@ -37,15 +37,26 @@ #include "PeerAbstractCommand.h" #include "RequestGroupAware.h" -#include "BtContextAwareCommand.h" namespace aria2 { +class BtContext; +class BtRuntime; +class PeerStorage; +class PieceStorage; + class PeerInitiateConnectionCommand : public PeerAbstractCommand, - public BtContextAwareCommand, public RequestGroupAware { private: + SharedHandle _btContext; + + SharedHandle _btRuntime; + + SharedHandle _peerStorage; + + SharedHandle _pieceStorage; + bool _mseHandshakeEnabled; protected: virtual bool executeInternal(); @@ -59,9 +70,14 @@ public: const SharedHandle& peer, DownloadEngine* e, const SharedHandle& btContext, + const SharedHandle& btRuntime, bool mseHandshakeEnabled = true); virtual ~PeerInitiateConnectionCommand(); + + void setPeerStorage(const SharedHandle& peerStorage); + + void setPieceStorage(const SharedHandle& pieceStorage); }; } // namespace aria2 diff --git a/src/PeerInteractionCommand.cc b/src/PeerInteractionCommand.cc index a0324371..50d9c7e8 100644 --- a/src/PeerInteractionCommand.cc +++ b/src/PeerInteractionCommand.cc @@ -33,6 +33,9 @@ */ /* copyright --> */ #include "PeerInteractionCommand.h" + +#include + #include "DownloadEngine.h" #include "PeerInitiateConnectionCommand.h" #include "DefaultBtInteractive.h" @@ -43,7 +46,6 @@ #include "Option.h" #include "BtContext.h" #include "BtRegistry.h" -#include "PeerObject.h" #include "Peer.h" #include "BtMessage.h" #include "BtRuntime.h" @@ -64,21 +66,28 @@ #include "DHTRegistry.h" #include "PieceStorage.h" #include "RequestGroup.h" -#include +#include "BtAnnounce.h" +#include "BtProgressInfoFile.h" +#include "DefaultExtensionMessageFactory.h" namespace aria2 { -PeerInteractionCommand::PeerInteractionCommand(int32_t cuid, - RequestGroup* requestGroup, - const PeerHandle& p, - DownloadEngine* e, - const BtContextHandle& btContext, - const SocketHandle& s, - Seq sequence, - const PeerConnectionHandle& passedPeerConnection) +PeerInteractionCommand::PeerInteractionCommand +(int32_t cuid, + RequestGroup* requestGroup, + const PeerHandle& p, + DownloadEngine* e, + const SharedHandle& btContext, + const SharedHandle& btRuntime, + const SharedHandle& pieceStorage, + const SocketHandle& s, + Seq sequence, + const PeerConnectionHandle& passedPeerConnection) :PeerAbstractCommand(cuid, p, e, s), - BtContextAwareCommand(btContext), RequestGroupAware(requestGroup), + _btContext(btContext), + _btRuntime(btRuntime), + _pieceStorage(pieceStorage), sequence(sequence), maxDownloadSpeedLimit(0) { @@ -88,9 +97,21 @@ PeerInteractionCommand::PeerInteractionCommand(int32_t cuid, setWriteCheckSocket(socket); setTimeout(e->option->getAsInt(PREF_PEER_CONNECTION_TIMEOUT)); } - DefaultBtMessageFactoryHandle factory(new DefaultBtMessageFactory()); + + SharedHandle btRegistry = e->getBtRegistry(); + SharedHandle peerStorage = + btRegistry->getPeerStorage(_btContext->getInfoHashAsString()); + + SharedHandle extensionMessageFactory + (new DefaultExtensionMessageFactory(_btContext, peer)); + extensionMessageFactory->setPeerStorage(peerStorage); + + SharedHandle factory(new DefaultBtMessageFactory()); factory->setCuid(cuid); - factory->setBtContext(btContext); + factory->setBtContext(_btContext); + factory->setPieceStorage(pieceStorage); + factory->setPeerStorage(peerStorage); + factory->setExtensionMessageFactory(extensionMessageFactory); factory->setPeer(peer); factory->setLocalNode(DHTRegistry::_localNode); factory->setRoutingTable(DHTRegistry::_routingTable); @@ -104,39 +125,52 @@ PeerInteractionCommand::PeerInteractionCommand(int32_t cuid, peerConnection = passedPeerConnection; } - DefaultBtMessageDispatcherHandle dispatcher(new DefaultBtMessageDispatcher()); + SharedHandle dispatcher + (new DefaultBtMessageDispatcher()); dispatcher->setCuid(cuid); dispatcher->setPeer(peer); - dispatcher->setBtContext(btContext); - dispatcher->setMaxUploadSpeedLimit(e->option->getAsInt(PREF_MAX_UPLOAD_LIMIT)); + dispatcher->setBtContext(_btContext); + dispatcher->setPieceStorage(pieceStorage); + dispatcher->setPeerStorage(peerStorage); + dispatcher->setMaxUploadSpeedLimit + (e->option->getAsInt(PREF_MAX_UPLOAD_LIMIT)); dispatcher->setRequestTimeout(e->option->getAsInt(PREF_BT_REQUEST_TIMEOUT)); dispatcher->setBtMessageFactory(factory); DefaultBtMessageReceiverHandle receiver(new DefaultBtMessageReceiver()); receiver->setCuid(cuid); receiver->setPeer(peer); - receiver->setBtContext(btContext); + receiver->setBtContext(_btContext); receiver->setPeerConnection(peerConnection); receiver->setDispatcher(dispatcher); receiver->setBtMessageFactory(factory); - DefaultBtRequestFactoryHandle reqFactory(new DefaultBtRequestFactory()); + SharedHandle reqFactory + (new DefaultBtRequestFactory()); reqFactory->setCuid(cuid); reqFactory->setPeer(peer); - reqFactory->setBtContext(btContext); + reqFactory->setBtContext(_btContext); + reqFactory->setPieceStorage(pieceStorage); reqFactory->setBtMessageDispatcher(dispatcher); reqFactory->setBtMessageFactory(factory); - DefaultBtInteractiveHandle btInteractive(new DefaultBtInteractive(btContext, peer)); + DefaultBtInteractiveHandle btInteractive + (new DefaultBtInteractive(_btContext, peer)); + btInteractive->setBtRuntime(_btRuntime); + btInteractive->setPieceStorage(_pieceStorage); + btInteractive->setPeerStorage(peerStorage); // Note: Not a member variable. btInteractive->setCuid(cuid); btInteractive->setBtMessageReceiver(receiver); btInteractive->setDispatcher(dispatcher); btInteractive->setBtRequestFactory(reqFactory); btInteractive->setPeerConnection(peerConnection); - btInteractive->setKeepAliveInterval(e->option->getAsInt(PREF_BT_KEEP_ALIVE_INTERVAL)); - btInteractive->setMaxDownloadSpeedLimit(e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT)); + btInteractive->setExtensionMessageFactory(extensionMessageFactory); + btInteractive->setKeepAliveInterval + (e->option->getAsInt(PREF_BT_KEEP_ALIVE_INTERVAL)); + btInteractive->setMaxDownloadSpeedLimit + (e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT)); btInteractive->setBtMessageFactory(factory); - if(!btContext->isPrivate()) { + if(!_btContext->isPrivate()) { if(e->option->getAsBool(PREF_ENABLE_PEER_EXCHANGE)) { btInteractive->setUTPexEnabled(true); } @@ -153,32 +187,23 @@ PeerInteractionCommand::PeerInteractionCommand(int32_t cuid, factory->setBtRequestFactory(reqFactory); factory->setPeerConnection(peerConnection); - PeerObjectHandle peerObject(new PeerObject()); - peerObject->btMessageDispatcher = dispatcher; - peerObject->btMessageReceiver = receiver; - peerObject->btMessageFactory = factory; - peerObject->btRequestFactory = reqFactory; - peerObject->peerConnection = peerConnection; - - PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), peerObject); - setUploadLimit(e->option->getAsInt(PREF_MAX_UPLOAD_LIMIT)); - peer->allocateSessionResource(btContext->getPieceLength(), btContext->getTotalLength()); + peer->allocateSessionResource(_btContext->getPieceLength(), + _btContext->getTotalLength()); maxDownloadSpeedLimit = e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT); - btRuntime->increaseConnections(); + _btRuntime->increaseConnections(); } PeerInteractionCommand::~PeerInteractionCommand() { if(peer->getCompletedLength() > 0) { - pieceStorage->subtractPieceStats(peer->getBitfield(), - peer->getBitfieldLength()); + _pieceStorage->subtractPieceStats(peer->getBitfield(), + peer->getBitfieldLength()); } peer->releaseSessionResource(); - PEER_OBJECT_CLUSTER(btContext)->unregisterHandle(peer->getID()); - btRuntime->decreaseConnections(); + _btRuntime->decreaseConnections(); //logger->debug("CUID#%d - unregistered message factory using ID:%s", //cuid, peer->getId().c_str()); } @@ -229,7 +254,8 @@ bool PeerInteractionCommand::executeInternal() { if(btInteractive->countReceivedMessageInIteration() > 0) { updateKeepAlive(); } - if((peer->amInterested() && !peer->peerChoking() && (peer->getLatency() < 1500)) || + if((peer->amInterested() && !peer->peerChoking() && + (peer->getLatency() < 1500)) || (peer->peerInterested() && !peer->amChoking())) { // Writable check causes CPU usage high because socket becomes writable @@ -263,15 +289,18 @@ bool PeerInteractionCommand::executeInternal() { // TODO this method removed when PeerBalancerCommand is implemented bool PeerInteractionCommand::prepareForNextPeer(time_t wait) { - if(peerStorage->isPeerAvailable() && btRuntime->lessThanEqMinPeers()) { - PeerHandle peer = peerStorage->getUnusedPeer(); + if(_peerStorage->isPeerAvailable() && _btRuntime->lessThanEqMinPeers()) { + PeerHandle peer = _peerStorage->getUnusedPeer(); peer->usedBy(CUIDCounterSingletonHolder::instance()->newID()); PeerInitiateConnectionCommand* command = new PeerInitiateConnectionCommand(peer->usedBy(), _requestGroup, peer, e, - btContext); + _btContext, + _btRuntime); + command->setPeerStorage(_peerStorage); + command->setPieceStorage(_pieceStorage); e->commands.push_back(command); } return true; @@ -279,7 +308,7 @@ bool PeerInteractionCommand::prepareForNextPeer(time_t wait) { void PeerInteractionCommand::onAbort() { btInteractive->cancelAllPiece(); - peerStorage->returnPeer(peer); + _peerStorage->returnPeer(peer); } void PeerInteractionCommand::onFailure() @@ -289,7 +318,13 @@ void PeerInteractionCommand::onFailure() bool PeerInteractionCommand::exitBeforeExecute() { - return btRuntime->isHalt(); + return _btRuntime->isHalt(); +} + +void PeerInteractionCommand::setPeerStorage +(const SharedHandle& peerStorage) +{ + _peerStorage = peerStorage; } } // namespace aria2 diff --git a/src/PeerInteractionCommand.h b/src/PeerInteractionCommand.h index b6533062..4e793484 100644 --- a/src/PeerInteractionCommand.h +++ b/src/PeerInteractionCommand.h @@ -37,15 +37,17 @@ #include "PeerAbstractCommand.h" #include "RequestGroupAware.h" -#include "BtContextAwareCommand.h" namespace aria2 { +class BtContext; class BtInteractive; class PeerConnection; +class BtRuntime; +class PeerStorage; +class PieceStorage; class PeerInteractionCommand : public PeerAbstractCommand, - public BtContextAwareCommand, public RequestGroupAware { public: @@ -55,6 +57,14 @@ public: RECEIVER_WAIT_HANDSHAKE, WIRED}; private: + SharedHandle _btContext; + + SharedHandle _btRuntime; + + SharedHandle _pieceStorage; + + SharedHandle _peerStorage; + Seq sequence; SharedHandle btInteractive; unsigned int maxDownloadSpeedLimit; @@ -70,12 +80,16 @@ public: const SharedHandle& peer, DownloadEngine* e, const SharedHandle& btContext, + const SharedHandle& btRuntime, + const SharedHandle& pieceStorage, const SharedHandle& s, Seq sequence, - const SharedHandle& peerConnection = SharedHandle()); + const SharedHandle& peerConnection = + SharedHandle()); virtual ~PeerInteractionCommand(); + void setPeerStorage(const SharedHandle& peerStorage); }; } // namespace aria2 diff --git a/src/PeerObject.h b/src/PeerObject.h deleted file mode 100644 index f83f95a5..00000000 --- a/src/PeerObject.h +++ /dev/null @@ -1,63 +0,0 @@ -/* */ -#ifndef _D_PEER_OBJECT_H_ -#define _D_PEER_OBJECT_H_ - -#include "common.h" - -namespace aria2 { - -class BtMessageFactory; -class BtRequestFactory; -class BtMessageDispatcher; -class BtMessageReceiver; -class ExtensionMessageFactory; -class PeerConnection; - -class PeerObject { -public: - SharedHandle btMessageFactory; - SharedHandle btRequestFactory; - SharedHandle btMessageDispatcher; - SharedHandle btMessageReceiver; - SharedHandle peerConnection; - SharedHandle extensionMessageFactory; -}; - -typedef SharedHandle PeerObjectHandle; - -} // namespace aria2 - -#endif // _D_PEER_OBJECT_H_ diff --git a/src/PeerReceiveHandshakeCommand.cc b/src/PeerReceiveHandshakeCommand.cc index 0c8b7baa..ad89c13f 100644 --- a/src/PeerReceiveHandshakeCommand.cc +++ b/src/PeerReceiveHandshakeCommand.cc @@ -45,6 +45,8 @@ #include "PeerStorage.h" #include "PieceStorage.h" #include "BtRuntime.h" +#include "BtAnnounce.h" +#include "BtProgressInfoFile.h" #include "BtConstants.h" #include "message.h" #include "Socket.h" @@ -57,11 +59,13 @@ namespace aria2 { -PeerReceiveHandshakeCommand::PeerReceiveHandshakeCommand(int32_t cuid, - const PeerHandle& peer, - DownloadEngine* e, - const SocketHandle& s, - const SharedHandle& peerConnection): +PeerReceiveHandshakeCommand::PeerReceiveHandshakeCommand +(int32_t cuid, + const PeerHandle& peer, + DownloadEngine* e, + const SocketHandle& s, + const SharedHandle& peerConnection) + : PeerAbstractCommand(cuid, peer, e, s), _peerConnection(peerConnection), _thresholdSpeed(e->option->getAsInt(PREF_BT_REQUEST_PEER_SPEED_LIMIT)) @@ -93,28 +97,40 @@ bool PeerReceiveHandshakeCommand::executeInternal() if(dataLength >= 48) { // check info_hash std::string infoHash = Util::toHex(&data[28], INFO_HASH_LENGTH); - BtContextHandle btContext = BtRegistry::getBtContext(infoHash); - if(btContext.isNull() || !BT_RUNTIME(btContext)->ready()) { + + SharedHandle btRegistry = e->getBtRegistry(); + SharedHandle btContext = btRegistry->getBtContext(infoHash); + SharedHandle btRuntime = btRegistry->getBtRuntime(infoHash); + SharedHandle pieceStorage = + btRegistry->getPieceStorage(infoHash); + SharedHandle peerStorage = + btRegistry->getPeerStorage(infoHash); + + if(btContext.isNull() || !btRuntime->ready()) { throw DlAbortEx (StringFormat("Unknown info hash %s", infoHash.c_str()).str()); } TransferStat tstat = btContext->getOwnerRequestGroup()->calculateStat(); - if((!PIECE_STORAGE(btContext)->downloadFinished() && + if((!pieceStorage->downloadFinished() && tstat.getDownloadSpeed() < _thresholdSpeed) || - BT_RUNTIME(btContext)->lessThanMaxPeers()) { - if(PEER_STORAGE(btContext)->addPeer(peer)) { + btRuntime->lessThanMaxPeers()) { + if(peerStorage->addPeer(peer)) { peer->usedBy(cuid); PeerInteractionCommand* command = - new PeerInteractionCommand(cuid, - btContext->getOwnerRequestGroup(), - peer, - e, - btContext, - socket, - PeerInteractionCommand::RECEIVER_WAIT_HANDSHAKE, - _peerConnection); + new PeerInteractionCommand + (cuid, + btContext->getOwnerRequestGroup(), + peer, + e, + btContext, + btRuntime, + pieceStorage, + socket, + PeerInteractionCommand::RECEIVER_WAIT_HANDSHAKE, + _peerConnection); + command->setPeerStorage(peerStorage); e->commands.push_back(command); logger->debug(MSG_INCOMING_PEER_CONNECTION, cuid, peer->usedBy()); } diff --git a/src/PeerReceiveHandshakeCommand.h b/src/PeerReceiveHandshakeCommand.h index b0ec6887..d63bcdec 100644 --- a/src/PeerReceiveHandshakeCommand.h +++ b/src/PeerReceiveHandshakeCommand.h @@ -54,15 +54,15 @@ protected: virtual bool executeInternal(); virtual bool exitBeforeExecute(); public: - PeerReceiveHandshakeCommand(int32_t cuid, - const SharedHandle& peer, - DownloadEngine* e, - const SharedHandle& s, - const SharedHandle& peerConnection = SharedHandle()); - + PeerReceiveHandshakeCommand + (int32_t cuid, + const SharedHandle& peer, + DownloadEngine* e, + const SharedHandle& s, + const SharedHandle& peerConnection = + SharedHandle()); + virtual ~PeerReceiveHandshakeCommand(); - - }; } // namespace aria2 diff --git a/src/ReceiverMSEHandshakeCommand.cc b/src/ReceiverMSEHandshakeCommand.cc index 73f20ef4..9d89e11d 100644 --- a/src/ReceiverMSEHandshakeCommand.cc +++ b/src/ReceiverMSEHandshakeCommand.cc @@ -48,6 +48,13 @@ #include "ARC4Encryptor.h" #include "ARC4Decryptor.h" #include "RequestGroupMan.h" +#include "BtRegistry.h" +#include "BtContext.h" +#include "PeerStorage.h" +#include "PieceStorage.h" +#include "BtAnnounce.h" +#include "BtRuntime.h" +#include "BtProgressInfoFile.h" namespace aria2 { @@ -128,7 +135,8 @@ bool ReceiverMSEHandshakeCommand::executeInternal() break; } case RECEIVER_RECEIVE_PAD_C_LENGTH: { - if(_mseHandshake->receiveReceiverHashAndPadCLength()) { + if(_mseHandshake->receiveReceiverHashAndPadCLength + (e->getBtRegistry()->getAllBtContext())) { _sequence = RECEIVER_RECEIVE_PAD_C; } break; diff --git a/src/RequestGroup.cc b/src/RequestGroup.cc index 8a83288f..a1ad0805 100644 --- a/src/RequestGroup.cc +++ b/src/RequestGroup.cc @@ -33,6 +33,10 @@ */ /* copyright --> */ #include "RequestGroup.h" + +#include +#include + #include "PostDownloadHandler.h" #include "DownloadEngine.h" #include "DefaultSegmentManFactory.h" @@ -87,7 +91,6 @@ # include "BtPostDownloadHandler.h" # include "DHTSetup.h" # include "DHTRegistry.h" -# include "PeerObject.h" # include "BtMessageFactory.h" # include "BtRequestFactory.h" # include "BtMessageDispatcher.h" @@ -100,8 +103,6 @@ #ifdef ENABLE_METALINK # include "MetalinkPostDownloadHandler.h" #endif // ENABLE_METALINK -#include -#include namespace aria2 { @@ -200,35 +201,46 @@ void RequestGroup::createInitialCommand(std::deque& commands, _pieceStorage->setFileFilter(Util::parseIntRange(_option->get(PREF_SELECT_FILE))); } - BtProgressInfoFileHandle + SharedHandle progressInfoFile(new DefaultBtProgressInfoFile(_downloadContext, _pieceStorage, _option)); - BtRegistry::registerBtContext(btContext->getInfoHashAsString(), btContext); - BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(), + SharedHandle btRegistry = e->getBtRegistry(); + + btRegistry->registerBtContext(btContext->getInfoHashAsString(), + btContext); + btRegistry->registerPieceStorage(btContext->getInfoHashAsString(), _pieceStorage); - BtRegistry::registerBtProgressInfoFile(btContext->getInfoHashAsString(), + btRegistry->registerBtProgressInfoFile(btContext->getInfoHashAsString(), progressInfoFile); BtRuntimeHandle btRuntime(new BtRuntime()); btRuntime->setListenPort(_option->getAsInt(PREF_LISTEN_PORT)); - BtRegistry::registerBtRuntime(btContext->getInfoHashAsString(), btRuntime); + btRegistry->registerBtRuntime(btContext->getInfoHashAsString(), + btRuntime); + _btRuntime = btRuntime; + progressInfoFile->setBtRuntime(btRuntime); - PeerStorageHandle peerStorage(new DefaultPeerStorage(btContext, _option)); - BtRegistry::registerPeerStorage(btContext->getInfoHashAsString(), peerStorage); + SharedHandle peerStorage + (new DefaultPeerStorage(btContext, _option)); + peerStorage->setBtRuntime(btRuntime); + peerStorage->setPieceStorage(_pieceStorage); + btRegistry->registerPeerStorage(btContext->getInfoHashAsString(), + peerStorage); + _peerStorage = peerStorage; + progressInfoFile->setPeerStorage(peerStorage); - BtAnnounceHandle btAnnounce(new DefaultBtAnnounce(btContext, _option)); - BtRegistry::registerBtAnnounce(btContext->getInfoHashAsString(), btAnnounce); + SharedHandle btAnnounce + (new DefaultBtAnnounce(btContext, _option)); + btAnnounce->setBtRuntime(btRuntime); + btAnnounce->setPieceStorage(_pieceStorage); + btAnnounce->setPeerStorage(peerStorage); + btRegistry->registerBtAnnounce(btContext->getInfoHashAsString(), + btAnnounce); btAnnounce->shuffleAnnounce(); - { - SharedHandle po(new PeerObjectCluster()); - BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(), - po); - } - // Remove the control file if download file doesn't exist if(progressInfoFile->exists() && !_pieceStorage->getDiskAdaptor()->fileExists()) { progressInfoFile->removeFile(); @@ -624,14 +636,8 @@ unsigned int RequestGroup::getNumConnection() const { unsigned int numConnection = _numStreamConnection; #ifdef ENABLE_BITTORRENT - { - BtContextHandle btContext = dynamic_pointer_cast(_downloadContext); - if(!btContext.isNull()) { - BtRuntimeHandle btRuntime = BT_RUNTIME(btContext); - if(!btRuntime.isNull()) { - numConnection += btRuntime->getConnections(); - } - } + if(!_btRuntime.isNull()) { + numConnection += _btRuntime->getConnections(); } #endif // ENABLE_BITTORRENT return numConnection; @@ -652,14 +658,8 @@ TransferStat RequestGroup::calculateStat() { TransferStat stat; #ifdef ENABLE_BITTORRENT - { - BtContextHandle btContext = dynamic_pointer_cast(_downloadContext); - if(!btContext.isNull()) { - PeerStorageHandle peerStorage = PEER_STORAGE(btContext); - if(!peerStorage.isNull()) { - stat = peerStorage->calculateStat(); - } - } + if(!_peerStorage.isNull()) { + stat = _peerStorage->calculateStat(); } #endif // ENABLE_BITTORRENT if(!_segmentMan.isNull()) { @@ -672,14 +672,8 @@ void RequestGroup::setHaltRequested(bool f) { _haltRequested = f; #ifdef ENABLE_BITTORRENT - { - BtContextHandle btContext = dynamic_pointer_cast(_downloadContext); - if(!btContext.isNull()) { - BtRuntimeHandle btRuntime = BT_RUNTIME(btContext); - if(!btRuntime.isNull()) { - btRuntime->setHalt(f); - } - } + if(!_btRuntime.isNull()) { + _btRuntime->setHalt(f); } #endif // ENABLE_BITTORRENT } @@ -690,18 +684,21 @@ void RequestGroup::setForceHaltRequested(bool f) _forceHaltRequested = f; } -void RequestGroup::releaseRuntimeResource() +void RequestGroup::releaseRuntimeResource(DownloadEngine* e) { #ifdef ENABLE_BITTORRENT BtContextHandle btContext = dynamic_pointer_cast(_downloadContext); if(!btContext.isNull()) { - BtContextHandle btContextInReg = BtRegistry::getBtContext(btContext->getInfoHashAsString()); + SharedHandle btRegistry = e->getBtRegistry(); + BtContextHandle btContextInReg = + btRegistry->getBtContext(btContext->getInfoHashAsString()); if(!btContextInReg.isNull() && btContextInReg->getOwnerRequestGroup()->getGID() == btContext->getOwnerRequestGroup()->getGID()) { - BtRegistry::unregister(btContext->getInfoHashAsString()); + btRegistry->unregister(btContext->getInfoHashAsString()); if(!DHTRegistry::_peerAnnounceStorage.isNull()) { - DHTRegistry::_peerAnnounceStorage->removePeerAnnounce(btContext); + DHTRegistry::_peerAnnounceStorage-> + removeLocalPeerAnnounce(btContext->getInfoHash()); } } } diff --git a/src/RequestGroup.h b/src/RequestGroup.h index 12dc0fb5..83b175f2 100644 --- a/src/RequestGroup.h +++ b/src/RequestGroup.h @@ -36,11 +36,13 @@ #define _D_REQUEST_GROUP_H_ #include "common.h" + +#include +#include + #include "SharedHandle.h" #include "TransferStat.h" #include "TimeA2.h" -#include -#include namespace aria2 { @@ -62,6 +64,10 @@ class CheckIntegrityEntry; class DownloadResult; class ServerHost; class URISelector; +#ifdef ENABLE_BITTORRENT +class BtRuntime; +class PeerStorage; +#endif // ENABLE_BITTORRENT class RequestGroup { private: @@ -120,6 +126,10 @@ private: unsigned int _fileNotFoundCount; + WeakHandle _btRuntime; + + WeakHandle _peerStorage; + const Option* _option; Logger* _logger; @@ -287,7 +297,7 @@ public: bool isDependencyResolved(); - void releaseRuntimeResource(); + void releaseRuntimeResource(DownloadEngine* e); void postDownloadProcessing(std::deque >& groups); diff --git a/src/RequestGroupMan.cc b/src/RequestGroupMan.cc index 8081a37a..4af5cff5 100644 --- a/src/RequestGroupMan.cc +++ b/src/RequestGroupMan.cc @@ -122,6 +122,7 @@ RequestGroupMan::getRequestGroups() const class ProcessStoppedRequestGroup { private: + DownloadEngine* _e; std::deque >& _reservedGroups; std::deque >& _downloadResults; Logger* _logger; @@ -143,8 +144,10 @@ private: } public: ProcessStoppedRequestGroup - (std::deque >& reservedGroups, + (DownloadEngine* e, + std::deque >& reservedGroups, std::deque >& downloadResults): + _e(e), _reservedGroups(reservedGroups), _downloadResults(downloadResults), _logger(LogFactory::getInstance()) {} @@ -178,7 +181,7 @@ public: } catch(RecoverableException& ex) { _logger->error(EX_EXCEPTION_CAUGHT, ex); } - group->releaseRuntimeResource(); + group->releaseRuntimeResource(_e); _downloadResults.push_back(group->createDownloadResult()); } } @@ -227,14 +230,15 @@ void RequestGroupMan::updateServerStat() CollectServerStat(this)); } -void RequestGroupMan::removeStoppedGroup() +void RequestGroupMan::removeStoppedGroup(DownloadEngine* e) { size_t numPrev = _requestGroups.size(); updateServerStat(); std::for_each(_requestGroups.begin(), _requestGroups.end(), - ProcessStoppedRequestGroup(_reservedGroups, _downloadResults)); + ProcessStoppedRequestGroup(e, _reservedGroups, + _downloadResults)); _requestGroups.erase(std::remove_if(_requestGroups.begin(), _requestGroups.end(), @@ -264,7 +268,7 @@ void RequestGroupMan::configureRequestGroup void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e) { RequestGroups temp; - removeStoppedGroup(); + removeStoppedGroup(e); unsigned int count = 0; for(int num = _maxSimultaneousDownloads-_requestGroups.size(); num > 0 && _reservedGroups.size() > 0; --num) { diff --git a/src/RequestGroupMan.h b/src/RequestGroupMan.h index 37cb7aaf..f793c4ec 100644 --- a/src/RequestGroupMan.h +++ b/src/RequestGroupMan.h @@ -89,7 +89,7 @@ public: void getInitialCommands(std::deque& commands, DownloadEngine* e); - void removeStoppedGroup(); + void removeStoppedGroup(DownloadEngine* e); void fillRequestGroupFromReserver(DownloadEngine* e); diff --git a/src/SeedCheckCommand.cc b/src/SeedCheckCommand.cc index 7b1bbe6e..48582dcd 100644 --- a/src/SeedCheckCommand.cc +++ b/src/SeedCheckCommand.cc @@ -46,17 +46,17 @@ namespace aria2 { SeedCheckCommand::SeedCheckCommand(int cuid, RequestGroup* requestGroup, DownloadEngine* e, - const BtContextHandle& btContext, + const SharedHandle& btContext, const SeedCriteriaHandle& seedCriteria) :Command(cuid), - BtContextAwareCommand(btContext), RequestGroupAware(requestGroup), e(e), + _btContext(btContext), seedCriteria(seedCriteria), checkStarted(false) {} bool SeedCheckCommand::execute() { - if(btRuntime->isHalt()) { + if(_btRuntime->isHalt()) { return true; } if(!seedCriteria.get()) { @@ -64,7 +64,7 @@ bool SeedCheckCommand::execute() { } if(checkPoint.elapsed(1)) { if(!checkStarted) { - if(pieceStorage->downloadFinished()) { + if(_pieceStorage->downloadFinished()) { checkStarted = true; seedCriteria->reset(); } @@ -72,7 +72,7 @@ bool SeedCheckCommand::execute() { if(checkStarted) { if(seedCriteria->evaluate()) { logger->notice(MSG_SEEDING_END); - btRuntime->setHalt(true); + _btRuntime->setHalt(true); } } } @@ -80,9 +80,22 @@ bool SeedCheckCommand::execute() { return false; } -void SeedCheckCommand::setSeedCriteria(const SharedHandle& seedCriteria) +void SeedCheckCommand::setSeedCriteria +(const SharedHandle& seedCriteria) { this->seedCriteria = seedCriteria; } +void SeedCheckCommand::setBtRuntime(const SharedHandle& btRuntime) +{ + _btRuntime = btRuntime; +} + +void SeedCheckCommand::setPieceStorage +(const SharedHandle& pieceStorage) +{ + _pieceStorage = pieceStorage; +} + + } // namespace aria2 diff --git a/src/SeedCheckCommand.h b/src/SeedCheckCommand.h index 843c31c2..c82061a1 100644 --- a/src/SeedCheckCommand.h +++ b/src/SeedCheckCommand.h @@ -36,7 +36,7 @@ #define _D_SEED_CHECK_COMMAND_H_ #include "Command.h" -#include "BtContextAwareCommand.h" +#include "SharedHandle.h" #include "RequestGroupAware.h" #include "TimeA2.h" @@ -44,13 +44,18 @@ namespace aria2 { class DownloadEngine; class SeedCriteria; +class BtContext; +class BtRuntime; +class PieceStorage; class SeedCheckCommand : public Command, - public BtContextAwareCommand, public RequestGroupAware { private: DownloadEngine* e; + SharedHandle _btContext; + SharedHandle _pieceStorage; + SharedHandle _btRuntime; Time checkPoint; SharedHandle seedCriteria; bool checkStarted; @@ -66,6 +71,10 @@ public: virtual bool execute(); void setSeedCriteria(const SharedHandle& seedCriteria); + + void setBtRuntime(const SharedHandle& btRuntime); + + void setPieceStorage(const SharedHandle& pieceStorage); }; } // namespace aria2 diff --git a/src/ShareRatioSeedCriteria.h b/src/ShareRatioSeedCriteria.h index 8a6b4115..41a8ae92 100644 --- a/src/ShareRatioSeedCriteria.h +++ b/src/ShareRatioSeedCriteria.h @@ -40,7 +40,6 @@ #include "PeerStorage.h" #include "PieceStorage.h" #include "BtRuntime.h" -#include "BtRegistry.h" namespace aria2 { @@ -48,16 +47,13 @@ class ShareRatioSeedCriteria : public SeedCriteria { private: double ratio; SharedHandle btContext; - SharedHandle peerStorage; - SharedHandle pieceStorage; - SharedHandle btRuntime; + SharedHandle _peerStorage; + SharedHandle _pieceStorage; + SharedHandle _btRuntime; public: ShareRatioSeedCriteria(double ratio, const SharedHandle& btContext) :ratio(ratio), - btContext(btContext), - peerStorage(PEER_STORAGE(btContext)), - pieceStorage(PIECE_STORAGE(btContext)), - btRuntime(BT_RUNTIME(btContext)) {} + btContext(btContext) {} virtual ~ShareRatioSeedCriteria() {} @@ -67,11 +63,11 @@ public: if(btContext->getTotalLength() == 0) { return false; } - TransferStat stat = peerStorage->calculateStat(); + TransferStat stat = _peerStorage->calculateStat(); uint64_t allTimeUploadLength = - btRuntime->getUploadLengthAtStartup()+stat.getSessionUploadLength(); + _btRuntime->getUploadLengthAtStartup()+stat.getSessionUploadLength(); return ratio <= - ((double)allTimeUploadLength)/pieceStorage->getCompletedLength(); + ((double)allTimeUploadLength)/_pieceStorage->getCompletedLength(); } void setRatio(double ratio) { @@ -81,6 +77,21 @@ public: double getRatio() const { return ratio; } + + void setPeerStorage(const SharedHandle& peerStorage) + { + _peerStorage = peerStorage; + } + + void setPieceStorage(const SharedHandle& pieceStorage) + { + _pieceStorage = pieceStorage; + } + + void setBtRuntime(const SharedHandle& btRuntime) + { + _btRuntime = btRuntime; + } }; } // namespace aria2 diff --git a/src/SimpleBtMessage.cc b/src/SimpleBtMessage.cc index 99c8d0f7..c22c34a5 100644 --- a/src/SimpleBtMessage.cc +++ b/src/SimpleBtMessage.cc @@ -34,7 +34,6 @@ /* copyright --> */ #include "SimpleBtMessage.h" #include "message.h" -#include "BtRegistry.h" #include "Peer.h" #include "PeerConnection.h" #include "Logger.h" diff --git a/src/TrackerWatcherCommand.cc b/src/TrackerWatcherCommand.cc index 2271d828..98869f1b 100644 --- a/src/TrackerWatcherCommand.cc +++ b/src/TrackerWatcherCommand.cc @@ -33,6 +33,9 @@ */ /* copyright --> */ #include "TrackerWatcherCommand.h" + +#include + #include "DownloadEngine.h" #include "BtContext.h" #include "BtAnnounce.h" @@ -55,7 +58,6 @@ #include "Logger.h" #include "A2STR.h" #include "SocketCore.h" -#include namespace aria2 { @@ -64,9 +66,9 @@ TrackerWatcherCommand::TrackerWatcherCommand(int32_t cuid, DownloadEngine* e, const BtContextHandle& btContext): Command(cuid), - BtContextAwareCommand(btContext), RequestGroupAware(requestGroup), - e(e) {} + e(e), + _btContext(btContext) {} TrackerWatcherCommand::~TrackerWatcherCommand() {} @@ -83,7 +85,7 @@ bool TrackerWatcherCommand::execute() { return false; } } - if(btAnnounce->noMoreAnnounce()) { + if(_btAnnounce->noMoreAnnounce()) { logger->debug("no more announce"); return true; } @@ -100,35 +102,37 @@ bool TrackerWatcherCommand::execute() { std::string trackerResponse = getTrackerResponse(_trackerRequestGroup); processTrackerResponse(trackerResponse); - btAnnounce->announceSuccess(); - btAnnounce->resetAnnounce(); + _btAnnounce->announceSuccess(); + _btAnnounce->resetAnnounce(); } catch(RecoverableException& ex) { logger->error(EX_EXCEPTION_CAUGHT, ex); - btAnnounce->announceFailure(); - if(btAnnounce->isAllAnnounceFailed()) { - btAnnounce->resetAnnounce(); + _btAnnounce->announceFailure(); + if(_btAnnounce->isAllAnnounceFailed()) { + _btAnnounce->resetAnnounce(); } } _trackerRequestGroup.reset(); } else if(_trackerRequestGroup->getNumCommand() == 0){ // handle errors here - btAnnounce->announceFailure(); // inside it, trackers = 0. + _btAnnounce->announceFailure(); // inside it, trackers = 0. _trackerRequestGroup.reset(); - if(btAnnounce->isAllAnnounceFailed()) { - btAnnounce->resetAnnounce(); + if(_btAnnounce->isAllAnnounceFailed()) { + _btAnnounce->resetAnnounce(); } } e->commands.push_back(this); return false; } -std::string TrackerWatcherCommand::getTrackerResponse(const RequestGroupHandle& requestGroup) +std::string TrackerWatcherCommand::getTrackerResponse +(const RequestGroupHandle& requestGroup) { std::stringstream strm; unsigned char data[2048]; requestGroup->getPieceStorage()->getDiskAdaptor()->openFile(); while(1) { - ssize_t dataLength = requestGroup->getPieceStorage()->getDiskAdaptor()->readData(data, sizeof(data), strm.tellp()); + ssize_t dataLength = requestGroup->getPieceStorage()-> + getDiskAdaptor()->readData(data, sizeof(data), strm.tellp()); if(dataLength == 0) { break; } @@ -138,12 +142,14 @@ std::string TrackerWatcherCommand::getTrackerResponse(const RequestGroupHandle& } // TODO we have to deal with the exception thrown By BtAnnounce -void TrackerWatcherCommand::processTrackerResponse(const std::string& trackerResponse) +void TrackerWatcherCommand::processTrackerResponse +(const std::string& trackerResponse) { - btAnnounce->processAnnounceResponse(reinterpret_cast(trackerResponse.c_str()), + _btAnnounce->processAnnounceResponse + (reinterpret_cast(trackerResponse.c_str()), trackerResponse.size()); - while(!btRuntime->isHalt() && btRuntime->lessThanMinPeers()) { - PeerHandle peer = peerStorage->getUnusedPeer(); + while(!_btRuntime->isHalt() && _btRuntime->lessThanMinPeers()) { + PeerHandle peer = _peerStorage->getUnusedPeer(); if(peer.isNull()) { break; } @@ -153,7 +159,10 @@ void TrackerWatcherCommand::processTrackerResponse(const std::string& trackerRes _requestGroup, peer, e, - btContext); + _btContext, + _btRuntime); + command->setPeerStorage(_peerStorage); + command->setPieceStorage(_pieceStorage); e->commands.push_back(command); logger->debug("CUID#%d - Adding new command CUID#%d", cuid, peer->usedBy()); } @@ -161,9 +170,9 @@ void TrackerWatcherCommand::processTrackerResponse(const std::string& trackerRes RequestGroupHandle TrackerWatcherCommand::createAnnounce() { RequestGroupHandle rg; - if(btAnnounce->isAnnounceReady()) { - rg = createRequestGroup(btAnnounce->getAnnounceUrl()); - btAnnounce->announceStart(); // inside it, trackers++. + if(_btAnnounce->isAnnounceReady()) { + rg = createRequestGroup(_btAnnounce->getAnnounceUrl()); + _btAnnounce->announceStart(); // inside it, trackers++. } return rg; } @@ -191,4 +200,28 @@ TrackerWatcherCommand::createRequestGroup(const std::string& uri) return rg; } +void TrackerWatcherCommand::setBtRuntime +(const SharedHandle& btRuntime) +{ + _btRuntime = btRuntime; +} + +void TrackerWatcherCommand::setPeerStorage +(const SharedHandle& peerStorage) +{ + _peerStorage = peerStorage; +} + +void TrackerWatcherCommand::setPieceStorage +(const SharedHandle& pieceStorage) +{ + _pieceStorage = pieceStorage; +} + +void TrackerWatcherCommand::setBtAnnounce +(const SharedHandle& btAnnounce) +{ + _btAnnounce = btAnnounce; +} + } // namespace aria2 diff --git a/src/TrackerWatcherCommand.h b/src/TrackerWatcherCommand.h index f5c59de8..3611dc7e 100644 --- a/src/TrackerWatcherCommand.h +++ b/src/TrackerWatcherCommand.h @@ -36,21 +36,35 @@ #define _D_TRACKER_WATCHER_COMMAND_H_ #include "Command.h" -#include "BtContextAwareCommand.h" +#include "SharedHandle.h" #include "RequestGroupAware.h" namespace aria2 { class DownloadEngine; class RequestGroup; +class BtContext; +class PeerStorage; +class PieceStorage; +class BtRuntime; +class BtAnnounce; class TrackerWatcherCommand : public Command, - public BtContextAwareCommand, public RequestGroupAware { private: DownloadEngine* e; + SharedHandle _btContext; + + SharedHandle _peerStorage; + + SharedHandle _pieceStorage; + + SharedHandle _btRuntime; + + SharedHandle _btAnnounce; + SharedHandle _trackerRequestGroup; /** * Returns a command for announce request. Returns 0 if no announce request @@ -73,6 +87,14 @@ public: SharedHandle createAnnounce(); virtual bool execute(); + + void setPeerStorage(const SharedHandle& peerStorage); + + void setPieceStorage(const SharedHandle& pieceStorage); + + void setBtRuntime(const SharedHandle& btRuntime); + + void setBtAnnounce(const SharedHandle& btAnnounce); }; } // namespace aria2 diff --git a/src/UTPexExtensionMessage.cc b/src/UTPexExtensionMessage.cc index fb7bfd7b..892483b1 100644 --- a/src/UTPexExtensionMessage.cc +++ b/src/UTPexExtensionMessage.cc @@ -34,13 +34,11 @@ /* copyright --> */ #include "UTPexExtensionMessage.h" #include "Peer.h" -#include "BtContext.h" #include "Dictionary.h" #include "Data.h" #include "BencodeVisitor.h" #include "Util.h" #include "PeerMessageUtil.h" -#include "BtRegistry.h" #include "PeerStorage.h" #include "CompactPeerListProcessor.h" #include "MetaFileUtil.h" @@ -96,7 +94,7 @@ std::string UTPexExtensionMessage::toString() const void UTPexExtensionMessage::doReceivedAction() { - PEER_STORAGE(_btContext)->addPeer(_freshPeers); + _peerStorage->addPeer(_freshPeers); } bool UTPexExtensionMessage::addFreshPeer(const PeerHandle& peer) @@ -161,14 +159,14 @@ size_t UTPexExtensionMessage::getMaxDroppedPeer() const return _maxDroppedPeer; } -void UTPexExtensionMessage::setBtContext(const BtContextHandle& btContext) +void UTPexExtensionMessage::setPeerStorage +(const SharedHandle& peerStorage) { - _btContext = btContext; + _peerStorage = peerStorage; } UTPexExtensionMessageHandle -UTPexExtensionMessage::create(const BtContextHandle& btContext, - const unsigned char* data, size_t len) +UTPexExtensionMessage::create(const unsigned char* data, size_t len) { if(len < 1) { throw DlAbortEx(StringFormat(MSG_TOO_SMALL_PAYLOAD_SIZE, diff --git a/src/UTPexExtensionMessage.h b/src/UTPexExtensionMessage.h index 41b38119..7bf19073 100644 --- a/src/UTPexExtensionMessage.h +++ b/src/UTPexExtensionMessage.h @@ -36,13 +36,15 @@ #define _D_UT_PEX_EXTENSION_MESSAGE_H_ #include "ExtensionMessage.h" -#include "a2time.h" + #include #include +#include "a2time.h" + namespace aria2 { -class BtContext; +class PeerStorage; class Peer; class UTPexExtensionMessage; typedef SharedHandle UTPexExtensionMessageHandle; @@ -55,7 +57,7 @@ private: std::deque > _droppedPeers; - SharedHandle _btContext; + SharedHandle _peerStorage; time_t _interval; @@ -101,10 +103,10 @@ public: bool droppedPeersAreFull() const; - void setBtContext(const SharedHandle& btContext); + void setPeerStorage(const SharedHandle& peerStorage); - static UTPexExtensionMessageHandle create(const SharedHandle& btContext, - const unsigned char* data, size_t len); + static UTPexExtensionMessageHandle + create(const unsigned char* data, size_t len); void setMaxFreshPeer(size_t maxFreshPeer); diff --git a/test/BtCancelMessageTest.cc b/test/BtCancelMessageTest.cc index a09bc76c..efc8a09a 100644 --- a/test/BtCancelMessageTest.cc +++ b/test/BtCancelMessageTest.cc @@ -1,19 +1,15 @@ #include "BtCancelMessage.h" + +#include + +#include + #include "PeerMessageUtil.h" #include "MockBtMessageDispatcher.h" #include "MockBtContext.h" #include "Peer.h" #include "FileEntry.h" -#include "PeerObject.h" #include "Piece.h" -#include "BtRegistry.h" -#include "BtMessageFactory.h" -#include "BtRequestFactory.h" -#include "BtMessageReceiver.h" -#include "PeerConnection.h" -#include "ExtensionMessageFactory.h" -#include -#include namespace aria2 { @@ -29,19 +25,9 @@ private: SharedHandle btContext; public: void setUp() { - BtRegistry::unregisterAll(); peer.reset(new Peer("host", 6969)); btContext.reset(new MockBtContext()); btContext->setInfoHash((const unsigned char*)"12345678901234567890"); - SharedHandle cluster(new PeerObjectCluster()); - BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(), - cluster); - SharedHandle po(new PeerObject()); - PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), po); - } - - void tearDown() { - BtRegistry::unregisterAll(); } void testCreate(); @@ -119,7 +105,8 @@ void BtCancelMessageTest::testDoReceivedAction() { msg.setLength(16*1024); msg.setBtContext(btContext); msg.setPeer(peer); - SharedHandle dispatcher(new MockBtMessageDispatcher2()); + SharedHandle dispatcher + (new MockBtMessageDispatcher2()); msg.setBtMessageDispatcher(dispatcher); msg.doReceivedAction(); diff --git a/test/BtChokeMessageTest.cc b/test/BtChokeMessageTest.cc index 758ed5df..b16e52a2 100644 --- a/test/BtChokeMessageTest.cc +++ b/test/BtChokeMessageTest.cc @@ -1,18 +1,15 @@ #include "BtChokeMessage.h" + +#include + +#include + #include "PeerMessageUtil.h" #include "MockBtMessageDispatcher.h" #include "MockBtRequestFactory.h" #include "MockBtContext.h" #include "Peer.h" #include "FileEntry.h" -#include "BtRegistry.h" -#include "PeerObject.h" -#include "BtMessageFactory.h" -#include "BtMessageReceiver.h" -#include "PeerConnection.h" -#include "ExtensionMessageFactory.h" -#include -#include namespace aria2 { @@ -34,16 +31,10 @@ public: SharedHandle btContext; void setUp() { - BtRegistry::unregisterAll(); peer.reset(new Peer("host", 6969)); peer->allocateSessionResource(1024, 1024*1024); btContext.reset(new MockBtContext()); btContext->setInfoHash((const unsigned char*)"12345678901234567890"); - SharedHandle cluster(new PeerObjectCluster()); - BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(), - cluster); - SharedHandle po(new PeerObject()); - PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), po); } void testCreate(); diff --git a/test/BtExtendedMessageTest.cc b/test/BtExtendedMessageTest.cc index ffe081a4..f7fb3027 100644 --- a/test/BtExtendedMessageTest.cc +++ b/test/BtExtendedMessageTest.cc @@ -1,21 +1,15 @@ #include "BtExtendedMessage.h" -#include "PeerMessageUtil.h" -#include "MockBtContext.h" -#include "MockExtensionMessageFactory.h" -#include "BtRegistry.h" -#include "Peer.h" -#include "PeerObject.h" -#include "BtMessageFactory.h" -#include "BtRequestFactory.h" -#include "BtMessageDispatcher.h" -#include "BtMessageReceiver.h" -#include "PeerConnection.h" -#include "Exception.h" -#include "FileEntry.h" + #include #include + #include +#include "PeerMessageUtil.h" +#include "MockExtensionMessageFactory.h" +#include "Peer.h" +#include "Exception.h" + namespace aria2 { class BtExtendedMessageTest:public CppUnit::TestFixture { @@ -29,16 +23,6 @@ class BtExtendedMessageTest:public CppUnit::TestFixture { private: public: - void setUp() - { - BtRegistry::unregisterAll(); - } - - void tearDown() - { - BtRegistry::unregisterAll(); - } - void testCreate(); void testGetMessage(); void testDoReceivedAction(); @@ -51,27 +35,18 @@ CPPUNIT_TEST_SUITE_REGISTRATION(BtExtendedMessageTest); void BtExtendedMessageTest::testCreate() { SharedHandle peer(new Peer("192.168.0.1", 6969)); peer->allocateSessionResource(1024, 1024*1024); - SharedHandle ctx(new MockBtContext()); - unsigned char infohash[20]; - memset(infohash, 0, sizeof(infohash)); - ctx->setInfoHash(infohash); + SharedHandle exmsgFactory (new MockExtensionMessageFactory()); - SharedHandle cluster(new PeerObjectCluster()); - BtRegistry::registerPeerObjectCluster(ctx->getInfoHashAsString(), cluster); - SharedHandle peerObject(new PeerObject()); - peerObject->extensionMessageFactory = exmsgFactory; - - PEER_OBJECT_CLUSTER(ctx)->registerHandle(peer->getID(), peerObject); - // payload:{4:name3:foo}->11bytes std::string payload = "4:name3:foo"; unsigned char msg[17];// 6+11bytes - PeerMessageUtil::createPeerMessageString((unsigned char*)msg, sizeof(msg), 13, 20); + PeerMessageUtil::createPeerMessageString((unsigned char*)msg, + sizeof(msg), 13, 20); msg[5] = 1; // Set dummy extended message ID 1 memcpy(msg+6, payload.c_str(), payload.size()); - SharedHandle pm = BtExtendedMessage::create(ctx, + SharedHandle pm = BtExtendedMessage::create(exmsgFactory, peer, &msg[4], 13); CPPUNIT_ASSERT_EQUAL((uint8_t)20, pm->getId()); @@ -80,7 +55,7 @@ void BtExtendedMessageTest::testCreate() { try { unsigned char msg[5]; PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 20); - BtExtendedMessage::create(ctx, peer, &msg[4], 1); + BtExtendedMessage::create(exmsgFactory, peer, &msg[4], 1); CPPUNIT_FAIL("exception must be thrown."); } catch(Exception& e) { std::cerr << e.stackTrace() << std::endl; @@ -89,7 +64,7 @@ void BtExtendedMessageTest::testCreate() { try { unsigned char msg[6]; PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 2, 21); - BtExtendedMessage::create(ctx, peer, &msg[4], 2); + BtExtendedMessage::create(exmsgFactory, peer, &msg[4], 2); CPPUNIT_FAIL("exception must be thrown."); } catch(Exception& e) { std::cerr << e.stackTrace() << std::endl; diff --git a/test/BtInterestedMessageTest.cc b/test/BtInterestedMessageTest.cc index 51c4e522..28226106 100644 --- a/test/BtInterestedMessageTest.cc +++ b/test/BtInterestedMessageTest.cc @@ -1,8 +1,11 @@ #include "BtInterestedMessage.h" + +#include + +#include + #include "PeerMessageUtil.h" #include "Peer.h" -#include -#include namespace aria2 { @@ -67,6 +70,7 @@ void BtInterestedMessageTest::testDoReceivedAction() { SharedHandle peer(new Peer("host", 6969)); peer->allocateSessionResource(1024, 1024*1024); msg.setPeer(peer); + CPPUNIT_ASSERT(!peer->peerInterested()); msg.doReceivedAction(); CPPUNIT_ASSERT(peer->peerInterested()); diff --git a/test/BtNotInterestedMessageTest.cc b/test/BtNotInterestedMessageTest.cc index 7827bd27..d57d8d89 100644 --- a/test/BtNotInterestedMessageTest.cc +++ b/test/BtNotInterestedMessageTest.cc @@ -1,8 +1,12 @@ #include "BtNotInterestedMessage.h" + +#include + +#include + #include "PeerMessageUtil.h" #include "Peer.h" -#include -#include +#include "PeerStorage.h" namespace aria2 { @@ -19,6 +23,7 @@ private: public: void setUp() { + // TODO add peer storage here } void testCreate(); diff --git a/test/BtPieceMessageTest.cc b/test/BtPieceMessageTest.cc index 19183f34..1c3a2017 100644 --- a/test/BtPieceMessageTest.cc +++ b/test/BtPieceMessageTest.cc @@ -1,4 +1,9 @@ #include "BtPieceMessage.h" + +#include + +#include + #include "PeerMessageUtil.h" #include "MockBtContext.h" #include "MockBtMessage.h" @@ -9,15 +14,7 @@ #include "FileEntry.h" #include "Peer.h" #include "Piece.h" -#include "BtRegistry.h" -#include "PeerObject.h" -#include "BtMessageReceiver.h" -#include "BtRequestFactory.h" -#include "PeerConnection.h" -#include "ExtensionMessageFactory.h" #include "BtHandshakeMessage.h" -#include -#include namespace aria2 { @@ -75,37 +72,33 @@ public: } }; + SharedHandle _btContext; SharedHandle btMessageDispatcher; + SharedHandle _btMessageFactory; SharedHandle peer; SharedHandle msg; void setUp() { - BtRegistry::unregisterAll(); - SharedHandle btContext(new MockBtContext()); - btContext->setInfoHash((const unsigned char*)"12345678901234567890"); - btContext->setPieceLength(16*1024); - btContext->setTotalLength(256*1024); + _btContext.reset(new MockBtContext()); + _btContext->setInfoHash((const unsigned char*)"12345678901234567890"); + _btContext->setPieceLength(16*1024); + _btContext->setTotalLength(256*1024); peer.reset(new Peer("host", 6969)); - peer->allocateSessionResource(btContext->getPieceLength(), - btContext->getTotalLength()); - SharedHandle cluster(new PeerObjectCluster()); - BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(), - cluster); - SharedHandle po(new PeerObject()); - PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), po); + peer->allocateSessionResource(_btContext->getPieceLength(), + _btContext->getTotalLength()); + btMessageDispatcher.reset(new MockBtMessageDispatcher()); - PEER_OBJECT(btContext, peer)->btMessageDispatcher = btMessageDispatcher; - PEER_OBJECT(btContext, peer)->btMessageFactory.reset(new MockBtMessageFactory2()); + _btMessageFactory.reset(new MockBtMessageFactory2()); msg.reset(new BtPieceMessage()); msg->setIndex(1); msg->setBegin(1024); msg->setBlockLength(16*1024); - msg->setBtContext(btContext); + msg->setBtContext(_btContext); msg->setPeer(peer); msg->setBtMessageDispatcher(btMessageDispatcher); - msg->setBtMessageFactory(BT_MESSAGE_FACTORY(btContext, peer)); + msg->setBtMessageFactory(_btMessageFactory); } }; @@ -181,7 +174,8 @@ void BtPieceMessageTest::testChokingEvent_allowedFastEnabled() { CPPUNIT_ASSERT(msg->isInvalidate()); CPPUNIT_ASSERT_EQUAL((size_t)1, btMessageDispatcher->messageQueue.size()); - MockBtMessage2* rej = (MockBtMessage2*)btMessageDispatcher->messageQueue.front().get(); + MockBtMessage2* rej = + (MockBtMessage2*)btMessageDispatcher->messageQueue.front().get(); CPPUNIT_ASSERT_EQUAL((size_t)1, rej->index); CPPUNIT_ASSERT_EQUAL((uint32_t)1024, rej->begin); CPPUNIT_ASSERT_EQUAL((size_t)16*1024, rej->length); @@ -271,7 +265,8 @@ void BtPieceMessageTest::testCancelSendingPieceEvent_allowedFastEnabled() { CPPUNIT_ASSERT(msg->isInvalidate()); CPPUNIT_ASSERT_EQUAL((size_t)1, btMessageDispatcher->messageQueue.size()); - MockBtMessage2* rej = (MockBtMessage2*)btMessageDispatcher->messageQueue.front().get(); + MockBtMessage2* rej = (MockBtMessage2*)btMessageDispatcher-> + messageQueue.front().get(); CPPUNIT_ASSERT_EQUAL((size_t)1, rej->index); CPPUNIT_ASSERT_EQUAL((uint32_t)1024, rej->begin); CPPUNIT_ASSERT_EQUAL((size_t)16*1024, rej->length); diff --git a/test/BtPortMessageTest.cc b/test/BtPortMessageTest.cc index 648cfeb1..f72087e0 100644 --- a/test/BtPortMessageTest.cc +++ b/test/BtPortMessageTest.cc @@ -1,4 +1,9 @@ #include "BtPortMessage.h" + +#include + +#include + #include "PeerMessageUtil.h" #include "Util.h" #include "array_fun.h" @@ -8,8 +13,6 @@ #include "MockDHTTask.h" #include "MockDHTTaskFactory.h" #include "MockDHTTaskQueue.h" -#include -#include namespace aria2 { diff --git a/test/BtRegistryTest.cc b/test/BtRegistryTest.cc index 028a74d8..6d8fc4d0 100644 --- a/test/BtRegistryTest.cc +++ b/test/BtRegistryTest.cc @@ -1,4 +1,7 @@ #include "BtRegistry.h" + +#include + #include "Exception.h" #include "MockBtContext.h" #include "MockPeerStorage.h" @@ -7,14 +10,6 @@ #include "MockBtProgressInfoFile.h" #include "BtRuntime.h" #include "FileEntry.h" -#include "PeerObject.h" -#include "BtMessageFactory.h" -#include "BtRequestFactory.h" -#include "BtMessageDispatcher.h" -#include "BtMessageReceiver.h" -#include "PeerConnection.h" -#include "ExtensionMessageFactory.h" -#include namespace aria2 { @@ -27,28 +22,16 @@ class BtRegistryTest:public CppUnit::TestFixture { CPPUNIT_TEST(testGetBtRuntime); CPPUNIT_TEST(testGetBtAnnounce); CPPUNIT_TEST(testGetBtProgressInfoFile); - CPPUNIT_TEST(testGetPeerObjectCluster); CPPUNIT_TEST_SUITE_END(); private: public: - void setUp() - { - BtRegistry::unregisterAll(); - } - - void tearDown() - { - BtRegistry::unregisterAll(); - } - void testGetBtContext(); void testGetPeerStorage(); void testGetPieceStorage(); void testGetBtRuntime(); void testGetBtAnnounce(); void testGetBtProgressInfoFile(); - void testGetPeerObjectCluster(); }; @@ -56,74 +39,67 @@ CPPUNIT_TEST_SUITE_REGISTRATION( BtRegistryTest ); void BtRegistryTest::testGetBtContext() { - CPPUNIT_ASSERT(BtRegistry::getBtContext("test").isNull()); + BtRegistry btRegistry; + CPPUNIT_ASSERT(btRegistry.getBtContext("test").isNull()); SharedHandle btContext(new MockBtContext()); - BtRegistry::registerBtContext("test", btContext); + btRegistry.registerBtContext("test", btContext); CPPUNIT_ASSERT_EQUAL(btContext.get(), - BtRegistry::getBtContext("test").get()); + btRegistry.getBtContext("test").get()); } void BtRegistryTest::testGetPeerStorage() { - CPPUNIT_ASSERT(!BtRegistry::getPeerStorage("test").get()); + BtRegistry btRegistry; + CPPUNIT_ASSERT(!btRegistry.getPeerStorage("test").get()); SharedHandle peerStorage(new MockPeerStorage()); - BtRegistry::registerPeerStorage("test", peerStorage); + btRegistry.registerPeerStorage("test", peerStorage); CPPUNIT_ASSERT_EQUAL(peerStorage.get(), - BtRegistry::getPeerStorage("test").get()); + btRegistry.getPeerStorage("test").get()); } void BtRegistryTest::testGetPieceStorage() { - CPPUNIT_ASSERT(!BtRegistry::getPieceStorage("test").get()); + BtRegistry btRegistry; + CPPUNIT_ASSERT(!btRegistry.getPieceStorage("test").get()); SharedHandle pieceStorage(new MockPieceStorage()); - BtRegistry::registerPieceStorage("test", pieceStorage); + btRegistry.registerPieceStorage("test", pieceStorage); CPPUNIT_ASSERT_EQUAL(pieceStorage.get(), - BtRegistry::getPieceStorage("test").get()); + btRegistry.getPieceStorage("test").get()); } void BtRegistryTest::testGetBtRuntime() { - CPPUNIT_ASSERT(!BtRegistry::getBtRuntime("test").get()); + BtRegistry btRegistry; + CPPUNIT_ASSERT(!btRegistry.getBtRuntime("test").get()); SharedHandle runtime; - BtRegistry::registerBtRuntime("test", runtime); + btRegistry.registerBtRuntime("test", runtime); CPPUNIT_ASSERT_EQUAL(runtime.get(), - BtRegistry::getBtRuntime("test").get()); + btRegistry.getBtRuntime("test").get()); } void BtRegistryTest::testGetBtAnnounce() { - CPPUNIT_ASSERT(!BtRegistry::getBtAnnounce("test").get()); + BtRegistry btRegistry; + CPPUNIT_ASSERT(!btRegistry.getBtAnnounce("test").get()); SharedHandle btAnnounce(new MockBtAnnounce()); - BtRegistry::registerBtAnnounce("test", btAnnounce); + btRegistry.registerBtAnnounce("test", btAnnounce); CPPUNIT_ASSERT_EQUAL(btAnnounce.get(), - BtRegistry::getBtAnnounce("test").get()); + btRegistry.getBtAnnounce("test").get()); } void BtRegistryTest::testGetBtProgressInfoFile() { - CPPUNIT_ASSERT(!BtRegistry::getBtProgressInfoFile("test").get()); + BtRegistry btRegistry; + CPPUNIT_ASSERT(!btRegistry.getBtProgressInfoFile("test").get()); SharedHandle btProgressInfoFile(new MockBtProgressInfoFile()); - BtRegistry::registerBtProgressInfoFile("test", btProgressInfoFile); + btRegistry.registerBtProgressInfoFile("test", btProgressInfoFile); CPPUNIT_ASSERT_EQUAL(btProgressInfoFile.get(), - BtRegistry::getBtProgressInfoFile("test").get()); -} - -void BtRegistryTest::testGetPeerObjectCluster() { - CPPUNIT_ASSERT(!BtRegistry::getPeerObjectCluster("test").get()); - - SharedHandle cluster(new PeerObjectCluster()); - BtRegistry::registerPeerObjectCluster("test", cluster); - - CPPUNIT_ASSERT(BtRegistry::getPeerObjectCluster("test").get()); - - BtRegistry::unregisterPeerObjectCluster("test"); - - CPPUNIT_ASSERT(!BtRegistry::getPeerObjectCluster("test").get()); + btRegistry.getBtProgressInfoFile("test").get()); } } // namespace aria2 diff --git a/test/BtRejectMessageTest.cc b/test/BtRejectMessageTest.cc index a9e1c993..b88ab62c 100644 --- a/test/BtRejectMessageTest.cc +++ b/test/BtRejectMessageTest.cc @@ -1,18 +1,14 @@ #include "BtRejectMessage.h" + +#include + +#include + #include "PeerMessageUtil.h" #include "Peer.h" #include "FileEntry.h" -#include "BtRegistry.h" -#include "PeerObject.h" -#include "BtMessageFactory.h" -#include "BtRequestFactory.h" -#include "BtMessageReceiver.h" -#include "ExtensionMessageFactory.h" -#include "PeerConnection.h" #include "MockBtMessageDispatcher.h" #include "MockBtContext.h" -#include -#include namespace aria2 { @@ -67,37 +63,28 @@ public: typedef SharedHandle MockBtMessageDispatcher2Handle; + SharedHandle _btContext; SharedHandle peer; SharedHandle dispatcher; SharedHandle msg; void setUp() { - BtRegistry::unregisterAll(); peer.reset(new Peer("host", 6969)); peer->allocateSessionResource(1024, 1024*1024); - SharedHandle btContext(new MockBtContext()); - btContext->setInfoHash((const unsigned char*)"12345678901234567890"); - SharedHandle cluster(new PeerObjectCluster()); - BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(), - cluster); - SharedHandle po(new PeerObject()); - PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), po); + _btContext.reset(new MockBtContext()); + _btContext->setInfoHash((const unsigned char*)"12345678901234567890"); + dispatcher.reset(new MockBtMessageDispatcher2()); - PEER_OBJECT(btContext, peer)->btMessageDispatcher = dispatcher; msg.reset(new BtRejectMessage()); msg->setPeer(peer); - msg->setBtContext(btContext); + msg->setBtContext(_btContext); msg->setIndex(1); msg->setBegin(16); msg->setLength(32); msg->setBtMessageDispatcher(dispatcher); } - - void tearDown() { - BtRegistry::unregisterAll(); - } }; @@ -151,11 +138,13 @@ void BtRejectMessageTest::testDoReceivedAction() { RequestSlot slot(1, 16, 32, 2); dispatcher->setRequestSlot(slot); - CPPUNIT_ASSERT(!RequestSlot::isNull(dispatcher->getOutstandingRequest(1, 16, 32))); + CPPUNIT_ASSERT + (!RequestSlot::isNull(dispatcher->getOutstandingRequest(1, 16, 32))); msg->doReceivedAction(); - CPPUNIT_ASSERT(RequestSlot::isNull(dispatcher->getOutstandingRequest(1, 16, 32))); + CPPUNIT_ASSERT + (RequestSlot::isNull(dispatcher->getOutstandingRequest(1, 16, 32))); } void BtRejectMessageTest::testDoReceivedActionNoMatch() { @@ -163,11 +152,13 @@ void BtRejectMessageTest::testDoReceivedActionNoMatch() { RequestSlot slot(2, 16, 32, 2); dispatcher->setRequestSlot(slot); - CPPUNIT_ASSERT(!RequestSlot::isNull(dispatcher->getOutstandingRequest(2, 16, 32))); + CPPUNIT_ASSERT + (!RequestSlot::isNull(dispatcher->getOutstandingRequest(2, 16, 32))); msg->doReceivedAction(); - CPPUNIT_ASSERT(!RequestSlot::isNull(dispatcher->getOutstandingRequest(2, 16, 32))); + CPPUNIT_ASSERT + (!RequestSlot::isNull(dispatcher->getOutstandingRequest(2, 16, 32))); } @@ -175,7 +166,8 @@ void BtRejectMessageTest::testDoReceivedActionFastExtensionDisabled() { RequestSlot slot(1, 16, 32, 2); dispatcher->setRequestSlot(slot); - CPPUNIT_ASSERT(!RequestSlot::isNull(dispatcher->getOutstandingRequest(1, 16, 32))); + CPPUNIT_ASSERT + (!RequestSlot::isNull(dispatcher->getOutstandingRequest(1, 16, 32))); try { msg->doReceivedAction(); CPPUNIT_FAIL("exception must be thrown."); diff --git a/test/BtRequestMessageTest.cc b/test/BtRequestMessageTest.cc index c6d990cb..2ab4c667 100644 --- a/test/BtRequestMessageTest.cc +++ b/test/BtRequestMessageTest.cc @@ -10,15 +10,8 @@ #include "MockPieceStorage.h" #include "MockBtMessageFactory.h" #include "MockBtMessageDispatcher.h" -#include "DefaultBtContext.h" #include "BtAbortOutstandingRequestEvent.h" #include "Peer.h" -#include "BtRegistry.h" -#include "PeerObject.h" -#include "BtMessageReceiver.h" -#include "BtRequestFactory.h" -#include "PeerConnection.h" -#include "ExtensionMessageFactory.h" #include "FileEntry.h" #include "BtHandshakeMessage.h" #include "BtRequestMessageValidator.h" @@ -75,7 +68,10 @@ public: uint32_t begin; size_t length; public: - MockBtMessage2(std::string type, size_t index, uint32_t begin, size_t length):type(type), index(index), begin(begin), length(length) {} + MockBtMessage2(std::string type, size_t index, uint32_t begin, + size_t length) + : + type(type), index(index), begin(begin), length(length) {} }; typedef SharedHandle MockBtMessage2Handle; @@ -84,59 +80,54 @@ public: public: virtual SharedHandle createPieceMessage(size_t index, uint32_t begin, size_t length) { - SharedHandle btMsg(new MockBtMessage2("piece", index, begin, length)); + SharedHandle btMsg + (new MockBtMessage2("piece", index, begin, length)); return btMsg; } virtual SharedHandle createRejectMessage(size_t index, uint32_t begin, size_t length) { - SharedHandle btMsg(new MockBtMessage2("reject", index, begin, length)); + SharedHandle btMsg + (new MockBtMessage2("reject", index, begin, length)); return btMsg; } }; typedef SharedHandle MockBtMessageFactory2Handle; - SharedHandle peer; - SharedHandle dispatcher; + SharedHandle _btContext; + SharedHandle _pieceStorage; + SharedHandle _peer; + SharedHandle _dispatcher; + SharedHandle _messageFactory; SharedHandle msg; void setUp() { - BtRegistry::unregisterAll(); + _btContext.reset(new MockBtContext()); + _btContext->setInfoHash((const unsigned char*)"12345678901234567890"); + _btContext->setPieceLength(16*1024); + _btContext->setTotalLength(256*1024); - SharedHandle btContext(new MockBtContext()); - btContext->setInfoHash((const unsigned char*)"12345678901234567890"); - btContext->setPieceLength(16*1024); - btContext->setTotalLength(256*1024); + _pieceStorage.reset(new MockPieceStorage2()); - SharedHandle pieceStorage(new MockPieceStorage2()); + _peer.reset(new Peer("host", 6969)); + _peer->allocateSessionResource(_btContext->getPieceLength(), + _btContext->getTotalLength()); - BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(), - pieceStorage); + _dispatcher.reset(new MockBtMessageDispatcher()); - peer.reset(new Peer("host", 6969)); - peer->allocateSessionResource(btContext->getPieceLength(), - btContext->getTotalLength()); - SharedHandle cluster(new PeerObjectCluster()); - BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(), - cluster); - SharedHandle po(new PeerObject()); - PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), po); - - dispatcher.reset(new MockBtMessageDispatcher()); - - PEER_OBJECT(btContext, peer)->btMessageDispatcher = dispatcher; - PEER_OBJECT(btContext, peer)->btMessageFactory.reset(new MockBtMessageFactory2()); + _messageFactory.reset(new MockBtMessageFactory2()); msg.reset(new BtRequestMessage()); - msg->setBtContext(btContext); - msg->setPeer(peer); + msg->setBtContext(_btContext); + msg->setPeer(_peer); msg->setIndex(1); msg->setBegin(16); msg->setLength(32); msg->setBlockIndex(2); - msg->setBtMessageDispatcher(dispatcher); - msg->setBtMessageFactory(BT_MESSAGE_FACTORY(btContext, peer)); + msg->setBtMessageDispatcher(_dispatcher); + msg->setBtMessageFactory(_messageFactory); + msg->setPieceStorage(_pieceStorage); } }; @@ -187,11 +178,12 @@ void BtRequestMessageTest::testGetMessage() { } void BtRequestMessageTest::testDoReceivedAction_hasPieceAndAmNotChoking() { - peer->amChoking(false); + _peer->amChoking(false); msg->doReceivedAction(); - CPPUNIT_ASSERT_EQUAL((size_t)1, dispatcher->messageQueue.size()); - MockBtMessage2* pieceMsg = (MockBtMessage2*)dispatcher->messageQueue.front().get(); + CPPUNIT_ASSERT_EQUAL((size_t)1, _dispatcher->messageQueue.size()); + MockBtMessage2* pieceMsg = + (MockBtMessage2*)_dispatcher->messageQueue.front().get(); CPPUNIT_ASSERT_EQUAL(std::string("piece"), pieceMsg->type); CPPUNIT_ASSERT_EQUAL((size_t)1, pieceMsg->index); CPPUNIT_ASSERT_EQUAL((uint32_t)16, pieceMsg->begin); @@ -199,12 +191,13 @@ void BtRequestMessageTest::testDoReceivedAction_hasPieceAndAmNotChoking() { } void BtRequestMessageTest::testDoReceivedAction_hasPieceAndAmChokingAndFastExtensionEnabled() { - peer->amChoking(true); - peer->setFastExtensionEnabled(true); + _peer->amChoking(true); + _peer->setFastExtensionEnabled(true); msg->doReceivedAction(); - CPPUNIT_ASSERT_EQUAL((size_t)1, dispatcher->messageQueue.size()); - MockBtMessage2* pieceMsg = (MockBtMessage2*)dispatcher->messageQueue.front().get(); + CPPUNIT_ASSERT_EQUAL((size_t)1, _dispatcher->messageQueue.size()); + MockBtMessage2* pieceMsg = + (MockBtMessage2*)_dispatcher->messageQueue.front().get(); CPPUNIT_ASSERT_EQUAL(std::string("reject"), pieceMsg->type); CPPUNIT_ASSERT_EQUAL((size_t)1, pieceMsg->index); CPPUNIT_ASSERT_EQUAL((uint32_t)16, pieceMsg->begin); @@ -212,20 +205,21 @@ void BtRequestMessageTest::testDoReceivedAction_hasPieceAndAmChokingAndFastExten } void BtRequestMessageTest::testDoReceivedAction_hasPieceAndAmChokingAndFastExtensionDisabled() { - peer->amChoking(true); + _peer->amChoking(true); msg->doReceivedAction(); - CPPUNIT_ASSERT_EQUAL((size_t)0, dispatcher->messageQueue.size()); + CPPUNIT_ASSERT_EQUAL((size_t)0, _dispatcher->messageQueue.size()); } void BtRequestMessageTest::testDoReceivedAction_doesntHavePieceAndFastExtensionEnabled() { msg->setIndex(2); - peer->amChoking(false); - peer->setFastExtensionEnabled(true); + _peer->amChoking(false); + _peer->setFastExtensionEnabled(true); msg->doReceivedAction(); - CPPUNIT_ASSERT_EQUAL((size_t)1, dispatcher->messageQueue.size()); - MockBtMessage2* pieceMsg = (MockBtMessage2*)dispatcher->messageQueue.front().get(); + CPPUNIT_ASSERT_EQUAL((size_t)1, _dispatcher->messageQueue.size()); + MockBtMessage2* pieceMsg = + (MockBtMessage2*)_dispatcher->messageQueue.front().get(); CPPUNIT_ASSERT_EQUAL(std::string("reject"), pieceMsg->type); CPPUNIT_ASSERT_EQUAL((size_t)2, pieceMsg->index); CPPUNIT_ASSERT_EQUAL((uint32_t)16, pieceMsg->begin); @@ -234,10 +228,10 @@ void BtRequestMessageTest::testDoReceivedAction_doesntHavePieceAndFastExtensionE void BtRequestMessageTest::testDoReceivedAction_doesntHavePieceAndFastExtensionDisabled() { msg->setIndex(2); - peer->amChoking(false); + _peer->amChoking(false); msg->doReceivedAction(); - CPPUNIT_ASSERT_EQUAL((size_t)0, dispatcher->messageQueue.size()); + CPPUNIT_ASSERT_EQUAL((size_t)0, _dispatcher->messageQueue.size()); } void BtRequestMessageTest::testHandleAbortRequestEvent() { diff --git a/test/DHTPeerAnnounceEntryTest.cc b/test/DHTPeerAnnounceEntryTest.cc index 16c4fccf..b679d783 100644 --- a/test/DHTPeerAnnounceEntryTest.cc +++ b/test/DHTPeerAnnounceEntryTest.cc @@ -1,11 +1,13 @@ #include "DHTPeerAnnounceEntry.h" + +#include + +#include + #include "Exception.h" #include "Util.h" -#include "MockBtContext.h" #include "MockPeerStorage.h" -#include "BtRegistry.h" #include "FileEntry.h" -#include namespace aria2 { @@ -18,16 +20,6 @@ class DHTPeerAnnounceEntryTest:public CppUnit::TestFixture { CPPUNIT_TEST(testGetPeers); CPPUNIT_TEST_SUITE_END(); public: - void setUp() - { - BtRegistry::unregisterAll(); - } - - void tearDown() - { - BtRegistry::unregisterAll(); - } - void testRemoveStalePeerAddrEntry(); void testEmpty(); void testAddPeerAddrEntry(); @@ -69,7 +61,7 @@ void DHTPeerAnnounceEntryTest::testEmpty() } { DHTPeerAnnounceEntry entry(infohash); - entry.setBtContext(SharedHandle(new MockBtContext())); + entry.setPeerStorage(SharedHandle(new MockPeerStorage())); CPPUNIT_ASSERT(!entry.empty()); } { @@ -100,8 +92,6 @@ void DHTPeerAnnounceEntryTest::testGetPeers() unsigned char infohash[DHT_ID_LENGTH]; memset(infohash, 0xff, DHT_ID_LENGTH); - SharedHandle ctx(new MockBtContext()); - ctx->setInfoHash(infohash); SharedHandle peerStorage(new MockPeerStorage()); { SharedHandle activePeers[2]; @@ -111,7 +101,6 @@ void DHTPeerAnnounceEntryTest::testGetPeers() peerStorage->setActivePeers(std::deque >(&activePeers[0], &activePeers[2])); } - BtRegistry::registerPeerStorage(ctx->getInfoHashAsString(), peerStorage); DHTPeerAnnounceEntry entry(infohash); { @@ -132,7 +121,7 @@ void DHTPeerAnnounceEntryTest::testGetPeers() CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), peers[1]->ipaddr); CPPUNIT_ASSERT_EQUAL((uint16_t)6882, peers[1]->port); } - entry.setBtContext(ctx); + entry.setPeerStorage(peerStorage); { std::deque > peers; entry.getPeers(peers); diff --git a/test/DHTPeerAnnounceStorageTest.cc b/test/DHTPeerAnnounceStorageTest.cc index 0f93a8a4..8951befb 100644 --- a/test/DHTPeerAnnounceStorageTest.cc +++ b/test/DHTPeerAnnounceStorageTest.cc @@ -1,11 +1,14 @@ #include "DHTPeerAnnounceStorage.h" + +#include + #include "Exception.h" #include "Util.h" #include "MockBtContext.h" +#include "MockPeerStorage.h" #include "DHTConstants.h" #include "Peer.h" #include "FileEntry.h" -#include namespace aria2 { @@ -16,10 +19,6 @@ class DHTPeerAnnounceStorageTest:public CppUnit::TestFixture { CPPUNIT_TEST(testRemovePeerAnnounce); CPPUNIT_TEST_SUITE_END(); public: - void setUp() {} - - void tearDown() {} - void testAddAnnounce(); void testRemovePeerAnnounce(); }; @@ -59,17 +58,21 @@ void DHTPeerAnnounceStorageTest::testRemovePeerAnnounce() SharedHandle ctx1(new MockBtContext()); ctx1->setInfoHash(infohash1); + SharedHandle peerStorage1(new MockPeerStorage()); + SharedHandle ctx2(new MockBtContext()); ctx2->setInfoHash(infohash2); - storage.addPeerAnnounce(infohash1, "192.168.0.1", 6881); - storage.addPeerAnnounce(ctx1); - storage.addPeerAnnounce(ctx2); + SharedHandle peerStorage2(new MockPeerStorage()); - storage.removePeerAnnounce(ctx2); + storage.addPeerAnnounce(infohash1, "192.168.0.1", 6881); + storage.addPeerAnnounce(ctx1->getInfoHash(), peerStorage1); + storage.addPeerAnnounce(ctx2->getInfoHash(), peerStorage2); + + storage.removeLocalPeerAnnounce(ctx2->getInfoHash()); CPPUNIT_ASSERT(!storage.contains(infohash2)); - storage.removePeerAnnounce(ctx1); + storage.removeLocalPeerAnnounce(ctx1->getInfoHash()); CPPUNIT_ASSERT(storage.contains(infohash1)); } diff --git a/test/DefaultBtMessageDispatcherTest.cc b/test/DefaultBtMessageDispatcherTest.cc index 1825a627..8d8ba205 100644 --- a/test/DefaultBtMessageDispatcherTest.cc +++ b/test/DefaultBtMessageDispatcherTest.cc @@ -1,22 +1,19 @@ #include "DefaultBtMessageDispatcher.h" + +#include + +#include + #include "Util.h" #include "Exception.h" #include "MockPieceStorage.h" #include "MockPeerStorage.h" -#include "BtRegistry.h" #include "DefaultBtContext.h" #include "MockBtMessage.h" #include "MockBtMessageFactory.h" #include "prefs.h" #include "BtCancelSendingPieceEvent.h" -#include "PeerObject.h" -#include "BtRequestFactory.h" -#include "BtMessageReceiver.h" -#include "ExtensionMessageFactory.h" -#include "PeerConnection.h" #include "BtHandshakeMessage.h" -#include -#include namespace aria2 { @@ -44,6 +41,7 @@ private: SharedHandle btMessageDispatcher; SharedHandle peerStorage; SharedHandle pieceStorage; + SharedHandle _messageFactory; public: void tearDown() {} @@ -137,26 +135,17 @@ public: btContext->getTotalLength()); peerStorage.reset(new MockPeerStorage()); pieceStorage.reset(new MockPieceStorage()); - BtRegistry::unregisterAll(); - BtRegistry::registerPeerStorage(btContext->getInfoHashAsString(), - peerStorage); - BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(), - pieceStorage); - SharedHandle cluster(new PeerObjectCluster()); - BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(), - cluster); - SharedHandle peerObject(new PeerObject()); - peerObject->btMessageFactory.reset(new MockBtMessageFactory2()); - - PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), peerObject); + _messageFactory.reset(new MockBtMessageFactory2()); btMessageDispatcher.reset(new DefaultBtMessageDispatcher()); - btMessageDispatcher->setCuid(1); - btMessageDispatcher->setBtContext(btContext); btMessageDispatcher->setPeer(peer); + btMessageDispatcher->setBtContext(btContext); + btMessageDispatcher->setPieceStorage(pieceStorage); + btMessageDispatcher->setPeerStorage(peerStorage); + btMessageDispatcher->setBtMessageFactory(_messageFactory); + btMessageDispatcher->setCuid(1); btMessageDispatcher->setMaxUploadSpeedLimit(0); - btMessageDispatcher->setBtMessageFactory(peerObject->btMessageFactory); } }; @@ -235,7 +224,8 @@ void DefaultBtMessageDispatcherTest::testSendMessages_overUploadLimit() { CPPUNIT_ASSERT(!msg2->isSendCalled()); CPPUNIT_ASSERT(msg3->isSendCalled()); - CPPUNIT_ASSERT_EQUAL((size_t)2, btMessageDispatcher->getMessageQueue().size()); + CPPUNIT_ASSERT_EQUAL((size_t)2, + btMessageDispatcher->getMessageQueue().size()); } void DefaultBtMessageDispatcherTest::testSendMessages_sendingInProgress() { @@ -259,7 +249,8 @@ void DefaultBtMessageDispatcherTest::testSendMessages_sendingInProgress() { CPPUNIT_ASSERT(msg2->isSendCalled()); CPPUNIT_ASSERT(!msg3->isSendCalled()); - CPPUNIT_ASSERT_EQUAL((size_t)2, btMessageDispatcher->getMessageQueue().size()); + CPPUNIT_ASSERT_EQUAL((size_t)2, + btMessageDispatcher->getMessageQueue().size()); } void DefaultBtMessageDispatcherTest::testDoCancelSendingPieceAction() { @@ -287,21 +278,17 @@ void DefaultBtMessageDispatcherTest::testCheckRequestSlotAndDoNecessaryThing() { SharedHandle pieceStorage(new MockPieceStorage2()); pieceStorage->setPiece(piece); - BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(), - pieceStorage); - - btMessageDispatcher.reset(new DefaultBtMessageDispatcher()); - btMessageDispatcher->setCuid(1); - btMessageDispatcher->setBtContext(btContext); - btMessageDispatcher->setPeer(peer); - btMessageDispatcher->setRequestTimeout(60); + btMessageDispatcher->setRequestTimeout(60); + btMessageDispatcher->setPieceStorage(pieceStorage); btMessageDispatcher->addOutstandingRequest(slot); btMessageDispatcher->checkRequestSlotAndDoNecessaryThing(); - CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->getMessageQueue().size()); - CPPUNIT_ASSERT_EQUAL((size_t)1, btMessageDispatcher->getRequestSlots().size()); + CPPUNIT_ASSERT_EQUAL((size_t)0, + btMessageDispatcher->getMessageQueue().size()); + CPPUNIT_ASSERT_EQUAL((size_t)1, + btMessageDispatcher->getRequestSlots().size()); } void DefaultBtMessageDispatcherTest::testCheckRequestSlotAndDoNecessaryThing_timeout() { @@ -316,23 +303,17 @@ void DefaultBtMessageDispatcherTest::testCheckRequestSlotAndDoNecessaryThing_tim SharedHandle pieceStorage(new MockPieceStorage2()); pieceStorage->setPiece(piece); - BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(), - pieceStorage); - btMessageDispatcher.reset(new DefaultBtMessageDispatcher()); - btMessageDispatcher->setCuid(1); - btMessageDispatcher->setBtContext(btContext); - btMessageDispatcher->setPeer(peer); btMessageDispatcher->setRequestTimeout(60); - btMessageDispatcher->setBtMessageFactory(BT_MESSAGE_FACTORY(btContext, - peer)); - + btMessageDispatcher->setPieceStorage(pieceStorage); btMessageDispatcher->addOutstandingRequest(slot); btMessageDispatcher->checkRequestSlotAndDoNecessaryThing(); - CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->getMessageQueue().size()); - CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->getRequestSlots().size()); + CPPUNIT_ASSERT_EQUAL((size_t)0, + btMessageDispatcher->getMessageQueue().size()); + CPPUNIT_ASSERT_EQUAL((size_t)0, + btMessageDispatcher->getRequestSlots().size()); CPPUNIT_ASSERT_EQUAL(false, piece->isBlockUsed(0)); CPPUNIT_ASSERT_EQUAL(true, peer->snubbing()); } @@ -345,24 +326,17 @@ void DefaultBtMessageDispatcherTest::testCheckRequestSlotAndDoNecessaryThing_com SharedHandle pieceStorage(new MockPieceStorage2()); pieceStorage->setPiece(piece); - BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(), - pieceStorage); - - btMessageDispatcher.reset(new DefaultBtMessageDispatcher()); - btMessageDispatcher->setCuid(1); - btMessageDispatcher->setBtContext(btContext); - btMessageDispatcher->setPeer(peer); btMessageDispatcher->setRequestTimeout(60); - btMessageDispatcher->setBtMessageFactory(BT_MESSAGE_FACTORY(btContext, - peer)); - + btMessageDispatcher->setPieceStorage(pieceStorage); btMessageDispatcher->addOutstandingRequest(slot); btMessageDispatcher->checkRequestSlotAndDoNecessaryThing(); - CPPUNIT_ASSERT_EQUAL((size_t)1, btMessageDispatcher->getMessageQueue().size()); - CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->getRequestSlots().size()); + CPPUNIT_ASSERT_EQUAL((size_t)1, + btMessageDispatcher->getMessageQueue().size()); + CPPUNIT_ASSERT_EQUAL((size_t)0, + btMessageDispatcher->getRequestSlots().size()); } void DefaultBtMessageDispatcherTest::testIsSendingInProgress() { @@ -378,7 +352,8 @@ void DefaultBtMessageDispatcherTest::testIsSendingInProgress() { void DefaultBtMessageDispatcherTest::testCountOutstandingRequest() { RequestSlot slot(0, 0, MY_PIECE_LENGTH, 0); btMessageDispatcher->addOutstandingRequest(slot); - CPPUNIT_ASSERT_EQUAL((size_t)1, btMessageDispatcher->countOutstandingRequest()); + CPPUNIT_ASSERT_EQUAL((size_t)1, + btMessageDispatcher->countOutstandingRequest()); } void DefaultBtMessageDispatcherTest::testIsOutstandingRequest() { @@ -401,7 +376,8 @@ void DefaultBtMessageDispatcherTest::testGetOutstandingRequest() { RequestSlot s3 = btMessageDispatcher->getOutstandingRequest(1, 1024, 17*1024); CPPUNIT_ASSERT(RequestSlot::isNull(s3)); - RequestSlot s4 = btMessageDispatcher->getOutstandingRequest(1, 2*1024, 16*1024); + RequestSlot s4 = + btMessageDispatcher->getOutstandingRequest(1, 2*1024, 16*1024); CPPUNIT_ASSERT(RequestSlot::isNull(s4)); RequestSlot s5 = btMessageDispatcher->getOutstandingRequest(2, 1024, 16*1024); diff --git a/test/DefaultBtMessageFactoryTest.cc b/test/DefaultBtMessageFactoryTest.cc index 3de44c06..abf8a420 100644 --- a/test/DefaultBtMessageFactoryTest.cc +++ b/test/DefaultBtMessageFactoryTest.cc @@ -1,21 +1,19 @@ #include "DefaultBtMessageFactory.h" + +#include + +#include + +#include + #include "Peer.h" #include "PeerMessageUtil.h" -#include "BtRegistry.h" #include "MockBtContext.h" #include "MockExtensionMessageFactory.h" #include "BtExtendedMessage.h" #include "BtPortMessage.h" -#include "PeerObject.h" -#include "BtRequestFactory.h" -#include "BtMessageDispatcher.h" -#include "BtMessageReceiver.h" -#include "PeerConnection.h" #include "Exception.h" #include "FileEntry.h" -#include -#include -#include namespace aria2 { @@ -28,34 +26,26 @@ class DefaultBtMessageFactoryTest:public CppUnit::TestFixture { private: SharedHandle _btContext; SharedHandle _peer; + SharedHandle _exmsgFactory; + SharedHandle _factory; public: void setUp() { - BtRegistry::unregisterAll(); - SharedHandle btContext(new MockBtContext()); + _btContext.reset(new MockBtContext()); unsigned char infohash[20]; memset(infohash, 0, sizeof(infohash)); - btContext->setInfoHash(infohash); - _btContext = btContext; + _btContext->setInfoHash(infohash); _peer.reset(new Peer("192.168.0.1", 6969)); _peer->allocateSessionResource(1024, 1024*1024); _peer->setExtendedMessagingEnabled(true); - SharedHandle exmsgFactory - (new MockExtensionMessageFactory()); - SharedHandle cluster(new PeerObjectCluster()); - BtRegistry::registerPeerObjectCluster(_btContext->getInfoHashAsString(), - cluster); - SharedHandle peerObject(new PeerObject()); - peerObject->extensionMessageFactory = exmsgFactory; + _exmsgFactory.reset(new MockExtensionMessageFactory()); - PEER_OBJECT_CLUSTER(_btContext)->registerHandle(_peer->getID(), peerObject); - } - - void tearDown() - { - BtRegistry::unregisterAll(); + _factory.reset(new DefaultBtMessageFactory()); + _factory->setBtContext(_btContext); + _factory->setPeer(_peer); + _factory->setExtensionMessageFactory(_exmsgFactory); } void testCreateBtMessage_BtExtendedMessage(); @@ -67,26 +57,22 @@ CPPUNIT_TEST_SUITE_REGISTRATION(DefaultBtMessageFactoryTest); void DefaultBtMessageFactoryTest::testCreateBtMessage_BtExtendedMessage() { - - DefaultBtMessageFactory factory; - factory.setBtContext(_btContext); - factory.setPeer(_peer); - // payload:{4:name3:foo}->11bytes std::string payload = "4:name3:foo"; char msg[17];// 6+11bytes - PeerMessageUtil::createPeerMessageString((unsigned char*)msg, sizeof(msg), 13, 20); + PeerMessageUtil::createPeerMessageString((unsigned char*)msg, sizeof(msg), + 13, 20); msg[5] = 1; // Set dummy extended message ID 1 memcpy(msg+6, payload.c_str(), payload.size()); SharedHandle m (dynamic_pointer_cast - (factory.createBtMessage((const unsigned char*)msg+4, sizeof(msg)))); + (_factory->createBtMessage((const unsigned char*)msg+4, sizeof(msg)))); try { // disable extended messaging _peer->setExtendedMessagingEnabled(false); - factory.createBtMessage((const unsigned char*)msg+4, sizeof(msg)); + _factory->createBtMessage((const unsigned char*)msg+4, sizeof(msg)); CPPUNIT_FAIL("exception must be thrown."); } catch(Exception& e) { std::cerr << e.stackTrace() << std::endl; @@ -95,10 +81,6 @@ void DefaultBtMessageFactoryTest::testCreateBtMessage_BtExtendedMessage() void DefaultBtMessageFactoryTest::testCreatePortMessage() { - DefaultBtMessageFactory factory; - factory.setBtContext(_btContext); - factory.setPeer(_peer); - { unsigned char data[7]; PeerMessageUtil::createPeerMessageString(data, sizeof(data), 3, 9); @@ -106,7 +88,7 @@ void DefaultBtMessageFactoryTest::testCreatePortMessage() try { SharedHandle m (dynamic_pointer_cast - (factory.createBtMessage(&data[4], sizeof(data)-4))); + (_factory->createBtMessage(&data[4], sizeof(data)-4))); CPPUNIT_ASSERT(!m.isNull()); CPPUNIT_ASSERT_EQUAL((uint16_t)6881, m->getPort()); } catch(Exception& e) { @@ -115,7 +97,7 @@ void DefaultBtMessageFactoryTest::testCreatePortMessage() } { SharedHandle m - (dynamic_pointer_cast(factory.createPortMessage(6881))); + (dynamic_pointer_cast(_factory->createPortMessage(6881))); CPPUNIT_ASSERT_EQUAL((uint16_t)6881, m->getPort()); } } diff --git a/test/DefaultBtProgressInfoFileTest.cc b/test/DefaultBtProgressInfoFileTest.cc index 5a672fd1..536b3ea5 100644 --- a/test/DefaultBtProgressInfoFileTest.cc +++ b/test/DefaultBtProgressInfoFileTest.cc @@ -1,4 +1,9 @@ #include "DefaultBtProgressInfoFile.h" + +#include + +#include + #include "Option.h" #include "Util.h" #include "Exception.h" @@ -8,13 +13,10 @@ #include "MockPeerStorage.h" #include "MockPieceStorage.h" #include "BtRuntime.h" -#include "BtRegistry.h" #include "prefs.h" #include "SingleFileDownloadContext.h" #include "Piece.h" #include "FileEntry.h" -#include -#include namespace aria2 { @@ -36,16 +38,16 @@ private: #ifdef ENABLE_BITTORRENT SharedHandle _btContext; + + SharedHandle _peerStorage; + + SharedHandle _btRuntime; #endif // ENABLE_BITTORRENT SharedHandle _pieceStorage; SharedHandle