DownloadEngine.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #ifndef _D_DOWNLOAD_ENGINE_H_
  23. #define _D_DOWNLOAD_ENGINE_H_
  24. #include "Command.h"
  25. #include "Socket.h"
  26. #include "SegmentMan.h"
  27. #include "common.h"
  28. #include "Logger.h"
  29. #include "Option.h"
  30. #ifdef ENABLE_ASYNC_DNS
  31. # include "NameResolver.h"
  32. #endif // ENABLE_ASYNC_DNS
  33. typedef deque<SocketHandle> Sockets;
  34. typedef deque<Command*> Commands;
  35. class SocketEntry {
  36. public:
  37. enum TYPE {
  38. TYPE_RD,
  39. TYPE_WR,
  40. };
  41. SocketHandle socket;
  42. Command* command;
  43. TYPE type;
  44. public:
  45. SocketEntry(const SocketHandle& socket,
  46. Command* command,
  47. TYPE type):
  48. socket(socket), command(command), type(type) {}
  49. ~SocketEntry() {}
  50. bool operator==(const SocketEntry& entry) {
  51. return socket == entry.socket &&
  52. command == entry.command &&
  53. type == entry.type;
  54. }
  55. };
  56. typedef deque<SocketEntry> SocketEntries;
  57. #ifdef ENABLE_ASYNC_DNS
  58. class NameResolverEntry {
  59. public:
  60. NameResolverHandle nameResolver;
  61. Command* command;
  62. public:
  63. NameResolverEntry(const NameResolverHandle& nameResolver,
  64. Command* command):
  65. nameResolver(nameResolver), command(command) {}
  66. ~NameResolverEntry() {}
  67. bool operator==(const NameResolverEntry& entry) {
  68. return nameResolver == entry.nameResolver &&
  69. command == entry.command;
  70. }
  71. };
  72. typedef deque<NameResolverEntry> NameResolverEntries;
  73. #endif // ENABLE_ASYNC_DNS
  74. class DownloadEngine {
  75. private:
  76. void waitData(Commands& activeCommands);
  77. SocketEntries socketEntries;
  78. #ifdef ENABLE_ASYNC_DNS
  79. NameResolverEntries nameResolverEntries;
  80. #endif // ENABLE_ASYNC_DNS
  81. fd_set rfdset;
  82. fd_set wfdset;
  83. int fdmax;
  84. void shortSleep() const;
  85. bool addSocket(const SocketEntry& socketEntry);
  86. bool deleteSocket(const SocketEntry& socketEntry);
  87. protected:
  88. const Logger* logger;
  89. virtual void initStatistics() = 0;
  90. virtual void calculateStatistics() = 0;
  91. virtual void onEndOfRun() = 0;
  92. virtual void afterEachIteration() {}
  93. public:
  94. bool noWait;
  95. Commands commands;
  96. SegmentMan* segmentMan;
  97. const Option* option;
  98. DownloadEngine();
  99. virtual ~DownloadEngine();
  100. void run();
  101. void cleanQueue();
  102. void updateFdSet();
  103. bool addSocketForReadCheck(const SocketHandle& socket,
  104. Command* command);
  105. bool deleteSocketForReadCheck(const SocketHandle& socket,
  106. Command* command);
  107. bool addSocketForWriteCheck(const SocketHandle& socket,
  108. Command* command);
  109. bool deleteSocketForWriteCheck(const SocketHandle& socket,
  110. Command* command);
  111. #ifdef ENABLE_ASYNC_DNS
  112. bool addNameResolverCheck(const NameResolverHandle& resolver,
  113. Command* command);
  114. bool deleteNameResolverCheck(const NameResolverHandle& resolver,
  115. Command* command);
  116. #endif // ENABLE_ASYNC_DNS
  117. };
  118. #endif // _D_DOWNLOAD_ENGINE_H_