123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556 |
- /* <!-- 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_REQUEST_GROUP_H
- #define D_REQUEST_GROUP_H
- #include "common.h"
- #include <string>
- #include <algorithm>
- #include <vector>
- #include "SharedHandle.h"
- #include "TransferStat.h"
- #include "TimeA2.h"
- #include "Request.h"
- #include "error_code.h"
- #include "MetadataInfo.h"
- namespace aria2 {
- class DownloadEngine;
- class SegmentMan;
- class Command;
- class DownloadCommand;
- class DownloadContext;
- class PieceStorage;
- class BtProgressInfoFile;
- class Dependency;
- class PreDownloadHandler;
- class PostDownloadHandler;
- class DiskWriterFactory;
- class Option;
- class RequestGroup;
- class CheckIntegrityEntry;
- struct DownloadResult;
- class URISelector;
- class URIResult;
- class RequestGroupMan;
- #ifdef ENABLE_BITTORRENT
- class BtRuntime;
- class PeerStorage;
- #endif // ENABLE_BITTORRENT
- typedef int64_t a2_gid_t;
- class RequestGroup {
- public:
- enum HaltReason {
- NONE,
- SHUTDOWN_SIGNAL,
- USER_REQUEST
- };
- enum State {
- // Waiting in the reserved queue
- STATE_WAITING,
- // Download has begun
- STATE_ACTIVE
- };
- private:
- static a2_gid_t gidCounter_;
- a2_gid_t gid_;
- int state_;
- SharedHandle<Option> option_;
- int numConcurrentCommand_;
- /**
- * This is the number of connections used in streaming protocol(http/ftp)
- */
- int numStreamConnection_;
- int numStreamCommand_;
- int numCommand_;
- SharedHandle<SegmentMan> segmentMan_;
- SharedHandle<DownloadContext> downloadContext_;
- SharedHandle<PieceStorage> pieceStorage_;
- bool saveControlFile_;
- SharedHandle<BtProgressInfoFile> progressInfoFile_;
- SharedHandle<DiskWriterFactory> diskWriterFactory_;
- SharedHandle<Dependency> dependency_;
- bool fileAllocationEnabled_;
- bool preLocalFileCheckEnabled_;
- bool haltRequested_;
- bool forceHaltRequested_;
- HaltReason haltReason_;
- bool pauseRequested_;
- std::vector<SharedHandle<PreDownloadHandler> > preDownloadHandlers_;
- std::vector<SharedHandle<PostDownloadHandler> > postDownloadHandlers_;
- SharedHandle<URISelector> uriSelector_;
- Time lastModifiedTime_;
- int fileNotFoundCount_;
- // Timeout used for HTTP/FTP downloads.
- time_t timeout_;
- #ifdef ENABLE_BITTORRENT
- BtRuntime* btRuntime_;
- PeerStorage* peerStorage_;
- #endif // ENABLE_BITTORRENT
- // This flag just indicates that the downloaded file is not saved disk but
- // just sits in memory.
- bool inMemoryDownload_;
- int maxDownloadSpeedLimit_;
- int maxUploadSpeedLimit_;
- error_code::Value lastErrorCode_;
- // If this download generates another downloads when completed(for
- // example, downloads generated by PostDownloadHandler), this field
- // has the GID of generated RequestGroups. empty list means there is
- // no such RequestGroup.
- std::vector<a2_gid_t> followedByGIDs_;
- // If this download is a part of another download(for example,
- // downloading torrent file described in Metalink file), this field
- // has the GID of parent RequestGroup. 0 means this is a parent
- // RequestGroup.
- a2_gid_t belongsToGID_;
- SharedHandle<MetadataInfo> metadataInfo_;
- RequestGroupMan* requestGroupMan_;
- int resumeFailureCount_;
- void validateFilename(const std::string& expectedFilename,
- const std::string& actualFilename) const;
- void initializePreDownloadHandler();
- void initializePostDownloadHandler();
- bool tryAutoFileRenaming();
- // Returns the result code of this RequestGroup. If the download
- // finished, then returns error_code::FINISHED. If the
- // download didn't finish and error result is available in
- // _uriResults, then last result code is returned. Otherwise
- // returns error_code::UNKNOWN_ERROR.
- error_code::Value downloadResult() const;
- void removeDefunctControlFile
- (const SharedHandle<BtProgressInfoFile>& progressInfoFile);
- public:
- RequestGroup(const SharedHandle<Option>& option);
- ~RequestGroup();
- bool isCheckIntegrityReady();
- const SharedHandle<SegmentMan>& getSegmentMan() const
- {
- return segmentMan_;
- }
- SharedHandle<CheckIntegrityEntry> createCheckIntegrityEntry();
- // Returns first bootstrap commands to initiate a download.
- // If this is HTTP/FTP download and file size is unknown, only 1 command
- // (usually, HttpInitiateConnection or FtpInitiateConnection) will be created.
- void createInitialCommand(std::vector<Command*>& commands,
- DownloadEngine* e);
- void createNextCommandWithAdj(std::vector<Command*>& commands,
- DownloadEngine* e, int numAdj);
- void createNextCommand(std::vector<Command*>& commands,
- DownloadEngine* e, int numCommand);
-
- void createNextCommand(std::vector<Command*>& commands, DownloadEngine* e);
- bool downloadFinished() const;
- bool allDownloadFinished() const;
- void closeFile();
- std::string getFirstFilePath() const;
- int64_t getTotalLength() const;
- int64_t getCompletedLength() const;
- /**
- * Compares expected filename with specified actualFilename.
- * The expected filename refers to FileEntry::getBasename() of the first
- * element of DownloadContext::getFileEntries()
- */
- void validateFilename(const std::string& actualFilename) const;
- void validateTotalLength(int64_t expectedTotalLength,
- int64_t actualTotalLength) const;
- void validateTotalLength(int64_t actualTotalLength) const;
- void setNumConcurrentCommand(int num)
- {
- numConcurrentCommand_ = num;
- }
- int getNumConcurrentCommand() const
- {
- return numConcurrentCommand_;
- }
- a2_gid_t getGID() const
- {
- return gid_;
- }
- TransferStat calculateStat() const;
- const SharedHandle<DownloadContext>& getDownloadContext() const
- {
- return downloadContext_;
- }
- // This function also calls
- // downloadContext->setOwnerRequestGroup(this).
- void setDownloadContext(const SharedHandle<DownloadContext>& downloadContext);
- const SharedHandle<PieceStorage>& getPieceStorage() const
- {
- return pieceStorage_;
- }
- void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
- void setProgressInfoFile(const SharedHandle<BtProgressInfoFile>& progressInfoFile);
- void increaseStreamCommand();
- void decreaseStreamCommand();
- void increaseStreamConnection();
- void decreaseStreamConnection();
- int getNumConnection() const;
- void increaseNumCommand();
- void decreaseNumCommand();
- int getNumCommand() const
- {
- return numCommand_;
- }
- // TODO is it better to move the following 2 methods to SingleFileDownloadContext?
- void setDiskWriterFactory(const SharedHandle<DiskWriterFactory>& diskWriterFactory);
- const SharedHandle<DiskWriterFactory>& getDiskWriterFactory() const
- {
- return diskWriterFactory_;
- }
- void setFileAllocationEnabled(bool f)
- {
- fileAllocationEnabled_ = f;
- }
- bool isFileAllocationEnabled() const
- {
- return fileAllocationEnabled_;
- }
- bool needsFileAllocation() const;
- /**
- * Setting preLocalFileCheckEnabled_ to false, then skip the check to see
- * if a file is already exists and control file exists etc.
- * Always open file with DiskAdaptor::initAndOpenFile()
- */
- void setPreLocalFileCheckEnabled(bool f)
- {
- preLocalFileCheckEnabled_ = f;
- }
- bool isPreLocalFileCheckEnabled() const
- {
- return preLocalFileCheckEnabled_;
- }
- void setHaltRequested(bool f, HaltReason = SHUTDOWN_SIGNAL);
- void setForceHaltRequested(bool f, HaltReason = SHUTDOWN_SIGNAL);
- bool isHaltRequested() const
- {
- return haltRequested_;
- }
- bool isForceHaltRequested() const
- {
- return forceHaltRequested_;
- }
- void setPauseRequested(bool f);
- bool isPauseRequested() const
- {
- return pauseRequested_;
- }
- void dependsOn(const SharedHandle<Dependency>& dep);
- bool isDependencyResolved();
- void releaseRuntimeResource(DownloadEngine* e);
- void postDownloadProcessing(std::vector<SharedHandle<RequestGroup> >& groups);
- void addPostDownloadHandler(const SharedHandle<PostDownloadHandler>& handler);
- void clearPostDownloadHandler();
- void preDownloadProcessing();
- void addPreDownloadHandler(const SharedHandle<PreDownloadHandler>& handler);
- void clearPreDownloadHandler();
- void processCheckIntegrityEntry(std::vector<Command*>& commands,
- const SharedHandle<CheckIntegrityEntry>& entry,
- DownloadEngine* e);
- // Initializes pieceStorage_ and segmentMan_. We guarantee that
- // either both of pieceStorage_ and segmentMan_ are initialized or
- // they are not.
- void initPieceStorage();
- void dropPieceStorage();
- bool downloadFinishedByFileLength();
- void loadAndOpenFile(const SharedHandle<BtProgressInfoFile>& progressInfoFile);
- void shouldCancelDownloadForSafety();
- void adjustFilename(const SharedHandle<BtProgressInfoFile>& infoFile);
- SharedHandle<DownloadResult> createDownloadResult() const;
- const SharedHandle<Option>& getOption() const
- {
- return option_;
- }
- void reportDownloadFinished();
- void setURISelector(const SharedHandle<URISelector>& uriSelector);
- const SharedHandle<URISelector>& getURISelector() const
- {
- return uriSelector_;
- }
- void applyLastModifiedTimeToLocalFiles();
- void updateLastModifiedTime(const Time& time);
- void increaseAndValidateFileNotFoundCount();
- // Just set inMemoryDownload flag true.
- void markInMemoryDownload();
- // Returns inMemoryDownload flag.
- bool inMemoryDownload() const
- {
- return inMemoryDownload_;
- }
- void setTimeout(time_t timeout);
- time_t getTimeout() const
- {
- return timeout_;
- }
- // Returns true if current download speed exceeds
- // maxDownloadSpeedLimit_. Always returns false if
- // maxDownloadSpeedLimit_ == 0. Otherwise returns false.
- bool doesDownloadSpeedExceed();
- // Returns true if current upload speed exceeds
- // maxUploadSpeedLimit_. Always returns false if
- // maxUploadSpeedLimit_ == 0. Otherwise returns false.
- bool doesUploadSpeedExceed();
- int getMaxDownloadSpeedLimit() const
- {
- return maxDownloadSpeedLimit_;
- }
- void setMaxDownloadSpeedLimit(int speed)
- {
- maxDownloadSpeedLimit_ = speed;
- }
- int getMaxUploadSpeedLimit() const
- {
- return maxUploadSpeedLimit_;
- }
- void setMaxUploadSpeedLimit(int speed)
- {
- maxUploadSpeedLimit_ = speed;
- }
- void setLastErrorCode(error_code::Value code)
- {
- lastErrorCode_ = code;
- }
- error_code::Value getLastErrorCode() const
- {
- return lastErrorCode_;
- }
- void saveControlFile() const;
- void removeControlFile() const;
- void enableSaveControlFile() { saveControlFile_ = true; }
- void disableSaveControlFile() { saveControlFile_ = false; }
- template<typename InputIterator>
- void followedBy(InputIterator groupFirst, InputIterator groupLast)
- {
- followedByGIDs_.clear();
- for(; groupFirst != groupLast; ++groupFirst) {
- followedByGIDs_.push_back((*groupFirst)->getGID());
- }
- }
- const std::vector<a2_gid_t>& followedBy() const
- {
- return followedByGIDs_;
- }
- void belongsTo(a2_gid_t gid)
- {
- belongsToGID_ = gid;
- }
- a2_gid_t belongsTo() const
- {
- return belongsToGID_;
- }
- void setRequestGroupMan(RequestGroupMan* requestGroupMan)
- {
- requestGroupMan_ = requestGroupMan;
- }
- int getResumeFailureCount() const
- {
- return resumeFailureCount_;
- }
- void increaseResumeFailureCount()
- {
- ++resumeFailureCount_;
- }
- bool p2pInvolved() const;
- void setMetadataInfo(const SharedHandle<MetadataInfo>& info)
- {
- metadataInfo_ = info;
- }
- const SharedHandle<MetadataInfo>& getMetadataInfo() const
- {
- return metadataInfo_;
- }
- int getState() const
- {
- return state_;
- }
- void setState(int state)
- {
- state_ = state;
- }
- static void resetGIDCounter() { gidCounter_ = 0; }
- static a2_gid_t newGID();
- };
- } // namespace aria2
- #endif // D_REQUEST_GROUP_H
|