RequestGroupMan.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. // This includes download result which did not finish, and deleted
  68. // from downloadResults_. This is used to save them in
  69. // SessionSerializer.
  70. std::vector<std::shared_ptr<DownloadResult>> unfinishedDownloadResults_;
  71. int maxConcurrentDownloads_;
  72. bool optimizeConcurrentDownloads_;
  73. double optimizeConcurrentDownloadsCoeffA_;
  74. double optimizeConcurrentDownloadsCoeffB_;
  75. int optimizationSpeed_;
  76. Timer optimizationSpeedTimer_;
  77. // The number of simultaneous active downloads, excluding seed only
  78. // item if PREF_BT_DETACH_SEED_ONLY is true. We rely on this
  79. // variable to maintain the number of concurrent downloads. If
  80. // PREF_BT_DETACH_SEED_ONLY is false, this variable is equal to
  81. // requestGroups_.size().
  82. size_t numActive_;
  83. const Option* option_;
  84. std::shared_ptr<ServerStatMan> serverStatMan_;
  85. int maxOverallDownloadSpeedLimit_;
  86. int maxOverallUploadSpeedLimit_;
  87. NetStat netStat_;
  88. // true if download engine should keep running even if there is no
  89. // download to perform.
  90. bool keepRunning_;
  91. bool queueCheck_;
  92. // The number of error DownloadResult removed because of upper limit
  93. // of the queue
  94. int removedErrorResult_;
  95. // The last error of removed DownloadResult
  96. error_code::Value removedLastErrorResult_;
  97. size_t maxDownloadResult_;
  98. // UriListParser for deferred input.
  99. std::shared_ptr<UriListParser> uriListParser_;
  100. std::unique_ptr<WrDiskCache> wrDiskCache_;
  101. std::shared_ptr<OpenedFileCounter> openedFileCounter_;
  102. // The number of stopped downloads so far in total, including
  103. // evicted DownloadResults.
  104. size_t numStoppedTotal_;
  105. // SHA1 hash value of the content of last session serialization.
  106. std::string lastSessionHash_;
  107. void formatDownloadResultFull(
  108. OutputFile& out, const char* status,
  109. const std::shared_ptr<DownloadResult>& downloadResult) const;
  110. std::string formatDownloadResult(
  111. const char* status,
  112. const std::shared_ptr<DownloadResult>& downloadResult) const;
  113. void configureRequestGroup(
  114. const std::shared_ptr<RequestGroup>& requestGroup) const;
  115. void addRequestGroupIndex(const std::shared_ptr<RequestGroup>& group);
  116. void addRequestGroupIndex(
  117. const std::vector<std::shared_ptr<RequestGroup>>& groups);
  118. int optimizeConcurrentDownloads();
  119. public:
  120. RequestGroupMan(std::vector<std::shared_ptr<RequestGroup>> requestGroups,
  121. int maxConcurrentDownloads, const Option* option);
  122. ~RequestGroupMan();
  123. bool downloadFinished();
  124. void save();
  125. void closeFile();
  126. void halt();
  127. void forceHalt();
  128. void removeStoppedGroup(DownloadEngine* e);
  129. void fillRequestGroupFromReserver(DownloadEngine* e);
  130. // Note that this method does not call addRequestGroupIndex(). This
  131. // method should be considered as private, but exposed for unit
  132. // testing purpose.
  133. void addRequestGroup(const std::shared_ptr<RequestGroup>& group);
  134. void
  135. addReservedGroup(const std::vector<std::shared_ptr<RequestGroup>>& groups);
  136. void addReservedGroup(const std::shared_ptr<RequestGroup>& group);
  137. void
  138. insertReservedGroup(size_t pos,
  139. const std::vector<std::shared_ptr<RequestGroup>>& groups);
  140. void insertReservedGroup(size_t pos,
  141. const std::shared_ptr<RequestGroup>& group);
  142. size_t countRequestGroup() const;
  143. const RequestGroupList& getRequestGroups() const { return requestGroups_; }
  144. const RequestGroupList& getReservedGroups() const { return reservedGroups_; }
  145. // Returns RequestGroup object whose gid is gid. This method returns
  146. // RequestGroup either in requestGroups_ or reservedGroups_.
  147. std::shared_ptr<RequestGroup> findGroup(a2_gid_t gid) const;
  148. // Changes the position of download denoted by gid. If how is
  149. // POS_SET, it moves the download to a position relative to the
  150. // beginning of the queue. If how is POS_CUR, it moves the download
  151. // to a position relative to the current position. If how is
  152. // POS_END, it moves the download to a position relative to the end
  153. // of the queue. If the destination position is less than 0 or
  154. // beyond the end of the queue, it moves the download to the
  155. // beginning or the end of the queue respectively. Returns the
  156. // destination position.
  157. size_t changeReservedGroupPosition(a2_gid_t gid, int pos, OffsetMode how);
  158. bool removeReservedGroup(a2_gid_t gid);
  159. bool getOptimizeConcurrentDownloads() const
  160. {
  161. return optimizeConcurrentDownloads_;
  162. }
  163. bool setupOptimizeConcurrentDownloads();
  164. void showDownloadResults(OutputFile& o, bool full) const;
  165. bool isSameFileBeingDownloaded(RequestGroup* requestGroup) const;
  166. TransferStat calculateStat();
  167. class DownloadStat {
  168. private:
  169. int error_;
  170. int inProgress_;
  171. int waiting_;
  172. error_code::Value lastErrorResult_;
  173. public:
  174. DownloadStat(int error, int inProgress, int waiting,
  175. error_code::Value lastErrorResult = error_code::FINISHED)
  176. : error_(error),
  177. inProgress_(inProgress),
  178. waiting_(waiting),
  179. lastErrorResult_(lastErrorResult)
  180. {
  181. }
  182. error_code::Value getLastErrorResult() const { return lastErrorResult_; }
  183. bool allCompleted() const
  184. {
  185. return error_ == 0 && inProgress_ == 0 && waiting_ == 0;
  186. }
  187. int getInProgress() const { return inProgress_; }
  188. };
  189. DownloadStat getDownloadStat() const;
  190. const DownloadResultList& getDownloadResults() const
  191. {
  192. return downloadResults_;
  193. }
  194. std::shared_ptr<DownloadResult> findDownloadResult(a2_gid_t gid) const;
  195. // Removes all download results.
  196. void purgeDownloadResult();
  197. // Removes download result of given gid. Returns true if download
  198. // result was removed. Otherwise returns false.
  199. bool removeDownloadResult(a2_gid_t gid);
  200. void addDownloadResult(const std::shared_ptr<DownloadResult>& downloadResult);
  201. const std::vector<std::shared_ptr<DownloadResult>>&
  202. getUnfinishedDownloadResult() const
  203. {
  204. return unfinishedDownloadResults_;
  205. }
  206. std::shared_ptr<ServerStat> findServerStat(const std::string& hostname,
  207. const std::string& protocol) const;
  208. std::shared_ptr<ServerStat>
  209. getOrCreateServerStat(const std::string& hostname,
  210. const std::string& protocol);
  211. bool addServerStat(const std::shared_ptr<ServerStat>& serverStat);
  212. bool loadServerStat(const std::string& filename);
  213. bool saveServerStat(const std::string& filename) const;
  214. void removeStaleServerStat(const std::chrono::seconds& timeout);
  215. // Returns true if current download speed exceeds
  216. // maxOverallDownloadSpeedLimit_. Always returns false if
  217. // maxOverallDownloadSpeedLimit_ == 0. Otherwise returns false.
  218. bool doesOverallDownloadSpeedExceed();
  219. void setMaxOverallDownloadSpeedLimit(int speed)
  220. {
  221. maxOverallDownloadSpeedLimit_ = speed;
  222. }
  223. int getMaxOverallDownloadSpeedLimit() const
  224. {
  225. return maxOverallDownloadSpeedLimit_;
  226. }
  227. // Returns true if current upload speed exceeds
  228. // maxOverallUploadSpeedLimit_. Always returns false if
  229. // maxOverallUploadSpeedLimit_ == 0. Otherwise returns false.
  230. bool doesOverallUploadSpeedExceed();
  231. void setMaxOverallUploadSpeedLimit(int speed)
  232. {
  233. maxOverallUploadSpeedLimit_ = speed;
  234. }
  235. int getMaxOverallUploadSpeedLimit() const
  236. {
  237. return maxOverallUploadSpeedLimit_;
  238. }
  239. void setMaxConcurrentDownloads(int max) { maxConcurrentDownloads_ = max; }
  240. // Call this function if requestGroups_ queue should be maintained.
  241. // This function is added to reduce the call of maintenance, but at
  242. // the same time, it provides fast maintenance reaction.
  243. void requestQueueCheck() { queueCheck_ = true; }
  244. void clearQueueCheck() { queueCheck_ = false; }
  245. bool queueCheckRequested() const { return queueCheck_; }
  246. // Returns currently used hosts and its use count.
  247. void getUsedHosts(std::vector<std::pair<size_t, std::string>>& usedHosts);
  248. const std::shared_ptr<ServerStatMan>& getServerStatMan() const
  249. {
  250. return serverStatMan_;
  251. }
  252. void setMaxDownloadResult(size_t v) { maxDownloadResult_ = v; }
  253. void setUriListParser(const std::shared_ptr<UriListParser>& uriListParser);
  254. NetStat& getNetStat() { return netStat_; }
  255. WrDiskCache* getWrDiskCache() const { return wrDiskCache_.get(); }
  256. // Initializes WrDiskCache according to PREF_DISK_CACHE option. If
  257. // its value is 0, cache storage will not be initialized.
  258. void initWrDiskCache();
  259. void setKeepRunning(bool flag) { keepRunning_ = flag; }
  260. bool getKeepRunning() const { return keepRunning_; }
  261. size_t getNumStoppedTotal() const { return numStoppedTotal_; }
  262. void setLastSessionHash(std::string lastSessionHash)
  263. {
  264. lastSessionHash_ = std::move(lastSessionHash);
  265. }
  266. const std::string& getLastSessionHash() const { return lastSessionHash_; }
  267. const std::shared_ptr<OpenedFileCounter>& getOpenedFileCounter() const
  268. {
  269. return openedFileCounter_;
  270. }
  271. void decreaseNumActive();
  272. };
  273. } // namespace aria2
  274. #endif // D_REQUEST_GROUP_MAN_H