RequestGroupMan.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 "SharedHandle.h"
  42. #include "DownloadResult.h"
  43. #include "TransferStat.h"
  44. #include "RequestGroup.h"
  45. namespace aria2 {
  46. class DownloadEngine;
  47. class Command;
  48. struct DownloadResult;
  49. class ServerStatMan;
  50. class ServerStat;
  51. class Option;
  52. class OutputFile;
  53. class RequestGroupMan {
  54. private:
  55. std::deque<SharedHandle<RequestGroup> > requestGroups_;
  56. std::deque<SharedHandle<RequestGroup> > reservedGroups_;
  57. std::deque<SharedHandle<DownloadResult> > downloadResults_;
  58. int maxSimultaneousDownloads_;
  59. const Option* option_;
  60. SharedHandle<ServerStatMan> serverStatMan_;
  61. int maxOverallDownloadSpeedLimit_;
  62. int maxOverallUploadSpeedLimit_;
  63. // true if JSON-RPC/XML-RPC is enabled.
  64. bool rpc_;
  65. bool queueCheck_;
  66. // The number of error DownloadResult removed because of upper limit
  67. // of the queue
  68. int removedErrorResult_;
  69. // The last error of removed DownloadResult
  70. error_code::Value removedLastErrorResult_;
  71. int maxDownloadResult_;
  72. void formatDownloadResultFull
  73. (OutputFile& out,
  74. const std::string& status,
  75. const DownloadResultHandle& downloadResult) const;
  76. std::string formatDownloadResult
  77. (const std::string& status,
  78. const DownloadResultHandle& downloadResult) const;
  79. void configureRequestGroup
  80. (const SharedHandle<RequestGroup>& requestGroup) const;
  81. public:
  82. RequestGroupMan(const std::vector<SharedHandle<RequestGroup> >& requestGroups,
  83. int maxSimultaneousDownloads,
  84. const Option* option);
  85. ~RequestGroupMan();
  86. bool downloadFinished();
  87. void save();
  88. void closeFile();
  89. void halt();
  90. void forceHalt();
  91. void removeStoppedGroup(DownloadEngine* e);
  92. void fillRequestGroupFromReserver(DownloadEngine* e);
  93. void addRequestGroup(const SharedHandle<RequestGroup>& group);
  94. void addReservedGroup(const std::vector<SharedHandle<RequestGroup> >& groups);
  95. void addReservedGroup(const SharedHandle<RequestGroup>& group);
  96. void insertReservedGroup
  97. (size_t pos, const std::vector<SharedHandle<RequestGroup> >& groups);
  98. void insertReservedGroup(size_t pos, const SharedHandle<RequestGroup>& group);
  99. size_t countRequestGroup() const;
  100. SharedHandle<RequestGroup> getRequestGroup(size_t index) const;
  101. const std::deque<SharedHandle<RequestGroup> >& getRequestGroups() const
  102. {
  103. return requestGroups_;
  104. }
  105. SharedHandle<RequestGroup> findRequestGroup(a2_gid_t gid) const;
  106. const std::deque<SharedHandle<RequestGroup> >& getReservedGroups() const
  107. {
  108. return reservedGroups_;
  109. }
  110. SharedHandle<RequestGroup> findReservedGroup(a2_gid_t gid) const;
  111. enum HOW {
  112. POS_SET,
  113. POS_CUR,
  114. POS_END
  115. };
  116. // Changes the position of download denoted by gid. If how is
  117. // POS_SET, it moves the download to a position relative to the
  118. // beginning of the queue. If how is POS_CUR, it moves the download
  119. // to a position relative to the current position. If how is
  120. // POS_END, it moves the download to a position relative to the end
  121. // of the queue. If the destination position is less than 0 or
  122. // beyond the end of the queue, it moves the download to the
  123. // beginning or the end of the queue respectively. Returns the
  124. // destination position.
  125. size_t changeReservedGroupPosition(a2_gid_t gid, int pos, HOW how);
  126. bool removeReservedGroup(a2_gid_t gid);
  127. void showDownloadResults(OutputFile& o, bool full) const;
  128. bool isSameFileBeingDownloaded(RequestGroup* requestGroup) const;
  129. TransferStat calculateStat();
  130. class DownloadStat {
  131. private:
  132. int completed_;
  133. int error_;
  134. int inProgress_;
  135. int removed_;
  136. int waiting_;
  137. error_code::Value lastErrorResult_;
  138. public:
  139. DownloadStat(int completed,
  140. int error,
  141. int inProgress,
  142. int removed,
  143. int waiting,
  144. error_code::Value lastErrorResult =
  145. error_code::FINISHED):
  146. completed_(completed),
  147. error_(error),
  148. inProgress_(inProgress),
  149. removed_(removed),
  150. waiting_(waiting),
  151. lastErrorResult_(lastErrorResult) {}
  152. error_code::Value getLastErrorResult() const
  153. {
  154. return lastErrorResult_;
  155. }
  156. bool allCompleted() const
  157. {
  158. return error_ == 0 && inProgress_ == 0 && waiting_ == 0;
  159. }
  160. int getInProgress() const
  161. {
  162. return inProgress_;
  163. }
  164. };
  165. DownloadStat getDownloadStat() const;
  166. const std::deque<SharedHandle<DownloadResult> >& getDownloadResults() const
  167. {
  168. return downloadResults_;
  169. }
  170. SharedHandle<DownloadResult> findDownloadResult(a2_gid_t gid) const;
  171. // Removes all download results.
  172. void purgeDownloadResult();
  173. // Removes download result of given gid. Returns true if download
  174. // result was removed. Otherwise returns false.
  175. bool removeDownloadResult(a2_gid_t gid);
  176. void addDownloadResult(const SharedHandle<DownloadResult>& downloadResult);
  177. SharedHandle<ServerStat> findServerStat(const std::string& hostname,
  178. const std::string& protocol) const;
  179. SharedHandle<ServerStat> getOrCreateServerStat(const std::string& hostname,
  180. const std::string& protocol);
  181. bool addServerStat(const SharedHandle<ServerStat>& serverStat);
  182. void updateServerStat();
  183. bool loadServerStat(const std::string& filename);
  184. bool saveServerStat(const std::string& filename) const;
  185. void removeStaleServerStat(time_t timeout);
  186. // Returns true if current download speed exceeds
  187. // maxOverallDownloadSpeedLimit_. Always returns false if
  188. // maxOverallDownloadSpeedLimit_ == 0. Otherwise returns false.
  189. bool doesOverallDownloadSpeedExceed();
  190. void setMaxOverallDownloadSpeedLimit(int speed)
  191. {
  192. maxOverallDownloadSpeedLimit_ = speed;
  193. }
  194. int getMaxOverallDownloadSpeedLimit() const
  195. {
  196. return maxOverallDownloadSpeedLimit_;
  197. }
  198. // Returns true if current upload speed exceeds
  199. // maxOverallUploadSpeedLimit_. Always returns false if
  200. // maxOverallUploadSpeedLimit_ == 0. Otherwise returns false.
  201. bool doesOverallUploadSpeedExceed();
  202. void setMaxOverallUploadSpeedLimit(int speed)
  203. {
  204. maxOverallUploadSpeedLimit_ = speed;
  205. }
  206. int getMaxOverallUploadSpeedLimit() const
  207. {
  208. return maxOverallUploadSpeedLimit_;
  209. }
  210. void setMaxSimultaneousDownloads(int max)
  211. {
  212. maxSimultaneousDownloads_ = max;
  213. }
  214. // Call this function if requestGroups_ queue should be maintained.
  215. // This function is added to reduce the call of maintenance, but at
  216. // the same time, it provides fast maintenance reaction.
  217. void requestQueueCheck()
  218. {
  219. queueCheck_ = true;
  220. }
  221. void clearQueueCheck()
  222. {
  223. queueCheck_ = false;
  224. }
  225. bool queueCheckRequested() const
  226. {
  227. return queueCheck_;
  228. }
  229. // Returns currently used hosts and its use count.
  230. void getUsedHosts(std::vector<std::pair<size_t, std::string> >& usedHosts);
  231. const SharedHandle<ServerStatMan>& getServerStatMan() const
  232. {
  233. return serverStatMan_;
  234. }
  235. void setMaxDownloadResult(int v)
  236. {
  237. maxDownloadResult_ = v;
  238. }
  239. };
  240. typedef SharedHandle<RequestGroupMan> RequestGroupManHandle;
  241. } // namespace aria2
  242. #endif // D_REQUEST_GROUP_MAN_H