summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2016-04-17 21:08:24 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2016-04-17 21:08:24 +0900
commitc9e2223741f9627f27b3d2befef48acdd79d504d (patch)
tree9450a2236f29dab85adb96a04dcdf6723175cee2
parent79298daf5feff76e2f7739e37c9f853d722afc94 (diff)
parentca634a82bde8764681a0abbedb38a23222dde608 (diff)
Merge branch 'add-btkey-to-completed'
-rw-r--r--src/DownloadContext.cc19
-rw-r--r--src/DownloadContext.h8
-rw-r--r--src/DownloadResult.h7
-rw-r--r--src/RequestGroup.cc1
-rw-r--r--src/RpcMethodImpl.cc12
-rw-r--r--src/TorrentAttribute.h4
-rw-r--r--src/ValueBase.cc2
-rw-r--r--src/bittorrent_helper.cc2
-rw-r--r--test/RpcMethodTest.cc29
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<int> sgl)
}
void DownloadContext::setAttribute(ContextAttributeType key,
- std::unique_ptr<ContextAttribute> value)
+ std::shared_ptr<ContextAttribute> value)
{
assert(key < MAX_CTX_ATTR);
attrs_[key] = std::move(value);
}
-const std::unique_ptr<ContextAttribute>&
+const std::shared_ptr<ContextAttribute>&
DownloadContext::getAttribute(ContextAttributeType key)
{
assert(key < MAX_CTX_ATTR);
- const std::unique_ptr<ContextAttribute>& 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<std::shared_ptr<ContextAttribute>>&
+DownloadContext::getAttributes() const
+{
+ return attrs_;
+}
+
void DownloadContext::releaseRuntimeResource()
{
for (std::vector<std::shared_ptr<FileEntry>>::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<std::unique_ptr<ContextAttribute>> attrs_;
+ std::vector<std::shared_ptr<ContextAttribute>> attrs_;
std::vector<std::shared_ptr<FileEntry>> fileEntries_;
@@ -197,13 +197,15 @@ public:
void setChecksumVerified(bool f) { checksumVerified_ = f; }
void setAttribute(ContextAttributeType key,
- std::unique_ptr<ContextAttribute> value);
+ std::shared_ptr<ContextAttribute> value);
- const std::unique_ptr<ContextAttribute>&
+ const std::shared_ptr<ContextAttribute>&
getAttribute(ContextAttributeType key);
bool hasAttribute(ContextAttributeType key) const;
+ const std::vector<std::shared_ptr<ContextAttribute>>& 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> metadataInfo;
+ std::vector<std::shared_ptr<ContextAttribute>> attrs;
+
std::vector<std::shared_ptr<FileEntry>> 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<DownloadResult> RequestGroup::createDownloadResult() const
TransferStat st = calculateStat();
auto res = std::make_shared<DownloadResult>();
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<TorrentAttribute*>(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<ValueBase> 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<ValueBase> 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<DownloadContext>& ctx,
throw DL_ABORT_EX2(fmt(MSG_MISSING_BT_INFO, C_INFO),
error_code::BITTORRENT_PARSE_ERROR);
}
- auto torrent = make_unique<TorrentAttribute>();
+ auto torrent = std::make_shared<TorrentAttribute>();
// 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<DownloadResult>();
+ d->gid = GroupId::create();
+ d->infoHash = "2089b05ecca3d829cee5497d2703803b52216d19";
+ d->attrs = std::vector<std::shared_ptr<ContextAttribute>>(MAX_CTX_ATTR);
+
+ auto torrentAttr = std::make_shared<TorrentAttribute>();
+ torrentAttr->creationDate = 1000000007;
+ d->attrs[CTX_ATTR_BT] = torrentAttr;
+
+ auto entry = Dict::g();
+ gatherStoppedDownload(entry.get(), d, {});
+
+ auto btDict = downcast<Dict>(entry->get("bittorrent"));
+ CPPUNIT_ASSERT(btDict);
+
+ CPPUNIT_ASSERT_EQUAL((int64_t)1000000007,
+ downcast<Integer>(btDict->get("creationDate"))->i());
+}
+#endif // ENABLE_BITTORRENT
+
void RpcMethodTest::testGatherProgressCommon()
{
auto dctx = std::make_shared<DownloadContext>(0, 0, "aria2.tar.bz2");