DownloadEngine.h 4.6 KB

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