Bladeren bron

Add changeGlobalOption, getGlobalOption, getGlobalOptions API

Tatsuhiro Tsujikawa 12 jaren geleden
bovenliggende
commit
06377d77c9
5 gewijzigde bestanden met toevoegingen van 157 en 31 verwijderingen
  1. 34 31
      src/RpcMethodImpl.cc
  2. 2 0
      src/RpcMethodImpl.h
  3. 50 0
      src/aria2api.cc
  4. 49 0
      src/includes/aria2/aria2.h
  5. 22 0
      test/Aria2ApiTest.cc

+ 34 - 31
src/RpcMethodImpl.cc

@@ -1112,37 +1112,7 @@ SharedHandle<ValueBase> 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

+ 2 - 0
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

+ 50 - 0
src/aria2api.cc

@@ -246,6 +246,17 @@ void apiGatherChangeableOptionForReserved
 }
 } // namespace
 
+namespace {
+void apiGatherChangeableGlobalOption
+(Option* option, const KeyVals& options,
+ const SharedHandle<OptionParser>& optionParser)
+{
+  apiGatherOption(options.begin(), options.end(),
+                  std::mem_fun(&OptionHandler::getChangeGlobalOption),
+                  option, optionParser);
+}
+} // namespace
+
 namespace {
 void addRequestGroup(const SharedHandle<RequestGroup>& group,
                      const SharedHandle<DownloadEngine>& 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<DownloadEngine>& e =
+    session->context->reqinfo->getDownloadEngine();
+  return e->getOption()->get(option::k2p(name));
+}
+
+KeyVals getGlobalOptions(Session* session)
+{
+  const SharedHandle<DownloadEngine>& e =
+    session->context->reqinfo->getDownloadEngine();
+  const SharedHandle<OptionParser>& 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<DownloadEngine>& 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<A2Gid> getActiveDownload(Session* session)
 {
   const SharedHandle<DownloadEngine>& e =

+ 49 - 0
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
  *

+ 22 - 0
test/Aria2ApiTest.cc

@@ -2,6 +2,10 @@
 
 #include <cppunit/extensions/HelperMacros.h>
 
+#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