DownloadCommand.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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) {
  30. AbstractCommand::checkSocketIsReadable = true;
  31. }
  32. DownloadCommand::~DownloadCommand() {}
  33. bool DownloadCommand::executeInternal(Segment seg) {
  34. TransferEncoding* te = NULL;
  35. if(transferEncoding.size()) {
  36. te = getTransferEncoding(transferEncoding);
  37. assert(te != NULL);
  38. }
  39. int bufSize = 4096;
  40. char buf[bufSize];
  41. socket->readData(buf, bufSize);
  42. if(te != NULL) {
  43. int infbufSize = 4096;
  44. char infbuf[infbufSize];
  45. te->inflate(infbuf, infbufSize, buf, bufSize);
  46. e->diskWriter->writeData(infbuf, infbufSize, seg.sp+seg.ds);
  47. seg.ds += infbufSize;
  48. } else {
  49. e->diskWriter->writeData(buf, bufSize, seg.sp+seg.ds);
  50. seg.ds += bufSize;
  51. }
  52. if(te != NULL && te->finished()
  53. || te == NULL && seg.ds >= seg.ep-seg.sp+1
  54. || e->segmentMan->totalSize == 0 && bufSize == 0) {
  55. if(te != NULL) te->end();
  56. e->logger->info(MSG_DOWNLOAD_COMPLETED, cuid);
  57. seg.ds = seg.ep-seg.sp+1;
  58. seg.finish = true;
  59. e->segmentMan->updateSegment(seg);
  60. // this unit is going to download another segment.
  61. return prepareForNextSegment();
  62. } else {
  63. e->segmentMan->updateSegment(seg);
  64. e->commands.push(this);
  65. return false;
  66. }
  67. }
  68. bool DownloadCommand::prepareForRetry(int wait) {
  69. //req->resetUrl();
  70. return AbstractCommand::prepareForRetry(wait);
  71. }
  72. bool DownloadCommand::prepareForNextSegment() {
  73. req->resetUrl();
  74. if(!e->segmentMan->finished()) {
  75. return AbstractCommand::prepareForRetry(0);
  76. } else {
  77. return true;
  78. }
  79. }