DownloadEngine.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #include "DownloadEngine.h"
  36. #include "Util.h"
  37. #include "LogFactory.h"
  38. #include "TimeA2.h"
  39. #include <unistd.h>
  40. #include <sys/types.h>
  41. #include <sys/stat.h>
  42. #include <fcntl.h>
  43. #include <errno.h>
  44. #include <algorithm>
  45. using namespace std;
  46. DownloadEngine::DownloadEngine():logger(LogFactory::getInstance()),
  47. noWait(false),
  48. _requestGroupMan(0),
  49. _fileAllocationMan(0) {}
  50. DownloadEngine::~DownloadEngine() {
  51. cleanQueue();
  52. }
  53. void DownloadEngine::cleanQueue() {
  54. for_each(commands.begin(), commands.end(), Deleter());
  55. commands.clear();
  56. }
  57. void DownloadEngine::executeCommand(Command::STATUS statusFilter)
  58. {
  59. int max = commands.size();
  60. for(int i = 0; i < max; i++) {
  61. Command* com = commands.front();
  62. commands.pop_front();
  63. if(com->statusMatch(statusFilter)) {
  64. if(com->execute()) {
  65. delete com;
  66. } else {
  67. com->transitStatus();
  68. }
  69. } else {
  70. commands.push_back(com);
  71. }
  72. }
  73. }
  74. void DownloadEngine::run() {
  75. initStatistics();
  76. Time cp;
  77. cp.setTimeInSec(0);
  78. Commands activeCommands;
  79. while(!commands.empty()) {
  80. if(cp.elapsed(1)) {
  81. cp.reset();
  82. executeCommand(Command::STATUS_ALL);
  83. } else {
  84. executeCommand(Command::STATUS_ACTIVE);
  85. }
  86. afterEachIteration();
  87. if(!commands.empty()) {
  88. waitData();
  89. }
  90. noWait = false;
  91. calculateStatistics();
  92. }
  93. onEndOfRun();
  94. }
  95. void DownloadEngine::shortSleep() const {
  96. struct timeval tv;
  97. tv.tv_sec = 0;
  98. tv.tv_usec = 1000;
  99. fd_set rfds;
  100. FD_ZERO(&rfds);
  101. select(0, &rfds, NULL, NULL, &tv);
  102. }
  103. void DownloadEngine::waitData() {
  104. fd_set rfds;
  105. fd_set wfds;
  106. int retval = 0;
  107. struct timeval tv;
  108. memcpy(&rfds, &rfdset, sizeof(fd_set));
  109. memcpy(&wfds, &wfdset, sizeof(fd_set));
  110. tv.tv_sec = noWait ? 0 : 1;
  111. tv.tv_usec = 0;
  112. retval = select(fdmax+1, &rfds, &wfds, NULL, &tv);
  113. if(retval > 0) {
  114. for(SocketEntries::iterator itr = socketEntries.begin();
  115. itr != socketEntries.end(); ++itr) {
  116. SocketEntry& entry = *itr;
  117. if(FD_ISSET(entry.socket->getSockfd(), &rfds) ||
  118. FD_ISSET(entry.socket->getSockfd(), &wfds)) {
  119. entry.command->setStatusActive();
  120. }
  121. }
  122. #ifdef ENABLE_ASYNC_DNS
  123. for(NameResolverEntries::iterator itr = nameResolverEntries.begin();
  124. itr != nameResolverEntries.end(); ++itr) {
  125. NameResolverEntry& entry = *itr;
  126. entry.nameResolver->process(&rfds, &wfds);
  127. switch(entry.nameResolver->getStatus()) {
  128. case NameResolver::STATUS_SUCCESS:
  129. case NameResolver::STATUS_ERROR:
  130. entry.command->setStatusActive();
  131. break;
  132. default:
  133. break;
  134. }
  135. }
  136. #endif // ENABLE_ASYNC_DNS
  137. }
  138. }
  139. void DownloadEngine::updateFdSet() {
  140. fdmax = 0;
  141. FD_ZERO(&rfdset);
  142. FD_ZERO(&wfdset);
  143. #ifdef ENABLE_ASYNC_DNS
  144. for(NameResolverEntries::iterator itr = nameResolverEntries.begin();
  145. itr != nameResolverEntries.end(); ++itr) {
  146. NameResolverEntry& entry = *itr;
  147. int fd = entry.nameResolver->getFds(&rfdset, &wfdset);
  148. if(fdmax < fd) {
  149. fdmax = fd;
  150. }
  151. }
  152. #endif // ENABLE_ASYNC_DNS
  153. for(SocketEntries::iterator itr = socketEntries.begin();
  154. itr != socketEntries.end(); ++itr) {
  155. SocketEntry& entry = *itr;
  156. int fd = entry.socket->getSockfd();
  157. switch(entry.type) {
  158. case SocketEntry::TYPE_RD:
  159. FD_SET(fd, &rfdset);
  160. break;
  161. case SocketEntry::TYPE_WR:
  162. FD_SET(fd, &wfdset);
  163. break;
  164. }
  165. if(fdmax < fd) {
  166. fdmax = fd;
  167. }
  168. }
  169. }
  170. bool DownloadEngine::addSocket(const SocketEntry& entry) {
  171. SocketEntries::iterator itr =
  172. find(socketEntries.begin(), socketEntries.end(), entry);
  173. if(itr == socketEntries.end()) {
  174. socketEntries.push_back(entry);
  175. updateFdSet();
  176. return true;
  177. } else {
  178. return false;
  179. }
  180. }
  181. bool DownloadEngine::deleteSocket(const SocketEntry& entry) {
  182. SocketEntries::iterator itr =
  183. find(socketEntries.begin(), socketEntries.end(), entry);
  184. if(itr == socketEntries.end()) {
  185. return false;
  186. } else {
  187. socketEntries.erase(itr);
  188. updateFdSet();
  189. return true;
  190. }
  191. }
  192. bool DownloadEngine::addSocketForReadCheck(const SocketHandle& socket,
  193. Command* command) {
  194. SocketEntry entry(socket, command, SocketEntry::TYPE_RD);
  195. return addSocket(entry);
  196. }
  197. bool DownloadEngine::deleteSocketForReadCheck(const SocketHandle& socket,
  198. Command* command) {
  199. SocketEntry entry(socket, command, SocketEntry::TYPE_RD);
  200. return deleteSocket(entry);
  201. }
  202. bool DownloadEngine::addSocketForWriteCheck(const SocketHandle& socket,
  203. Command* command) {
  204. SocketEntry entry(socket, command, SocketEntry::TYPE_WR);
  205. return addSocket(entry);
  206. }
  207. bool DownloadEngine::deleteSocketForWriteCheck(const SocketHandle& socket,
  208. Command* command) {
  209. SocketEntry entry(socket, command, SocketEntry::TYPE_WR);
  210. return deleteSocket(entry);
  211. }
  212. #ifdef ENABLE_ASYNC_DNS
  213. bool DownloadEngine::addNameResolverCheck(const NameResolverHandle& resolver,
  214. Command* command) {
  215. NameResolverEntry entry(resolver, command);
  216. NameResolverEntries::iterator itr = find(nameResolverEntries.begin(),
  217. nameResolverEntries.end(),
  218. entry);
  219. if(itr == nameResolverEntries.end()) {
  220. nameResolverEntries.push_back(entry);
  221. updateFdSet();
  222. return true;
  223. } else {
  224. return false;
  225. }
  226. }
  227. bool DownloadEngine::deleteNameResolverCheck(const NameResolverHandle& resolver,
  228. Command* command) {
  229. NameResolverEntry entry(resolver, command);
  230. NameResolverEntries::iterator itr = find(nameResolverEntries.begin(),
  231. nameResolverEntries.end(),
  232. entry);
  233. if(itr == nameResolverEntries.end()) {
  234. return false;
  235. } else {
  236. nameResolverEntries.erase(itr);
  237. updateFdSet();
  238. return true;
  239. }
  240. }
  241. #endif // ENABLE_ASYNC_DNS