Skip to content

Commit

Permalink
2008-08-09 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Browse files Browse the repository at this point in the history
	Implemented ServerStatMan::save(...) function and its test case.
	* src/ServerStat.cc
	* src/ServerStat.h
	* src/ServerStatMan.cc
	* src/ServerStatMan.h
	* test/ServerStatManTest.cc
  • Loading branch information
tatsuhiro-t committed Aug 9, 2008
1 parent 70b457d commit 26690f6
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 13 deletions.
9 changes: 9 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 18 additions & 2 deletions src/ServerStat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
6 changes: 4 additions & 2 deletions src/ServerStat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -60,7 +62,7 @@ class ServerStat {

const std::string& getProtocol() const;

Time getLastUpdated() const;
const Time& getLastUpdated() const;

void setLastUpdated(const Time& time);

Expand Down
11 changes: 4 additions & 7 deletions src/ServerStatMan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "ServerStat.h"
#include <algorithm>
#include <ostream>
#include <iterator>

namespace aria2 {

Expand Down Expand Up @@ -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
2 changes: 0 additions & 2 deletions src/ServerStatMan.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ class ServerStatMan {
void load(std::istream& in);

void save(std::ostream& out) const;

void print(std::ostream& o) const;
private:
std::deque<SharedHandle<ServerStat> > _serverStats;
};
Expand Down
33 changes: 33 additions & 0 deletions test/ServerStatManTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Exception.h"
#include "Util.h"
#include <iostream>
#include <sstream>
#include <cppunit/extensions/HelperMacros.h>

namespace aria2 {
Expand All @@ -11,13 +12,16 @@ class ServerStatManTest:public CppUnit::TestFixture {

CPPUNIT_TEST_SUITE(ServerStatManTest);
CPPUNIT_TEST(testAddAndFind);
CPPUNIT_TEST(testSave);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {}

void tearDown() {}

void testAddAndFind();

void testSave();
};


Expand Down Expand Up @@ -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

0 comments on commit 26690f6

Please sign in to comment.