Add DownloadHandle::getNumFiles and getFile API

pull/89/head
Tatsuhiro Tsujikawa 2013-05-09 00:48:41 +09:00
parent 1f38699d32
commit b733431ac4
2 changed files with 50 additions and 9 deletions

View File

@ -391,6 +391,22 @@ void createUriEntry
}
} // namespace
namespace {
FileData createFileData
(const SharedHandle<FileEntry>& fe, int index, const BitfieldMan* bf)
{
FileData file;
file.index = index;
file.path = fe->getPath();
file.length = fe->getLength();
file.completedLength = bf->getOffsetCompletedLength
(fe->getOffset(), fe->getLength());
file.selected = fe->isRequested();
createUriEntry(std::back_inserter(file.uris), fe);
return file;
}
} // namespace
namespace {
template<typename OutputIterator, typename InputIterator>
void createFileEntry
@ -400,15 +416,7 @@ void createFileEntry
{
size_t index = 1;
for(; first != last; ++first) {
FileData file;
file.index = index++;
file.path = (*first)->getPath();
file.length = (*first)->getLength();
file.completedLength = bf->getOffsetCompletedLength
((*first)->getOffset(), (*first)->getLength());
file.selected = (*first)->isRequested();
createUriEntry(std::back_inserter(file.uris), *first);
out++ = file;
out++ = createFileData(*first, index++, bf);
}
}
} // namespace
@ -530,6 +538,21 @@ struct RequestGroupDH : public DownloadHandle {
group->getPieceStorage());
return res;
}
virtual int getNumFiles()
{
const SharedHandle<DownloadContext>& dctx = group->getDownloadContext();
return dctx->getFileEntries().size();
}
virtual FileData getFile(int index)
{
const SharedHandle<DownloadContext>& dctx = group->getDownloadContext();
BitfieldMan bf(dctx->getPieceLength(), dctx->getTotalLength());
const SharedHandle<PieceStorage>& ps = group->getPieceStorage();
if(ps) {
bf.setBitfield(ps->getBitfield(), ps->getBitfieldLength());
}
return createFileData(dctx->getFileEntries()[index-1], index, &bf);
}
SharedHandle<RequestGroup> group;
TransferStat ts;
};
@ -608,6 +631,17 @@ struct DownloadResultDH : public DownloadHandle {
dr->totalLength, dr->pieceLength, dr->bitfield);
return res;
}
virtual int getNumFiles()
{
return dr->fileEntries.size();
}
virtual FileData getFile(int index)
{
BitfieldMan bf(dr->pieceLength, dr->totalLength);
bf.setBitfield(reinterpret_cast<const unsigned char*>(dr->bitfield.data()),
dr->bitfield.size());
return createFileData(dr->fileEntries[index-1], index, &bf);
}
SharedHandle<DownloadResult> dr;
};
} // namespace

View File

@ -217,6 +217,7 @@ struct UriData {
};
struct FileData {
// 1-based index
int index;
std::string path;
int64_t length;
@ -265,6 +266,12 @@ struct DownloadHandle {
virtual A2Gid getBelongsTo() = 0;
virtual const std::string& getDir() = 0;
virtual std::vector<FileData> getFiles() = 0;
// Returns the number of files. The return value is equivalent to
// getFiles().size().
virtual int getNumFiles() = 0;
// Returns the FileData of the file at the specified |index|. Please
// note that the index is 1-based.
virtual FileData getFile(int index) = 0;
};
// Returns handle for the download denoted by the |gid|. The caller