2006-03-21 14:12:51 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-03-21 14:12:51 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-09-21 15:31:24 +00:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
2006-03-21 14:12:51 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "Piece.h"
|
2013-06-21 16:10:38 +00:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "BitfieldMan.h"
|
2008-06-04 16:28:16 +00:00
|
|
|
#include "A2STR.h"
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2009-06-06 12:33:07 +00:00
|
|
|
#include "a2functional.h"
|
2012-11-27 13:04:59 +00:00
|
|
|
#include "WrDiskCache.h"
|
|
|
|
#include "WrDiskCacheEntry.h"
|
|
|
|
#include "LogFactory.h"
|
|
|
|
#include "fmt.h"
|
|
|
|
#include "DiskAdaptor.h"
|
2008-06-04 16:28:16 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2010-11-11 02:56:24 +00:00
|
|
|
# include "MessageDigest.h"
|
2008-06-04 16:28:16 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2013-08-20 16:57:17 +00:00
|
|
|
Piece::Piece():index_(0), length_(0), blockLength_(BLOCK_LENGTH), bitfield_(nullptr),
|
|
|
|
usedBySegment_(false), wrCache_(nullptr)
|
2008-06-04 16:28:16 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2010-06-21 13:51:56 +00:00
|
|
|
, nextBegin_(0)
|
2008-06-04 16:28:16 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
|
|
|
{}
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2011-12-07 15:03:25 +00:00
|
|
|
Piece::Piece(size_t index, int32_t length, int32_t blockLength)
|
|
|
|
: index_(index),
|
|
|
|
length_(length),
|
|
|
|
blockLength_(blockLength),
|
|
|
|
bitfield_(new BitfieldMan(blockLength_, length)),
|
2013-08-20 16:57:17 +00:00
|
|
|
usedBySegment_(false), wrCache_(nullptr)
|
2008-06-04 16:28:16 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2011-12-07 15:03:25 +00:00
|
|
|
,nextBegin_(0)
|
2008-06-04 16:28:16 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2010-02-11 08:28:41 +00:00
|
|
|
{}
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
Piece::~Piece()
|
|
|
|
{
|
2012-11-27 13:04:59 +00:00
|
|
|
delete wrCache_;
|
2010-06-21 13:51:56 +00:00
|
|
|
delete bitfield_;
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 08:04:28 +00:00
|
|
|
void Piece::completeBlock(size_t blockIndex) {
|
2010-06-21 13:51:56 +00:00
|
|
|
bitfield_->setBit(blockIndex);
|
|
|
|
bitfield_->unsetUseBit(blockIndex);
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 15:39:14 +00:00
|
|
|
void Piece::clearAllBlock(WrDiskCache* diskCache) {
|
2010-06-21 13:51:56 +00:00
|
|
|
bitfield_->clearAllBit();
|
|
|
|
bitfield_->clearAllUseBit();
|
2012-12-05 15:39:14 +00:00
|
|
|
if(diskCache && wrCache_) {
|
|
|
|
clearWrCache(diskCache);
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Piece::setAllBlock() {
|
2010-06-21 13:51:56 +00:00
|
|
|
bitfield_->setAllBit();
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Piece::pieceComplete() const {
|
2010-06-21 13:51:56 +00:00
|
|
|
return bitfield_->isAllBitSet();
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 08:04:28 +00:00
|
|
|
size_t Piece::countBlock() const
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return bitfield_->countBlock();
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
2011-12-07 15:03:25 +00:00
|
|
|
int32_t Piece::getBlockLength(size_t index) const
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return bitfield_->getBlockLength(index);
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
2011-12-07 15:03:25 +00:00
|
|
|
int32_t Piece::getBlockLength() const
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return bitfield_->getBlockLength();
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned char* Piece::getBitfield() const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return bitfield_->getBitfield();
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 08:04:28 +00:00
|
|
|
size_t Piece::getBitfieldLength() const
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return bitfield_->getBitfieldLength();
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 08:04:28 +00:00
|
|
|
bool Piece::isBlockUsed(size_t index) const
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return bitfield_->isUseBitSet(index);
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 08:04:28 +00:00
|
|
|
void Piece::cancelBlock(size_t blockIndex) {
|
2010-06-21 13:51:56 +00:00
|
|
|
bitfield_->unsetUseBit(blockIndex);
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 08:04:28 +00:00
|
|
|
size_t Piece::countCompleteBlock() const
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return bitfield_->countBlock()-bitfield_->countMissingBlock();
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
2008-06-17 11:43:29 +00:00
|
|
|
size_t Piece::countMissingBlock() const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return bitfield_->countMissingBlock();
|
2008-06-17 11:43:29 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 08:04:28 +00:00
|
|
|
bool Piece::hasBlock(size_t blockIndex) const
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return bitfield_->isBitSet(blockIndex);
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 08:04:28 +00:00
|
|
|
bool Piece::getMissingUnusedBlockIndex(size_t& index) const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
if(bitfield_->getFirstMissingUnusedIndex(index)) {
|
|
|
|
bitfield_->setUseBit(index);
|
2008-03-08 08:04:28 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-11 07:24:06 +00:00
|
|
|
size_t Piece::getMissingUnusedBlockIndex
|
2010-02-10 15:07:06 +00:00
|
|
|
(std::vector<size_t>& indexes, size_t n) const
|
2008-03-08 08:04:28 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
size_t num = bitfield_->getFirstNMissingUnusedIndex(indexes, n);
|
2010-02-11 07:24:06 +00:00
|
|
|
if(num) {
|
2010-02-28 16:04:52 +00:00
|
|
|
for(std::vector<size_t>::const_iterator i = indexes.end()-num,
|
|
|
|
eoi = indexes.end(); i != eoi; ++i) {
|
2010-06-21 13:51:56 +00:00
|
|
|
bitfield_->setUseBit(*i);
|
2010-02-10 15:07:06 +00:00
|
|
|
}
|
2006-05-21 16:19:17 +00:00
|
|
|
}
|
2010-02-11 07:24:06 +00:00
|
|
|
return num;
|
2006-05-21 16:19:17 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 08:04:28 +00:00
|
|
|
bool Piece::getFirstMissingBlockIndexWithoutLock(size_t& index) const
|
2008-01-11 13:37:46 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return bitfield_->getFirstMissingIndex(index);
|
2008-01-11 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
2009-03-28 13:29:38 +00:00
|
|
|
bool Piece::getAllMissingBlockIndexes
|
|
|
|
(unsigned char* misbitfield, size_t mislen) const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return bitfield_->getAllMissingIndexes(misbitfield, mislen);
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2006-11-05 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To divide TorrentMan into 6 classes: BtContext, BtRuntime,
PeerStorage, PieceStorage, BtAnnounce and BtProgressInfoFile
* src/TrackerWatcherComand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.cc: Use pieceStorage, btRuntime
* src/PeerAbstractCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerAbstractCommand.cc: Use btRuntime.
* src/BtContextAwareCommand.h: New class.
* src/FileEntry.h: Added accessor methods for following
variables.
(path): Made private.
(length): Made private.
(offset): Made private.
(extracted): Made private.
(requested): Made private.
(FileEntries): New definition.
(FileEntryHandle): New definition.
* src/FileEntry.cc: New file.
* src/HaveEraseCommand.h: Made subclass of
BtContextAwareCommand.
* src/HaveEraseCommand.cc: Use btRuntime, pieceStorage.
* src/PeerChokeCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerChokeCommand.cc: Use btRuntime, peerStorage,
pieceStorage.
* src/PieceStorage.h: New file.
* src/PeerInteractionCommand.h: Use btContext.
* src/PeerInteractionCommand.cc: Use pieceStorage, peerStorage,
btRuntime.
* src/DefaultBtProgressInfoFile.h: New file.
* src/DefaultBtProgressInfoFile.cc: New file.
* src/File.cc
(Util.h): New include.
(mkdirs): New function.
* src/MultiDiskAdaptor.h
(mkdir): New function.
* src/PeerListProcessor.h
(Peers): Removed.
* src/PeerInteraction.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(btAnnounce): New variable.
(getTorrentMan): Removed.
(getBtContext): New function.
* src/PeerInteraction.cc: Use btContext, peerStorage,
pieceStorage,
btAnnounce.
* src/HandshakeMessage.h
(TorrentMan.h): Removed.
* src/HandshakeMessage.cc: Use btContext.
* src/DefaultBtAnnounce.cc: New file.
* src/MultiDiskWriter.cc: Use the accessor methods of FileEntry.
* src/DefaultPieceStorage.cc: New file.
* src/DefaultBtContext.h: New file.
* src/TorrentRequestInfo.cc: Use btContext, pieceStorage.
Use the accessor methods of FileEntry.
* src/CookieBox.cc: Updated to use Util::slice().
* src/PieceMessage.cc: Use btContext, pieceStorage.
* src/common.h (SharedHandle.h): New include.
* src/PeerMessage.cc (PeerMessage): Added btContext,
peerStorage,
pieceStorage.
* src/TorrentAutoSaveCommand.h: Made subclass of
BtContextAwareCommand.
* src/DiskAdaptor.h
(topDir): Removed.
(getFileEntryFromPath): Changed the return type to
FileEntryHandle.
(setTopDir): Removed.
(getTopDir): Removed.
* src/BtContext.h: New file.
* src/DefaultPeerStorage.h: New file.
* src/PieceMessage.h (TorrentMan.h): Removed.
* src/RequestMessage.h (TorrentMan.h): Removed.
* src/TorrentDownloadEngine.h
(uploadLength): New variable.
(btContext): New variable.
(btRuntime): New variable.
(pieceStorage): New variable.
(peerStorage): New variable.
(btAnnounce): New variable.
(btProgressInfoFile): New variable.
(torrentMan): Removed.
(setBtContext): New function.
* src/TorrentDownloadEngine.cc: Use BtContext, BtRuntime,
pieceStorage,
peerStorage, btAnnounce, btProgressInfoFile.
* src/Piece.h
(toString): New function.
(Pieces): New type definition.
* src/Peer.h
(active): New variable.
(Peer): Added active.
(activate): Set active to true.
(deactivate): Set active to false.
(isActive): New function.
(Peers): New type definition.
* src/DirectDiskAdaptor.cc
(getFilePath): Use the accessor methods of FileEntry.
* src/TorrentConsoleDownloadEngine.h
(afterEachIteration): New function.
* src/TorrentConsoleDownloadEngine.cc
(haltRequested): New variable.
(sendStatistics): Use pieceStorage, btRuntime.
(afterEachIteration): New function.
* src/AnnounceList: AnnounceTier->AnnounceTierHandle.
* src/Directry.h
(Directory): New function.
(DirectoryHandle): New type definition.
* src/BtProgressInfoFile.h: New file.
* src/RequestMessage.cc: Use pieceStorage.
* src/BtRuntime.h: New file.
* src/DefaultBtContext.cc: New file.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/MultiDiskAdaptor.cc
(mkdir): New function.
(openFile): Call mkdir().
(initAndOpenFile): Call mkdir().
* src/CancelMessage.h
(TorrentMan.h): Removed.
* src/RejectMessage.h
(TorrentMan.h): Removed.
* src/DownloadEngineFactory.cc
(DefaultPieceStorage.h): New include.
(DefaultPeerStorage.h): New include.
(DefaultBtAnnounce.h): New include.
(DefaultBtProgressInfoFile.h): New include.
(newTorrentConsoleEngine): Rewritten.
* src/ShareRatioSeedCriteria.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(btRuntime): New variable.
(evaluate): Use btContext, btRuntime, peerStorage.
* src/AnnounceTier.h: New file.
* src/BtAnnounce.h: New file.
* src/BtRegistry.h: New file.
* src/PeerInitiateConnectionCommand.h: Added btContext.
* src/PeerConnection.h (TorrentMan.h): Removed.
* src/PeerMessageFactory.cc: Use btContext, pieceStorage.
* src/Util.h
(slice): Added an argument.
* src/Util.cc
(slice): Added an argument to control whether trim is made or
not.
* src/PeerStorage.h: New file.
* src/BtRegistry.cc: New file.
* src/TrackerUpdateCommand.h: Made subclass of
BtContextAwareCommand.
* src/CopyDiskAdaptor.cc: Use the accessor methods of FileEntry.
* src/MultiDiskWriter.h: FileEntry -> FileEntryHandle
* src/PeerListenCommand.cc: Use btRuntime, peerStorage,
btContext.
* src/TorrentRequestInfo.h
(e): Removed.
(showFileEntry): Added an argument.
(getDownloadEngine): Return 0.
* src/DefaultBtAnnounce.h: New file.
* src/TorrentAutoSaveCommand.cc: Use btRuntime,
btProgressInfoFile.
* src/TrackerWatcherComand.cc: Use btRuntime, btAnnounce,
* src/PeerMessageFactory.h
(btContext): New variable.
(pieceStorage): New variable.
* src/TrackerUpdateCommand.cc: Use btRuntime, peerStorage,
btContext,
btAnnounce.
* src/DiskAdaptor.cc
(DiskAdaptor): Removed topDir.
(~DiskAdaptor): Removed topDir.
* src/PeerListenCommand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.h: Made subclass of
BtContextAwareCommand.
* src/File.h (mkdirs): New function.
* src/DefaultPeerStorage): New file.
* src/DownloadEngineFactory.h
(newTorrentConsoleEngine): Use btContext.
* src/BtContextAwareCommand.cc: New file.
* src/PeerInitiateConnectionCommand.cc: Use btRuntime,
peerStorage.
* src/PeerMessage.h
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(setBtContext): New function.
* src/Directry.cc
(Directory): New function.
(createDir): Do nothing if name.size() == 0.
* src/AnnounceList.h
(AnnounceTier): Removed.
(AnnounceTiers): Removed.
* src/DefaultPieceStorage.h: New file.
* src/Piece.cc (toString): New function.
To fix typo:
* src/main.cc (showVersion): Fixed typo.
To fix compile warning:
* src/DelegatingPeerListProcessor.cc
(canHandle): Added "return false".
2006-11-05 15:04:17 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string Piece::toString() const {
|
2011-12-07 15:03:25 +00:00
|
|
|
return fmt("piece: index=%lu, length=%d",
|
|
|
|
static_cast<unsigned long>(index_), length_);
|
2006-11-05 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To divide TorrentMan into 6 classes: BtContext, BtRuntime,
PeerStorage, PieceStorage, BtAnnounce and BtProgressInfoFile
* src/TrackerWatcherComand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.cc: Use pieceStorage, btRuntime
* src/PeerAbstractCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerAbstractCommand.cc: Use btRuntime.
* src/BtContextAwareCommand.h: New class.
* src/FileEntry.h: Added accessor methods for following
variables.
(path): Made private.
(length): Made private.
(offset): Made private.
(extracted): Made private.
(requested): Made private.
(FileEntries): New definition.
(FileEntryHandle): New definition.
* src/FileEntry.cc: New file.
* src/HaveEraseCommand.h: Made subclass of
BtContextAwareCommand.
* src/HaveEraseCommand.cc: Use btRuntime, pieceStorage.
* src/PeerChokeCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerChokeCommand.cc: Use btRuntime, peerStorage,
pieceStorage.
* src/PieceStorage.h: New file.
* src/PeerInteractionCommand.h: Use btContext.
* src/PeerInteractionCommand.cc: Use pieceStorage, peerStorage,
btRuntime.
* src/DefaultBtProgressInfoFile.h: New file.
* src/DefaultBtProgressInfoFile.cc: New file.
* src/File.cc
(Util.h): New include.
(mkdirs): New function.
* src/MultiDiskAdaptor.h
(mkdir): New function.
* src/PeerListProcessor.h
(Peers): Removed.
* src/PeerInteraction.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(btAnnounce): New variable.
(getTorrentMan): Removed.
(getBtContext): New function.
* src/PeerInteraction.cc: Use btContext, peerStorage,
pieceStorage,
btAnnounce.
* src/HandshakeMessage.h
(TorrentMan.h): Removed.
* src/HandshakeMessage.cc: Use btContext.
* src/DefaultBtAnnounce.cc: New file.
* src/MultiDiskWriter.cc: Use the accessor methods of FileEntry.
* src/DefaultPieceStorage.cc: New file.
* src/DefaultBtContext.h: New file.
* src/TorrentRequestInfo.cc: Use btContext, pieceStorage.
Use the accessor methods of FileEntry.
* src/CookieBox.cc: Updated to use Util::slice().
* src/PieceMessage.cc: Use btContext, pieceStorage.
* src/common.h (SharedHandle.h): New include.
* src/PeerMessage.cc (PeerMessage): Added btContext,
peerStorage,
pieceStorage.
* src/TorrentAutoSaveCommand.h: Made subclass of
BtContextAwareCommand.
* src/DiskAdaptor.h
(topDir): Removed.
(getFileEntryFromPath): Changed the return type to
FileEntryHandle.
(setTopDir): Removed.
(getTopDir): Removed.
* src/BtContext.h: New file.
* src/DefaultPeerStorage.h: New file.
* src/PieceMessage.h (TorrentMan.h): Removed.
* src/RequestMessage.h (TorrentMan.h): Removed.
* src/TorrentDownloadEngine.h
(uploadLength): New variable.
(btContext): New variable.
(btRuntime): New variable.
(pieceStorage): New variable.
(peerStorage): New variable.
(btAnnounce): New variable.
(btProgressInfoFile): New variable.
(torrentMan): Removed.
(setBtContext): New function.
* src/TorrentDownloadEngine.cc: Use BtContext, BtRuntime,
pieceStorage,
peerStorage, btAnnounce, btProgressInfoFile.
* src/Piece.h
(toString): New function.
(Pieces): New type definition.
* src/Peer.h
(active): New variable.
(Peer): Added active.
(activate): Set active to true.
(deactivate): Set active to false.
(isActive): New function.
(Peers): New type definition.
* src/DirectDiskAdaptor.cc
(getFilePath): Use the accessor methods of FileEntry.
* src/TorrentConsoleDownloadEngine.h
(afterEachIteration): New function.
* src/TorrentConsoleDownloadEngine.cc
(haltRequested): New variable.
(sendStatistics): Use pieceStorage, btRuntime.
(afterEachIteration): New function.
* src/AnnounceList: AnnounceTier->AnnounceTierHandle.
* src/Directry.h
(Directory): New function.
(DirectoryHandle): New type definition.
* src/BtProgressInfoFile.h: New file.
* src/RequestMessage.cc: Use pieceStorage.
* src/BtRuntime.h: New file.
* src/DefaultBtContext.cc: New file.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/MultiDiskAdaptor.cc
(mkdir): New function.
(openFile): Call mkdir().
(initAndOpenFile): Call mkdir().
* src/CancelMessage.h
(TorrentMan.h): Removed.
* src/RejectMessage.h
(TorrentMan.h): Removed.
* src/DownloadEngineFactory.cc
(DefaultPieceStorage.h): New include.
(DefaultPeerStorage.h): New include.
(DefaultBtAnnounce.h): New include.
(DefaultBtProgressInfoFile.h): New include.
(newTorrentConsoleEngine): Rewritten.
* src/ShareRatioSeedCriteria.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(btRuntime): New variable.
(evaluate): Use btContext, btRuntime, peerStorage.
* src/AnnounceTier.h: New file.
* src/BtAnnounce.h: New file.
* src/BtRegistry.h: New file.
* src/PeerInitiateConnectionCommand.h: Added btContext.
* src/PeerConnection.h (TorrentMan.h): Removed.
* src/PeerMessageFactory.cc: Use btContext, pieceStorage.
* src/Util.h
(slice): Added an argument.
* src/Util.cc
(slice): Added an argument to control whether trim is made or
not.
* src/PeerStorage.h: New file.
* src/BtRegistry.cc: New file.
* src/TrackerUpdateCommand.h: Made subclass of
BtContextAwareCommand.
* src/CopyDiskAdaptor.cc: Use the accessor methods of FileEntry.
* src/MultiDiskWriter.h: FileEntry -> FileEntryHandle
* src/PeerListenCommand.cc: Use btRuntime, peerStorage,
btContext.
* src/TorrentRequestInfo.h
(e): Removed.
(showFileEntry): Added an argument.
(getDownloadEngine): Return 0.
* src/DefaultBtAnnounce.h: New file.
* src/TorrentAutoSaveCommand.cc: Use btRuntime,
btProgressInfoFile.
* src/TrackerWatcherComand.cc: Use btRuntime, btAnnounce,
* src/PeerMessageFactory.h
(btContext): New variable.
(pieceStorage): New variable.
* src/TrackerUpdateCommand.cc: Use btRuntime, peerStorage,
btContext,
btAnnounce.
* src/DiskAdaptor.cc
(DiskAdaptor): Removed topDir.
(~DiskAdaptor): Removed topDir.
* src/PeerListenCommand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.h: Made subclass of
BtContextAwareCommand.
* src/File.h (mkdirs): New function.
* src/DefaultPeerStorage): New file.
* src/DownloadEngineFactory.h
(newTorrentConsoleEngine): Use btContext.
* src/BtContextAwareCommand.cc: New file.
* src/PeerInitiateConnectionCommand.cc: Use btRuntime,
peerStorage.
* src/PeerMessage.h
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(setBtContext): New function.
* src/Directry.cc
(Directory): New function.
(createDir): Do nothing if name.size() == 0.
* src/AnnounceList.h
(AnnounceTier): Removed.
(AnnounceTiers): Removed.
* src/DefaultPieceStorage.h: New file.
* src/Piece.cc (toString): New function.
To fix typo:
* src/main.cc (showVersion): Fixed typo.
To fix compile warning:
* src/DelegatingPeerListProcessor.cc
(canHandle): Added "return false".
2006-11-05 15:04:17 +00:00
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2011-12-07 15:03:25 +00:00
|
|
|
void Piece::reconfigure(int32_t length)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
delete bitfield_;
|
|
|
|
length_ = length;
|
|
|
|
bitfield_ = new BitfieldMan(blockLength_, length_);
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 08:04:28 +00:00
|
|
|
void Piece::setBitfield(const unsigned char* bitfield, size_t len)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
bitfield_->setBitfield(bitfield, len);
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
|
2011-12-07 15:03:25 +00:00
|
|
|
int32_t Piece::getCompletedLength()
|
2007-10-23 16:29:37 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return bitfield_->getCompletedLength();
|
2007-10-23 16:29:37 +00:00
|
|
|
}
|
|
|
|
|
2008-06-04 16:28:16 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
|
|
|
|
2011-07-27 13:50:10 +00:00
|
|
|
void Piece::setHashType(const std::string& hashType)
|
2008-06-04 16:28:16 +00:00
|
|
|
{
|
2011-07-27 13:50:10 +00:00
|
|
|
hashType_ = hashType;
|
2008-06-04 16:28:16 +00:00
|
|
|
}
|
|
|
|
|
2010-06-12 13:29:40 +00:00
|
|
|
bool Piece::updateHash
|
2011-12-07 15:03:25 +00:00
|
|
|
(int32_t begin, const unsigned char* data, size_t dataLength)
|
2008-06-04 16:28:16 +00:00
|
|
|
{
|
2011-07-27 13:50:10 +00:00
|
|
|
if(hashType_.empty()) {
|
2008-06-04 16:28:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-12-07 15:03:25 +00:00
|
|
|
if(begin == nextBegin_ &&
|
|
|
|
nextBegin_+dataLength <= static_cast<size_t>(length_)) {
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!mdctx_) {
|
2011-07-27 13:50:10 +00:00
|
|
|
mdctx_ = MessageDigest::create(hashType_);
|
2008-06-04 16:28:16 +00:00
|
|
|
}
|
2010-11-11 02:56:24 +00:00
|
|
|
mdctx_->update(data, dataLength);
|
2010-06-21 13:51:56 +00:00
|
|
|
nextBegin_ += dataLength;
|
2008-06-04 16:28:16 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Piece::isHashCalculated() const
|
|
|
|
{
|
2010-11-12 12:48:48 +00:00
|
|
|
return mdctx_ && nextBegin_ == length_;
|
2008-06-04 16:28:16 +00:00
|
|
|
}
|
|
|
|
|
2011-10-13 12:40:07 +00:00
|
|
|
std::string Piece::getDigest()
|
2008-06-04 16:28:16 +00:00
|
|
|
{
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!mdctx_) {
|
2008-06-04 16:28:16 +00:00
|
|
|
return A2STR::NIL;
|
|
|
|
} else {
|
2011-10-13 12:40:07 +00:00
|
|
|
std::string hash = mdctx_->digest();
|
2010-02-11 08:33:58 +00:00
|
|
|
destroyHashContext();
|
|
|
|
return hash;
|
2008-06-04 16:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-27 13:04:59 +00:00
|
|
|
namespace {
|
2013-07-02 16:13:13 +00:00
|
|
|
void updateHashWithRead(MessageDigest* mdctx,
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<DiskAdaptor>& adaptor,
|
2012-11-27 13:04:59 +00:00
|
|
|
int64_t offset, size_t len)
|
|
|
|
{
|
|
|
|
const size_t BUFSIZE = 4096;
|
|
|
|
unsigned char buf[BUFSIZE];
|
|
|
|
ldiv_t res = ldiv(len, BUFSIZE);
|
|
|
|
for(int j = 0; j < res.quot; ++j) {
|
|
|
|
ssize_t nread = adaptor->readData(buf, BUFSIZE, offset);
|
|
|
|
if((size_t)nread != BUFSIZE) {
|
|
|
|
throw DL_ABORT_EX(fmt(EX_FILE_READ, "n/a", "data is too short"));
|
|
|
|
}
|
|
|
|
mdctx->update(buf, nread);
|
|
|
|
offset += nread;
|
|
|
|
}
|
|
|
|
if(res.rem) {
|
|
|
|
ssize_t nread = adaptor->readData(buf, res.rem, offset);
|
|
|
|
if(nread != res.rem) {
|
|
|
|
throw DL_ABORT_EX(fmt(EX_FILE_READ, "n/a", "data is too short"));
|
|
|
|
}
|
|
|
|
mdctx->update(buf, nread);
|
|
|
|
offset += nread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::string Piece::getDigestWithWrCache
|
2013-06-21 16:10:38 +00:00
|
|
|
(size_t pieceLength, const std::shared_ptr<DiskAdaptor>& adaptor)
|
2012-11-27 13:04:59 +00:00
|
|
|
{
|
2013-07-02 16:13:13 +00:00
|
|
|
auto mdctx = MessageDigest::create(hashType_);
|
2012-11-27 13:04:59 +00:00
|
|
|
int64_t start = static_cast<int64_t>(index_)*pieceLength;
|
|
|
|
int64_t goff = start;
|
|
|
|
if(wrCache_) {
|
|
|
|
const WrDiskCacheEntry::DataCellSet& dataSet = wrCache_->getDataSet();
|
|
|
|
for(WrDiskCacheEntry::DataCellSet::iterator i = dataSet.begin(),
|
|
|
|
eoi = dataSet.end(); i != eoi; ++i) {
|
|
|
|
if(goff < (*i)->goff) {
|
2013-07-02 16:13:13 +00:00
|
|
|
updateHashWithRead(mdctx.get(), adaptor, goff, (*i)->goff - goff);
|
2012-11-27 13:04:59 +00:00
|
|
|
}
|
|
|
|
mdctx->update((*i)->data+(*i)->offset, (*i)->len);
|
|
|
|
goff = (*i)->goff + (*i)->len;
|
|
|
|
}
|
2013-07-02 16:13:13 +00:00
|
|
|
updateHashWithRead(mdctx.get(), adaptor, goff, start+length_-goff);
|
2012-11-27 13:04:59 +00:00
|
|
|
} else {
|
2013-07-02 16:13:13 +00:00
|
|
|
updateHashWithRead(mdctx.get(), adaptor, goff, length_);
|
2012-11-27 13:04:59 +00:00
|
|
|
}
|
|
|
|
return mdctx->digest();
|
|
|
|
}
|
|
|
|
|
2008-06-04 16:28:16 +00:00
|
|
|
void Piece::destroyHashContext()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
mdctx_.reset();
|
|
|
|
nextBegin_ = 0;
|
2008-06-04 16:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
|
|
|
|
2011-07-15 15:58:41 +00:00
|
|
|
bool Piece::usedBy(cuid_t cuid) const
|
|
|
|
{
|
|
|
|
return std::find(users_.begin(), users_.end(), cuid) != users_.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Piece::addUser(cuid_t cuid)
|
|
|
|
{
|
|
|
|
if(std::find(users_.begin(), users_.end(), cuid) == users_.end()) {
|
|
|
|
users_.push_back(cuid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Piece::removeUser(cuid_t cuid)
|
|
|
|
{
|
|
|
|
users_.erase(std::remove(users_.begin(), users_.end(), cuid), users_.end());
|
|
|
|
}
|
|
|
|
|
2012-11-27 13:04:59 +00:00
|
|
|
void Piece::initWrCache(WrDiskCache* diskCache,
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<DiskAdaptor>& diskAdaptor)
|
2012-11-27 13:04:59 +00:00
|
|
|
{
|
2012-12-05 16:16:28 +00:00
|
|
|
if(!diskCache) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-20 16:57:17 +00:00
|
|
|
assert(wrCache_ == nullptr);
|
2012-11-27 13:04:59 +00:00
|
|
|
wrCache_ = new WrDiskCacheEntry(diskAdaptor);
|
|
|
|
bool rv = diskCache->add(wrCache_);
|
|
|
|
assert(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Piece::flushWrCache(WrDiskCache* diskCache)
|
|
|
|
{
|
2012-12-05 16:16:28 +00:00
|
|
|
if(!diskCache) {
|
|
|
|
return;
|
|
|
|
}
|
2012-11-27 13:04:59 +00:00
|
|
|
assert(wrCache_);
|
|
|
|
ssize_t size = static_cast<ssize_t>(wrCache_->getSize());
|
|
|
|
diskCache->update(wrCache_, -size);
|
|
|
|
wrCache_->writeToDisk();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Piece::clearWrCache(WrDiskCache* diskCache)
|
|
|
|
{
|
2012-12-05 16:16:28 +00:00
|
|
|
if(!diskCache) {
|
|
|
|
return;
|
|
|
|
}
|
2012-11-27 13:04:59 +00:00
|
|
|
assert(wrCache_);
|
|
|
|
ssize_t size = static_cast<ssize_t>(wrCache_->getSize());
|
|
|
|
diskCache->update(wrCache_, -size);
|
|
|
|
wrCache_->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Piece::updateWrCache(WrDiskCache* diskCache, unsigned char* data,
|
2012-12-05 17:23:32 +00:00
|
|
|
size_t offset, size_t len, size_t capacity,
|
|
|
|
int64_t goff)
|
2012-11-27 13:04:59 +00:00
|
|
|
{
|
2012-12-05 16:16:28 +00:00
|
|
|
if(!diskCache) {
|
|
|
|
return;
|
|
|
|
}
|
2012-11-27 13:04:59 +00:00
|
|
|
assert(wrCache_);
|
2012-12-05 16:16:28 +00:00
|
|
|
A2_LOG_DEBUG(fmt("updateWrCache entry=%p", wrCache_));
|
2013-08-21 03:06:53 +00:00
|
|
|
auto cell = new WrDiskCacheEntry::DataCell();
|
2012-11-27 13:04:59 +00:00
|
|
|
cell->goff = goff;
|
|
|
|
cell->data = data;
|
|
|
|
cell->offset = offset;
|
|
|
|
cell->len = len;
|
2012-12-05 17:23:32 +00:00
|
|
|
cell->capacity = capacity;
|
2012-11-27 13:04:59 +00:00
|
|
|
bool rv;
|
|
|
|
rv = wrCache_->cacheData(cell);
|
|
|
|
assert(rv);
|
|
|
|
rv = diskCache->update(wrCache_, len);
|
|
|
|
assert(rv);
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:23:32 +00:00
|
|
|
size_t Piece::appendWrCache(WrDiskCache* diskCache, int64_t goff,
|
|
|
|
const unsigned char* data, size_t len)
|
|
|
|
{
|
|
|
|
if(!diskCache) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
assert(wrCache_);
|
|
|
|
size_t delta = wrCache_->append(goff, data, len);
|
|
|
|
bool rv;
|
|
|
|
if(delta > 0) {
|
|
|
|
rv = diskCache->update(wrCache_, delta);
|
|
|
|
assert(rv);
|
|
|
|
}
|
|
|
|
return delta;
|
|
|
|
}
|
|
|
|
|
2012-11-27 13:04:59 +00:00
|
|
|
void Piece::releaseWrCache(WrDiskCache* diskCache)
|
|
|
|
{
|
2012-12-05 16:16:28 +00:00
|
|
|
if(diskCache && wrCache_) {
|
2012-11-27 13:04:59 +00:00
|
|
|
diskCache->remove(wrCache_);
|
|
|
|
delete wrCache_;
|
2013-08-20 16:57:17 +00:00
|
|
|
wrCache_ = nullptr;
|
2012-11-27 13:04:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|