DownloadEngine.h 10 KB

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