DownloadEngine.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "SharedHandle.h"
  39. #include "Command.h"
  40. #include <deque>
  41. #include <map>
  42. #include "a2netcompat.h"
  43. namespace aria2 {
  44. class Logger;
  45. class Option;
  46. class NameResolver;
  47. class RequestGroupMan;
  48. class FileAllocationMan;
  49. class StatCalc;
  50. class CheckIntegrityMan;
  51. class SocketCore;
  52. class SocketEntry {
  53. public:
  54. enum TYPE {
  55. TYPE_RD,
  56. TYPE_WR,
  57. };
  58. SharedHandle<SocketCore> socket;
  59. Command* command;
  60. TYPE type;
  61. public:
  62. SocketEntry(const SharedHandle<SocketCore>& socket,
  63. Command* command,
  64. TYPE type);
  65. bool operator==(const SocketEntry& entry);
  66. };
  67. typedef std::deque<SocketEntry> SocketEntries;
  68. #ifdef ENABLE_ASYNC_DNS
  69. class NameResolverEntry {
  70. public:
  71. SharedHandle<NameResolver> nameResolver;
  72. Command* command;
  73. public:
  74. NameResolverEntry(const SharedHandle<NameResolver>& nameResolver,
  75. Command* command);
  76. bool operator==(const NameResolverEntry& entry);
  77. };
  78. typedef std::deque<NameResolverEntry> NameResolverEntries;
  79. #endif // ENABLE_ASYNC_DNS
  80. class DownloadEngine {
  81. private:
  82. void waitData();
  83. SocketEntries socketEntries;
  84. #ifdef ENABLE_ASYNC_DNS
  85. NameResolverEntries nameResolverEntries;
  86. #endif // ENABLE_ASYNC_DNS
  87. fd_set rfdset;
  88. fd_set wfdset;
  89. int fdmax;
  90. const Logger* logger;
  91. SharedHandle<StatCalc> _statCalc;
  92. bool _haltRequested;
  93. // key = IP address:port, value = Socket
  94. std::multimap<std::string, SharedHandle<SocketCore> > _socketPool;
  95. void shortSleep() const;
  96. bool addSocket(const SocketEntry& socketEntry);
  97. bool deleteSocket(const SocketEntry& socketEntry);
  98. /**
  99. * Delegates to StatCalc
  100. */
  101. void calculateStatistics();
  102. void onEndOfRun();
  103. void afterEachIteration();
  104. private:
  105. bool _noWait;
  106. std::deque<Command*> _routineCommands;
  107. public:
  108. std::deque<Command*> commands;
  109. SharedHandle<RequestGroupMan> _requestGroupMan;
  110. SharedHandle<FileAllocationMan> _fileAllocationMan;
  111. SharedHandle<CheckIntegrityMan> _checkIntegrityMan;
  112. const Option* option;
  113. DownloadEngine();
  114. virtual ~DownloadEngine();
  115. void run();
  116. void cleanQueue();
  117. void updateFdSet();
  118. bool addSocketForReadCheck(const SharedHandle<SocketCore>& socket,
  119. Command* command);
  120. bool deleteSocketForReadCheck(const SharedHandle<SocketCore>& socket,
  121. Command* command);
  122. bool addSocketForWriteCheck(const SharedHandle<SocketCore>& socket,
  123. Command* command);
  124. bool deleteSocketForWriteCheck(const SharedHandle<SocketCore>& socket,
  125. Command* command);
  126. #ifdef ENABLE_ASYNC_DNS
  127. bool addNameResolverCheck(const SharedHandle<NameResolver>& resolver,
  128. Command* command);
  129. bool deleteNameResolverCheck(const SharedHandle<NameResolver>& resolver,
  130. Command* command);
  131. #endif // ENABLE_ASYNC_DNS
  132. void addCommand(const Commands& commands);
  133. void fillCommand();
  134. void setStatCalc(const SharedHandle<StatCalc>& statCalc);
  135. bool isHaltRequested() const
  136. {
  137. return _haltRequested;
  138. }
  139. void requestHalt();
  140. void setNoWait(bool b);
  141. void addRoutineCommand(Command* command);
  142. void poolSocket(const std::string& ipaddr, uint16_t port,
  143. const SharedHandle<SocketCore>& sock);
  144. SharedHandle<SocketCore> popPooledSocket(const std::string& ipaddr,
  145. uint16_t port);
  146. };
  147. typedef SharedHandle<DownloadEngine> DownloadEngineHandle;
  148. } // namespace aria2
  149. #endif // _D_DOWNLOAD_ENGINE_H_