DownloadContext::attrs_ now holds std::unique_ptr

DownloadContext::getAttribute() returns a raw pointer.
pull/103/head
Tatsuhiro Tsujikawa 2013-06-23 00:59:55 +09:00
parent 1a299c4d7c
commit bef6236da8
29 changed files with 146 additions and 157 deletions

View File

@ -101,7 +101,7 @@ bool BtDependency::resolve()
diskAdaptor->openExistingFile();
std::string content = util::toString(diskAdaptor);
if(dependee->getDownloadContext()->hasAttribute(CTX_ATTR_BT)) {
std::shared_ptr<TorrentAttribute> attrs =
auto attrs =
bittorrent::getTorrentAttrs(dependee->getDownloadContext());
bittorrent::loadFromMemory
(bittorrent::metadata2Torrent(content, attrs), context,

View File

@ -98,7 +98,7 @@ void BtSetup::setup(std::vector<Command*>& commands,
if(!requestGroup->getDownloadContext()->hasAttribute(CTX_ATTR_BT)){
return;
}
std::shared_ptr<TorrentAttribute> torrentAttrs =
auto torrentAttrs =
bittorrent::getTorrentAttrs(requestGroup->getDownloadContext());
bool metadataGetMode = torrentAttrs->metadata.empty();
const std::shared_ptr<BtRegistry>& btReg = e->getBtRegistry();

View File

@ -195,8 +195,7 @@ void DefaultBtInteractive::addHandshakeExtendedMessageToQueue()
m->setClientVersion("aria2/" PACKAGE_VERSION);
m->setTCPPort(tcpPort_);
m->setExtensions(extensionMessageRegistry_->getExtensions());
std::shared_ptr<TorrentAttribute> attrs =
bittorrent::getTorrentAttrs(downloadContext_);
auto attrs = bittorrent::getTorrentAttrs(downloadContext_);
if(!attrs->metadata.empty()) {
m->setMetadataSize(attrs->metadataSize);
}

View File

@ -478,8 +478,7 @@ void DefaultPieceStorage::completePiece(const std::shared_ptr<Piece>& piece)
}
#ifdef ENABLE_BITTORRENT
if(downloadContext_->hasAttribute(CTX_ATTR_BT)) {
std::shared_ptr<TorrentAttribute> torrentAttrs =
bittorrent::getTorrentAttrs(downloadContext_);
auto torrentAttrs = bittorrent::getTorrentAttrs(downloadContext_);
if(!torrentAttrs->metadata.empty()) {
#ifdef __MINGW32__
// On Windows, if aria2 opens files with GENERIC_WRITE access

View File

@ -150,19 +150,18 @@ void DownloadContext::setFileFilter(SegList<int>& sgl)
}
void DownloadContext::setAttribute
(ContextAttributeType key, const std::shared_ptr<ContextAttribute>& value)
(ContextAttributeType key, std::unique_ptr<ContextAttribute>&& value)
{
assert(key < MAX_CTX_ATTR);
attrs_[key] = value;
attrs_[key] = std::move(value);
}
const std::shared_ptr<ContextAttribute>& DownloadContext::getAttribute
(ContextAttributeType key)
ContextAttribute* DownloadContext::getAttribute(ContextAttributeType key)
{
assert(key < MAX_CTX_ATTR);
const std::shared_ptr<ContextAttribute>& attr = attrs_[key];
const std::unique_ptr<ContextAttribute>& attr = attrs_[key];
if(attr) {
return attr;
return attr.get();
} else {
throw DL_ABORT_EX(fmt("No attribute named %s",
strContextAttributeType(key)));

View File

@ -78,7 +78,7 @@ private:
RequestGroup* ownerRequestGroup_;
std::vector<std::shared_ptr<ContextAttribute> > attrs_;
std::vector<std::unique_ptr<ContextAttribute> > attrs_;
NetStat netStat_;
@ -204,9 +204,9 @@ public:
}
void setAttribute
(ContextAttributeType key, const std::shared_ptr<ContextAttribute>& value);
(ContextAttributeType key, std::unique_ptr<ContextAttribute>&& value);
const std::shared_ptr<ContextAttribute>& getAttribute(ContextAttributeType key);
ContextAttribute* getAttribute(ContextAttributeType key);
bool hasAttribute(ContextAttributeType key) const;

View File

@ -109,7 +109,7 @@ void HandshakeExtensionMessage::doReceivedAction()
peer_->setExtension(i, id);
}
}
std::shared_ptr<TorrentAttribute> attrs = bittorrent::getTorrentAttrs(dctx_);
auto attrs = bittorrent::getTorrentAttrs(dctx_);
if(attrs->metadata.empty()) {
if(!peer_->getExtensionMessageID(ExtensionMessageRegistry::UT_METADATA)) {
// TODO In metadataGetMode, if peer don't support metadata

View File

@ -114,7 +114,7 @@ PeerInteractionCommand::PeerInteractionCommand
family = AF_INET;
}
std::shared_ptr<TorrentAttribute> torrentAttrs =
auto torrentAttrs =
bittorrent::getTorrentAttrs(requestGroup_->getDownloadContext());
bool metadataGetMode = torrentAttrs->metadata.empty();

View File

@ -287,8 +287,7 @@ void RequestGroup::createInitialCommand
#ifdef ENABLE_BITTORRENT
{
if(downloadContext_->hasAttribute(CTX_ATTR_BT)) {
std::shared_ptr<TorrentAttribute> torrentAttrs =
bittorrent::getTorrentAttrs(downloadContext_);
auto torrentAttrs = bittorrent::getTorrentAttrs(downloadContext_);
bool metadataGetMode = torrentAttrs->metadata.empty();
if(option_->getAsBool(PREF_DRY_RUN)) {
throw DOWNLOAD_FAILURE_EXCEPTION
@ -1207,8 +1206,7 @@ void RequestGroup::reportDownloadFinished()
int64_t completedLength = getCompletedLength();
double shareRatio = completedLength == 0 ? 0.0 :
1.0*stat.allTimeUploadLength/completedLength;
std::shared_ptr<TorrentAttribute> attrs =
bittorrent::getTorrentAttrs(downloadContext_);
auto attrs = bittorrent::getTorrentAttrs(downloadContext_);
if(!attrs->metadata.empty()) {
A2_LOG_NOTICE(fmt(MSG_SHARE_RATIO_REPORT,
shareRatio,

View File

@ -697,8 +697,7 @@ void gatherProgressCommon
#ifdef ENABLE_BITTORRENT
void gatherBitTorrentMetadata
(const std::shared_ptr<Dict>& btDict,
const std::shared_ptr<TorrentAttribute>& torrentAttrs)
(const std::shared_ptr<Dict>& btDict, TorrentAttribute* torrentAttrs)
{
if(!torrentAttrs->comment.empty()) {
btDict->put(KEY_COMMENT, torrentAttrs->comment);
@ -731,7 +730,7 @@ void gatherBitTorrentMetadata
namespace {
void gatherProgressBitTorrent
(const std::shared_ptr<Dict>& entryDict,
const std::shared_ptr<TorrentAttribute>& torrentAttrs,
TorrentAttribute* torrentAttrs,
const std::shared_ptr<BtObject>& btObject,
const std::vector<std::string>& keys)
{
@ -801,7 +800,7 @@ void gatherProgress
gatherProgressCommon(entryDict, group, keys);
#ifdef ENABLE_BITTORRENT
if(group->getDownloadContext()->hasAttribute(CTX_ATTR_BT)) {
std::shared_ptr<TorrentAttribute> torrentAttrs =
auto torrentAttrs =
bittorrent::getTorrentAttrs(group->getDownloadContext());
const std::shared_ptr<BtObject>& btObject =
e->getBtRegistry()->get(group->getGID());

View File

@ -599,8 +599,7 @@ void gatherProgressCommon
#ifdef ENABLE_BITTORRENT
// Helper function to store BitTorrent metadata from torrentAttrs.
void gatherBitTorrentMetadata
(const std::shared_ptr<Dict>& btDict,
const std::shared_ptr<TorrentAttribute>& torrentAttrs);
(const std::shared_ptr<Dict>& btDict, TorrentAttribute* torrentAttrs);
#endif // ENABLE_BITTORRENT
} // namespace rpc

View File

@ -328,8 +328,7 @@ namespace {
bool backupTrackerIsAvailable
(const std::shared_ptr<DownloadContext>& context)
{
std::shared_ptr<TorrentAttribute> torrentAttrs =
bittorrent::getTorrentAttrs(context);
auto torrentAttrs = bittorrent::getTorrentAttrs(context);
if(torrentAttrs->announceList.size() >= 2) {
return true;
}

View File

@ -59,7 +59,7 @@ bool UTMetadataPostDownloadHandler::Criteria::match
const std::shared_ptr<DownloadContext>& dctx =
requestGroup->getDownloadContext();
if(dctx->hasAttribute(CTX_ATTR_BT)) {
std::shared_ptr<TorrentAttribute> attrs = bittorrent::getTorrentAttrs(dctx);
auto attrs = bittorrent::getTorrentAttrs(dctx);
if(attrs->metadata.empty()) {
return true;
}
@ -77,7 +77,7 @@ void UTMetadataPostDownloadHandler::getNextRequestGroups
(std::vector<std::shared_ptr<RequestGroup> >& groups, RequestGroup* requestGroup)
{
const std::shared_ptr<DownloadContext>& dctx =requestGroup->getDownloadContext();
std::shared_ptr<TorrentAttribute> attrs = bittorrent::getTorrentAttrs(dctx);
auto attrs = bittorrent::getTorrentAttrs(dctx);
std::string metadata =
util::toString(requestGroup->getPieceStorage()->getDiskAdaptor());
std::string torrent = bittorrent::metadata2Torrent(metadata, attrs);

View File

@ -76,7 +76,7 @@ std::string UTMetadataRequestExtensionMessage::toString() const
void UTMetadataRequestExtensionMessage::doReceivedAction()
{
std::shared_ptr<TorrentAttribute> attrs = bittorrent::getTorrentAttrs(dctx_);
auto attrs = bittorrent::getTorrentAttrs(dctx_);
uint8_t id = peer_->getExtensionMessageID
(ExtensionMessageRegistry::UT_METADATA);
if(attrs->metadata.empty()) {

View File

@ -129,17 +129,16 @@ void extractPieceHash(const std::shared_ptr<DownloadContext>& ctx,
namespace {
void extractUrlList
(const std::shared_ptr<TorrentAttribute>& torrent, std::vector<std::string>& uris,
(TorrentAttribute* torrent, std::vector<std::string>& uris,
const ValueBase* v)
{
class UrlListVisitor:public ValueBaseVisitor {
private:
std::vector<std::string>& uris_;
const std::shared_ptr<TorrentAttribute>& torrent_;
TorrentAttribute* torrent_;
public:
UrlListVisitor
(std::vector<std::string>& uris,
const std::shared_ptr<TorrentAttribute>& torrent):
(std::vector<std::string>& uris, TorrentAttribute* torrent):
uris_(uris), torrent_(torrent) {}
virtual void visit(const String& v)
@ -195,7 +194,7 @@ OutputIterator createUri
namespace {
void extractFileEntries
(const std::shared_ptr<DownloadContext>& ctx,
const std::shared_ptr<TorrentAttribute>& torrent,
TorrentAttribute* torrent,
const Dict* infoDict,
const std::shared_ptr<Option>& option,
const std::string& defaultName,
@ -341,8 +340,7 @@ void extractFileEntries
} // namespace
namespace {
void extractAnnounce
(const std::shared_ptr<TorrentAttribute>& torrent, const Dict* rootDict)
void extractAnnounce(TorrentAttribute* torrent, const Dict* rootDict)
{
const List* announceList = downcast<List>(rootDict->get(C_ANNOUNCE_LIST));
if(announceList) {
@ -376,8 +374,7 @@ void extractAnnounce
} // namespace
namespace {
void extractNodes
(const std::shared_ptr<TorrentAttribute>& torrent, const ValueBase* nodesListSrc)
void extractNodes(TorrentAttribute* torrent, const ValueBase* nodesListSrc)
{
const List* nodesList = downcast<List>(nodesListSrc);
if(nodesList) {
@ -425,7 +422,7 @@ void processRootDictionary
throw DL_ABORT_EX2(fmt(MSG_MISSING_BT_INFO, C_INFO.c_str()),
error_code::BITTORRENT_PARSE_ERROR);
}
std::shared_ptr<TorrentAttribute> torrent(new TorrentAttribute());
std::unique_ptr<TorrentAttribute> torrent(new TorrentAttribute());
// retrieve infoHash
std::string encodedInfoDict = bencode2::encode(infoDict);
@ -478,22 +475,22 @@ void processRootDictionary
// This implemantation obeys HTTP-Seeding specification:
// see http://www.getright.com/seedtorrent.html
std::vector<std::string> urlList;
extractUrlList(torrent, urlList, rootDict->get(C_URL_LIST).get());
extractUrlList(torrent.get(), urlList, rootDict->get(C_URL_LIST).get());
urlList.insert(urlList.end(), uris.begin(), uris.end());
std::sort(urlList.begin(), urlList.end());
urlList.erase(std::unique(urlList.begin(), urlList.end()), urlList.end());
// retrieve file entries
extractFileEntries
(ctx, torrent, infoDict, option, defaultName, overrideName, urlList);
(ctx, torrent.get(), infoDict, option, defaultName, overrideName, urlList);
if((ctx->getTotalLength()+pieceLength-1)/pieceLength != numPieces) {
throw DL_ABORT_EX2("Too few/many piece hash.",
error_code::BITTORRENT_PARSE_ERROR);
}
// retrieve announce
extractAnnounce(torrent, rootDict);
extractAnnounce(torrent.get(), rootDict);
// retrieve nodes
extractNodes(torrent, rootDict->get(C_NODES).get());
extractNodes(torrent.get(), rootDict->get(C_NODES).get());
const Integer* creationDate = downcast<Integer>(rootDict->get(C_CREATION_DATE));
if(creationDate) {
@ -513,7 +510,7 @@ void processRootDictionary
torrent->createdBy = util::encodeNonUtf8(createdBy->s());
}
ctx->setAttribute(CTX_ATTR_BT, torrent);
ctx->setAttribute(CTX_ATTR_BT, std::move(torrent));
}
} // namespace
@ -621,16 +618,15 @@ void loadFromMemory(const std::shared_ptr<ValueBase>& torrent,
uris);
}
std::shared_ptr<TorrentAttribute> getTorrentAttrs
TorrentAttribute* getTorrentAttrs
(const std::shared_ptr<DownloadContext>& dctx)
{
return getTorrentAttrs(dctx.get());
}
std::shared_ptr<TorrentAttribute> getTorrentAttrs(DownloadContext* dctx)
TorrentAttribute* getTorrentAttrs(DownloadContext* dctx)
{
return std::static_pointer_cast<TorrentAttribute>
(dctx->getAttribute(CTX_ATTR_BT));
return static_cast<TorrentAttribute*>(dctx->getAttribute(CTX_ATTR_BT));
}
const unsigned char* getInfoHash
@ -907,7 +903,7 @@ void assertID
}
}
std::shared_ptr<TorrentAttribute> parseMagnet(const std::string& magnet)
std::unique_ptr<TorrentAttribute> parseMagnet(const std::string& magnet)
{
std::shared_ptr<Dict> r = magnet::parse(magnet);
if(!r) {
@ -919,7 +915,7 @@ std::shared_ptr<TorrentAttribute> parseMagnet(const std::string& magnet)
throw DL_ABORT_EX2("Missing xt parameter in Magnet URI.",
error_code::MAGNET_PARSE_ERROR);
}
std::shared_ptr<TorrentAttribute> attrs(new TorrentAttribute());
auto attrs = make_unique<TorrentAttribute>();
std::string infoHash;
for(List::ValueType::const_iterator xtiter = xts->begin(),
eoi = xts->end(); xtiter != eoi && infoHash.empty(); ++xtiter) {
@ -969,12 +965,11 @@ std::shared_ptr<TorrentAttribute> parseMagnet(const std::string& magnet)
void loadMagnet
(const std::string& magnet, const std::shared_ptr<DownloadContext>& dctx)
{
std::shared_ptr<TorrentAttribute> attrs = parseMagnet(magnet);
dctx->setAttribute(CTX_ATTR_BT, attrs);
dctx->setAttribute(CTX_ATTR_BT, parseMagnet(magnet));
}
std::string metadata2Torrent
(const std::string& metadata, const std::shared_ptr<TorrentAttribute>& attrs)
(const std::string& metadata, const TorrentAttribute* attrs)
{
std::string torrent = "d";
@ -1001,7 +996,7 @@ std::string metadata2Torrent
return torrent;
}
std::string torrent2Magnet(const std::shared_ptr<TorrentAttribute>& attrs)
std::string torrent2Magnet(const TorrentAttribute* attrs)
{
std::string uri = "magnet:?";
if(!attrs->infoHash.empty()) {
@ -1038,8 +1033,7 @@ int getCompactLength(int family)
}
void removeAnnounceUri
(const std::shared_ptr<TorrentAttribute>& attrs,
const std::vector<std::string>& uris)
(TorrentAttribute* attrs, const std::vector<std::string>& uris)
{
if(uris.empty()) {
return;
@ -1066,8 +1060,7 @@ void removeAnnounceUri
}
void addAnnounceUri
(const std::shared_ptr<TorrentAttribute>& attrs,
const std::vector<std::string>& uris)
(TorrentAttribute* attrs, const std::vector<std::string>& uris)
{
for(std::vector<std::string>::const_iterator i = uris.begin(),
eoi = uris.end(); i != eoi; ++i) {
@ -1078,8 +1071,7 @@ void addAnnounceUri
}
void adjustAnnounceUri
(const std::shared_ptr<TorrentAttribute>& attrs,
const std::shared_ptr<Option>& option)
(TorrentAttribute* attrs, const std::shared_ptr<Option>& option)
{
std::vector<std::string> excludeUris;
std::vector<std::string> addUris;

View File

@ -117,7 +117,7 @@ void loadFromMemory(const std::shared_ptr<ValueBase>& torrent,
// magnet:?xt=urn:btih:<info-hash>&dn=<name>&tr=<tracker-url>
// <info-hash> comes in 2 flavors: 40bytes hexadecimal ascii string,
// or 32bytes Base32 encoded string.
std::shared_ptr<TorrentAttribute> parseMagnet(const std::string& magnet);
std::unique_ptr<TorrentAttribute> parseMagnet(const std::string& magnet);
// Parses BitTorrent Magnet URI and set them in ctx as a
// bittorrent::BITTORRENT attibute. If parsing operation failed, an
@ -149,8 +149,8 @@ void computeFastSet
(std::vector<size_t>& fastSet, const std::string& ipaddr,
size_t numPieces, const unsigned char* infoHash, size_t fastSetSize);
std::shared_ptr<TorrentAttribute> getTorrentAttrs(DownloadContext* dctx);
std::shared_ptr<TorrentAttribute> getTorrentAttrs
TorrentAttribute* getTorrentAttrs(DownloadContext* dctx);
TorrentAttribute* getTorrentAttrs
(const std::shared_ptr<DownloadContext>& dctx);
// Returns the value associated with INFO_HASH key in BITTORRENT
@ -240,22 +240,20 @@ void assertID
// Converts attrs into torrent data. This function does not guarantee
// the returned string is valid torrent data.
std::string metadata2Torrent
(const std::string& metadata, const std::shared_ptr<TorrentAttribute>& attrs);
(const std::string& metadata, const TorrentAttribute* attrs);
// Constructs BitTorrent Magnet URI using attrs.
std::string torrent2Magnet(const std::shared_ptr<TorrentAttribute>& attrs);
std::string torrent2Magnet(const TorrentAttribute* attrs);
// Removes announce URI in uris from attrs. If uris contains '*', all
// announce URIs are removed.
void removeAnnounceUri
(const std::shared_ptr<TorrentAttribute>& attrs,
const std::vector<std::string>& uris);
(TorrentAttribute* attrs, const std::vector<std::string>& uris);
// Adds announce URI in uris to attrs. Each URI in uris creates its
// own tier.
void addAnnounceUri
(const std::shared_ptr<TorrentAttribute>& attrs,
const std::vector<std::string>& uris);
(TorrentAttribute* attrs, const std::vector<std::string>& uris);
// This helper function uses 2 option values: PREF_BT_TRACKER and
// PREF_BT_EXCLUDE_TRACKER. First, the value of
@ -263,8 +261,7 @@ void addAnnounceUri
// and call removeAnnounceUri(). Then the value of PREF_BT_TRACKER is
// converted to std::vector<std::string> and call addAnnounceUri().
void adjustAnnounceUri
(const std::shared_ptr<TorrentAttribute>& attrs,
const std::shared_ptr<Option>& option);
(TorrentAttribute* attrs, const std::shared_ptr<Option>& option);
template<typename OutputIterator>
void extractPeer(const ValueBase* peerData, int family, OutputIterator dest)
@ -346,7 +343,7 @@ const char* getModeString(BtFileMode mode);
template<typename Output>
void print(Output& o, const std::shared_ptr<DownloadContext>& dctx)
{
std::shared_ptr<TorrentAttribute> torrentAttrs = getTorrentAttrs(dctx);
TorrentAttribute* torrentAttrs = getTorrentAttrs(dctx);
o.write("*** BitTorrent File Information ***\n");
if(!torrentAttrs->comment.empty()) {
o.printf("Comment: %s\n", torrentAttrs->comment.c_str());

View File

@ -244,8 +244,7 @@ createBtMagnetRequestGroup
rg->setFileAllocationEnabled(false);
rg->setPreLocalFileCheckEnabled(false);
bittorrent::loadMagnet(magnetLink, dctx);
std::shared_ptr<TorrentAttribute> torrentAttrs =
bittorrent::getTorrentAttrs(dctx);
auto torrentAttrs = bittorrent::getTorrentAttrs(dctx);
bittorrent::adjustAnnounceUri(torrentAttrs, option);
dctx->getFirstFileEntry()->setPath(torrentAttrs->name);
rg->setDownloadContext(dctx);

View File

@ -260,7 +260,7 @@ void BittorrentHelperTest::testOverrideName()
void BittorrentHelperTest::testGetAnnounceTier() {
std::shared_ptr<DownloadContext> dctx(new DownloadContext());
load(A2_TEST_DIR"/single.torrent", dctx, option_);
std::shared_ptr<TorrentAttribute> attrs = getTorrentAttrs(dctx);
auto attrs = getTorrentAttrs(dctx);
// There is 1 tier.
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList.size());
@ -272,7 +272,7 @@ void BittorrentHelperTest::testGetAnnounceTier() {
void BittorrentHelperTest::testGetAnnounceTierAnnounceList() {
std::shared_ptr<DownloadContext> dctx(new DownloadContext());
load(A2_TEST_DIR"/test.torrent", dctx, option_);
std::shared_ptr<TorrentAttribute> attrs = getTorrentAttrs(dctx);
auto attrs = getTorrentAttrs(dctx);
// There are 3 tiers.
CPPUNIT_ASSERT_EQUAL((size_t)3, attrs->announceList.size());
@ -534,7 +534,7 @@ void BittorrentHelperTest::testGetNodes()
std::shared_ptr<DownloadContext> dctx(new DownloadContext());
loadFromMemory(memory, dctx, option_, "default");
std::shared_ptr<TorrentAttribute> attrs = getTorrentAttrs(dctx);
auto attrs = getTorrentAttrs(dctx);
CPPUNIT_ASSERT_EQUAL((size_t)2, attrs->nodes.size());
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), attrs->nodes[0].first);
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, attrs->nodes[0].second);
@ -554,7 +554,7 @@ void BittorrentHelperTest::testGetNodes()
std::shared_ptr<DownloadContext> dctx(new DownloadContext());
loadFromMemory(memory, dctx, option_, "default");
std::shared_ptr<TorrentAttribute> attrs = getTorrentAttrs(dctx);
auto attrs = getTorrentAttrs(dctx);
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
@ -572,7 +572,7 @@ void BittorrentHelperTest::testGetNodes()
std::shared_ptr<DownloadContext> dctx(new DownloadContext());
loadFromMemory(memory, dctx, option_, "default");
std::shared_ptr<TorrentAttribute> attrs = getTorrentAttrs(dctx);
auto attrs = getTorrentAttrs(dctx);
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
@ -590,7 +590,7 @@ void BittorrentHelperTest::testGetNodes()
std::shared_ptr<DownloadContext> dctx(new DownloadContext());
loadFromMemory(memory, dctx, option_, "default");
std::shared_ptr<TorrentAttribute> attrs = getTorrentAttrs(dctx);
auto attrs = getTorrentAttrs(dctx);
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
@ -607,7 +607,7 @@ void BittorrentHelperTest::testGetNodes()
std::shared_ptr<DownloadContext> dctx(new DownloadContext());
loadFromMemory(memory, dctx, option_, "default");
std::shared_ptr<TorrentAttribute> attrs = getTorrentAttrs(dctx);
auto attrs = getTorrentAttrs(dctx);
CPPUNIT_ASSERT_EQUAL((size_t)0, attrs->nodes.size());
}
{
@ -623,7 +623,7 @@ void BittorrentHelperTest::testGetNodes()
std::shared_ptr<DownloadContext> dctx(new DownloadContext());
loadFromMemory(memory, dctx, option_, "default");
std::shared_ptr<TorrentAttribute> attrs = getTorrentAttrs(dctx);
auto attrs = getTorrentAttrs(dctx);
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->nodes.size());
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), attrs->nodes[0].first);
CPPUNIT_ASSERT_EQUAL((uint16_t)6882, attrs->nodes[0].second);
@ -749,7 +749,7 @@ void BittorrentHelperTest::testMetadata() {
std::shared_ptr<ValueBase> tr = bencode2::decode(torrentData);
std::shared_ptr<ValueBase> infoDic = downcast<Dict>(tr)->get("info");
std::string metadata = bencode2::encode(infoDic);
std::shared_ptr<TorrentAttribute> attrs = getTorrentAttrs(dctx);
auto attrs = getTorrentAttrs(dctx);
CPPUNIT_ASSERT(metadata == attrs->metadata);
CPPUNIT_ASSERT_EQUAL(metadata.size(), attrs->metadataSize);
}
@ -759,7 +759,7 @@ void BittorrentHelperTest::testParseMagnet()
std::string magnet =
"magnet:?xt=urn:btih:248d0a1cd08284299de78d5c1ed359bb46717d8c&dn=aria2"
"&tr=http://tracker1&tr=http://tracker2";
std::shared_ptr<TorrentAttribute> attrs = bittorrent::parseMagnet(magnet);
auto attrs = bittorrent::parseMagnet(magnet);
CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
util::toHex(attrs->infoHash));
CPPUNIT_ASSERT_EQUAL(std::string("[METADATA]aria2"), attrs->name);
@ -788,7 +788,7 @@ void BittorrentHelperTest::testParseMagnet_base32()
std::string infoHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
std::string base32InfoHash = base32::encode(fromHex(infoHash));
std::string magnet = "magnet:?xt=urn:btih:"+base32InfoHash+"&dn=aria2";
std::shared_ptr<TorrentAttribute> attrs = bittorrent::parseMagnet(magnet);
auto attrs = bittorrent::parseMagnet(magnet);
CPPUNIT_ASSERT_EQUAL
(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
util::toHex(attrs->infoHash));
@ -796,19 +796,19 @@ void BittorrentHelperTest::testParseMagnet_base32()
void BittorrentHelperTest::testMetadata2Torrent()
{
TorrentAttribute attrs;
std::string metadata = "METADATA";
std::shared_ptr<TorrentAttribute> attrs(new TorrentAttribute());
CPPUNIT_ASSERT_EQUAL
(std::string("d4:infoMETADATAe"), metadata2Torrent(metadata, attrs));
attrs->announceList.push_back(std::vector<std::string>());
attrs->announceList[0].push_back("http://localhost/announce");
(std::string("d4:infoMETADATAe"), metadata2Torrent(metadata, &attrs));
attrs.announceList.push_back(std::vector<std::string>());
attrs.announceList[0].push_back("http://localhost/announce");
CPPUNIT_ASSERT_EQUAL
(std::string("d"
"13:announce-list"
"ll25:http://localhost/announceee"
"4:infoMETADATA"
"e"),
metadata2Torrent(metadata, attrs));
metadata2Torrent(metadata, &attrs));
}
void BittorrentHelperTest::testTorrent2Magnet()
@ -927,78 +927,78 @@ void BittorrentHelperTest::testUnpackcompact()
void BittorrentHelperTest::testRemoveAnnounceUri()
{
std::shared_ptr<TorrentAttribute> attrs(new TorrentAttribute());
TorrentAttribute attrs;
std::vector<std::string> tier1;
tier1.push_back("http://host1/announce");
std::vector<std::string> tier2;
tier2.push_back("http://host2/announce");
tier2.push_back("http://host3/announce");
attrs->announceList.push_back(tier1);
attrs->announceList.push_back(tier2);
attrs.announceList.push_back(tier1);
attrs.announceList.push_back(tier2);
std::vector<std::string> removeUris;
removeUris.push_back(tier1[0]);
removeUris.push_back(tier2[0]);
removeAnnounceUri(attrs, removeUris);
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList.size());
removeAnnounceUri(&attrs, removeUris);
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs.announceList.size());
CPPUNIT_ASSERT_EQUAL(std::string("http://host3/announce"),
attrs->announceList[0][0]);
attrs.announceList[0][0]);
removeUris.clear();
removeUris.push_back("*");
removeAnnounceUri(attrs, removeUris);
CPPUNIT_ASSERT(attrs->announceList.empty());
removeAnnounceUri(&attrs, removeUris);
CPPUNIT_ASSERT(attrs.announceList.empty());
}
void BittorrentHelperTest::testAddAnnounceUri()
{
std::shared_ptr<TorrentAttribute> attrs(new TorrentAttribute());
TorrentAttribute attrs;
std::vector<std::string> addUris;
addUris.push_back("http://host1/announce");
addUris.push_back("http://host2/announce");
addAnnounceUri(attrs, addUris);
CPPUNIT_ASSERT_EQUAL((size_t)2, attrs->announceList.size());
addAnnounceUri(&attrs, addUris);
CPPUNIT_ASSERT_EQUAL((size_t)2, attrs.announceList.size());
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[0].size());
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs.announceList[0].size());
CPPUNIT_ASSERT_EQUAL(std::string("http://host1/announce"),
attrs->announceList[0][0]);
attrs.announceList[0][0]);
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[1].size());
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs.announceList[1].size());
CPPUNIT_ASSERT_EQUAL(std::string("http://host2/announce"),
attrs->announceList[1][0]);
attrs.announceList[1][0]);
}
void BittorrentHelperTest::testAdjustAnnounceUri()
{
std::shared_ptr<TorrentAttribute> attrs(new TorrentAttribute());
TorrentAttribute attrs;
std::vector<std::string> tier1;
tier1.push_back("http://host1/announce");
std::vector<std::string> tier2;
tier2.push_back("http://host2/announce");
tier2.push_back("http://host3/announce");
attrs->announceList.push_back(tier1);
attrs->announceList.push_back(tier2);
attrs.announceList.push_back(tier1);
attrs.announceList.push_back(tier2);
std::shared_ptr<Option> option(new Option());
option->put(PREF_BT_TRACKER, "http://host1/announce,http://host4/announce");
option->put(PREF_BT_EXCLUDE_TRACKER,
"http://host1/announce,http://host2/announce");
adjustAnnounceUri(attrs, option);
adjustAnnounceUri(&attrs, option);
CPPUNIT_ASSERT_EQUAL((size_t)3, attrs->announceList.size());
CPPUNIT_ASSERT_EQUAL((size_t)3, attrs.announceList.size());
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[0].size());
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs.announceList[0].size());
CPPUNIT_ASSERT_EQUAL(std::string("http://host3/announce"),
attrs->announceList[0][0]);
attrs.announceList[0][0]);
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[1].size());
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs.announceList[1].size());
CPPUNIT_ASSERT_EQUAL(std::string("http://host1/announce"),
attrs->announceList[1][0]);
attrs.announceList[1][0]);
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs->announceList[2].size());
CPPUNIT_ASSERT_EQUAL((size_t)1, attrs.announceList[2].size());
CPPUNIT_ASSERT_EQUAL(std::string("http://host4/announce"),
attrs->announceList[2][0]);
attrs.announceList[2][0]);
}
} // namespace bittorrent

View File

@ -192,9 +192,8 @@ void BtDependencyTest::testResolve_metadata()
pieceStorage->setDiskAdaptor(diskAdaptor);
pieceStorage->setDownloadFinished(true);
dependee->setPieceStorage(pieceStorage);
std::shared_ptr<TorrentAttribute> attrs(new TorrentAttribute());
dependee->getDownloadContext()->setAttribute(CTX_ATTR_BT, attrs);
dependee->getDownloadContext()->setAttribute(CTX_ATTR_BT,
make_unique<TorrentAttribute>());
BtDependency dep(dependant.get(), dependee);
CPPUNIT_ASSERT(dep.resolve());

View File

@ -66,13 +66,16 @@ void BtRegistryTest::testGetDownloadContext_infoHash()
{
BtRegistry btRegistry;
addTwoDownloadContext(btRegistry);
std::shared_ptr<TorrentAttribute> attrs1(new TorrentAttribute());
{
auto attrs1 = make_unique<TorrentAttribute>();
attrs1->infoHash = "hash1";
std::shared_ptr<TorrentAttribute> attrs2(new TorrentAttribute());
auto attrs2 = make_unique<TorrentAttribute>();
attrs2->infoHash = "hash2";
btRegistry.getDownloadContext(1)->setAttribute(CTX_ATTR_BT, attrs1);
btRegistry.getDownloadContext(2)->setAttribute(CTX_ATTR_BT, attrs2);
btRegistry.getDownloadContext(1)->setAttribute(CTX_ATTR_BT,
std::move(attrs1));
btRegistry.getDownloadContext(2)->setAttribute(CTX_ATTR_BT,
std::move(attrs2));
}
CPPUNIT_ASSERT(btRegistry.getDownloadContext("hash1"));
CPPUNIT_ASSERT(btRegistry.getDownloadContext("hash1").get() ==
btRegistry.getDownloadContext(1).get());

View File

@ -61,9 +61,11 @@ public:
std::string peerId = "-aria2-ultrafastdltl";
dctx_.reset(new DownloadContext(pieceLength, totalLength));
std::shared_ptr<TorrentAttribute> torrentAttrs(new TorrentAttribute());
{
auto torrentAttrs = make_unique<TorrentAttribute>();
torrentAttrs->infoHash = std::string(vbegin(infoHash), vend(infoHash));
dctx_->setAttribute(CTX_ATTR_BT, torrentAttrs);
dctx_->setAttribute(CTX_ATTR_BT, std::move(torrentAttrs));
}
dctx_->getNetStat().updateDownloadLength(pieceLength*5);
dctx_->getNetStat().updateUploadLength(pieceLength*6);
bittorrent::setStaticPeerId(peerId);

View File

@ -70,9 +70,11 @@ public:
};
dctx_.reset(new DownloadContext());
std::shared_ptr<TorrentAttribute> torrentAttrs(new TorrentAttribute());
{
auto torrentAttrs = make_unique<TorrentAttribute>();
torrentAttrs->infoHash = std::string(vbegin(infoHash), vend(infoHash));
dctx_->setAttribute(CTX_ATTR_BT, torrentAttrs);
dctx_->setAttribute(CTX_ATTR_BT, std::move(torrentAttrs));
}
const std::shared_ptr<FileEntry> fileEntries[] = {
std::shared_ptr<FileEntry>(new FileEntry("/path/to/file",totalLength,0))
};

View File

@ -336,8 +336,7 @@ void DownloadHelperTest::testCreateRequestGroupForBitTorrent()
CPPUNIT_ASSERT_EQUAL(array[i]+"/aria2-test/aria2/src/aria2c", uris[i]);
}
CPPUNIT_ASSERT_EQUAL(5, group->getNumConcurrentCommand());
std::shared_ptr<TorrentAttribute> attrs =
bittorrent::getTorrentAttrs(group->getDownloadContext());
auto attrs = bittorrent::getTorrentAttrs(group->getDownloadContext());
// http://tracker1 was deleted.
CPPUNIT_ASSERT_EQUAL((size_t)2, attrs->announceList.size());
}

View File

@ -97,8 +97,7 @@ void HandshakeExtensionMessageTest::testDoReceivedAction()
RequestGroup rg(GroupId::create(), op);
rg.setDownloadContext(dctx);
std::shared_ptr<TorrentAttribute> attrs(new TorrentAttribute());
dctx->setAttribute(CTX_ATTR_BT, attrs);
dctx->setAttribute(CTX_ATTR_BT, make_unique<TorrentAttribute>());
dctx->markTotalLengthIsUnknown();
std::shared_ptr<Peer> peer(new Peer("192.168.0.1", 0));
@ -122,6 +121,7 @@ void HandshakeExtensionMessageTest::testDoReceivedAction()
peer->getExtensionMessageID
(ExtensionMessageRegistry::UT_METADATA));
CPPUNIT_ASSERT(peer->isSeeder());
auto attrs = bittorrent::getTorrentAttrs(dctx);
CPPUNIT_ASSERT_EQUAL((size_t)1024, attrs->metadataSize);
CPPUNIT_ASSERT_EQUAL((int64_t)1024, dctx->getTotalLength());
CPPUNIT_ASSERT(dctx->knowsTotalLength());

View File

@ -33,9 +33,11 @@ public:
dctx_.reset(new DownloadContext());
unsigned char infoHash[20];
memset(infoHash, 0, sizeof(infoHash));
std::shared_ptr<TorrentAttribute> torrentAttrs(new TorrentAttribute());
{
auto torrentAttrs = make_unique<TorrentAttribute>();
torrentAttrs->infoHash = std::string(vbegin(infoHash), vend(infoHash));
dctx_->setAttribute(CTX_ATTR_BT, torrentAttrs);
dctx_->setAttribute(CTX_ATTR_BT, std::move(torrentAttrs));
}
}
void testHandshake();

View File

@ -949,7 +949,7 @@ void RpcMethodTest::testGatherBitTorrentMetadata()
CPPUNIT_ASSERT_EQUAL(std::string("http://tracker3"),
downcast<String>(downcast<List>(announceList->get(2))->get(0))->s());
// Remove some keys
std::shared_ptr<TorrentAttribute> modBtAttrs = bittorrent::getTorrentAttrs(dctx);
auto modBtAttrs = bittorrent::getTorrentAttrs(dctx);
modBtAttrs->comment.clear();
modBtAttrs->creationDate = 0;
modBtAttrs->mode = BT_FILE_MODE_NONE;

View File

@ -73,7 +73,6 @@ void UTMetadataDataExtensionMessageTest::testDoReceivedAction()
std::shared_ptr<UTMetadataRequestTracker> tracker
(new UTMetadataRequestTracker());
std::shared_ptr<DownloadContext> dctx(new DownloadContext());
std::shared_ptr<TorrentAttribute> attrs(new TorrentAttribute());
std::string piece0 = std::string(METADATA_PIECE_SIZE, '0');
std::string piece1 = std::string(METADATA_PIECE_SIZE, '1');
@ -83,9 +82,11 @@ void UTMetadataDataExtensionMessageTest::testDoReceivedAction()
message_digest::digest(infoHash, INFO_HASH_LENGTH,
MessageDigest::sha1(),
metadata.data(), metadata.size());
{
auto attrs = make_unique<TorrentAttribute>();
attrs->infoHash = std::string(&infoHash[0], &infoHash[20]);
dctx->setAttribute(CTX_ATTR_BT, attrs);
dctx->setAttribute(CTX_ATTR_BT, std::move(attrs));
}
UTMetadataDataExtensionMessage m(1);
m.setPieceStorage(pieceStorage);
m.setUTMetadataRequestTracker(tracker.get());

View File

@ -52,11 +52,11 @@ void UTMetadataPostDownloadHandlerTest::testCanHandle()
CPPUNIT_ASSERT(!handler.canHandle(requestGroup_.get()));
std::shared_ptr<TorrentAttribute> attrs(new TorrentAttribute());
dctx_->setAttribute(CTX_ATTR_BT, attrs);
dctx_->setAttribute(CTX_ATTR_BT, make_unique<TorrentAttribute>());
CPPUNIT_ASSERT(handler.canHandle(requestGroup_.get()));
auto attrs = bittorrent::getTorrentAttrs(dctx_);
// Only checks whether metadata is empty or not
attrs->metadata = "metadata";
@ -77,14 +77,16 @@ void UTMetadataPostDownloadHandlerTest::testGetNextRequestGroups()
(infoHash, sizeof(infoHash), MessageDigest::sha1(),
reinterpret_cast<const unsigned char*>(metadata.data()), metadata.size());
dctx_->getFirstFileEntry()->setLength(metadata.size());
std::shared_ptr<TorrentAttribute> attrs(new TorrentAttribute());
attrs->infoHash = std::string(&infoHash[0], &infoHash[20]);
std::vector<std::vector<std::string> > announceList;
std::vector<std::string> announceTier;
announceTier.push_back("http://tracker");
announceList.push_back(announceTier);
{
auto attrs = make_unique<TorrentAttribute>();
attrs->infoHash = std::string(&infoHash[0], &infoHash[20]);
attrs->announceList = announceList;
dctx_->setAttribute(CTX_ATTR_BT, attrs);
dctx_->setAttribute(CTX_ATTR_BT, std::move(attrs));
}
requestGroup_->setDiskWriterFactory
(std::shared_ptr<DiskWriterFactory>(new ByteArrayDiskWriterFactory()));
requestGroup_->initPieceStorage();
@ -99,8 +101,7 @@ void UTMetadataPostDownloadHandlerTest::testGetNextRequestGroups()
CPPUNIT_ASSERT_EQUAL((size_t)1, results.size());
std::shared_ptr<RequestGroup> newRg = results.front();
std::shared_ptr<DownloadContext> newDctx = newRg->getDownloadContext();
std::shared_ptr<TorrentAttribute> newAttrs =
bittorrent::getTorrentAttrs(newDctx);
auto newAttrs = bittorrent::getTorrentAttrs(newDctx);
CPPUNIT_ASSERT_EQUAL(bittorrent::getInfoHashString(dctx_),
bittorrent::getInfoHashString(newDctx));
const std::vector<std::vector<std::string> >& newAnnounceList =

View File

@ -42,7 +42,7 @@ public:
dispatcher_.reset(new MockBtMessageDispatcher());
dctx_.reset(new DownloadContext());
std::shared_ptr<TorrentAttribute> attrs(new TorrentAttribute());
dctx_->setAttribute(CTX_ATTR_BT, attrs);
dctx_->setAttribute(CTX_ATTR_BT, make_unique<TorrentAttribute>());
peer_.reset(new Peer("host", 6880));
peer_->allocateSessionResource(0, 0);
peer_->setExtension(ExtensionMessageRegistry::UT_METADATA, 1);
@ -125,7 +125,7 @@ void UTMetadataRequestExtensionMessageTest::testDoReceivedAction_data()
msg.setBtMessageDispatcher(dispatcher_.get());
size_t metadataSize = METADATA_PIECE_SIZE*2;
std::shared_ptr<TorrentAttribute> attrs = bittorrent::getTorrentAttrs(dctx_);
auto attrs = bittorrent::getTorrentAttrs(dctx_);
std::string first(METADATA_PIECE_SIZE, '0');
std::string second(METADATA_PIECE_SIZE, '1');
attrs->metadata = first+second;