RequestGroupMan.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef D_REQUEST_GROUP_MAN_H
  36. #define D_REQUEST_GROUP_MAN_H
  37. #include "common.h"
  38. #include <string>
  39. #include <deque>
  40. #include <vector>
  41. #include <map>
  42. #include <memory>
  43. #include "DownloadResult.h"
  44. #include "TransferStat.h"
  45. #include "RequestGroup.h"
  46. #include "NetStat.h"
  47. #include "IndexedList.h"
  48. namespace aria2 {
  49. class DownloadEngine;
  50. class Command;
  51. struct DownloadResult;
  52. class ServerStatMan;
  53. class ServerStat;
  54. class Option;
  55. class OutputFile;
  56. class UriListParser;
  57. class WrDiskCache;
  58. class OpenedFileCounter;
  59. typedef IndexedList<a2_gid_t, std::shared_ptr<RequestGroup>> RequestGroupList;
  60. typedef IndexedList<a2_gid_t, std::shared_ptr<DownloadResult>>
  61. DownloadResultList;
  62. class RequestGroupMan {
  63. private:
  64. RequestGroupList requestGroups_;
  65. RequestGroupList reservedGroups_;
  66. DownloadResultList downloadResults_;
  67. int maxSimultaneousDownloads_;
  68. // The number of simultaneous active downloads, excluding seed only
  69. // item if PREF_BT_DETACH_SEED_ONLY is true. We rely on this
  70. // variable to maintain the number of concurrent downloads. If
  71. // PREF_BT_DETACH_SEED_ONLY is false, this variable is equal to
  72. // requestGroups_.size().
  73. size_t numActive_;
  74. const Option* option_;
  75. std::shared_ptr<ServerStatMan> serverStatMan_;
  76. int maxOverallDownloadSpeedLimit_;
  77. int maxOverallUploadSpeedLimit_;
  78. NetStat netStat_;
  79. // true if download engine should keep running even if there is no
  80. // download to perform.
  81. bool keepRunning_;
  82. bool queueCheck_;
  83. // The number of error DownloadResult removed because of upper limit
  84. // of the queue
  85. int removedErrorResult_;
  86. // The last error of removed DownloadResult
  87. error_code::Value removedLastErrorResult_;
  88. size_t maxDownloadResult_;
  89. // UriListParser for deferred input.
  90. std::shared_ptr<UriListParser> uriListParser_;
  91. std::unique_ptr<WrDiskCache> wrDiskCache_;
  92. std::shared_ptr<OpenedFileCounter> openedFileCounter_;
  93. // The number of stopped downloads so far in total, including
  94. // evicted DownloadResults.
  95. size_t numStoppedTotal_;
  96. // SHA1 hash value of the content of last session serialization.
  97. std::string lastSessionHash_;
  98. void formatDownloadResultFull(
  99. OutputFile& out, const char* status,
  100. const std::shared_ptr<DownloadResult>& downloadResult) const;
  101. std::string formatDownloadResult(
  102. const char* status,
  103. const std::shared_ptr<DownloadResult>& downloadResult) const;
  104. void configureRequestGroup(
  105. const std::shared_ptr<RequestGroup>& requestGroup) const;
  106. void addRequestGroupIndex(const std::shared_ptr<RequestGroup>& group);
  107. void addRequestGroupIndex(
  108. const std::vector<std::shared_ptr<RequestGroup>>& groups);
  109. public:
  110. RequestGroupMan(std::vector<std::shared_ptr<RequestGroup>> requestGroups,
  111. int maxSimultaneousDownloads, const Option* option);
  112. ~RequestGroupMan();
  113. bool downloadFinished();
  114. void save();
  115. void closeFile();
  116. void halt();
  117. void forceHalt();
  118. void removeStoppedGroup(DownloadEngine* e);
  119. void fillRequestGroupFromReserver(DownloadEngine* e);
  120. // Note that this method does not call addRequestGroupIndex(). This
  121. // method should be considered as private, but exposed for unit
  122. // testing purpose.
  123. void addRequestGroup(const std::shared_ptr<RequestGroup>& group);
  124. void
  125. addReservedGroup(const std::vector<std::shared_ptr<RequestGroup>>& groups);
  126. void addReservedGroup(const std::shared_ptr<RequestGroup>& group);
  127. void
  128. insertReservedGroup(size_t pos,
  129. const std::vector<std::shared_ptr<RequestGroup>>& groups);
  130. void insertReservedGroup(size_t pos,
  131. const std::shared_ptr<RequestGroup>& group);
  132. size_t countRequestGroup() const;
  133. const RequestGroupList& getRequestGroups() const { return requestGroups_; }
  134. const RequestGroupList& getReservedGroups() const { return reservedGroups_; }
  135. // Returns RequestGroup object whose gid is gid. This method returns
  136. // RequestGroup either in requestGroups_ or reservedGroups_.
  137. std::shared_ptr<RequestGroup> findGroup(a2_gid_t gid) const;
  138. // Changes the position of download denoted by gid. If how is
  139. // POS_SET, it moves the download to a position relative to the
  140. // beginning of the queue. If how is POS_CUR, it moves the download
  141. // to a position relative to the current position. If how is
  142. // POS_END, it moves the download to a position relative to the end
  143. // of the queue. If the destination position is less than 0 or
  144. // beyond the end of the queue, it moves the download to the
  145. // beginning or the end of the queue respectively. Returns the
  146. // destination position.
  147. size_t changeReservedGroupPosition(a2_gid_t gid, int pos, OffsetMode how);
  148. bool removeReservedGroup(a2_gid_t gid);
  149. void showDownloadResults(OutputFile& o, bool full) const;
  150. bool isSameFileBeingDownloaded(RequestGroup* requestGroup) const;
  151. TransferStat calculateStat();
  152. class DownloadStat {
  153. private:
  154. int error_;
  155. int inProgress_;
  156. int waiting_;
  157. error_code::Value lastErrorResult_;
  158. public:
  159. DownloadStat(int error, int inProgress, int waiting,
  160. error_code::Value lastErrorResult = error_code::FINISHED)
  161. : error_(error),
  162. inProgress_(inProgress),
  163. waiting_(waiting),
  164. lastErrorResult_(lastErrorResult)
  165. {
  166. }
  167. error_code::Value getLastErrorResult() const { return lastErrorResult_; }
  168. bool allCompleted() const
  169. {
  170. return error_ == 0 && inProgress_ == 0 && waiting_ == 0;
  171. }
  172. int getInProgress() const { return inProgress_; }
  173. };
  174. DownloadStat getDownloadStat() const;
  175. const DownloadResultList& getDownloadResults() const
  176. {
  177. return downloadResults_;
  178. }
  179. std::shared_ptr<DownloadResult> findDownloadResult(a2_gid_t gid) const;
  180. // Removes all download results.
  181. void purgeDownloadResult();
  182. // Removes download result of given gid. Returns true if download
  183. // result was removed. Otherwise returns false.
  184. bool removeDownloadResult(a2_gid_t gid);
  185. void addDownloadResult(const std::shared_ptr<DownloadResult>& downloadResult);
  186. std::shared_ptr<ServerStat> findServerStat(const std::string& hostname,
  187. const std::string& protocol) const;
  188. std::shared_ptr<ServerStat>
  189. getOrCreateServerStat(const std::string& hostname,
  190. const std::string& protocol);
  191. bool addServerStat(const std::shared_ptr<ServerStat>& serverStat);
  192. bool loadServerStat(const std::string& filename);
  193. bool saveServerStat(const std::string& filename) const;
  194. void removeStaleServerStat(const std::chrono::seconds& timeout);
  195. // Returns true if current download speed exceeds
  196. // maxOverallDownloadSpeedLimit_. Always returns false if
  197. // maxOverallDownloadSpeedLimit_ == 0. Otherwise returns false.
  198. bool doesOverallDownloadSpeedExceed();
  199. void setMaxOverallDownloadSpeedLimit(int speed)
  200. {
  201. maxOverallDownloadSpeedLimit_ = speed;
  202. }
  203. int getMaxOverallDownloadSpeedLimit() const
  204. {
  205. return maxOverallDownloadSpeedLimit_;
  206. }
  207. // Returns true if current upload speed exceeds
  208. // maxOverallUploadSpeedLimit_. Always returns false if
  209. // maxOverallUploadSpeedLimit_ == 0. Otherwise returns false.
  210. bool doesOverallUploadSpeedExceed();
  211. void setMaxOverallUploadSpeedLimit(int speed)
  212. {
  213. maxOverallUploadSpeedLimit_ = speed;
  214. }
  215. int getMaxOverallUploadSpeedLimit() const
  216. {
  217. return maxOverallUploadSpeedLimit_;
  218. }
  219. void setMaxSimultaneousDownloads(int max) { maxSimultaneousDownloads_ = max; }
  220. // Call this function if requestGroups_ queue should be maintained.
  221. // This function is added to reduce the call of maintenance, but at
  222. // the same time, it provides fast maintenance reaction.
  223. void requestQueueCheck() { queueCheck_ = true; }
  224. void clearQueueCheck() { queueCheck_ = false; }
  225. bool queueCheckRequested() const { return queueCheck_; }
  226. // Returns currently used hosts and its use count.
  227. void getUsedHosts(std::vector<std::pair<size_t, std::string>>& usedHosts);
  228. const std::shared_ptr<ServerStatMan>& getServerStatMan() const
  229. {
  230. return serverStatMan_;
  231. }
  232. void setMaxDownloadResult(size_t v) { maxDownloadResult_ = v; }
  233. void setUriListParser(const std::shared_ptr<UriListParser>& uriListParser);
  234. NetStat& getNetStat() { return netStat_; }
  235. WrDiskCache* getWrDiskCache() const { return wrDiskCache_.get(); }
  236. // Initializes WrDiskCache according to PREF_DISK_CACHE option. If
  237. // its value is 0, cache storage will not be initialized.
  238. void initWrDiskCache();
  239. void setKeepRunning(bool flag) { keepRunning_ = flag; }
  240. bool getKeepRunning() const { return keepRunning_; }
  241. size_t getNumStoppedTotal() const { return numStoppedTotal_; }
  242. void setLastSessionHash(std::string lastSessionHash)
  243. {
  244. lastSessionHash_ = std::move(lastSessionHash);
  245. }
  246. const std::string& getLastSessionHash() const { return lastSessionHash_; }
  247. const std::shared_ptr<OpenedFileCounter>& getOpenedFileCounter() const
  248. {
  249. return openedFileCounter_;
  250. }
  251. void decreaseNumActive();
  252. };
  253. } // namespace aria2
  254. #endif // D_REQUEST_GROUP_MAN_H