/* */ #include "CreateRequestCommand.h" #include "InitiateConnectionCommandFactory.h" #include "RequestGroup.h" #include "Segment.h" #include "DownloadContext.h" #include "DlAbortEx.h" #include "DownloadEngine.h" #include "SocketCore.h" #include "SegmentMan.h" #include "prefs.h" #include "Option.h" #include "SleepCommand.h" #include "Logger.h" #include "util.h" namespace aria2 { CreateRequestCommand::CreateRequestCommand(cuid_t cuid, RequestGroup* requestGroup, DownloadEngine* e): AbstractCommand (cuid, SharedHandle(), SharedHandle(), requestGroup, e) { setStatus(Command::STATUS_ONESHOT_REALTIME); disableReadCheckSocket(); disableWriteCheckSocket(); } bool CreateRequestCommand::executeInternal() { if(_segments.empty()) { _fileEntry = _requestGroup->getDownloadContext()->findFileEntryByOffset(0); } else { // We assume all segments belongs to same file. _fileEntry = _requestGroup->getDownloadContext()->findFileEntryByOffset (_segments.front()->getPositionToWrite()); } if(_fileEntry->getRemainingUris().empty() && getOption()->getAsBool(PREF_REUSE_URI) && _fileEntry->countPooledRequest() == 0) { _fileEntry->reuseUri(_requestGroup->getNumConcurrentCommand()); } req = _fileEntry->getRequest(_requestGroup->getURISelector(), getOption()->get(PREF_REFERER), // Don't use HEAD request when file // size is known. // Use HEAD for dry-run mode. (_fileEntry->getLength() == 0 && getOption()->getAsBool(PREF_USE_HEAD)) || getOption()->getAsBool(PREF_DRY_RUN)? Request::METHOD_HEAD:Request::METHOD_GET); if(req.isNull()) { if(!_requestGroup->getSegmentMan().isNull()) { _requestGroup->getSegmentMan()->ignoreSegmentFor(_fileEntry); } throw DL_ABORT_EX("No URI available."); } Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand (cuid, req, _fileEntry, _requestGroup, e); e->setNoWait(true); e->commands.push_back(command); return true; } bool CreateRequestCommand::prepareForRetry(time_t wait) { // We assume that this method is called from AbstractCommand when // Segment is not available. Normally, // AbstractCommand::prepareForRetry() does the job, but it creates // CreateRequestCommand and deletes current one. At the last stage // of the download, commands are idle and prepareForRetry() is // called repeatedly. This means that newly created // CreateRequestCommand is deleted one second later: This is not // efficient. For this reason, reuse current CreateRequestCommand. if(!_requestGroup->getPieceStorage().isNull()) { _requestGroup->getSegmentMan()->cancelSegment(cuid); } if(logger->debug()) { logger->debug("CUID#%s - Reusing CreateRequestCommand", util::itos(cuid).c_str()); } SleepCommand* scom = new SleepCommand(cuid, e, _requestGroup, this, wait); e->commands.push_back(scom); return false; } } // namespace aria2