DownloadEngine.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. void DownloadEngine::waitData(Commands& activeCommands) {
  105. fd_set rfds;
  106. fd_set wfds;
  107. int retval = 0;
  108. struct timeval tv;
  109. memcpy(&rfds, &rfdset, sizeof(fd_set));
  110. memcpy(&wfds, &wfdset, sizeof(fd_set));
  111. tv.tv_sec = 1;
  112. tv.tv_usec = 0;
  113. retval = select(fdmax+1, &rfds, &wfds, NULL, &tv);
  114. if(retval > 0) {
  115. for(SocketEntries::iterator itr = socketEntries.begin();
  116. itr != socketEntries.end(); ++itr) {
  117. SocketEntry& entry = *itr;
  118. if(FD_ISSET(entry.socket->getSockfd(), &rfds) ||
  119. FD_ISSET(entry.socket->getSockfd(), &wfds)) {
  120. if(find(activeCommands.begin(), activeCommands.end(), entry.command) == activeCommands.end()) {
  121. activeCommands.push_back(entry.command);
  122. }
  123. }
  124. }
  125. #ifdef ENABLE_ASYNC_DNS
  126. for(NameResolverEntries::iterator itr = nameResolverEntries.begin();
  127. itr != nameResolverEntries.end(); ++itr) {
  128. NameResolverEntry& entry = *itr;
  129. entry.nameResolver->process(&rfds, &wfds);
  130. switch(entry.nameResolver->getStatus()) {
  131. case NameResolver::STATUS_SUCCESS:
  132. case NameResolver::STATUS_ERROR:
  133. if(find(activeCommands.begin(), activeCommands.end(), entry.command) == activeCommands.end()) {
  134. activeCommands.push_back(entry.command);
  135. }
  136. break;
  137. default:
  138. break;
  139. }
  140. }
  141. #endif // ENABLE_ASYNC_DNS
  142. }
  143. }
  144. void DownloadEngine::updateFdSet() {
  145. fdmax = 0;
  146. FD_ZERO(&rfdset);
  147. FD_ZERO(&wfdset);
  148. #ifdef ENABLE_ASYNC_DNS
  149. for(NameResolverEntries::iterator itr = nameResolverEntries.begin();
  150. itr != nameResolverEntries.end(); ++itr) {
  151. NameResolverEntry& entry = *itr;
  152. int fd = entry.nameResolver->getFds(&rfdset, &wfdset);
  153. if(fdmax < fd) {
  154. fdmax = fd;
  155. }
  156. }
  157. #endif // ENABLE_ASYNC_DNS
  158. for(SocketEntries::iterator itr = socketEntries.begin();
  159. itr != socketEntries.end(); ++itr) {
  160. SocketEntry& entry = *itr;
  161. int fd = entry.socket->getSockfd();
  162. switch(entry.type) {
  163. case SocketEntry::TYPE_RD:
  164. FD_SET(fd, &rfdset);
  165. break;
  166. case SocketEntry::TYPE_WR:
  167. FD_SET(fd, &wfdset);
  168. break;
  169. }
  170. if(fdmax < fd) {
  171. fdmax = fd;
  172. }
  173. }
  174. }
  175. bool DownloadEngine::addSocket(const SocketEntry& entry) {
  176. SocketEntries::iterator itr =
  177. find(socketEntries.begin(), socketEntries.end(), entry);
  178. if(itr == socketEntries.end()) {
  179. socketEntries.push_back(entry);
  180. updateFdSet();
  181. return true;
  182. } else {
  183. return false;
  184. }
  185. }
  186. bool DownloadEngine::deleteSocket(const SocketEntry& entry) {
  187. SocketEntries::iterator itr =
  188. find(socketEntries.begin(), socketEntries.end(), entry);
  189. if(itr == socketEntries.end()) {
  190. return false;
  191. } else {
  192. socketEntries.erase(itr);
  193. updateFdSet();
  194. return true;
  195. }
  196. }
  197. bool DownloadEngine::addSocketForReadCheck(const SocketHandle& socket,
  198. Command* command) {
  199. SocketEntry entry(socket, command, SocketEntry::TYPE_RD);
  200. return addSocket(entry);
  201. }
  202. bool DownloadEngine::deleteSocketForReadCheck(const SocketHandle& socket,
  203. Command* command) {
  204. SocketEntry entry(socket, command, SocketEntry::TYPE_RD);
  205. return deleteSocket(entry);
  206. }
  207. bool DownloadEngine::addSocketForWriteCheck(const SocketHandle& socket,
  208. Command* command) {
  209. SocketEntry entry(socket, command, SocketEntry::TYPE_WR);
  210. return addSocket(entry);
  211. }
  212. bool DownloadEngine::deleteSocketForWriteCheck(const SocketHandle& socket,
  213. Command* command) {
  214. SocketEntry entry(socket, command, SocketEntry::TYPE_WR);
  215. return deleteSocket(entry);
  216. }
  217. #ifdef ENABLE_ASYNC_DNS
  218. bool DownloadEngine::addNameResolverCheck(const NameResolverHandle& resolver,
  219. Command* command) {
  220. NameResolverEntry entry(resolver, command);
  221. NameResolverEntries::iterator itr = find(nameResolverEntries.begin(),
  222. nameResolverEntries.end(),
  223. entry);
  224. if(itr == nameResolverEntries.end()) {
  225. nameResolverEntries.push_back(entry);
  226. updateFdSet();
  227. return true;
  228. } else {
  229. return false;
  230. }
  231. }
  232. bool DownloadEngine::deleteNameResolverCheck(const NameResolverHandle& resolver,
  233. Command* command) {
  234. NameResolverEntry entry(resolver, command);
  235. NameResolverEntries::iterator itr = find(nameResolverEntries.begin(),
  236. nameResolverEntries.end(),
  237. entry);
  238. if(itr == nameResolverEntries.end()) {
  239. return false;
  240. } else {
  241. nameResolverEntries.erase(itr);
  242. updateFdSet();
  243. return true;
  244. }
  245. }
  246. #endif // ENABLE_ASYNC_DNS