DownloadEngine.h 4.5 KB

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