AbstractCommand.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "AbstractCommand.h"
  23. #include "DlAbortEx.h"
  24. #include "DlRetryEx.h"
  25. #include "InitiateConnectionCommandFactory.h"
  26. #include "Util.h"
  27. #include "message.h"
  28. #include "SleepCommand.h"
  29. #include "prefs.h"
  30. AbstractCommand::AbstractCommand(int cuid, Request* req, DownloadEngine* e,
  31. const SocketHandle& s):
  32. Command(cuid), req(req), e(e), socket(s),
  33. checkSocketIsReadable(false), checkSocketIsWritable(false) {
  34. setReadCheckSocket(socket);
  35. timeout = this->e->option->getAsInt(PREF_TIMEOUT);
  36. }
  37. AbstractCommand::~AbstractCommand() {
  38. disableReadCheckSocket();
  39. disableWriteCheckSocket();
  40. }
  41. bool AbstractCommand::execute() {
  42. try {
  43. if(checkSocketIsReadable && readCheckTarget->isReadable(0) ||
  44. checkSocketIsWritable && writeCheckTarget->isWritable(0) ||
  45. !checkSocketIsReadable && !checkSocketIsWritable) {
  46. checkPoint.reset();
  47. Segment seg = { 0, 0, 0, false };
  48. if(e->segmentMan->downloadStarted) {
  49. // get segment information in order to set Range header.
  50. if(!e->segmentMan->getSegment(seg, cuid)) {
  51. // no segment available
  52. logger->info(MSG_NO_SEGMENT_AVAILABLE, cuid);
  53. return true;
  54. }
  55. }
  56. return executeInternal(seg);
  57. } else {
  58. if(checkPoint.elapsed(timeout)) {
  59. throw new DlRetryEx(EX_TIME_OUT);
  60. }
  61. e->commands.push_back(this);
  62. return false;
  63. }
  64. } catch(DlAbortEx* err) {
  65. logger->error(MSG_DOWNLOAD_ABORTED, err, cuid);
  66. onAbort(err);
  67. delete(err);
  68. req->resetUrl();
  69. e->segmentMan->errors++;
  70. tryReserved();
  71. return true;
  72. } catch(DlRetryEx* err) {
  73. logger->error(MSG_RESTARTING_DOWNLOAD, err, cuid);
  74. req->addTryCount();
  75. bool isAbort = e->option->getAsInt(PREF_MAX_TRIES) != 0 &&
  76. req->getTryCount() >= e->option->getAsInt(PREF_MAX_TRIES);
  77. if(isAbort) {
  78. onAbort(err);
  79. }
  80. delete(err);
  81. if(isAbort) {
  82. logger->error(MSG_MAX_TRY, cuid, req->getTryCount());
  83. e->segmentMan->errors++;
  84. tryReserved();
  85. return true;
  86. } else {
  87. return prepareForRetry(e->option->getAsInt(PREF_RETRY_WAIT));
  88. }
  89. }
  90. }
  91. void AbstractCommand::tryReserved() {
  92. if(!e->segmentMan->reserved.empty()) {
  93. Request* req = e->segmentMan->reserved.front();
  94. e->segmentMan->reserved.pop_front();
  95. Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, e);
  96. e->commands.push_back(command);
  97. }
  98. }
  99. bool AbstractCommand::prepareForRetry(int wait) {
  100. Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, e);
  101. if(wait == 0) {
  102. e->commands.push_back(command);
  103. } else {
  104. SleepCommand* scom = new SleepCommand(cuid, e, command, wait);
  105. e->commands.push_back(scom);
  106. }
  107. return true;
  108. }
  109. void AbstractCommand::onAbort(Exception* ex) {
  110. logger->debug(MSG_UNREGISTER_CUID, cuid);
  111. e->segmentMan->unregisterId(cuid);
  112. }
  113. void AbstractCommand::disableReadCheckSocket() {
  114. if(checkSocketIsReadable) {
  115. e->deleteSocketForReadCheck(readCheckTarget, this);
  116. checkSocketIsReadable = false;
  117. readCheckTarget = SocketHandle();
  118. }
  119. }
  120. void AbstractCommand::setReadCheckSocket(const SocketHandle& socket) {
  121. if(!socket->isOpen()) {
  122. disableReadCheckSocket();
  123. } else {
  124. if(checkSocketIsReadable) {
  125. if(readCheckTarget != socket) {
  126. e->deleteSocketForReadCheck(readCheckTarget, this);
  127. e->addSocketForReadCheck(socket, this);
  128. readCheckTarget = socket;
  129. }
  130. } else {
  131. e->addSocketForReadCheck(socket, this);
  132. checkSocketIsReadable = true;
  133. readCheckTarget = socket;
  134. }
  135. }
  136. }
  137. void AbstractCommand::disableWriteCheckSocket() {
  138. if(checkSocketIsWritable) {
  139. e->deleteSocketForWriteCheck(writeCheckTarget, this);
  140. checkSocketIsWritable = false;
  141. writeCheckTarget = SocketHandle();
  142. }
  143. }
  144. void AbstractCommand::setWriteCheckSocket(const SocketHandle& socket) {
  145. if(!socket->isOpen()) {
  146. disableWriteCheckSocket();
  147. } else {
  148. if(checkSocketIsWritable) {
  149. if(writeCheckTarget != socket) {
  150. e->deleteSocketForWriteCheck(writeCheckTarget, this);
  151. e->addSocketForWriteCheck(socket, this);
  152. writeCheckTarget = socket;
  153. }
  154. } else {
  155. e->addSocketForWriteCheck(socket, this);
  156. checkSocketIsWritable = true;
  157. writeCheckTarget = socket;
  158. }
  159. }
  160. }
  161. #ifdef HAVE_LIBARES
  162. void AbstractCommand::setNameResolverCheck(const NameResolverHandle& resolver) {
  163. e->addNameResolverCheck(resolver, this);
  164. }
  165. void AbstractCommand::disableNameResolverCheck(const NameResolverHandle& resolver) {
  166. e->deleteNameResolverCheck(resolver, this);
  167. }
  168. bool AbstractCommand::resolveHostname(const string& hostname,
  169. const NameResolverHandle& resolver) {
  170. switch(resolver->getStatus()) {
  171. case NameResolver::STATUS_READY:
  172. logger->info("CUID#%d - Resolving hostname %s", cuid, hostname.c_str());
  173. resolver->resolve(hostname);
  174. setNameResolverCheck(resolver);
  175. return false;
  176. case NameResolver::STATUS_SUCCESS:
  177. logger->info("CUID#%d - Name resolution complete: %s -> %s", cuid,
  178. hostname.c_str(), resolver->getAddrString().c_str());
  179. return true;
  180. break;
  181. case NameResolver::STATUS_ERROR:
  182. throw new DlRetryEx("CUID#%d - Name resolution failed:%s", cuid,
  183. resolver->getError().c_str());
  184. default:
  185. return false;
  186. }
  187. }
  188. #endif // HAVE_LIBARES