From 7d55341fded7b88f528b62a5527f9df8bc3259d8 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 2 May 2013 12:44:55 +0900 Subject: [PATCH] Fix event polling not working with no downloads but keepRunning_ is true --- src/KeepRunningCommand.cc | 60 +++++++++++++++++++++++++++++++++++++++ src/KeepRunningCommand.h | 57 +++++++++++++++++++++++++++++++++++++ src/Makefile.am | 3 +- src/RequestGroupMan.h | 5 ++++ src/aria2api.cc | 9 ++++++ 5 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 src/KeepRunningCommand.cc create mode 100644 src/KeepRunningCommand.h diff --git a/src/KeepRunningCommand.cc b/src/KeepRunningCommand.cc new file mode 100644 index 00000000..f917d5e1 --- /dev/null +++ b/src/KeepRunningCommand.cc @@ -0,0 +1,60 @@ +/* */ +#include "KeepRunningCommand.h" +#include "DownloadEngine.h" +#include "RequestGroupMan.h" + +namespace aria2 { + +KeepRunningCommand::KeepRunningCommand(cuid_t cuid, DownloadEngine* e) + : Command(cuid), + e_(e) +{ + setStatusRealtime(); +} + +KeepRunningCommand::~KeepRunningCommand() {} + +bool KeepRunningCommand::execute() +{ + if(!e_->getRequestGroupMan()->getKeepRunning() || + e_->isHaltRequested()) { + return true; + } + e_->addCommand(this); + return false; +} + +} // namespace aria2 diff --git a/src/KeepRunningCommand.h b/src/KeepRunningCommand.h new file mode 100644 index 00000000..9be030cb --- /dev/null +++ b/src/KeepRunningCommand.h @@ -0,0 +1,57 @@ +/* */ +#ifndef KEEP_RUNNING_COMMAND_H +#define KEEP_RUNNING_COMMAND_H + +#include "Command.h" + +namespace aria2 { + +class DownloadEngine; + +// This object does nothing but it is added to DownloadEngine command +// queue (not routine one), and keeps event polling work. +class KeepRunningCommand : public Command { +public: + KeepRunningCommand(cuid_t cuid, DownloadEngine* e); + virtual ~KeepRunningCommand(); + virtual bool execute(); +private: + DownloadEngine* e_; +}; + +} // namespace aria2 + +#endif // KEEP_RUNNING_COMMAND_H diff --git a/src/Makefile.am b/src/Makefile.am index 59212bb6..80e755cf 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -639,7 +639,8 @@ DISTCLEANFILES = $(pkgconfig_DATA) lib_LTLIBRARIES = libaria2.la libaria2_la_SOURCES = $(SRCS) \ - aria2api.cc aria2api.h + aria2api.cc aria2api.h \ + KeepRunningCommand.cc KeepRunningCommand.h libaria2_la_LIBADD = @WSLAY_LIBS@ LDADD = libaria2.la @LIBINTL@ @ALLOCA@ #-lprofiler diff --git a/src/RequestGroupMan.h b/src/RequestGroupMan.h index debb2573..186ed379 100644 --- a/src/RequestGroupMan.h +++ b/src/RequestGroupMan.h @@ -341,6 +341,11 @@ public: { keepRunning_ = flag; } + + bool getKeepRunning() const + { + return keepRunning_; + } }; } // namespace aria2 diff --git a/src/aria2api.cc b/src/aria2api.cc index c9474a8b..dcf5a007 100644 --- a/src/aria2api.cc +++ b/src/aria2api.cc @@ -57,6 +57,7 @@ #include "DownloadContext.h" #include "RpcMethodImpl.h" #include "console.h" +#include "KeepRunningCommand.h" namespace aria2 { @@ -105,6 +106,11 @@ Session* sessionNew(const KeyVals& options) delete session; session = 0; } + const SharedHandle& e = + session->context->reqinfo->getDownloadEngine(); + // Add command to make aria2 keep event polling if + // sessionConfigSetKeepRunning is set to true. + e->addCommand(new KeepRunningCommand(e->newCUID(), e.get())); } else { delete session; session = 0; @@ -142,6 +148,9 @@ int shutdown(Session* session, bool force) } else { e->requestHalt(); } + // Skip next polling timeout. This avoids 1 second delay when there + // is no Command other than KeepRunningCommand in the queue. + e->setNoWait(true); return 0; }