DownloadEngine.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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():noWait(false), segmentMan(0) {
  47. logger = LogFactory::getInstance();
  48. }
  49. DownloadEngine::~DownloadEngine() {
  50. cleanQueue();
  51. delete segmentMan;
  52. }
  53. void DownloadEngine::cleanQueue() {
  54. for_each(commands.begin(), commands.end(), Deleter());
  55. commands.clear();
  56. }
  57. void DownloadEngine::run() {
  58. initStatistics();
  59. Time cp;
  60. cp.setTimeInSec(0);
  61. Commands activeCommands;
  62. while(!commands.empty()) {
  63. if(cp.elapsed(1)) {
  64. cp.reset();
  65. int max = commands.size();
  66. for(int i = 0; i < max; i++) {
  67. Command* com = commands.front();
  68. commands.pop_front();
  69. if(com->execute()) {
  70. delete com;
  71. }
  72. }
  73. } else {
  74. for(Commands::iterator itr = activeCommands.begin();
  75. itr != activeCommands.end(); itr++) {
  76. Commands::iterator comItr = find(commands.begin(), commands.end(),
  77. *itr);
  78. assert(comItr != commands.end());
  79. Command* command = *itr;
  80. commands.erase(comItr);
  81. if(command->execute()) {
  82. delete command;
  83. }
  84. }
  85. }
  86. afterEachIteration();
  87. activeCommands.clear();
  88. if(!noWait && !commands.empty()) {
  89. waitData(activeCommands);
  90. }
  91. noWait = false;
  92. calculateStatistics();
  93. }
  94. onEndOfRun();
  95. }
  96. void DownloadEngine::shortSleep() const {
  97. struct timeval tv;
  98. tv.tv_sec = 0;
  99. tv.tv_usec = 1000;
  100. fd_set rfds;
  101. FD_ZERO(&rfds);
  102. select(0, &rfds, NULL, NULL, &tv);
  103. }
  104. class SetDescriptor {
  105. private:
  106. int* max_ptr;
  107. fd_set* rfds_ptr;
  108. fd_set* wfds_ptr;
  109. public:
  110. SetDescriptor(int* max_ptr, fd_set* rfds_ptr, fd_set* wfds_ptr):
  111. max_ptr(max_ptr),
  112. rfds_ptr(rfds_ptr),
  113. wfds_ptr(wfds_ptr) {}
  114. void operator()(const SocketEntry& entry) {
  115. int fd = entry.socket->getSockfd();
  116. switch(entry.type) {
  117. case SocketEntry::TYPE_RD:
  118. FD_SET(fd, rfds_ptr);
  119. break;
  120. case SocketEntry::TYPE_WR:
  121. FD_SET(fd, wfds_ptr);
  122. break;
  123. }
  124. if(*max_ptr < fd) {
  125. *max_ptr = fd;
  126. }
  127. }
  128. #ifdef ENABLE_ASYNC_DNS
  129. void operator()(const NameResolverEntry& entry) {
  130. int tempFd = entry.nameResolver->getFds(rfds_ptr, wfds_ptr);
  131. if(*max_ptr < tempFd) {
  132. *max_ptr = tempFd;
  133. }
  134. }
  135. #endif // ENABLE_ASYNC_DNS
  136. };
  137. class AccumulateActiveCommand {
  138. private:
  139. Commands* activeCommands_ptr;
  140. fd_set* rfds_ptr;
  141. fd_set* wfds_ptr;
  142. public:
  143. AccumulateActiveCommand(Commands* activeCommands_ptr,
  144. fd_set* rfds_ptr,
  145. fd_set* wfds_ptr):
  146. activeCommands_ptr(activeCommands_ptr),
  147. rfds_ptr(rfds_ptr),
  148. wfds_ptr(wfds_ptr) {}
  149. void operator()(const SocketEntry& entry) {
  150. if(FD_ISSET(entry.socket->getSockfd(), rfds_ptr) ||
  151. FD_ISSET(entry.socket->getSockfd(), wfds_ptr)) {
  152. activeCommands_ptr->push_back(entry.command);
  153. }
  154. /*
  155. switch(entry.type) {
  156. case SocketEntry::TYPE_RD:
  157. if(FD_ISSET(entry.socket->getSockfd(), rfds_ptr)) {
  158. activeCommands_ptr->push_back(entry.command);
  159. }
  160. break;
  161. case SocketEntry::TYPE_WR:
  162. if(FD_ISSET(entry.socket->getSockfd(), wfds_ptr)) {
  163. activeCommands_ptr->push_back(entry.command);
  164. }
  165. break;
  166. }
  167. */
  168. }
  169. #ifdef ENABLE_ASYNC_DNS
  170. void operator()(const NameResolverEntry& entry) {
  171. entry.nameResolver->process(rfds_ptr, wfds_ptr);
  172. switch(entry.nameResolver->getStatus()) {
  173. case NameResolver::STATUS_SUCCESS:
  174. case NameResolver::STATUS_ERROR:
  175. activeCommands_ptr->push_back(entry.command);
  176. break;
  177. default:
  178. break;
  179. }
  180. }
  181. #endif // ENABLE_ASYNC_DNS
  182. };
  183. void DownloadEngine::waitData(Commands& activeCommands) {
  184. fd_set rfds;
  185. fd_set wfds;
  186. int retval = 0;
  187. struct timeval tv;
  188. memcpy(&rfds, &rfdset, sizeof(fd_set));
  189. memcpy(&wfds, &wfdset, sizeof(fd_set));
  190. tv.tv_sec = 1;
  191. tv.tv_usec = 0;
  192. retval = select(fdmax+1, &rfds, &wfds, NULL, &tv);
  193. if(retval > 0) {
  194. for_each(socketEntries.begin(), socketEntries.end(),
  195. AccumulateActiveCommand(&activeCommands, &rfds, &wfds));
  196. #ifdef ENABLE_ASYNC_DNS
  197. for_each(nameResolverEntries.begin(), nameResolverEntries.end(),
  198. AccumulateActiveCommand(&activeCommands, &rfds, &wfds));
  199. #endif // ENABLE_ASYNC_DNS
  200. sort(activeCommands.begin(), activeCommands.end());
  201. activeCommands.erase(unique(activeCommands.begin(),
  202. activeCommands.end()),
  203. activeCommands.end());
  204. }
  205. }
  206. void DownloadEngine::updateFdSet() {
  207. fdmax = 0;
  208. FD_ZERO(&rfdset);
  209. FD_ZERO(&wfdset);
  210. #ifdef ENABLE_ASYNC_DNS
  211. for_each(nameResolverEntries.begin(), nameResolverEntries.end(),
  212. SetDescriptor(&fdmax, &rfdset, &wfdset));
  213. #endif // ENABLE_ASYNC_DNS
  214. for_each(socketEntries.begin(), socketEntries.end(),
  215. SetDescriptor(&fdmax, &rfdset, &wfdset));
  216. }
  217. bool DownloadEngine::addSocket(const SocketEntry& entry) {
  218. SocketEntries::iterator itr =
  219. find(socketEntries.begin(), socketEntries.end(), entry);
  220. if(itr == socketEntries.end()) {
  221. socketEntries.push_back(entry);
  222. updateFdSet();
  223. return true;
  224. } else {
  225. return false;
  226. }
  227. }
  228. bool DownloadEngine::deleteSocket(const SocketEntry& entry) {
  229. SocketEntries::iterator itr =
  230. find(socketEntries.begin(), socketEntries.end(), entry);
  231. if(itr == socketEntries.end()) {
  232. return false;
  233. } else {
  234. socketEntries.erase(itr);
  235. updateFdSet();
  236. return true;
  237. }
  238. }
  239. bool DownloadEngine::addSocketForReadCheck(const SocketHandle& socket,
  240. Command* command) {
  241. SocketEntry entry(socket, command, SocketEntry::TYPE_RD);
  242. return addSocket(entry);
  243. }
  244. bool DownloadEngine::deleteSocketForReadCheck(const SocketHandle& socket,
  245. Command* command) {
  246. SocketEntry entry(socket, command, SocketEntry::TYPE_RD);
  247. return deleteSocket(entry);
  248. }
  249. bool DownloadEngine::addSocketForWriteCheck(const SocketHandle& socket,
  250. Command* command) {
  251. SocketEntry entry(socket, command, SocketEntry::TYPE_WR);
  252. return addSocket(entry);
  253. }
  254. bool DownloadEngine::deleteSocketForWriteCheck(const SocketHandle& socket,
  255. Command* command) {
  256. SocketEntry entry(socket, command, SocketEntry::TYPE_WR);
  257. return deleteSocket(entry);
  258. }
  259. #ifdef ENABLE_ASYNC_DNS
  260. bool DownloadEngine::addNameResolverCheck(const NameResolverHandle& resolver,
  261. Command* command) {
  262. NameResolverEntry entry(resolver, command);
  263. NameResolverEntries::iterator itr = find(nameResolverEntries.begin(),
  264. nameResolverEntries.end(),
  265. entry);
  266. if(itr == nameResolverEntries.end()) {
  267. nameResolverEntries.push_back(entry);
  268. updateFdSet();
  269. return true;
  270. } else {
  271. return false;
  272. }
  273. }
  274. bool DownloadEngine::deleteNameResolverCheck(const NameResolverHandle& resolver,
  275. Command* command) {
  276. NameResolverEntry entry(resolver, command);
  277. NameResolverEntries::iterator itr = find(nameResolverEntries.begin(),
  278. nameResolverEntries.end(),
  279. entry);
  280. if(itr == nameResolverEntries.end()) {
  281. return false;
  282. } else {
  283. nameResolverEntries.erase(itr);
  284. updateFdSet();
  285. return true;
  286. }
  287. }
  288. #endif // ENABLE_ASYNC_DNS