DownloadCommand.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <sys/time.h>
  24. #include "Util.h"
  25. #include "DlRetryEx.h"
  26. #include "HttpInitiateConnectionCommand.h"
  27. #include "InitiateConnectionCommandFactory.h"
  28. #include "message.h"
  29. DownloadCommand::DownloadCommand(int cuid, Request* req, DownloadEngine* e, Socket* s):AbstractCommand(cuid, req, e, s), lastSize(0) {
  30. sw.tv_sec = 0;
  31. sw.tv_usec = 0;
  32. }
  33. DownloadCommand::~DownloadCommand() {}
  34. bool DownloadCommand::executeInternal(Segment seg) {
  35. TransferEncoding* te = NULL;
  36. if(transferEncoding.size()) {
  37. te = getTransferEncoding(transferEncoding);
  38. assert(te != NULL);
  39. }
  40. int bufSize = 4096;
  41. char buf[bufSize];
  42. socket->readData(buf, bufSize);
  43. if(te != NULL) {
  44. int infbufSize = 4096;
  45. char infbuf[infbufSize];
  46. te->inflate(infbuf, infbufSize, buf, bufSize);
  47. e->diskWriter->writeData(infbuf, infbufSize, seg.sp+seg.ds);
  48. seg.ds += infbufSize;
  49. } else {
  50. e->diskWriter->writeData(buf, bufSize, seg.sp+seg.ds);
  51. seg.ds += bufSize;
  52. }
  53. // calculate downloading speed
  54. struct timeval now;
  55. gettimeofday(&now, NULL);
  56. if(sw.tv_sec == 0 && sw.tv_usec == 0) {
  57. sw = now;
  58. lastSize = seg.ds;
  59. } else {
  60. long long int diff = Util::difftv(now, sw);
  61. if(diff >= 1000000) {
  62. seg.speed = (int)((seg.ds-lastSize)/(diff/1000000.0));
  63. sw = now;
  64. lastSize = seg.ds;
  65. }
  66. }
  67. if(te != NULL && te->finished()
  68. || te == NULL && seg.ds >= seg.ep-seg.sp+1
  69. || e->segmentMan->totalSize == 0 && bufSize == 0) {
  70. if(te != NULL) te->end();
  71. e->logger->info(MSG_DOWNLOAD_COMPLETED, cuid);
  72. seg.ds = seg.ep-seg.sp+1;
  73. seg.finish = true;
  74. e->segmentMan->updateSegment(seg);
  75. // this unit is going to download another segment.
  76. return prepareForNextSegment();
  77. } else {
  78. e->segmentMan->updateSegment(seg);
  79. e->commands.push(this);
  80. return false;
  81. }
  82. }
  83. bool DownloadCommand::prepareForRetry(int wait) {
  84. //req->resetUrl();
  85. return AbstractCommand::prepareForRetry(wait);
  86. }
  87. bool DownloadCommand::prepareForNextSegment() {
  88. req->resetUrl();
  89. if(!e->segmentMan->finished()) {
  90. return AbstractCommand::prepareForRetry(0);
  91. } else {
  92. return true;
  93. }
  94. }
  95. void DownloadCommand::onAbort(Exception* ex) {
  96. e->logger->debug(MSG_UNREGISTER_CUID, cuid);
  97. e->segmentMan->unregisterId(cuid);
  98. }