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
pull/1/head
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>
Added --max-overall-upload-limit option. This option limits the

View File

@ -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<Dictionary> 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<std::string, uint8_t>::const_iterator itr = _extensions.begin();
itr != _extensions.end(); ++itr) {
const std::map<std::string, uint8_t>::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<MetaEntry> root(MetaFileUtil::bdecoding(data+1, length-1));
Dictionary* d = dynamic_cast<Dictionary*>(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<const Data*>(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<const Data*>(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<const Dictionary*>(d->get("m"));
if(m) {
const std::deque<std::string>& order = m->getOrder();
for(std::deque<std::string>::const_iterator i = order.begin(); i != order.end(); ++i) {
const Data* e = dynamic_cast<const Data*>(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();
}
}
}

View File

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

View File

@ -1,10 +1,13 @@
#include "HandshakeExtensionMessage.h"
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
#include "Peer.h"
#include "MockBtContext.h"
#include "Exception.h"
#include "FileEntry.h"
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
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<HandshakeExtensionMessage> m =
HandshakeExtensionMessage::create(reinterpret_cast<const unsigned char*>(in.c_str()),
HandshakeExtensionMessage::create
(reinterpret_cast<const unsigned char*>(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