AbstractCommand.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #define TIMEOUT_SEC 5
  31. AbstractCommand::AbstractCommand(int cuid, Request* req, DownloadEngine* e, Socket* s):
  32. Command(cuid), req(req), e(e), checkSocketIsReadable(false), checkSocketIsWritable(false) {
  33. if(s != NULL) {
  34. socket = new Socket(*s);
  35. e->addSocketForReadCheck(socket);
  36. } else {
  37. socket = NULL;
  38. }
  39. this->checkPoint.tv_sec = 0;
  40. this->checkPoint.tv_usec = 0;
  41. }
  42. AbstractCommand::~AbstractCommand() {
  43. e->deleteSocketForReadCheck(socket);
  44. e->deleteSocketForWriteCheck(socket);
  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. return false;
  57. } else {
  58. long long int elapsed = Util::difftv(now, checkPoint);
  59. if(elapsed >= TIMEOUT_SEC*1000000) {
  60. return true;
  61. } else {
  62. return false;
  63. }
  64. }
  65. }
  66. bool AbstractCommand::execute() {
  67. try {
  68. if(checkSocketIsReadable && !socket->isReadable(0)
  69. || checkSocketIsWritable && !socket->isWritable(0)) {
  70. if(isTimeoutDetected()) {
  71. throw new DlRetryEx(EX_TIME_OUT);
  72. }
  73. updateCheckPoint();
  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. onError(err);
  91. delete(err);
  92. req->resetUrl();
  93. return true;
  94. } catch(DlRetryEx* err) {
  95. e->logger->error(MSG_RESTARTING_DOWNLOAD, err, cuid);
  96. onError(err);
  97. delete(err);
  98. //req->resetUrl();
  99. req->addRetryCount();
  100. if(req->noMoreRetry()) {
  101. e->logger->error(MSG_MAX_RETRY, cuid);
  102. return true;
  103. } else {
  104. return prepareForRetry(e->option->getAsInt("retry_wait"));
  105. }
  106. }
  107. }
  108. bool AbstractCommand::prepareForRetry(int wait) {
  109. Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, e);
  110. if(wait == 0) {
  111. e->commands.push(command);
  112. } else {
  113. SleepCommand* scom = new SleepCommand(cuid, e, command, wait);
  114. e->commands.push(scom);
  115. }
  116. return true;
  117. }
  118. void AbstractCommand::onError(Exception* e) {
  119. }