/* */ #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" #include "RequestGroupMan.h" #include "FileAllocationEntry.h" #include "CheckIntegrityEntry.h" #include "ServerStatMan.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(getSegments().empty()) { setFileEntry(getDownloadContext()->findFileEntryByOffset(0)); } else { // We assume all segments belongs to same file. setFileEntry(getDownloadContext()->findFileEntryByOffset (getSegments().front()->getPositionToWrite())); } std::vector > usedHosts; getDownloadEngine()->getRequestGroupMan()->getUsedHosts(usedHosts); setRequest (getFileEntry()->getRequest(getRequestGroup()->getURISelector(), getOption()->getAsBool(PREF_REUSE_URI), usedHosts, getOption()->get(PREF_REFERER), // Don't use HEAD request when file // size is known. // Use HEAD for dry-run mode. (getFileEntry()->getLength() == 0 && getOption()->getAsBool(PREF_USE_HEAD)) || getOption()->getAsBool(PREF_DRY_RUN)? Request::METHOD_HEAD:Request::METHOD_GET)); if(getRequest().isNull()) { if(!getSegmentMan().isNull()) { getSegmentMan()->ignoreSegmentFor(getFileEntry()); } throw DL_ABORT_EX("No URI available."); } Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), getDownloadEngine()); getDownloadEngine()->setNoWait(true); getDownloadEngine()->addCommand(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(!getPieceStorage().isNull()) { getSegmentMan()->cancelSegment(getCuid()); } if(getLogger()->debug()) { getLogger()->debug("CUID#%s - Reusing CreateRequestCommand", util::itos(getCuid()).c_str()); } SleepCommand* scom = new SleepCommand (getCuid(), getDownloadEngine(), getRequestGroup(), this, wait); getDownloadEngine()->addCommand(scom); return false; } } // namespace aria2