DownloadCommand.cc 5.8 KB

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