summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2008-11-03 06:49:02 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2008-11-03 06:49:02 +0000
commit7cf589e78483e6ca7daff2ddd1a30af8091a4448 (patch)
treed595de6899aa7ddad9ad6242c9f50dd6c2a85e22 /src
parent93edc7bd202df92174cc4ce3b8c0c0c7684170fd (diff)
2008-11-03 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
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
Diffstat (limited to 'src')
-rw-r--r--src/AbstractBtMessage.cc2
-rw-r--r--src/ActivePeerConnectionCommand.cc79
-rw-r--r--src/ActivePeerConnectionCommand.h22
-rw-r--r--src/BtContextAwareCommand.cc56
-rw-r--r--src/BtContextAwareCommand.h67
-rw-r--r--src/BtExtendedMessage.cc11
-rw-r--r--src/BtExtendedMessage.h10
-rw-r--r--src/BtInterestedMessage.cc5
-rw-r--r--src/BtInterestedMessage.h1
-rw-r--r--src/BtNotInterestedMessage.cc9
-rw-r--r--src/BtNotInterestedMessage.h5
-rw-r--r--src/BtRegistry.cc36
-rw-r--r--src/BtRegistry.h122
-rw-r--r--src/BtSeederStateChoke.cc24
-rw-r--r--src/BtSetup.cc94
-rw-r--r--src/DHTGetPeersCommand.cc22
-rw-r--r--src/DHTGetPeersCommand.h16
-rw-r--r--src/DHTPeerAnnounceEntry.cc27
-rw-r--r--src/DHTPeerAnnounceEntry.h12
-rw-r--r--src/DHTPeerAnnounceStorage.cc27
-rw-r--r--src/DHTPeerAnnounceStorage.h9
-rw-r--r--src/DHTPeerLookupTask.cc15
-rw-r--r--src/DHTPeerLookupTask.h4
-rw-r--r--src/DHTSetup.cc9
-rw-r--r--src/DHTSetup.h2
-rw-r--r--src/DHTTaskFactory.h6
-rw-r--r--src/DHTTaskFactoryImpl.cc8
-rw-r--r--src/DHTTaskFactoryImpl.h4
-rw-r--r--src/DefaultBtAnnounce.cc6
-rw-r--r--src/DefaultBtInteractive.cc113
-rw-r--r--src/DefaultBtInteractive.h51
-rw-r--r--src/DefaultBtMessageDispatcher.cc27
-rw-r--r--src/DefaultBtMessageDispatcher.h8
-rw-r--r--src/DefaultBtMessageFactory.cc53
-rw-r--r--src/DefaultBtMessageFactory.h16
-rw-r--r--src/DefaultBtMessageReceiver.cc5
-rw-r--r--src/DefaultBtProgressInfoFile.cc29
-rw-r--r--src/DefaultBtProgressInfoFile.h9
-rw-r--r--src/DefaultBtRequestFactory.cc18
-rw-r--r--src/DefaultBtRequestFactory.h4
-rw-r--r--src/DefaultExtensionMessageFactory.cc12
-rw-r--r--src/DefaultExtensionMessageFactory.h5
-rw-r--r--src/DefaultPeerStorage.cc22
-rw-r--r--src/DefaultPeerStorage.h13
-rw-r--r--src/DownloadEngine.cc29
-rw-r--r--src/DownloadEngine.h20
-rw-r--r--src/InitiatorMSEHandshakeCommand.cc56
-rw-r--r--src/InitiatorMSEHandshakeCommand.h18
-rw-r--r--src/MSEHandshake.cc11
-rw-r--r--src/MSEHandshake.h7
-rw-r--r--src/Makefile.am1
-rw-r--r--src/Makefile.in8
-rw-r--r--src/PeerChokeCommand.cc25
-rw-r--r--src/PeerChokeCommand.h18
-rw-r--r--src/PeerInitiateConnectionCommand.cc80
-rw-r--r--src/PeerInitiateConnectionCommand.h20
-rw-r--r--src/PeerInteractionCommand.cc123
-rw-r--r--src/PeerInteractionCommand.h20
-rw-r--r--src/PeerObject.h63
-rw-r--r--src/PeerReceiveHandshakeCommand.cc52
-rw-r--r--src/PeerReceiveHandshakeCommand.h16
-rw-r--r--src/ReceiverMSEHandshakeCommand.cc10
-rw-r--r--src/RequestGroup.cc93
-rw-r--r--src/RequestGroup.h16
-rw-r--r--src/RequestGroupMan.cc14
-rw-r--r--src/RequestGroupMan.h2
-rw-r--r--src/SeedCheckCommand.cc25
-rw-r--r--src/SeedCheckCommand.h13
-rw-r--r--src/ShareRatioSeedCriteria.h33
-rw-r--r--src/SimpleBtMessage.cc1
-rw-r--r--src/TrackerWatcherCommand.cc77
-rw-r--r--src/TrackerWatcherCommand.h26
-rw-r--r--src/UTPexExtensionMessage.cc12
-rw-r--r--src/UTPexExtensionMessage.h14
74 files changed, 1161 insertions, 837 deletions
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<BtMessageValidator> AbstractBtMessage::getBtMessageValidator() cons
void AbstractBtMessage::setBtContext(const SharedHandle<BtContext>& btContext)
{
this->btContext = btContext;
- this->pieceStorage = PIECE_STORAGE(btContext);
}
SharedHandle<BtContext> 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 = btRuntime;
+}
+
+void ActivePeerConnectionCommand::setPieceStorage
+(const SharedHandle<PieceStorage>& pieceStorage)
+{
+ _pieceStorage = pieceStorage;
+}
+
+void ActivePeerConnectionCommand::setPeerStorage
+(const SharedHandle<PeerStorage>& peerStorage)
+{
+ _peerStorage = peerStorage;
+}
+
+void ActivePeerConnectionCommand::setBtAnnounce
+(const SharedHandle<BtAnnounce>& 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> _btContext;
+ SharedHandle<BtRuntime> _btRuntime;
+ SharedHandle<PieceStorage> _pieceStorage;
+ SharedHandle<PeerStorage> _peerStorage;
+ SharedHandle<BtAnnounce> _btAnnounce;
+
time_t interval; // UNIT: sec
DownloadEngine* e;
Time checkPoint;
@@ -73,6 +83,14 @@ public:
{
_numNewConnection = numNewConnection;
}
+
+ void setBtRuntime(const SharedHandle<BtRuntime>& btRuntime);
+
+ void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
+
+ void setPeerStorage(const SharedHandle<PeerStorage>& peerStorage);
+
+ void setBtAnnounce(const SharedHandle<BtAnnounce>& 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 @@
-/* <!-- copyright */
-/*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2006 Tatsuhiro Tsujikawa
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * In addition, as a special exception, the copyright holders give
- * permission to link the code of portions of this program with the
- * OpenSSL library under certain conditions as described in each
- * individual source file, and distribute linked combinations
- * including the two.
- * You must obey the GNU General Public License in all respects
- * for all of the code used other than OpenSSL. If you modify
- * file(s) with this exception, you may extend this exception to your
- * version of the file(s), but you are not obligated to do so. If you
- * do not wish to do so, delete this exception statement from your
- * version. If you delete this exception statement from all source
- * files in the program, then also delete it here.
- */
-/* copyright --> */
-#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 @@
-/* <!-- copyright */
-/*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2006 Tatsuhiro Tsujikawa
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * In addition, as a special exception, the copyright holders give
- * permission to link the code of portions of this program with the
- * OpenSSL library under certain conditions as described in each
- * individual source file, and distribute linked combinations
- * including the two.
- * You must obey the GNU General Public License in all respects
- * for all of the code used other than OpenSSL. If you modify
- * file(s) with this exception, you may extend this exception to your
- * version of the file(s), but you are not obligated to do so. If you
- * do not wish to do so, delete this exception statement from your
- * version. If you delete this exception statement from all source
- * files in the program, then also delete it here.
- */
-/* copyright --> */
-#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> btContext;
- SharedHandle<BtRuntime> btRuntime;
- SharedHandle<PieceStorage> pieceStorage;
- SharedHandle<PeerStorage> peerStorage;
- SharedHandle<BtAnnounce> btAnnounce;
- SharedHandle<BtProgressInfoFile> btProgressInfoFile;
-public:
- BtContextAwareCommand(const SharedHandle<BtContext>& 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<ExtensionMessageFactory>& 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<BtExtendedMessage> BtExtendedMessageHandle;
@@ -59,10 +60,11 @@ public:
static const uint8_t ID = 20;
- static BtExtendedMessageHandle create(const SharedHandle<BtContext>& btContext,
- const SharedHandle<Peer>& peer,
- const unsigned char* data,
- size_t dataLength);
+ static BtExtendedMessageHandle create
+ (const SharedHandle<ExtensionMessageFactory>& factory,
+ const SharedHandle<Peer>& 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 = 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<BtNotInterestedMessage> BtNotInterestedMessageHandle;
@@ -47,6 +48,8 @@ class BtNotInterestedMessage : public SimpleBtMessage {
private:
unsigned char* msg;
+ SharedHandle<PeerStorage> _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>& 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<SharedHandle<BtContext> > 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 <string>
#include <map>
+#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<std::string, PeerStorage> PeerStorageMap;
typedef HandleRegistry<std::string, PieceStorage> PieceStorageMap;
@@ -58,104 +59,49 @@ typedef HandleRegistry<std::string, BtRuntime> BtRuntimeMap;
typedef HandleRegistry<std::string, BtProgressInfoFile> BtProgressInfoFileMap;
typedef HandleRegistry<std::string, BtContext> BtContextMap;
-// for BtMessageFactory
-typedef HandleRegistry<std::string, PeerObject> PeerObjectCluster;
-typedef SharedHandle<PeerObjectCluster> PeerObjectClusterHandle;
-typedef HandleRegistry<std::string, PeerObjectCluster> 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<BtContext> getBtContext(const std::string& key);
- static void registerBtContext(const std::string& key,
- const SharedHandle<BtContext>& btContext);
-
- static SharedHandle<PeerStorage> getPeerStorage(const std::string& key);
- static void registerPeerStorage(const std::string& key,
- const SharedHandle<PeerStorage>& peer);
+ BtRegistry();
+
+ SharedHandle<BtContext> getBtContext(const std::string& key);
+ void registerBtContext(const std::string& key,
+ const SharedHandle<BtContext>& btContext);
+
+ SharedHandle<PeerStorage> getPeerStorage(const std::string& key);
+ void registerPeerStorage(const std::string& key,
+ const SharedHandle<PeerStorage>& peer);
- static SharedHandle<PieceStorage> getPieceStorage(const std::string& key);
- static void registerPieceStorage(const std::string& key,
- const SharedHandle<PieceStorage>& pieceStorage);
+ SharedHandle<PieceStorage> getPieceStorage(const std::string& key);
+ void registerPieceStorage(const std::string& key,
+ const SharedHandle<PieceStorage>& pieceStorage);
- static SharedHandle<BtRuntime> getBtRuntime(const std::string& key);
- static void registerBtRuntime(const std::string& key,
- const SharedHandle<BtRuntime>& btRuntime);
+ SharedHandle<BtRuntime> getBtRuntime(const std::string& key);
+ void registerBtRuntime(const std::string& key,
+ const SharedHandle<BtRuntime>& btRuntime);
- static SharedHandle<BtAnnounce> getBtAnnounce(const std::string& key);
- static void registerBtAnnounce(const std::string& key,
+ SharedHandle<BtAnnounce> getBtAnnounce(const std::string& key);
+ void registerBtAnnounce(const std::string& key,
const SharedHandle<BtAnnounce>& btAnnounce);
- static SharedHandle<BtProgressInfoFile> getBtProgressInfoFile(const std::string& key);
- static void registerBtProgressInfoFile(const std::string& key,
- const SharedHandle<BtProgressInfoFile>& btProgressInfoFile);
-
- static std::deque<SharedHandle<BtContext> > getAllBtContext();
+ SharedHandle<BtProgressInfoFile>
+ getBtProgressInfoFile(const std::string& key);
+ void registerBtProgressInfoFile(const std::string& key,
+ const SharedHandle<BtProgressInfoFile>& file);
- // for PeerObject
- static PeerObjectClusterHandle
- getPeerObjectCluster(const std::string& key);
+ std::deque<SharedHandle<BtContext> > getAllBtContext();
- static void
- registerPeerObjectCluster(const std::string& key,
- const PeerObjectClusterHandle& cluster);
+ void unregisterAll();
- 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 <algorithm>
+
#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 <algorithm>
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<Command*>& commands,
if(btContext.isNull()) {
return;
}
+ SharedHandle<BtRegistry> btRegistry = e->getBtRegistry();
+ SharedHandle<PieceStorage> pieceStorage =
+ btRegistry->getPieceStorage(btContext->getInfoHashAsString());
+ SharedHandle<PeerStorage> peerStorage =
+ btRegistry->getPeerStorage(btContext->getInfoHashAsString());
+ SharedHandle<BtRuntime> btRuntime =
+ btRegistry->getBtRuntime(btContext->getInfoHashAsString());
+ SharedHandle<BtAnnounce> btAnnounce =
+ btRegistry->getBtAnnounce(btContext->getInfoHashAsString());
+
// commands
- commands.push_back(new TrackerWatcherCommand(CUIDCounterSingletonHolder::instance()->newID(),
- requestGroup,
- e,
- btContext));
- commands.push_back(new PeerChokeCommand(CUIDCounterSingletonHolder::instance()->newID(),
- e,
- btContext));
+ {
+ 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(new ActivePeerConnectionCommand(CUIDCounterSingletonHolder::instance()->newID(),
- requestGroup, e, btContext, 10));
+ 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<UnionSeedCriteria> unionCri(new UnionSeedCriteria());
@@ -100,16 +140,26 @@ void BtSetup::setup(std::deque<Command*>& commands,
{
double ratio = option->getAsDouble(PREF_SEED_RATIO);
if(ratio > 0.0) {
- SharedHandle<SeedCriteria> cri(new ShareRatioSeedCriteria(option->getAsDouble(PREF_SEED_RATIO), btContext));
+ SharedHandle<ShareRatioSeedCriteria> 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<Command*>& 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<Command*>& 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<DHTPeerLookupTask>(_taskFactory->createPeerLookupTask(btContext));
+ _btContext->getInfoHashAsString().c_str());
+ _task = dynamic_pointer_cast<DHTPeerLookupTask>
+ (_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<DHTTaskFactory>& task
_taskFactory = taskFactory;
}
+void DHTGetPeersCommand::setBtRuntime(const SharedHandle<BtRuntime>& btRuntime)
+{
+ _btRuntime = btRuntime;
+}
+
+void DHTGetPeersCommand::setPeerStorage(const SharedHandle<PeerStorage>& 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> _btContext;
+
+ SharedHandle<BtRuntime> _btRuntime;
+
+ SharedHandle<PeerStorage> _peerStorage;
+
DownloadEngine* _e;
SharedHandle<DHTTaskQueue> _taskQueue;
@@ -82,6 +90,10 @@ public:
void setTaskQueue(const SharedHandle<DHTTaskQueue>& taskQueue);
void setTaskFactory(const SharedHandle<DHTTaskFactory>& taskFactory);
+
+ void setBtRuntime(const SharedHandle<BtRuntime>& btRuntime);
+
+ void setPeerStorage(const SharedHandle<PeerStorage>& 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 <cstring>
#include <algorithm>
+#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<BtContext>& btCtx)
+void DHTPeerAnnounceEntry::clearLocal()
{
- _btCtx = btCtx;
+ _peerStorage.reset();
+}
+
+void DHTPeerAnnounceEntry::setPeerStorage
+(const SharedHandle<PeerStorage>& 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<SharedHandle<Peer> >& peers) const
@@ -109,11 +115,8 @@ void DHTPeerAnnounceEntry::getPeers(std::deque<SharedHandle<Peer> >& peers) cons
SharedHandle<Peer> peer(new Peer((*i).getIPAddress(), (*i).getPort()));
peers.push_back(peer);
}
- if(!_btCtx.isNull()) {
- SharedHandle<PeerStorage> 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 <deque>
+
#include "SharedHandle.h"
#include "DHTConstants.h"
#include "PeerAddrEntry.h"
#include "TimeA2.h"
-#include <deque>
namespace aria2 {
class Peer;
-class BtContext;
+class PeerStorage;
class DHTPeerAnnounceEntry {
private:
@@ -53,7 +55,7 @@ private:
std::deque<PeerAddrEntry> _peerAddrEntries;
- SharedHandle<BtContext> _btCtx;
+ SharedHandle<PeerStorage> _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<BtContext>& btCtx);
+ void clearLocal();
+
+ void setPeerStorage(const SharedHandle<PeerStorage>& 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 <cstring>
+#include <algorithm>
+
#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 <cstring>
-#include <algorithm>
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>& 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<DHTPeerAnnounceEntry> entry =
+ getPeerAnnounceEntry(infoHash);
+ entry->setPeerStorage(peerStorage);
}
-void DHTPeerAnnounceStorage::removePeerAnnounce(const BtContextHandle& ctx)
+void DHTPeerAnnounceStorage::removeLocalPeerAnnounce
+(const unsigned char* infoHash)
{
- SharedHandle<DHTPeerAnnounceEntry> entry(new DHTPeerAnnounceEntry(
- ctx->getInfoHash()));
+ SharedHandle<DHTPeerAnnounceEntry> entry(new DHTPeerAnnounceEntry(infoHash));
std::deque<SharedHandle<DHTPeerAnnounceEntry> >::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<BtContext>());
+ 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<BtContext>& ctx);
+ void addPeerAnnounce(const unsigned char* infoHash,
+ const SharedHandle<PeerStorage>& peerStorage);
- // give 0 to DHTPeerAnnounceEntry::setBtContext().
+ // Call DHTPeerAnnounceEntry::clearLocal() for infoHash if found.
// If DHTPeerAnnounceEntry is empty, it is erased.
- void removePeerAnnounce(const SharedHandle<BtContext>& 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>& btContext):
DHTAbstractNodeLookupTask(btContext->getInfoHash()),
- _ctx(btContext),
- _peerStorage(PEER_STORAGE(btContext)),
- _btRuntime(BT_RUNTIME(btContext)) {}
+ _ctx(btContext) {}
void
DHTPeerLookupTask::getNodesFromMessage(std::deque<SharedHandle<DHTNode> >& nodes,
@@ -110,4 +107,14 @@ const std::deque<SharedHandle<Peer> >& DHTPeerLookupTask::getPeers() const
return _peers;
}
+void DHTPeerLookupTask::setBtRuntime(const SharedHandle<BtRuntime>& btRuntime)
+{
+ _btRuntime = btRuntime;
+}
+
+void DHTPeerLookupTask::setPeerStorage(const SharedHandle<PeerStorage>& 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<SharedHandle<Peer> >& getPeers() const;
+
+ void setBtRuntime(const SharedHandle<BtRuntime>& btRuntime);
+
+ void setPeerStorage(const SharedHandle<PeerStorage>& 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 <fstream>
+#include <algorithm>
+
#include "LogFactory.h"
#include "Logger.h"
#include "Util.h"
@@ -66,12 +70,11 @@
#include "DlAbortEx.h"
#include "RecoverableException.h"
#include "a2functional.h"
-#include <fstream>
-#include <algorithm>
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<DHTTask> createBucketRefreshTask() = 0;
virtual SharedHandle<DHTTask>
- createPeerLookupTask(const SharedHandle<BtContext>& ctx) = 0;
+ createPeerLookupTask(const SharedHandle<BtContext>& ctx,
+ const SharedHandle<BtRuntime>& btRuntime,
+ const SharedHandle<PeerStorage>& peerStorage) = 0;
virtual SharedHandle<DHTTask>
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<DHTTask>
-DHTTaskFactoryImpl::createPeerLookupTask(const SharedHandle<BtContext>& ctx)
+DHTTaskFactoryImpl::createPeerLookupTask
+(const SharedHandle<BtContext>& ctx,
+ const SharedHandle<BtRuntime>& btRuntime,
+ const SharedHandle<PeerStorage>& peerStorage)
{
SharedHandle<DHTPeerLookupTask> 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<DHTTask> createBucketRefreshTask();
virtual SharedHandle<DHTTask>
- createPeerLookupTask(const SharedHandle<BtContext>& ctx);
+ createPeerLookupTask(const SharedHandle<BtContext>& ctx,
+ const SharedHandle<BtRuntime>& btRuntime,
+ const SharedHandle<PeerStorage>& peerStorage);
virtual SharedHandle<DHTTask>
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 <cstring>
+
#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 <cstring>
namespace aria2 {
-DefaultBtInteractive::DefaultBtInteractive(const SharedHandle<BtContext>& btContext, const SharedHandle<Peer>& peer):
- BtContextAwareCommand(btContext),
+DefaultBtInteractive::DefaultBtInteractive
+(const SharedHandle<BtContext>& btContext, const SharedHandle<Peer>& peer)
+ :
+ _btContext(btContext),
peer(peer),
logger(LogFactory::getInstance()),
allowedFastSetSize(10),
@@ -84,11 +85,10 @@ DefaultBtInteractive::DefaultBtInteractive(const SharedHandle<BtContext>& btCont
DefaultBtInteractive::~DefaultBtInteractive() {}
-
void DefaultBtInteractive::initiateHandshake() {
SharedHandle<BtMessage> 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<BtMessage>();
}
- 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<BtMessage> 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<size_t> fastSet;
- btContext->computeFastSet(fastSet, peer->ipaddr, allowedFastSetSize);
+ _btContext->computeFastSet(fastSet, peer->ipaddr, allowedFastSetSize);
for(std::deque<size_t>::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<size_t> 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<size_t>::iterator itr = indexes.begin(); itr != indexes.end(); itr++) {
+ for(std::deque<size_t>::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> 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> 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<SharedHandle<Peer> >::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 = btRuntime;
+}
+
+void DefaultBtInteractive::setPieceStorage
+(const SharedHandle<PieceStorage>& pieceStorage)
+{
+ _pieceStorage = pieceStorage;
+}
+
+void DefaultBtInteractive::setPeerStorage
+(const SharedHandle<PeerStorage>& peerStorage)
+{
+ _peerStorage = peerStorage;
+}
+
void DefaultBtInteractive::setPeer(const SharedHandle<Peer>& peer)
{
this->peer = peer;
}
-void DefaultBtInteractive::setBtMessageReceiver(const BtMessageReceiverWeakHandle& receiver)
+void DefaultBtInteractive::setBtMessageReceiver
+(const SharedHandle<BtMessageReceiver>& receiver)
{
this->btMessageReceiver = receiver;
}
-void DefaultBtInteractive::setDispatcher(const BtMessageDispatcherWeakHandle& dispatcher)
+void DefaultBtInteractive::setDispatcher
+(const SharedHandle<BtMessageDispatcher>& dispatcher)
{
this->dispatcher = dispatcher;
}
-void DefaultBtInteractive::setBtRequestFactory(const BtRequestFactoryWeakHandle& factory)
+void DefaultBtInteractive::setBtRequestFactory
+(const SharedHandle<BtRequestFactory>& factory)
{
this->btRequestFactory = factory;
}
-void DefaultBtInteractive::setPeerConnection(const PeerConnectionWeakHandle& peerConnection)
+void DefaultBtInteractive::setPeerConnection
+(const SharedHandle<PeerConnection>& peerConnection)
{
this->peerConnection = peerConnection;
}
-void DefaultBtInteractive::setBtMessageFactory(const BtMessageFactoryWeakHandle& factory)
+void DefaultBtInteractive::setExtensionMessageFactory
+(const SharedHandle<ExtensionMessageFactory>& factory)
+{
+ _extensionMessageFactory = factory;
+}
+
+void DefaultBtInteractive::setBtMessageFactory
+(const SharedHandle<BtMessageFactory>& 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 <limits.h>
+#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> _btContext;
+
+ SharedHandle<BtRuntime> _btRuntime;
+
+ SharedHandle<PieceStorage> _pieceStorage;
+
+ SharedHandle<PeerStorage> _peerStorage;
+
SharedHandle<Peer> peer;
- WeakHandle<BtMessageReceiver> btMessageReceiver;
- WeakHandle<BtMessageDispatcher> dispatcher;
- WeakHandle<BtRequestFactory> btRequestFactory;
- WeakHandle<PeerConnection> peerConnection;
- WeakHandle<BtMessageFactory> messageFactory;
+ SharedHandle<BtMessageReceiver> btMessageReceiver;
+ SharedHandle<BtMessageDispatcher> dispatcher;
+ SharedHandle<BtRequestFactory> btRequestFactory;
+ SharedHandle<PeerConnection> peerConnection;
+ SharedHandle<BtMessageFactory> messageFactory;
+ SharedHandle<ExtensionMessageFactory> _extensionMessageFactory;
WeakHandle<DHTNode> _localNode;
@@ -163,17 +179,26 @@ public:
this->cuid = cuid;
}
+ void setBtRuntime(const SharedHandle<BtRuntime>& btRuntime);
+
+ void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
+
+ void setPeerStorage(const SharedHandle<PeerStorage>& peerStorage);
+
void setPeer(const SharedHandle<Peer>& peer);
- void setBtMessageReceiver(const WeakHandle<BtMessageReceiver>& receiver);
+ void setBtMessageReceiver(const SharedHandle<BtMessageReceiver>& receiver);
+
+ void setDispatcher(const SharedHandle<BtMessageDispatcher>& dispatcher);
- void setDispatcher(const WeakHandle<BtMessageDispatcher>& dispatcher);
+ void setBtRequestFactory(const SharedHandle<BtRequestFactory>& factory);
- void setBtRequestFactory(const WeakHandle<BtRequestFactory>& factory);
+ void setPeerConnection(const SharedHandle<PeerConnection>& peerConnection);
- void setPeerConnection(const WeakHandle<PeerConnection>& peerConnection);
+ void setBtMessageFactory(const SharedHandle<BtMessageFactory>& factory);
- void setBtMessageFactory(const WeakHandle<BtMessageFactory>& factory);
+ void setExtensionMessageFactory
+ (const SharedHandle<ExtensionMessageFactory>& 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 <algorithm>
+
#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 <algorithm>
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>& 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 = pieceStorage;
+}
+
+void DefaultBtMessageDispatcher::setPeerStorage
+(const SharedHandle<PeerStorage>& peerStorage)
+{
+ _peerStorage = peerStorage;
}
void DefaultBtMessageDispatcher::setBtMessageFactory(const WeakHandle<BtMessageFactory>& 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<SharedHandle<BtMessage> > messageQueue;
std::deque<RequestSlot> requestSlots;
SharedHandle<BtContext> btContext;
- SharedHandle<PeerStorage> peerStorage;
- SharedHandle<PieceStorage> pieceStorage;
+ SharedHandle<PeerStorage> _peerStorage;
+ SharedHandle<PieceStorage> _pieceStorage;
WeakHandle<BtMessageFactory> messageFactory;
SharedHandle<Peer> peer;
unsigned int maxUploadSpeedLimit;
@@ -111,6 +111,10 @@ public:
void setBtContext(const SharedHandle<BtContext>& btContext);
+ void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
+
+ void setPeerStorage(const SharedHandle<PeerStorage>& peerStorage);
+
void setBtMessageFactory(const WeakHandle<BtMessageFactory>& 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<BtNotInterestedMessage> 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<BtMessageFactory>(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<BtMessageValidator> 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>& peer)
void DefaultBtMessageFactory::setBtContext(const SharedHandle<BtContext>& btContext)
{
this->btContext = btContext;
- this->pieceStorage = PIECE_STORAGE(btContext);
+}
+
+void DefaultBtMessageFactory::setPieceStorage
+(const SharedHandle<PieceStorage>& pieceStorage)
+{
+ _pieceStorage = pieceStorage;
+}
+
+void DefaultBtMessageFactory::setPeerStorage
+(const SharedHandle<PeerStorage>& peerStorage)
+{
+ _peerStorage = peerStorage;
}
void DefaultBtMessageFactory::setBtMessageDispatcher(const WeakHandle<BtMessageDispatcher>& dispatcher)
@@ -435,6 +454,12 @@ void DefaultBtMessageFactory::setBtMessageDispatcher(const WeakHandle<BtMessageD
this->dispatcher = dispatcher;
}
+void DefaultBtMessageFactory::setExtensionMessageFactory
+(const SharedHandle<ExtensionMessageFactory>& factory)
+{
+ _extensionMessageFactory = factory;
+}
+
void DefaultBtMessageFactory::setLocalNode(const WeakHandle<DHTNode>& 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> btContext;
- SharedHandle<PieceStorage> pieceStorage;
+ SharedHandle<PieceStorage> _pieceStorage;
+ SharedHandle<PeerStorage> _peerStorage;
SharedHandle<Peer> peer;
bool _dhtEnabled;
@@ -66,6 +69,8 @@ private:
WeakHandle<PeerConnection> peerConnection;
+ SharedHandle<ExtensionMessageFactory> _extensionMessageFactory;
+
WeakHandle<DHTNode> _localNode;
WeakHandle<DHTRoutingTable> _routingTable;
@@ -131,6 +136,10 @@ public:
void setBtContext(const SharedHandle<BtContext>& btContext);
+ void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
+
+ void setPeerStorage(const SharedHandle<PeerStorage>& peerStorage);
+
void setCuid(int32_t cuid) {
this->cuid = cuid;
}
@@ -145,6 +154,9 @@ public:
void setPeerConnection(const WeakHandle<PeerConnection>& connection);
+ void setExtensionMessageFactory
+ (const SharedHandle<ExtensionMessageFactory>& factory);
+
void setLocalNode(const WeakHandle<DHTNode>& localNode);
void setRoutingTable(const WeakHandle<DHTRoutingTable>& 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 <cstring>
+
#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 <cstring>
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 <cerrno>
+#include <cstring>
+#include <fstream>
+
#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 <fstream>
-#include <cerrno>
-#include <cstring>
namespace aria2 {
@@ -86,7 +87,7 @@ void DefaultBtProgressInfoFile::updateFilename()
bool DefaultBtProgressInfoFile::isTorrentDownload()
{
- return !dynamic_pointer_cast<BtContext>(_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<BtContext>(_dctx));
- TransferStat stat = PEER_STORAGE(btContext)->calculateStat();
+ TransferStat stat = _peerStorage->calculateStat();
uploadLengthNL = hton64(stat.getAllTimeUploadLength());
}
o.write(reinterpret_cast<const char*>(&uploadLengthNL),
@@ -267,8 +267,7 @@ void DefaultBtProgressInfoFile::load()
uploadLength = ntoh64(uploadLength);
}
if(isTorrentDownload()) {
- BT_RUNTIME(dynamic_pointer_cast<BtContext>(_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 = peerStorage;
+}
+
+void DefaultBtProgressInfoFile::setBtRuntime
+(const SharedHandle<BtRuntime>& 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<DownloadContext> _dctx;
SharedHandle<PieceStorage> _pieceStorage;
+ SharedHandle<PeerStorage> _peerStorage;
+ SharedHandle<BtRuntime> _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>& peerStorage);
+
+ void setBtRuntime(const SharedHandle<BtRuntime>& 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 <algorithm>
+
#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 <algorithm>
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<SharedHandle<Piece> >& DefaultBtRequestFactory::getTargetPieces()
void DefaultBtRequestFactory::setBtContext(const SharedHandle<BtContext>& btContext)
{
this->btContext = btContext;
- this->pieceStorage = PIECE_STORAGE(btContext);
+}
+
+void DefaultBtRequestFactory::setPieceStorage
+(const SharedHandle<PieceStorage>& pieceStorage)
+{
+ _pieceStorage = pieceStorage;
}
void DefaultBtRequestFactory::setPeer(const SharedHandle<Peer>& 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> btContext;
- SharedHandle<PieceStorage> pieceStorage;
+ SharedHandle<PieceStorage> _pieceStorage;
SharedHandle<Peer> peer;
WeakHandle<BtMessageDispatcher> dispatcher;
WeakHandle<BtMessageFactory> messageFactory;
@@ -95,6 +95,8 @@ public:
void setBtContext(const SharedHandle<BtContext>& btContext);
+ void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
+
void setPeer(const SharedHandle<Peer>& peer);
void setBtMessageDispatcher(const WeakHandle<BtMessageDispatcher>& 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 = 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> _btContext;
+ SharedHandle<PeerStorage> _peerStorage;
+
SharedHandle<Peer> _peer;
Logger* _logger;
@@ -64,6 +67,8 @@ public:
void setBtContext(const SharedHandle<BtContext>& btContext);
+ void setPeerStorage(const SharedHandle<PeerStorage>& peerStorage);
+
void setPeer(const SharedHandle<Peer>& 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 <algorithm>
+
#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 <algorithm>
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<SharedHandle<Peer> > 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<PieceStorage>& ps)
+{
+ _pieceStorage = ps;
+}
+
+void DefaultPeerStorage::setBtRuntime(const SharedHandle<BtRuntime>& 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> btContext;
+ SharedHandle<BtRuntime> _btRuntime;
+ SharedHandle<PieceStorage> _pieceStorage;
const Option* option;
std::deque<SharedHandle<Peer> > peers;
Logger* logger;
- SharedHandle<BtRuntime> btRuntime;
size_t maxPeerListSize;
uint64_t removedPeerSessionDownloadLength;
uint64_t removedPeerSessionUploadLength;
@@ -67,12 +69,6 @@ public:
virtual ~DefaultPeerStorage();
- void setBtRuntime(const SharedHandle<BtRuntime>& btRuntime) {
- this->btRuntime = btRuntime;
- }
-
- SharedHandle<BtRuntime> getBtRuntime() const { return btRuntime; }
-
virtual bool addPeer(const SharedHandle<Peer>& peer);
size_t countPeer() const;
@@ -107,6 +103,9 @@ public:
void onReturningPeer(const SharedHandle<Peer>& peer);
+ void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
+
+ void setBtRuntime(const SharedHandle<BtRuntime>& 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 <signal.h>
+
+#include <cstring>
+#include <cerrno>
+#include <algorithm>
+#include <numeric>
+
#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 <signal.h>
-#include <cstring>
-#include <algorithm>
-#include <numeric>
-#include <cerrno>
+
+#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<BtRegistry> DownloadEngine::getBtRegistry() const
+{
+ return _btRegistry;
+}
+
DownloadEngine::SocketPoolEntry::SocketPoolEntry
(const SharedHandle<SocketCore>& socket,
const std::map<std::string, std::string>& 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 <sys/epoll.h>
+#endif // HAVE_EPOLL_CREATE
+
+#include <string>
+#include <deque>
+#include <map>
+
#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 <string>
-#include <deque>
-#include <map>
-#ifdef HAVE_EPOLL
-# include <sys/epoll.h>
-#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> _cookieStorage;
+ SharedHandle<BtRegistry> _btRegistry;
+
void shortSleep() const;
/**
@@ -405,6 +411,8 @@ public:
uint16_t port);
SharedHandle<CookieStorage> getCookieStorage() const;
+
+ SharedHandle<BtRegistry> getBtRegistry() const;
};
typedef SharedHandle<DownloadEngine> 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<Peer>& p,
DownloadEngine* e,
const SharedHandle<BtContext>& btContext,
+ const SharedHandle<BtRuntime>& btRuntime,
const SharedHandle<SocketCore>& 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> peer = peerStorage->getUnusedPeer();
+ if(_peerStorage->isPeerAvailable() && _btRuntime->lessThanEqMinPeers()) {
+ SharedHandle<Peer> 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 = peerStorage;
+}
+
+void InitiatorMSEHandshakeCommand::setPieceStorage
+(const SharedHandle<PieceStorage>& 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> _btContext;
+
+ SharedHandle<PeerStorage> _peerStorage;
+
+ SharedHandle<PieceStorage> _pieceStorage;
+
+ SharedHandle<BtRuntime> _btRuntime;
+
Seq _sequence;
MSEHandshake* _mseHandshake;
protected:
@@ -71,10 +81,14 @@ public:
const SharedHandle<Peer>& peer,
DownloadEngine* e,
const SharedHandle<BtContext>& btContext,
+ const SharedHandle<BtRuntime>& btRuntime,
const SharedHandle<SocketCore>& s);
virtual ~InitiatorMSEHandshakeCommand();
+ void setPeerStorage(const SharedHandle<PeerStorage>& peerStorage);
+
+ void setPieceStorage(const SharedHandle<PieceStorage>& 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 <cstring>
+#include <cassert>
+
#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 <cstring>
-#include <cassert>
namespace aria2 {
@@ -424,14 +425,14 @@ bool MSEHandshake::findReceiverHashMarker()
return true;
}
-bool MSEHandshake::receiveReceiverHashAndPadCLength()
+bool MSEHandshake::receiveReceiverHashAndPadCLength
+(const std::deque<SharedHandle<BtContext> >& 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<SharedHandle<BtContext> > btContexts = BtRegistry::getAllBtContext();
// pointing to the position of HASH('req2', SKEY) xor HASH('req3', S)
unsigned char* rbufptr = _rbuf;
SharedHandle<BtContext> 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 <deque>
+
#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<SharedHandle<BtContext> >& 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>& 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 = btRuntime;
+}
+
+void PeerChokeCommand::setPeerStorage
+(const SharedHandle<PeerStorage>& 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> _btContext;
+
+ SharedHandle<PeerStorage> _peerStorage;
+
+ SharedHandle<BtRuntime> _btRuntime;
public:
PeerChokeCommand(int32_t cuid,
DownloadEngine* e,
@@ -56,6 +62,10 @@ public:
virtual ~PeerChokeCommand();
virtual bool execute();
+
+ void setPeerStorage(const SharedHandle<PeerStorage>& peerStorage);
+
+ void setBtRuntime(const SharedHandle<BtRuntime>& 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>& btContext,
+ const SharedHandle<BtRuntime>& 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 = peerStorage;
+}
+
+void PeerInitiateConnectionCommand::setPieceStorage
+(const SharedHandle<PieceStorage>& 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> _btContext;
+
+ SharedHandle<BtRuntime> _btRuntime;
+
+ SharedHandle<PeerStorage> _peerStorage;
+
+ SharedHandle<PieceStorage> _pieceStorage;
+
bool _mseHandshakeEnabled;
protected:
virtual bool executeInternal();
@@ -59,9 +70,14 @@ public:
const SharedHandle<Peer>& peer,
DownloadEngine* e,
const SharedHandle<BtContext>& btContext,
+ const SharedHandle<BtRuntime>& btRuntime,
bool mseHandshakeEnabled = true);
virtual ~PeerInitiateConnectionCommand();
+
+ void setPeerStorage(const SharedHandle<PeerStorage>& peerStorage);
+
+ void setPieceStorage(const SharedHandle<PieceStorage>& 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 <algorithm>
+
#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 <algorithm>
+#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>& btContext,
+ const SharedHandle<BtRuntime>& btRuntime,
+ const SharedHandle<PieceStorage>& 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> btRegistry = e->getBtRegistry();
+ SharedHandle<PeerStorage> peerStorage =
+ btRegistry->getPeerStorage(_btContext->getInfoHashAsString());
+
+ SharedHandle<DefaultExtensionMessageFactory> extensionMessageFactory
+ (new DefaultExtensionMessageFactory(_btContext, peer));
+ extensionMessageFactory->setPeerStorage(peerStorage);
+
+ SharedHandle<DefaultBtMessageFactory> 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<DefaultBtMessageDispatcher> 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<DefaultBtRequestFactory> 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 = 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> _btContext;
+
+ SharedHandle<BtRuntime> _btRuntime;
+
+ SharedHandle<PieceStorage> _pieceStorage;
+
+ SharedHandle<PeerStorage> _peerStorage;
+
Seq sequence;
SharedHandle<BtInteractive> btInteractive;
unsigned int maxDownloadSpeedLimit;
@@ -70,12 +80,16 @@ public:
const SharedHandle<Peer>& peer,
DownloadEngine* e,
const SharedHandle<BtContext>& btContext,
+ const SharedHandle<BtRuntime>& btRuntime,
+ const SharedHandle<PieceStorage>& pieceStorage,
const SharedHandle<SocketCore>& s,
Seq sequence,
- const SharedHandle<PeerConnection>& peerConnection = SharedHandle<PeerConnection>());
+ const SharedHandle<PeerConnection>& peerConnection =
+ SharedHandle<PeerConnection>());
virtual ~PeerInteractionCommand();
+ void setPeerStorage(const SharedHandle<PeerStorage>& 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 @@
-/* <!-- copyright */
-/*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2006 Tatsuhiro Tsujikawa
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * In addition, as a special exception, the copyright holders give
- * permission to link the code of portions of this program with the
- * OpenSSL library under certain conditions as described in each
- * individual source file, and distribute linked combinations
- * including the two.
- * You must obey the GNU General Public License in all respects
- * for all of the code used other than OpenSSL. If you modify
- * file(s) with this exception, you may extend this exception to your
- * version of the file(s), but you are not obligated to do so. If you
- * do not wish to do so, delete this exception statement from your
- * version. If you delete this exception statement from all source
- * files in the program, then also delete it here.
- */
-/* copyright --> */
-#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> btMessageFactory;
- SharedHandle<BtRequestFactory> btRequestFactory;
- SharedHandle<BtMessageDispatcher> btMessageDispatcher;
- SharedHandle<BtMessageReceiver> btMessageReceiver;
- SharedHandle<PeerConnection> peerConnection;
- SharedHandle<ExtensionMessageFactory> extensionMessageFactory;
-};
-
-typedef SharedHandle<PeerObject> 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>& peerConnection):
+PeerReceiveHandshakeCommand::PeerReceiveHandshakeCommand
+(int32_t cuid,
+ const PeerHandle& peer,
+ DownloadEngine* e,
+ const SocketHandle& s,
+ const SharedHandle<PeerConnection>& 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> btRegistry = e->getBtRegistry();
+ SharedHandle<BtContext> btContext = btRegistry->getBtContext(infoHash);
+ SharedHandle<BtRuntime> btRuntime = btRegistry->getBtRuntime(infoHash);
+ SharedHandle<PieceStorage> pieceStorage =
+ btRegistry->getPieceStorage(infoHash);
+ SharedHandle<PeerStorage> peerStorage =
+ btRegistry->getPeerStorage(infoHash);
+
+ 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>& peer,
- DownloadEngine* e,
- const SharedHandle<SocketCore>& s,
- const SharedHandle<PeerConnection>& peerConnection = SharedHandle<PeerConnection>());
-
+ PeerReceiveHandshakeCommand
+ (int32_t cuid,
+ const SharedHandle<Peer>& peer,
+ DownloadEngine* e,
+ const SharedHandle<SocketCore>& s,
+ const SharedHandle<PeerConnection>& peerConnection =
+ SharedHandle<PeerConnection>());
+
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 <cassert>
+#include <algorithm>
+
#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 <cassert>
-#include <algorithm>
namespace aria2 {
@@ -200,35 +201,46 @@ void RequestGroup::createInitialCommand(std::deque<Command*>& commands,
_pieceStorage->setFileFilter(Util::parseIntRange(_option->get(PREF_SELECT_FILE)));
}
- BtProgressInfoFileHandle
+ SharedHandle<DefaultBtProgressInfoFile>
progressInfoFile(new DefaultBtProgressInfoFile(_downloadContext,
_pieceStorage,
_option));
- BtRegistry::registerBtContext(btContext->getInfoHashAsString(), btContext);
- BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(),
+ SharedHandle<BtRegistry> 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);
-
- PeerStorageHandle peerStorage(new DefaultPeerStorage(btContext, _option));
- BtRegistry::registerPeerStorage(btContext->getInfoHashAsString(), peerStorage);
-
- BtAnnounceHandle btAnnounce(new DefaultBtAnnounce(btContext, _option));
- BtRegistry::registerBtAnnounce(btContext->getInfoHashAsString(), btAnnounce);
+ btRegistry->registerBtRuntime(btContext->getInfoHashAsString(),
+ btRuntime);
+ _btRuntime = btRuntime;
+ progressInfoFile->setBtRuntime(btRuntime);
+
+ SharedHandle<DefaultPeerStorage> peerStorage
+ (new DefaultPeerStorage(btContext, _option));
+ peerStorage->setBtRuntime(btRuntime);
+ peerStorage->setPieceStorage(_pieceStorage);
+ btRegistry->registerPeerStorage(btContext->getInfoHashAsString(),
+ peerStorage);
+ _peerStorage = peerStorage;
+ progressInfoFile->setPeerStorage(peerStorage);
+
+ SharedHandle<DefaultBtAnnounce> btAnnounce
+ (new DefaultBtAnnounce(btContext, _option));
+ btAnnounce->setBtRuntime(btRuntime);
+ btAnnounce->setPieceStorage(_pieceStorage);
+ btAnnounce->setPeerStorage(peerStorage);
+ btRegistry->registerBtAnnounce(btContext->getInfoHashAsString(),
+ btAnnounce);
btAnnounce->shuffleAnnounce();
- {
- SharedHandle<PeerObjectCluster> 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<BtContext>(_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<BtContext>(_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<BtContext>(_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<BtContext>(_downloadContext);
if(!btContext.isNull()) {
- BtContextHandle btContextInReg = BtRegistry::getBtContext(btContext->getInfoHashAsString());
+ SharedHandle<BtRegistry> 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 <string>
+#include <deque>
+
#include "SharedHandle.h"
#include "TransferStat.h"
#include "TimeA2.h"
-#include <string>
-#include <deque>
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> _btRuntime;
+
+ WeakHandle<PeerStorage> _peerStorage;
+
const Option* _option;
Logger* _logger;
@@ -287,7 +297,7 @@ public:
bool isDependencyResolved();
- void releaseRuntimeResource();
+ void releaseRuntimeResource(DownloadEngine* e);
void postDownloadProcessing(std::deque<SharedHandle<RequestGroup> >& 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<SharedHandle<RequestGroup> >& _reservedGroups;
std::deque<SharedHandle<DownloadResult> >& _downloadResults;
Logger* _logger;
@@ -143,8 +144,10 @@ private:
}
public:
ProcessStoppedRequestGroup
- (std::deque<SharedHandle<RequestGroup> >& reservedGroups,
+ (DownloadEngine* e,
+ std::deque<SharedHandle<RequestGroup> >& reservedGroups,
std::deque<SharedHandle<DownloadResult> >& 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<Command*>& 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>& 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>& seedCriteria)
+void SeedCheckCommand::setSeedCriteria
+(const SharedHandle<SeedCriteria>& seedCriteria)
{
this->seedCriteria = seedCriteria;
}
+void SeedCheckCommand::setBtRuntime(const SharedHandle<BtRuntime>& btRuntime)
+{
+ _btRuntime = btRuntime;
+}
+
+void SeedCheckCommand::setPieceStorage
+(const SharedHandle<PieceStorage>& 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> _btContext;
+ SharedHandle<PieceStorage> _pieceStorage;
+ SharedHandle<BtRuntime> _btRuntime;
Time checkPoint;
SharedHandle<SeedCriteria> seedCriteria;
bool checkStarted;
@@ -66,6 +71,10 @@ public:
virtual bool execute();
void setSeedCriteria(const SharedHandle<SeedCriteria>& seedCriteria);
+
+ void setBtRuntime(const SharedHandle<BtRuntime>& btRuntime);
+
+ void setPieceStorage(const SharedHandle<PieceStorage>& 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> btContext;
- SharedHandle<PeerStorage> peerStorage;
- SharedHandle<PieceStorage> pieceStorage;
- SharedHandle<BtRuntime> btRuntime;
+ SharedHandle<PeerStorage> _peerStorage;
+ SharedHandle<PieceStorage> _pieceStorage;
+ SharedHandle<BtRuntime> _btRuntime;
public:
ShareRatioSeedCriteria(double ratio, const SharedHandle<BtContext>& 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 = peerStorage;
+ }
+
+ void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage)
+ {
+ _pieceStorage = pieceStorage;
+ }
+
+ void setBtRuntime(const SharedHandle<BtRuntime>& 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 <sstream>
+
#include "DownloadEngine.h"
#include "BtContext.h"
#include "BtAnnounce.h"
@@ -55,7 +58,6 @@
#include "Logger.h"
#include "A2STR.h"
#include "SocketCore.h"
-#include <sstream>
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<const unsigned char*>(trackerResponse.c_str()),
+ _btAnnounce->processAnnounceResponse
+ (reinterpret_cast<const unsigned char*>(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 = btRuntime;
+}
+
+void TrackerWatcherCommand::setPeerStorage
+(const SharedHandle<PeerStorage>& peerStorage)
+{
+ _peerStorage = peerStorage;
+}
+
+void TrackerWatcherCommand::setPieceStorage
+(const SharedHandle<PieceStorage>& pieceStorage)
+{
+ _pieceStorage = pieceStorage;
+}
+
+void TrackerWatcherCommand::setBtAnnounce
+(const SharedHandle<BtAnnounce>& 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> _btContext;
+
+ SharedHandle<PeerStorage> _peerStorage;
+
+ SharedHandle<PieceStorage> _pieceStorage;
+
+ SharedHandle<BtRuntime> _btRuntime;
+
+ SharedHandle<BtAnnounce> _btAnnounce;
+
SharedHandle<RequestGroup> _trackerRequestGroup;
/**
* Returns a command for announce request. Returns 0 if no announce request
@@ -73,6 +87,14 @@ public:
SharedHandle<RequestGroup> createAnnounce();
virtual bool execute();
+
+ void setPeerStorage(const SharedHandle<PeerStorage>& peerStorage);
+
+ void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
+
+ void setBtRuntime(const SharedHandle<BtRuntime>& btRuntime);
+
+ void setBtAnnounce(const SharedHandle<BtAnnounce>& 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>& 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 <utility>
#include <deque>
+#include "a2time.h"
+
namespace aria2 {
-class BtContext;
+class PeerStorage;
class Peer;
class UTPexExtensionMessage;
typedef SharedHandle<UTPexExtensionMessage> UTPexExtensionMessageHandle;
@@ -55,7 +57,7 @@ private:
std::deque<SharedHandle<Peer> > _droppedPeers;
- SharedHandle<BtContext> _btContext;
+ SharedHandle<PeerStorage> _peerStorage;
time_t _interval;
@@ -101,10 +103,10 @@ public:
bool droppedPeersAreFull() const;
- void setBtContext(const SharedHandle<BtContext>& btContext);
+ void setPeerStorage(const SharedHandle<PeerStorage>& peerStorage);
- static UTPexExtensionMessageHandle create(const SharedHandle<BtContext>& btContext,
- const unsigned char* data, size_t len);
+ static UTPexExtensionMessageHandle
+ create(const unsigned char* data, size_t len);
void setMaxFreshPeer(size_t maxFreshPeer);