Eliminated static variable from LpdReceiveMessageCommand.

BtRegistry now holds LpdMessageReceiver.
pull/1/head
Tatsuhiro Tsujikawa 2011-07-22 00:41:45 +09:00
parent 97f76b7cf4
commit a6b7ef5f07
5 changed files with 24 additions and 56 deletions

View File

@ -41,6 +41,7 @@
#include "BtRuntime.h" #include "BtRuntime.h"
#include "BtProgressInfoFile.h" #include "BtProgressInfoFile.h"
#include "bittorrent_helper.h" #include "bittorrent_helper.h"
#include "LpdMessageReceiver.h"
namespace aria2 { namespace aria2 {
@ -95,6 +96,12 @@ void BtRegistry::removeAll() {
pool_.clear(); pool_.clear();
} }
void BtRegistry::setLpdMessageReceiver
(const SharedHandle<LpdMessageReceiver>& receiver)
{
lpdMessageReceiver_ = receiver;
}
BtObject::BtObject BtObject::BtObject
(const SharedHandle<DownloadContext>& downloadContext, (const SharedHandle<DownloadContext>& downloadContext,
const SharedHandle<PieceStorage>& pieceStorage, const SharedHandle<PieceStorage>& pieceStorage,

View File

@ -50,6 +50,7 @@ class BtAnnounce;
class BtRuntime; class BtRuntime;
class BtProgressInfoFile; class BtProgressInfoFile;
class DownloadContext; class DownloadContext;
class LpdMessageReceiver;
struct BtObject { struct BtObject {
SharedHandle<DownloadContext> downloadContext_; SharedHandle<DownloadContext> downloadContext_;
@ -81,6 +82,7 @@ class BtRegistry {
private: private:
std::map<a2_gid_t, BtObject> pool_; std::map<a2_gid_t, BtObject> pool_;
uint16_t tcpPort_; uint16_t tcpPort_;
SharedHandle<LpdMessageReceiver> lpdMessageReceiver_;
public: public:
BtRegistry(); BtRegistry();
~BtRegistry(); ~BtRegistry();
@ -117,6 +119,12 @@ public:
{ {
return tcpPort_; return tcpPort_;
} }
void setLpdMessageReceiver(const SharedHandle<LpdMessageReceiver>& receiver);
const SharedHandle<LpdMessageReceiver>& getLpdMessageReceiver() const
{
return lpdMessageReceiver_;
}
}; };
} // namespace aria2 } // namespace aria2

View File

@ -217,7 +217,7 @@ void BtSetup::setup(std::vector<Command*>& commands,
if(option->getAsBool(PREF_BT_ENABLE_LPD) && if(option->getAsBool(PREF_BT_ENABLE_LPD) &&
btReg->getTcpPort() && btReg->getTcpPort() &&
(metadataGetMode || !torrentAttrs->privateTorrent)) { (metadataGetMode || !torrentAttrs->privateTorrent)) {
if(LpdReceiveMessageCommand::getNumInstance() == 0) { if(!btReg->getLpdMessageReceiver()) {
A2_LOG_INFO("Initializing LpdMessageReceiver."); A2_LOG_INFO("Initializing LpdMessageReceiver.");
SharedHandle<LpdMessageReceiver> receiver SharedHandle<LpdMessageReceiver> receiver
(new LpdMessageReceiver(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT)); (new LpdMessageReceiver(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT));
@ -242,29 +242,29 @@ void BtSetup::setup(std::vector<Command*>& commands,
} }
} }
if(initialized) { if(initialized) {
btReg->setLpdMessageReceiver(receiver);
A2_LOG_INFO(fmt("LpdMessageReceiver initialized. multicastAddr=%s:%u," A2_LOG_INFO(fmt("LpdMessageReceiver initialized. multicastAddr=%s:%u,"
" localAddr=%s", " localAddr=%s",
LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT, LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT,
receiver->getLocalAddress().c_str())); receiver->getLocalAddress().c_str()));
LpdReceiveMessageCommand* cmd = LpdReceiveMessageCommand* cmd =
LpdReceiveMessageCommand::getInstance(e, receiver); new LpdReceiveMessageCommand(e->newCUID(), receiver, e);
e->addCommand(cmd); e->addCommand(cmd);
} else { } else {
A2_LOG_INFO("LpdMessageReceiver not initialized."); A2_LOG_INFO("LpdMessageReceiver not initialized.");
} }
} }
if(LpdReceiveMessageCommand::getNumInstance()) { if(btReg->getLpdMessageReceiver()) {
const unsigned char* infoHash = const unsigned char* infoHash =
bittorrent::getInfoHash(requestGroup->getDownloadContext()); bittorrent::getInfoHash(requestGroup->getDownloadContext());
SharedHandle<LpdMessageReceiver> receiver =
LpdReceiveMessageCommand::getInstance()->getLpdMessageReceiver();
A2_LOG_INFO("Initializing LpdMessageDispatcher."); A2_LOG_INFO("Initializing LpdMessageDispatcher.");
SharedHandle<LpdMessageDispatcher> dispatcher SharedHandle<LpdMessageDispatcher> dispatcher
(new LpdMessageDispatcher (new LpdMessageDispatcher
(std::string(&infoHash[0], &infoHash[INFO_HASH_LENGTH]), (std::string(&infoHash[0], &infoHash[INFO_HASH_LENGTH]),
btReg->getTcpPort(), btReg->getTcpPort(),
LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT)); LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT));
if(dispatcher->init(receiver->getLocalAddress(), /*ttl*/1, /*loop*/0)) { if(dispatcher->init(btReg->getLpdMessageReceiver()->getLocalAddress(),
/*ttl*/1, /*loop*/0)) {
A2_LOG_INFO("LpdMessageDispatcher initialized."); A2_LOG_INFO("LpdMessageDispatcher initialized.");
LpdDispatchMessageCommand* cmd = LpdDispatchMessageCommand* cmd =
new LpdDispatchMessageCommand(e->newCUID(), dispatcher, e); new LpdDispatchMessageCommand(e->newCUID(), dispatcher, e);

View File

@ -51,10 +51,6 @@
namespace aria2 { namespace aria2 {
unsigned int LpdReceiveMessageCommand::numInstance_ = 0;
LpdReceiveMessageCommand* LpdReceiveMessageCommand::instance_ = 0;
LpdReceiveMessageCommand::LpdReceiveMessageCommand LpdReceiveMessageCommand::LpdReceiveMessageCommand
(cuid_t cuid, (cuid_t cuid,
const SharedHandle<LpdMessageReceiver>& receiver, const SharedHandle<LpdMessageReceiver>& receiver,
@ -64,16 +60,11 @@ LpdReceiveMessageCommand::LpdReceiveMessageCommand
e_(e) e_(e)
{ {
e_->addSocketForReadCheck(receiver_->getSocket(), this); e_->addSocketForReadCheck(receiver_->getSocket(), this);
++numInstance_;
} }
LpdReceiveMessageCommand::~LpdReceiveMessageCommand() LpdReceiveMessageCommand::~LpdReceiveMessageCommand()
{ {
e_->deleteSocketForReadCheck(receiver_->getSocket(), this); e_->deleteSocketForReadCheck(receiver_->getSocket(), this);
--numInstance_;
if(numInstance_ == 0) {
instance_ = 0;
}
} }
bool LpdReceiveMessageCommand::execute() bool LpdReceiveMessageCommand::execute()
@ -123,23 +114,4 @@ bool LpdReceiveMessageCommand::execute()
return false; return false;
} }
LpdReceiveMessageCommand*
LpdReceiveMessageCommand::getInstance
(DownloadEngine* e, const SharedHandle<LpdMessageReceiver>& receiver)
{
if(numInstance_ == 0) {
instance_ = new LpdReceiveMessageCommand(e->newCUID(), receiver, e);
}
return instance_;
}
LpdReceiveMessageCommand* LpdReceiveMessageCommand::getInstance()
{
if(numInstance_ == 0) {
return 0;
} else {
return instance_;
}
}
} // namespace aria2 } // namespace aria2

View File

@ -47,17 +47,11 @@ class SocketCore;
class LpdReceiveMessageCommand:public Command { class LpdReceiveMessageCommand:public Command {
private: private:
SharedHandle<LpdMessageReceiver> receiver_; SharedHandle<LpdMessageReceiver> receiver_;
DownloadEngine* e_;
static unsigned int numInstance_; public:
static LpdReceiveMessageCommand* instance_;
LpdReceiveMessageCommand LpdReceiveMessageCommand
(cuid_t cuid, const SharedHandle<LpdMessageReceiver>& receiver, (cuid_t cuid, const SharedHandle<LpdMessageReceiver>& receiver,
DownloadEngine* e); DownloadEngine* e);
DownloadEngine* e_;
public:
virtual ~LpdReceiveMessageCommand(); virtual ~LpdReceiveMessageCommand();
virtual bool execute(); virtual bool execute();
@ -66,19 +60,6 @@ public:
{ {
return receiver_; return receiver_;
} }
static LpdReceiveMessageCommand*
getInstance
(DownloadEngine* e, const SharedHandle<LpdMessageReceiver>& receiver);
// If numInstance_ is 0, then return 0. If numInstance_ > 0, it
// returns instance_
static LpdReceiveMessageCommand* getInstance();
static unsigned int getNumInstance()
{
return numInstance_;
}
}; };
} // namespace aria2 } // namespace aria2