DownloadCommand.cc 5.2 KB

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