RequestGroupMan.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 "SharedHandle.h"
  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. typedef IndexedList<a2_gid_t,
  59. SharedHandle<RequestGroup> > RequestGroupList;
  60. typedef IndexedList<a2_gid_t,
  61. SharedHandle<DownloadResult> > DownloadResultList;
  62. class RequestGroupMan {
  63. private:
  64. RequestGroupList requestGroups_;
  65. RequestGroupList reservedGroups_;
  66. DownloadResultList downloadResults_;
  67. int maxSimultaneousDownloads_;
  68. const Option* option_;
  69. SharedHandle<ServerStatMan> serverStatMan_;
  70. int maxOverallDownloadSpeedLimit_;
  71. int maxOverallUploadSpeedLimit_;
  72. NetStat netStat_;
  73. // true if download engine should keep running even if there is no
  74. // download to perform.
  75. bool keepRunning_;
  76. bool queueCheck_;
  77. // The number of error DownloadResult removed because of upper limit
  78. // of the queue
  79. int removedErrorResult_;
  80. // The last error of removed DownloadResult
  81. error_code::Value removedLastErrorResult_;
  82. size_t maxDownloadResult_;
  83. // UriListParser for deferred input.
  84. SharedHandle<UriListParser> uriListParser_;
  85. WrDiskCache* wrDiskCache_;
  86. void formatDownloadResultFull
  87. (OutputFile& out,
  88. const char* status,
  89. const SharedHandle<DownloadResult>& downloadResult) const;
  90. std::string formatDownloadResult
  91. (const char* status,
  92. const SharedHandle<DownloadResult>& downloadResult) const;
  93. void configureRequestGroup
  94. (const SharedHandle<RequestGroup>& requestGroup) const;
  95. void addRequestGroupIndex(const SharedHandle<RequestGroup>& group);
  96. void addRequestGroupIndex
  97. (const std::vector<SharedHandle<RequestGroup> >& groups);
  98. public:
  99. RequestGroupMan(const std::vector<SharedHandle<RequestGroup> >& requestGroups,
  100. int maxSimultaneousDownloads,
  101. const Option* option);
  102. ~RequestGroupMan();
  103. bool downloadFinished();
  104. void save();
  105. void closeFile();
  106. void halt();
  107. void forceHalt();
  108. void removeStoppedGroup(DownloadEngine* e);
  109. void fillRequestGroupFromReserver(DownloadEngine* e);
  110. // Note that this method does not call addRequestGroupIndex(). This
  111. // method should be considered as private, but exposed for unit
  112. // testing purpose.
  113. void addRequestGroup(const SharedHandle<RequestGroup>& group);
  114. void addReservedGroup(const std::vector<SharedHandle<RequestGroup> >& groups);
  115. void addReservedGroup(const SharedHandle<RequestGroup>& group);
  116. void insertReservedGroup
  117. (size_t pos, const std::vector<SharedHandle<RequestGroup> >& groups);
  118. void insertReservedGroup(size_t pos, const SharedHandle<RequestGroup>& group);
  119. size_t countRequestGroup() const;
  120. const RequestGroupList& getRequestGroups() const
  121. {
  122. return requestGroups_;
  123. }
  124. const RequestGroupList& getReservedGroups() const
  125. {
  126. return reservedGroups_;
  127. }
  128. // Returns RequestGroup object whose gid is gid. This method returns
  129. // RequestGroup either in requestGroups_ or reservedGroups_.
  130. SharedHandle<RequestGroup> findGroup(a2_gid_t gid) const;
  131. // Changes the position of download denoted by gid. If how is
  132. // POS_SET, it moves the download to a position relative to the
  133. // beginning of the queue. If how is POS_CUR, it moves the download
  134. // to a position relative to the current position. If how is
  135. // POS_END, it moves the download to a position relative to the end
  136. // of the queue. If the destination position is less than 0 or
  137. // beyond the end of the queue, it moves the download to the
  138. // beginning or the end of the queue respectively. Returns the
  139. // destination position.
  140. size_t changeReservedGroupPosition(a2_gid_t gid, int pos,
  141. OffsetMode how);
  142. bool removeReservedGroup(a2_gid_t gid);
  143. void showDownloadResults(OutputFile& o, bool full) const;
  144. bool isSameFileBeingDownloaded(RequestGroup* requestGroup) const;
  145. TransferStat calculateStat();
  146. class DownloadStat {
  147. private:
  148. int error_;
  149. int inProgress_;
  150. int waiting_;
  151. error_code::Value lastErrorResult_;
  152. public:
  153. DownloadStat(int error,
  154. int inProgress,
  155. int waiting,
  156. error_code::Value lastErrorResult =
  157. error_code::FINISHED):
  158. error_(error),
  159. inProgress_(inProgress),
  160. waiting_(waiting),
  161. lastErrorResult_(lastErrorResult) {}
  162. error_code::Value getLastErrorResult() const
  163. {
  164. return lastErrorResult_;
  165. }
  166. bool allCompleted() const
  167. {
  168. return error_ == 0 && inProgress_ == 0 && waiting_ == 0;
  169. }
  170. int getInProgress() const
  171. {
  172. return inProgress_;
  173. }
  174. };
  175. DownloadStat getDownloadStat() const;
  176. const DownloadResultList& getDownloadResults() const
  177. {
  178. return downloadResults_;
  179. }
  180. SharedHandle<DownloadResult> findDownloadResult(a2_gid_t gid) const;
  181. // Removes all download results.
  182. void purgeDownloadResult();
  183. // Removes download result of given gid. Returns true if download
  184. // result was removed. Otherwise returns false.
  185. bool removeDownloadResult(a2_gid_t gid);
  186. void addDownloadResult(const SharedHandle<DownloadResult>& downloadResult);
  187. SharedHandle<ServerStat> findServerStat(const std::string& hostname,
  188. const std::string& protocol) const;
  189. SharedHandle<ServerStat> getOrCreateServerStat(const std::string& hostname,
  190. const std::string& protocol);
  191. bool addServerStat(const SharedHandle<ServerStat>& serverStat);
  192. bool loadServerStat(const std::string& filename);
  193. bool saveServerStat(const std::string& filename) const;
  194. void removeStaleServerStat(time_t 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)
  220. {
  221. maxSimultaneousDownloads_ = max;
  222. }
  223. // Call this function if requestGroups_ queue should be maintained.
  224. // This function is added to reduce the call of maintenance, but at
  225. // the same time, it provides fast maintenance reaction.
  226. void requestQueueCheck()
  227. {
  228. queueCheck_ = true;
  229. }
  230. void clearQueueCheck()
  231. {
  232. queueCheck_ = false;
  233. }
  234. bool queueCheckRequested() const
  235. {
  236. return queueCheck_;
  237. }
  238. // Returns currently used hosts and its use count.
  239. void getUsedHosts(std::vector<std::pair<size_t, std::string> >& usedHosts);
  240. const SharedHandle<ServerStatMan>& getServerStatMan() const
  241. {
  242. return serverStatMan_;
  243. }
  244. void setMaxDownloadResult(size_t v)
  245. {
  246. maxDownloadResult_ = v;
  247. }
  248. void setUriListParser(const SharedHandle<UriListParser>& uriListParser);
  249. NetStat& getNetStat()
  250. {
  251. return netStat_;
  252. }
  253. WrDiskCache* getWrDiskCache() const
  254. {
  255. return wrDiskCache_;
  256. }
  257. // Initializes WrDiskCache according to PREF_DISK_CACHE option. If
  258. // its value is 0, cache storage will not be initialized.
  259. void initWrDiskCache();
  260. void setKeepRunning(bool flag)
  261. {
  262. keepRunning_ = flag;
  263. }
  264. bool getKeepRunning() const
  265. {
  266. return keepRunning_;
  267. }
  268. };
  269. } // namespace aria2
  270. #endif // D_REQUEST_GROUP_MAN_H