Added StreamPieceSelector class.

This class abstracts the piece selection algorithm for HTTP/FTP
download.
pull/1/head
Tatsuhiro Tsujikawa 2011-06-11 17:34:17 +09:00
parent 533cdfa850
commit cc82da3bab
11 changed files with 202 additions and 13 deletions

View File

@ -56,6 +56,7 @@
#include "Option.h"
#include "fmt.h"
#include "RarestPieceSelector.h"
#include "DefaultStreamPieceSelector.h"
#include "array_fun.h"
#include "PieceStatMan.h"
#include "wallclock.h"
@ -76,7 +77,8 @@ DefaultPieceStorage::DefaultPieceStorage
endGamePieceNum_(END_GAME_PIECE_NUM),
option_(option),
pieceStatMan_(new PieceStatMan(downloadContext->getNumPieces(), true)),
pieceSelector_(new RarestPieceSelector(pieceStatMan_))
pieceSelector_(new RarestPieceSelector(pieceStatMan_)),
streamPieceSelector_(new DefaultStreamPieceSelector(bitfieldMan_))
{}
DefaultPieceStorage::~DefaultPieceStorage()
@ -338,11 +340,11 @@ bool DefaultPieceStorage::hasMissingUnusedPiece()
return bitfieldMan_->getFirstMissingUnusedIndex(index);
}
SharedHandle<Piece> DefaultPieceStorage::getSparseMissingUnusedPiece
SharedHandle<Piece> DefaultPieceStorage::getMissingPiece
(size_t minSplitSize, const unsigned char* ignoreBitfield, size_t length)
{
size_t index;
if(bitfieldMan_->getSparseMissingUnusedIndex
if(streamPieceSelector_->select
(index, minSplitSize, ignoreBitfield, length)) {
return checkOutPiece(index);
} else {

View File

@ -48,6 +48,7 @@ class DiskWriterFactory;
class FileEntry;
class PieceStatMan;
class PieceSelector;
class StreamPieceSelector;
#define END_GAME_PIECE_NUM 20
@ -85,7 +86,7 @@ private:
SharedHandle<PieceStatMan> pieceStatMan_;
SharedHandle<PieceSelector> pieceSelector_;
SharedHandle<StreamPieceSelector> streamPieceSelector_;
#ifdef ENABLE_BITTORRENT
void getMissingPiece
(std::vector<SharedHandle<Piece> >& pieces,
@ -156,7 +157,7 @@ public:
virtual bool hasMissingUnusedPiece();
virtual SharedHandle<Piece> getSparseMissingUnusedPiece
virtual SharedHandle<Piece> getMissingPiece
(size_t minSplitSize, const unsigned char* ignoreBitfield, size_t length);
virtual SharedHandle<Piece> getMissingPiece(size_t index);

View File

@ -0,0 +1,57 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2011 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* 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 "DefaultStreamPieceSelector.h"
#include "BitfieldMan.h"
namespace aria2 {
DefaultStreamPieceSelector::DefaultStreamPieceSelector
(BitfieldMan* bitfieldMan)
: bitfieldMan_(bitfieldMan)
{}
DefaultStreamPieceSelector::~DefaultStreamPieceSelector() {}
bool DefaultStreamPieceSelector::select
(size_t& index,
size_t minSplitSize,
const unsigned char* ignoreBitfield,
size_t length)
{
return bitfieldMan_->getSparseMissingUnusedIndex
(index, minSplitSize, ignoreBitfield, length);
}
} // namespace aria2

View File

@ -0,0 +1,60 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2011 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* 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 --> */
#ifndef D_DEFAULT_STREAM_PIECE_SELECTOR_H
#define D_DEFAULT_STREAM_PIECE_SELECTOR_H
#include "StreamPieceSelector.h"
namespace aria2 {
class BitfieldMan;
class DefaultStreamPieceSelector:public StreamPieceSelector {
public:
DefaultStreamPieceSelector(BitfieldMan* bitfieldMan);
virtual ~DefaultStreamPieceSelector();
virtual bool select
(size_t& index,
size_t minSplitSize,
const unsigned char* ignoreBitfield,
size_t length);
private:
BitfieldMan* bitfieldMan_;
};
} // namespace aria2
#endif // D_DEFAULT_STREAM_PIECE_SELECTOR_H

View File

@ -221,7 +221,9 @@ SRCS = Socket.h\
HttpListenCommand.cc HttpListenCommand.h\
HttpServerCommand.cc HttpServerCommand.h\
HttpServerResponseCommand.cc HttpServerResponseCommand.h\
HttpServer.cc HttpServer.h
HttpServer.cc HttpServer.h\
StreamPieceSelector.h\
DefaultStreamPieceSelector.cc DefaultStreamPieceSelector.h
if ENABLE_XML_RPC
SRCS += XmlRpcRequestParserController.cc XmlRpcRequestParserController.h\

View File

@ -131,7 +131,7 @@ public:
* Returns a missing piece if available. Otherwise returns 0;
* If ignoreBitfield is set, indexes of true bit are excluded.
*/
virtual SharedHandle<Piece> getSparseMissingUnusedPiece
virtual SharedHandle<Piece> getMissingPiece
(size_t minSplitSize, const unsigned char* ignoreBitfield, size_t length) = 0;
/**

View File

@ -173,7 +173,7 @@ void SegmentMan::getInFlightSegment
SharedHandle<Segment> SegmentMan::getSegment(cuid_t cuid, size_t minSplitSize)
{
SharedHandle<Piece> piece =
pieceStorage_->getSparseMissingUnusedPiece
pieceStorage_->getMissingPiece
(minSplitSize,
ignoreBitfield_.getFilterBitfield(), ignoreBitfield_.getBitfieldLength());
return checkoutSegment(cuid, piece);
@ -193,7 +193,7 @@ void SegmentMan::getSegment
while(segments.size() < maxSegments) {
SharedHandle<Segment> segment =
checkoutSegment(cuid,
pieceStorage_->getSparseMissingUnusedPiece
pieceStorage_->getMissingPiece
(minSplitSize,
filter.getFilterBitfield(), filter.getBitfieldLength()));
if(!segment) {

67
src/StreamPieceSelector.h Normal file
View File

@ -0,0 +1,67 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2011 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* 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 --> */
#ifndef D_STREAM_PIECE_SELECTOR_H
#define D_STREAM_PIECE_SELECTOR_H
#include "common.h"
#include <cstdlib>
namespace aria2 {
// Select piece for HTTP/FTP download. For BitTorrent downloads, use
// PieceSelector interface.
class StreamPieceSelector {
public:
virtual ~StreamPieceSelector() {}
// Selected piece index will be stored in index. To ignore some
// piece indexes from selection, use ignoreBitfield and set
// corresponding bit. length is the number of byte of
// ignoreBitfield. The number of bits in ignoreBitfield must be
// equal to the number of blocks in BitfieldMan. If piece index is
// selected, returns true. Otherwise, returns false. minSplitSize
// is a advice to derived class not to select piece such which the
// distance between it and already selected piece is less than
// minSplitSize.
virtual bool select
(size_t& index,
size_t minSplitSize,
const unsigned char* ignoreBitfield,
size_t length) = 0;
};
} // namespace aria2
#endif // D_STREAM_PIECE_SELECTOR_H

View File

@ -129,7 +129,7 @@ bool UnknownLengthPieceStorage::hasMissingUnusedPiece()
abort();
}
SharedHandle<Piece> UnknownLengthPieceStorage::getSparseMissingUnusedPiece
SharedHandle<Piece> UnknownLengthPieceStorage::getMissingPiece
(size_t minSplitSize, const unsigned char* ignoreBitfield, size_t length)
{
if(downloadFinished_) {
@ -146,7 +146,7 @@ SharedHandle<Piece> UnknownLengthPieceStorage::getSparseMissingUnusedPiece
SharedHandle<Piece> UnknownLengthPieceStorage::getMissingPiece(size_t index)
{
if(index == 0) {
return getSparseMissingUnusedPiece(0, 0, 0);
return getMissingPiece(0, 0, 0);
} else {
return SharedHandle<Piece>();
}

View File

@ -106,7 +106,7 @@ public:
/**
* Returns a missing piece if available. Otherwise returns 0;
*/
virtual SharedHandle<Piece> getSparseMissingUnusedPiece
virtual SharedHandle<Piece> getMissingPiece
(size_t minSplitSize, const unsigned char* ignoreBitfield, size_t length);
/**

View File

@ -87,7 +87,7 @@ public:
return false;
}
virtual SharedHandle<Piece> getSparseMissingUnusedPiece
virtual SharedHandle<Piece> getMissingPiece
(size_t minSplitSize, const unsigned char* ignoreBitfield, size_t length)
{
return SharedHandle<Piece>(new Piece());