From 048a2cf59737f761876e81a77e8d064c45c752d7 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 11 Oct 2007 16:58:24 +0000 Subject: 2007-10-12 Tatsuhiro Tsujikawa Implemented BitTorrent/http/ftp integrated download. I've rewritten lots of files and now some headers have forward class declarations to reduce compile time. The implementation is extremely alpha stage, I recommend to use this for testing purpose only. --- src/MultiFileAllocationIterator.cc | 101 +++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/MultiFileAllocationIterator.cc (limited to 'src/MultiFileAllocationIterator.cc') diff --git a/src/MultiFileAllocationIterator.cc b/src/MultiFileAllocationIterator.cc new file mode 100644 index 00000000..fb2f7fe4 --- /dev/null +++ b/src/MultiFileAllocationIterator.cc @@ -0,0 +1,101 @@ +/* */ +#include "MultiFileAllocationIterator.h" +#include "MultiDiskAdaptor.h" +#include "FileEntry.h" + +#define BUFSIZE 16*1024 + +MultiFileAllocationIterator::MultiFileAllocationIterator(MultiDiskAdaptor* diskAdaptor): + _diskAdaptor(diskAdaptor), + _entries(diskAdaptor->getFileEntries()), + _currentEntry(0), + _offset(0) +{} + +MultiFileAllocationIterator::~MultiFileAllocationIterator() {} + +void MultiFileAllocationIterator::prepareNextEntry() +{ + _currentEntry = 0; + _offset = 0; + while(!_entries.empty()) { + FileEntryHandle entry = _entries.front(); + _entries.pop_front(); + if(entry->isRequested()) { + _currentEntry = entry; + _offset = File(_diskAdaptor->getStoreDir()+"/"+ + _diskAdaptor->getTopDir()+"/"+ + _currentEntry->getPath()).size(); + break; + } + } +} + + +void MultiFileAllocationIterator::allocateChunk() +{ + while(_currentEntry.isNull() || _currentEntry->getLength() <= _offset) { + prepareNextEntry(); + if(_currentEntry.isNull()) { + break; + } + } + if(finished()) { + return; + } + int32_t bufSize = BUFSIZE; + unsigned char buf[BUFSIZE]; + memset(buf, 0, bufSize); + + int32_t wsize = _offset+bufSize > _currentEntry->getLength() ? + _currentEntry->getLength()-_offset:bufSize; + _diskAdaptor->writeData(buf, wsize, _offset+_currentEntry->getOffset()); + _offset += wsize; +} + +bool MultiFileAllocationIterator::finished() +{ + return _entries.empty() && _currentEntry.isNull(); +} + +int64_t MultiFileAllocationIterator::getTotalLength() +{ + if(_currentEntry.isNull()) { + return 0; + } else { + return _currentEntry->getLength(); + } +} -- cgit