From 06377d77c9a7bf2237f1bfcd5bf79e209b0e2247 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 17 May 2013 22:56:21 +0900 Subject: [PATCH] Add changeGlobalOption, getGlobalOption, getGlobalOptions API --- src/RpcMethodImpl.cc | 65 ++++++++++++++++++++------------------ src/RpcMethodImpl.h | 2 ++ src/aria2api.cc | 50 +++++++++++++++++++++++++++++ src/includes/aria2/aria2.h | 49 ++++++++++++++++++++++++++++ test/Aria2ApiTest.cc | 22 +++++++++++++ 5 files changed, 157 insertions(+), 31 deletions(-) diff --git a/src/RpcMethodImpl.cc b/src/RpcMethodImpl.cc index 1142c445..dcf1c84d 100644 --- a/src/RpcMethodImpl.cc +++ b/src/RpcMethodImpl.cc @@ -1112,37 +1112,7 @@ SharedHandle ChangeGlobalOptionRpcMethod::process Option option; gatherChangeableGlobalOption(&option, optsParam); - e->getOption()->merge(option); - - if(option.defined(PREF_MAX_OVERALL_DOWNLOAD_LIMIT)) { - e->getRequestGroupMan()->setMaxOverallDownloadSpeedLimit - (option.getAsInt(PREF_MAX_OVERALL_DOWNLOAD_LIMIT)); - } - if(option.defined(PREF_MAX_OVERALL_UPLOAD_LIMIT)) { - e->getRequestGroupMan()->setMaxOverallUploadSpeedLimit - (option.getAsInt(PREF_MAX_OVERALL_UPLOAD_LIMIT)); - } - if(option.defined(PREF_MAX_CONCURRENT_DOWNLOADS)) { - e->getRequestGroupMan()->setMaxSimultaneousDownloads - (option.getAsInt(PREF_MAX_CONCURRENT_DOWNLOADS)); - e->getRequestGroupMan()->requestQueueCheck(); - } - if(option.defined(PREF_MAX_DOWNLOAD_RESULT)) { - e->getRequestGroupMan()->setMaxDownloadResult - (option.getAsInt(PREF_MAX_DOWNLOAD_RESULT)); - } - if(option.defined(PREF_LOG_LEVEL)) { - LogFactory::setLogLevel(option.get(PREF_LOG_LEVEL)); - } - if(option.defined(PREF_LOG)) { - LogFactory::setLogFile(option.get(PREF_LOG)); - try { - LogFactory::reconfigure(); - } catch(RecoverableException& e) { - // TODO no exception handling - } - } - + changeGlobalOption(option, e); return VLB_OK; } @@ -1542,4 +1512,37 @@ void changeOption #endif // ENABLE_BITTORRENT } +void changeGlobalOption(const Option& option, DownloadEngine* e) +{ + e->getOption()->merge(option); + if(option.defined(PREF_MAX_OVERALL_DOWNLOAD_LIMIT)) { + e->getRequestGroupMan()->setMaxOverallDownloadSpeedLimit + (option.getAsInt(PREF_MAX_OVERALL_DOWNLOAD_LIMIT)); + } + if(option.defined(PREF_MAX_OVERALL_UPLOAD_LIMIT)) { + e->getRequestGroupMan()->setMaxOverallUploadSpeedLimit + (option.getAsInt(PREF_MAX_OVERALL_UPLOAD_LIMIT)); + } + if(option.defined(PREF_MAX_CONCURRENT_DOWNLOADS)) { + e->getRequestGroupMan()->setMaxSimultaneousDownloads + (option.getAsInt(PREF_MAX_CONCURRENT_DOWNLOADS)); + e->getRequestGroupMan()->requestQueueCheck(); + } + if(option.defined(PREF_MAX_DOWNLOAD_RESULT)) { + e->getRequestGroupMan()->setMaxDownloadResult + (option.getAsInt(PREF_MAX_DOWNLOAD_RESULT)); + } + if(option.defined(PREF_LOG_LEVEL)) { + LogFactory::setLogLevel(option.get(PREF_LOG_LEVEL)); + } + if(option.defined(PREF_LOG)) { + LogFactory::setLogFile(option.get(PREF_LOG)); + try { + LogFactory::reconfigure(); + } catch(RecoverableException& e) { + // TODO no exception handling + } + } +} + } // namespace aria2 diff --git a/src/RpcMethodImpl.h b/src/RpcMethodImpl.h index b1b254b5..72baf438 100644 --- a/src/RpcMethodImpl.h +++ b/src/RpcMethodImpl.h @@ -613,6 +613,8 @@ void changeOption const Option& option, DownloadEngine* e); +void changeGlobalOption(const Option& option, DownloadEngine* e); + } // namespace aria2 #endif // D_RPC_METHOD_IMPL_H diff --git a/src/aria2api.cc b/src/aria2api.cc index 069bb12a..c7504941 100644 --- a/src/aria2api.cc +++ b/src/aria2api.cc @@ -246,6 +246,17 @@ void apiGatherChangeableOptionForReserved } } // namespace +namespace { +void apiGatherChangeableGlobalOption +(Option* option, const KeyVals& options, + const SharedHandle& optionParser) +{ + apiGatherOption(options.begin(), options.end(), + std::mem_fun(&OptionHandler::getChangeGlobalOption), + option, optionParser); +} +} // namespace + namespace { void addRequestGroup(const SharedHandle& group, const SharedHandle& e, @@ -466,6 +477,45 @@ int changeOption(Session* session, const A2Gid& gid, const KeyVals& options) } } +const std::string& getGlobalOption(Session* session, const std::string& name) +{ + const SharedHandle& e = + session->context->reqinfo->getDownloadEngine(); + return e->getOption()->get(option::k2p(name)); +} + +KeyVals getGlobalOptions(Session* session) +{ + const SharedHandle& e = + session->context->reqinfo->getDownloadEngine(); + const SharedHandle& optionParser = OptionParser::getInstance(); + const Option* option = e->getOption(); + KeyVals options; + for(size_t i = 1, len = option::countOption(); i < len; ++i) { + const Pref* pref = option::i2p(i); + if(option->defined(pref) && optionParser->find(pref)) { + options.push_back(KeyVals::value_type(pref->k, option->get(pref))); + } + } + return options; +} + +int changeGlobalOption(Session* session, const KeyVals& options) +{ + const SharedHandle& e = + session->context->reqinfo->getDownloadEngine(); + Option option; + try { + apiGatherChangeableGlobalOption(&option, options, + OptionParser::getInstance()); + } catch(RecoverableException& err) { + A2_LOG_INFO_EX(EX_EXCEPTION_CAUGHT, err); + return -1; + } + changeGlobalOption(option, e.get()); + return 0; +} + std::vector getActiveDownload(Session* session) { const SharedHandle& e = diff --git a/src/includes/aria2/aria2.h b/src/includes/aria2/aria2.h index c1a4b637..7328d2c0 100644 --- a/src/includes/aria2/aria2.h +++ b/src/includes/aria2/aria2.h @@ -436,6 +436,55 @@ int unpauseDownload(Session* session, const A2Gid& gid); */ int changeOption(Session* session, const A2Gid& gid, const KeyVals& options); +/** + * @function + * + * Returns global option denoted by the |name|. If such option is not + * available, returns empty string. + */ +const std::string& getGlobalOption(Session* session, const std::string& name); + +/** + * @function + * + * Returns global options. Note that this function does not return + * options which have no default value and have not been set by + * :func:`sessionNew()`, configuration files or API functions. + */ +KeyVals getGlobalOptions(Session* session); + +/** + * @function + * + * Apply global options in the |options| dynamically. The following + * options are available: + * + * * :option:`download-result <--download-result>` + * * :option:`log <-l>` + * * :option:`log-level <--log-level>` + * * :option:`max-concurrent-downloads <-j>` + * * :option:`max-download-result <--max-download-result>` + * * :option:`max-overall-download-limit <--max-overall-download-limit>` + * * :option:`max-overall-upload-limit <--max-overall-upload-limit>` + * * :option:`save-cookies <--save-cookies>` + * * :option:`save-session <--save-session>` + * * :option:`server-stat-of <--server-stat-of>` + * + * In addition to them, options listed in :ref:`input-file` subsection + * are available, except for following options: + * :option:`checksum <--checksum>`, + * :option:`index-out <-O>`, + * :option:`out <-o>`, + * :option:`pause <--pause>` and + * :option:`select-file <--select-file>`. + * + * The options which are not applicable or unknown, they are just + * ignored. + * + * This function returns 0 if it succeeds, or negative error code. + */ +int changeGlobalOption(Session* session, const KeyVals& options); + /** * @enum * diff --git a/test/Aria2ApiTest.cc b/test/Aria2ApiTest.cc index 96be3b61..0c5429e7 100644 --- a/test/Aria2ApiTest.cc +++ b/test/Aria2ApiTest.cc @@ -2,6 +2,10 @@ #include +#include "prefs.h" +#include "OptionParser.h" +#include "OptionHandler.h" + namespace aria2 { class Aria2ApiTest:public CppUnit::TestFixture { @@ -13,6 +17,7 @@ class Aria2ApiTest:public CppUnit::TestFixture { CPPUNIT_TEST(testRemovePause); CPPUNIT_TEST(testChangePosition); CPPUNIT_TEST(testChangeOption); + CPPUNIT_TEST(testChangeGlobalOption); CPPUNIT_TEST_SUITE_END(); Session* session_; @@ -35,6 +40,7 @@ public: void testRemovePause(); void testChangePosition(); void testChangeOption(); + void testChangeGlobalOption(); }; CPPUNIT_TEST_SUITE_REGISTRATION(Aria2ApiTest); @@ -189,5 +195,21 @@ void Aria2ApiTest::testChangeOption() CPPUNIT_ASSERT_EQUAL(-1, changeOption(session_, gid, options)); } +void Aria2ApiTest::testChangeGlobalOption() +{ + CPPUNIT_ASSERT_EQUAL(OptionParser::getInstance()->find(PREF_FILE_ALLOCATION) + ->getDefaultValue(), + getGlobalOption(session_, PREF_FILE_ALLOCATION->k)); + KeyVals options; + options.push_back(KeyVals::value_type(PREF_FILE_ALLOCATION->k, "none")); + CPPUNIT_ASSERT_EQUAL(0, changeGlobalOption(session_, options)); + CPPUNIT_ASSERT_EQUAL(std::string("none"), + getGlobalOption(session_, PREF_FILE_ALLOCATION->k)); + + // failure with bad option value + options.clear(); + options.push_back(KeyVals::value_type(PREF_FILE_ALLOCATION->k, "foo")); + CPPUNIT_ASSERT_EQUAL(-1, changeGlobalOption(session_, options)); +} } // namespace aria2