From 09ce8960fd3c3b8f61ecfe2616513fd1eebc74f7 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 23 Aug 2011 18:47:27 +0900 Subject: [PATCH] Added completedLength response key in aria2.getFiles RPC method. --- doc/aria2c.1.asciidoc | 16 ++++++++++++ src/RpcMethodImpl.cc | 60 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/doc/aria2c.1.asciidoc b/doc/aria2c.1.asciidoc index 1fcd7248..c72618e9 100644 --- a/doc/aria2c.1.asciidoc +++ b/doc/aria2c.1.asciidoc @@ -2157,6 +2157,7 @@ The following example gets information about download whose GID is u'downloadSpeed': u'15158', u'files': [{u'index': u'1', u'length': u'34896138', + u'completedLength': u'34896138', u'path': u'/downloads/file', u'selected': u'true', u'uris': [{u'status': u'used', @@ -2208,6 +2209,7 @@ The following example gets information about download whose GID is 'errorCode': '0', 'files': [{'index': '1', 'length': '34896138', + 'completedLength': '34896138', 'path': '/downloads/file', 'selected': 'true', 'uris': [{'status': 'used', @@ -2302,6 +2304,18 @@ length:: File size in bytes. +completedLength:: + + Completed length of this file in bytes. Please note that it is + possible that sum of completedLength is less than completedLength in + *<>* method. + This is because completedLength in + *<>* + only calculates completed pieces. On the other hand, completedLength + in + *<>* takes into account + of partially completed piece. + selected:: "true" if this file is selected by *<>* option. If @@ -2327,6 +2341,7 @@ JSON-RPC Example u'jsonrpc': u'2.0', u'result': [{u'index': u'1', u'length': u'34896138', + u'completedLength': u'34896138', u'path': u'/downloads/file', u'selected': u'true', u'uris': [{u'status': u'used', @@ -2344,6 +2359,7 @@ XML-RPC Example >>> pprint(r) [{'index': '1', 'length': '34896138', + 'completedLength': '34896138', 'path': '/downloads/file', 'selected': 'true', 'uris': [{'status': 'used', diff --git a/src/RpcMethodImpl.cc b/src/RpcMethodImpl.cc index 4f14561f..7834da16 100644 --- a/src/RpcMethodImpl.cc +++ b/src/RpcMethodImpl.cc @@ -64,6 +64,7 @@ #include "TimedHaltCommand.h" #include "PeerStat.h" #include "Base64.h" +#include "BitfieldMan.h" #ifdef ENABLE_MESSAGE_DIGEST # include "MessageDigest.h" # include "message_digest_helper.h" @@ -579,7 +580,9 @@ void createUriEntry namespace { template void createFileEntry -(const SharedHandle& files, InputIterator first, InputIterator last) +(const SharedHandle& files, + InputIterator first, InputIterator last, + const BitfieldMan* bf) { size_t index = 1; for(; first != last; ++first, ++index) { @@ -588,6 +591,9 @@ void createFileEntry entry->put(KEY_PATH, (*first)->getPath()); entry->put(KEY_SELECTED, (*first)->isRequested()?VLB_TRUE:VLB_FALSE); entry->put(KEY_LENGTH, util::uitos((*first)->getLength())); + uint64_t completedLength = bf->getOffsetCompletedLength + ((*first)->getOffset(), (*first)->getLength()); + entry->put(KEY_COMPLETED_LENGTH, util::uitos(completedLength)); SharedHandle uriList = List::g(); createUriEntry(uriList, *first); @@ -597,6 +603,40 @@ void createFileEntry } } // namespace +namespace { +template +void createFileEntry +(const SharedHandle& files, + InputIterator first, InputIterator last, + uint64_t totalLength, + size_t pieceLength, + const std::string& bitfieldStr) +{ + std::string bitfield = util::fromHex(bitfieldStr); + BitfieldMan bf(pieceLength, totalLength); + bf.setBitfield(reinterpret_cast(bitfield.data()), + bitfield.size()); + createFileEntry(files, first, last, &bf); +} +} // namespace + +namespace { +template +void createFileEntry +(const SharedHandle& files, + InputIterator first, InputIterator last, + uint64_t totalLength, + size_t pieceLength, + const SharedHandle& ps) +{ + BitfieldMan bf(pieceLength, totalLength); + if(ps) { + bf.setBitfield(ps->getBitfield(), ps->getBitfieldLength()); + } + createFileEntry(files, first, last, &bf); +} +} // namespace + namespace { bool requested_key (const std::vector& keys, const std::string& k) @@ -610,6 +650,7 @@ void gatherProgressCommon const SharedHandle& group, const std::vector& keys) { + const SharedHandle& ps = group->getPieceStorage(); if(requested_key(keys, KEY_GID)) { entryDict->put(KEY_GID, util::itos(group->getGID())); } @@ -637,7 +678,6 @@ void gatherProgressCommon entryDict->put(KEY_CONNECTIONS, util::uitos(group->getNumConnection())); } if(requested_key(keys, KEY_BITFIELD)) { - SharedHandle ps = group->getPieceStorage(); if(ps) { if(ps->getBitfieldLength() > 0) { entryDict->put(KEY_BITFIELD, @@ -671,7 +711,8 @@ void gatherProgressCommon if(requested_key(keys, KEY_FILES)) { SharedHandle files = List::g(); createFileEntry - (files, dctx->getFileEntries().begin(), dctx->getFileEntries().end()); + (files, dctx->getFileEntries().begin(), dctx->getFileEntries().end(), + dctx->getTotalLength(), dctx->getPieceLength(), ps); entryDict->put(KEY_FILES, files); } if(requested_key(keys, KEY_DIR)) { @@ -829,7 +870,8 @@ void gatherStoppedDownload } if(requested_key(keys, KEY_FILES)) { SharedHandle files = List::g(); - createFileEntry(files, ds->fileEntries.begin(), ds->fileEntries.end()); + createFileEntry(files, ds->fileEntries.begin(), ds->fileEntries.end(), + ds->totalLength, ds->pieceLength, ds->bitfieldStr); entryDict->put(KEY_FILES, files); } if(requested_key(keys, KEY_TOTAL_LENGTH)) { @@ -889,12 +931,18 @@ SharedHandle GetFilesRpcMethod::process (fmt("No file data is available for GID#%s", util::itos(gid).c_str())); } else { - createFileEntry(files, dr->fileEntries.begin(), dr->fileEntries.end()); + createFileEntry(files, dr->fileEntries.begin(), dr->fileEntries.end(), + dr->totalLength, dr->pieceLength, dr->bitfieldStr); } } else { + const SharedHandle& ps = group->getPieceStorage(); + const SharedHandle& dctx = group->getDownloadContext(); createFileEntry(files, group->getDownloadContext()->getFileEntries().begin(), - group->getDownloadContext()->getFileEntries().end()); + group->getDownloadContext()->getFileEntries().end(), + dctx->getTotalLength(), + dctx->getPieceLength(), + ps); } return files; }