DownloadCommand.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "ChecksumCommand.h"
  44. #include <sys/time.h>
  45. DownloadCommand::DownloadCommand(int cuid,
  46. const RequestHandle req,
  47. RequestGroup* requestGroup,
  48. DownloadEngine* e,
  49. const SocketHandle& s):
  50. AbstractCommand(cuid, req, requestGroup, e, s),
  51. peerStat(0),
  52. transferDecoder(0)
  53. {
  54. peerStat = _requestGroup->getSegmentMan()->getPeerStat(cuid);
  55. if(peerStat.isNull()) {
  56. peerStat = new PeerStat(cuid);
  57. _requestGroup->getSegmentMan()->registerPeerStat(peerStat);
  58. }
  59. peerStat->downloadStart();
  60. }
  61. DownloadCommand::~DownloadCommand() {
  62. assert(peerStat.get());
  63. peerStat->downloadStop();
  64. }
  65. bool DownloadCommand::executeInternal() {
  66. // TODO we need to specify the sum of all segmentMan's download speed here.
  67. if(maxDownloadSpeedLimit > 0 &&
  68. maxDownloadSpeedLimit < _requestGroup->getSegmentMan()->calculateDownloadSpeed()) {
  69. usleep(1);
  70. e->commands.push_back(this);
  71. return false;
  72. }
  73. int32_t bufSize = 16*1024;
  74. char buf[bufSize];
  75. socket->readData(buf, bufSize);
  76. if(transferDecoder.isNull()) {
  77. _requestGroup->getSegmentMan()->diskWriter->writeData(buf, bufSize,
  78. segment->getPositionToWrite());
  79. segment->writtenLength += bufSize;
  80. peerStat->updateDownloadLength(bufSize);
  81. } else {
  82. int32_t infbufSize = 16*1024;
  83. char infbuf[infbufSize];
  84. transferDecoder->inflate(infbuf, infbufSize, buf, bufSize);
  85. _requestGroup->getSegmentMan()->diskWriter->writeData(infbuf, infbufSize,
  86. segment->getPositionToWrite());
  87. segment->writtenLength += infbufSize;
  88. peerStat->updateDownloadLength(infbufSize);
  89. }
  90. // calculate downloading speed
  91. if(peerStat->getDownloadStartTime().elapsed(startupIdleTime)) {
  92. int32_t nowSpeed = peerStat->calculateDownloadSpeed();
  93. if(lowestDownloadSpeedLimit > 0 && nowSpeed <= lowestDownloadSpeedLimit) {
  94. throw new DlAbortEx("CUID#%d - Too slow Downloading speed: %d <= %d(B/s)",
  95. cuid,
  96. nowSpeed,
  97. lowestDownloadSpeedLimit);
  98. }
  99. }
  100. if(_requestGroup->getSegmentMan()->totalSize != 0 && bufSize == 0) {
  101. throw new DlRetryEx(EX_GOT_EOF);
  102. }
  103. if(!transferDecoder.isNull() && transferDecoder->finished()
  104. || transferDecoder.isNull() && segment->complete()
  105. || bufSize == 0) {
  106. if(!transferDecoder.isNull()) transferDecoder->end();
  107. logger->info(MSG_DOWNLOAD_COMPLETED, cuid);
  108. _requestGroup->getSegmentMan()->completeSegment(cuid, segment);
  109. #ifdef ENABLE_MESSAGE_DIGEST
  110. if(e->option->get(PREF_REALTIME_CHUNK_CHECKSUM) == V_TRUE) {
  111. _requestGroup->getSegmentMan()->tryChunkChecksumValidation(segment);
  112. }
  113. #endif // ENABLE_MESSAGE_DIGEST
  114. // this unit is going to download another segment.
  115. return prepareForNextSegment();
  116. } else {
  117. e->commands.push_back(this);
  118. return false;
  119. }
  120. }
  121. bool DownloadCommand::prepareForNextSegment() {
  122. if(_requestGroup->getSegmentMan()->finished()) {
  123. if(!_requestGroup->getChecksum().isNull() &&
  124. !_requestGroup->getChecksum()->isEmpty()) {
  125. ChecksumCommand* command = new ChecksumCommand(cuid, _requestGroup, e);
  126. command->initValidator();
  127. e->commands.push_back(command);
  128. }
  129. return true;
  130. } else {
  131. // Merge segment with next segment, if segment.index+1 == nextSegment.index
  132. SegmentHandle tempSegment = segment;
  133. while(1) {
  134. SegmentHandle nextSegment =
  135. _requestGroup->getSegmentMan()->getSegment(cuid,
  136. tempSegment->index+1);
  137. if(nextSegment.isNull()) {
  138. break;
  139. } else {
  140. if(nextSegment->writtenLength > 0) {
  141. return prepareForRetry(0);
  142. }
  143. nextSegment->writtenLength =
  144. tempSegment->writtenLength-tempSegment->length;
  145. if(nextSegment->complete()) {
  146. _requestGroup->getSegmentMan()->completeSegment(cuid, nextSegment);
  147. tempSegment = nextSegment;
  148. } else {
  149. segment = nextSegment;
  150. e->commands.push_back(this);
  151. return false;
  152. }
  153. }
  154. }
  155. return prepareForRetry(0);
  156. }
  157. }