BtRegistry: Use std::unique_ptr for BtObject

pull/106/head
Tatsuhiro Tsujikawa 2013-07-06 19:02:39 +09:00
parent cc3cd8a58b
commit 162c138362
10 changed files with 63 additions and 99 deletions

View File

@ -48,16 +48,14 @@
namespace aria2 { namespace aria2 {
BtRegistry::BtRegistry() BtRegistry::BtRegistry()
: tcpPort_(0), : tcpPort_{0},
udpPort_(0) udpPort_{0}
{} {}
BtRegistry::~BtRegistry() {}
const std::shared_ptr<DownloadContext>& const std::shared_ptr<DownloadContext>&
BtRegistry::getDownloadContext(a2_gid_t gid) const BtRegistry::getDownloadContext(a2_gid_t gid) const
{ {
const std::shared_ptr<BtObject>& res = get(gid); auto res = get(gid);
if(res) { if(res) {
return res->downloadContext; return res->downloadContext;
} else { } else {
@ -68,29 +66,27 @@ BtRegistry::getDownloadContext(a2_gid_t gid) const
const std::shared_ptr<DownloadContext>& const std::shared_ptr<DownloadContext>&
BtRegistry::getDownloadContext(const std::string& infoHash) const BtRegistry::getDownloadContext(const std::string& infoHash) const
{ {
for(std::map<a2_gid_t, std::shared_ptr<BtObject> >::const_iterator i = for(auto& kv : pool_) {
pool_.begin(), eoi = pool_.end(); i != eoi; ++i) { if(bittorrent::getTorrentAttrs(kv.second->downloadContext)->infoHash ==
if(bittorrent::getTorrentAttrs((*i).second->downloadContext)->infoHash ==
infoHash) { infoHash) {
return (*i).second->downloadContext; return kv.second->downloadContext;
} }
} }
return getNull<DownloadContext>(); return getNull<DownloadContext>();
} }
void BtRegistry::put(a2_gid_t gid, const std::shared_ptr<BtObject>& obj) void BtRegistry::put(a2_gid_t gid, std::unique_ptr<BtObject> obj)
{ {
pool_[gid] = obj; pool_[gid] = std::move(obj);
} }
const std::shared_ptr<BtObject>& BtRegistry::get(a2_gid_t gid) const BtObject* BtRegistry::get(a2_gid_t gid) const
{ {
std::map<a2_gid_t, std::shared_ptr<BtObject> >::const_iterator i = auto i = pool_.find(gid);
pool_.find(gid); if(i == std::end(pool_)) {
if(i == pool_.end()) { return nullptr;
return getNull<BtObject>();
} else { } else {
return (*i).second; return (*i).second.get();
} }
} }
@ -99,7 +95,8 @@ bool BtRegistry::remove(a2_gid_t gid)
return pool_.erase(gid); return pool_.erase(gid);
} }
void BtRegistry::removeAll() { void BtRegistry::removeAll()
{
pool_.clear(); pool_.clear();
} }
@ -122,38 +119,14 @@ BtObject::BtObject
const std::shared_ptr<BtAnnounce>& btAnnounce, const std::shared_ptr<BtAnnounce>& btAnnounce,
const std::shared_ptr<BtRuntime>& btRuntime, const std::shared_ptr<BtRuntime>& btRuntime,
const std::shared_ptr<BtProgressInfoFile>& btProgressInfoFile) const std::shared_ptr<BtProgressInfoFile>& btProgressInfoFile)
: downloadContext(downloadContext), : downloadContext{downloadContext},
pieceStorage(pieceStorage), pieceStorage{pieceStorage},
peerStorage(peerStorage), peerStorage{peerStorage},
btAnnounce(btAnnounce), btAnnounce{btAnnounce},
btRuntime(btRuntime), btRuntime{btRuntime},
btProgressInfoFile(btProgressInfoFile) btProgressInfoFile{btProgressInfoFile}
{} {}
BtObject::BtObject() {} BtObject::BtObject() {}
BtObject::BtObject(const BtObject& c)
: downloadContext(c.downloadContext),
pieceStorage(c.pieceStorage),
peerStorage(c.peerStorage),
btAnnounce(c.btAnnounce),
btRuntime(c.btRuntime),
btProgressInfoFile(c.btProgressInfoFile)
{}
BtObject::~BtObject() {}
BtObject& BtObject::operator=(const BtObject& c)
{
if(this != &c) {
downloadContext = c.downloadContext;
pieceStorage = c.pieceStorage;
peerStorage = c.peerStorage;
btAnnounce = c.btAnnounce;
btRuntime = c.btRuntime;
btProgressInfoFile = c.btProgressInfoFile;
}
return *this;
}
} // namespace aria2 } // namespace aria2

View File

@ -69,17 +69,11 @@ struct BtObject {
const std::shared_ptr<BtProgressInfoFile>& btProgressInfoFile); const std::shared_ptr<BtProgressInfoFile>& btProgressInfoFile);
BtObject(); BtObject();
BtObject(const BtObject& c);
~BtObject();
BtObject& operator=(const BtObject& c);
}; };
class BtRegistry { class BtRegistry {
private: private:
std::map<a2_gid_t, std::shared_ptr<BtObject> > pool_; std::map<a2_gid_t, std::unique_ptr<BtObject>> pool_;
uint16_t tcpPort_; uint16_t tcpPort_;
// This is UDP port for DHT and UDP tracker. But currently UDP // This is UDP port for DHT and UDP tracker. But currently UDP
// tracker is not supported in IPv6. // tracker is not supported in IPv6.
@ -88,7 +82,6 @@ private:
std::shared_ptr<UDPTrackerClient> udpTrackerClient_; std::shared_ptr<UDPTrackerClient> udpTrackerClient_;
public: public:
BtRegistry(); BtRegistry();
~BtRegistry();
const std::shared_ptr<DownloadContext>& const std::shared_ptr<DownloadContext>&
getDownloadContext(a2_gid_t gid) const; getDownloadContext(a2_gid_t gid) const;
@ -96,16 +89,15 @@ public:
const std::shared_ptr<DownloadContext>& const std::shared_ptr<DownloadContext>&
getDownloadContext(const std::string& infoHash) const; getDownloadContext(const std::string& infoHash) const;
void put(a2_gid_t gid, const std::shared_ptr<BtObject>& obj); void put(a2_gid_t gid, std::unique_ptr<BtObject> obj);
const std::shared_ptr<BtObject>& get(a2_gid_t gid) const; BtObject* get(a2_gid_t gid) const;
template<typename OutputIterator> template<typename OutputIterator>
OutputIterator getAllDownloadContext(OutputIterator dest) OutputIterator getAllDownloadContext(OutputIterator dest)
{ {
for(std::map<a2_gid_t, std::shared_ptr<BtObject> >::const_iterator i = for(auto& kv : pool_) {
pool_.begin(), eoi = pool_.end(); i != eoi; ++i) { *dest++ = kv.second->downloadContext;
*dest++ = (*i).second->downloadContext;
} }
return dest; return dest;
} }

View File

@ -102,7 +102,7 @@ void BtSetup::setup(std::vector<std::unique_ptr<Command>>& commands,
bittorrent::getTorrentAttrs(requestGroup->getDownloadContext()); bittorrent::getTorrentAttrs(requestGroup->getDownloadContext());
bool metadataGetMode = torrentAttrs->metadata.empty(); bool metadataGetMode = torrentAttrs->metadata.empty();
auto& btReg = e->getBtRegistry(); auto& btReg = e->getBtRegistry();
auto& btObject = btReg->get(requestGroup->getGID()); auto btObject = btReg->get(requestGroup->getGID());
auto& pieceStorage = btObject->pieceStorage; auto& pieceStorage = btObject->pieceStorage;
auto& peerStorage = btObject->peerStorage; auto& peerStorage = btObject->peerStorage;
auto& btRuntime = btObject->btRuntime; auto& btRuntime = btObject->btRuntime;

View File

@ -171,7 +171,7 @@ void printProgress
o << " CN:" o << " CN:"
<< rg->getNumConnection(); << rg->getNumConnection();
#ifdef ENABLE_BITTORRENT #ifdef ENABLE_BITTORRENT
const std::shared_ptr<BtObject>& btObj = e->getBtRegistry()->get(rg->getGID()); auto btObj = e->getBtRegistry()->get(rg->getGID());
if(btObj) { if(btObj) {
const PeerSet& peers = btObj->peerStorage->getUsedPeers(); const PeerSet& peers = btObj->peerStorage->getUsedPeers();
o << " SD:" o << " SD:"

View File

@ -90,7 +90,7 @@ bool LpdReceiveMessageCommand::execute()
} }
RequestGroup* group = dctx->getOwnerRequestGroup(); RequestGroup* group = dctx->getOwnerRequestGroup();
assert(group); assert(group);
auto& btobj = reg->get(group->getGID()); auto btobj = reg->get(group->getGID());
assert(btobj); assert(btobj);
auto& peerStorage = btobj->peerStorage; auto& peerStorage = btobj->peerStorage;
assert(peerStorage); assert(peerStorage);

View File

@ -109,8 +109,7 @@ bool PeerReceiveHandshakeCommand::executeInternal()
(fmt("Unknown info hash %s", (fmt("Unknown info hash %s",
util::toHex(infoHash).c_str())); util::toHex(infoHash).c_str()));
} }
const std::shared_ptr<BtObject>& btObject = auto btObject = getDownloadEngine()->getBtRegistry()->get
getDownloadEngine()->getBtRegistry()->get
(downloadContext->getOwnerRequestGroup()->getGID()); (downloadContext->getOwnerRequestGroup()->getGID());
const std::shared_ptr<BtRuntime>& btRuntime = btObject->btRuntime; const std::shared_ptr<BtRuntime>& btRuntime = btObject->btRuntime;
const std::shared_ptr<PieceStorage>& pieceStorage = btObject->pieceStorage; const std::shared_ptr<PieceStorage>& pieceStorage = btObject->pieceStorage;

View File

@ -350,16 +350,14 @@ void RequestGroup::createInitialCommand
btAnnouncePtr->shuffleAnnounce(); btAnnouncePtr->shuffleAnnounce();
assert(!btRegistry->get(gid_->getNumericId())); assert(!btRegistry->get(gid_->getNumericId()));
btRegistry->put btRegistry->put(gid_->getNumericId(), make_unique<BtObject>
(gid_->getNumericId(), std::shared_ptr<BtObject> (downloadContext_,
(new BtObject pieceStorage_,
(downloadContext_, peerStorage,
pieceStorage_, btAnnounce,
peerStorage, btRuntime,
btAnnounce, (progressInfoFile ?
btRuntime, progressInfoFile : progressInfoFile_)));
(progressInfoFile ?
progressInfoFile : progressInfoFile_))));
if(metadataGetMode) { if(metadataGetMode) {
if(option_->getAsBool(PREF_ENABLE_DHT) || if(option_->getAsBool(PREF_ENABLE_DHT) ||
(!e->getOption()->getAsBool(PREF_DISABLE_IPV6) && (!e->getOption()->getAsBool(PREF_DISABLE_IPV6) &&

View File

@ -731,7 +731,7 @@ namespace {
void gatherProgressBitTorrent void gatherProgressBitTorrent
(const std::shared_ptr<Dict>& entryDict, (const std::shared_ptr<Dict>& entryDict,
TorrentAttribute* torrentAttrs, TorrentAttribute* torrentAttrs,
const std::shared_ptr<BtObject>& btObject, BtObject* btObject,
const std::vector<std::string>& keys) const std::vector<std::string>& keys)
{ {
if(requested_key(keys, KEY_INFO_HASH)) { if(requested_key(keys, KEY_INFO_HASH)) {
@ -800,11 +800,11 @@ void gatherProgress
gatherProgressCommon(entryDict, group, keys); gatherProgressCommon(entryDict, group, keys);
#ifdef ENABLE_BITTORRENT #ifdef ENABLE_BITTORRENT
if(group->getDownloadContext()->hasAttribute(CTX_ATTR_BT)) { if(group->getDownloadContext()->hasAttribute(CTX_ATTR_BT)) {
auto torrentAttrs = gatherProgressBitTorrent(entryDict,
bittorrent::getTorrentAttrs(group->getDownloadContext()); bittorrent::getTorrentAttrs
const std::shared_ptr<BtObject>& btObject = (group->getDownloadContext()),
e->getBtRegistry()->get(group->getGID()); e->getBtRegistry()->get(group->getGID()),
gatherProgressBitTorrent(entryDict, torrentAttrs, btObject, keys); keys);
} }
#endif // ENABLE_BITTORRENT #endif // ENABLE_BITTORRENT
} }
@ -956,8 +956,7 @@ std::shared_ptr<ValueBase> GetPeersRpcMethod::process
GroupId::toHex(gid).c_str())); GroupId::toHex(gid).c_str()));
} }
std::shared_ptr<List> peers = List::g(); std::shared_ptr<List> peers = List::g();
const std::shared_ptr<BtObject>& btObject = auto btObject = e->getBtRegistry()->get(group->getGID());
e->getBtRegistry()->get(group->getGID());
if(btObject) { if(btObject) {
assert(btObject->peerStorage); assert(btObject->peerStorage);
gatherPeer(peers, btObject->peerStorage); gatherPeer(peers, btObject->peerStorage);
@ -1506,8 +1505,7 @@ void changeOption
group->setMaxUploadSpeedLimit(grOption->getAsInt(PREF_MAX_UPLOAD_LIMIT)); group->setMaxUploadSpeedLimit(grOption->getAsInt(PREF_MAX_UPLOAD_LIMIT));
} }
#ifdef ENABLE_BITTORRENT #ifdef ENABLE_BITTORRENT
const std::shared_ptr<BtObject>& btObject = auto btObject = e->getBtRegistry()->get(group->getGID());
e->getBtRegistry()->get(group->getGID());
if(btObject) { if(btObject) {
if(option.defined(PREF_BT_MAX_PEERS)) { if(option.defined(PREF_BT_MAX_PEERS)) {
btObject->btRuntime->setMaxPeers(grOption->getAsInt(PREF_BT_MAX_PEERS)); btObject->btRuntime->setMaxPeers(grOption->getAsInt(PREF_BT_MAX_PEERS));

View File

@ -41,24 +41,24 @@ void BtRegistryTest::testGetDownloadContext()
{ {
BtRegistry btRegistry; BtRegistry btRegistry;
CPPUNIT_ASSERT(!btRegistry.getDownloadContext(1)); CPPUNIT_ASSERT(!btRegistry.getDownloadContext(1));
std::shared_ptr<DownloadContext> dctx(new DownloadContext()); auto dctx = std::make_shared<DownloadContext>();
std::shared_ptr<BtObject> btObject(new BtObject()); auto btObject = make_unique<BtObject>();
btObject->downloadContext = dctx; btObject->downloadContext = dctx;
btRegistry.put(1, btObject); btRegistry.put(1, std::move(btObject));
CPPUNIT_ASSERT_EQUAL(dctx.get(), btRegistry.getDownloadContext(1).get()); CPPUNIT_ASSERT_EQUAL(dctx.get(), btRegistry.getDownloadContext(1).get());
} }
namespace { namespace {
void addTwoDownloadContext(BtRegistry& btRegistry) void addTwoDownloadContext(BtRegistry& btRegistry)
{ {
std::shared_ptr<DownloadContext> dctx1(new DownloadContext()); auto dctx1 = std::make_shared<DownloadContext>();
std::shared_ptr<DownloadContext> dctx2(new DownloadContext()); auto dctx2 = std::make_shared<DownloadContext>();
std::shared_ptr<BtObject> btObject1(new BtObject()); auto btObject1 = make_unique<BtObject>();
btObject1->downloadContext = dctx1; btObject1->downloadContext = dctx1;
std::shared_ptr<BtObject> btObject2(new BtObject()); auto btObject2 = make_unique<BtObject>();
btObject2->downloadContext = dctx2; btObject2->downloadContext = dctx2;
btRegistry.put(1, btObject1); btRegistry.put(1, std::move(btObject1));
btRegistry.put(2, btObject2); btRegistry.put(2, std::move(btObject2));
} }
} // namespace } // namespace

View File

@ -535,9 +535,11 @@ void RpcMethodTest::testChangeOption()
opt->put(PREF_BT_REQUEST_PEER_SPEED_LIMIT->k, "300K"); opt->put(PREF_BT_REQUEST_PEER_SPEED_LIMIT->k, "300K");
opt->put(PREF_MAX_UPLOAD_LIMIT->k, "50K"); opt->put(PREF_MAX_UPLOAD_LIMIT->k, "50K");
std::shared_ptr<BtObject> btObject(new BtObject()); {
btObject->btRuntime = std::shared_ptr<BtRuntime>(new BtRuntime()); auto btObject = make_unique<BtObject>();
e_->getBtRegistry()->put(group->getGID(), btObject); btObject->btRuntime = std::make_shared<BtRuntime>();
e_->getBtRegistry()->put(group->getGID(), std::move(btObject));
}
#endif // ENABLE_BITTORRENT #endif // ENABLE_BITTORRENT
req.params->append(opt); req.params->append(opt);
RpcResponse res = m.execute(req, e_.get()); RpcResponse res = m.execute(req, e_.get());
@ -554,7 +556,9 @@ void RpcMethodTest::testChangeOption()
option->get(PREF_BT_REQUEST_PEER_SPEED_LIMIT)); option->get(PREF_BT_REQUEST_PEER_SPEED_LIMIT));
CPPUNIT_ASSERT_EQUAL(std::string("100"), option->get(PREF_BT_MAX_PEERS)); CPPUNIT_ASSERT_EQUAL(std::string("100"), option->get(PREF_BT_MAX_PEERS));
CPPUNIT_ASSERT_EQUAL(100, btObject->btRuntime->getMaxPeers()); CPPUNIT_ASSERT_EQUAL(100,
e_->getBtRegistry()->get(group->getGID())
->btRuntime->getMaxPeers());
CPPUNIT_ASSERT_EQUAL(50*1024, CPPUNIT_ASSERT_EQUAL(50*1024,
group->getMaxUploadSpeedLimit()); group->getMaxUploadSpeedLimit());