123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- /* <!-- 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
- * 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_SEGMENT_MAN_H_
- #define _D_SEGMENT_MAN_H_
- #include "common.h"
- #include "Logger.h"
- #include "Segment.h"
- #include "Option.h"
- #include "DiskWriter.h"
- #include "Request.h"
- #include "BitfieldMan.h"
- #include "PeerStat.h"
- #ifdef ENABLE_MESSAGE_DIGEST
- # include "ChunkChecksum.h"
- #endif // ENABLE_MESSAGE_DIGEST
- using namespace std;
- #define SEGMENT_FILE_EXTENSION ".aria2"
- class SegmentEntry {
- public:
- int32_t cuid;
- SegmentHandle segment;
- public:
- SegmentEntry(int32_t cuid, const SegmentHandle& segment)
- :cuid(cuid), segment(segment) {}
- ~SegmentEntry() {}
- };
- typedef SharedHandle<SegmentEntry> SegmentEntryHandle;
- typedef deque<SegmentEntryHandle> SegmentEntries;
- typedef deque<PeerStatHandle> PeerStats;
- /**
- * This class holds the download progress of the one download entry.
- */
- class SegmentMan {
- private:
- const Logger* logger;
- BitfieldMan* bitfield;
- SegmentEntries usedSegmentEntries;
- PeerStats peerStats;
- void read(FILE* file);
- FILE* openSegFile(const string& segFilename, const string& mode) const;
- SegmentHandle onNullBitfield(int32_t cuid);
- SegmentHandle checkoutSegment(int32_t cuid, int32_t index);
- SegmentEntryHandle findSlowerSegmentEntry(const PeerStatHandle& peerStat) const;
- SegmentEntryHandle getSegmentEntryByIndex(int32_t index) {
- for(SegmentEntries::const_iterator itr = usedSegmentEntries.begin();
- itr != usedSegmentEntries.end(); ++itr) {
- const SegmentEntryHandle& segmentEntry = *itr;
- if(segmentEntry->segment->index == index) {
- return segmentEntry;
- }
- }
- return 0;
- }
-
- SegmentEntryHandle getSegmentEntryByCuid(int32_t cuid) {
- for(SegmentEntries::const_iterator itr = usedSegmentEntries.begin();
- itr != usedSegmentEntries.end(); ++itr) {
- const SegmentEntryHandle& segmentEntry = *itr;
- if(segmentEntry->cuid == cuid) {
- return segmentEntry;
- }
- }
- return 0;
- }
- SegmentEntries::iterator getSegmentEntryIteratorByCuid(int32_t cuid) {
- for(SegmentEntries::iterator itr = usedSegmentEntries.begin();
- itr != usedSegmentEntries.end(); ++itr) {
- const SegmentEntryHandle& segmentEntry = *itr;
- if(segmentEntry->cuid == cuid) {
- return itr;
- }
- }
- return usedSegmentEntries.end();
- }
- 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.
- */
- int64_t 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;
- /**
- * Represents the number of failures(usually, DlAbortEx) in downloads.
- */
- int32_t errors;
- const Option* option;
- DiskWriterHandle diskWriter;
- Requests reserved;
- SegmentMan();
- ~SegmentMan();
-
- // Initializes totalSize, isSplittable, downloadStarted, errors.
- // Clears command queue. Also, closes diskWriter.
- void init();
- /**
- * Returns dir+"/"+filename.
- * If filename is empty, then returns dir+"/"+"inex.html";
- */
- string getFilePath() const {
- return (dir == "" ? "." : dir)+"/"+
- (ufilename == "" ?
- (filename == "" ? "index.html" : filename) : ufilename);
- }
- string getSegmentFilePath() const {
- 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.
- */
- bool segmentFileExists() const;
- /**
- * 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.
- */
- void save() const;
- /**
- * Removes the segment data file.
- * If isSplittable is false, then returns without any operation.
- */
- void remove() const;
- /**
- * 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.
- */
- bool finished() const;
- /**
- * if finished() is true, then call remove()
- */
- void removeIfFinished() const;
- /**
- * Returns a vacant segment.
- * If there is no vacant segment, then returns a segment instance whose
- * isNull call is true.
- */
- SegmentHandle getSegment(int32_t 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.
- */
- SegmentHandle getSegment(int32_t cuid, int32_t index);
- /**
- * Updates download status.
- */
- //bool updateSegment(int cuid, const Segment& segment);
- /**
- * Cancels all the segment which the command having given cuid
- * uses.
- */
- void cancelSegment(int32_t cuid);
- /**
- * Tells SegmentMan that the segment has been downloaded successfully.
- */
- bool completeSegment(int32_t cuid, const SegmentHandle& segment);
- /**
- * Initializes bitfield with the provided length parameters.
- */
- void initBitfield(int32_t segmentLength, int64_t totalLength);
- BitfieldMan* getBitfield() const
- {
- return bitfield;
- }
- /**
- * Returns true if the segment whose index is index has been downloaded.
- */
- bool hasSegment(int32_t index) const;
- /**
- * Returns the length of bytes downloaded.
- */
- int64_t getDownloadLength() const;
- /**
- * Registers given peerStat if it has not been registerd.
- * Otherwise does nothing.
- */
- void registerPeerStat(const PeerStatHandle& peerStat);
- /**
- * Returns peerStat whose cuid is given cuid. If it is not found, returns
- * 0.
- */
- PeerStatHandle 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;
- }
- /**
- * Returns current download speed in bytes per sec.
- */
- int32_t calculateDownloadSpeed() const;
- bool fileExists() const;
- void markAllPiecesDone();
- void markPieceDone(int64_t length);
- /**
- * This function must be called when none of segment entries is used.
- */
- void purgeSegmentEntry()
- {
- usedSegmentEntries.clear();
- }
- #ifdef ENABLE_MESSAGE_DIGEST
- void tryChunkChecksumValidation(const SegmentHandle& segment, const ChunkChecksumHandle& chunkChecksum);
- bool isChunkChecksumValidationReady(const ChunkChecksumHandle& chunkChecksum) const;
- #endif // ENABLE_MESSAGE_DIGEST
- };
- typedef SharedHandle<SegmentMan> SegmentManHandle;
- #endif // _D_SEGMENT_MAN_H_
|