diff --git a/ChangeLog b/ChangeLog index d22c94a8..90238f5a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2008-08-09 Tatsuhiro Tsujikawa + + 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 Now aria2 uses name attribute in Metalink as local filename in diff --git a/src/ServerStat.cc b/src/ServerStat.cc index d1782c61..196b6866 100644 --- a/src/ServerStat.cc +++ b/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; } diff --git a/src/ServerStat.h b/src/ServerStat.h index 7f31ea93..e307be94 100644 --- a/src/ServerStat.h +++ b/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); diff --git a/src/ServerStatMan.cc b/src/ServerStatMan.cc index a4719029..bd02bc57 100644 --- a/src/ServerStatMan.cc +++ b/src/ServerStatMan.cc @@ -36,6 +36,7 @@ #include "ServerStat.h" #include #include +#include namespace aria2 { @@ -70,14 +71,10 @@ bool ServerStatMan::add(const SharedHandle& 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 >::const_iterator i = - _serverStats.begin(); i != _serverStats.end(); ++i) { - o << *i; - } + std::copy(_serverStats.begin(), _serverStats.end(), + std::ostream_iterator >(out, "\n")); } } // namespace aria2 diff --git a/src/ServerStatMan.h b/src/ServerStatMan.h index b2301ccc..6fe8f65e 100644 --- a/src/ServerStatMan.h +++ b/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 > _serverStats; }; diff --git a/test/ServerStatManTest.cc b/test/ServerStatManTest.cc index df6cafa8..ee50782b 100644 --- a/test/ServerStatManTest.cc +++ b/test/ServerStatManTest.cc @@ -3,6 +3,7 @@ #include "Exception.h" #include "Util.h" #include +#include #include 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 localhost_http(new ServerStat("localhost", "http")); + localhost_http->setDownloadSpeed(25000); + localhost_http->setLastUpdated(Time(1210000000)); + SharedHandle localhost_ftp(new ServerStat("localhost", "ftp")); + localhost_ftp->setDownloadSpeed(30000); + localhost_ftp->setLastUpdated(Time(1210000001)); + SharedHandle 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