DownloadEngine.h 7.0 KB

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