AbstractCommand.cc 7.1 KB

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