AbstractCommand.cc 8.8 KB

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