diff --git a/ChangeLog b/ChangeLog index 4aa78f2a..b26aa95a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2008-12-10 Tatsuhiro Tsujikawa + + Use BDE instead of Dictionary/List/Data. + * src/HandshakeExtensionMessage.cc + * src/HandshakeExtensionMessage.h + * test/HandshakeExtensionMessageTest.cc + 2008-12-10 Tatsuhiro Tsujikawa Added --max-overall-upload-limit option. This option limits the diff --git a/src/HandshakeExtensionMessage.cc b/src/HandshakeExtensionMessage.cc index 973fb088..6344d2ea 100644 --- a/src/HandshakeExtensionMessage.cc +++ b/src/HandshakeExtensionMessage.cc @@ -35,16 +35,13 @@ #include "HandshakeExtensionMessage.h" #include "Peer.h" #include "BtContext.h" -#include "Dictionary.h" -#include "Data.h" #include "Util.h" -#include "BencodeVisitor.h" -#include "MetaFileUtil.h" #include "DlAbortEx.h" #include "LogFactory.h" #include "Logger.h" #include "message.h" #include "StringFormat.h" +#include "bencode.h" namespace aria2 { @@ -58,27 +55,21 @@ HandshakeExtensionMessage::~HandshakeExtensionMessage() {} std::string HandshakeExtensionMessage::getBencodedData() { - SharedHandle dic(new Dictionary()); + bencode::BDE dict = bencode::BDE::dict(); if(!_clientVersion.empty()) { - Data* v = new Data(_clientVersion); - dic->put("v", v); + dict["v"] = _clientVersion; } if(_tcpPort > 0) { - std::string portStr = Util::uitos(_tcpPort); - Data* p = new Data(portStr, true); - dic->put("p", p); + dict["p"] = _tcpPort; } - Dictionary* exts = new Dictionary(); - dic->put("m", exts); + bencode::BDE extDict = bencode::BDE::dict(); for(std::map::const_iterator itr = _extensions.begin(); itr != _extensions.end(); ++itr) { const std::map::value_type& vt = *itr; - std::string idStr = Util::uitos(vt.second); - exts->put(vt.first, new Data(idStr, true)); + extDict[vt.first] = vt.second; } - BencodeVisitor v; - dic->accept(&v); - return v.getBencodedData(); + dict["m"] = extDict; + return bencode::encode(dict); } std::string HandshakeExtensionMessage::toString() const @@ -142,26 +133,24 @@ HandshakeExtensionMessage::create(const unsigned char* data, size_t length) HandshakeExtensionMessageHandle msg(new HandshakeExtensionMessage()); msg->_logger->debug("Creating HandshakeExtensionMessage from %s", Util::urlencode(data, length).c_str()); - SharedHandle root(MetaFileUtil::bdecoding(data+1, length-1)); - Dictionary* d = dynamic_cast(root.get()); - if(d == 0) { + const bencode::BDE dict = bencode::decode(data+1, length-1); + if(!dict.isDict()) { throw DlAbortEx("Unexpected payload format for extended message handshake"); } - const Data* p = dynamic_cast(d->get("p")); - if(p) { - msg->_tcpPort = p->toInt(); + const bencode::BDE& port = dict["p"]; + if(port.isInteger() && 0 < port.i() && port.i() < 65536) { + msg->_tcpPort = port.i(); } - const Data* v = dynamic_cast(d->get("v")); - if(v) { - msg->_clientVersion = v->toString(); + const bencode::BDE& version = dict["v"]; + if(version.isString()) { + msg->_clientVersion = version.s(); } - const Dictionary* m = dynamic_cast(d->get("m")); - if(m) { - const std::deque& order = m->getOrder(); - for(std::deque::const_iterator i = order.begin(); i != order.end(); ++i) { - const Data* e = dynamic_cast(m->get(*i)); - if(e) { - msg->_extensions[*i] = e->toInt(); + const bencode::BDE& extDict = dict["m"]; + if(extDict.isDict()) { + for(bencode::BDE::Dict::const_iterator i = extDict.dictBegin(); + i != extDict.dictEnd(); ++i) { + if((*i).second.isInteger()) { + msg->_extensions[(*i).first] = (*i).second.i(); } } } diff --git a/src/HandshakeExtensionMessage.h b/src/HandshakeExtensionMessage.h index ca33fc9e..dc054f52 100644 --- a/src/HandshakeExtensionMessage.h +++ b/src/HandshakeExtensionMessage.h @@ -36,9 +36,11 @@ #define _D_HANDSHAKE_EXTENSION_MESSAGE_H_ #include "ExtensionMessage.h" -#include "BtConstants.h" + #include +#include "BtConstants.h" + namespace aria2 { class BtContext; diff --git a/test/HandshakeExtensionMessageTest.cc b/test/HandshakeExtensionMessageTest.cc index 898ca8cb..eb97efeb 100644 --- a/test/HandshakeExtensionMessageTest.cc +++ b/test/HandshakeExtensionMessageTest.cc @@ -1,10 +1,13 @@ #include "HandshakeExtensionMessage.h" + +#include + +#include + #include "Peer.h" #include "MockBtContext.h" #include "Exception.h" #include "FileEntry.h" -#include -#include namespace aria2 { @@ -57,7 +60,11 @@ void HandshakeExtensionMessageTest::testGetBencodedData() msg.setTCPPort(6889); msg.setExtension("ut_pex", 1); msg.setExtension("a2_dht", 2); - CPPUNIT_ASSERT_EQUAL(std::string("d1:v5:aria21:pi6889e1:md6:a2_dhti2e6:ut_pexi1eee"), msg.getBencodedData()); + CPPUNIT_ASSERT_EQUAL(std::string("d" + "1:md6:a2_dhti2e6:ut_pexi1ee" + "1:pi6889e" + "1:v5:aria2" + "e"), msg.getBencodedData()); } void HandshakeExtensionMessageTest::testToString() @@ -132,11 +139,14 @@ void HandshakeExtensionMessageTest::testCreate_stringnum() { std::string in = "0d1:p4:68811:v5:aria21:md6:ut_pex1:1ee"; SharedHandle m = - HandshakeExtensionMessage::create(reinterpret_cast(in.c_str()), + HandshakeExtensionMessage::create + (reinterpret_cast(in.c_str()), in.size()); CPPUNIT_ASSERT_EQUAL(std::string("aria2"), m->getClientVersion()); - CPPUNIT_ASSERT_EQUAL((uint16_t)6881, m->getTCPPort()); - CPPUNIT_ASSERT_EQUAL((uint8_t)1, m->getExtensionMessageID("ut_pex")); + // port number in string is not allowed + CPPUNIT_ASSERT_EQUAL((uint16_t)0, m->getTCPPort()); + // extension ID in string is not allowed + CPPUNIT_ASSERT_EQUAL((uint8_t)0, m->getExtensionMessageID("ut_pex")); } } // namespace aria2