DownloadCommand.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "DownloadCommand.h"
  23. #include "Util.h"
  24. #include "DlRetryEx.h"
  25. #include "DlAbortEx.h"
  26. #include "HttpInitiateConnectionCommand.h"
  27. #include "InitiateConnectionCommandFactory.h"
  28. #include "message.h"
  29. #include "prefs.h"
  30. #include <sys/time.h>
  31. #define STARTUP_IDLE_TIME 10
  32. DownloadCommand::DownloadCommand(int cuid, Request* req, DownloadEngine* e,
  33. const SocketHandle& s):
  34. AbstractCommand(cuid, req, e, s), lastSize(0) {
  35. PeerStatHandle peerStat = PeerStatHandle(new PeerStat(cuid));
  36. peerStat->downloadStart();
  37. this->e->segmentMan->registerPeerStat(peerStat);
  38. }
  39. DownloadCommand::~DownloadCommand() {
  40. PeerStatHandle peerStat = e->segmentMan->getPeerStat(cuid);
  41. assert(peerStat.get());
  42. peerStat->downloadStop();
  43. }
  44. bool DownloadCommand::executeInternal(Segment& segment) {
  45. int maxSpeedLimit = e->option->getAsInt(PREF_MAX_SPEED_LIMIT);
  46. if(maxSpeedLimit > 0 &&
  47. maxSpeedLimit < e->segmentMan->calculateDownloadSpeed()) {
  48. usleep(1);
  49. e->commands.push_back(this);
  50. return false;
  51. }
  52. TransferEncoding* te = NULL;
  53. if(transferEncoding.size()) {
  54. te = getTransferEncoding(transferEncoding);
  55. assert(te != NULL);
  56. }
  57. int bufSize = 4096;
  58. char buf[bufSize];
  59. socket->readData(buf, bufSize);
  60. PeerStatHandle peerStat = e->segmentMan->getPeerStat(cuid);
  61. assert(peerStat.get());
  62. if(te != NULL) {
  63. int infbufSize = 4096;
  64. char infbuf[infbufSize];
  65. te->inflate(infbuf, infbufSize, buf, bufSize);
  66. e->segmentMan->diskWriter->writeData(infbuf, infbufSize,
  67. segment.getPosition()+segment.writtenLength);
  68. segment.writtenLength += infbufSize;
  69. peerStat->updateDownloadLength(infbufSize);
  70. } else {
  71. e->segmentMan->diskWriter->writeData(buf, bufSize,
  72. segment.getPosition()+segment.writtenLength);
  73. segment.writtenLength += bufSize;
  74. peerStat->updateDownloadLength(bufSize);
  75. }
  76. // calculate downloading speed
  77. if(/*sw.elapsed(1) >= 1 && */peerStat->getDownloadStartTime().elapsed(STARTUP_IDLE_TIME)) {
  78. int lowestLimit = e->option->getAsInt(PREF_LOWEST_SPEED_LIMIT);
  79. int nowSpeed = peerStat->calculateDownloadSpeed();
  80. if(lowestLimit > 0 && nowSpeed <= lowestLimit) {
  81. throw new DlAbortEx("CUID#%d - Too slow Downloading speed: %d <= %d(B/s)",
  82. cuid,
  83. nowSpeed,
  84. lowestLimit);
  85. }
  86. //sw.reset();
  87. }
  88. if(e->segmentMan->totalSize != 0 && bufSize == 0) {
  89. throw new DlRetryEx(EX_GOT_EOF);
  90. }
  91. if(te != NULL && te->finished()
  92. || te == NULL && segment.complete()
  93. || bufSize == 0) {
  94. if(te != NULL) te->end();
  95. logger->info(MSG_DOWNLOAD_COMPLETED, cuid);
  96. e->segmentMan->completeSegment(cuid, segment);
  97. // this unit is going to download another segment.
  98. return prepareForNextSegment(segment);
  99. } else {
  100. e->segmentMan->updateSegment(cuid, segment);
  101. e->commands.push_back(this);
  102. return false;
  103. }
  104. }
  105. bool DownloadCommand::prepareForNextSegment(const Segment& currentSegment) {
  106. if(e->segmentMan->finished()) {
  107. return true;
  108. } else {
  109. // Merge segment with next segment, if segment.index+1 == nextSegment.index
  110. Segment tempSegment = currentSegment;
  111. while(1) {
  112. Segment nextSegment;
  113. if(e->segmentMan->getSegment(nextSegment, cuid, tempSegment.index+1)) {
  114. if(nextSegment.writtenLength > 0) {
  115. return prepareForRetry(0);
  116. }
  117. nextSegment.writtenLength = tempSegment.writtenLength-tempSegment.length;
  118. if(nextSegment.complete()) {
  119. e->segmentMan->completeSegment(cuid, nextSegment);
  120. tempSegment = nextSegment;
  121. } else {
  122. e->segmentMan->updateSegment(cuid, nextSegment);
  123. e->commands.push_back(this);
  124. return false;
  125. }
  126. } else {
  127. break;
  128. }
  129. }
  130. return prepareForRetry(0);
  131. }
  132. }