DownloadCommand.cc 5.8 KB

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