Permalink
Browse files

BtRegistry now holds BitTorrent TCP server port.

Because BitTorrent TCP server port is global configuration, it is not
preferable that per download resource BtRuntime holds it.  We also
refactored and eliminated ugly static variable in PeerListenCommand.
If TCP port is necessary, we inject it to them directly.
  • Loading branch information...
1 parent f0cd83f commit c58f736bb91f55fb68151c5c7d977911f993b1ab @tatsuhiro-t tatsuhiro-t committed Jul 21, 2011
View
@@ -109,6 +109,8 @@ class BtAnnounce {
virtual void overrideMinInterval(time_t interval) = 0;
+ virtual void setTcpPort(uint16_t port) = 0;
+
static const std::string FAILURE_REASON;
static const std::string WARNING_MESSAGE;
View
@@ -44,6 +44,12 @@
namespace aria2 {
+BtRegistry::BtRegistry()
+ : tcpPort_(0)
+{}
+
+BtRegistry::~BtRegistry() {}
+
SharedHandle<DownloadContext>
BtRegistry::getDownloadContext(a2_gid_t gid) const
{
View
@@ -80,7 +80,11 @@ struct BtObject {
class BtRegistry {
private:
std::map<a2_gid_t, BtObject> pool_;
+ uint16_t tcpPort_;
public:
+ BtRegistry();
+ ~BtRegistry();
+
SharedHandle<DownloadContext>
getDownloadContext(a2_gid_t gid) const;
@@ -104,6 +108,15 @@ class BtRegistry {
void removeAll();
bool remove(a2_gid_t gid);
+
+ void setTcpPort(uint16_t port)
+ {
+ tcpPort_ = port;
+ }
+ uint16_t getTcpPort() const
+ {
+ return tcpPort_;
+ }
};
} // namespace aria2
View
@@ -39,7 +39,6 @@ namespace aria2 {
BtRuntime::BtRuntime()
: uploadLengthAtStartup_(0),
- port_(0),
halt_(false),
connections_(0),
ready_(false),
View
@@ -43,7 +43,6 @@ namespace aria2 {
class BtRuntime {
private:
uint64_t uploadLengthAtStartup_;
- uint16_t port_;
bool halt_;
unsigned int connections_;
bool ready_;
@@ -69,12 +68,6 @@ class BtRuntime {
uploadLengthAtStartup_ = length;
}
- void setListenPort(uint16_t port) {
- port_ = port;
- }
-
- uint16_t getListenPort() const { return port_; }
-
bool isHalt() const { return halt_; }
void setHalt(bool halt) {
View
@@ -100,7 +100,8 @@ void BtSetup::setup(std::vector<Command*>& commands,
SharedHandle<TorrentAttribute> torrentAttrs =
bittorrent::getTorrentAttrs(requestGroup->getDownloadContext());
bool metadataGetMode = torrentAttrs->metadata.empty();
- BtObject btObject = e->getBtRegistry()->get(requestGroup->getGID());
+ const SharedHandle<BtRegistry>& btReg = e->getBtRegistry();
+ BtObject btObject = btReg->get(requestGroup->getGID());
SharedHandle<PieceStorage> pieceStorage = btObject.pieceStorage_;
SharedHandle<PeerStorage> peerStorage = btObject.peerStorage_;
SharedHandle<BtRuntime> btRuntime = btObject.btRuntime_;
@@ -183,43 +184,38 @@ void BtSetup::setup(std::vector<Command*>& commands,
commands.push_back(c);
}
}
- if(PeerListenCommand::getNumInstance() == 0) {
+ if(btReg->getTcpPort() == 0) {
static int families[] = { AF_INET, AF_INET6 };
size_t familiesLength = e->getOption()->getAsBool(PREF_DISABLE_IPV6)?1:2;
for(size_t i = 0; i < familiesLength; ++i) {
- PeerListenCommand* listenCommand =
- PeerListenCommand::getInstance(e, families[i]);
+ PeerListenCommand* command =
+ new PeerListenCommand(e->newCUID(), e, families[i]);
bool ret;
uint16_t port;
- if(btRuntime->getListenPort()) {
- IntSequence seq =
- util::parseIntRange(util::uitos(btRuntime->getListenPort()));
- ret = listenCommand->bindPort(port, seq);
+ if(btReg->getTcpPort()) {
+ IntSequence seq = util::parseIntRange(util::uitos(btReg->getTcpPort()));
+ ret = command->bindPort(port, seq);
} else {
IntSequence seq =
util::parseIntRange(e->getOption()->get(PREF_LISTEN_PORT));
- ret = listenCommand->bindPort(port, seq);
+ ret = command->bindPort(port, seq);
}
if(ret) {
- btRuntime->setListenPort(port);
+ btReg->setTcpPort(port);
// Add command to DownloadEngine directly.
- e->addCommand(listenCommand);
+ e->addCommand(command);
} else {
- delete listenCommand;
+ delete command;
}
}
- if(PeerListenCommand::getNumInstance() == 0) {
+ if(btReg->getTcpPort() == 0) {
throw DL_ABORT_EX(_("Errors occurred while binding port.\n"));
}
- } else {
- PeerListenCommand* listenCommand =
- PeerListenCommand::getInstance(e, AF_INET);
- if(!listenCommand) {
- listenCommand = PeerListenCommand::getInstance(e, AF_INET6);
- }
- btRuntime->setListenPort(listenCommand->getPort());
}
+ btAnnounce->setTcpPort(btReg->getTcpPort());
+
if(option->getAsBool(PREF_BT_ENABLE_LPD) &&
+ btReg->getTcpPort() &&
(metadataGetMode || !torrentAttrs->privateTorrent)) {
if(LpdReceiveMessageCommand::getNumInstance() == 0) {
A2_LOG_INFO("Initializing LpdMessageReceiver.");
@@ -266,7 +262,7 @@ void BtSetup::setup(std::vector<Command*>& commands,
SharedHandle<LpdMessageDispatcher> dispatcher
(new LpdMessageDispatcher
(std::string(&infoHash[0], &infoHash[INFO_HASH_LENGTH]),
- btRuntime->getListenPort(),
+ btReg->getTcpPort(),
LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT));
if(dispatcher->init(receiver->getLocalAddress(), /*ttl*/1, /*loop*/0)) {
A2_LOG_INFO("LpdMessageDispatcher initialized.");
@@ -49,6 +49,7 @@
#include "DownloadContext.h"
#include "wallclock.h"
#include "fmt.h"
+#include "BtRegistry.h"
namespace aria2 {
@@ -102,7 +103,9 @@ bool DHTGetPeersCommand::execute()
bittorrent::getInfoHashString
(requestGroup_->getDownloadContext()).c_str()));
task_ = taskFactory_->createPeerLookupTask
- (requestGroup_->getDownloadContext(), btRuntime_, peerStorage_);
+ (requestGroup_->getDownloadContext(),
+ e_->getBtRegistry()->getTcpPort(),
+ peerStorage_);
taskQueue_->addPeriodicTask2(task_);
} else if(task_ && task_->finished()) {
A2_LOG_DEBUG("task finished detected");
View
@@ -43,7 +43,6 @@
#include "DHTMessageDispatcher.h"
#include "DHTMessageCallback.h"
#include "PeerStorage.h"
-#include "BtRuntime.h"
#include "util.h"
#include "DHTBucket.h"
#include "bittorrent_helper.h"
@@ -54,9 +53,11 @@
namespace aria2 {
DHTPeerLookupTask::DHTPeerLookupTask
-(const SharedHandle<DownloadContext>& downloadContext)
+(const SharedHandle<DownloadContext>& downloadContext,
+ uint16_t tcpPort)
: DHTAbstractNodeLookupTask<DHTGetPeersReplyMessage>
- (bittorrent::getInfoHash(downloadContext))
+ (bittorrent::getInfoHash(downloadContext)),
+ tcpPort_(tcpPort)
{}
void
@@ -115,18 +116,13 @@ void DHTPeerLookupTask::onFinish()
getMessageFactory()->createAnnouncePeerMessage
(node,
getTargetID(), // this is infoHash
- btRuntime_->getListenPort(),
+ tcpPort_,
token);
getMessageDispatcher()->addMessageToQueue(m);
--num;
}
}
-void DHTPeerLookupTask::setBtRuntime(const SharedHandle<BtRuntime>& btRuntime)
-{
- btRuntime_ = btRuntime;
-}
-
void DHTPeerLookupTask::setPeerStorage(const SharedHandle<PeerStorage>& ps)
{
peerStorage_ = ps;
View
@@ -43,7 +43,6 @@ namespace aria2 {
class DownloadContext;
class Peer;
class PeerStorage;
-class BtRuntime;
class DHTGetPeersReplyMessage;
class DHTPeerLookupTask:
@@ -52,10 +51,11 @@ class DHTPeerLookupTask:
std::map<std::string, std::string> tokenStorage_;
SharedHandle<PeerStorage> peerStorage_;
-
- SharedHandle<BtRuntime> btRuntime_;
+ uint16_t tcpPort_;
public:
- DHTPeerLookupTask(const SharedHandle<DownloadContext>& downloadContext);
+ DHTPeerLookupTask
+ (const SharedHandle<DownloadContext>& downloadContext,
+ uint16_t tcpPort);
virtual void getNodesFromMessage
(std::vector<SharedHandle<DHTNode> >& nodes,
@@ -69,8 +69,6 @@ class DHTPeerLookupTask:
virtual SharedHandle<DHTMessageCallback> createCallback();
virtual void onFinish();
-
- void setBtRuntime(const SharedHandle<BtRuntime>& btRuntime);
void setPeerStorage(const SharedHandle<PeerStorage>& peerStorage);
};
View
@@ -41,7 +41,6 @@
namespace aria2 {
class DownloadContext;
-class BtRuntime;
class PeerStorage;
class DHTTask;
class DHTNode;
@@ -62,7 +61,7 @@ class DHTTaskFactory {
virtual SharedHandle<DHTTask>
createPeerLookupTask(const SharedHandle<DownloadContext>& ctx,
- const SharedHandle<BtRuntime>& btRuntime,
+ uint16_t tcpPort,
const SharedHandle<PeerStorage>& peerStorage) = 0;
virtual SharedHandle<DHTTask>
@@ -46,7 +46,6 @@
#include "Peer.h"
#include "DHTNodeLookupEntry.h"
#include "PeerStorage.h"
-#include "BtRuntime.h"
#include "DHTMessageCallback.h"
namespace aria2 {
@@ -90,12 +89,11 @@ DHTTaskFactoryImpl::createBucketRefreshTask()
SharedHandle<DHTTask>
DHTTaskFactoryImpl::createPeerLookupTask
(const SharedHandle<DownloadContext>& ctx,
- const SharedHandle<BtRuntime>& btRuntime,
+ uint16_t tcpPort,
const SharedHandle<PeerStorage>& peerStorage)
{
- SharedHandle<DHTPeerLookupTask> task(new DHTPeerLookupTask(ctx));
- // TODO these may be not freed by RequestGroup::releaseRuntimeResource()
- task->setBtRuntime(btRuntime);
+ SharedHandle<DHTPeerLookupTask> task(new DHTPeerLookupTask(ctx, tcpPort));
+ // TODO this may be not freed by RequestGroup::releaseRuntimeResource()
task->setPeerStorage(peerStorage);
setCommonProperty(task);
return task;
View
@@ -78,7 +78,7 @@ class DHTTaskFactoryImpl:public DHTTaskFactory {
virtual SharedHandle<DHTTask>
createPeerLookupTask(const SharedHandle<DownloadContext>& ctx,
- const SharedHandle<BtRuntime>& btRuntime,
+ uint16_t tcpPort,
const SharedHandle<PeerStorage>& peerStorage);
virtual SharedHandle<DHTTask>
View
@@ -68,7 +68,8 @@ DefaultBtAnnounce::DefaultBtAnnounce
incomplete_(0),
announceList_(bittorrent::getTorrentAttrs(downloadContext)->announceList),
option_(option),
- randomizer_(SimpleRandomizer::getInstance())
+ randomizer_(SimpleRandomizer::getInstance()),
+ tcpPort_(0)
{}
DefaultBtAnnounce::~DefaultBtAnnounce() {
@@ -166,9 +167,9 @@ std::string DefaultBtAnnounce::getAnnounceUrl() {
uri += "&numwant=";
uri += util::uitos(numWant);
uri += "&no_peer_id=1";
- if(btRuntime_->getListenPort() > 0) {
+ if(tcpPort_) {
uri += "&port=";
- uri += util::uitos(btRuntime_->getListenPort());
+ uri += util::uitos(tcpPort_);
}
std::string event = announceList_.getEventString();
if(!event.empty()) {
View
@@ -65,6 +65,7 @@ class DefaultBtAnnounce : public BtAnnounce {
SharedHandle<BtRuntime> btRuntime_;
SharedHandle<PieceStorage> pieceStorage_;
SharedHandle<PeerStorage> peerStorage_;
+ uint16_t tcpPort_;
public:
DefaultBtAnnounce(const SharedHandle<DownloadContext>& downloadContext,
const Option* option);
@@ -121,6 +122,11 @@ class DefaultBtAnnounce : public BtAnnounce {
virtual void overrideMinInterval(time_t interval);
+ virtual void setTcpPort(uint16_t port)
+ {
+ tcpPort_ = port;
+ }
+
void setRandomizer(const SharedHandle<Randomizer>& randomizer);
time_t getInterval() const
@@ -190,7 +190,7 @@ void DefaultBtInteractive::addHandshakeExtendedMessageToQueue()
static const std::string CLIENT_ARIA2("aria2/"PACKAGE_VERSION);
HandshakeExtensionMessageHandle m(new HandshakeExtensionMessage());
m->setClientVersion(CLIENT_ARIA2);
- m->setTCPPort(btRuntime_->getListenPort());
+ m->setTCPPort(tcpPort_);
m->setExtensions(extensionMessageRegistry_->getExtensions());
SharedHandle<TorrentAttribute> attrs =
bittorrent::getTorrentAttrs(downloadContext_);
@@ -143,6 +143,8 @@ class DefaultBtInteractive : public BtInteractive {
RequestGroupMan* requestGroupMan_;
+ uint16_t tcpPort_;
+
static const time_t FLOODING_CHECK_INTERVAL = 5;
void addBitfieldMessageToQueue();
@@ -255,6 +257,11 @@ class DefaultBtInteractive : public BtInteractive {
{
metadataGetMode_ = true;
}
+
+ void setTcpPort(uint16_t port)
+ {
+ tcpPort_ = port;
+ }
};
typedef SharedHandle<DefaultBtInteractive> DefaultBtInteractiveHandle;
@@ -75,6 +75,7 @@
#include "bittorrent_helper.h"
#include "UTMetadataRequestFactory.h"
#include "UTMetadataRequestTracker.h"
+#include "BtRegistry.h"
namespace aria2 {
@@ -234,6 +235,7 @@ PeerInteractionCommand::PeerInteractionCommand
}
btInteractive->setUTMetadataRequestFactory(utMetadataRequestFactory);
btInteractive->setUTMetadataRequestTracker(utMetadataRequestTracker);
+ btInteractive->setTcpPort(e->getBtRegistry()->getTcpPort());
if(metadataGetMode) {
btInteractive->enableMetadataGetMode();
}
Oops, something went wrong.

0 comments on commit c58f736

Please sign in to comment.