Bläddra i källkod

Add aria2.saveSession RPC method

This method saves the current session to a file specified by
--save-session option. This method returns "OK" if it succeeds.
Tatsuhiro Tsujikawa 11 år sedan
förälder
incheckning
a8319a8b78
5 ändrade filer med 40 tillägg och 0 borttagningar
  1. 6 0
      doc/manual-src/en/aria2c.rst
  2. 3 0
      doc/xmlrpc/aria2rpc
  3. 2 0
      src/RpcMethodFactory.cc
  4. 18 0
      src/RpcMethodImpl.cc
  5. 11 0
      src/RpcMethodImpl.h

+ 6 - 0
doc/manual-src/en/aria2c.rst

@@ -3209,6 +3209,12 @@ For *secret* parameter, see :ref:`rpc_auth`.
   except that any actions which takes time such as contacting BitTorrent
   tracker are skipped. This method returns ``OK``.
 
+.. function:: aria2.saveSession([secret])
+
+  This method saves the current session to a file specified by
+  :option:`--save-session` option. This method returns ``OK`` if it
+  succeeds.
+
 .. function:: system.multicall(methods)
 
   This methods encapsulates multiple method calls in a single request.

+ 3 - 0
doc/xmlrpc/aria2rpc

@@ -273,6 +273,7 @@ Usage: #{program_name} addUri URI... [options]
        #{program_name} shutdown [options]
        #{program_name} forceShutdown [options]
        #{program_name} getGlobalStat [options]
+       #{program_name} saveSession [options]
        #{program_name} appendUri GID fileIndex URI... [options]
            This command calls aria2.changeUri(GID, fileIndex, [], [URI,...])
            internally.
@@ -394,6 +395,8 @@ elsif command == "forceShutdown" then
   result=client_call(client, secret, "aria2."+command)
 elsif command == "getGlobalStat" then
   result=client_call(client, secret, "aria2."+command)
+elsif command == "saveSession" then
+  result=client_call(client, secret, "aria2."+command)
 elsif command == "appendUri" then
   result=client_call(client, secret, "aria2.changeUri", resources[0],
                      resources[1].to_i(), [], resources[2..-1])

+ 2 - 0
src/RpcMethodFactory.cc

@@ -126,6 +126,8 @@ createMethod(const std::string& methodName)
     return make_unique<ForceShutdownRpcMethod>();
   } else if(methodName == GetGlobalStatRpcMethod::getMethodName()) {
     return make_unique<GetGlobalStatRpcMethod>();
+  } else if(methodName == SaveSessionRpcMethod::getMethodName()) {
+    return make_unique<SaveSessionRpcMethod>();
   } else if(methodName == SystemMulticallRpcMethod::getMethodName()) {
     return make_unique<SystemMulticallRpcMethod>();
   } else {

+ 18 - 0
src/RpcMethodImpl.cc

@@ -65,6 +65,7 @@
 #include "PeerStat.h"
 #include "base64.h"
 #include "BitfieldMan.h"
+#include "SessionSerializer.h"
 #ifdef ENABLE_MESSAGE_DIGEST
 # include "MessageDigest.h"
 # include "message_digest_helper.h"
@@ -1347,6 +1348,23 @@ std::unique_ptr<ValueBase> GetGlobalStatRpcMethod::process
   return std::move(res);
 }
 
+std::unique_ptr<ValueBase> SaveSessionRpcMethod::process
+(const RpcRequest& req, DownloadEngine* e)
+{
+  const std::string& filename = e->getOption()->get(PREF_SAVE_SESSION);
+  if(filename.empty()) {
+    throw DL_ABORT_EX("Filename is not given.");
+  }
+  SessionSerializer sessionSerializer(e->getRequestGroupMan().get());
+  if(sessionSerializer.save(filename)) {
+    A2_LOG_NOTICE(fmt(_("Serialized session to '%s' successfully."),
+                      filename.c_str()));
+    return createOKResponse();
+  }
+  throw DL_ABORT_EX(fmt("Failed to serialize session to '%s'.",
+                        filename.c_str()));
+}
+
 std::unique_ptr<ValueBase> SystemMulticallRpcMethod::process
 (const RpcRequest& req, DownloadEngine* e)
 {

+ 11 - 0
src/RpcMethodImpl.h

@@ -567,6 +567,17 @@ public:
   }
 };
 
+class SaveSessionRpcMethod:public RpcMethod {
+protected:
+  virtual std::unique_ptr<ValueBase> process
+  (const RpcRequest& req, DownloadEngine* e) CXX11_OVERRIDE;
+public:
+  static const char* getMethodName()
+  {
+    return "aria2.saveSession";
+  }
+};
+
 class SystemMulticallRpcMethod:public RpcMethod {
 protected:
   virtual std::unique_ptr<ValueBase> process