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.
pull/1/head
Tatsuhiro Tsujikawa 2011-07-21 23:42:41 +09:00
parent f0cd83f809
commit c58f736bb9
22 changed files with 89 additions and 111 deletions

View File

@ -109,6 +109,8 @@ public:
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 File

@ -44,6 +44,12 @@
namespace aria2 {
BtRegistry::BtRegistry()
: tcpPort_(0)
{}
BtRegistry::~BtRegistry() {}
SharedHandle<DownloadContext>
BtRegistry::getDownloadContext(a2_gid_t gid) const
{

View File

@ -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 @@ public:
void removeAll();
bool remove(a2_gid_t gid);
void setTcpPort(uint16_t port)
{
tcpPort_ = port;
}
uint16_t getTcpPort() const
{
return tcpPort_;
}
};
} // namespace aria2

View File

@ -39,7 +39,6 @@ namespace aria2 {
BtRuntime::BtRuntime()
: uploadLengthAtStartup_(0),
port_(0),
halt_(false),
connections_(0),
ready_(false),

View File

@ -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 @@ public:
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 File

@ -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.");

View File

@ -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 File

@ -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 File

@ -43,7 +43,6 @@ namespace aria2 {
class DownloadContext;
class Peer;
class PeerStorage;
class BtRuntime;
class DHTGetPeersReplyMessage;
class DHTPeerLookupTask:
@ -52,10 +51,11 @@ private:
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,
@ -70,8 +70,6 @@ public:
virtual void onFinish();
void setBtRuntime(const SharedHandle<BtRuntime>& btRuntime);
void setPeerStorage(const SharedHandle<PeerStorage>& peerStorage);
};

View File

@ -41,7 +41,6 @@
namespace aria2 {
class DownloadContext;
class BtRuntime;
class PeerStorage;
class DHTTask;
class DHTNode;
@ -62,7 +61,7 @@ public:
virtual SharedHandle<DHTTask>
createPeerLookupTask(const SharedHandle<DownloadContext>& ctx,
const SharedHandle<BtRuntime>& btRuntime,
uint16_t tcpPort,
const SharedHandle<PeerStorage>& peerStorage) = 0;
virtual SharedHandle<DHTTask>

View File

@ -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 File

@ -78,7 +78,7 @@ public:
virtual SharedHandle<DHTTask>
createPeerLookupTask(const SharedHandle<DownloadContext>& ctx,
const SharedHandle<BtRuntime>& btRuntime,
uint16_t tcpPort,
const SharedHandle<PeerStorage>& peerStorage);
virtual SharedHandle<DHTTask>

View File

@ -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 File

@ -65,6 +65,7 @@ private:
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 @@ public:
virtual void overrideMinInterval(time_t interval);
virtual void setTcpPort(uint16_t port)
{
tcpPort_ = port;
}
void setRandomizer(const SharedHandle<Randomizer>& randomizer);
time_t getInterval() const

View File

@ -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_);

View File

@ -143,6 +143,8 @@ private:
RequestGroupMan* requestGroupMan_;
uint16_t tcpPort_;
static const time_t FLOODING_CHECK_INTERVAL = 5;
void addBitfieldMessageToQueue();
@ -255,6 +257,11 @@ public:
{
metadataGetMode_ = true;
}
void setTcpPort(uint16_t port)
{
tcpPort_ = port;
}
};
typedef SharedHandle<DefaultBtInteractive> DefaultBtInteractiveHandle;

View File

@ -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();
}

View File

@ -52,28 +52,16 @@
namespace aria2 {
unsigned int PeerListenCommand::numInstance_ = 0;
PeerListenCommand* PeerListenCommand::instance_ = 0;
PeerListenCommand* PeerListenCommand::instance6_ = 0;
PeerListenCommand::PeerListenCommand
(cuid_t cuid,
DownloadEngine* e,
int family)
: Command(cuid),
e_(e),
family_(family),
lowestSpeedLimit_(20*1024)
{
++numInstance_;
}
family_(family)
{}
PeerListenCommand::~PeerListenCommand()
{
--numInstance_;
}
PeerListenCommand::~PeerListenCommand() {}
bool PeerListenCommand::bindPort(uint16_t& port, IntSequence& seq)
{
@ -152,21 +140,4 @@ bool PeerListenCommand::execute() {
return false;
}
PeerListenCommand* PeerListenCommand::getInstance(DownloadEngine* e, int family)
{
if(family == AF_INET) {
if(!instance_) {
instance_ = new PeerListenCommand(e->newCUID(), e, family);
}
return instance_;
} else if(family == AF_INET6) {
if(!instance6_) {
instance6_ = new PeerListenCommand(e->newCUID(), e, family);
}
return instance6_;
} else {
return 0;
}
}
} // namespace aria2

View File

@ -49,13 +49,6 @@ private:
DownloadEngine* e_;
int family_;
SharedHandle<SocketCore> socket_;
unsigned int lowestSpeedLimit_;
static unsigned int numInstance_;
static PeerListenCommand* instance_;
static PeerListenCommand* instance6_;
public:
PeerListenCommand(cuid_t cuid, DownloadEngine* e, int family);
@ -71,19 +64,6 @@ public:
// Returns binded port
uint16_t getPort() const;
void setLowestSpeedLimit(unsigned int speed)
{
lowestSpeedLimit_ = speed;
}
static PeerListenCommand* getInstance(DownloadEngine* e, int family);
static unsigned int getNumInstance()
{
return numInstance_;
}
};
} // namespace aria2

View File

@ -72,7 +72,6 @@ public:
peerStorage_->setStat(stat);
btRuntime_.reset(new BtRuntime());
btRuntime_->setListenPort(6989);
}
void tearDown()
@ -150,6 +149,7 @@ void DefaultBtAnnounceTest::testNoMoreAnnounce()
btAnnounce.setPeerStorage(peerStorage_);
btAnnounce.setBtRuntime(btRuntime_);
btAnnounce.setRandomizer(SharedHandle<Randomizer>(new FixedNumberRandomizer()));
btAnnounce.setTcpPort(6989);
CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=fastdltl&numwant=50&no_peer_id=1&port=6989&event=started&supportcrypto=1"), btAnnounce.getAnnounceUrl());
@ -198,6 +198,7 @@ void DefaultBtAnnounceTest::testGetAnnounceUrl()
btAnnounce.setPeerStorage(peerStorage_);
btAnnounce.setBtRuntime(btRuntime_);
btAnnounce.setRandomizer(SharedHandle<Randomizer>(new FixedNumberRandomizer()));
btAnnounce.setTcpPort(6989);
CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=fastdltl&numwant=50&no_peer_id=1&port=6989&event=started&supportcrypto=1"), btAnnounce.getAnnounceUrl());
@ -229,6 +230,7 @@ void DefaultBtAnnounceTest::testGetAnnounceUrl_withQuery()
btAnnounce.setPeerStorage(peerStorage_);
btAnnounce.setBtRuntime(btRuntime_);
btAnnounce.setRandomizer(SharedHandle<Randomizer>(new FixedNumberRandomizer()));
btAnnounce.setTcpPort(6989);
CPPUNIT_ASSERT_EQUAL
(std::string("http://localhost/announce?k=v&"
@ -252,6 +254,7 @@ void DefaultBtAnnounceTest::testGetAnnounceUrl_externalIP()
btAnnounce.setPeerStorage(peerStorage_);
btAnnounce.setBtRuntime(btRuntime_);
btAnnounce.setRandomizer(SharedHandle<Randomizer>(new FixedNumberRandomizer()));
btAnnounce.setTcpPort(6989);
CPPUNIT_ASSERT_EQUAL
(std::string("http://localhost/announce?"
@ -275,6 +278,7 @@ void DefaultBtAnnounceTest::testIsAllAnnounceFailed()
btAnnounce.setPeerStorage(peerStorage_);
btAnnounce.setBtRuntime(btRuntime_);
btAnnounce.setRandomizer(SharedHandle<Randomizer>(new FixedNumberRandomizer()));
btAnnounce.setTcpPort(6989);
CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=fastdltl&numwant=50&no_peer_id=1&port=6989&event=started&supportcrypto=1"), btAnnounce.getAnnounceUrl());
@ -307,6 +311,7 @@ void DefaultBtAnnounceTest::testURLOrderInStoppedEvent()
btAnnounce.setPeerStorage(peerStorage_);
btAnnounce.setBtRuntime(btRuntime_);
btAnnounce.setRandomizer(SharedHandle<Randomizer>(new FixedNumberRandomizer()));
btAnnounce.setTcpPort(6989);
CPPUNIT_ASSERT_EQUAL(std::string("http://localhost1/announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=fastdltl&numwant=50&no_peer_id=1&port=6989&event=started&supportcrypto=1"), btAnnounce.getAnnounceUrl());
@ -337,6 +342,7 @@ void DefaultBtAnnounceTest::testURLOrderInCompletedEvent()
btAnnounce.setPeerStorage(peerStorage_);
btAnnounce.setBtRuntime(btRuntime_);
btAnnounce.setRandomizer(SharedHandle<Randomizer>(new FixedNumberRandomizer()));
btAnnounce.setTcpPort(6989);
CPPUNIT_ASSERT_EQUAL(std::string("http://localhost1/announce?info_hash=%01%23Eg%89%AB%CD%EF%01%23Eg%89%AB%CD%EF%01%23Eg&peer_id=%2Daria2%2Dultrafastdltl&uploaded=1572864&downloaded=1310720&left=1572864&compact=1&key=fastdltl&numwant=50&no_peer_id=1&port=6989&event=started&supportcrypto=1"), btAnnounce.getAnnounceUrl());

View File

@ -58,6 +58,8 @@ public:
virtual void overrideMinInterval(time_t interval) {}
virtual void setTcpPort(uint16_t port) {}
void setPeerId(const std::string& peerId) {
this->peerId = peerId;
}

View File

@ -36,7 +36,7 @@ public:
virtual SharedHandle<DHTTask>
createPeerLookupTask(const SharedHandle<DownloadContext>& ctx,
const SharedHandle<BtRuntime>& btRuntime,
uint16_t tcpPort,
const SharedHandle<PeerStorage>& peerStorage)
{
return SharedHandle<DHTTask>();