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
|
|
|
|
2015-06-24 12:24:10 +00:00
|
|
|
#include <array>
|
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"
|
2014-04-16 23:36:19 +00:00
|
|
|
#include "MessageDigest.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
Piece::Piece() : index_(0), length_(0), nextBegin_(0), usedBySegment_(false) {}
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2014-07-12 07:52:01 +00:00
|
|
|
Piece::Piece(size_t index, int64_t length, int32_t blockLength)
|
2015-12-27 09:39:47 +00:00
|
|
|
: bitfield_(make_unique<BitfieldMan>(blockLength, length)),
|
|
|
|
index_(index),
|
|
|
|
length_(length),
|
|
|
|
nextBegin_(0),
|
|
|
|
usedBySegment_(false)
|
|
|
|
{
|
|
|
|
}
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
Piece::~Piece() {}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2015-12-27 09:39:47 +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
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void Piece::clearAllBlock(WrDiskCache* diskCache)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
bitfield_->clearAllBit();
|
|
|
|
bitfield_->clearAllUseBit();
|
2015-12-27 09:39:47 +00:00
|
|
|
if (diskCache && wrCache_) {
|
2012-12-05 15:39:14 +00:00
|
|
|
clearWrCache(diskCache);
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void Piece::setAllBlock() { bitfield_->setAllBit(); }
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
bool Piece::pieceComplete() const { return bitfield_->isAllBitSet(); }
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
size_t Piece::countBlock() const { 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
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
int32_t Piece::getBlockLength() const { 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
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +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
|
|
|
{
|
2015-12-27 09:39:47 +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
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (bitfield_->getFirstMissingUnusedIndex(index)) {
|
2010-06-21 13:51:56 +00:00
|
|
|
bitfield_->setUseBit(index);
|
2008-03-08 08:04:28 +00:00
|
|
|
return true;
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2008-03-08 08:04:28 +00:00
|
|
|
return false;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
size_t Piece::getMissingUnusedBlockIndex(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);
|
2015-12-27 09:39:47 +00:00
|
|
|
if (num) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
bool Piece::getAllMissingBlockIndexes(unsigned char* misbitfield,
|
|
|
|
size_t mislen) const
|
2009-03-28 13:29:38 +00:00
|
|
|
{
|
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
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
std::string Piece::toString() const
|
|
|
|
{
|
2014-07-12 07:52:01 +00:00
|
|
|
return fmt("piece: index=%lu, length=%" PRId64,
|
2011-12-07 15:03:25 +00:00
|
|
|
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
|
|
|
|
2014-07-12 07:52:01 +00:00
|
|
|
void Piece::reconfigure(int64_t length)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
length_ = length;
|
2014-07-12 07:52:01 +00:00
|
|
|
// TODO currently, this function is only called from
|
|
|
|
// GrowSegment::updateWrittenLength(). If we use default block
|
|
|
|
// length (16K), and length_ gets large (e.g., 4GB), creating
|
|
|
|
// BitfieldMan for each call is very expensive. Therefore, we use
|
|
|
|
// maximum block length for now to reduce the overhead. Ideally, we
|
|
|
|
// check the code thoroughly and remove bitfield_ if we can.
|
2015-12-27 09:39:47 +00:00
|
|
|
bitfield_ =
|
|
|
|
make_unique<BitfieldMan>(std::numeric_limits<int32_t>::max(), 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
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
int64_t Piece::getCompletedLength() { return bitfield_->getCompletedLength(); }
|
2007-10-23 16:29:37 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void Piece::setHashType(const std::string& hashType) { hashType_ = hashType; }
|
2008-06-04 16:28:16 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
bool Piece::updateHash(int64_t begin, const unsigned char* data,
|
|
|
|
size_t dataLength)
|
2008-06-04 16:28:16 +00:00
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (hashType_.empty()) {
|
2008-06-04 16:28:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-12-27 09:39:47 +00:00
|
|
|
if (begin == nextBegin_ &&
|
|
|
|
nextBegin_ + static_cast<int64_t>(dataLength) <= length_) {
|
|
|
|
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;
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2008-06-04 16:28:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
bool Piece::isHashCalculated() const { 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
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (!mdctx_) {
|
2008-06-04 16:28:16 +00:00
|
|
|
return A2STR::NIL;
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2015-06-21 09:04:30 +00:00
|
|
|
std::array<unsigned char, 4_k> buf;
|
|
|
|
ldiv_t res = ldiv(len, buf.size());
|
2015-12-27 09:39:47 +00:00
|
|
|
for (int j = 0; j < res.quot; ++j) {
|
2015-06-21 09:04:30 +00:00
|
|
|
ssize_t nread = adaptor->readData(buf.data(), buf.size(), offset);
|
2015-12-27 09:39:47 +00:00
|
|
|
if ((size_t)nread != buf.size()) {
|
2012-11-27 13:04:59 +00:00
|
|
|
throw DL_ABORT_EX(fmt(EX_FILE_READ, "n/a", "data is too short"));
|
|
|
|
}
|
2015-06-21 09:04:30 +00:00
|
|
|
mdctx->update(buf.data(), nread);
|
2012-11-27 13:04:59 +00:00
|
|
|
offset += nread;
|
|
|
|
}
|
2015-12-27 09:39:47 +00:00
|
|
|
if (res.rem) {
|
2015-06-21 09:04:30 +00:00
|
|
|
ssize_t nread = adaptor->readData(buf.data(), res.rem, offset);
|
2015-12-27 09:39:47 +00:00
|
|
|
if (nread != res.rem) {
|
2012-11-27 13:04:59 +00:00
|
|
|
throw DL_ABORT_EX(fmt(EX_FILE_READ, "n/a", "data is too short"));
|
|
|
|
}
|
2015-06-21 09:04:30 +00:00
|
|
|
mdctx->update(buf.data(), nread);
|
2012-11-27 13:04:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
std::string
|
|
|
|
Piece::getDigestWithWrCache(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_);
|
2015-12-27 09:39:47 +00:00
|
|
|
int64_t start = static_cast<int64_t>(index_) * pieceLength;
|
2012-11-27 13:04:59 +00:00
|
|
|
int64_t goff = start;
|
2015-12-27 09:39:47 +00:00
|
|
|
if (wrCache_) {
|
2012-11-27 13:04:59 +00:00
|
|
|
const WrDiskCacheEntry::DataCellSet& dataSet = wrCache_->getDataSet();
|
2015-12-27 09:39:47 +00:00
|
|
|
for (auto& d : dataSet) {
|
|
|
|
if (goff < d->goff) {
|
2013-08-25 16:00:46 +00:00
|
|
|
updateHashWithRead(mdctx.get(), adaptor, goff, d->goff - goff);
|
2012-11-27 13:04:59 +00:00
|
|
|
}
|
2013-08-25 16:00:46 +00:00
|
|
|
mdctx->update(d->data + d->offset, d->len);
|
|
|
|
goff = d->goff + d->len;
|
2012-11-27 13:04:59 +00:00
|
|
|
}
|
2015-12-27 09:39:47 +00:00
|
|
|
updateHashWithRead(mdctx.get(), adaptor, goff, start + length_ - goff);
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (std::find(users_.begin(), users_.end(), cuid) == users_.end()) {
|
2011-07-15 15:58:41 +00:00
|
|
|
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
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (!diskCache) {
|
2012-12-05 16:16:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-09-13 09:37:57 +00:00
|
|
|
assert(!wrCache_);
|
|
|
|
wrCache_ = make_unique<WrDiskCacheEntry>(diskAdaptor);
|
|
|
|
bool rv = diskCache->add(wrCache_.get());
|
2012-11-27 13:04:59 +00:00
|
|
|
assert(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Piece::flushWrCache(WrDiskCache* diskCache)
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (!diskCache) {
|
2012-12-05 16:16:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-27 13:04:59 +00:00
|
|
|
assert(wrCache_);
|
|
|
|
ssize_t size = static_cast<ssize_t>(wrCache_->getSize());
|
2014-09-13 09:37:57 +00:00
|
|
|
diskCache->update(wrCache_.get(), -size);
|
2012-11-27 13:04:59 +00:00
|
|
|
wrCache_->writeToDisk();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Piece::clearWrCache(WrDiskCache* diskCache)
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (!diskCache) {
|
2012-12-05 16:16:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-27 13:04:59 +00:00
|
|
|
assert(wrCache_);
|
|
|
|
ssize_t size = static_cast<ssize_t>(wrCache_->getSize());
|
2014-09-13 09:37:57 +00:00
|
|
|
diskCache->update(wrCache_.get(), -size);
|
2012-11-27 13:04:59 +00:00
|
|
|
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
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (!diskCache) {
|
2012-12-05 16:16:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-27 13:04:59 +00:00
|
|
|
assert(wrCache_);
|
2014-09-13 09:37:57 +00:00
|
|
|
A2_LOG_DEBUG(fmt("updateWrCache entry=%p", wrCache_.get()));
|
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);
|
2014-09-13 09:37:57 +00:00
|
|
|
rv = diskCache->update(wrCache_.get(), len);
|
2012-11-27 13:04:59 +00:00
|
|
|
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)
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (!diskCache) {
|
2012-12-05 17:23:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
assert(wrCache_);
|
|
|
|
size_t delta = wrCache_->append(goff, data, len);
|
|
|
|
bool rv;
|
2015-12-27 09:39:47 +00:00
|
|
|
if (delta > 0) {
|
2014-09-13 09:37:57 +00:00
|
|
|
rv = diskCache->update(wrCache_.get(), delta);
|
2012-12-05 17:23:32 +00:00
|
|
|
assert(rv);
|
|
|
|
}
|
|
|
|
return delta;
|
|
|
|
}
|
|
|
|
|
2012-11-27 13:04:59 +00:00
|
|
|
void Piece::releaseWrCache(WrDiskCache* diskCache)
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (diskCache && wrCache_) {
|
2014-09-13 09:37:57 +00:00
|
|
|
diskCache->remove(wrCache_.get());
|
|
|
|
wrCache_.reset();
|
2012-11-27 13:04:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|