Преглед на файлове

Added --save-session-interval option

--save-session-interval option saves error/unfinished downloads to a
file specified by --save-session option every SEC seconds. If 0 is
given, file will be saved only when aria2 exits.
Tatsuhiro Tsujikawa преди 12 години
родител
ревизия
8e63bad2da
променени са 8 файла, в които са добавени 160 реда и са изтрити 1 реда
  1. 6 0
      src/DownloadEngineFactory.cc
  2. 2 1
      src/Makefile.am
  3. 9 0
      src/OptionHandlerFactory.cc
  4. 78 0
      src/SaveSessionCommand.cc
  5. 56 0
      src/SaveSessionCommand.h
  6. 2 0
      src/prefs.cc
  7. 2 0
      src/prefs.h
  8. 5 0
      src/usage_text.h

+ 6 - 0
src/DownloadEngineFactory.cc

@@ -50,6 +50,7 @@
 #include "FillRequestGroupCommand.h"
 #include "FileAllocationDispatcherCommand.h"
 #include "AutoSaveCommand.h"
+#include "SaveSessionCommand.h"
 #include "HaveEraseCommand.h"
 #include "TimedHaltCommand.h"
 #include "WatchProcessCommand.h"
@@ -158,6 +159,11 @@ DownloadEngineFactory::newDownloadEngine
       (new AutoSaveCommand(e->newCUID(), e.get(),
                            op->getAsInt(PREF_AUTO_SAVE_INTERVAL)));
   }
+  if(op->getAsInt(PREF_SAVE_SESSION_INTERVAL) > 0) {
+    e->addRoutineCommand
+      (new SaveSessionCommand(e->newCUID(), e.get(),
+                              op->getAsInt(PREF_SAVE_SESSION_INTERVAL)));
+  }
   e->addRoutineCommand(new HaveEraseCommand(e->newCUID(), e.get(), 10));
   {
     time_t stopSec = op->getAsInt(PREF_STOP);

+ 2 - 1
src/Makefile.am

@@ -244,7 +244,8 @@ SRCS =  SocketCore.cc SocketCore.h\
 	WrDiskCache.cc WrDiskCache.h\
 	WrDiskCacheEntry.cc WrDiskCacheEntry.h\
 	GroupId.cc GroupId.h\
-	IndexedList.h
+	IndexedList.h\
+	SaveSessionCommand.h SaveSessionCommand.cc
 
 if MINGW_BUILD
 SRCS += WinConsoleFile.cc WinConsoleFile.h

+ 9 - 0
src/OptionHandlerFactory.cc

@@ -728,6 +728,15 @@ std::vector<OptionHandler*> OptionHandlerFactory::createOptionHandlers()
     op->setChangeGlobalOption(true);
     handlers.push_back(op);
   }
+  {
+    OptionHandler* op(new NumberOptionHandler
+                      (PREF_SAVE_SESSION_INTERVAL,
+                       TEXT_SAVE_SESSION_INTERVAL,
+                       "0",
+                       0));
+    op->addTag(TAG_ADVANCED);
+    handlers.push_back(op);
+  }
   {
     OptionHandler* op(new BooleanOptionHandler
                       (PREF_SELECT_LEAST_USED_HOST,

+ 78 - 0
src/SaveSessionCommand.cc

@@ -0,0 +1,78 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2013 Tatsuhiro Tsujikawa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#include "SaveSessionCommand.h"
+#include "DownloadEngine.h"
+#include "RequestGroupMan.h"
+#include "SessionSerializer.h"
+#include "prefs.h"
+#include "fmt.h"
+#include "LogFactory.h"
+#include "Option.h"
+
+namespace aria2 {
+
+SaveSessionCommand::SaveSessionCommand
+(cuid_t cuid, DownloadEngine* e, time_t interval)
+  : TimeBasedCommand(cuid, e, interval, true)
+{}
+
+SaveSessionCommand::~SaveSessionCommand() {}
+
+void SaveSessionCommand::preProcess()
+{
+  if(getDownloadEngine()->getRequestGroupMan()->downloadFinished() ||
+     getDownloadEngine()->isHaltRequested()) {
+    enableExit();
+  }
+}
+
+void SaveSessionCommand::process()
+{
+  const std::string& filename = getDownloadEngine()->getOption()
+    ->get(PREF_SAVE_SESSION);
+  if(!filename.empty()) {
+    SessionSerializer sessionSerializer(getDownloadEngine()->
+                                        getRequestGroupMan());
+    if(sessionSerializer.save(filename)) {
+      A2_LOG_NOTICE(fmt(_("Serialized session to '%s' successfully."),
+                        filename.c_str()));
+    } else {
+      A2_LOG_NOTICE(fmt(_("Failed to serialize session to '%s'."),
+                        filename.c_str()));
+    }
+  }
+}
+
+} // namespace aria2

+ 56 - 0
src/SaveSessionCommand.h

@@ -0,0 +1,56 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2013 Tatsuhiro Tsujikawa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef D_SAVE_SESSION_COMMAND_H
+#define D_SAVE_SESSION_COMMAND_H
+
+#include "TimeBasedCommand.h"
+
+namespace aria2 {
+
+class SaveSessionCommand : public TimeBasedCommand
+{
+public:
+  SaveSessionCommand(cuid_t cuid, DownloadEngine* e, time_t interval);
+
+  virtual ~SaveSessionCommand();
+
+  virtual void preProcess();
+
+  virtual void process();
+};
+
+} // namespace aria2
+
+#endif // D_SAVE_SESSION_COMMAND_H

+ 2 - 0
src/prefs.cc

@@ -356,6 +356,8 @@ const Pref* PREF_FORCE_SAVE = makePref("force-save");
 const Pref* PREF_DISK_CACHE = makePref("disk-cache");
 // value: string
 const Pref* PREF_GID = makePref("gid");
+// values: 1*digit
+const Pref* PREF_SAVE_SESSION_INTERVAL = makePref("save-session-interval");
 
 /**
  * FTP related preferences

+ 2 - 0
src/prefs.h

@@ -291,6 +291,8 @@ extern const Pref* PREF_FORCE_SAVE;
 extern const Pref* PREF_DISK_CACHE;
 // value: string
 extern const Pref* PREF_GID;
+// values: 1*digit
+extern const Pref* PREF_SAVE_SESSION_INTERVAL;
 
 /**
  * FTP related preferences

+ 5 - 0
src/usage_text.h

@@ -944,3 +944,8 @@
     "                              by aria2.")
 #define TEXT_CONSOLE_LOG_LEVEL                                          \
   _(" --console-log-level=LEVEL    Set log level to output to console.")
+#define TEXT_SAVE_SESSION_INTERVAL                                      \
+  _(" --save-session-interval=SEC  Save error/unfinished downloads to a file\n" \
+    "                              specified by --save-session option every SEC\n" \
+    "                              seconds. If 0 is given, file will be saved only\n" \
+    "                              when aria2 exits.")