DownloadEngine.h 11 KB

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