AbstractCommand.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 "SegmentMan.h"
  37. #include "NameResolver.h"
  38. #include "CUIDCounter.h"
  39. #include "DlAbortEx.h"
  40. #include "DlRetryEx.h"
  41. #include "DownloadFailureException.h"
  42. #include "InitiateConnectionCommandFactory.h"
  43. #include "Util.h"
  44. #include "message.h"
  45. #include "SleepCommand.h"
  46. #include "prefs.h"
  47. #include "DNSCache.h"
  48. #include "StreamCheckIntegrityEntry.h"
  49. #include "PeerStat.h"
  50. #include "Segment.h"
  51. #include "Option.h"
  52. #include "PieceStorage.h"
  53. AbstractCommand::AbstractCommand(int32_t cuid,
  54. const RequestHandle& req,
  55. RequestGroup* requestGroup,
  56. DownloadEngine* e,
  57. const SocketHandle& s):
  58. Command(cuid), RequestGroupAware(requestGroup),
  59. req(req), e(e), socket(s),
  60. checkSocketIsReadable(false), checkSocketIsWritable(false),
  61. nameResolverCheck(false)
  62. {
  63. setReadCheckSocket(socket);
  64. timeout = this->e->option->getAsInt(PREF_TIMEOUT);
  65. _requestGroup->increaseStreamConnection();
  66. }
  67. AbstractCommand::~AbstractCommand() {
  68. disableReadCheckSocket();
  69. disableWriteCheckSocket();
  70. _requestGroup->decreaseStreamConnection();
  71. }
  72. bool AbstractCommand::execute() {
  73. try {
  74. if(_requestGroup->downloadFinished() || _requestGroup->isHaltRequested()) {
  75. //logger->debug("CUID#%d - finished.", cuid);
  76. return true;
  77. }
  78. PeerStatHandle peerStat = 0;
  79. if(!_requestGroup->getSegmentMan().isNull()) {
  80. peerStat = _requestGroup->getSegmentMan()->getPeerStat(cuid);
  81. }
  82. if(!peerStat.isNull()) {
  83. if(peerStat->getStatus() == PeerStat::REQUEST_IDLE) {
  84. logger->info(MSG_ABORT_REQUESTED, cuid);
  85. onAbort(0);
  86. req->resetUrl();
  87. tryReserved();
  88. return true;
  89. }
  90. }
  91. if(checkSocketIsReadable && readCheckTarget->isReadable(0) ||
  92. checkSocketIsWritable && writeCheckTarget->isWritable(0) ||
  93. #ifdef ENABLE_ASYNC_DNS
  94. nameResolverCheck && nameResolveFinished() ||
  95. #endif // ENABLE_ASYNC_DNS
  96. !checkSocketIsReadable && !checkSocketIsWritable && !nameResolverCheck) {
  97. checkPoint.reset();
  98. if(!_requestGroup->getPieceStorage().isNull()) {
  99. _segments = _requestGroup->getSegmentMan()->getInFlightSegment(cuid);
  100. int32_t maxSegments;
  101. if(req->isKeepAlive() && e->option->get(PREF_ENABLE_HTTP_PIPELINING) == V_TRUE) {
  102. maxSegments = e->option->getAsInt(PREF_MAX_HTTP_PIPELINING);
  103. } else {
  104. maxSegments = 1;
  105. }
  106. while((int32_t)_segments.size() < maxSegments) {
  107. SegmentHandle segment = _requestGroup->getSegmentMan()->getSegment(cuid);
  108. if(segment.isNull()) {
  109. break;
  110. }
  111. _segments.push_back(segment);
  112. }
  113. if(_segments.empty()) {
  114. logger->info(MSG_NO_SEGMENT_AVAILABLE, cuid);
  115. return prepareForRetry(1);
  116. }
  117. }
  118. return executeInternal();
  119. } else {
  120. if(checkPoint.elapsed(timeout)) {
  121. throw new DlRetryEx(EX_TIME_OUT);
  122. }
  123. e->commands.push_back(this);
  124. return false;
  125. }
  126. } catch(DlAbortEx* err) {
  127. logger->error(MSG_DOWNLOAD_ABORTED, err, cuid, req->getUrl().c_str());
  128. onAbort(err);
  129. delete(err);
  130. req->resetUrl();
  131. tryReserved();
  132. return true;
  133. } catch(DlRetryEx* err) {
  134. logger->info(MSG_RESTARTING_DOWNLOAD, err, cuid, req->getUrl().c_str());
  135. req->addTryCount();
  136. bool isAbort = e->option->getAsInt(PREF_MAX_TRIES) != 0 &&
  137. req->getTryCount() >= e->option->getAsInt(PREF_MAX_TRIES);
  138. if(isAbort) {
  139. onAbort(err);
  140. }
  141. if(isAbort) {
  142. logger->info(MSG_MAX_TRY, cuid, req->getTryCount());
  143. logger->error(MSG_DOWNLOAD_ABORTED, err, cuid, req->getUrl().c_str());
  144. delete(err);
  145. tryReserved();
  146. return true;
  147. } else {
  148. delete(err);
  149. return prepareForRetry(e->option->getAsInt(PREF_RETRY_WAIT));
  150. }
  151. } catch(DownloadFailureException* err) {
  152. logger->error(EX_EXCEPTION_CAUGHT, err);
  153. delete(err);
  154. _requestGroup->setHaltRequested(true);
  155. return true;
  156. }
  157. }
  158. void AbstractCommand::tryReserved() {
  159. _requestGroup->removeServerHost(cuid);
  160. Commands commands = _requestGroup->createNextCommand(e, 1);
  161. e->addCommand(commands);
  162. }
  163. bool AbstractCommand::prepareForRetry(int32_t wait) {
  164. if(!_requestGroup->getPieceStorage().isNull()) {
  165. _requestGroup->getSegmentMan()->cancelSegment(cuid);
  166. }
  167. Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, _requestGroup, e);
  168. if(wait == 0) {
  169. e->commands.push_back(command);
  170. } else {
  171. SleepCommand* scom = new SleepCommand(cuid, e, command, wait);
  172. e->commands.push_back(scom);
  173. }
  174. return true;
  175. }
  176. void AbstractCommand::onAbort(Exception* ex) {
  177. logger->debug(MSG_UNREGISTER_CUID, cuid);
  178. //_segmentMan->unregisterId(cuid);
  179. if(!_requestGroup->getPieceStorage().isNull()) {
  180. _requestGroup->getSegmentMan()->cancelSegment(cuid);
  181. }
  182. }
  183. void AbstractCommand::disableReadCheckSocket() {
  184. if(checkSocketIsReadable) {
  185. e->deleteSocketForReadCheck(readCheckTarget, this);
  186. checkSocketIsReadable = false;
  187. readCheckTarget = SocketHandle();
  188. }
  189. }
  190. void AbstractCommand::setReadCheckSocket(const SocketHandle& socket) {
  191. if(!socket->isOpen()) {
  192. disableReadCheckSocket();
  193. } else {
  194. if(checkSocketIsReadable) {
  195. if(readCheckTarget != socket) {
  196. e->deleteSocketForReadCheck(readCheckTarget, this);
  197. e->addSocketForReadCheck(socket, this);
  198. readCheckTarget = socket;
  199. }
  200. } else {
  201. e->addSocketForReadCheck(socket, this);
  202. checkSocketIsReadable = true;
  203. readCheckTarget = socket;
  204. }
  205. }
  206. }
  207. void AbstractCommand::disableWriteCheckSocket() {
  208. if(checkSocketIsWritable) {
  209. e->deleteSocketForWriteCheck(writeCheckTarget, this);
  210. checkSocketIsWritable = false;
  211. writeCheckTarget = SocketHandle();
  212. }
  213. }
  214. void AbstractCommand::setWriteCheckSocket(const SocketHandle& socket) {
  215. if(!socket->isOpen()) {
  216. disableWriteCheckSocket();
  217. } else {
  218. if(checkSocketIsWritable) {
  219. if(writeCheckTarget != socket) {
  220. e->deleteSocketForWriteCheck(writeCheckTarget, this);
  221. e->addSocketForWriteCheck(socket, this);
  222. writeCheckTarget = socket;
  223. }
  224. } else {
  225. e->addSocketForWriteCheck(socket, this);
  226. checkSocketIsWritable = true;
  227. writeCheckTarget = socket;
  228. }
  229. }
  230. }
  231. bool AbstractCommand::resolveHostname(const string& hostname,
  232. const NameResolverHandle& resolver) {
  233. string ipaddr = DNSCacheSingletonHolder::instance()->find(hostname);
  234. if(ipaddr.empty()) {
  235. #ifdef ENABLE_ASYNC_DNS
  236. switch(resolver->getStatus()) {
  237. case NameResolver::STATUS_READY:
  238. logger->info(MSG_RESOLVING_HOSTNAME, cuid, hostname.c_str());
  239. resolver->resolve(hostname);
  240. setNameResolverCheck(resolver);
  241. return false;
  242. case NameResolver::STATUS_SUCCESS:
  243. logger->info(MSG_NAME_RESOLUTION_COMPLETE, cuid,
  244. hostname.c_str(), resolver->getAddrString().c_str());
  245. DNSCacheSingletonHolder::instance()->put(hostname, resolver->getAddrString());
  246. return true;
  247. break;
  248. case NameResolver::STATUS_ERROR:
  249. throw new DlAbortEx(MSG_NAME_RESOLUTION_FAILED, cuid,
  250. hostname.c_str(),
  251. resolver->getError().c_str());
  252. default:
  253. return false;
  254. }
  255. #else
  256. logger->info(MSG_RESOLVING_HOSTNAME, cuid, hostname.c_str());
  257. resolver->resolve(hostname);
  258. logger->info(MSG_NAME_RESOLUTION_COMPLETE, cuid,
  259. hostname.c_str(), resolver->getAddrString().c_str());
  260. DNSCacheSingletonHolder::instance()->put(hostname, resolver->getAddrString());
  261. return true;
  262. #endif // ENABLE_ASYNC_DNS
  263. } else {
  264. logger->info(MSG_DNS_CACHE_HIT, cuid,
  265. hostname.c_str(), ipaddr.c_str());
  266. resolver->setAddr(ipaddr);
  267. return true;
  268. }
  269. }
  270. #ifdef ENABLE_ASYNC_DNS
  271. void AbstractCommand::setNameResolverCheck(const NameResolverHandle& resolver) {
  272. nameResolverCheck = true;
  273. e->addNameResolverCheck(resolver, this);
  274. }
  275. void AbstractCommand::disableNameResolverCheck(const NameResolverHandle& resolver) {
  276. nameResolverCheck = false;
  277. e->deleteNameResolverCheck(resolver, this);
  278. }
  279. bool AbstractCommand::nameResolveFinished() const {
  280. return false;
  281. }
  282. #endif // ENABLE_ASYNC_DNS
  283. void AbstractCommand::prepareForNextAction(Command* nextCommand)
  284. {
  285. CheckIntegrityEntryHandle entry =
  286. new StreamCheckIntegrityEntry(req, _requestGroup, nextCommand);
  287. e->addCommand(_requestGroup->processCheckIntegrityEntry(entry, e));
  288. }