AbstractCommand.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #define TIMEOUT_SEC 5
  30. AbstractCommand::AbstractCommand(int cuid, Request* req, DownloadEngine* e, Socket* s):
  31. Command(cuid), req(req), e(e), checkSocketIsReadable(false), checkSocketIsWritable(false) {
  32. if(s != NULL) {
  33. socket = new Socket(*s);
  34. e->addSocketForReadCheck(socket);
  35. } else {
  36. socket = NULL;
  37. }
  38. this->checkPoint.tv_sec = 0;
  39. this->checkPoint.tv_usec = 0;
  40. }
  41. AbstractCommand::~AbstractCommand() {
  42. e->deleteSocketForReadCheck(socket);
  43. e->deleteSocketForWriteCheck(socket);
  44. if(socket != NULL) {
  45. delete(socket);
  46. }
  47. }
  48. void AbstractCommand::updateCheckPoint() {
  49. gettimeofday(&checkPoint, NULL);
  50. }
  51. bool AbstractCommand::isTimeoutDetected() {
  52. struct timeval now;
  53. gettimeofday(&now, NULL);
  54. if(checkPoint.tv_sec == 0 && checkPoint.tv_usec == 0) {
  55. return false;
  56. } else {
  57. long long int elapsed = Util::difftv(now, checkPoint);
  58. if(elapsed >= TIMEOUT_SEC*1000000) {
  59. return true;
  60. } else {
  61. return false;
  62. }
  63. }
  64. }
  65. bool AbstractCommand::execute() {
  66. try {
  67. if(checkSocketIsReadable && !socket->isReadable(0)
  68. || checkSocketIsWritable && !socket->isWritable(0)) {
  69. if(isTimeoutDetected()) {
  70. throw new DlRetryEx(EX_TIME_OUT);
  71. }
  72. updateCheckPoint();
  73. e->commands.push(this);
  74. return false;
  75. }
  76. updateCheckPoint();
  77. Segment seg = { 0, 0, 0, false };
  78. if(e->segmentMan->downloadStarted) {
  79. // get segment information in order to set Range header.
  80. if(!e->segmentMan->getSegment(seg, cuid)) {
  81. // no segment available
  82. e->logger->info(MSG_NO_SEGMENT_AVAILABLE, cuid);
  83. return true;
  84. }
  85. }
  86. return executeInternal(seg);
  87. } catch(DlAbortEx* err) {
  88. e->logger->error(MSG_DOWNLOAD_ABORTED, err);
  89. onError(err);
  90. delete(err);
  91. req->resetUrl();
  92. return true;
  93. } catch(DlRetryEx* err) {
  94. e->logger->error(MSG_RESTARTING_DOWNLOAD, err);
  95. onError(err);
  96. delete(err);
  97. req->resetUrl();
  98. req->addRetryCount();
  99. if(req->noMoreRetry()) {
  100. e->logger->error(MSG_MAX_RETRY);
  101. return true;
  102. } else {
  103. return prepareForRetry();
  104. }
  105. }
  106. }
  107. bool AbstractCommand::prepareForRetry() {
  108. e->commands.push(InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, e));
  109. return true;
  110. }
  111. void AbstractCommand::onError(Exception* e) {
  112. }