DownloadEngine.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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_DOWNLOAD_ENGINE_H
  36. #define D_DOWNLOAD_ENGINE_H
  37. #include "common.h"
  38. #include <string>
  39. #include <deque>
  40. #include <map>
  41. #include <vector>
  42. #include <memory>
  43. #include "a2netcompat.h"
  44. #include "TimerA2.h"
  45. #include "a2io.h"
  46. #include "CUIDCounter.h"
  47. #include "FileAllocationMan.h"
  48. #include "CheckIntegrityMan.h"
  49. #include "DNSCache.h"
  50. #ifdef ENABLE_ASYNC_DNS
  51. # include "AsyncNameResolver.h"
  52. #endif // ENABLE_ASYNC_DNS
  53. namespace aria2 {
  54. class Option;
  55. class RequestGroupMan;
  56. class StatCalc;
  57. class SocketCore;
  58. class CookieStorage;
  59. class AuthConfigFactory;
  60. class Request;
  61. class EventPoll;
  62. class Command;
  63. #ifdef ENABLE_BITTORRENT
  64. class BtRegistry;
  65. #endif // ENABLE_BITTORRENT
  66. #ifdef ENABLE_WEBSOCKET
  67. namespace rpc {
  68. class WebSocketSessionMan;
  69. } // namespace rpc
  70. #endif // ENABLE_WEBSOCKET
  71. class DownloadEngine {
  72. private:
  73. void waitData();
  74. std::string sessionId_;
  75. std::shared_ptr<EventPoll> eventPoll_;
  76. std::unique_ptr<StatCalc> statCalc_;
  77. int haltRequested_;
  78. class SocketPoolEntry {
  79. private:
  80. std::shared_ptr<SocketCore> socket_;
  81. // protocol specific option string
  82. std::string options_;
  83. time_t timeout_;
  84. Timer registeredTime_;
  85. public:
  86. SocketPoolEntry(const std::shared_ptr<SocketCore>& socket,
  87. const std::string& option,
  88. time_t timeout);
  89. SocketPoolEntry(const std::shared_ptr<SocketCore>& socket,
  90. time_t timeout);
  91. ~SocketPoolEntry();
  92. bool isTimeout() const;
  93. const std::shared_ptr<SocketCore>& getSocket() const
  94. {
  95. return socket_;
  96. }
  97. const std::string& getOptions() const
  98. {
  99. return options_;
  100. }
  101. };
  102. // key = IP address:port, value = SocketPoolEntry
  103. std::multimap<std::string, SocketPoolEntry> socketPool_;
  104. Timer lastSocketPoolScan_;
  105. bool noWait_;
  106. static const int64_t DEFAULT_REFRESH_INTERVAL = 1000;
  107. // Milliseconds
  108. int64_t refreshInterval_;
  109. Timer lastRefresh_;
  110. std::deque<std::unique_ptr<Command>> routineCommands_;
  111. std::unique_ptr<CookieStorage> cookieStorage_;
  112. #ifdef ENABLE_BITTORRENT
  113. std::unique_ptr<BtRegistry> btRegistry_;
  114. #endif // ENABLE_BITTORRENT
  115. CUIDCounter cuidCounter_;
  116. #ifdef HAVE_ARES_ADDR_NODE
  117. ares_addr_node* asyncDNSServers_;
  118. #endif // HAVE_ARES_ADDR_NODE
  119. std::unique_ptr<DNSCache> dnsCache_;
  120. std::unique_ptr<AuthConfigFactory> authConfigFactory_;
  121. #ifdef ENABLE_WEBSOCKET
  122. std::unique_ptr<rpc::WebSocketSessionMan> webSocketSessionMan_;
  123. #endif // ENABLE_WEBSOCKET
  124. /**
  125. * Delegates to StatCalc
  126. */
  127. void calculateStatistics();
  128. void onEndOfRun();
  129. void afterEachIteration();
  130. void poolSocket(const std::string& key, const SocketPoolEntry& entry);
  131. std::multimap<std::string, SocketPoolEntry>::iterator
  132. findSocketPoolEntry(const std::string& key);
  133. std::deque<std::unique_ptr<Command>> commands_;
  134. std::unique_ptr<RequestGroupMan> requestGroupMan_;
  135. std::unique_ptr<FileAllocationMan> fileAllocationMan_;
  136. std::unique_ptr<CheckIntegrityMan> checkIntegrityMan_;
  137. Option* option_;
  138. public:
  139. DownloadEngine(const std::shared_ptr<EventPoll>& eventPoll);
  140. ~DownloadEngine();
  141. // If oneshot is true, this function returns after one event polling
  142. // and performing action for them. This function returns 1 when
  143. // oneshot is true and there are still downloads to be
  144. // processed. Otherwise, returns 0.
  145. int run(bool oneshot=false);
  146. bool addSocketForReadCheck(const std::shared_ptr<SocketCore>& socket,
  147. Command* command);
  148. bool deleteSocketForReadCheck(const std::shared_ptr<SocketCore>& socket,
  149. Command* command);
  150. bool addSocketForWriteCheck(const std::shared_ptr<SocketCore>& socket,
  151. Command* command);
  152. bool deleteSocketForWriteCheck(const std::shared_ptr<SocketCore>& socket,
  153. Command* command);
  154. #ifdef ENABLE_ASYNC_DNS
  155. bool addNameResolverCheck(const std::shared_ptr<AsyncNameResolver>& resolver,
  156. Command* command);
  157. bool deleteNameResolverCheck(const std::shared_ptr<AsyncNameResolver>& resolver,
  158. Command* command);
  159. #endif // ENABLE_ASYNC_DNS
  160. void addCommand(std::vector<std::unique_ptr<Command>> commands);
  161. void addCommand(std::unique_ptr<Command> command);
  162. const std::unique_ptr<RequestGroupMan>& getRequestGroupMan() const
  163. {
  164. return requestGroupMan_;
  165. }
  166. void setRequestGroupMan(std::unique_ptr<RequestGroupMan> rgman);
  167. const std::unique_ptr<FileAllocationMan>& getFileAllocationMan() const
  168. {
  169. return fileAllocationMan_;
  170. }
  171. void setFileAllocationMan(std::unique_ptr<FileAllocationMan> faman);
  172. const std::unique_ptr<CheckIntegrityMan>& getCheckIntegrityMan() const
  173. {
  174. return checkIntegrityMan_;
  175. }
  176. void setCheckIntegrityMan(std::unique_ptr<CheckIntegrityMan> ciman);
  177. Option* getOption() const
  178. {
  179. return option_;
  180. }
  181. void setOption(Option* op)
  182. {
  183. option_ = op;
  184. }
  185. void setStatCalc(std::unique_ptr<StatCalc> statCalc);
  186. bool isHaltRequested() const
  187. {
  188. return haltRequested_;
  189. }
  190. bool isForceHaltRequested() const
  191. {
  192. return haltRequested_ >= 2;
  193. }
  194. void requestHalt();
  195. void requestForceHalt();
  196. void setNoWait(bool b);
  197. void addRoutineCommand(std::unique_ptr<Command> command);
  198. void poolSocket(const std::string& ipaddr, uint16_t port,
  199. const std::string& username,
  200. const std::string& proxyhost, uint16_t proxyport,
  201. const std::shared_ptr<SocketCore>& sock,
  202. const std::string& options,
  203. time_t timeout = 15);
  204. void poolSocket(const std::shared_ptr<Request>& request,
  205. const std::string& username,
  206. const std::shared_ptr<Request>& proxyRequest,
  207. const std::shared_ptr<SocketCore>& socket,
  208. const std::string& options,
  209. time_t timeout = 15);
  210. void poolSocket(const std::string& ipaddr, uint16_t port,
  211. const std::string& proxyhost, uint16_t proxyport,
  212. const std::shared_ptr<SocketCore>& sock,
  213. time_t timeout = 15);
  214. void poolSocket(const std::shared_ptr<Request>& request,
  215. const std::shared_ptr<Request>& proxyRequest,
  216. const std::shared_ptr<SocketCore>& socket,
  217. time_t timeout = 15);
  218. std::shared_ptr<SocketCore> popPooledSocket
  219. (const std::string& ipaddr,
  220. uint16_t port,
  221. const std::string& proxyhost, uint16_t proxyport);
  222. std::shared_ptr<SocketCore> popPooledSocket
  223. (std::string& options,
  224. const std::string& ipaddr,
  225. uint16_t port,
  226. const std::string& username,
  227. const std::string& proxyhost, uint16_t proxyport);
  228. std::shared_ptr<SocketCore>
  229. popPooledSocket
  230. (const std::vector<std::string>& ipaddrs, uint16_t port);
  231. std::shared_ptr<SocketCore>
  232. popPooledSocket
  233. (std::string& options,
  234. const std::vector<std::string>& ipaddrs,
  235. uint16_t port,
  236. const std::string& username);
  237. const std::unique_ptr<CookieStorage>& getCookieStorage() const;
  238. #ifdef ENABLE_BITTORRENT
  239. const std::unique_ptr<BtRegistry>& getBtRegistry() const
  240. {
  241. return btRegistry_;
  242. }
  243. #endif // ENABLE_BITTORRENT
  244. cuid_t newCUID();
  245. const std::string& findCachedIPAddress
  246. (const std::string& hostname, uint16_t port) const;
  247. template<typename OutputIterator>
  248. void findAllCachedIPAddresses
  249. (OutputIterator out, const std::string& hostname, uint16_t port) const
  250. {
  251. dnsCache_->findAll(out, hostname, port);
  252. }
  253. void cacheIPAddress
  254. (const std::string& hostname, const std::string& ipaddr, uint16_t port);
  255. void markBadIPAddress
  256. (const std::string& hostname, const std::string& ipaddr, uint16_t port);
  257. void removeCachedIPAddress(const std::string& hostname, uint16_t port);
  258. void setAuthConfigFactory(std::unique_ptr<AuthConfigFactory> factory);
  259. const std::unique_ptr<AuthConfigFactory>& getAuthConfigFactory() const;
  260. void setRefreshInterval(int64_t interval);
  261. const std::string getSessionId() const
  262. {
  263. return sessionId_;
  264. }
  265. #ifdef HAVE_ARES_ADDR_NODE
  266. void setAsyncDNSServers(ares_addr_node* asyncDNSServers);
  267. ares_addr_node* getAsyncDNSServers() const
  268. {
  269. return asyncDNSServers_;
  270. }
  271. #endif // HAVE_ARES_ADDR_NODE
  272. #ifdef ENABLE_WEBSOCKET
  273. void setWebSocketSessionMan(std::unique_ptr<rpc::WebSocketSessionMan> wsman);
  274. const std::unique_ptr<rpc::WebSocketSessionMan>& getWebSocketSessionMan()
  275. const
  276. {
  277. return webSocketSessionMan_;
  278. }
  279. #endif // ENABLE_WEBSOCKET
  280. };
  281. } // namespace aria2
  282. #endif // D_DOWNLOAD_ENGINE_H