DownloadEngine.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "SharedHandle.h"
  42. #include "a2netcompat.h"
  43. #include "TimeA2.h"
  44. #include "a2io.h"
  45. #ifdef ENABLE_ASYNC_DNS
  46. # include "AsyncNameResolver.h"
  47. #endif // ENABLE_ASYNC_DNS
  48. #include "CUIDCounter.h"
  49. #include "FileAllocationMan.h"
  50. #include "CheckIntegrityMan.h"
  51. #include "DNSCache.h"
  52. namespace aria2 {
  53. class Logger;
  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. class DownloadEngine {
  67. private:
  68. void waitData();
  69. std::string _sessionId;
  70. SharedHandle<EventPoll> _eventPoll;
  71. Logger* logger;
  72. SharedHandle<StatCalc> _statCalc;
  73. bool _haltRequested;
  74. class SocketPoolEntry {
  75. private:
  76. SharedHandle<SocketCore> _socket;
  77. std::map<std::string, std::string> _options;
  78. time_t _timeout;
  79. Time _registeredTime;
  80. public:
  81. SocketPoolEntry(const SharedHandle<SocketCore>& socket,
  82. const std::map<std::string, std::string>& option,
  83. time_t timeout);
  84. ~SocketPoolEntry();
  85. bool isTimeout() const;
  86. const SharedHandle<SocketCore>& getSocket() const
  87. {
  88. return _socket;
  89. }
  90. const std::map<std::string, std::string>& getOptions() const
  91. {
  92. return _options;
  93. }
  94. };
  95. // key = IP address:port, value = SocketPoolEntry
  96. std::multimap<std::string, SocketPoolEntry> _socketPool;
  97. Time _lastSocketPoolScan;
  98. bool _noWait;
  99. static const time_t DEFAULT_REFRESH_INTERVAL = 1;
  100. time_t _refreshInterval;
  101. std::deque<Command*> _routineCommands;
  102. SharedHandle<CookieStorage> _cookieStorage;
  103. #ifdef ENABLE_BITTORRENT
  104. SharedHandle<BtRegistry> _btRegistry;
  105. #endif // ENABLE_BITTORRENT
  106. CUIDCounter _cuidCounter;
  107. SharedHandle<DNSCache> _dnsCache;
  108. SharedHandle<AuthConfigFactory> _authConfigFactory;
  109. /**
  110. * Delegates to StatCalc
  111. */
  112. void calculateStatistics();
  113. void onEndOfRun();
  114. void afterEachIteration();
  115. void poolSocket(const std::string& ipaddr,
  116. uint16_t port,
  117. const SocketPoolEntry& entry);
  118. std::multimap<std::string, SocketPoolEntry>::iterator
  119. findSocketPoolEntry(const std::string& ipaddr, uint16_t port);
  120. public:
  121. std::deque<Command*> commands;
  122. SharedHandle<RequestGroupMan> _requestGroupMan;
  123. SharedHandle<FileAllocationMan> _fileAllocationMan;
  124. SharedHandle<CheckIntegrityMan> _checkIntegrityMan;
  125. Option* option;
  126. DownloadEngine(const SharedHandle<EventPoll>& eventPoll);
  127. virtual ~DownloadEngine();
  128. void run();
  129. void cleanQueue();
  130. bool addSocketForReadCheck(const SharedHandle<SocketCore>& socket,
  131. Command* command);
  132. bool deleteSocketForReadCheck(const SharedHandle<SocketCore>& socket,
  133. Command* command);
  134. bool addSocketForWriteCheck(const SharedHandle<SocketCore>& socket,
  135. Command* command);
  136. bool deleteSocketForWriteCheck(const SharedHandle<SocketCore>& socket,
  137. Command* command);
  138. #ifdef ENABLE_ASYNC_DNS
  139. bool addNameResolverCheck(const SharedHandle<AsyncNameResolver>& resolver,
  140. Command* command);
  141. bool deleteNameResolverCheck(const SharedHandle<AsyncNameResolver>& resolver,
  142. Command* command);
  143. #endif // ENABLE_ASYNC_DNS
  144. void addCommand(const std::deque<Command*>& commands);
  145. void setStatCalc(const SharedHandle<StatCalc>& statCalc);
  146. bool isHaltRequested() const
  147. {
  148. return _haltRequested;
  149. }
  150. void requestHalt();
  151. void setNoWait(bool b);
  152. void addRoutineCommand(Command* command);
  153. void poolSocket(const std::string& ipaddr, uint16_t port,
  154. const SharedHandle<SocketCore>& sock,
  155. const std::map<std::string, std::string>& options,
  156. time_t timeout = 15);
  157. void poolSocket(const std::string& ipaddr, uint16_t port,
  158. const SharedHandle<SocketCore>& sock,
  159. time_t timeout = 15);
  160. void poolSocket(const SharedHandle<Request>& request,
  161. bool proxyDefined,
  162. const SharedHandle<SocketCore>& socket,
  163. const std::map<std::string, std::string>& options,
  164. time_t timeout = 15);
  165. void poolSocket(const SharedHandle<Request>& request,
  166. bool proxyDefined,
  167. const SharedHandle<SocketCore>& socket,
  168. time_t timeout = 15);
  169. SharedHandle<SocketCore> popPooledSocket(const std::string& ipaddr,
  170. uint16_t port);
  171. SharedHandle<SocketCore> popPooledSocket
  172. (std::map<std::string, std::string>& options,
  173. const std::string& ipaddr,
  174. uint16_t port);
  175. SharedHandle<SocketCore>
  176. popPooledSocket(const std::deque<std::string>& ipaddrs, uint16_t port);
  177. SharedHandle<SocketCore>
  178. popPooledSocket
  179. (std::map<std::string, std::string>& options,
  180. const std::deque<std::string>& ipaddrs,
  181. uint16_t port);
  182. const SharedHandle<CookieStorage>& getCookieStorage() const
  183. {
  184. return _cookieStorage;
  185. }
  186. #ifdef ENABLE_BITTORRENT
  187. const SharedHandle<BtRegistry>& getBtRegistry() const
  188. {
  189. return _btRegistry;
  190. }
  191. #endif // ENABLE_BITTORRENT
  192. cuid_t newCUID();
  193. const std::string& findCachedIPAddress
  194. (const std::string& hostname, uint16_t port) const;
  195. template<typename OutputIterator>
  196. void findAllCachedIPAddresses
  197. (OutputIterator out, const std::string& hostname, uint16_t port) const
  198. {
  199. _dnsCache->findAll(out, hostname, port);
  200. }
  201. void cacheIPAddress
  202. (const std::string& hostname, const std::string& ipaddr, uint16_t port);
  203. void markBadIPAddress
  204. (const std::string& hostname, const std::string& ipaddr, uint16_t port);
  205. void removeCachedIPAddress(const std::string& hostname, uint16_t port);
  206. void setAuthConfigFactory(const SharedHandle<AuthConfigFactory>& factory);
  207. const SharedHandle<AuthConfigFactory>& getAuthConfigFactory() const
  208. {
  209. return _authConfigFactory;
  210. }
  211. void setRefreshInterval(time_t interval);
  212. const std::string getSessionId() const
  213. {
  214. return _sessionId;
  215. }
  216. };
  217. typedef SharedHandle<DownloadEngine> DownloadEngineHandle;
  218. } // namespace aria2
  219. #endif // _D_DOWNLOAD_ENGINE_H_