2008-12-10 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

Use BDE instead of Dictionary/List/Data.
	* src/HandshakeExtensionMessage.cc
	* src/HandshakeExtensionMessage.h
	* test/HandshakeExtensionMessageTest.cc
This commit is contained in:
Tatsuhiro Tsujikawa 2008-12-10 14:25:42 +00:00
parent 8c1894720f
commit a67ed743a2
4 changed files with 48 additions and 40 deletions

View file

@ -1,3 +1,10 @@
2008-12-10 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Use BDE instead of Dictionary/List/Data.
* src/HandshakeExtensionMessage.cc
* src/HandshakeExtensionMessage.h
* test/HandshakeExtensionMessageTest.cc
2008-12-10 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> 2008-12-10 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Added --max-overall-upload-limit option. This option limits the Added --max-overall-upload-limit option. This option limits the

View file

@ -35,16 +35,13 @@
#include "HandshakeExtensionMessage.h" #include "HandshakeExtensionMessage.h"
#include "Peer.h" #include "Peer.h"
#include "BtContext.h" #include "BtContext.h"
#include "Dictionary.h"
#include "Data.h"
#include "Util.h" #include "Util.h"
#include "BencodeVisitor.h"
#include "MetaFileUtil.h"
#include "DlAbortEx.h" #include "DlAbortEx.h"
#include "LogFactory.h" #include "LogFactory.h"
#include "Logger.h" #include "Logger.h"
#include "message.h" #include "message.h"
#include "StringFormat.h" #include "StringFormat.h"
#include "bencode.h"
namespace aria2 { namespace aria2 {
@ -58,27 +55,21 @@ HandshakeExtensionMessage::~HandshakeExtensionMessage() {}
std::string HandshakeExtensionMessage::getBencodedData() std::string HandshakeExtensionMessage::getBencodedData()
{ {
SharedHandle<Dictionary> dic(new Dictionary()); bencode::BDE dict = bencode::BDE::dict();
if(!_clientVersion.empty()) { if(!_clientVersion.empty()) {
Data* v = new Data(_clientVersion); dict["v"] = _clientVersion;
dic->put("v", v);
} }
if(_tcpPort > 0) { if(_tcpPort > 0) {
std::string portStr = Util::uitos(_tcpPort); dict["p"] = _tcpPort;
Data* p = new Data(portStr, true);
dic->put("p", p);
} }
Dictionary* exts = new Dictionary(); bencode::BDE extDict = bencode::BDE::dict();
dic->put("m", exts);
for(std::map<std::string, uint8_t>::const_iterator itr = _extensions.begin(); for(std::map<std::string, uint8_t>::const_iterator itr = _extensions.begin();
itr != _extensions.end(); ++itr) { itr != _extensions.end(); ++itr) {
const std::map<std::string, uint8_t>::value_type& vt = *itr; const std::map<std::string, uint8_t>::value_type& vt = *itr;
std::string idStr = Util::uitos(vt.second); extDict[vt.first] = vt.second;
exts->put(vt.first, new Data(idStr, true));
} }
BencodeVisitor v; dict["m"] = extDict;
dic->accept(&v); return bencode::encode(dict);
return v.getBencodedData();
} }
std::string HandshakeExtensionMessage::toString() const std::string HandshakeExtensionMessage::toString() const
@ -142,26 +133,24 @@ HandshakeExtensionMessage::create(const unsigned char* data, size_t length)
HandshakeExtensionMessageHandle msg(new HandshakeExtensionMessage()); HandshakeExtensionMessageHandle msg(new HandshakeExtensionMessage());
msg->_logger->debug("Creating HandshakeExtensionMessage from %s", msg->_logger->debug("Creating HandshakeExtensionMessage from %s",
Util::urlencode(data, length).c_str()); Util::urlencode(data, length).c_str());
SharedHandle<MetaEntry> root(MetaFileUtil::bdecoding(data+1, length-1)); const bencode::BDE dict = bencode::decode(data+1, length-1);
Dictionary* d = dynamic_cast<Dictionary*>(root.get()); if(!dict.isDict()) {
if(d == 0) {
throw DlAbortEx("Unexpected payload format for extended message handshake"); throw DlAbortEx("Unexpected payload format for extended message handshake");
} }
const Data* p = dynamic_cast<const Data*>(d->get("p")); const bencode::BDE& port = dict["p"];
if(p) { if(port.isInteger() && 0 < port.i() && port.i() < 65536) {
msg->_tcpPort = p->toInt(); msg->_tcpPort = port.i();
} }
const Data* v = dynamic_cast<const Data*>(d->get("v")); const bencode::BDE& version = dict["v"];
if(v) { if(version.isString()) {
msg->_clientVersion = v->toString(); msg->_clientVersion = version.s();
} }
const Dictionary* m = dynamic_cast<const Dictionary*>(d->get("m")); const bencode::BDE& extDict = dict["m"];
if(m) { if(extDict.isDict()) {
const std::deque<std::string>& order = m->getOrder(); for(bencode::BDE::Dict::const_iterator i = extDict.dictBegin();
for(std::deque<std::string>::const_iterator i = order.begin(); i != order.end(); ++i) { i != extDict.dictEnd(); ++i) {
const Data* e = dynamic_cast<const Data*>(m->get(*i)); if((*i).second.isInteger()) {
if(e) { msg->_extensions[(*i).first] = (*i).second.i();
msg->_extensions[*i] = e->toInt();
} }
} }
} }

View file

@ -36,9 +36,11 @@
#define _D_HANDSHAKE_EXTENSION_MESSAGE_H_ #define _D_HANDSHAKE_EXTENSION_MESSAGE_H_
#include "ExtensionMessage.h" #include "ExtensionMessage.h"
#include "BtConstants.h"
#include <map> #include <map>
#include "BtConstants.h"
namespace aria2 { namespace aria2 {
class BtContext; class BtContext;

View file

@ -1,10 +1,13 @@
#include "HandshakeExtensionMessage.h" #include "HandshakeExtensionMessage.h"
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
#include "Peer.h" #include "Peer.h"
#include "MockBtContext.h" #include "MockBtContext.h"
#include "Exception.h" #include "Exception.h"
#include "FileEntry.h" #include "FileEntry.h"
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
namespace aria2 { namespace aria2 {
@ -57,7 +60,11 @@ void HandshakeExtensionMessageTest::testGetBencodedData()
msg.setTCPPort(6889); msg.setTCPPort(6889);
msg.setExtension("ut_pex", 1); msg.setExtension("ut_pex", 1);
msg.setExtension("a2_dht", 2); 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() void HandshakeExtensionMessageTest::testToString()
@ -132,11 +139,14 @@ void HandshakeExtensionMessageTest::testCreate_stringnum()
{ {
std::string in = "0d1:p4:68811:v5:aria21:md6:ut_pex1:1ee"; std::string in = "0d1:p4:68811:v5:aria21:md6:ut_pex1:1ee";
SharedHandle<HandshakeExtensionMessage> m = SharedHandle<HandshakeExtensionMessage> m =
HandshakeExtensionMessage::create(reinterpret_cast<const unsigned char*>(in.c_str()), HandshakeExtensionMessage::create
(reinterpret_cast<const unsigned char*>(in.c_str()),
in.size()); in.size());
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), m->getClientVersion()); CPPUNIT_ASSERT_EQUAL(std::string("aria2"), m->getClientVersion());
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, m->getTCPPort()); // port number in string is not allowed
CPPUNIT_ASSERT_EQUAL((uint8_t)1, m->getExtensionMessageID("ut_pex")); 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 } // namespace aria2