浏览代码

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

	Implemented ServerStatMan::save(...) function and its test case.
	* src/ServerStat.cc
	* src/ServerStat.h
	* src/ServerStatMan.cc
	* src/ServerStatMan.h
	* test/ServerStatManTest.cc
Tatsuhiro Tsujikawa 17 年之前
父节点
当前提交
26690f692b
共有 6 个文件被更改,包括 68 次插入13 次删除
  1. 9 0
      ChangeLog
  2. 18 2
      src/ServerStat.cc
  3. 4 2
      src/ServerStat.h
  4. 4 7
      src/ServerStatMan.cc
  5. 0 2
      src/ServerStatMan.h
  6. 33 0
      test/ServerStatManTest.cc

+ 9 - 0
ChangeLog

@@ -1,3 +1,12 @@
+2008-08-09  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
+
+	Implemented ServerStatMan::save(...) function and its test case.
+	* src/ServerStat.cc
+	* src/ServerStat.h
+	* src/ServerStatMan.cc
+	* src/ServerStatMan.h
+	* test/ServerStatManTest.cc
+
 2008-08-09  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
 
 	Now aria2 uses name attribute in Metalink as local filename in

+ 18 - 2
src/ServerStat.cc

@@ -37,6 +37,11 @@
 
 namespace aria2 {
 
+const std::string ServerStat::STATUS_STRING[] = {
+  "OK",
+  "ERROR"
+};
+
 ServerStat::ServerStat(const std::string& hostname, const std::string& protocol)
   :
   _hostname(hostname),
@@ -56,16 +61,26 @@ const std::string& ServerStat::getProtocol() const
   return _protocol;
 }
 
-Time ServerStat::getLastUpdated() const
+const Time& ServerStat::getLastUpdated() const
 {
   return _lastUpdated;
 }
 
+void ServerStat::setLastUpdated(const Time& time)
+{
+  _lastUpdated = time;
+}
+
 unsigned int ServerStat::getDownloadSpeed() const
 {
   return _downloadSpeed;
 }
 
+void ServerStat::setDownloadSpeed(unsigned int downloadSpeed)
+{
+  _downloadSpeed = downloadSpeed;
+}
+
 void ServerStat::updateDownloadSpeed(unsigned int downloadSpeed)
 {
   _downloadSpeed = downloadSpeed;
@@ -126,7 +141,8 @@ std::ostream& operator<<(std::ostream& o, const ServerStat& serverStat)
   o << "host=" << serverStat.getHostname() << ", "
     << "protocol=" << serverStat.getProtocol() << ", "
     << "dl_speed=" << serverStat.getDownloadSpeed() << ", "
-    << "status=" << serverStat.getStatus() << "\n";
+    << "last_updated=" << serverStat.getLastUpdated().getTime() << ", "
+    << "status=" << ServerStat::STATUS_STRING[serverStat.getStatus()];
   return o;
 }
 

+ 4 - 2
src/ServerStat.h

@@ -48,9 +48,11 @@ namespace aria2 {
 class ServerStat {
 public:
   enum STATUS {
-    OK,
+    OK = 0,
     ERROR
   };
+  
+  static const std::string STATUS_STRING[];
 
   ServerStat(const std::string& hostname, const std::string& protocol);
 
@@ -60,7 +62,7 @@ public:
 
   const std::string& getProtocol() const;
 
-  Time getLastUpdated() const;
+  const Time& getLastUpdated() const;
 
   void setLastUpdated(const Time& time);
 

+ 4 - 7
src/ServerStatMan.cc

@@ -36,6 +36,7 @@
 #include "ServerStat.h"
 #include <algorithm>
 #include <ostream>
+#include <iterator>
 
 namespace aria2 {
 
@@ -70,14 +71,10 @@ bool ServerStatMan::add(const SharedHandle<ServerStat>& serverStat)
   } 
 }
 
-//bool save(const std::string& filepath) const;
-
-void ServerStatMan::print(std::ostream& o) const
+void ServerStatMan::save(std::ostream& out) const
 {
-  for(std::deque<SharedHandle<ServerStat> >::const_iterator i =
-	_serverStats.begin(); i != _serverStats.end(); ++i) {
-    o << *i;
-  }
+  std::copy(_serverStats.begin(), _serverStats.end(),
+	    std::ostream_iterator<SharedHandle<ServerStat> >(out, "\n"));
 }
 
 } // namespace aria2

+ 0 - 2
src/ServerStatMan.h

@@ -58,8 +58,6 @@ public:
   void load(std::istream& in);
 
   void save(std::ostream& out) const;
-
-  void print(std::ostream& o) const;
 private:
   std::deque<SharedHandle<ServerStat> > _serverStats;
 };

+ 33 - 0
test/ServerStatManTest.cc

@@ -3,6 +3,7 @@
 #include "Exception.h"
 #include "Util.h"
 #include <iostream>
+#include <sstream>
 #include <cppunit/extensions/HelperMacros.h>
 
 namespace aria2 {
@@ -11,6 +12,7 @@ class ServerStatManTest:public CppUnit::TestFixture {
 
   CPPUNIT_TEST_SUITE(ServerStatManTest);
   CPPUNIT_TEST(testAddAndFind);
+  CPPUNIT_TEST(testSave);
   CPPUNIT_TEST_SUITE_END();
 public:
   void setUp() {}
@@ -18,6 +20,8 @@ public:
   void tearDown() {}
 
   void testAddAndFind();
+
+  void testSave();
 };
 
 
@@ -47,4 +51,33 @@ void ServerStatManTest::testAddAndFind()
   }
 }
 
+void ServerStatManTest::testSave()
+{
+  SharedHandle<ServerStat> localhost_http(new ServerStat("localhost", "http"));
+  localhost_http->setDownloadSpeed(25000);
+  localhost_http->setLastUpdated(Time(1210000000));
+  SharedHandle<ServerStat> localhost_ftp(new ServerStat("localhost", "ftp"));
+  localhost_ftp->setDownloadSpeed(30000);
+  localhost_ftp->setLastUpdated(Time(1210000001));
+  SharedHandle<ServerStat> mirror(new ServerStat("mirror", "http"));
+  mirror->setDownloadSpeed(0);
+  mirror->setError();
+  mirror->setLastUpdated(Time(1210000002));
+
+  ServerStatMan ssm;
+  CPPUNIT_ASSERT(ssm.add(localhost_http));
+  CPPUNIT_ASSERT(ssm.add(localhost_ftp));
+  CPPUNIT_ASSERT(ssm.add(mirror));
+
+  std::stringstream ss;
+  ssm.save(ss);
+  std::string out = ss.str();
+  CPPUNIT_ASSERT_EQUAL
+    (std::string
+     ("host=localhost, protocol=ftp, dl_speed=30000, last_updated=1210000001, status=OK\n"
+      "host=localhost, protocol=http, dl_speed=25000, last_updated=1210000000, status=OK\n"
+      "host=mirror, protocol=http, dl_speed=0, last_updated=1210000002, status=ERROR\n"),
+     out);			   
+}
+
 } // namespace aria2