ソースを参照

2008-08-11 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

	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
Tatsuhiro Tsujikawa 17 年 前
コミット
db46bab38b

+ 21 - 0
ChangeLog

@@ -1,3 +1,24 @@
+2008-08-11  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
+
+	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  <tujikawa at rednoah dot com>
 
 	Implemented ServerStatMan::removeStaleServerStat() and its test case.

+ 10 - 0
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;
 

+ 5 - 0
src/OptionHandlerFactory.cc

@@ -150,6 +150,11 @@ OptionHandlers OptionHandlerFactory::createOptionHandlers()
 			   std::deque<std::string>
 			   (&params[0], &params[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;
 }
 

+ 41 - 0
src/RequestGroupMan.cc

@@ -54,6 +54,7 @@
 #include <iomanip>
 #include <sstream>
 #include <ostream>
+#include <fstream>
 #include <numeric>
 #include <algorithm>
 
@@ -477,4 +478,44 @@ bool RequestGroupMan::addServerStat(const SharedHandle<ServerStat>& 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

+ 6 - 0
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<RequestGroupMan> RequestGroupManHandle;

+ 4 - 2
src/ServerStatMan.cc

@@ -74,13 +74,14 @@ bool ServerStatMan::add(const SharedHandle<ServerStat>& serverStat)
   } 
 }
 
-void ServerStatMan::save(std::ostream& out) const
+bool ServerStatMan::save(std::ostream& out) const
 {
   std::copy(_serverStats.begin(), _serverStats.end(),
 	    std::ostream_iterator<SharedHandle<ServerStat> >(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 {

+ 2 - 2
src/ServerStatMan.h

@@ -55,9 +55,9 @@ public:
 
   bool add(const SharedHandle<ServerStat>& 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:

+ 13 - 0
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;
     }

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

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

+ 2 - 2
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<ServerStat> localhost_http = ssm.find("localhost", "http");
   CPPUNIT_ASSERT(!localhost_http.isNull());