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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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