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

Implemented ServerStatMan::load(...) function and its test case.
	* src/ServerStat.cc
	* src/ServerStat.h
	* src/ServerStatMan.cc
	* test/ServerStatManTest.cc
	* test/ServerStatTest.cc
pull/1/head
Tatsuhiro Tsujikawa 2008-08-09 11:29:27 +00:00
parent 8208e538ba
commit d85014b937
8 changed files with 195 additions and 9 deletions

View File

@ -1,3 +1,12 @@
2008-08-09 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Implemented ServerStatMan::load(...) function and its test case.
* src/ServerStat.cc
* src/ServerStat.h
* src/ServerStatMan.cc
* test/ServerStatManTest.cc
* test/ServerStatTest.cc
2008-08-09 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Use time_t instead of int32_t. Use int64_t where milli second is

View File

@ -33,7 +33,9 @@
*/
/* copyright --> */
#include "ServerStat.h"
#include "array_fun.h"
#include <ostream>
#include <algorithm>
namespace aria2 {
@ -91,6 +93,23 @@ void ServerStat::updateDownloadSpeed(unsigned int downloadSpeed)
}
void ServerStat::setStatus(STATUS status)
{
_status = status;
}
void ServerStat::setStatus(const std::string& status)
{
size_t len = arrayLength(STATUS_STRING);
const std::string* p = std::find(&STATUS_STRING[0],
&STATUS_STRING[len],
status);
if(p != &STATUS_STRING[len]) {
_status = static_cast<STATUS>(ServerStat::OK+
std::distance(&STATUS_STRING[0], p));
}
}
void ServerStat::setStatusInternal(STATUS status)
{
_status = status;
_lastUpdated.reset();
@ -108,7 +127,7 @@ bool ServerStat::isOK() const
void ServerStat::setOK()
{
setStatus(OK);
setStatusInternal(OK);
}
bool ServerStat::isError() const
@ -118,7 +137,7 @@ bool ServerStat::isError() const
void ServerStat::setError()
{
setStatus(ERROR);
setStatusInternal(ERROR);
}
bool ServerStat::operator<(const ServerStat& serverStat) const

View File

@ -64,6 +64,7 @@ public:
const Time& getLastUpdated() const;
// This method doesn't update _lastUpdate.
void setLastUpdated(const Time& time);
unsigned int getDownloadSpeed() const;
@ -74,8 +75,14 @@ public:
// set download speed. This method doesn't update _lastUpdate.
void setDownloadSpeed(unsigned int downloadSpeed);
// This method doesn't update _lastUpdate.
void setStatus(STATUS status);
// status should be one of the followings: "OK", "ERROR".
// Giving other string will not change the status of this object.
// This method doesn't update _lastUpdate.
void setStatus(const std::string& status);
STATUS getStatus() const;
bool isOK() const;
@ -101,6 +108,8 @@ private:
STATUS _status;
Time _lastUpdated;
void setStatusInternal(STATUS status);
};
std::ostream& operator<<(std::ostream& o, const ServerStat& serverStat);

View File

@ -34,9 +34,12 @@
/* copyright --> */
#include "ServerStatMan.h"
#include "ServerStat.h"
#include "Util.h"
#include "RecoverableException.h"
#include <algorithm>
#include <ostream>
#include <iterator>
#include <map>
namespace aria2 {
@ -77,4 +80,43 @@ void ServerStatMan::save(std::ostream& out) const
std::ostream_iterator<SharedHandle<ServerStat> >(out, "\n"));
}
void ServerStatMan::load(std::istream& in)
{
static const std::string S_HOST = "host";
static const std::string S_PROTOCOL = "protocol";
static const std::string S_DL_SPEED = "dl_speed";
static const std::string S_LAST_UPDATED = "last_updated";
static const std::string S_STATUS = "status";
std::string line;
while(getline(in, line)) {
Util::trimSelf(line);
if(line.empty()) {
continue;
}
std::deque<std::string> items;
Util::slice(items, line, ',');
std::map<std::string, std::string> m;
for(std::deque<std::string>::const_iterator i = items.begin();
i != items.end(); ++i) {
std::pair<std::string, std::string> p = Util::split(*i, "=");
Util::trimSelf(p.first);
Util::trimSelf(p.second);
m[p.first] = p.second;
}
if(m[S_HOST].empty() || m[S_PROTOCOL].empty()) {
continue;
}
SharedHandle<ServerStat> sstat(new ServerStat(m[S_HOST], m[S_PROTOCOL]));
try {
sstat->setDownloadSpeed(Util::parseUInt(m[S_DL_SPEED]));
sstat->setLastUpdated(Time(Util::parseInt(m[S_LAST_UPDATED])));
sstat->setStatus(m[S_STATUS]);
add(sstat);
} catch(RecoverableException* e) {
continue;
}
}
}
} // namespace aria2

View File

@ -56,7 +56,8 @@ aria2c_SOURCES = AllTest.cc\
SignatureTest.cc\
ServerStatManTest.cc\
ServerStatURISelectorTest.cc\
InOrderURISelectorTest.cc
InOrderURISelectorTest.cc\
ServerStatTest.cc
if HAVE_LIBZ
aria2c_SOURCES += GZipDecoderTest.cc

View File

@ -190,7 +190,8 @@ am__aria2c_SOURCES_DIST = AllTest.cc SocketCoreTest.cc \
DownloadHandlerFactoryTest.cc ChunkedDecoderTest.cc \
SignatureTest.cc ServerStatManTest.cc \
ServerStatURISelectorTest.cc InOrderURISelectorTest.cc \
GZipDecoderTest.cc MessageDigestHelperTest.cc \
ServerStatTest.cc GZipDecoderTest.cc \
MessageDigestHelperTest.cc \
IteratableChunkChecksumValidatorTest.cc \
IteratableChecksumValidatorTest.cc BtAllowedFastMessageTest.cc \
BtBitfieldMessageTest.cc BtCancelMessageTest.cc \
@ -357,8 +358,9 @@ am_aria2c_OBJECTS = AllTest.$(OBJEXT) SocketCoreTest.$(OBJEXT) \
ChunkedDecoderTest.$(OBJEXT) SignatureTest.$(OBJEXT) \
ServerStatManTest.$(OBJEXT) \
ServerStatURISelectorTest.$(OBJEXT) \
InOrderURISelectorTest.$(OBJEXT) $(am__objects_1) \
$(am__objects_2) $(am__objects_3) $(am__objects_4)
InOrderURISelectorTest.$(OBJEXT) ServerStatTest.$(OBJEXT) \
$(am__objects_1) $(am__objects_2) $(am__objects_3) \
$(am__objects_4)
aria2c_OBJECTS = $(am_aria2c_OBJECTS)
am__DEPENDENCIES_1 =
aria2c_DEPENDENCIES = ../src/libaria2c.a $(am__DEPENDENCIES_1)
@ -575,8 +577,8 @@ aria2c_SOURCES = AllTest.cc SocketCoreTest.cc array_funTest.cc \
DownloadHandlerFactoryTest.cc ChunkedDecoderTest.cc \
SignatureTest.cc ServerStatManTest.cc \
ServerStatURISelectorTest.cc InOrderURISelectorTest.cc \
$(am__append_1) $(am__append_2) $(am__append_3) \
$(am__append_4)
ServerStatTest.cc $(am__append_1) $(am__append_2) \
$(am__append_3) $(am__append_4)
#aria2c_CXXFLAGS = ${CPPUNIT_CFLAGS} -I../src -I../lib -Wall -D_FILE_OFFSET_BITS=64
#aria2c_LDFLAGS = ${CPPUNIT_LIBS}
@ -781,6 +783,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SegmentTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SequenceTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ServerStatManTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ServerStatTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ServerStatURISelectorTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ShareRatioSeedCriteriaTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SharedHandleTest.Po@am__quote@

View File

@ -13,6 +13,7 @@ class ServerStatManTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(ServerStatManTest);
CPPUNIT_TEST(testAddAndFind);
CPPUNIT_TEST(testSave);
CPPUNIT_TEST(testLoad);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {}
@ -20,8 +21,8 @@ public:
void tearDown() {}
void testAddAndFind();
void testSave();
void testLoad();
};
@ -80,4 +81,31 @@ void ServerStatManTest::testSave()
out);
}
void ServerStatManTest::testLoad()
{
std::string in =
"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";
std::stringstream ss(in);
ServerStatMan ssm;
ssm.load(ss);
SharedHandle<ServerStat> localhost_http = ssm.find("localhost", "http");
CPPUNIT_ASSERT(!localhost_http.isNull());
CPPUNIT_ASSERT_EQUAL(std::string("localhost"), localhost_http->getHostname());
CPPUNIT_ASSERT_EQUAL(std::string("http"), localhost_http->getProtocol());
CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(25000),
localhost_http->getDownloadSpeed());
CPPUNIT_ASSERT_EQUAL(static_cast<time_t>(1210000000),
localhost_http->getLastUpdated().getTime());
CPPUNIT_ASSERT_EQUAL(ServerStat::OK, localhost_http->getStatus());
SharedHandle<ServerStat> mirror = ssm.find("mirror", "http");
CPPUNIT_ASSERT(!mirror.isNull());
CPPUNIT_ASSERT_EQUAL(ServerStat::ERROR, mirror->getStatus());
}
} // namespace aria2

75
test/ServerStatTest.cc Normal file
View File

@ -0,0 +1,75 @@
#include "ServerStat.h"
#include "Exception.h"
#include "Util.h"
#include <iostream>
#include <sstream>
#include <cppunit/extensions/HelperMacros.h>
namespace aria2 {
class ServerStatTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(ServerStatTest);
CPPUNIT_TEST(testSetStatus);
CPPUNIT_TEST(testOperatorOstream);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {}
void tearDown() {}
void testSetStatus();
void testOperatorOstream();
};
CPPUNIT_TEST_SUITE_REGISTRATION(ServerStatTest);
void ServerStatTest::testSetStatus()
{
ServerStat ss("localhost", "http");
CPPUNIT_ASSERT_EQUAL(ServerStat::OK, ss.getStatus());
ss.setStatus("ERROR");
CPPUNIT_ASSERT_EQUAL(ServerStat::ERROR, ss.getStatus());
// See undefined status string will not change current status.
ss.setStatus("__BADSTATUS");
CPPUNIT_ASSERT_EQUAL(ServerStat::ERROR, ss.getStatus());
ss.setStatus("OK");
CPPUNIT_ASSERT_EQUAL(ServerStat::OK, ss.getStatus());
// See undefined status string will not change current status.
ss.setStatus("__BADSTATUS");
CPPUNIT_ASSERT_EQUAL(ServerStat::OK, ss.getStatus());
}
void ServerStatTest::testOperatorOstream()
{
ServerStat localhost_http("localhost", "http");
localhost_http.setDownloadSpeed(90000);
localhost_http.setLastUpdated(Time(1000));
std::stringstream ss;
ss << localhost_http;
CPPUNIT_ASSERT_EQUAL
(std::string
("host=localhost, protocol=http, dl_speed=90000, last_updated=1000, status=OK"),
ss.str());
ss.str("");
ServerStat localhost_ftp("localhost", "ftp");
localhost_ftp.setDownloadSpeed(10000);
localhost_ftp.setLastUpdated(Time(1210000000));
localhost_ftp.setStatus("ERROR");
ss << localhost_ftp;
CPPUNIT_ASSERT_EQUAL
(std::string
("host=localhost, protocol=ftp, dl_speed=10000, last_updated=1210000000, status=ERROR"),
ss.str());
}
} // namespace aria2