AbstractCommand.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 "AbstractCommand.h"
  36. #include "DlAbortEx.h"
  37. #include "DlRetryEx.h"
  38. #include "InitiateConnectionCommandFactory.h"
  39. #include "Util.h"
  40. #include "message.h"
  41. #include "SleepCommand.h"
  42. #include "prefs.h"
  43. #include "DNSCache.h"
  44. AbstractCommand::AbstractCommand(int cuid,
  45. const RequestHandle& req,
  46. DownloadEngine* e,
  47. const SocketHandle& s):
  48. Command(cuid), req(req), e(e), socket(s),
  49. checkSocketIsReadable(false), checkSocketIsWritable(false),
  50. nameResolverCheck(false) {
  51. setReadCheckSocket(socket);
  52. timeout = this->e->option->getAsInt(PREF_TIMEOUT);
  53. }
  54. AbstractCommand::~AbstractCommand() {
  55. disableReadCheckSocket();
  56. disableWriteCheckSocket();
  57. }
  58. bool AbstractCommand::execute() {
  59. try {
  60. if(e->segmentMan->finished()) {
  61. logger->debug("CUID#%d - finished.", cuid);
  62. return true;
  63. }
  64. PeerStatHandle peerStat = e->segmentMan->getPeerStat(cuid);
  65. if(peerStat.get()) {
  66. if(peerStat->getStatus() == PeerStat::REQUEST_IDLE) {
  67. logger->info("CUID#%d - Request idle.", cuid);
  68. onAbort(0);
  69. req->resetUrl();
  70. tryReserved();
  71. return true;
  72. }
  73. }
  74. if(checkSocketIsReadable && readCheckTarget->isReadable(0) ||
  75. checkSocketIsWritable && writeCheckTarget->isWritable(0) ||
  76. #ifdef ENABLE_ASYNC_DNS
  77. nameResolverCheck && nameResolveFinished() ||
  78. #endif // ENABLE_ASYNC_DNS
  79. !checkSocketIsReadable && !checkSocketIsWritable && !nameResolverCheck) {
  80. checkPoint.reset();
  81. if(e->segmentMan->downloadStarted) {
  82. // TODO Segment::isNull(), Change method name, it is very confusing.
  83. if(segment->isNull()) {
  84. segment = e->segmentMan->getSegment(cuid);
  85. if(segment.isNull()) {
  86. logger->info(MSG_NO_SEGMENT_AVAILABLE, cuid);
  87. return prepareForRetry(1);
  88. }
  89. }
  90. }
  91. return executeInternal();
  92. } else {
  93. if(checkPoint.elapsed(timeout)) {
  94. throw new DlRetryEx(EX_TIME_OUT);
  95. }
  96. e->commands.push_back(this);
  97. return false;
  98. }
  99. } catch(DlAbortEx* err) {
  100. logger->error(MSG_DOWNLOAD_ABORTED, err, cuid);
  101. onAbort(err);
  102. delete(err);
  103. req->resetUrl();
  104. e->segmentMan->errors++;
  105. tryReserved();
  106. return true;
  107. } catch(DlRetryEx* err) {
  108. logger->error(MSG_RESTARTING_DOWNLOAD, err, cuid);
  109. req->addTryCount();
  110. bool isAbort = e->option->getAsInt(PREF_MAX_TRIES) != 0 &&
  111. req->getTryCount() >= e->option->getAsInt(PREF_MAX_TRIES);
  112. if(isAbort) {
  113. onAbort(err);
  114. }
  115. delete(err);
  116. if(isAbort) {
  117. logger->error(MSG_MAX_TRY, cuid, req->getTryCount());
  118. e->segmentMan->errors++;
  119. tryReserved();
  120. return true;
  121. } else {
  122. return prepareForRetry(e->option->getAsInt(PREF_RETRY_WAIT));
  123. }
  124. }
  125. }
  126. void AbstractCommand::tryReserved() {
  127. if(!e->segmentMan->reserved.empty()) {
  128. RequestHandle req = e->segmentMan->reserved.front();
  129. e->segmentMan->reserved.pop_front();
  130. Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, e);
  131. e->commands.push_back(command);
  132. }
  133. }
  134. bool AbstractCommand::prepareForRetry(int wait) {
  135. e->segmentMan->cancelSegment(cuid);
  136. Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, e);
  137. if(wait == 0) {
  138. e->commands.push_back(command);
  139. } else {
  140. SleepCommand* scom = new SleepCommand(cuid, e, command, wait);
  141. e->commands.push_back(scom);
  142. }
  143. return true;
  144. }
  145. void AbstractCommand::onAbort(RecoverableException* ex) {
  146. logger->debug(MSG_UNREGISTER_CUID, cuid);
  147. //e->segmentMan->unregisterId(cuid);
  148. e->segmentMan->cancelSegment(cuid);
  149. }
  150. void AbstractCommand::disableReadCheckSocket() {
  151. if(checkSocketIsReadable) {
  152. e->deleteSocketForReadCheck(readCheckTarget, this);
  153. checkSocketIsReadable = false;
  154. readCheckTarget = SocketHandle();
  155. }
  156. }
  157. void AbstractCommand::setReadCheckSocket(const SocketHandle& socket) {
  158. if(!socket->isOpen()) {
  159. disableReadCheckSocket();
  160. } else {
  161. if(checkSocketIsReadable) {
  162. if(readCheckTarget != socket) {
  163. e->deleteSocketForReadCheck(readCheckTarget, this);
  164. e->addSocketForReadCheck(socket, this);
  165. readCheckTarget = socket;
  166. }
  167. } else {
  168. e->addSocketForReadCheck(socket, this);
  169. checkSocketIsReadable = true;
  170. readCheckTarget = socket;
  171. }
  172. }
  173. }
  174. void AbstractCommand::disableWriteCheckSocket() {
  175. if(checkSocketIsWritable) {
  176. e->deleteSocketForWriteCheck(writeCheckTarget, this);
  177. checkSocketIsWritable = false;
  178. writeCheckTarget = SocketHandle();
  179. }
  180. }
  181. void AbstractCommand::setWriteCheckSocket(const SocketHandle& socket) {
  182. if(!socket->isOpen()) {
  183. disableWriteCheckSocket();
  184. } else {
  185. if(checkSocketIsWritable) {
  186. if(writeCheckTarget != socket) {
  187. e->deleteSocketForWriteCheck(writeCheckTarget, this);
  188. e->addSocketForWriteCheck(socket, this);
  189. writeCheckTarget = socket;
  190. }
  191. } else {
  192. e->addSocketForWriteCheck(socket, this);
  193. checkSocketIsWritable = true;
  194. writeCheckTarget = socket;
  195. }
  196. }
  197. }
  198. bool AbstractCommand::resolveHostname(const string& hostname,
  199. const NameResolverHandle& resolver) {
  200. string ipaddr = DNSCacheSingletonHolder::instance()->find(hostname);
  201. if(ipaddr.empty()) {
  202. #ifdef ENABLE_ASYNC_DNS
  203. switch(resolver->getStatus()) {
  204. case NameResolver::STATUS_READY:
  205. logger->info("CUID#%d - Resolving hostname %s", cuid, hostname.c_str());
  206. resolver->resolve(hostname);
  207. setNameResolverCheck(resolver);
  208. return false;
  209. case NameResolver::STATUS_SUCCESS:
  210. logger->info("CUID#%d - Name resolution complete: %s -> %s", cuid,
  211. hostname.c_str(), resolver->getAddrString().c_str());
  212. DNSCacheSingletonHolder::instance()->put(hostname, resolver->getAddrString());
  213. return true;
  214. break;
  215. case NameResolver::STATUS_ERROR:
  216. throw new DlAbortEx("CUID#%d - Name resolution for %s failed:%s", cuid,
  217. hostname.c_str(),
  218. resolver->getError().c_str());
  219. default:
  220. return false;
  221. }
  222. #else
  223. logger->info("CUID#%d - Resolving hostname %s", cuid, hostname.c_str());
  224. resolver->resolve(hostname);
  225. logger->info("CUID#%d - Name resolution complete: %s -> %s", cuid,
  226. hostname.c_str(), resolver->getAddrString().c_str());
  227. DNSCacheSingletonHolder::instance()->put(hostname, resolver->getAddrString());
  228. return true;
  229. #endif // ENABLE_ASYNC_DNS
  230. } else {
  231. logger->info("CUID#%d - DNS cache hit: %s -> %s", cuid,
  232. hostname.c_str(), ipaddr.c_str());
  233. resolver->setAddr(ipaddr);
  234. return true;
  235. }
  236. }
  237. #ifdef ENABLE_ASYNC_DNS
  238. void AbstractCommand::setNameResolverCheck(const NameResolverHandle& resolver) {
  239. nameResolverCheck = true;
  240. e->addNameResolverCheck(resolver, this);
  241. }
  242. void AbstractCommand::disableNameResolverCheck(const NameResolverHandle& resolver) {
  243. nameResolverCheck = false;
  244. e->deleteNameResolverCheck(resolver, this);
  245. }
  246. bool AbstractCommand::nameResolveFinished() const {
  247. return false;
  248. }
  249. #endif // ENABLE_ASYNC_DNS