2006-02-17 13:35:04 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - a simple utility for downloading files faster
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#ifndef _D_SEGMENT_MAN_H_
|
|
|
|
#define _D_SEGMENT_MAN_H_
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "Logger.h"
|
|
|
|
#include "Segment.h"
|
2006-02-22 11:18:47 +00:00
|
|
|
#include "Option.h"
|
2006-04-19 17:23:58 +00:00
|
|
|
#include "DiskWriter.h"
|
2006-07-03 14:19:23 +00:00
|
|
|
#include "Request.h"
|
2006-09-19 14:52:59 +00:00
|
|
|
#include "BitfieldMan.h"
|
|
|
|
#include "PeerStat.h"
|
2006-02-17 13:35:04 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#define SEGMENT_FILE_EXTENSION ".aria2"
|
|
|
|
|
2006-09-19 14:52:59 +00:00
|
|
|
class SegmentEntry {
|
|
|
|
public:
|
|
|
|
int cuid;
|
|
|
|
Segment segment;
|
|
|
|
public:
|
|
|
|
SegmentEntry(int cuid, const Segment& segment)
|
|
|
|
:cuid(cuid), segment(segment) {}
|
|
|
|
~SegmentEntry() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef deque<SegmentEntry> SegmentEntries;
|
|
|
|
typedef deque<PeerStatHandle> PeerStats;
|
|
|
|
|
2006-02-17 13:35:04 +00:00
|
|
|
/**
|
|
|
|
* This class holds the download progress of the one download entry.
|
|
|
|
*/
|
|
|
|
class SegmentMan {
|
|
|
|
private:
|
2006-04-17 16:17:20 +00:00
|
|
|
const Logger* logger;
|
2006-09-19 14:52:59 +00:00
|
|
|
BitfieldMan* bitfield;
|
|
|
|
SegmentEntries usedSegmentEntries;
|
|
|
|
PeerStats peerStats;
|
2006-04-17 16:17:20 +00:00
|
|
|
|
2006-02-17 13:35:04 +00:00
|
|
|
void read(FILE* file);
|
2006-04-19 17:49:03 +00:00
|
|
|
FILE* openSegFile(const string& segFilename, const string& mode) const;
|
2006-09-19 14:52:59 +00:00
|
|
|
bool onNullBitfield(Segment& segment, int cuid);
|
|
|
|
Segment checkoutSegment(int cuid, int index);
|
2006-02-17 13:35:04 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
long long int totalSize;
|
|
|
|
/**
|
|
|
|
* Represents whether this download is splittable.
|
|
|
|
* In Split download(or segmented download), http client establishes
|
|
|
|
* more than one connections to the server, and downloads sevral parts of
|
|
|
|
* a file at the same time. This boosts download speed.
|
|
|
|
* This value is true by default. If total number of bytes is not known or
|
|
|
|
* Chunked transfer encoding is used, then this value is set to be 0 by
|
|
|
|
* DownloadCommand class.
|
|
|
|
*/
|
|
|
|
bool isSplittable;
|
|
|
|
/**
|
|
|
|
* Represents whether the download is start or not.
|
|
|
|
* The default value is false.
|
|
|
|
*/
|
|
|
|
bool downloadStarted;
|
|
|
|
/**
|
|
|
|
* Respresents the file name of the downloaded file.
|
|
|
|
* If the URL does not contain file name part(http://www.rednoah.com/, for
|
|
|
|
* example), this value may be 0 length string.
|
|
|
|
* The default value is 0 length string.
|
|
|
|
*/
|
|
|
|
string filename;
|
|
|
|
/**
|
|
|
|
* directory to store a file
|
|
|
|
*/
|
|
|
|
string dir;
|
|
|
|
/**
|
|
|
|
* User defined file name for downloaded content
|
|
|
|
*/
|
|
|
|
string ufilename;
|
|
|
|
|
2006-06-12 16:55:08 +00:00
|
|
|
/**
|
|
|
|
* Represents the number of failures(usually, DlAbortEx) in downloads.
|
|
|
|
*/
|
|
|
|
int errors;
|
|
|
|
|
2006-02-22 11:18:47 +00:00
|
|
|
const Option* option;
|
2006-04-19 17:23:58 +00:00
|
|
|
DiskWriter* diskWriter;
|
2006-07-03 14:19:23 +00:00
|
|
|
Requests reserved;
|
2006-02-17 13:35:04 +00:00
|
|
|
|
|
|
|
SegmentMan();
|
|
|
|
~SegmentMan();
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Returns dir+"/"+filename.
|
|
|
|
* If filename is empty, then returns dir+"/"+"inex.html";
|
|
|
|
*/
|
2006-02-21 12:27:17 +00:00
|
|
|
string getFilePath() const {
|
2006-02-17 13:35:04 +00:00
|
|
|
return (dir == "" ? "." : dir)+"/"+
|
|
|
|
(ufilename == "" ?
|
|
|
|
(filename == "" ? "index.html" : filename) : ufilename);
|
|
|
|
}
|
|
|
|
|
2006-02-21 12:27:17 +00:00
|
|
|
string getSegmentFilePath() const {
|
2006-02-17 13:35:04 +00:00
|
|
|
return getFilePath()+SEGMENT_FILE_EXTENSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true only if the segment data file exists.
|
|
|
|
* The file name of the segment data is filename appended by ".aria2".
|
|
|
|
* If isSplittable is false, then returns simply false without any operation.
|
|
|
|
*/
|
2006-02-21 12:27:17 +00:00
|
|
|
bool segmentFileExists() const;
|
2006-02-17 13:35:04 +00:00
|
|
|
/**
|
|
|
|
* Loads the segment data file.
|
|
|
|
* If isSplittable is false, then returns without any operation.
|
|
|
|
*/
|
|
|
|
void load();
|
|
|
|
/**
|
|
|
|
* Saves the segment data file.
|
|
|
|
* If isSplittable is false, then returns without any operation.
|
|
|
|
*/
|
2006-02-21 12:27:17 +00:00
|
|
|
void save() const;
|
2006-02-17 13:35:04 +00:00
|
|
|
/**
|
|
|
|
* Removes the segment data file.
|
|
|
|
* If isSplittable is false, then returns without any operation.
|
|
|
|
*/
|
2006-02-21 12:27:17 +00:00
|
|
|
void remove() 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.
|
|
|
|
*/
|
2006-02-21 12:27:17 +00:00
|
|
|
bool finished() const;
|
2006-02-17 13:35:04 +00:00
|
|
|
/**
|
|
|
|
* if finished() is true, then call remove()
|
|
|
|
*/
|
2006-02-21 12:27:17 +00:00
|
|
|
void removeIfFinished() const;
|
2006-02-17 13:35:04 +00:00
|
|
|
/**
|
2006-09-19 14:52:59 +00:00
|
|
|
* Returns a vacant segment.
|
|
|
|
* If there is no vacant segment, then returns a segment instance whose
|
|
|
|
* isNull call is true.
|
|
|
|
*/
|
|
|
|
bool getSegment(Segment& segment, int cuid);
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
bool getSegment(Segment& segment, int cuid, int index);
|
|
|
|
/**
|
|
|
|
* Updates download status.
|
|
|
|
*/
|
|
|
|
bool updateSegment(int cuid, const Segment& segment);
|
|
|
|
/**
|
|
|
|
* Cancels all the segment which the command having given cuid
|
|
|
|
* uses.
|
|
|
|
*/
|
|
|
|
void cancelSegment(int cuid);
|
|
|
|
/**
|
|
|
|
* Tells SegmentMan that the segment has been downloaded successfully.
|
|
|
|
*/
|
|
|
|
bool completeSegment(int cuid, const Segment& segment);
|
|
|
|
/**
|
|
|
|
* Initializes bitfield with the provided length parameters.
|
|
|
|
*/
|
|
|
|
void initBitfield(int segmentLength, long long int totalLength);
|
|
|
|
/**
|
|
|
|
* Returns true if the segment whose index is index has been downloaded.
|
|
|
|
*/
|
|
|
|
bool hasSegment(int index) const;
|
|
|
|
/**
|
|
|
|
* Returns the length of bytes downloaded.
|
|
|
|
*/
|
|
|
|
long long int getDownloadLength() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers given peerStat if it has not been registerd.
|
|
|
|
* Otherwise does nothing.
|
|
|
|
*/
|
|
|
|
void registerPeerStat(const PeerStatHandle& peerStat);
|
|
|
|
|
|
|
|
class FindPeerStat {
|
|
|
|
private:
|
|
|
|
int cuid;
|
|
|
|
public:
|
|
|
|
FindPeerStat(int cuid):cuid(cuid) {}
|
|
|
|
|
|
|
|
bool operator()(const PeerStatHandle& peerStat) {
|
|
|
|
if(peerStat->getCuid() == cuid) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns peerStat whose cuid is given cuid. If it is not found, returns
|
|
|
|
* PeerStatHandle(0).
|
|
|
|
*/
|
|
|
|
PeerStatHandle getPeerStat(int cuid) const {
|
|
|
|
PeerStats::const_iterator itr = find_if(peerStats.begin(), peerStats.end(),
|
|
|
|
FindPeerStat(cuid));
|
|
|
|
if(itr == peerStats.end()) {
|
|
|
|
// TODO
|
|
|
|
return PeerStatHandle(0);
|
|
|
|
} else {
|
|
|
|
return *itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns current download speed in bytes per sec.
|
2006-02-17 13:35:04 +00:00
|
|
|
*/
|
2006-09-19 14:52:59 +00:00
|
|
|
int calculateDownloadSpeed() const;
|
2006-02-17 13:35:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _D_SEGMENT_MAN_H_
|