DownloadCommand.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "SleepCommand.h"
  28. #include "InitiateConnectionCommandFactory.h"
  29. #include "message.h"
  30. DownloadCommand::DownloadCommand(int cuid, Request* req, DownloadEngine* e, Socket* s):AbstractCommand(cuid, req, e, s) {
  31. AbstractCommand::checkSocketIsReadable = true;
  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. if(te != NULL && te->finished()
  54. || te == NULL && seg.ds >= seg.ep-seg.sp+1
  55. || e->segmentMan->totalSize == 0 && bufSize == 0) {
  56. if(te != NULL) te->end();
  57. e->logger->info(MSG_DOWNLOAD_COMPLETED, cuid);
  58. seg.ds = seg.ep-seg.sp+1;
  59. seg.finish = true;
  60. e->segmentMan->updateSegment(seg);
  61. // this unit is going to download another segment.
  62. return prepareForNextSegment();
  63. } else {
  64. e->segmentMan->updateSegment(seg);
  65. e->commands.push(this);
  66. return false;
  67. }
  68. }
  69. bool DownloadCommand::prepareForRetry() {
  70. req->resetUrl();
  71. Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, e);
  72. SleepCommand* scom = new SleepCommand(cuid, e, command);
  73. e->commands.push(scom);
  74. return true;
  75. }
  76. bool DownloadCommand::prepareForNextSegment() {
  77. req->resetUrl();
  78. if(!e->segmentMan->finished()) {
  79. return AbstractCommand::prepareForRetry();
  80. } else {
  81. return true;
  82. }
  83. }