From 6aba376430b45979a420cfe8fd0f8d6a3025f581 Mon Sep 17 00:00:00 2001
From: Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>
Date: Sat, 9 Aug 2008 13:30:40 +0000
Subject: [PATCH] 2008-08-09  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot
 com>

	Implemented ServerStatMan::removeStaleServerStat() and its test
case.
	* src/ServerStatMan.cc
	* src/ServerStatMan.h
	* test/ServerStatManTest.cc
---
 ChangeLog                 |  7 +++++++
 src/ServerStatMan.cc      | 19 +++++++++++++++++++
 src/ServerStatMan.h       |  2 ++
 test/ServerStatManTest.cc | 30 +++++++++++++++++++++++++++++-
 4 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 610d1b48..25d26614 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2008-08-09  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
+
+	Implemented ServerStatMan::removeStaleServerStat() and its test case.
+	* src/ServerStatMan.cc
+	* src/ServerStatMan.h
+	* test/ServerStatManTest.cc
+
 2008-08-09  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
 
 	Implemented ServerStatMan::load(...) function and its test case.
diff --git a/src/ServerStatMan.cc b/src/ServerStatMan.cc
index cf059964..7f671fdf 100644
--- a/src/ServerStatMan.cc
+++ b/src/ServerStatMan.cc
@@ -119,4 +119,23 @@ void ServerStatMan::load(std::istream& in)
   }
 }
 
+class FindStaleServerStat {
+private:
+  time_t _timeout;
+public:
+  FindStaleServerStat(time_t timeout):_timeout(timeout) {}
+
+  bool operator()(const SharedHandle<ServerStat>& ss) const
+  {
+    return ss->getLastUpdated().elapsed(_timeout);
+  }
+};
+
+void ServerStatMan::removeStaleServerStat(time_t timeout)
+{
+  _serverStats.erase(std::remove_if(_serverStats.begin(), _serverStats.end(),
+				    FindStaleServerStat(timeout)),
+		     _serverStats.end());
+}
+
 } // namespace aria2
diff --git a/src/ServerStatMan.h b/src/ServerStatMan.h
index 6fe8f65e..0104b5e5 100644
--- a/src/ServerStatMan.h
+++ b/src/ServerStatMan.h
@@ -58,6 +58,8 @@ public:
   void load(std::istream& in);
 
   void save(std::ostream& out) const;
+
+  void removeStaleServerStat(time_t timeout);
 private:
   std::deque<SharedHandle<ServerStat> > _serverStats;
 };
diff --git a/test/ServerStatManTest.cc b/test/ServerStatManTest.cc
index c5c5aad3..63d46d02 100644
--- a/test/ServerStatManTest.cc
+++ b/test/ServerStatManTest.cc
@@ -14,6 +14,7 @@ class ServerStatManTest:public CppUnit::TestFixture {
   CPPUNIT_TEST(testAddAndFind);
   CPPUNIT_TEST(testSave);
   CPPUNIT_TEST(testLoad);
+  CPPUNIT_TEST(testRemoveStaleServerStat);
   CPPUNIT_TEST_SUITE_END();
 public:
   void setUp() {}
@@ -23,6 +24,7 @@ public:
   void testAddAndFind();
   void testSave();
   void testLoad();
+  void testRemoveStaleServerStat();
 };
 
 
@@ -62,7 +64,7 @@ void ServerStatManTest::testSave()
   localhost_ftp->setLastUpdated(Time(1210000001));
   SharedHandle<ServerStat> mirror(new ServerStat("mirror", "http"));
   mirror->setDownloadSpeed(0);
-  mirror->setError();
+  mirror->setStatus(ServerStat::ERROR);
   mirror->setLastUpdated(Time(1210000002));
 
   ServerStatMan ssm;
@@ -108,4 +110,30 @@ void ServerStatManTest::testLoad()
   CPPUNIT_ASSERT_EQUAL(ServerStat::ERROR, mirror->getStatus());
 }
 
+void ServerStatManTest::testRemoveStaleServerStat()
+{
+  Time now;
+  SharedHandle<ServerStat> localhost_http(new ServerStat("localhost", "http"));
+  localhost_http->setDownloadSpeed(25000);
+  localhost_http->setLastUpdated(now);
+  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->setStatus(ServerStat::ERROR);
+  mirror->setLastUpdated(Time(1210000002));
+
+  ServerStatMan ssm;
+  CPPUNIT_ASSERT(ssm.add(localhost_http));
+  CPPUNIT_ASSERT(ssm.add(localhost_ftp));
+  CPPUNIT_ASSERT(ssm.add(mirror));
+
+  ssm.removeStaleServerStat(24*60*60);
+
+  CPPUNIT_ASSERT(!ssm.find("localhost", "http").isNull());
+  CPPUNIT_ASSERT(ssm.find("localhost", "ftp").isNull());
+  CPPUNIT_ASSERT(ssm.find("mirror", "http").isNull());
+}
+
 } // namespace aria2