2007-10-11 16:58:24 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* 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
|
2007-10-11 16:58: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.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "UnknownLengthPieceStorage.h"
|
2009-05-29 12:32:17 +00:00
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "DefaultDiskWriter.h"
|
|
|
|
#include "DirectDiskAdaptor.h"
|
|
|
|
#include "prefs.h"
|
|
|
|
#include "DefaultDiskWriterFactory.h"
|
|
|
|
#include "DownloadContext.h"
|
|
|
|
#include "Piece.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "FileEntry.h"
|
2014-07-15 14:36:10 +00:00
|
|
|
#include "BitfieldMan.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
UnknownLengthPieceStorage::UnknownLengthPieceStorage
|
2013-06-21 16:10:38 +00:00
|
|
|
(const std::shared_ptr<DownloadContext>& downloadContext)
|
2013-03-01 12:17:52 +00:00
|
|
|
: downloadContext_(downloadContext),
|
2014-07-15 14:48:32 +00:00
|
|
|
diskWriterFactory_(std::make_shared<DefaultDiskWriterFactory>()),
|
|
|
|
totalLength_(0),
|
|
|
|
downloadFinished_(false) {}
|
2007-10-11 16:58:24 +00:00
|
|
|
|
|
|
|
UnknownLengthPieceStorage::~UnknownLengthPieceStorage() {}
|
|
|
|
|
|
|
|
void UnknownLengthPieceStorage::initStorage()
|
|
|
|
{
|
2014-07-15 14:48:32 +00:00
|
|
|
auto directDiskAdaptor = std::make_shared<DirectDiskAdaptor>();
|
2010-06-21 13:51:56 +00:00
|
|
|
directDiskAdaptor->setTotalLength(downloadContext_->getTotalLength());
|
|
|
|
directDiskAdaptor->setFileEntries(downloadContext_->getFileEntries().begin(),
|
|
|
|
downloadContext_->getFileEntries().end());
|
2009-05-04 07:50:38 +00:00
|
|
|
|
2013-07-05 12:08:24 +00:00
|
|
|
directDiskAdaptor->setDiskWriter
|
|
|
|
(diskWriterFactory_->newDiskWriter(directDiskAdaptor->getFilePath()));
|
2009-05-04 07:50:38 +00:00
|
|
|
|
2014-07-15 14:48:32 +00:00
|
|
|
diskAdaptor_ = std::move(directDiskAdaptor);
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
|
2009-05-29 12:32:17 +00:00
|
|
|
#ifdef ENABLE_BITTORRENT
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
bool UnknownLengthPieceStorage::hasMissingPiece(const std::shared_ptr<Peer>& peer)
|
2008-03-15 04:19:46 +00:00
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2010-08-29 08:53:19 +00:00
|
|
|
void UnknownLengthPieceStorage::getMissingPiece
|
2013-06-21 16:10:38 +00:00
|
|
|
(std::vector<std::shared_ptr<Piece> >& pieces,
|
2010-08-29 08:53:19 +00:00
|
|
|
size_t minMissingBlocks,
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<Peer>& peer,
|
2011-07-15 15:58:41 +00:00
|
|
|
cuid_t cuid)
|
2010-08-29 08:53:19 +00:00
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnknownLengthPieceStorage::getMissingPiece
|
2013-06-21 16:10:38 +00:00
|
|
|
(std::vector<std::shared_ptr<Piece> >& pieces,
|
2010-08-29 08:53:19 +00:00
|
|
|
size_t minMissingBlocks,
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<Peer>& peer,
|
2011-07-15 15:58:41 +00:00
|
|
|
const std::vector<size_t>& excludedIndexes,
|
|
|
|
cuid_t cuid)
|
2010-08-29 08:53:19 +00:00
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnknownLengthPieceStorage::getMissingFastPiece
|
2013-06-21 16:10:38 +00:00
|
|
|
(std::vector<std::shared_ptr<Piece> >& pieces,
|
2010-08-29 08:53:19 +00:00
|
|
|
size_t minMissingBlocks,
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<Peer>& peer,
|
2011-07-15 15:58:41 +00:00
|
|
|
cuid_t cuid)
|
2010-08-29 08:53:19 +00:00
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnknownLengthPieceStorage::getMissingFastPiece
|
2013-06-21 16:10:38 +00:00
|
|
|
(std::vector<std::shared_ptr<Piece> >& pieces,
|
2010-08-29 08:53:19 +00:00
|
|
|
size_t minMissingBlocks,
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<Peer>& peer,
|
2011-07-15 15:58:41 +00:00
|
|
|
const std::vector<size_t>& excludedIndexes,
|
|
|
|
cuid_t cuid)
|
2010-08-29 08:53:19 +00:00
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Piece> UnknownLengthPieceStorage::getMissingPiece
|
|
|
|
(const std::shared_ptr<Peer>& peer,
|
2011-07-15 15:58:41 +00:00
|
|
|
cuid_t cuid)
|
2008-03-15 04:19:46 +00:00
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Piece> UnknownLengthPieceStorage::getMissingPiece
|
|
|
|
(const std::shared_ptr<Peer>& peer,
|
2011-07-15 15:58:41 +00:00
|
|
|
const std::vector<size_t>& excludedIndexes,
|
|
|
|
cuid_t cuid)
|
2008-09-19 14:11:41 +00:00
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
2009-05-29 12:32:17 +00:00
|
|
|
#endif // ENABLE_BITTORRENT
|
|
|
|
|
2009-12-05 11:35:18 +00:00
|
|
|
bool UnknownLengthPieceStorage::hasMissingUnusedPiece()
|
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Piece> UnknownLengthPieceStorage::getMissingPiece
|
2011-07-15 15:58:41 +00:00
|
|
|
(size_t minSplitSize,
|
|
|
|
const unsigned char* ignoreBitfield,
|
|
|
|
size_t length,
|
|
|
|
cuid_t cuid)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
if(downloadFinished_) {
|
2013-07-04 12:44:09 +00:00
|
|
|
return nullptr;
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!piece_) {
|
2014-07-15 14:48:32 +00:00
|
|
|
piece_ = std::make_shared<Piece>();
|
2010-06-21 13:51:56 +00:00
|
|
|
return piece_;
|
2007-10-11 16:58:24 +00:00
|
|
|
} else {
|
2013-07-04 12:44:09 +00:00
|
|
|
return nullptr;
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Piece> UnknownLengthPieceStorage::getMissingPiece
|
2011-07-15 15:58:41 +00:00
|
|
|
(size_t index,
|
|
|
|
cuid_t cuid)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
|
|
|
if(index == 0) {
|
2013-08-20 16:57:17 +00:00
|
|
|
return getMissingPiece(0, nullptr, 0, cuid);
|
2007-10-11 16:58:24 +00:00
|
|
|
} else {
|
2013-07-04 12:44:09 +00:00
|
|
|
return nullptr;
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Piece> UnknownLengthPieceStorage::getPiece(size_t index)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
|
|
|
if(index == 0) {
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!piece_) {
|
2014-07-15 14:48:32 +00:00
|
|
|
return std::make_shared<Piece>();
|
2007-10-11 16:58:24 +00:00
|
|
|
} else {
|
2010-06-21 13:51:56 +00:00
|
|
|
return piece_;
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-07-04 12:44:09 +00:00
|
|
|
return nullptr;
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
void UnknownLengthPieceStorage::completePiece(const std::shared_ptr<Piece>& piece)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2010-11-11 07:33:43 +00:00
|
|
|
if(*piece_ == *piece) {
|
2010-06-21 13:51:56 +00:00
|
|
|
downloadFinished_ = true;
|
|
|
|
totalLength_ = piece_->getLength();
|
|
|
|
diskAdaptor_->setTotalLength(totalLength_);
|
|
|
|
piece_.reset();
|
2014-07-15 14:36:10 +00:00
|
|
|
|
|
|
|
createBitfield();
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-15 15:58:41 +00:00
|
|
|
void UnknownLengthPieceStorage::cancelPiece
|
2013-06-21 16:10:38 +00:00
|
|
|
(const std::shared_ptr<Piece>& piece,
|
2011-07-15 15:58:41 +00:00
|
|
|
cuid_t cuid)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2010-11-11 07:33:43 +00:00
|
|
|
if(*piece_ == *piece) {
|
2010-06-21 13:51:56 +00:00
|
|
|
piece_.reset();
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-08 08:04:28 +00:00
|
|
|
bool UnknownLengthPieceStorage::hasPiece(size_t index)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
if(index == 0 && downloadFinished_) {
|
2007-10-11 16:58:24 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-08 08:04:28 +00:00
|
|
|
bool UnknownLengthPieceStorage::isPieceUsed(size_t index)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2010-11-12 12:48:48 +00:00
|
|
|
if(index == 0 && piece_) {
|
2007-10-11 16:58:24 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<DiskAdaptor> UnknownLengthPieceStorage::getDiskAdaptor()
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return diskAdaptor_;
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
|
2011-12-07 15:03:25 +00:00
|
|
|
int32_t UnknownLengthPieceStorage::getPieceLength(size_t index)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2014-07-15 14:36:10 +00:00
|
|
|
// TODO Basically, PieceStorage::getPieceLength() is only used by
|
|
|
|
// BitTorrent, and it does not use UnknownLengthPieceStorage.
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnknownLengthPieceStorage::createBitfield()
|
|
|
|
{
|
|
|
|
if(totalLength_ > 0) {
|
|
|
|
bitfield_ = make_unique<BitfieldMan>(downloadContext_->getPieceLength(),
|
|
|
|
totalLength_);
|
|
|
|
bitfield_->setAllBit();
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnknownLengthPieceStorage::markAllPiecesDone()
|
|
|
|
{
|
2010-11-12 12:48:48 +00:00
|
|
|
if(piece_) {
|
2010-06-21 13:51:56 +00:00
|
|
|
totalLength_ = piece_->getLength();
|
|
|
|
piece_.reset();
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
2014-07-15 14:36:10 +00:00
|
|
|
|
|
|
|
createBitfield();
|
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
downloadFinished_ = true;
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
|
2012-06-25 14:35:24 +00:00
|
|
|
void UnknownLengthPieceStorage::markPiecesDone(int64_t length)
|
2008-03-15 04:19:46 +00:00
|
|
|
{
|
|
|
|
// TODO not implemented yet
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnknownLengthPieceStorage::markPieceMissing(size_t index)
|
|
|
|
{
|
|
|
|
// TODO not implemented yet
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
void UnknownLengthPieceStorage::getInFlightPieces
|
2013-06-21 16:10:38 +00:00
|
|
|
(std::vector<std::shared_ptr<Piece> >& pieces)
|
2008-05-11 14:42:34 +00:00
|
|
|
{}
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
void UnknownLengthPieceStorage::setDiskWriterFactory
|
2013-06-21 16:10:38 +00:00
|
|
|
(const std::shared_ptr<DiskWriterFactory>& diskWriterFactory)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
diskWriterFactory_ = diskWriterFactory;
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2014-07-15 14:36:10 +00:00
|
|
|
const unsigned char* UnknownLengthPieceStorage::getBitfield()
|
|
|
|
{
|
|
|
|
if(bitfield_) {
|
|
|
|
return bitfield_->getBitfield();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t UnknownLengthPieceStorage::getBitfieldLength()
|
|
|
|
{
|
|
|
|
if(bitfield_) {
|
|
|
|
return bitfield_->getBitfieldLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|