/* */ #include "DownloadCommand.h" #include #include "Util.h" #include "DlRetryEx.h" #include "HttpInitiateConnectionCommand.h" #include "InitiateConnectionCommandFactory.h" #include "message.h" DownloadCommand::DownloadCommand(int cuid, Request* req, DownloadEngine* e, const Socket* s):AbstractCommand(cuid, req, e, s), lastSize(0) {} DownloadCommand::~DownloadCommand() {} bool DownloadCommand::executeInternal(Segment seg) { TransferEncoding* te = NULL; if(transferEncoding.size()) { te = getTransferEncoding(transferEncoding); assert(te != NULL); } int bufSize = 4096; char buf[bufSize]; socket->readData(buf, bufSize); if(te != NULL) { int infbufSize = 4096; char infbuf[infbufSize]; te->inflate(infbuf, infbufSize, buf, bufSize); e->segmentMan->diskWriter->writeData(infbuf, infbufSize, seg.sp+seg.ds); seg.ds += infbufSize; } else { e->segmentMan->diskWriter->writeData(buf, bufSize, seg.sp+seg.ds); seg.ds += bufSize; } // calculate downloading speed int diff = sw.difference(); if(diff >= 1) { seg.speed = (int)((seg.ds-lastSize)/(diff*1.0)); sw.reset(); lastSize = seg.ds; } if(e->segmentMan->totalSize != 0 && bufSize == 0) { throw new DlRetryEx(EX_GOT_EOF); } if(te != NULL && te->finished() || te == NULL && seg.ds >= seg.ep-seg.sp+1 || bufSize == 0) { if(te != NULL) te->end(); logger->info(MSG_DOWNLOAD_COMPLETED, cuid); seg.ds = seg.ep-seg.sp+1; seg.finish = true; e->segmentMan->updateSegment(seg); // this unit is going to download another segment. return prepareForNextSegment(); } else { e->segmentMan->updateSegment(seg); e->commands.push_back(this); return false; } } bool DownloadCommand::prepareForRetry(int wait) { //req->resetUrl(); return AbstractCommand::prepareForRetry(wait); } bool DownloadCommand::prepareForNextSegment() { req->resetUrl(); if(!e->segmentMan->finished()) { return AbstractCommand::prepareForRetry(0); } else { return true; } }