From db46bab38ba1b9e96949ba8cd1ffaa59fe14ef34 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 10 Aug 2008 15:22:55 +0000 Subject: [PATCH] 2008-08-11 Tatsuhiro Tsujikawa Added options to load/save the server's performance/status to a file and the timeout to drop their data. --server-stat-of=FILE specifies the file to which performance data is saved. --server-stat-if=FILE specifies the file to read previously saved by --server-stat-of option. Might be used with --uri-selector=feedback. --server-stat-timeout=TIMEOUT specifies timeout to invalidate the data. TIMEOUT is specified in seconds and the default value is 24hours. * src/MultiUrlRequestInfo.cc * src/OptionHandlerFactory.cc * src/RequestGroupMan.cc * src/RequestGroupMan.h * src/ServerStatMan.cc * src/ServerStatMan.h * src/option_processing.cc * src/prefs.cc * src/prefs.h * test/ServerStatManTest.cc --- ChangeLog | 21 +++++++++++++++++++ src/MultiUrlRequestInfo.cc | 10 +++++++++ src/OptionHandlerFactory.cc | 5 +++++ src/RequestGroupMan.cc | 41 +++++++++++++++++++++++++++++++++++++ src/RequestGroupMan.h | 6 ++++++ src/ServerStatMan.cc | 6 ++++-- src/ServerStatMan.h | 4 ++-- src/option_processing.cc | 13 ++++++++++++ src/prefs.cc | 6 ++++++ src/prefs.h | 6 ++++++ test/ServerStatManTest.cc | 4 ++-- 11 files changed, 116 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 25d26614..65180fa9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,24 @@ +2008-08-11 Tatsuhiro Tsujikawa + + Added options to load/save the server's performance/status to a file + and the timeout to drop their data. + --server-stat-of=FILE specifies the file to which performance data + is saved. + --server-stat-if=FILE specifies the file to read previously saved + by --server-stat-of option. Might be used with --uri-selector=feedback. + --server-stat-timeout=TIMEOUT specifies timeout to invalidate the data. + TIMEOUT is specified in seconds and the default value is 24hours. + * src/MultiUrlRequestInfo.cc + * src/OptionHandlerFactory.cc + * src/RequestGroupMan.cc + * src/RequestGroupMan.h + * src/ServerStatMan.cc + * src/ServerStatMan.h + * src/option_processing.cc + * src/prefs.cc + * src/prefs.h + * test/ServerStatManTest.cc + 2008-08-09 Tatsuhiro Tsujikawa Implemented ServerStatMan::removeStaleServerStat() and its test case. diff --git a/src/MultiUrlRequestInfo.cc b/src/MultiUrlRequestInfo.cc index ac394af9..17ef6c4e 100644 --- a/src/MultiUrlRequestInfo.cc +++ b/src/MultiUrlRequestInfo.cc @@ -100,6 +100,12 @@ int MultiUrlRequestInfo::execute() DownloadEngineHandle e = DownloadEngineFactory().newDownloadEngine(_option, _requestGroups); + std::string serverStatIf = _option->get(PREF_SERVER_STAT_IF); + if(!serverStatIf.empty()) { + e->_requestGroupMan->loadServerStat(serverStatIf); + e->_requestGroupMan->removeStaleServerStat + (_option->getAsInt(PREF_SERVER_STAT_TIMEOUT)); + } e->setStatCalc(_statCalc); e->fillCommand(); @@ -115,6 +121,10 @@ int MultiUrlRequestInfo::execute() e->run(); + std::string serverStatOf = _option->get(PREF_SERVER_STAT_OF); + if(!serverStatOf.empty()) { + e->_requestGroupMan->saveServerStat(serverStatOf); + } e->_requestGroupMan->showDownloadResults(_summaryOut); _summaryOut << std::flush; diff --git a/src/OptionHandlerFactory.cc b/src/OptionHandlerFactory.cc index 8153f6eb..4c106fb2 100644 --- a/src/OptionHandlerFactory.cc +++ b/src/OptionHandlerFactory.cc @@ -150,6 +150,11 @@ OptionHandlers OptionHandlerFactory::createOptionHandlers() std::deque (¶ms[0], ¶ms[arrayLength(params)])))); } + handlers.push_back(SH(new NumberOptionHandler(PREF_SERVER_STAT_TIMEOUT, + 0, INT32_MAX))); + handlers.push_back(SH(new DefaultOptionHandler(PREF_SERVER_STAT_IF))); + handlers.push_back(SH(new DefaultOptionHandler(PREF_SERVER_STAT_OF))); + return handlers; } diff --git a/src/RequestGroupMan.cc b/src/RequestGroupMan.cc index 530cf2e3..a795192b 100644 --- a/src/RequestGroupMan.cc +++ b/src/RequestGroupMan.cc @@ -54,6 +54,7 @@ #include #include #include +#include #include #include @@ -477,4 +478,44 @@ bool RequestGroupMan::addServerStat(const SharedHandle& serverStat) return _serverStatMan->add(serverStat); } +bool RequestGroupMan::loadServerStat(const std::string& filename) +{ + std::ifstream in(filename.c_str()); + if(!in) { + _logger->error("Failed to open ServerStat file %s for read.", + filename.c_str()); + return false; + } + if(_serverStatMan->load(in)) { + _logger->notice("ServerStat file %s loaded successfully.", + filename.c_str()); + return true; + } else { + _logger->error("Failed to read ServerStat from %s.", filename.c_str()); + return false; + } +} + +bool RequestGroupMan::saveServerStat(const std::string& filename) const +{ + std::ofstream out(filename.c_str()); + if(!out) { + _logger->error("Failed to open ServerStat file %s for write.", + filename.c_str()); + return false; + } + if(_serverStatMan->save(out)) { + _logger->notice("ServerStat file %s saved successfully.", filename.c_str()); + return true; + } else { + _logger->error("Failed to write ServerStat to %s.", filename.c_str()); + return false; + } +} + +void RequestGroupMan::removeStaleServerStat(time_t timeout) +{ + _serverStatMan->removeStaleServerStat(timeout); +} + } // namespace aria2 diff --git a/src/RequestGroupMan.h b/src/RequestGroupMan.h index 700b71a4..446c4299 100644 --- a/src/RequestGroupMan.h +++ b/src/RequestGroupMan.h @@ -143,6 +143,12 @@ public: void updateServerStat(); + + bool loadServerStat(const std::string& filename); + + bool saveServerStat(const std::string& filename) const; + + void removeStaleServerStat(time_t timeout); }; typedef SharedHandle RequestGroupManHandle; diff --git a/src/ServerStatMan.cc b/src/ServerStatMan.cc index 7f671fdf..464e228c 100644 --- a/src/ServerStatMan.cc +++ b/src/ServerStatMan.cc @@ -74,13 +74,14 @@ bool ServerStatMan::add(const SharedHandle& serverStat) } } -void ServerStatMan::save(std::ostream& out) const +bool ServerStatMan::save(std::ostream& out) const { std::copy(_serverStats.begin(), _serverStats.end(), std::ostream_iterator >(out, "\n")); + return !out.bad(); } -void ServerStatMan::load(std::istream& in) +bool ServerStatMan::load(std::istream& in) { static const std::string S_HOST = "host"; static const std::string S_PROTOCOL = "protocol"; @@ -117,6 +118,7 @@ void ServerStatMan::load(std::istream& in) continue; } } + return !in.bad(); } class FindStaleServerStat { diff --git a/src/ServerStatMan.h b/src/ServerStatMan.h index 0104b5e5..574dc085 100644 --- a/src/ServerStatMan.h +++ b/src/ServerStatMan.h @@ -55,9 +55,9 @@ public: bool add(const SharedHandle& serverStat); - void load(std::istream& in); + bool load(std::istream& in); - void save(std::ostream& out) const; + bool save(std::ostream& out) const; void removeStaleServerStat(time_t timeout); private: diff --git a/src/option_processing.cc b/src/option_processing.cc index 9736ea7a..ce594448 100644 --- a/src/option_processing.cc +++ b/src/option_processing.cc @@ -157,6 +157,7 @@ Option* createDefaultOption() op->put(PREF_SUMMARY_INTERVAL, "60"); op->put(PREF_LOG_LEVEL, V_DEBUG); op->put(PREF_URI_SELECTOR, V_INORDER); + op->put(PREF_SERVER_STAT_TIMEOUT, "86400");// 1day return op; } @@ -235,6 +236,9 @@ Option* option_processing(int argc, char* const argv[]) { PREF_SUMMARY_INTERVAL.c_str(), required_argument, &lopt, 218 }, { PREF_LOG_LEVEL.c_str(), required_argument, &lopt, 219 }, { PREF_URI_SELECTOR.c_str(), required_argument, &lopt, 220 }, + { PREF_SERVER_STAT_IF.c_str(), required_argument, &lopt, 221 }, + { PREF_SERVER_STAT_OF.c_str(), required_argument, &lopt, 222 }, + { PREF_SERVER_STAT_TIMEOUT.c_str(), required_argument, &lopt, 223 }, #if defined ENABLE_BITTORRENT || defined ENABLE_METALINK { PREF_SHOW_FILES.c_str(), no_argument, NULL, 'S' }, { PREF_SELECT_FILE.c_str(), required_argument, &lopt, 21 }, @@ -466,6 +470,15 @@ Option* option_processing(int argc, char* const argv[]) case 220: cmdstream << PREF_URI_SELECTOR << "=" << optarg << "\n"; break; + case 221: + cmdstream << PREF_SERVER_STAT_IF << "=" << optarg << "\n"; + break; + case 222: + cmdstream << PREF_SERVER_STAT_OF << "=" << optarg << "\n"; + break; + case 223: + cmdstream << PREF_SERVER_STAT_TIMEOUT << "=" << optarg << "\n"; + break; } break; } diff --git a/src/prefs.cc b/src/prefs.cc index 34608fb5..44205fa8 100644 --- a/src/prefs.cc +++ b/src/prefs.cc @@ -139,6 +139,12 @@ const std::string V_ERROR("error"); const std::string PREF_URI_SELECTOR("uri-selector"); const std::string V_INORDER("inorder"); const std::string V_FEEDBACK("feedback"); +// value: 1*digit +const std::string PREF_SERVER_STAT_TIMEOUT("server-stat-timeout"); +// value: string that your file system recognizes as a file name. +const std::string PREF_SERVER_STAT_IF("server-stat-if"); +// value: string that your file system recognizes as a file name. +const std::string PREF_SERVER_STAT_OF("server-stat-of"); /** * FTP related preferences diff --git a/src/prefs.h b/src/prefs.h index e4f9c02d..63304c0f 100644 --- a/src/prefs.h +++ b/src/prefs.h @@ -143,6 +143,12 @@ extern const std::string V_ERROR; extern const std::string PREF_URI_SELECTOR; extern const std::string V_INORDER; extern const std::string V_FEEDBACK; +// value: 1*digit +extern const std::string PREF_SERVER_STAT_TIMEOUT; +// value: string that your file system recognizes as a file name. +extern const std::string PREF_SERVER_STAT_IF; +// value: string that your file system recognizes as a file name. +extern const std::string PREF_SERVER_STAT_OF; /** * FTP related preferences diff --git a/test/ServerStatManTest.cc b/test/ServerStatManTest.cc index 63d46d02..c000ff52 100644 --- a/test/ServerStatManTest.cc +++ b/test/ServerStatManTest.cc @@ -73,7 +73,7 @@ void ServerStatManTest::testSave() CPPUNIT_ASSERT(ssm.add(mirror)); std::stringstream ss; - ssm.save(ss); + CPPUNIT_ASSERT(ssm.save(ss)); std::string out = ss.str(); CPPUNIT_ASSERT_EQUAL (std::string @@ -93,7 +93,7 @@ void ServerStatManTest::testLoad() std::stringstream ss(in); ServerStatMan ssm; - ssm.load(ss); + CPPUNIT_ASSERT(ssm.load(ss)); SharedHandle localhost_http = ssm.find("localhost", "http"); CPPUNIT_ASSERT(!localhost_http.isNull());