From cf5cb05816fd25b43b271c6175494f6b976ba7bc Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 3 Sep 2007 10:32:19 +0000 Subject: [PATCH] 2007-09-03 Tatsuhiro Tsujikawa Automatically save *.aria2 control file of http/ftp download in every 60 seconds. * src/AutoSaveCommand.{h, cc}: New class. * src/TimeBasedCommand.{h, cc}: New class. * src/DownloadEngineFactory.cc (newConsoleEngine) --- ChangeLog | 8 ++++ TODO | 8 ++++ src/AutoSaveCommand.cc | 47 ++++++++++++++++++++ src/AutoSaveCommand.h | 53 +++++++++++++++++++++++ src/DownloadEngineFactory.cc | 2 + src/Makefile.am | 4 +- src/Makefile.in | 25 ++++++----- src/TimeBasedCommand.cc | 56 ++++++++++++++++++++++++ src/TimeBasedCommand.h | 84 ++++++++++++++++++++++++++++++++++++ 9 files changed, 276 insertions(+), 11 deletions(-) create mode 100644 src/AutoSaveCommand.cc create mode 100644 src/AutoSaveCommand.h create mode 100644 src/TimeBasedCommand.cc create mode 100644 src/TimeBasedCommand.h diff --git a/ChangeLog b/ChangeLog index 6654c83a..6c0ec18b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2007-09-03 Tatsuhiro Tsujikawa + + Automatically save *.aria2 control file of http/ftp download in every + 60 seconds. + * src/AutoSaveCommand.{h, cc}: New class. + * src/TimeBasedCommand.{h, cc}: New class. + * src/DownloadEngineFactory.cc (newConsoleEngine) + 2007-09-02 Tatsuhiro Tsujikawa Now *.aria2 contorol file is first saved to *.aria2__temp and if diff --git a/TODO b/TODO index f2a7cbf0..eed20372 100644 --- a/TODO +++ b/TODO @@ -39,4 +39,12 @@ * used globally -> common.h * Add pgp verification support. I have to use libgpgme?? + +* Rewrite following classes using TimeBasedCommand + src/ActivePeerConnectionCommand.h + src/FillRequestGroupCommand.h + src/HaveEraseCommand.h + src/PeerChokeCommand.h + src/TorrentAutoSaveCommand.h * Rewrite MetaFileUtil +* Integrate FTP/HTTP/BitTorrent downloads from metalinks diff --git a/src/AutoSaveCommand.cc b/src/AutoSaveCommand.cc new file mode 100644 index 00000000..a894ac7e --- /dev/null +++ b/src/AutoSaveCommand.cc @@ -0,0 +1,47 @@ +/* */ +#include "AutoSaveCommand.h" + +void AutoSaveCommand::preProcess() +{ + if(_e->_requestGroupMan->downloadFinished()) { + _exit = true; + } +} + +void AutoSaveCommand::process() +{ + _e->_requestGroupMan->save(); +} diff --git a/src/AutoSaveCommand.h b/src/AutoSaveCommand.h new file mode 100644 index 00000000..6a1f23ff --- /dev/null +++ b/src/AutoSaveCommand.h @@ -0,0 +1,53 @@ +/* */ +#ifndef _D_AUTO_SAVE_COMMAND_H_ +#define _D_AUTO_SAVE_COMMAND_H_ + +#include "TimeBasedCommand.h" + +class AutoSaveCommand : public TimeBasedCommand +{ +public: + AutoSaveCommand(int32_t cuid, DownloadEngine* e, int32_t interval): + TimeBasedCommand(cuid, e, interval) {} + + virtual ~AutoSaveCommand() {} + + virtual void preProcess(); + + virtual void process(); +}; + +#endif // _D_AUTO_SAVE_COMMAND_H_ diff --git a/src/DownloadEngineFactory.cc b/src/DownloadEngineFactory.cc index eb3ea985..2c8f5b24 100644 --- a/src/DownloadEngineFactory.cc +++ b/src/DownloadEngineFactory.cc @@ -43,6 +43,7 @@ #include "CUIDCounter.h" #include "FileAllocationDispatcherCommand.h" #include "FileAllocationMan.h" +#include "AutoSaveCommand.h" #ifdef ENABLE_MESSAGE_DIGEST # include "CheckIntegrityMan.h" #endif // ENABLE_MESSAGE_DIGEST @@ -94,6 +95,7 @@ DownloadEngineFactory::newConsoleEngine(const Option* op, #endif // ENABLE_MESSAGE_DIGEST e->commands.push_back(new FillRequestGroupCommand(CUIDCounterSingletonHolder::instance()->newID(), e, 1)); e->commands.push_back(new FileAllocationDispatcherCommand(CUIDCounterSingletonHolder::instance()->newID(), e)); + e->commands.push_back(new AutoSaveCommand(CUIDCounterSingletonHolder::instance()->newID(), e, op->getAsInt(PREF_AUTO_SAVE_INTERVAL))); return e; } diff --git a/src/Makefile.am b/src/Makefile.am index 697b4af4..39441014 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -108,7 +108,9 @@ SRCS = Socket.h\ ParameterizedStringParser.cc ParameterizedStringParser.h\ FixedWidthNumberDecorator.h\ NumberDecorator.h\ - AlphaNumberDecorator.h + AlphaNumberDecorator.h\ + TimeBasedCommand.cc TimeBasedCommand.h\ + AutoSaveCommand.cc AutoSaveCommand.h # debug_new.cpp if ENABLE_MESSAGE_DIGEST diff --git a/src/Makefile.in b/src/Makefile.in index cba6ff5d..92fbf514 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -261,7 +261,8 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \ PStringBuildVisitor.cc PStringBuildVisitor.h \ ParameterizedStringParser.cc ParameterizedStringParser.h \ FixedWidthNumberDecorator.h NumberDecorator.h \ - AlphaNumberDecorator.h ChunkChecksumValidator.cc \ + AlphaNumberDecorator.h TimeBasedCommand.cc TimeBasedCommand.h \ + AutoSaveCommand.cc AutoSaveCommand.h ChunkChecksumValidator.cc \ ChunkChecksumValidator.h IteratableChunkChecksumValidator.cc \ IteratableChunkChecksumValidator.h \ IteratableChecksumValidator.cc IteratableChecksumValidator.h \ @@ -471,11 +472,11 @@ am__objects_12 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \ CookieBoxFactory.$(OBJEXT) HttpHeaderProcessor.$(OBJEXT) \ FileEntry.$(OBJEXT) Platform.$(OBJEXT) \ PStringSegment.$(OBJEXT) PStringBuildVisitor.$(OBJEXT) \ - ParameterizedStringParser.$(OBJEXT) $(am__objects_1) \ - $(am__objects_2) $(am__objects_3) $(am__objects_4) \ - $(am__objects_5) $(am__objects_6) $(am__objects_7) \ - $(am__objects_8) $(am__objects_9) $(am__objects_10) \ - $(am__objects_11) + ParameterizedStringParser.$(OBJEXT) TimeBasedCommand.$(OBJEXT) \ + AutoSaveCommand.$(OBJEXT) $(am__objects_1) $(am__objects_2) \ + $(am__objects_3) $(am__objects_4) $(am__objects_5) \ + $(am__objects_6) $(am__objects_7) $(am__objects_8) \ + $(am__objects_9) $(am__objects_10) $(am__objects_11) am_libaria2c_a_OBJECTS = $(am__objects_12) libaria2c_a_OBJECTS = $(am_libaria2c_a_OBJECTS) am__installdirs = "$(DESTDIR)$(bindir)" @@ -733,10 +734,12 @@ SRCS = Socket.h SocketCore.cc SocketCore.h Command.cc Command.h \ PStringBuildVisitor.cc PStringBuildVisitor.h \ ParameterizedStringParser.cc ParameterizedStringParser.h \ FixedWidthNumberDecorator.h NumberDecorator.h \ - AlphaNumberDecorator.h $(am__append_1) $(am__append_2) \ - $(am__append_3) $(am__append_4) $(am__append_5) \ - $(am__append_6) $(am__append_7) $(am__append_8) \ - $(am__append_9) $(am__append_10) $(am__append_11) + AlphaNumberDecorator.h TimeBasedCommand.cc TimeBasedCommand.h \ + AutoSaveCommand.cc AutoSaveCommand.h $(am__append_1) \ + $(am__append_2) $(am__append_3) $(am__append_4) \ + $(am__append_5) $(am__append_6) $(am__append_7) \ + $(am__append_8) $(am__append_9) $(am__append_10) \ + $(am__append_11) noinst_LIBRARIES = libaria2c.a libaria2c_a_SOURCES = $(SRCS) aria2c_LDADD = libaria2c.a @LIBINTL@ @ALLOCA@ @LIBGNUTLS_LIBS@\ @@ -832,6 +835,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ActivePeerConnectionCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AnnounceList.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AuthConfig.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AutoSaveCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Base64.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BitfieldMan.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BitfieldManFactory.Po@am__quote@ @@ -970,6 +974,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SocketCore.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SpeedCalc.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TimeA2.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TimeBasedCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TorrentAutoSaveCommand.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TorrentConsoleDownloadEngine.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/TorrentDownloadEngine.Po@am__quote@ diff --git a/src/TimeBasedCommand.cc b/src/TimeBasedCommand.cc new file mode 100644 index 00000000..1803a38e --- /dev/null +++ b/src/TimeBasedCommand.cc @@ -0,0 +1,56 @@ +/* */ +#include "TimeBasedCommand.h" + +bool TimeBasedCommand::execute() +{ + preProcess(); + if(_exit) { + return true; + } + if(_checkPoint.elapsed(_interval)) { + _checkPoint.reset(); + process(); + if(_exit) { + return true; + } + } + postProcess(); + if(_exit) { + return true; + } + _e->commands.push_back(this); + return false; +} diff --git a/src/TimeBasedCommand.h b/src/TimeBasedCommand.h new file mode 100644 index 00000000..f23353f6 --- /dev/null +++ b/src/TimeBasedCommand.h @@ -0,0 +1,84 @@ +/* */ +#ifndef _D_TIME_BASED_COMMAND_H_ +#define _D_TIME_BASED_COMMAND_H_ + +#include "Command.h" +#include "TimeA2.h" +#include "DownloadEngine.h" + +class TimeBasedCommand : public Command +{ +protected: + DownloadEngine* _e; + + /** + * setting _exit to true if this command's job has finished and you want to + * delete this command. + * The _exit variable is evaluated after preProcess(), process(), + * postProcess(), and terminate processing immediately and excute() returns + * true. + */ + bool _exit; +private: + int32_t _interval; // unit: sec + Time _checkPoint; +public: + /** + * preProcess() is called each time when excute() is called. + */ + virtual void preProcess() {}; + + /** + * process() is called only when excute() is called and specified time has + * elapsed. + */ + virtual void process() = 0; + + /** + * postProcess() is called each time when excute() is called. + */ + virtual void postProcess() {}; + +public: + TimeBasedCommand(int32_t cuid, DownloadEngine* e, int32_t interval): + Command(cuid), _e(e),_exit(false), _interval(interval) {} + + virtual ~TimeBasedCommand() {} + + virtual bool execute(); +}; + +#endif // _D_TIME_BASED_COMMAND_H_