2006-02-17 13:35:04 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-02-17 13:35:04 +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
|
2006-09-21 15:31:24 +00:00
|
|
|
* 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.
|
2006-02-17 13:35:04 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "SegmentMan.h"
|
|
|
|
#include "Util.h"
|
|
|
|
#include "message.h"
|
2006-02-22 11:18:47 +00:00
|
|
|
#include "prefs.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "PiecedSegment.h"
|
|
|
|
#include "GrowSegment.h"
|
2006-04-17 16:17:20 +00:00
|
|
|
#include "LogFactory.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Logger.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "PieceStorage.h"
|
|
|
|
#include "PeerStat.h"
|
|
|
|
#include "Option.h"
|
|
|
|
#include "DownloadContext.h"
|
|
|
|
#include "Piece.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
SegmentEntry::SegmentEntry(int32_t cuid, const SegmentHandle& segment):
|
|
|
|
cuid(cuid), segment(segment) {}
|
|
|
|
|
|
|
|
SegmentEntry::~SegmentEntry() {}
|
|
|
|
|
|
|
|
SegmentMan::SegmentMan(const Option* option,
|
|
|
|
const DownloadContextHandle& downloadContext,
|
|
|
|
const PieceStorageHandle& pieceStorage):
|
|
|
|
_option(option),
|
|
|
|
logger(LogFactory::getInstance()),
|
|
|
|
_downloadContext(downloadContext),
|
|
|
|
_pieceStorage(pieceStorage)
|
2007-01-28 14:18:35 +00:00
|
|
|
{}
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
SegmentMan::~SegmentMan() {}
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
bool SegmentMan::downloadFinished() const
|
|
|
|
{
|
|
|
|
if(_pieceStorage.isNull()) {
|
2006-02-17 13:35:04 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
2007-10-11 16:58:24 +00:00
|
|
|
return _pieceStorage->downloadFinished();
|
2007-09-01 16:10:30 +00:00
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void SegmentMan::init()
|
|
|
|
{
|
|
|
|
// TODO Do we have to do something about DownloadContext and PieceStorage here?
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
int64_t SegmentMan::getTotalLength() const
|
|
|
|
{
|
|
|
|
if(_pieceStorage.isNull()) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return _pieceStorage->getTotalLength();
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void SegmentMan::setPieceStorage(const PieceStorageHandle& pieceStorage)
|
|
|
|
{
|
|
|
|
_pieceStorage = pieceStorage;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void SegmentMan::setDownloadContext(const DownloadContextHandle& downloadContext)
|
|
|
|
{
|
|
|
|
_downloadContext = downloadContext;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
SegmentHandle SegmentMan::checkoutSegment(int32_t cuid,
|
|
|
|
const PieceHandle& piece)
|
|
|
|
{
|
|
|
|
if(piece.isNull()) {
|
|
|
|
return 0;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
logger->debug("Attach segment#%d to CUID#%d.", piece->getIndex(), cuid);
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
SegmentHandle segment = 0;
|
|
|
|
if(piece->getLength() == 0) {
|
|
|
|
segment = new GrowSegment(piece);
|
2006-09-19 14:52:59 +00:00
|
|
|
} else {
|
2007-10-11 16:58:24 +00:00
|
|
|
segment = new PiecedSegment(_downloadContext->getPieceLength(), piece);
|
2006-09-19 14:52:59 +00:00
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
SegmentEntryHandle entry = new SegmentEntry(cuid, segment);
|
|
|
|
usedSegmentEntries.push_back(entry);
|
|
|
|
|
2006-09-19 14:52:59 +00:00
|
|
|
logger->debug("index=%d, length=%d, segmentLength=%d, writtenLength=%d",
|
2007-10-11 16:58:24 +00:00
|
|
|
segment->getIndex(),
|
|
|
|
segment->getLength(),
|
|
|
|
segment->getSegmentLength(),
|
|
|
|
segment->getWrittenLength());
|
2006-09-19 14:52:59 +00:00
|
|
|
return segment;
|
|
|
|
}
|
|
|
|
|
2006-09-21 06:56:54 +00:00
|
|
|
SegmentEntryHandle SegmentMan::findSlowerSegmentEntry(const PeerStatHandle& peerStat) const {
|
2007-07-21 08:56:16 +00:00
|
|
|
int32_t speed = (int32_t)(peerStat->getAvgDownloadSpeed()*0.8);
|
2006-09-21 06:56:54 +00:00
|
|
|
SegmentEntryHandle slowSegmentEntry(0);
|
|
|
|
for(SegmentEntries::const_iterator itr = usedSegmentEntries.begin();
|
2007-01-23 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To add chunk checksum validation:
* src/MetalinkEntry.h
(MetalinkChunkChecksum.h): New include.
(chunkChecksum): New variable.
* src/Request.h
(method): New variable.
(setMethod): New function.
(getMethod): New function.
(METHOD_GET): New static constant.
(METHOD_HEAD): New static constant.
* src/Xml2MetalinkProcessor.h
(getPieceHash): New function.
* src/PieceStorage.h
(markAllPiecesDone): New function.
(checkIntegrity): New function.
* src/FileAllocator.h
(NullFileAllocationMonitor.h): New include.
(FileAllocator): Initialize fileAllocationMonitor with new
NullFileAllocationMonitor().
* src/MultiDiskAdaptor.h
(messageDigest.h): Remove include.
(ctx): Removed.
(hashUpdate): Added ctx.
(MultiDiskAdaptor): Removed ctx.
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/UrlRequestInfo.h
(HeadResult): New class.
(digestAlgo): New variable.
(chunkChecksumLength): New variable.
(chunkChecksums): New variable.
(getHeadResult): New function.
(UrlRequestInfo): Added digestAlgo, chunkChecksumLength.
(setDigestAlgo): New function.
(setChunkChecksumLength): New function.
(setChunkChecksums): New function.
* src/DefaultPieceStorage.cc
(DiskAdaptorWriter.h): New include.
(ChunkChecksumValidator.h): New include.
(markAllPiecesDone): New function.
(checkIntegrity): New function.
* src/DefaultBtContext.h
(getPieceHashes): New function.
* src/TorrentRequestInfo.cc
(execute): Try to validate chunk checksum if file already exists
and
.aria2 file doesn't there and user allows aria2 to overwrite it.
* src/messageDigest.h
(~MessageDigestContext): Added digestFree().
* src/MetalinkRequestInfo.cc
(execute): Set digestAlgo, chunkChecksum, chunkChecksums to
reqInfo.
* src/DiskAdaptor.h
(messageDigest.h): New include.
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/DownloadCommand.h
(PeerStat.h): New include.
(maxDownloadSpeedLimit): New variable.
(startupIdleTime): New variable.
(lowestDownloadSpeedLimit): New variable.
(peerStat): New variable.
(setMaxDownloadSpeedLimit): New function.
(setStartupIdleTime): New function.
(setLowestDownloadSPeedLimit): New function.
* src/BtContext.h
(getPieceHashes): New function.
* src/main.cc
(main): Set PREF_REALTIME_CHUNK_CHECKSUM and
PREF_CHECK_INTEGRITY
option to true for testing purpose.
* src/BtPieceMessage.cc
(checkPieceHash): Use messageDigest
* src/DownloadEngine.cc
(SetDescriptor): Removed.
(AccumulateActiveCommand): Removed.
(waitData): Rewritten.
(updateFdSet): Rewritten.
* src/MultiDiskAdaptor.cc
(hashUpdate): Added ctx.
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/BitfieldMan.h
(isBitRangeSet): New function.
(unsetBitRange): New function.
* src/ByteArrayDiskWriter.h
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/ConsoleDownloadEngine.cc
(calculateStatistics): If nspeed < 0 then set nspeed to 0.
* src/DiskWriter.h
(messageDigest.h): New include.
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/ChunkChecksumValidator.h: New class.
* src/DiskAdaptorWriter.h: New class.
* src/prefs.h
(PREF_REALTIME_CHUNK_CHECKSUM): New definition.
(PREF_CHECK_INTEGRITY): New definition.
* src/HttpResponseCommand.cc
(handleDefaultEncoding): Added method "HEAD" handling.
Removed the call to
e->segmentMan->shouldCancelDownloadForSafety().
(handleOtherEncoding):
Added the call to
e->segmentMan->shouldCancelDownloadForSafety().
(createHttpDownloadCommand): Set maxDownloadSpeedLimit,
startupIdleTime, lowestDownloadSpeedLimit to command.
* src/SegmentMan.h
(getSegmentEntryByIndex): New function.
(getSegmentEntryByCuid): New function.
(getSegmentEntryIteratorByCuid): New function.
(diskWriter): DiskWriter -> DiskWriterHandle
(pieceHashes): New variable.
(chunkHashLength): New variable.
(digestAlgo): New variable.
(FindPeerStat): Removed.
(getPeerStat): Rewritten.
(markAllPiecesDone): New function.
(checkIntegrity): New function.
(tryChunkChecksumValidation): New function.
(isChunkChecksumValidationReady): New function.
* src/BitfieldMan.cc
(BitfieldMan): Initialized bitfieldLength, blocks to 0.
(BitfieldMan): Initialized blockLength, totalLength,
bitfieldLength,
blocks to 0.
(isBitRangeSet): New function.
(unsetBitRange): New function.
* src/FtpNegotiateCommand.cc
(executeInternal): Set maxDownloadSpeedLimit,
startupIdleTime, lowestDownloadSpeedLimit to command.
(recvSize): Added method "HEAD" handling.
Removed the call to
e->segmentMan->shouldCancelDownloadForSafety().
* src/AbstractSingleDiskAdaptor.cc
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/AbstractSingleDiskAdaptor.h
(sha1Sum): Renamed as messageDigest.
(messageDigest): New function.
* src/Util.h
(indexRange): New function.
* src/MetalinkEntry.cc
(MetalinkEntry): Initialized chunkChecksum to 0.
* src/ShaVisitor.cc
(~ShaVisitor): Removed the call to ctx.digestFree().
* src/SegmentMan.cc
(ChunkChecksumValidator.h): New include.
(SegmentMan): Initialized chunkHashLength to 0. Initialized
digestAlgo
to DIGEST_ALGO_SHA1.
(~SegmentMan): Removed diskWriter.
(FindSegmentEntryByIndex): Removed.
(FindSegmentEntryByCuid): Removed.
(checkoutSegment): Rewritten.
(findSlowerSegmentEntry): Rewritten.
(getSegment): Rewritten.
(updateSegment): Rewritten.
(completeSegment): Rewritten.
(markAllPiecesDone): New function.
(checkIntegrity): New function.
(isChunkChecksumValidationReady): New function.
(tryChunkChecksumValidation): New function.
* src/Xml2MetalinkProcessor.cc
(getEntry): Get size and set it to entry.
Get chunk checksum and set it to entry.
(getPieceHash): New function.
* src/Util.cc
(sha1Sum): Removed ctx.digestFree()
(fileChecksum): Removed ctx.digestFree()
(indexRange): New function.
* src/Request.cc
(METHOD_GET): New variable.
(METHOD_HEAD): New variable.
(Request): Added method.
* src/UrlRequestInfo.cc
(FatalException.h): New include.
(message.h): New include.
(operator<<): Added operator<< for class HeadResult.
(getHeadResult): New function.
(execute): Get filename and size in separate download engine.
* src/ChunkChecksumValidator.cc: New class.
* src/DownloadCommand.cc:
(DownloadCommand): Added peerStat.
(executeInternal): Use maxDownloadSpeedLimit member instead of
getting
the value from Option.
The buffer size is now 16KB.
Use peerStat member instead of getting it from SegmentMan.
Use startupIdleTime member instead of gettingit from Option.
Added chunk checksum validation.
* src/AbstractDiskWriter.cc
(AbstractDiskWriter): Removed ctx.
(~AbstractDiskWriter): Removed ctx.digestFree()
(writeDataInternal): Returns the return value of write.
(readDataInternal): Returns the return value of read.
(sha1Sum): Renamed as messageDigest
(messageDigest): New function.
* src/AbstractDiwkWriter.h
(messageDigest.h): Removed include.
(ctx): Removed.
(sha1Sum): Renamed as messageDigest
(messageDigest): New function.
* src/DefaultPieceStorage.h
(markAllPiecesDone): New function.
(checkIntegrity): New function.
* src/NullFileAllocationMonitor.h: New class.
2007-01-24 15:55:34 +00:00
|
|
|
itr != usedSegmentEntries.end(); ++itr) {
|
2006-09-21 06:56:54 +00:00
|
|
|
const SegmentEntryHandle& segmentEntry = *itr;
|
|
|
|
if(segmentEntry->cuid == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
PeerStatHandle p = getPeerStat(segmentEntry->cuid);
|
2006-11-08 16:25:38 +00:00
|
|
|
if(!p.get() || p->getCuid() == peerStat->getCuid() ||
|
|
|
|
p->getStatus() != PeerStat::ACTIVE ||
|
2007-10-11 16:58:24 +00:00
|
|
|
!p->getDownloadStartTime().elapsed(_option->getAsInt(PREF_STARTUP_IDLE_TIME))) {
|
2006-09-21 06:56:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-07-21 08:56:16 +00:00
|
|
|
int32_t pSpeed = p->calculateDownloadSpeed();
|
2006-11-08 16:25:38 +00:00
|
|
|
if(pSpeed < speed) {
|
2006-09-21 06:56:54 +00:00
|
|
|
speed = pSpeed;
|
|
|
|
slowSegmentEntry = segmentEntry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return slowSegmentEntry;
|
|
|
|
}
|
|
|
|
|
2007-10-17 16:26:51 +00:00
|
|
|
Segments SegmentMan::getInFlightSegment(int32_t cuid)
|
|
|
|
{
|
|
|
|
Segments temp;
|
|
|
|
for(SegmentEntries::iterator itr = usedSegmentEntries.begin();
|
|
|
|
itr != usedSegmentEntries.end(); ++itr) {
|
|
|
|
const SegmentEntryHandle& segmentEntry = *itr;
|
|
|
|
if(segmentEntry->cuid == cuid) {
|
|
|
|
temp.push_back(segmentEntry->segment);
|
|
|
|
}
|
2006-09-19 14:52:59 +00:00
|
|
|
}
|
2007-10-17 16:26:51 +00:00
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
SegmentHandle SegmentMan::getSegment(int32_t cuid) {
|
2007-10-11 16:58:24 +00:00
|
|
|
PieceHandle piece = _pieceStorage->getMissingPiece();
|
|
|
|
if(piece.isNull()) {
|
2006-09-21 06:56:54 +00:00
|
|
|
PeerStatHandle myPeerStat = getPeerStat(cuid);
|
2007-10-11 16:58:24 +00:00
|
|
|
if(myPeerStat.isNull()) {
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
return 0;
|
2006-09-21 06:56:54 +00:00
|
|
|
}
|
|
|
|
SegmentEntryHandle slowSegmentEntry = findSlowerSegmentEntry(myPeerStat);
|
|
|
|
if(slowSegmentEntry.get()) {
|
2007-07-20 17:06:21 +00:00
|
|
|
logger->info(MSG_SEGMENT_FORWARDING,
|
2006-09-21 06:56:54 +00:00
|
|
|
slowSegmentEntry->cuid,
|
2007-10-11 16:58:24 +00:00
|
|
|
slowSegmentEntry->segment->getIndex(),
|
2006-09-21 06:56:54 +00:00
|
|
|
cuid);
|
|
|
|
PeerStatHandle slowPeerStat = getPeerStat(slowSegmentEntry->cuid);
|
|
|
|
slowPeerStat->requestIdle();
|
|
|
|
cancelSegment(slowSegmentEntry->cuid);
|
2007-10-11 16:58:24 +00:00
|
|
|
return checkoutSegment(cuid, slowSegmentEntry->segment->getPiece());
|
2006-09-21 06:56:54 +00:00
|
|
|
} else {
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
return 0;
|
2006-09-21 06:56:54 +00:00
|
|
|
}
|
2006-09-19 14:52:59 +00:00
|
|
|
} else {
|
2007-10-11 16:58:24 +00:00
|
|
|
return checkoutSegment(cuid, piece);
|
2006-09-19 14:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
SegmentHandle SegmentMan::getSegment(int32_t cuid, int32_t index) {
|
2007-10-11 16:58:24 +00:00
|
|
|
if(index < 0 || _downloadContext->getNumPieces() <= index) {
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
return 0;
|
2006-09-19 14:52:59 +00:00
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
return checkoutSegment(cuid, _pieceStorage->getMissingPiece(index));
|
2006-09-19 14:52:59 +00:00
|
|
|
}
|
|
|
|
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
void SegmentMan::cancelSegment(int32_t cuid) {
|
2007-10-11 16:58:24 +00:00
|
|
|
for(SegmentEntries::iterator itr = usedSegmentEntries.begin();
|
2007-10-17 16:26:51 +00:00
|
|
|
itr != usedSegmentEntries.end();) {
|
2007-10-11 16:58:24 +00:00
|
|
|
if((*itr)->cuid == cuid) {
|
|
|
|
_pieceStorage->cancelPiece((*itr)->segment->getPiece());
|
2007-10-17 16:26:51 +00:00
|
|
|
itr = usedSegmentEntries.erase(itr);
|
|
|
|
} else {
|
|
|
|
++itr;
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
}
|
2006-09-19 14:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-06 16:37:25 +00:00
|
|
|
class FindSegmentEntry {
|
|
|
|
private:
|
|
|
|
SegmentHandle _segment;
|
|
|
|
public:
|
|
|
|
FindSegmentEntry(const SegmentHandle& segment):_segment(segment) {}
|
|
|
|
|
|
|
|
bool operator()(const SegmentEntryHandle& segmentEntry) const
|
|
|
|
{
|
|
|
|
return segmentEntry->segment->getIndex() == _segment->getIndex();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
bool SegmentMan::completeSegment(int32_t cuid, const SegmentHandle& segment) {
|
2007-10-11 16:58:24 +00:00
|
|
|
_pieceStorage->completePiece(segment->getPiece());
|
|
|
|
_pieceStorage->advertisePiece(cuid, segment->getPiece()->getIndex());
|
2008-02-08 15:53:45 +00:00
|
|
|
SegmentEntries::iterator itr = std::find_if(usedSegmentEntries.begin(),
|
|
|
|
usedSegmentEntries.end(),
|
|
|
|
FindSegmentEntry(segment));
|
2006-09-19 14:52:59 +00:00
|
|
|
if(itr == usedSegmentEntries.end()) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
usedSegmentEntries.erase(itr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
bool SegmentMan::hasSegment(int32_t index) const {
|
2007-10-11 16:58:24 +00:00
|
|
|
return _pieceStorage->hasPiece(index);
|
2006-09-19 14:52:59 +00:00
|
|
|
}
|
|
|
|
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
int64_t SegmentMan::getDownloadLength() const {
|
2007-10-11 16:58:24 +00:00
|
|
|
if(_pieceStorage.isNull()) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return _pieceStorage->getCompletedLength();
|
2006-09-19 14:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SegmentMan::registerPeerStat(const PeerStatHandle& peerStat) {
|
|
|
|
PeerStatHandle temp = getPeerStat(peerStat->getCuid());
|
|
|
|
if(!temp.get()) {
|
|
|
|
peerStats.push_back(peerStat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
PeerStatHandle SegmentMan::getPeerStat(int32_t cuid) const
|
|
|
|
{
|
|
|
|
for(PeerStats::const_iterator itr = peerStats.begin(); itr != peerStats.end(); ++itr) {
|
|
|
|
const PeerStatHandle& peerStat = *itr;
|
|
|
|
if(peerStat->getCuid() == cuid) {
|
|
|
|
return peerStat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-25 16:47:29 +00:00
|
|
|
int32_t SegmentMan::calculateDownloadSpeed() const {
|
2007-07-21 08:56:16 +00:00
|
|
|
int32_t speed = 0;
|
2006-09-19 14:52:59 +00:00
|
|
|
for(PeerStats::const_iterator itr = peerStats.begin();
|
|
|
|
itr != peerStats.end(); itr++) {
|
|
|
|
const PeerStatHandle& peerStat = *itr;
|
|
|
|
if(peerStat->getStatus() == PeerStat::ACTIVE) {
|
|
|
|
speed += peerStat->calculateDownloadSpeed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return speed;
|
2006-04-18 17:06:17 +00:00
|
|
|
}
|
2007-01-11 16:32:31 +00:00
|
|
|
|
2007-10-17 16:26:51 +00:00
|
|
|
int32_t SegmentMan::countFreePieceFrom(int32_t index) const
|
|
|
|
{
|
|
|
|
for(int32_t i = index; i < _downloadContext->getNumPieces(); ++i) {
|
|
|
|
if(_pieceStorage->hasPiece(i) || _pieceStorage->isPieceUsed(i)) {
|
|
|
|
return i-index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _downloadContext->getNumPieces()-index;
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|