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
|
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-02-17 13:35:04 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#ifndef _D_SEGMENT_MAN_H_
|
|
|
|
#define _D_SEGMENT_MAN_H_
|
|
|
|
|
|
|
|
#include "common.h"
|
2009-03-19 13:42:10 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
#include <deque>
|
2009-03-19 13:42:10 +00:00
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "SharedHandle.h"
|
|
|
|
#include "TimeA2.h"
|
|
|
|
#include "Command.h"
|
2009-06-23 15:35:45 +00:00
|
|
|
#include "BitfieldMan.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
|
|
|
class Segment;
|
|
|
|
class Logger;
|
|
|
|
class Option;
|
|
|
|
class PeerStat;
|
|
|
|
class DownloadContext;
|
|
|
|
class PieceStorage;
|
|
|
|
class Piece;
|
2009-06-23 15:35:45 +00:00
|
|
|
class FileEntry;
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2006-09-19 14:52:59 +00:00
|
|
|
class SegmentEntry {
|
|
|
|
public:
|
2009-03-19 13:54:09 +00:00
|
|
|
cuid_t cuid;
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<Segment> segment;
|
2006-09-19 14:52:59 +00:00
|
|
|
public:
|
2009-03-19 13:54:09 +00:00
|
|
|
SegmentEntry(cuid_t cuid, const SharedHandle<Segment>& segment);
|
2007-10-11 16:58:24 +00:00
|
|
|
|
|
|
|
~SegmentEntry();
|
2006-09-19 14:52:59 +00:00
|
|
|
};
|
|
|
|
|
2006-09-21 06:56:54 +00:00
|
|
|
typedef SharedHandle<SegmentEntry> SegmentEntryHandle;
|
2008-02-08 15:53:45 +00:00
|
|
|
typedef std::deque<SegmentEntryHandle> SegmentEntries;
|
2006-09-19 14:52:59 +00:00
|
|
|
|
2006-02-17 13:35:04 +00:00
|
|
|
/**
|
|
|
|
* This class holds the download progress of the one download entry.
|
|
|
|
*/
|
|
|
|
class SegmentMan {
|
|
|
|
private:
|
2007-10-11 16:58:24 +00:00
|
|
|
const Option* _option;
|
|
|
|
|
2008-04-26 05:58:49 +00:00
|
|
|
Logger* logger;
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<DownloadContext> _downloadContext;
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<PieceStorage> _pieceStorage;
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2006-09-19 14:52:59 +00:00
|
|
|
SegmentEntries usedSegmentEntries;
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2009-06-23 15:35:45 +00:00
|
|
|
// Remember writtenLength for each segment. The key is an index of a
|
|
|
|
// segment. The value is writtenLength for that segment.
|
|
|
|
std::map<size_t, size_t> _segmentWrittenLengthMemo;
|
|
|
|
|
2009-07-02 16:26:04 +00:00
|
|
|
// Used for calculating download speed.
|
2008-02-08 15:53:45 +00:00
|
|
|
std::deque<SharedHandle<PeerStat> > peerStats;
|
2006-04-17 16:17:20 +00:00
|
|
|
|
2009-07-02 16:26:04 +00:00
|
|
|
// Keep track of fastest PeerStat for each server
|
|
|
|
std::deque<SharedHandle<PeerStat> > _fastestPeerStats;
|
|
|
|
|
2009-03-19 13:42:10 +00:00
|
|
|
// key: PeerStat's cuid, value: its download speed
|
2009-03-19 13:54:09 +00:00
|
|
|
std::map<cuid_t, unsigned int> _peerStatDlspdMap;
|
2009-03-19 13:42:10 +00:00
|
|
|
|
|
|
|
Time _lastPeerStatDlspdMapUpdated;
|
|
|
|
|
2009-03-20 13:29:33 +00:00
|
|
|
unsigned int _cachedDlspd;
|
|
|
|
|
2009-06-23 15:35:45 +00:00
|
|
|
BitfieldMan _ignoreBitfield;
|
|
|
|
|
2009-03-19 13:54:09 +00:00
|
|
|
SharedHandle<Segment> checkoutSegment(cuid_t cuid,
|
2010-01-05 16:01:46 +00:00
|
|
|
const SharedHandle<Piece>& piece);
|
2009-06-30 17:03:57 +00:00
|
|
|
|
|
|
|
void cancelSegment(const SharedHandle<Segment>& segment);
|
2006-02-17 13:35:04 +00:00
|
|
|
public:
|
2007-10-11 16:58:24 +00:00
|
|
|
SegmentMan(const Option* option,
|
2010-01-05 16:01:46 +00:00
|
|
|
const SharedHandle<DownloadContext>& downloadContext,
|
|
|
|
const SharedHandle<PieceStorage>& pieceStorage);
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
~SegmentMan();
|
2006-06-12 16:55:08 +00:00
|
|
|
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2006-06-12 16:55:08 +00:00
|
|
|
// Initializes totalSize, isSplittable, downloadStarted, errors.
|
2006-04-19 17:23:58 +00:00
|
|
|
// Clears command queue. Also, closes diskWriter.
|
2006-04-18 17:06:17 +00:00
|
|
|
void init();
|
|
|
|
|
2006-02-17 13:35:04 +00:00
|
|
|
/**
|
2007-10-11 16:58:24 +00:00
|
|
|
* The total number of bytes to download.
|
|
|
|
* If Transfer-Encoding is Chunked or Content-Length header is not provided,
|
|
|
|
* then this value is set to be 0.
|
2006-02-17 13:35:04 +00:00
|
|
|
*/
|
2008-03-08 10:33:56 +00:00
|
|
|
uint64_t getTotalLength() const;
|
2006-02-17 13:35:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returs true when the download has finished.
|
|
|
|
* If downloadStarted is false or the number of the segments of this object
|
|
|
|
* holds is 0, then returns false.
|
|
|
|
*/
|
2007-10-11 16:58:24 +00:00
|
|
|
bool downloadFinished() const;
|
|
|
|
|
2006-02-17 13:35:04 +00:00
|
|
|
/**
|
2008-05-11 10:46:52 +00:00
|
|
|
* Fill segments which are assigned to the command whose CUID is cuid.
|
|
|
|
* This function doesn't clear passed segments.
|
2006-09-19 14:52:59 +00:00
|
|
|
*/
|
2008-05-11 10:46:52 +00:00
|
|
|
void getInFlightSegment(std::deque<SharedHandle<Segment> >& segments,
|
2010-01-05 16:01:46 +00:00
|
|
|
cuid_t cuid);
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2009-03-19 13:54:09 +00:00
|
|
|
SharedHandle<Segment> getSegment(cuid_t cuid);
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2009-06-30 17:03:57 +00:00
|
|
|
// Checkouts segments in the range of fileEntry and push back to
|
|
|
|
// segments until segments.size() < maxSegments holds false
|
|
|
|
void getSegment(std::deque<SharedHandle<Segment> >& segments,
|
2010-01-05 16:01:46 +00:00
|
|
|
cuid_t cuid,
|
|
|
|
const SharedHandle<FileEntry>& fileEntry,
|
|
|
|
size_t maxSegments);
|
2009-06-30 17:03:57 +00:00
|
|
|
|
2006-09-19 14:52:59 +00:00
|
|
|
/**
|
|
|
|
* Returns a segment whose index is index.
|
|
|
|
* If it has already assigned
|
|
|
|
* to another cuid or has been downloaded, then returns a segment instance
|
|
|
|
* whose isNull call is true.
|
|
|
|
*/
|
2009-03-19 13:54:09 +00:00
|
|
|
SharedHandle<Segment> getSegment(cuid_t cuid, size_t index);
|
2006-09-19 14:52:59 +00:00
|
|
|
/**
|
|
|
|
* Updates download status.
|
|
|
|
*/
|
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 updateSegment(int cuid, const Segment& segment);
|
2006-09-19 14:52:59 +00:00
|
|
|
/**
|
|
|
|
* Cancels all the segment which the command having given cuid
|
|
|
|
* uses.
|
|
|
|
*/
|
2009-03-19 13:54:09 +00:00
|
|
|
void cancelSegment(cuid_t cuid);
|
2009-06-30 17:03:57 +00:00
|
|
|
|
|
|
|
void cancelSegment(cuid_t cuid, const SharedHandle<Segment>& segment);
|
|
|
|
|
2006-09-19 14:52:59 +00:00
|
|
|
/**
|
|
|
|
* Tells SegmentMan that the segment has been downloaded successfully.
|
|
|
|
*/
|
2009-03-19 13:54:09 +00:00
|
|
|
bool completeSegment(cuid_t cuid, const SharedHandle<Segment>& segment);
|
2007-10-11 16:58:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Injects PieceStorage.
|
|
|
|
*/
|
2008-02-08 15:53:45 +00:00
|
|
|
void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2006-09-19 14:52:59 +00:00
|
|
|
/**
|
2007-10-11 16:58:24 +00:00
|
|
|
* Injects DownloadContext.
|
2006-09-19 14:52:59 +00:00
|
|
|
*/
|
2008-02-08 15:53:45 +00:00
|
|
|
void setDownloadContext(const SharedHandle<DownloadContext>& downloadContext);
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2006-09-19 14:52:59 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the segment whose index is index has been downloaded.
|
|
|
|
*/
|
2008-03-08 10:33:56 +00:00
|
|
|
bool hasSegment(size_t index) const;
|
2006-09-19 14:52:59 +00:00
|
|
|
/**
|
|
|
|
* Returns the length of bytes downloaded.
|
|
|
|
*/
|
2008-03-08 10:33:56 +00:00
|
|
|
uint64_t getDownloadLength() const;
|
2006-09-19 14:52:59 +00:00
|
|
|
|
2009-07-02 16:26:04 +00:00
|
|
|
|
|
|
|
// If there is inactive PeerStat in peerStats, it is replaced with
|
|
|
|
// given peerStat. If no such PeerStat exist, the given peerStat is
|
|
|
|
// inserted.
|
2009-06-29 15:18:21 +00:00
|
|
|
void registerPeerStat(const SharedHandle<PeerStat>& peerStat);
|
2006-09-19 14:52:59 +00:00
|
|
|
|
2009-05-29 12:12:22 +00:00
|
|
|
const std::deque<SharedHandle<PeerStat> >& getPeerStats() const
|
|
|
|
{
|
|
|
|
return peerStats;
|
|
|
|
}
|
2008-08-04 17:06:47 +00:00
|
|
|
|
2009-07-02 16:26:04 +00:00
|
|
|
// If there is slower PeerStat than given peerStat for the same
|
|
|
|
// hostname and protocol in _fastestPeerStats, the former is
|
|
|
|
// replaced with latter. If there are no PeerStat with same hostname
|
|
|
|
// and protocol with given peerStat, given peerStat is inserted.
|
|
|
|
void updateFastestPeerStat(const SharedHandle<PeerStat>& peerStat);
|
|
|
|
|
|
|
|
const std::deque<SharedHandle<PeerStat> >& getFastestPeerStats() const
|
|
|
|
{
|
|
|
|
return _fastestPeerStats;
|
|
|
|
}
|
|
|
|
|
2006-09-19 14:52:59 +00:00
|
|
|
/**
|
|
|
|
* Returns current download speed in bytes per sec.
|
2006-02-17 13:35:04 +00:00
|
|
|
*/
|
2009-03-19 13:42:10 +00:00
|
|
|
unsigned int calculateDownloadSpeed();
|
|
|
|
|
|
|
|
void updateDownloadSpeedFor(const SharedHandle<PeerStat>& pstat);
|
2007-01-11 16:32:31 +00:00
|
|
|
|
2008-09-10 14:56:44 +00:00
|
|
|
/**
|
|
|
|
* Returns the downloaded bytes in this session.
|
|
|
|
*/
|
|
|
|
uint64_t calculateSessionDownloadLength() const;
|
|
|
|
|
2008-03-08 10:33:56 +00:00
|
|
|
size_t countFreePieceFrom(size_t index) const;
|
2009-06-23 15:35:45 +00:00
|
|
|
|
|
|
|
// Excludes segments that fileEntry covers from segment selection.
|
|
|
|
void ignoreSegmentFor(const SharedHandle<FileEntry>& fileEntry);
|
|
|
|
|
|
|
|
// Includes segments that fileEntry covers in segment selection.
|
|
|
|
void recognizeSegmentFor(const SharedHandle<FileEntry>& fileEntry);
|
2009-07-13 15:02:32 +00:00
|
|
|
|
|
|
|
bool allSegmentsIgnored() const;
|
2006-02-17 13:35:04 +00:00
|
|
|
};
|
|
|
|
|
2007-05-20 13:51:52 +00:00
|
|
|
typedef SharedHandle<SegmentMan> SegmentManHandle;
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|
|
|
|
|
2006-02-17 13:35:04 +00:00
|
|
|
#endif // _D_SEGMENT_MAN_H_
|