AbstractCommand.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <sys/time.h>
  27. #include "Util.h"
  28. #include "message.h"
  29. #include "SleepCommand.h"
  30. #include "prefs.h"
  31. AbstractCommand::AbstractCommand(int cuid, Request* req, DownloadEngine* e, const Socket* s):
  32. Command(cuid), req(req), e(e), checkSocketIsReadable(false), checkSocketIsWritable(false) {
  33. if(s != NULL) {
  34. socket = new Socket(*s);
  35. setReadCheckSocket(socket);
  36. } else {
  37. socket = NULL;
  38. }
  39. this->checkPoint.tv_sec = 0;
  40. this->checkPoint.tv_usec = 0;
  41. }
  42. AbstractCommand::~AbstractCommand() {
  43. setReadCheckSocket(NULL);
  44. setWriteCheckSocket(NULL);
  45. if(socket != NULL) {
  46. delete(socket);
  47. }
  48. }
  49. void AbstractCommand::updateCheckPoint() {
  50. gettimeofday(&checkPoint, NULL);
  51. }
  52. bool AbstractCommand::isTimeoutDetected() {
  53. struct timeval now;
  54. gettimeofday(&now, NULL);
  55. if(checkPoint.tv_sec == 0 && checkPoint.tv_usec == 0) {
  56. checkPoint = now;
  57. return false;
  58. } else {
  59. long long int elapsed = Util::difftv(now, checkPoint);
  60. if(elapsed >= e->option->getAsLLInt(PREF_TIMEOUT)*1000000) {
  61. return true;
  62. } else {
  63. return false;
  64. }
  65. }
  66. }
  67. bool AbstractCommand::execute() {
  68. try {
  69. if(checkSocketIsReadable && !readCheckTarget->isReadable(0)
  70. || checkSocketIsWritable && !writeCheckTarget->isWritable(0)) {
  71. if(isTimeoutDetected()) {
  72. throw new DlRetryEx(EX_TIME_OUT);
  73. }
  74. e->commands.push(this);
  75. return false;
  76. }
  77. updateCheckPoint();
  78. Segment seg = { 0, 0, 0, false };
  79. if(e->segmentMan->downloadStarted) {
  80. // get segment information in order to set Range header.
  81. if(!e->segmentMan->getSegment(seg, cuid)) {
  82. // no segment available
  83. e->logger->info(MSG_NO_SEGMENT_AVAILABLE, cuid);
  84. return true;
  85. }
  86. }
  87. return executeInternal(seg);
  88. } catch(DlAbortEx* err) {
  89. e->logger->error(MSG_DOWNLOAD_ABORTED, err, cuid);
  90. onAbort(err);
  91. delete(err);
  92. req->resetUrl();
  93. return true;
  94. } catch(DlRetryEx* err) {
  95. e->logger->error(MSG_RESTARTING_DOWNLOAD, err, cuid);
  96. req->addTryCount();
  97. bool isAbort = e->option->getAsInt(PREF_MAX_TRIES) != 0 &&
  98. req->getTryCount() >= e->option->getAsInt(PREF_MAX_TRIES);
  99. if(isAbort) {
  100. onAbort(err);
  101. }
  102. delete(err);
  103. if(isAbort) {
  104. e->logger->error(MSG_MAX_TRY, cuid, req->getTryCount());
  105. return true;
  106. } else {
  107. return prepareForRetry(e->option->getAsInt(PREF_RETRY_WAIT));
  108. }
  109. }
  110. }
  111. bool AbstractCommand::prepareForRetry(int wait) {
  112. Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, e);
  113. if(wait == 0) {
  114. e->commands.push(command);
  115. } else {
  116. SleepCommand* scom = new SleepCommand(cuid, e, command, wait);
  117. e->commands.push(scom);
  118. }
  119. return true;
  120. }
  121. void AbstractCommand::onAbort(Exception* ex) {
  122. e->logger->debug(MSG_UNREGISTER_CUID, cuid);
  123. e->segmentMan->unregisterId(cuid);
  124. }
  125. void AbstractCommand::setReadCheckSocket(Socket* socket) {
  126. if(socket == NULL) {
  127. if(checkSocketIsReadable) {
  128. e->deleteSocketForReadCheck(readCheckTarget);
  129. checkSocketIsReadable = false;
  130. readCheckTarget = NULL;
  131. }
  132. } else {
  133. if(checkSocketIsReadable) {
  134. if(readCheckTarget != socket) {
  135. e->deleteSocketForReadCheck(readCheckTarget);
  136. e->addSocketForReadCheck(socket);
  137. readCheckTarget = socket;
  138. }
  139. } else {
  140. e->addSocketForReadCheck(socket);
  141. checkSocketIsReadable = true;
  142. readCheckTarget = socket;
  143. }
  144. }
  145. }
  146. void AbstractCommand::setWriteCheckSocket(Socket* socket) {
  147. if(socket == NULL) {
  148. if(checkSocketIsWritable) {
  149. e->deleteSocketForWriteCheck(writeCheckTarget);
  150. checkSocketIsWritable = false;
  151. writeCheckTarget = NULL;
  152. }
  153. } else {
  154. if(checkSocketIsWritable) {
  155. if(writeCheckTarget != socket) {
  156. e->deleteSocketForWriteCheck(writeCheckTarget);
  157. e->addSocketForWriteCheck(socket);
  158. writeCheckTarget = socket;
  159. }
  160. } else {
  161. e->addSocketForWriteCheck(socket);
  162. checkSocketIsWritable = true;
  163. writeCheckTarget = socket;
  164. }
  165. }
  166. }