From ca634a82bde8764681a0abbedb38a23222dde608 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 17 Apr 2016 21:06:41 +0900 Subject: [PATCH] Add bittorrent key to aria2.tellStopped status --- src/DownloadContext.cc | 19 ++++++++++++------- src/DownloadContext.h | 8 +++++--- src/DownloadResult.h | 7 +++++-- src/RequestGroup.cc | 1 + src/RpcMethodImpl.cc | 12 ++++++++++++ src/TorrentAttribute.h | 4 ++-- src/ValueBase.cc | 2 +- src/bittorrent_helper.cc | 2 +- test/RpcMethodTest.cc | 29 +++++++++++++++++++++++++++++ 9 files changed, 68 insertions(+), 16 deletions(-) diff --git a/src/DownloadContext.cc b/src/DownloadContext.cc index 1f6eb26f..b7d21ed3 100644 --- a/src/DownloadContext.cc +++ b/src/DownloadContext.cc @@ -153,24 +153,23 @@ void DownloadContext::setFileFilter(SegList sgl) } void DownloadContext::setAttribute(ContextAttributeType key, - std::unique_ptr value) + std::shared_ptr value) { assert(key < MAX_CTX_ATTR); attrs_[key] = std::move(value); } -const std::unique_ptr& +const std::shared_ptr& DownloadContext::getAttribute(ContextAttributeType key) { assert(key < MAX_CTX_ATTR); - const std::unique_ptr& attr = attrs_[key]; - if (attr) { - return attr; - } - else { + const auto& attr = attrs_[key]; + if (!attr) { throw DL_ABORT_EX( fmt("No attribute named %s", strContextAttributeType(key))); } + + return attr; } bool DownloadContext::hasAttribute(ContextAttributeType key) const @@ -179,6 +178,12 @@ bool DownloadContext::hasAttribute(ContextAttributeType key) const return attrs_[key].get(); } +const std::vector>& +DownloadContext::getAttributes() const +{ + return attrs_; +} + void DownloadContext::releaseRuntimeResource() { for (std::vector>::const_iterator diff --git a/src/DownloadContext.h b/src/DownloadContext.h index 66b5c1f5..04d55ffe 100644 --- a/src/DownloadContext.h +++ b/src/DownloadContext.h @@ -61,7 +61,7 @@ private: RequestGroup* ownerRequestGroup_; - std::vector> attrs_; + std::vector> attrs_; std::vector> fileEntries_; @@ -197,13 +197,15 @@ public: void setChecksumVerified(bool f) { checksumVerified_ = f; } void setAttribute(ContextAttributeType key, - std::unique_ptr value); + std::shared_ptr value); - const std::unique_ptr& + const std::shared_ptr& getAttribute(ContextAttributeType key); bool hasAttribute(ContextAttributeType key) const; + const std::vector>& getAttributes() const; + void resetDownloadStartTime(); void resetDownloadStopTime(); diff --git a/src/DownloadResult.h b/src/DownloadResult.h index 7cdc72bb..836cc672 100644 --- a/src/DownloadResult.h +++ b/src/DownloadResult.h @@ -45,6 +45,7 @@ #include "error_code.h" #include "RequestGroup.h" +#include "ContextAttribute.h" namespace aria2 { @@ -73,6 +74,8 @@ struct DownloadResult { std::shared_ptr metadataInfo; + std::vector> attrs; + std::vector> fileEntries; // This field contains GIDs. See comment in @@ -102,8 +105,8 @@ struct DownloadResult { ~DownloadResult(); // Don't allow copying - DownloadResult(const DownloadResult& c); - DownloadResult& operator=(const DownloadResult& c); + DownloadResult(const DownloadResult& c) = delete; + DownloadResult& operator=(const DownloadResult& c) = delete; }; } // namespace aria2 diff --git a/src/RequestGroup.cc b/src/RequestGroup.cc index eb188c9e..cf436650 100644 --- a/src/RequestGroup.cc +++ b/src/RequestGroup.cc @@ -1110,6 +1110,7 @@ std::shared_ptr RequestGroup::createDownloadResult() const TransferStat st = calculateStat(); auto res = std::make_shared(); res->gid = gid_; + res->attrs = downloadContext_->getAttributes(); res->fileEntries = downloadContext_->getFileEntries(); res->inMemoryDownload = inMemoryDownload_; res->sessionDownloadLength = st.sessionDownloadLength; diff --git a/src/RpcMethodImpl.cc b/src/RpcMethodImpl.cc index d808d83c..058dda82 100644 --- a/src/RpcMethodImpl.cc +++ b/src/RpcMethodImpl.cc @@ -907,6 +907,18 @@ void gatherStoppedDownload(Dict* entryDict, if (requested_key(keys, KEY_DIR)) { entryDict->put(KEY_DIR, ds->dir); } + +#ifdef ENABLE_BITTORRENT + if (ds->attrs.size() > CTX_ATTR_BT && ds->attrs[CTX_ATTR_BT]) { + const auto attrs = + static_cast(ds->attrs[CTX_ATTR_BT].get()); + if (requested_key(keys, KEY_BITTORRENT)) { + auto btDict = Dict::g(); + gatherBitTorrentMetadata(btDict.get(), attrs); + entryDict->put(KEY_BITTORRENT, std::move(btDict)); + } + } +#endif // ENABLE_BITTORRENT } std::unique_ptr GetFilesRpcMethod::process(const RpcRequest& req, diff --git a/src/TorrentAttribute.h b/src/TorrentAttribute.h index 037b04c5..27bd04ba 100644 --- a/src/TorrentAttribute.h +++ b/src/TorrentAttribute.h @@ -64,8 +64,8 @@ struct TorrentAttribute : public ContextAttribute { ~TorrentAttribute(); // Don't allow copying - TorrentAttribute(const TorrentAttribute&); - TorrentAttribute& operator=(const TorrentAttribute&); + TorrentAttribute(const TorrentAttribute&) = delete; + TorrentAttribute& operator=(const TorrentAttribute&) = delete; }; } // namespace aria2 diff --git a/src/ValueBase.cc b/src/ValueBase.cc index f1f6ede0..5c1edd35 100644 --- a/src/ValueBase.cc +++ b/src/ValueBase.cc @@ -165,7 +165,7 @@ void Dict::put(std::string key, std::unique_ptr vlb) auto p = std::make_pair(std::move(key), std::move(vlb)); auto r = dict_.insert(std::move(p)); if (!r.second) { - (*r.first).second = std::move(vlb); + (*r.first).second = std::move(p.second); } } diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc index ba0895ae..15ea3498 100644 --- a/src/bittorrent_helper.cc +++ b/src/bittorrent_helper.cc @@ -424,7 +424,7 @@ void processRootDictionary(const std::shared_ptr& ctx, throw DL_ABORT_EX2(fmt(MSG_MISSING_BT_INFO, C_INFO), error_code::BITTORRENT_PARSE_ERROR); } - auto torrent = make_unique(); + auto torrent = std::make_shared(); // retrieve infoHash std::string encodedInfoDict = bencode2::encode(infoDict); diff --git a/test/RpcMethodTest.cc b/test/RpcMethodTest.cc index f89afd9b..501d9c7e 100644 --- a/test/RpcMethodTest.cc +++ b/test/RpcMethodTest.cc @@ -67,6 +67,9 @@ class RpcMethodTest : public CppUnit::TestFixture { CPPUNIT_TEST(testGetVersion); CPPUNIT_TEST(testNoSuchMethod); CPPUNIT_TEST(testGatherStoppedDownload); +#ifdef ENABLE_BITTORRENT + CPPUNIT_TEST(testGatherStoppedDownload_bt); +#endif // ENABLE_BITTORRENT CPPUNIT_TEST(testGatherProgressCommon); #ifdef ENABLE_BITTORRENT CPPUNIT_TEST(testGatherBitTorrentMetadata); @@ -134,6 +137,9 @@ public: void testGetVersion(); void testNoSuchMethod(); void testGatherStoppedDownload(); +#ifdef ENABLE_BITTORRENT + void testGatherStoppedDownload_bt(); +#endif // ENABLE_BITTORRENT void testGatherProgressCommon(); #ifdef ENABLE_BITTORRENT void testGatherBitTorrentMetadata(); @@ -950,6 +956,29 @@ void RpcMethodTest::testGatherStoppedDownload() CPPUNIT_ASSERT(entry->containsKey("gid")); } +#ifdef ENABLE_BITTORRENT +void RpcMethodTest::testGatherStoppedDownload_bt() +{ + auto d = std::make_shared(); + d->gid = GroupId::create(); + d->infoHash = "2089b05ecca3d829cee5497d2703803b52216d19"; + d->attrs = std::vector>(MAX_CTX_ATTR); + + auto torrentAttr = std::make_shared(); + torrentAttr->creationDate = 1000000007; + d->attrs[CTX_ATTR_BT] = torrentAttr; + + auto entry = Dict::g(); + gatherStoppedDownload(entry.get(), d, {}); + + auto btDict = downcast(entry->get("bittorrent")); + CPPUNIT_ASSERT(btDict); + + CPPUNIT_ASSERT_EQUAL((int64_t)1000000007, + downcast(btDict->get("creationDate"))->i()); +} +#endif // ENABLE_BITTORRENT + void RpcMethodTest::testGatherProgressCommon() { auto dctx = std::make_shared(0, 0, "aria2.tar.bz2");