DownloadEngine.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 "Socket.h"
  37. #include "NameResolver.h"
  38. #include "StatCalc.h"
  39. #include "DownloadResult.h"
  40. #include "RequestGroup.h"
  41. #include "RequestGroupMan.h"
  42. #include "FileAllocationMan.h"
  43. #include "CheckIntegrityMan.h"
  44. #include "Util.h"
  45. #include "LogFactory.h"
  46. #include "TimeA2.h"
  47. #include <unistd.h>
  48. #include <sys/types.h>
  49. #include <sys/stat.h>
  50. #include <fcntl.h>
  51. #include <signal.h>
  52. volatile sig_atomic_t globalHaltRequested;
  53. SocketEntry::SocketEntry(const SocketHandle& socket,
  54. Command* command,
  55. TYPE type):
  56. socket(socket), command(command), type(type) {}
  57. bool SocketEntry::operator==(const SocketEntry& entry)
  58. {
  59. return socket == entry.socket &&
  60. command == entry.command &&
  61. type == entry.type;
  62. }
  63. #ifdef ENABLE_ASYNC_DNS
  64. NameResolverEntry::NameResolverEntry(const NameResolverHandle& nameResolver,
  65. Command* command):
  66. nameResolver(nameResolver), command(command) {}
  67. bool NameResolverEntry::operator==(const NameResolverEntry& entry)
  68. {
  69. return nameResolver == entry.nameResolver &&
  70. command == entry.command;
  71. }
  72. #endif // ENABLE_ASYNC_DNS
  73. DownloadEngine::DownloadEngine():logger(LogFactory::getInstance()),
  74. _statCalc(0),
  75. _haltRequested(false),
  76. noWait(false),
  77. _requestGroupMan(0),
  78. _fileAllocationMan(0),
  79. _checkIntegrityMan(0)
  80. {}
  81. DownloadEngine::~DownloadEngine() {
  82. cleanQueue();
  83. }
  84. void DownloadEngine::cleanQueue() {
  85. for_each(commands.begin(), commands.end(), Deleter());
  86. commands.clear();
  87. }
  88. void DownloadEngine::executeCommand(Command::STATUS statusFilter)
  89. {
  90. int32_t max = commands.size();
  91. for(int32_t i = 0; i < max; i++) {
  92. Command* com = commands.front();
  93. commands.pop_front();
  94. if(com->statusMatch(statusFilter)) {
  95. if(com->execute()) {
  96. delete com;
  97. } else {
  98. com->transitStatus();
  99. }
  100. } else {
  101. commands.push_back(com);
  102. }
  103. }
  104. }
  105. void DownloadEngine::run() {
  106. Time cp;
  107. cp.setTimeInSec(0);
  108. Commands activeCommands;
  109. while(!commands.empty()) {
  110. if(cp.elapsed(1)) {
  111. cp.reset();
  112. executeCommand(Command::STATUS_ALL);
  113. } else {
  114. executeCommand(Command::STATUS_ACTIVE);
  115. }
  116. afterEachIteration();
  117. if(!commands.empty()) {
  118. waitData();
  119. }
  120. noWait = false;
  121. calculateStatistics();
  122. }
  123. onEndOfRun();
  124. }
  125. void DownloadEngine::shortSleep() const {
  126. struct timeval tv;
  127. tv.tv_sec = 0;
  128. tv.tv_usec = 1000;
  129. fd_set rfds;
  130. FD_ZERO(&rfds);
  131. select(0, &rfds, NULL, NULL, &tv);
  132. }
  133. void DownloadEngine::waitData() {
  134. fd_set rfds;
  135. fd_set wfds;
  136. int32_t retval = 0;
  137. struct timeval tv;
  138. memcpy(&rfds, &rfdset, sizeof(fd_set));
  139. memcpy(&wfds, &wfdset, sizeof(fd_set));
  140. tv.tv_sec = noWait ? 0 : 1;
  141. tv.tv_usec = 0;
  142. retval = select(fdmax+1, &rfds, &wfds, NULL, &tv);
  143. if(retval > 0) {
  144. for(SocketEntries::iterator itr = socketEntries.begin();
  145. itr != socketEntries.end(); ++itr) {
  146. SocketEntry& entry = *itr;
  147. if(FD_ISSET(entry.socket->getSockfd(), &rfds) ||
  148. FD_ISSET(entry.socket->getSockfd(), &wfds)) {
  149. entry.command->setStatusActive();
  150. }
  151. }
  152. #ifdef ENABLE_ASYNC_DNS
  153. for(NameResolverEntries::iterator itr = nameResolverEntries.begin();
  154. itr != nameResolverEntries.end(); ++itr) {
  155. NameResolverEntry& entry = *itr;
  156. entry.nameResolver->process(&rfds, &wfds);
  157. switch(entry.nameResolver->getStatus()) {
  158. case NameResolver::STATUS_SUCCESS:
  159. case NameResolver::STATUS_ERROR:
  160. entry.command->setStatusActive();
  161. break;
  162. default:
  163. break;
  164. }
  165. }
  166. #endif // ENABLE_ASYNC_DNS
  167. }
  168. }
  169. void DownloadEngine::updateFdSet() {
  170. fdmax = 0;
  171. FD_ZERO(&rfdset);
  172. FD_ZERO(&wfdset);
  173. #ifdef ENABLE_ASYNC_DNS
  174. for(NameResolverEntries::iterator itr = nameResolverEntries.begin();
  175. itr != nameResolverEntries.end(); ++itr) {
  176. NameResolverEntry& entry = *itr;
  177. int32_t fd = entry.nameResolver->getFds(&rfdset, &wfdset);
  178. if(fdmax < fd) {
  179. fdmax = fd;
  180. }
  181. }
  182. #endif // ENABLE_ASYNC_DNS
  183. for(SocketEntries::iterator itr = socketEntries.begin();
  184. itr != socketEntries.end(); ++itr) {
  185. SocketEntry& entry = *itr;
  186. int32_t fd = entry.socket->getSockfd();
  187. switch(entry.type) {
  188. case SocketEntry::TYPE_RD:
  189. FD_SET(fd, &rfdset);
  190. break;
  191. case SocketEntry::TYPE_WR:
  192. FD_SET(fd, &wfdset);
  193. break;
  194. }
  195. if(fdmax < fd) {
  196. fdmax = fd;
  197. }
  198. }
  199. }
  200. bool DownloadEngine::addSocket(const SocketEntry& entry) {
  201. SocketEntries::iterator itr =
  202. find(socketEntries.begin(), socketEntries.end(), entry);
  203. if(itr == socketEntries.end()) {
  204. socketEntries.push_back(entry);
  205. updateFdSet();
  206. return true;
  207. } else {
  208. return false;
  209. }
  210. }
  211. bool DownloadEngine::deleteSocket(const SocketEntry& entry) {
  212. SocketEntries::iterator itr =
  213. find(socketEntries.begin(), socketEntries.end(), entry);
  214. if(itr == socketEntries.end()) {
  215. return false;
  216. } else {
  217. socketEntries.erase(itr);
  218. updateFdSet();
  219. return true;
  220. }
  221. }
  222. bool DownloadEngine::addSocketForReadCheck(const SocketHandle& socket,
  223. Command* command) {
  224. SocketEntry entry(socket, command, SocketEntry::TYPE_RD);
  225. return addSocket(entry);
  226. }
  227. bool DownloadEngine::deleteSocketForReadCheck(const SocketHandle& socket,
  228. Command* command) {
  229. SocketEntry entry(socket, command, SocketEntry::TYPE_RD);
  230. return deleteSocket(entry);
  231. }
  232. bool DownloadEngine::addSocketForWriteCheck(const SocketHandle& socket,
  233. Command* command) {
  234. SocketEntry entry(socket, command, SocketEntry::TYPE_WR);
  235. return addSocket(entry);
  236. }
  237. bool DownloadEngine::deleteSocketForWriteCheck(const SocketHandle& socket,
  238. Command* command) {
  239. SocketEntry entry(socket, command, SocketEntry::TYPE_WR);
  240. return deleteSocket(entry);
  241. }
  242. void DownloadEngine::calculateStatistics()
  243. {
  244. if(!_statCalc.isNull()) {
  245. _statCalc->calculateStat(_requestGroupMan, _fileAllocationMan, _checkIntegrityMan);
  246. }
  247. }
  248. void DownloadEngine::onEndOfRun()
  249. {
  250. _requestGroupMan->closeFile();
  251. _requestGroupMan->save();
  252. }
  253. void DownloadEngine::afterEachIteration()
  254. {
  255. if(globalHaltRequested) {
  256. globalHaltRequested = false;
  257. _haltRequested = true;
  258. _requestGroupMan->halt();
  259. }
  260. }
  261. void DownloadEngine::fillCommand()
  262. {
  263. addCommand(_requestGroupMan->getInitialCommands(this));
  264. }
  265. void DownloadEngine::setStatCalc(const StatCalcHandle& statCalc)
  266. {
  267. _statCalc = statCalc;
  268. }
  269. void DownloadEngine::addCommand(const Commands& commands)
  270. {
  271. this->commands.insert(this->commands.end(), commands.begin(), commands.end());
  272. }
  273. #ifdef ENABLE_ASYNC_DNS
  274. bool DownloadEngine::addNameResolverCheck(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. nameResolverEntries.push_back(entry);
  282. updateFdSet();
  283. return true;
  284. } else {
  285. return false;
  286. }
  287. }
  288. bool DownloadEngine::deleteNameResolverCheck(const NameResolverHandle& resolver,
  289. Command* command) {
  290. NameResolverEntry entry(resolver, command);
  291. NameResolverEntries::iterator itr = find(nameResolverEntries.begin(),
  292. nameResolverEntries.end(),
  293. entry);
  294. if(itr == nameResolverEntries.end()) {
  295. return false;
  296. } else {
  297. nameResolverEntries.erase(itr);
  298. updateFdSet();
  299. return true;
  300. }
  301. }
  302. #endif // ENABLE_ASYNC_DNS