mirror of https://github.com/aria2/aria2
2009-04-27 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Put BDE in aria2 namespace. BDE will also be used to store xml-rpc request. * src/AnnounceList.cc * src/AnnounceList.h * src/BDE.cc: Added * src/BDE.h: Added * src/DHTAbstractMessage.cc * src/DHTAbstractMessage.h * src/DHTAnnouncePeerMessage.cc * src/DHTAnnouncePeerMessage.h * src/DHTAnnouncePeerReplyMessage.cc * src/DHTAnnouncePeerReplyMessage.h * src/DHTFindNodeMessage.cc * src/DHTFindNodeMessage.h * src/DHTFindNodeReplyMessage.cc * src/DHTFindNodeReplyMessage.h * src/DHTGetPeersMessage.cc * src/DHTGetPeersMessage.h * src/DHTGetPeersReplyMessage.cc * src/DHTGetPeersReplyMessage.h * src/DHTMessageFactory.h * src/DHTMessageFactoryImpl.cc * src/DHTMessageFactoryImpl.h * src/DHTMessageReceiver.cc * src/DHTMessageTracker.cc * src/DHTMessageTracker.h * src/DHTPingMessage.cc * src/DHTPingMessage.h * src/DHTPingReplyMessage.cc * src/DHTPingReplyMessage.h * src/DHTQueryMessage.cc * src/DHTQueryMessage.h * src/DHTResponseMessage.cc * src/DHTResponseMessage.h * src/DefaultBtAnnounce.cc * src/DefaultBtContext.cc * src/DefaultBtContext.h * src/HandshakeExtensionMessage.cc * src/Makefile.am * src/PeerListProcessor.h * src/UTPexExtensionMessage.cc * src/bencode.cc * src/bencode.h * test/AnnounceListTest.cc * test/BDETest.cc: Added * test/BencodeTest.cc * test/DHTAnnouncePeerMessageTest.cc * test/DHTAnnouncePeerReplyMessageTest.cc * test/DHTFindNodeMessageTest.cc * test/DHTFindNodeReplyMessageTest.cc * test/DHTGetPeersMessageTest.cc * test/DHTGetPeersReplyMessageTest.cc * test/DHTMessageFactoryImplTest.cc * test/DHTMessageTrackerTest.cc * test/DHTPingMessageTest.cc * test/DHTPingReplyMessageTest.cc * test/Makefile.am * test/MockDHTMessageFactory.h * test/PeerListProcessorTest.ccpull/1/head
parent
d58742ad21
commit
49b4cfbfcd
62
ChangeLog
62
ChangeLog
|
@ -1,3 +1,65 @@
|
||||||
|
2009-04-27 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
|
Put BDE in aria2 namespace. BDE will also be used to store xml-rpc
|
||||||
|
request.
|
||||||
|
* src/AnnounceList.cc
|
||||||
|
* src/AnnounceList.h
|
||||||
|
* src/BDE.cc: Added
|
||||||
|
* src/BDE.h: Added
|
||||||
|
* src/DHTAbstractMessage.cc
|
||||||
|
* src/DHTAbstractMessage.h
|
||||||
|
* src/DHTAnnouncePeerMessage.cc
|
||||||
|
* src/DHTAnnouncePeerMessage.h
|
||||||
|
* src/DHTAnnouncePeerReplyMessage.cc
|
||||||
|
* src/DHTAnnouncePeerReplyMessage.h
|
||||||
|
* src/DHTFindNodeMessage.cc
|
||||||
|
* src/DHTFindNodeMessage.h
|
||||||
|
* src/DHTFindNodeReplyMessage.cc
|
||||||
|
* src/DHTFindNodeReplyMessage.h
|
||||||
|
* src/DHTGetPeersMessage.cc
|
||||||
|
* src/DHTGetPeersMessage.h
|
||||||
|
* src/DHTGetPeersReplyMessage.cc
|
||||||
|
* src/DHTGetPeersReplyMessage.h
|
||||||
|
* src/DHTMessageFactory.h
|
||||||
|
* src/DHTMessageFactoryImpl.cc
|
||||||
|
* src/DHTMessageFactoryImpl.h
|
||||||
|
* src/DHTMessageReceiver.cc
|
||||||
|
* src/DHTMessageTracker.cc
|
||||||
|
* src/DHTMessageTracker.h
|
||||||
|
* src/DHTPingMessage.cc
|
||||||
|
* src/DHTPingMessage.h
|
||||||
|
* src/DHTPingReplyMessage.cc
|
||||||
|
* src/DHTPingReplyMessage.h
|
||||||
|
* src/DHTQueryMessage.cc
|
||||||
|
* src/DHTQueryMessage.h
|
||||||
|
* src/DHTResponseMessage.cc
|
||||||
|
* src/DHTResponseMessage.h
|
||||||
|
* src/DefaultBtAnnounce.cc
|
||||||
|
* src/DefaultBtContext.cc
|
||||||
|
* src/DefaultBtContext.h
|
||||||
|
* src/HandshakeExtensionMessage.cc
|
||||||
|
* src/Makefile.am
|
||||||
|
* src/PeerListProcessor.h
|
||||||
|
* src/UTPexExtensionMessage.cc
|
||||||
|
* src/bencode.cc
|
||||||
|
* src/bencode.h
|
||||||
|
* test/AnnounceListTest.cc
|
||||||
|
* test/BDETest.cc: Added
|
||||||
|
* test/BencodeTest.cc
|
||||||
|
* test/DHTAnnouncePeerMessageTest.cc
|
||||||
|
* test/DHTAnnouncePeerReplyMessageTest.cc
|
||||||
|
* test/DHTFindNodeMessageTest.cc
|
||||||
|
* test/DHTFindNodeReplyMessageTest.cc
|
||||||
|
* test/DHTGetPeersMessageTest.cc
|
||||||
|
* test/DHTGetPeersReplyMessageTest.cc
|
||||||
|
* test/DHTMessageFactoryImplTest.cc
|
||||||
|
* test/DHTMessageTrackerTest.cc
|
||||||
|
* test/DHTPingMessageTest.cc
|
||||||
|
* test/DHTPingReplyMessageTest.cc
|
||||||
|
* test/Makefile.am
|
||||||
|
* test/MockDHTMessageFactory.h
|
||||||
|
* test/PeerListProcessorTest.cc
|
||||||
|
|
||||||
2009-04-27 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
2009-04-27 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
Use url-list (web-seeding) only for single-file torrent. This is
|
Use url-list (web-seeding) only for single-file torrent. This is
|
||||||
|
|
|
@ -48,7 +48,7 @@ const std::string AnnounceList::STOPPED("stopped");
|
||||||
|
|
||||||
const std::string AnnounceList::COMPLETED("completed");
|
const std::string AnnounceList::COMPLETED("completed");
|
||||||
|
|
||||||
AnnounceList::AnnounceList(const bencode::BDE& announceList):
|
AnnounceList::AnnounceList(const BDE& announceList):
|
||||||
currentTrackerInitialized(false) {
|
currentTrackerInitialized(false) {
|
||||||
reconfigure(announceList);
|
reconfigure(announceList);
|
||||||
}
|
}
|
||||||
|
@ -58,19 +58,19 @@ AnnounceList::AnnounceList(const AnnounceTiers& announceTiers):
|
||||||
resetIterator();
|
resetIterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
void AnnounceList::reconfigure(const bencode::BDE& announceList)
|
void AnnounceList::reconfigure(const BDE& announceList)
|
||||||
{
|
{
|
||||||
if(announceList.isList()) {
|
if(announceList.isList()) {
|
||||||
for(bencode::BDE::List::const_iterator itr = announceList.listBegin();
|
for(BDE::List::const_iterator itr = announceList.listBegin();
|
||||||
itr != announceList.listEnd(); ++itr) {
|
itr != announceList.listEnd(); ++itr) {
|
||||||
const bencode::BDE& elemList = *itr;
|
const BDE& elemList = *itr;
|
||||||
if(!elemList.isList()) {
|
if(!elemList.isList()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::deque<std::string> urls;
|
std::deque<std::string> urls;
|
||||||
for(bencode::BDE::List::const_iterator elemItr = elemList.listBegin();
|
for(BDE::List::const_iterator elemItr = elemList.listBegin();
|
||||||
elemItr != elemList.listEnd(); ++elemItr) {
|
elemItr != elemList.listEnd(); ++elemItr) {
|
||||||
const bencode::BDE& data = *elemItr;
|
const BDE& data = *elemItr;
|
||||||
if(data.isString()) {
|
if(data.isString()) {
|
||||||
urls.push_back(data.s());
|
urls.push_back(data.s());
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,9 +41,7 @@
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
namespace bencode {
|
|
||||||
class BDE;
|
class BDE;
|
||||||
} // namespace bencode
|
|
||||||
|
|
||||||
class AnnounceList {
|
class AnnounceList {
|
||||||
public:
|
public:
|
||||||
|
@ -57,10 +55,10 @@ private:
|
||||||
void setCurrentTier(const std::deque<SharedHandle<AnnounceTier> >::iterator& itr);
|
void setCurrentTier(const std::deque<SharedHandle<AnnounceTier> >::iterator& itr);
|
||||||
public:
|
public:
|
||||||
AnnounceList():currentTrackerInitialized(false) {}
|
AnnounceList():currentTrackerInitialized(false) {}
|
||||||
AnnounceList(const bencode::BDE& announceList);
|
AnnounceList(const BDE& announceList);
|
||||||
AnnounceList(const std::deque<SharedHandle<AnnounceTier> >& tiers);
|
AnnounceList(const std::deque<SharedHandle<AnnounceTier> >& tiers);
|
||||||
|
|
||||||
void reconfigure(const bencode::BDE& announceList);
|
void reconfigure(const BDE& announceList);
|
||||||
void reconfigure(const std::string& url);
|
void reconfigure(const std::string& url);
|
||||||
|
|
||||||
size_t countTier() const {
|
size_t countTier() const {
|
||||||
|
|
|
@ -0,0 +1,223 @@
|
||||||
|
/* <!-- copyright */
|
||||||
|
/*
|
||||||
|
* aria2 - The high speed download utility
|
||||||
|
*
|
||||||
|
* Copyright (C) 2009 Tatsuhiro Tsujikawa
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
* In addition, as a special exception, the copyright holders give
|
||||||
|
* permission to link the code of portions of this program with the
|
||||||
|
* OpenSSL library under certain conditions as described in each
|
||||||
|
* individual source file, and distribute linked combinations
|
||||||
|
* including the two.
|
||||||
|
* You must obey the GNU General Public License in all respects
|
||||||
|
* for all of the code used other than OpenSSL. If you modify
|
||||||
|
* file(s) with this exception, you may extend this exception to your
|
||||||
|
* version of the file(s), but you are not obligated to do so. If you
|
||||||
|
* do not wish to do so, delete this exception statement from your
|
||||||
|
* version. If you delete this exception statement from all source
|
||||||
|
* files in the program, then also delete it here.
|
||||||
|
*/
|
||||||
|
/* copyright --> */
|
||||||
|
|
||||||
|
#include "BDE.h"
|
||||||
|
|
||||||
|
namespace aria2 {
|
||||||
|
|
||||||
|
const BDE BDE::none;
|
||||||
|
|
||||||
|
BDE::BDE():_type(TYPE_NONE) {}
|
||||||
|
|
||||||
|
BDE::BDE(Integer integer):_type(TYPE_INTEGER),
|
||||||
|
_bobject(new BInteger(integer)) {}
|
||||||
|
|
||||||
|
|
||||||
|
BDE::BDE(const std::string& string):_type(TYPE_STRING),
|
||||||
|
_bobject(new BString(std::string(string))) {}
|
||||||
|
|
||||||
|
BDE::BDE(const char* cstring):_type(TYPE_STRING),
|
||||||
|
_bobject(new BString(std::string(cstring))) {}
|
||||||
|
|
||||||
|
BDE::BDE(const char* data, size_t length):
|
||||||
|
_type(TYPE_STRING),
|
||||||
|
_bobject(new BString(std::string(&data[0], &data[length]))) {}
|
||||||
|
|
||||||
|
BDE::BDE(const unsigned char* data, size_t length):
|
||||||
|
_type(TYPE_STRING),
|
||||||
|
_bobject(new BString(std::string(&data[0], &data[length]))) {}
|
||||||
|
|
||||||
|
BDE BDE::dict()
|
||||||
|
{
|
||||||
|
BDE bde;
|
||||||
|
bde._type = TYPE_DICT;
|
||||||
|
bde._bobject.reset(new BDict());
|
||||||
|
return bde;
|
||||||
|
}
|
||||||
|
|
||||||
|
BDE BDE::list()
|
||||||
|
{
|
||||||
|
BDE bde;
|
||||||
|
bde._type = TYPE_LIST;
|
||||||
|
bde._bobject.reset(new BList());
|
||||||
|
return bde;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test for Null data
|
||||||
|
bool BDE::isNone() const
|
||||||
|
{
|
||||||
|
return _type == TYPE_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Integer Interface
|
||||||
|
|
||||||
|
bool BDE::isInteger() const
|
||||||
|
{
|
||||||
|
return _type == TYPE_INTEGER;
|
||||||
|
}
|
||||||
|
|
||||||
|
BDE::Integer BDE::i() const
|
||||||
|
{
|
||||||
|
return _bobject->i();
|
||||||
|
}
|
||||||
|
|
||||||
|
// String Interface
|
||||||
|
|
||||||
|
bool BDE::isString() const
|
||||||
|
{
|
||||||
|
return _type == TYPE_STRING;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string& BDE::s() const
|
||||||
|
{
|
||||||
|
return _bobject->s();
|
||||||
|
}
|
||||||
|
|
||||||
|
const unsigned char* BDE::uc() const
|
||||||
|
{
|
||||||
|
return _bobject->uc();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dictionary Interface
|
||||||
|
|
||||||
|
bool BDE::isDict() const
|
||||||
|
{
|
||||||
|
return _type == TYPE_DICT;
|
||||||
|
}
|
||||||
|
|
||||||
|
BDE& BDE::operator[](const std::string& key)
|
||||||
|
{
|
||||||
|
return _bobject->operator[](key);
|
||||||
|
}
|
||||||
|
|
||||||
|
const BDE& BDE::operator[](const std::string& key) const
|
||||||
|
{
|
||||||
|
if(_bobject->containsKey(key)) {
|
||||||
|
return _bobject->operator[](key);
|
||||||
|
} else {
|
||||||
|
return none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BDE::containsKey(const std::string& key) const
|
||||||
|
{
|
||||||
|
return _bobject->containsKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BDE::removeKey(const std::string& key)
|
||||||
|
{
|
||||||
|
_bobject->removeKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
BDE::Dict::iterator BDE::dictBegin()
|
||||||
|
{
|
||||||
|
return _bobject->dictBegin();
|
||||||
|
}
|
||||||
|
|
||||||
|
BDE::Dict::const_iterator BDE::dictBegin() const
|
||||||
|
{
|
||||||
|
return _bobject->dictBegin();
|
||||||
|
}
|
||||||
|
|
||||||
|
BDE::Dict::iterator BDE::dictEnd()
|
||||||
|
{
|
||||||
|
return _bobject->dictEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
BDE::Dict::const_iterator BDE::dictEnd() const
|
||||||
|
{
|
||||||
|
return _bobject->dictEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
// List Interface
|
||||||
|
|
||||||
|
bool BDE::isList() const
|
||||||
|
{
|
||||||
|
return _type == TYPE_LIST;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BDE::append(const BDE& bde)
|
||||||
|
{
|
||||||
|
_bobject->append(bde);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BDE::operator<<(const BDE& bde)
|
||||||
|
{
|
||||||
|
_bobject->operator<<(bde);
|
||||||
|
}
|
||||||
|
|
||||||
|
BDE& BDE::operator[](size_t index)
|
||||||
|
{
|
||||||
|
return _bobject->operator[](index);
|
||||||
|
}
|
||||||
|
|
||||||
|
const BDE& BDE::operator[](size_t index) const
|
||||||
|
{
|
||||||
|
return _bobject->operator[](index);
|
||||||
|
}
|
||||||
|
|
||||||
|
BDE::List::iterator BDE::listBegin()
|
||||||
|
{
|
||||||
|
return _bobject->listBegin();
|
||||||
|
}
|
||||||
|
|
||||||
|
BDE::List::const_iterator BDE::listBegin() const
|
||||||
|
{
|
||||||
|
return _bobject->listBegin();
|
||||||
|
}
|
||||||
|
|
||||||
|
BDE::List::iterator BDE::listEnd()
|
||||||
|
{
|
||||||
|
return _bobject->listEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
BDE::List::const_iterator BDE::listEnd() const
|
||||||
|
{
|
||||||
|
return _bobject->listEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Callable from List and Dict
|
||||||
|
size_t BDE::size() const
|
||||||
|
{
|
||||||
|
return _bobject->size();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Callable from List and Dict
|
||||||
|
bool BDE::empty() const
|
||||||
|
{
|
||||||
|
return _bobject->empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace aria2
|
|
@ -0,0 +1,431 @@
|
||||||
|
/* <!-- copyright */
|
||||||
|
/*
|
||||||
|
* aria2 - The high speed download utility
|
||||||
|
*
|
||||||
|
* Copyright (C) 2009 Tatsuhiro Tsujikawa
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
* In addition, as a special exception, the copyright holders give
|
||||||
|
* permission to link the code of portions of this program with the
|
||||||
|
* OpenSSL library under certain conditions as described in each
|
||||||
|
* individual source file, and distribute linked combinations
|
||||||
|
* including the two.
|
||||||
|
* You must obey the GNU General Public License in all respects
|
||||||
|
* for all of the code used other than OpenSSL. If you modify
|
||||||
|
* file(s) with this exception, you may extend this exception to your
|
||||||
|
* version of the file(s), but you are not obligated to do so. If you
|
||||||
|
* do not wish to do so, delete this exception statement from your
|
||||||
|
* version. If you delete this exception statement from all source
|
||||||
|
* files in the program, then also delete it here.
|
||||||
|
*/
|
||||||
|
/* copyright --> */
|
||||||
|
#ifndef _D_BDE_H_
|
||||||
|
#define _D_BDE_H_
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
#include <deque>
|
||||||
|
|
||||||
|
#include "SharedHandle.h"
|
||||||
|
#include "RecoverableException.h"
|
||||||
|
|
||||||
|
namespace aria2 {
|
||||||
|
|
||||||
|
class BDE;
|
||||||
|
|
||||||
|
class BDE {
|
||||||
|
public:
|
||||||
|
typedef std::map<std::string, BDE> Dict;
|
||||||
|
typedef std::deque<BDE> List;
|
||||||
|
typedef int64_t Integer;
|
||||||
|
private:
|
||||||
|
enum TYPE{
|
||||||
|
TYPE_NONE,
|
||||||
|
TYPE_INTEGER,
|
||||||
|
TYPE_STRING,
|
||||||
|
TYPE_DICT,
|
||||||
|
TYPE_LIST,
|
||||||
|
};
|
||||||
|
|
||||||
|
class BObject {
|
||||||
|
public:
|
||||||
|
virtual ~BObject() {}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Integer Interface
|
||||||
|
|
||||||
|
// Returns Integer.
|
||||||
|
virtual Integer i() const
|
||||||
|
{
|
||||||
|
throw RecoverableException("Not Integer");
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////
|
||||||
|
// String Interface
|
||||||
|
|
||||||
|
// Returns std::string.
|
||||||
|
virtual const std::string& s() const
|
||||||
|
{
|
||||||
|
throw RecoverableException("Not String");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns std::string.data() casted to unsigned char*.
|
||||||
|
// Use s().size() to get length.
|
||||||
|
virtual const unsigned char* uc() const
|
||||||
|
{
|
||||||
|
throw RecoverableException("Not String");
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Dictionary Interface
|
||||||
|
|
||||||
|
// Returns the reference to BDE object associated with given key.
|
||||||
|
// If the key is not found, new pair with that key is created
|
||||||
|
// using default values, which is then returned. In other words,
|
||||||
|
// this is the same behavior of std::map's operator[].
|
||||||
|
virtual BDE& operator[](const std::string& key)
|
||||||
|
{
|
||||||
|
throw RecoverableException("Not Dict");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns true if the given key is found in dict.
|
||||||
|
virtual bool containsKey(const std::string& key) const
|
||||||
|
{
|
||||||
|
throw RecoverableException("Not Dict");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Removes specified key from dict.
|
||||||
|
virtual void removeKey(const std::string& key)
|
||||||
|
{
|
||||||
|
throw RecoverableException("Not Dict");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns a read/write iterator that points to the first pair in
|
||||||
|
// the dict.
|
||||||
|
virtual Dict::iterator dictBegin()
|
||||||
|
{
|
||||||
|
throw RecoverableException("Not Dict");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns a read/write read-only iterator that points to one past
|
||||||
|
// the last pair in the dict.
|
||||||
|
virtual Dict::iterator dictEnd()
|
||||||
|
{
|
||||||
|
throw RecoverableException("Not Dict");
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////
|
||||||
|
// List Interface
|
||||||
|
|
||||||
|
// Appends given bde to list.
|
||||||
|
virtual void append(const BDE& bde)
|
||||||
|
{
|
||||||
|
throw RecoverableException("Not List");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Alias for append()
|
||||||
|
virtual void operator<<(const BDE& bde)
|
||||||
|
{
|
||||||
|
throw RecoverableException("Not List");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the reference of the object at the given index.
|
||||||
|
virtual BDE& operator[](size_t index)
|
||||||
|
{
|
||||||
|
throw RecoverableException("Not List");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns a read/write iterator that points to the first object
|
||||||
|
// in list.
|
||||||
|
virtual List::iterator listBegin()
|
||||||
|
{
|
||||||
|
throw RecoverableException("Not List");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns a read/write iterator that points to the one past the
|
||||||
|
// last object in list.
|
||||||
|
virtual List::iterator listEnd()
|
||||||
|
{
|
||||||
|
throw RecoverableException("Not List");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns size of list or dict.
|
||||||
|
virtual size_t size() const
|
||||||
|
{
|
||||||
|
throw RecoverableException("Neither Dict nor List");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns true if size of list or dict is 0.
|
||||||
|
virtual bool empty() const
|
||||||
|
{
|
||||||
|
throw RecoverableException("Neither Dict nor List");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class BInteger : public BObject {
|
||||||
|
private:
|
||||||
|
Integer _integer;
|
||||||
|
public:
|
||||||
|
BInteger(Integer i):_integer(i) {}
|
||||||
|
|
||||||
|
virtual BDE::Integer i() const
|
||||||
|
{
|
||||||
|
return _integer;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class BString : public BObject {
|
||||||
|
private:
|
||||||
|
std::string _string;
|
||||||
|
public:
|
||||||
|
BString(const std::string& string):_string(string) {}
|
||||||
|
|
||||||
|
virtual const std::string& s() const
|
||||||
|
{
|
||||||
|
return _string;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual const unsigned char* uc() const
|
||||||
|
{
|
||||||
|
return reinterpret_cast<const unsigned char*>(_string.data());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class BDict : public BObject {
|
||||||
|
private:
|
||||||
|
Dict _dict;
|
||||||
|
public:
|
||||||
|
|
||||||
|
virtual BDE& operator[](const std::string& key)
|
||||||
|
{
|
||||||
|
return _dict[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual bool containsKey(const std::string& key) const
|
||||||
|
{
|
||||||
|
return _dict.find(key) != _dict.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void removeKey(const std::string& key)
|
||||||
|
{
|
||||||
|
_dict.erase(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual BDE::Dict::iterator dictBegin()
|
||||||
|
{
|
||||||
|
return _dict.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual BDE::Dict::iterator dictEnd()
|
||||||
|
{
|
||||||
|
return _dict.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual size_t size() const
|
||||||
|
{
|
||||||
|
return _dict.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual bool empty() const
|
||||||
|
{
|
||||||
|
return _dict.empty();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class BList : public BObject {
|
||||||
|
private:
|
||||||
|
List _list;
|
||||||
|
public:
|
||||||
|
virtual void append(const BDE& bde)
|
||||||
|
{
|
||||||
|
_list.push_back(bde);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void operator<<(const BDE& bde)
|
||||||
|
{
|
||||||
|
_list.push_back(bde);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual BDE& operator[](size_t index)
|
||||||
|
{
|
||||||
|
return _list[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual BDE::List::iterator listBegin()
|
||||||
|
{
|
||||||
|
return _list.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual BDE::List::iterator listEnd()
|
||||||
|
{
|
||||||
|
return _list.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual size_t size() const
|
||||||
|
{
|
||||||
|
return _list.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual bool empty() const
|
||||||
|
{
|
||||||
|
return _list.empty();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
TYPE _type;
|
||||||
|
|
||||||
|
SharedHandle<BObject> _bobject;
|
||||||
|
public:
|
||||||
|
BDE();
|
||||||
|
|
||||||
|
static BDE dict();
|
||||||
|
|
||||||
|
static BDE list();
|
||||||
|
|
||||||
|
static const BDE none;
|
||||||
|
|
||||||
|
// Test for Null data
|
||||||
|
// Return true if the type of this object is None.
|
||||||
|
bool isNone() const;
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Integer Interface
|
||||||
|
|
||||||
|
BDE(Integer integer);
|
||||||
|
|
||||||
|
// Returns true if the type of this object is Integer.
|
||||||
|
bool isInteger() const;
|
||||||
|
|
||||||
|
// Returns Integer. Requires this object to be Integer.
|
||||||
|
Integer i() const;
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
// String Interface
|
||||||
|
|
||||||
|
BDE(const std::string& string);
|
||||||
|
|
||||||
|
// Made explicit to avoid ambiguity with BDE(Integer).
|
||||||
|
explicit BDE(const char* cstring);
|
||||||
|
|
||||||
|
BDE(const char* data, size_t length);
|
||||||
|
|
||||||
|
BDE(const unsigned char* data, size_t length);
|
||||||
|
|
||||||
|
// Returns true if the type of this object is String.
|
||||||
|
bool isString() const;
|
||||||
|
|
||||||
|
// Returns std::string. Requires this object to be String
|
||||||
|
const std::string& s() const;
|
||||||
|
|
||||||
|
// Returns std::string.data() casted to unsigned char*.
|
||||||
|
// Use s().size() to get length.
|
||||||
|
const unsigned char* uc() const;
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Dictionary Interface
|
||||||
|
|
||||||
|
// Returns true if the type of this object is Dict.
|
||||||
|
bool isDict() const;
|
||||||
|
|
||||||
|
// Returns the reference to BDE object associated with given key.
|
||||||
|
// If the key is not found, new pair with that key is created using default
|
||||||
|
// values, which is then returned. In other words, this is the same behavior
|
||||||
|
// of std::map's operator[].
|
||||||
|
// Requires this object to be Dict.
|
||||||
|
BDE& operator[](const std::string& key);
|
||||||
|
|
||||||
|
// Returns the const reference to BDE ojbect associated with given key.
|
||||||
|
// If the key is not found, BDE::none is returned.
|
||||||
|
// Requires this object to be Dict.
|
||||||
|
const BDE& operator[](const std::string& key) const;
|
||||||
|
|
||||||
|
// Returns true if the given key is found in dict.
|
||||||
|
// Requires this object to be Dict.
|
||||||
|
bool containsKey(const std::string& key) const;
|
||||||
|
|
||||||
|
// Removes specified key from dict.
|
||||||
|
// Requires this object to be Dict.
|
||||||
|
void removeKey(const std::string& key);
|
||||||
|
|
||||||
|
// Returns a read/write iterator that points to the first pair in the dict.
|
||||||
|
// Requires this object to be Dict.
|
||||||
|
Dict::iterator dictBegin();
|
||||||
|
|
||||||
|
// Returns a read/write read-only iterator that points to the first pair in
|
||||||
|
// the dict.
|
||||||
|
// Requires this object to be Dict.
|
||||||
|
Dict::const_iterator dictBegin() const;
|
||||||
|
|
||||||
|
// Returns a read/write read-only iterator that points to one past the last
|
||||||
|
// pair in the dict.
|
||||||
|
// Requires this object to be Dict.
|
||||||
|
Dict::iterator dictEnd();
|
||||||
|
|
||||||
|
// Returns a read/write read-only iterator that points to one past the last
|
||||||
|
// pair in the dict.
|
||||||
|
// Requires this object to be Dict.
|
||||||
|
Dict::const_iterator dictEnd() const;
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
// List Interface
|
||||||
|
|
||||||
|
// Returns true if the type of this object is List.
|
||||||
|
bool isList() const;
|
||||||
|
|
||||||
|
// Appends given bde to list. Required the type of this object to be List.
|
||||||
|
void append(const BDE& bde);
|
||||||
|
|
||||||
|
// Alias for append()
|
||||||
|
void operator<<(const BDE& bde);
|
||||||
|
|
||||||
|
// Returns the reference of the object at the given index. Required this
|
||||||
|
// object to be List.
|
||||||
|
BDE& operator[](size_t index);
|
||||||
|
|
||||||
|
// Returns the const reference of the object at the given index.
|
||||||
|
// Required this object to be List.
|
||||||
|
const BDE& operator[](size_t index) const;
|
||||||
|
|
||||||
|
// Returns a read/write iterator that points to the first object in list.
|
||||||
|
// Required this object to be List.
|
||||||
|
List::iterator listBegin();
|
||||||
|
|
||||||
|
// Returns a read/write read-only iterator that points to the first object
|
||||||
|
// in list. Required this object to be List.
|
||||||
|
List::const_iterator listBegin() const;
|
||||||
|
|
||||||
|
// Returns a read/write iterator that points to the one past the last object
|
||||||
|
// in list. Required this object to be List.
|
||||||
|
List::iterator listEnd();
|
||||||
|
|
||||||
|
// Returns a read/write read-only iterator that points to the one past the
|
||||||
|
// last object in list. Required this object to be List.
|
||||||
|
List::const_iterator listEnd() const;
|
||||||
|
|
||||||
|
// For List type: Returns size of list.
|
||||||
|
// For Dict type: Returns size of dict.
|
||||||
|
size_t size() const;
|
||||||
|
|
||||||
|
// For List type: Returns true if size of list is 0.
|
||||||
|
// For Dict type: Returns true if size of dict is 0.
|
||||||
|
bool empty() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace aria2
|
||||||
|
|
||||||
|
#endif // _D_BDE_H_
|
|
@ -55,7 +55,7 @@ DHTAbstractMessage::~DHTAbstractMessage() {}
|
||||||
|
|
||||||
std::string DHTAbstractMessage::getBencodedMessage()
|
std::string DHTAbstractMessage::getBencodedMessage()
|
||||||
{
|
{
|
||||||
bencode::BDE msgDict = bencode::BDE::dict();
|
BDE msgDict = BDE::dict();
|
||||||
msgDict[T] = _transactionID;
|
msgDict[T] = _transactionID;
|
||||||
msgDict[Y] = getType();
|
msgDict[Y] = getType();
|
||||||
fillMessage(msgDict);
|
fillMessage(msgDict);
|
||||||
|
|
|
@ -40,9 +40,7 @@
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
namespace bencode {
|
|
||||||
class BDE;
|
class BDE;
|
||||||
} // namespace bencode
|
|
||||||
class DHTConnection;
|
class DHTConnection;
|
||||||
class DHTMessageDispatcher;
|
class DHTMessageDispatcher;
|
||||||
class DHTMessageFactory;
|
class DHTMessageFactory;
|
||||||
|
@ -68,7 +66,7 @@ public:
|
||||||
|
|
||||||
virtual std::string getType() const = 0;
|
virtual std::string getType() const = 0;
|
||||||
|
|
||||||
virtual void fillMessage(bencode::BDE& msgDict) = 0;
|
virtual void fillMessage(BDE& msgDict) = 0;
|
||||||
|
|
||||||
std::string getBencodedMessage();
|
std::string getBencodedMessage();
|
||||||
|
|
||||||
|
|
|
@ -84,11 +84,11 @@ void DHTAnnouncePeerMessage::doReceivedAction()
|
||||||
_dispatcher->addMessageToQueue(reply);
|
_dispatcher->addMessageToQueue(reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
bencode::BDE DHTAnnouncePeerMessage::getArgument()
|
BDE DHTAnnouncePeerMessage::getArgument()
|
||||||
{
|
{
|
||||||
bencode::BDE aDict = bencode::BDE::dict();
|
BDE aDict = BDE::dict();
|
||||||
aDict[DHTMessage::ID] = bencode::BDE(_localNode->getID(), DHT_ID_LENGTH);
|
aDict[DHTMessage::ID] = BDE(_localNode->getID(), DHT_ID_LENGTH);
|
||||||
aDict[INFO_HASH] = bencode::BDE(_infoHash, DHT_ID_LENGTH);
|
aDict[INFO_HASH] = BDE(_infoHash, DHT_ID_LENGTH);
|
||||||
aDict[PORT] = _tcpPort;
|
aDict[PORT] = _tcpPort;
|
||||||
aDict[TOKEN] = _token;
|
aDict[TOKEN] = _token;
|
||||||
return aDict;
|
return aDict;
|
||||||
|
|
|
@ -69,7 +69,7 @@ public:
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual bencode::BDE getArgument();
|
virtual BDE getArgument();
|
||||||
|
|
||||||
virtual std::string getMessageType() const;
|
virtual std::string getMessageType() const;
|
||||||
|
|
||||||
|
|
|
@ -49,10 +49,10 @@ DHTAnnouncePeerReplyMessage::~DHTAnnouncePeerReplyMessage() {}
|
||||||
|
|
||||||
void DHTAnnouncePeerReplyMessage::doReceivedAction() {}
|
void DHTAnnouncePeerReplyMessage::doReceivedAction() {}
|
||||||
|
|
||||||
bencode::BDE DHTAnnouncePeerReplyMessage::getResponse()
|
BDE DHTAnnouncePeerReplyMessage::getResponse()
|
||||||
{
|
{
|
||||||
bencode::BDE rDict = bencode::BDE::dict();
|
BDE rDict = BDE::dict();
|
||||||
rDict[DHTMessage::ID] = bencode::BDE(_localNode->getID(), DHT_ID_LENGTH);
|
rDict[DHTMessage::ID] = BDE(_localNode->getID(), DHT_ID_LENGTH);
|
||||||
return rDict;
|
return rDict;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ public:
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual bencode::BDE getResponse();
|
virtual BDE getResponse();
|
||||||
|
|
||||||
virtual std::string getMessageType() const;
|
virtual std::string getMessageType() const;
|
||||||
|
|
||||||
|
|
|
@ -70,11 +70,11 @@ void DHTFindNodeMessage::doReceivedAction()
|
||||||
_dispatcher->addMessageToQueue(reply);
|
_dispatcher->addMessageToQueue(reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
bencode::BDE DHTFindNodeMessage::getArgument()
|
BDE DHTFindNodeMessage::getArgument()
|
||||||
{
|
{
|
||||||
bencode::BDE aDict = bencode::BDE::dict();
|
BDE aDict = BDE::dict();
|
||||||
aDict[DHTMessage::ID] = bencode::BDE(_localNode->getID(), DHT_ID_LENGTH);
|
aDict[DHTMessage::ID] = BDE(_localNode->getID(), DHT_ID_LENGTH);
|
||||||
aDict[TARGET_NODE] = bencode::BDE(_targetNodeID, DHT_ID_LENGTH);
|
aDict[TARGET_NODE] = BDE(_targetNodeID, DHT_ID_LENGTH);
|
||||||
return aDict;
|
return aDict;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -55,7 +55,7 @@ public:
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual bencode::BDE getArgument();
|
virtual BDE getArgument();
|
||||||
|
|
||||||
virtual std::string getMessageType() const;
|
virtual std::string getMessageType() const;
|
||||||
|
|
||||||
|
|
|
@ -68,10 +68,10 @@ void DHTFindNodeReplyMessage::doReceivedAction()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bencode::BDE DHTFindNodeReplyMessage::getResponse()
|
BDE DHTFindNodeReplyMessage::getResponse()
|
||||||
{
|
{
|
||||||
bencode::BDE aDict = bencode::BDE::dict();
|
BDE aDict = BDE::dict();
|
||||||
aDict[DHTMessage::ID] = bencode::BDE(_localNode->getID(), DHT_ID_LENGTH);
|
aDict[DHTMessage::ID] = BDE(_localNode->getID(), DHT_ID_LENGTH);
|
||||||
size_t offset = 0;
|
size_t offset = 0;
|
||||||
unsigned char buffer[DHTBucket::K*26];
|
unsigned char buffer[DHTBucket::K*26];
|
||||||
// TODO if _closestKNodes.size() > DHTBucket::K ??
|
// TODO if _closestKNodes.size() > DHTBucket::K ??
|
||||||
|
@ -85,7 +85,7 @@ bencode::BDE DHTFindNodeReplyMessage::getResponse()
|
||||||
offset += 26;
|
offset += 26;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
aDict[NODES] = bencode::BDE(buffer, offset);
|
aDict[NODES] = BDE(buffer, offset);
|
||||||
return aDict;
|
return aDict;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -54,7 +54,7 @@ public:
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual bencode::BDE getResponse();
|
virtual BDE getResponse();
|
||||||
|
|
||||||
virtual std::string getMessageType() const;
|
virtual std::string getMessageType() const;
|
||||||
|
|
||||||
|
|
|
@ -88,11 +88,11 @@ void DHTGetPeersMessage::doReceivedAction()
|
||||||
_dispatcher->addMessageToQueue(reply);
|
_dispatcher->addMessageToQueue(reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
bencode::BDE DHTGetPeersMessage::getArgument()
|
BDE DHTGetPeersMessage::getArgument()
|
||||||
{
|
{
|
||||||
bencode::BDE aDict = bencode::BDE::dict();
|
BDE aDict = BDE::dict();
|
||||||
aDict[DHTMessage::ID] = bencode::BDE(_localNode->getID(), DHT_ID_LENGTH);
|
aDict[DHTMessage::ID] = BDE(_localNode->getID(), DHT_ID_LENGTH);
|
||||||
aDict[INFO_HASH] = bencode::BDE(_infoHash, DHT_ID_LENGTH);
|
aDict[INFO_HASH] = BDE(_infoHash, DHT_ID_LENGTH);
|
||||||
return aDict;
|
return aDict;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,7 +63,7 @@ public:
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual bencode::BDE getArgument();
|
virtual BDE getArgument();
|
||||||
|
|
||||||
virtual std::string getMessageType() const;
|
virtual std::string getMessageType() const;
|
||||||
|
|
||||||
|
|
|
@ -72,10 +72,10 @@ void DHTGetPeersReplyMessage::doReceivedAction()
|
||||||
// Returned peers and nodes are handled in DHTPeerLookupTask.
|
// Returned peers and nodes are handled in DHTPeerLookupTask.
|
||||||
}
|
}
|
||||||
|
|
||||||
bencode::BDE DHTGetPeersReplyMessage::getResponse()
|
BDE DHTGetPeersReplyMessage::getResponse()
|
||||||
{
|
{
|
||||||
bencode::BDE rDict = bencode::BDE::dict();
|
BDE rDict = BDE::dict();
|
||||||
rDict[DHTMessage::ID] = bencode::BDE(_localNode->getID(), DHT_ID_LENGTH);
|
rDict[DHTMessage::ID] = BDE(_localNode->getID(), DHT_ID_LENGTH);
|
||||||
rDict[TOKEN] = _token;
|
rDict[TOKEN] = _token;
|
||||||
if(_values.empty()) {
|
if(_values.empty()) {
|
||||||
size_t offset = 0;
|
size_t offset = 0;
|
||||||
|
@ -89,15 +89,15 @@ bencode::BDE DHTGetPeersReplyMessage::getResponse()
|
||||||
offset += 26;
|
offset += 26;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rDict[NODES] = bencode::BDE(buffer, offset);
|
rDict[NODES] = BDE(buffer, offset);
|
||||||
} else {
|
} else {
|
||||||
bencode::BDE valuesList = bencode::BDE::list();
|
BDE valuesList = BDE::list();
|
||||||
for(std::deque<SharedHandle<Peer> >::const_iterator i = _values.begin();
|
for(std::deque<SharedHandle<Peer> >::const_iterator i = _values.begin();
|
||||||
i != _values.end(); ++i) {
|
i != _values.end(); ++i) {
|
||||||
const SharedHandle<Peer>& peer = *i;
|
const SharedHandle<Peer>& peer = *i;
|
||||||
unsigned char buffer[6];
|
unsigned char buffer[6];
|
||||||
if(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port)) {
|
if(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port)) {
|
||||||
valuesList << bencode::BDE(buffer, sizeof(buffer));
|
valuesList << BDE(buffer, sizeof(buffer));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rDict[VALUES] = valuesList;
|
rDict[VALUES] = valuesList;
|
||||||
|
|
|
@ -64,7 +64,7 @@ public:
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual bencode::BDE getResponse();
|
virtual BDE getResponse();
|
||||||
|
|
||||||
virtual std::string getMessageType() const;
|
virtual std::string getMessageType() const;
|
||||||
|
|
||||||
|
|
|
@ -48,21 +48,19 @@ namespace aria2 {
|
||||||
class DHTMessage;
|
class DHTMessage;
|
||||||
class DHTNode;
|
class DHTNode;
|
||||||
class Peer;
|
class Peer;
|
||||||
namespace bencode {
|
|
||||||
class BDE;
|
class BDE;
|
||||||
} // namespace bencode
|
|
||||||
|
|
||||||
class DHTMessageFactory {
|
class DHTMessageFactory {
|
||||||
public:
|
public:
|
||||||
virtual ~DHTMessageFactory() {}
|
virtual ~DHTMessageFactory() {}
|
||||||
|
|
||||||
virtual SharedHandle<DHTMessage>
|
virtual SharedHandle<DHTMessage>
|
||||||
createQueryMessage(const bencode::BDE& dict,
|
createQueryMessage(const BDE& dict,
|
||||||
const std::string& ipaddr, uint16_t port) = 0;
|
const std::string& ipaddr, uint16_t port) = 0;
|
||||||
|
|
||||||
virtual SharedHandle<DHTMessage>
|
virtual SharedHandle<DHTMessage>
|
||||||
createResponseMessage(const std::string& messageType,
|
createResponseMessage(const std::string& messageType,
|
||||||
const bencode::BDE& dict,
|
const BDE& dict,
|
||||||
const std::string& ipaddr, uint16_t port) = 0;
|
const std::string& ipaddr, uint16_t port) = 0;
|
||||||
|
|
||||||
virtual SharedHandle<DHTMessage>
|
virtual SharedHandle<DHTMessage>
|
||||||
|
|
|
@ -82,10 +82,10 @@ DHTMessageFactoryImpl::getRemoteNode(const unsigned char* id, const std::string&
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const bencode::BDE& getDictionary(const bencode::BDE& dict,
|
static const BDE& getDictionary(const BDE& dict,
|
||||||
const std::string& key)
|
const std::string& key)
|
||||||
{
|
{
|
||||||
const bencode::BDE& d = dict[key];
|
const BDE& d = dict[key];
|
||||||
if(d.isDict()) {
|
if(d.isDict()) {
|
||||||
return d;
|
return d;
|
||||||
} else {
|
} else {
|
||||||
|
@ -94,10 +94,10 @@ static const bencode::BDE& getDictionary(const bencode::BDE& dict,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static const bencode::BDE& getString(const bencode::BDE& dict,
|
static const BDE& getString(const BDE& dict,
|
||||||
const std::string& key)
|
const std::string& key)
|
||||||
{
|
{
|
||||||
const bencode::BDE& c = dict[key];
|
const BDE& c = dict[key];
|
||||||
if(c.isString()) {
|
if(c.isString()) {
|
||||||
return c;
|
return c;
|
||||||
} else {
|
} else {
|
||||||
|
@ -106,10 +106,10 @@ static const bencode::BDE& getString(const bencode::BDE& dict,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static const bencode::BDE& getInteger(const bencode::BDE& dict,
|
static const BDE& getInteger(const BDE& dict,
|
||||||
const std::string& key)
|
const std::string& key)
|
||||||
{
|
{
|
||||||
const bencode::BDE& c = dict[key];
|
const BDE& c = dict[key];
|
||||||
if(c.isInteger()) {
|
if(c.isInteger()) {
|
||||||
return c;
|
return c;
|
||||||
} else {
|
} else {
|
||||||
|
@ -118,9 +118,9 @@ static const bencode::BDE& getInteger(const bencode::BDE& dict,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static const bencode::BDE& getString(const bencode::BDE& list, size_t index)
|
static const BDE& getString(const BDE& list, size_t index)
|
||||||
{
|
{
|
||||||
const bencode::BDE& c = list[index];
|
const BDE& c = list[index];
|
||||||
if(c.isString()) {
|
if(c.isString()) {
|
||||||
return c;
|
return c;
|
||||||
} else {
|
} else {
|
||||||
|
@ -130,9 +130,9 @@ static const bencode::BDE& getString(const bencode::BDE& list, size_t index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static const bencode::BDE& getInteger(const bencode::BDE& list, size_t index)
|
static const BDE& getInteger(const BDE& list, size_t index)
|
||||||
{
|
{
|
||||||
const bencode::BDE& c = list[index];
|
const BDE& c = list[index];
|
||||||
if(c.isInteger()) {
|
if(c.isInteger()) {
|
||||||
return c;
|
return c;
|
||||||
} else {
|
} else {
|
||||||
|
@ -142,10 +142,10 @@ static const bencode::BDE& getInteger(const bencode::BDE& list, size_t index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static const bencode::BDE& getList(const bencode::BDE& dict,
|
static const BDE& getList(const BDE& dict,
|
||||||
const std::string& key)
|
const std::string& key)
|
||||||
{
|
{
|
||||||
const bencode::BDE& l = dict[key];
|
const BDE& l = dict[key];
|
||||||
if(l.isList()) {
|
if(l.isList()) {
|
||||||
return l;
|
return l;
|
||||||
} else {
|
} else {
|
||||||
|
@ -154,7 +154,7 @@ static const bencode::BDE& getList(const bencode::BDE& dict,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DHTMessageFactoryImpl::validateID(const bencode::BDE& id) const
|
void DHTMessageFactoryImpl::validateID(const BDE& id) const
|
||||||
{
|
{
|
||||||
if(id.s().size() != DHT_ID_LENGTH) {
|
if(id.s().size() != DHT_ID_LENGTH) {
|
||||||
throw DlAbortEx
|
throw DlAbortEx
|
||||||
|
@ -164,9 +164,9 @@ void DHTMessageFactoryImpl::validateID(const bencode::BDE& id) const
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DHTMessageFactoryImpl::validatePort(const bencode::BDE& i) const
|
void DHTMessageFactoryImpl::validatePort(const BDE& i) const
|
||||||
{
|
{
|
||||||
bencode::BDE::Integer port = i.i();
|
BDE::Integer port = i.i();
|
||||||
if(!(0 < port && port < UINT16_MAX)) {
|
if(!(0 < port && port < UINT16_MAX)) {
|
||||||
throw DlAbortEx
|
throw DlAbortEx
|
||||||
(StringFormat("Malformed DHT message. Invalid port=%s",
|
(StringFormat("Malformed DHT message. Invalid port=%s",
|
||||||
|
@ -175,41 +175,41 @@ void DHTMessageFactoryImpl::validatePort(const bencode::BDE& i) const
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedHandle<DHTMessage> DHTMessageFactoryImpl::createQueryMessage
|
SharedHandle<DHTMessage> DHTMessageFactoryImpl::createQueryMessage
|
||||||
(const bencode::BDE& dict,
|
(const BDE& dict,
|
||||||
const std::string& ipaddr,
|
const std::string& ipaddr,
|
||||||
uint16_t port)
|
uint16_t port)
|
||||||
{
|
{
|
||||||
const bencode::BDE& messageType = getString(dict, DHTQueryMessage::Q);
|
const BDE& messageType = getString(dict, DHTQueryMessage::Q);
|
||||||
const bencode::BDE& transactionID = getString(dict, DHTMessage::T);
|
const BDE& transactionID = getString(dict, DHTMessage::T);
|
||||||
const bencode::BDE& y = getString(dict, DHTMessage::Y);
|
const BDE& y = getString(dict, DHTMessage::Y);
|
||||||
const bencode::BDE& aDict = getDictionary(dict, DHTQueryMessage::A);
|
const BDE& aDict = getDictionary(dict, DHTQueryMessage::A);
|
||||||
if(y.s() != DHTQueryMessage::Q) {
|
if(y.s() != DHTQueryMessage::Q) {
|
||||||
throw DlAbortEx("Malformed DHT message. y != q");
|
throw DlAbortEx("Malformed DHT message. y != q");
|
||||||
}
|
}
|
||||||
const bencode::BDE& id = getString(aDict, DHTMessage::ID);
|
const BDE& id = getString(aDict, DHTMessage::ID);
|
||||||
validateID(id);
|
validateID(id);
|
||||||
SharedHandle<DHTNode> remoteNode = getRemoteNode(id.uc(), ipaddr, port);
|
SharedHandle<DHTNode> remoteNode = getRemoteNode(id.uc(), ipaddr, port);
|
||||||
if(messageType.s() == DHTPingMessage::PING) {
|
if(messageType.s() == DHTPingMessage::PING) {
|
||||||
return createPingMessage(remoteNode, transactionID.s());
|
return createPingMessage(remoteNode, transactionID.s());
|
||||||
} else if(messageType.s() == DHTFindNodeMessage::FIND_NODE) {
|
} else if(messageType.s() == DHTFindNodeMessage::FIND_NODE) {
|
||||||
const bencode::BDE& targetNodeID =
|
const BDE& targetNodeID =
|
||||||
getString(aDict, DHTFindNodeMessage::TARGET_NODE);
|
getString(aDict, DHTFindNodeMessage::TARGET_NODE);
|
||||||
validateID(targetNodeID);
|
validateID(targetNodeID);
|
||||||
return createFindNodeMessage(remoteNode, targetNodeID.uc(),
|
return createFindNodeMessage(remoteNode, targetNodeID.uc(),
|
||||||
transactionID.s());
|
transactionID.s());
|
||||||
} else if(messageType.s() == DHTGetPeersMessage::GET_PEERS) {
|
} else if(messageType.s() == DHTGetPeersMessage::GET_PEERS) {
|
||||||
const bencode::BDE& infoHash =
|
const BDE& infoHash =
|
||||||
getString(aDict, DHTGetPeersMessage::INFO_HASH);
|
getString(aDict, DHTGetPeersMessage::INFO_HASH);
|
||||||
validateID(infoHash);
|
validateID(infoHash);
|
||||||
return createGetPeersMessage(remoteNode,
|
return createGetPeersMessage(remoteNode,
|
||||||
infoHash.uc(), transactionID.s());
|
infoHash.uc(), transactionID.s());
|
||||||
} else if(messageType.s() == DHTAnnouncePeerMessage::ANNOUNCE_PEER) {
|
} else if(messageType.s() == DHTAnnouncePeerMessage::ANNOUNCE_PEER) {
|
||||||
const bencode::BDE& infoHash =
|
const BDE& infoHash =
|
||||||
getString(aDict, DHTAnnouncePeerMessage::INFO_HASH);
|
getString(aDict, DHTAnnouncePeerMessage::INFO_HASH);
|
||||||
validateID(infoHash);
|
validateID(infoHash);
|
||||||
const bencode::BDE& port = getInteger(aDict, DHTAnnouncePeerMessage::PORT);
|
const BDE& port = getInteger(aDict, DHTAnnouncePeerMessage::PORT);
|
||||||
validatePort(port);
|
validatePort(port);
|
||||||
const bencode::BDE& token = getString(aDict, DHTAnnouncePeerMessage::TOKEN);
|
const BDE& token = getString(aDict, DHTAnnouncePeerMessage::TOKEN);
|
||||||
return createAnnouncePeerMessage(remoteNode, infoHash.uc(),
|
return createAnnouncePeerMessage(remoteNode, infoHash.uc(),
|
||||||
static_cast<uint16_t>(port.i()),
|
static_cast<uint16_t>(port.i()),
|
||||||
token.s(), transactionID.s());
|
token.s(), transactionID.s());
|
||||||
|
@ -222,15 +222,15 @@ SharedHandle<DHTMessage> DHTMessageFactoryImpl::createQueryMessage
|
||||||
|
|
||||||
SharedHandle<DHTMessage>
|
SharedHandle<DHTMessage>
|
||||||
DHTMessageFactoryImpl::createResponseMessage(const std::string& messageType,
|
DHTMessageFactoryImpl::createResponseMessage(const std::string& messageType,
|
||||||
const bencode::BDE& dict,
|
const BDE& dict,
|
||||||
const std::string& ipaddr,
|
const std::string& ipaddr,
|
||||||
uint16_t port)
|
uint16_t port)
|
||||||
{
|
{
|
||||||
const bencode::BDE& transactionID = getString(dict, DHTMessage::T);
|
const BDE& transactionID = getString(dict, DHTMessage::T);
|
||||||
const bencode::BDE& y = getString(dict, DHTMessage::Y);
|
const BDE& y = getString(dict, DHTMessage::Y);
|
||||||
if(y.s() == DHTUnknownMessage::E) {
|
if(y.s() == DHTUnknownMessage::E) {
|
||||||
// for now, just report error message arrived and throw exception.
|
// for now, just report error message arrived and throw exception.
|
||||||
const bencode::BDE& e = getList(dict, DHTUnknownMessage::E);
|
const BDE& e = getList(dict, DHTUnknownMessage::E);
|
||||||
if(e.size() == 2) {
|
if(e.size() == 2) {
|
||||||
_logger->info("Received Error DHT message. code=%s, msg=%s",
|
_logger->info("Received Error DHT message. code=%s, msg=%s",
|
||||||
Util::itos(getInteger(e, 0).i()).c_str(),
|
Util::itos(getInteger(e, 0).i()).c_str(),
|
||||||
|
@ -244,8 +244,8 @@ DHTMessageFactoryImpl::createResponseMessage(const std::string& messageType,
|
||||||
(StringFormat("Malformed DHT message. y != r: y=%s",
|
(StringFormat("Malformed DHT message. y != r: y=%s",
|
||||||
Util::urlencode(y.s()).c_str()).str());
|
Util::urlencode(y.s()).c_str()).str());
|
||||||
}
|
}
|
||||||
const bencode::BDE& rDict = getDictionary(dict, DHTResponseMessage::R);
|
const BDE& rDict = getDictionary(dict, DHTResponseMessage::R);
|
||||||
const bencode::BDE& id = getString(rDict, DHTMessage::ID);
|
const BDE& id = getString(rDict, DHTMessage::ID);
|
||||||
validateID(id);
|
validateID(id);
|
||||||
SharedHandle<DHTNode> remoteNode = getRemoteNode(id.uc(), ipaddr, port);
|
SharedHandle<DHTNode> remoteNode = getRemoteNode(id.uc(), ipaddr, port);
|
||||||
|
|
||||||
|
@ -254,12 +254,12 @@ DHTMessageFactoryImpl::createResponseMessage(const std::string& messageType,
|
||||||
} else if(messageType == DHTFindNodeReplyMessage::FIND_NODE) {
|
} else if(messageType == DHTFindNodeReplyMessage::FIND_NODE) {
|
||||||
return createFindNodeReplyMessage(remoteNode, dict, transactionID.s());
|
return createFindNodeReplyMessage(remoteNode, dict, transactionID.s());
|
||||||
} else if(messageType == DHTGetPeersReplyMessage::GET_PEERS) {
|
} else if(messageType == DHTGetPeersReplyMessage::GET_PEERS) {
|
||||||
const bencode::BDE& valuesList = rDict[DHTGetPeersReplyMessage::VALUES];
|
const BDE& valuesList = rDict[DHTGetPeersReplyMessage::VALUES];
|
||||||
if(valuesList.isList()) {
|
if(valuesList.isList()) {
|
||||||
return createGetPeersReplyMessageWithValues(remoteNode, dict,
|
return createGetPeersReplyMessageWithValues(remoteNode, dict,
|
||||||
transactionID.s());
|
transactionID.s());
|
||||||
} else {
|
} else {
|
||||||
const bencode::BDE& nodes = rDict[DHTGetPeersReplyMessage::NODES];
|
const BDE& nodes = rDict[DHTGetPeersReplyMessage::NODES];
|
||||||
if(nodes.isString()) {
|
if(nodes.isString()) {
|
||||||
return createGetPeersReplyMessageWithNodes(remoteNode, dict,
|
return createGetPeersReplyMessageWithNodes(remoteNode, dict,
|
||||||
transactionID.s());
|
transactionID.s());
|
||||||
|
@ -346,10 +346,10 @@ DHTMessageFactoryImpl::extractNodes(const unsigned char* src, size_t length)
|
||||||
SharedHandle<DHTMessage>
|
SharedHandle<DHTMessage>
|
||||||
DHTMessageFactoryImpl::createFindNodeReplyMessage
|
DHTMessageFactoryImpl::createFindNodeReplyMessage
|
||||||
(const SharedHandle<DHTNode>& remoteNode,
|
(const SharedHandle<DHTNode>& remoteNode,
|
||||||
const bencode::BDE& dict,
|
const BDE& dict,
|
||||||
const std::string& transactionID)
|
const std::string& transactionID)
|
||||||
{
|
{
|
||||||
const bencode::BDE& nodesData =
|
const BDE& nodesData =
|
||||||
getString(getDictionary(dict, DHTResponseMessage::R),
|
getString(getDictionary(dict, DHTResponseMessage::R),
|
||||||
DHTFindNodeReplyMessage::NODES);
|
DHTFindNodeReplyMessage::NODES);
|
||||||
std::deque<SharedHandle<DHTNode> > nodes = extractNodes(nodesData.uc(),
|
std::deque<SharedHandle<DHTNode> > nodes = extractNodes(nodesData.uc(),
|
||||||
|
@ -375,15 +375,15 @@ DHTMessageFactoryImpl::createGetPeersMessage(const SharedHandle<DHTNode>& remote
|
||||||
SharedHandle<DHTMessage>
|
SharedHandle<DHTMessage>
|
||||||
DHTMessageFactoryImpl::createGetPeersReplyMessageWithNodes
|
DHTMessageFactoryImpl::createGetPeersReplyMessageWithNodes
|
||||||
(const SharedHandle<DHTNode>& remoteNode,
|
(const SharedHandle<DHTNode>& remoteNode,
|
||||||
const bencode::BDE& dict,
|
const BDE& dict,
|
||||||
const std::string& transactionID)
|
const std::string& transactionID)
|
||||||
{
|
{
|
||||||
const bencode::BDE& rDict = getDictionary(dict, DHTResponseMessage::R);
|
const BDE& rDict = getDictionary(dict, DHTResponseMessage::R);
|
||||||
const bencode::BDE& nodesData = getString(rDict,
|
const BDE& nodesData = getString(rDict,
|
||||||
DHTGetPeersReplyMessage::NODES);
|
DHTGetPeersReplyMessage::NODES);
|
||||||
std::deque<SharedHandle<DHTNode> > nodes = extractNodes(nodesData.uc(),
|
std::deque<SharedHandle<DHTNode> > nodes = extractNodes(nodesData.uc(),
|
||||||
nodesData.s().size());
|
nodesData.s().size());
|
||||||
const bencode::BDE& token = getString(rDict, DHTGetPeersReplyMessage::TOKEN);
|
const BDE& token = getString(rDict, DHTGetPeersReplyMessage::TOKEN);
|
||||||
return createGetPeersReplyMessage(remoteNode, nodes, token.s(),
|
return createGetPeersReplyMessage(remoteNode, nodes, token.s(),
|
||||||
transactionID);
|
transactionID);
|
||||||
}
|
}
|
||||||
|
@ -404,16 +404,16 @@ DHTMessageFactoryImpl::createGetPeersReplyMessage(const SharedHandle<DHTNode>& r
|
||||||
SharedHandle<DHTMessage>
|
SharedHandle<DHTMessage>
|
||||||
DHTMessageFactoryImpl::createGetPeersReplyMessageWithValues
|
DHTMessageFactoryImpl::createGetPeersReplyMessageWithValues
|
||||||
(const SharedHandle<DHTNode>& remoteNode,
|
(const SharedHandle<DHTNode>& remoteNode,
|
||||||
const bencode::BDE& dict,
|
const BDE& dict,
|
||||||
const std::string& transactionID)
|
const std::string& transactionID)
|
||||||
{
|
{
|
||||||
const bencode::BDE& rDict = getDictionary(dict, DHTResponseMessage::R);
|
const BDE& rDict = getDictionary(dict, DHTResponseMessage::R);
|
||||||
const bencode::BDE& valuesList = getList(rDict,
|
const BDE& valuesList = getList(rDict,
|
||||||
DHTGetPeersReplyMessage::VALUES);
|
DHTGetPeersReplyMessage::VALUES);
|
||||||
std::deque<SharedHandle<Peer> > peers;
|
std::deque<SharedHandle<Peer> > peers;
|
||||||
for(bencode::BDE::List::const_iterator i = valuesList.listBegin();
|
for(BDE::List::const_iterator i = valuesList.listBegin();
|
||||||
i != valuesList.listEnd(); ++i) {
|
i != valuesList.listEnd(); ++i) {
|
||||||
const bencode::BDE& data = *i;
|
const BDE& data = *i;
|
||||||
if(data.isString() && data.s().size() == 6) {
|
if(data.isString() && data.s().size() == 6) {
|
||||||
std::pair<std::string, uint16_t> addr =
|
std::pair<std::string, uint16_t> addr =
|
||||||
PeerMessageUtil::unpackcompact(data.uc());
|
PeerMessageUtil::unpackcompact(data.uc());
|
||||||
|
@ -421,7 +421,7 @@ DHTMessageFactoryImpl::createGetPeersReplyMessageWithValues
|
||||||
peers.push_back(peer);
|
peers.push_back(peer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const bencode::BDE& token = getString(rDict, DHTGetPeersReplyMessage::TOKEN);
|
const BDE& token = getString(rDict, DHTGetPeersReplyMessage::TOKEN);
|
||||||
return createGetPeersReplyMessage(remoteNode, peers, token.s(),
|
return createGetPeersReplyMessage(remoteNode, peers, token.s(),
|
||||||
transactionID);
|
transactionID);
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,9 +68,9 @@ private:
|
||||||
// search node in routingTable. If it is not found, create new one.
|
// search node in routingTable. If it is not found, create new one.
|
||||||
SharedHandle<DHTNode> getRemoteNode(const unsigned char* id, const std::string& ipaddr, uint16_t port) const;
|
SharedHandle<DHTNode> getRemoteNode(const unsigned char* id, const std::string& ipaddr, uint16_t port) const;
|
||||||
|
|
||||||
void validateID(const bencode::BDE& id) const;
|
void validateID(const BDE& id) const;
|
||||||
|
|
||||||
void validatePort(const bencode::BDE& i) const;
|
void validatePort(const BDE& i) const;
|
||||||
|
|
||||||
std::deque<SharedHandle<DHTNode> > extractNodes(const unsigned char* src, size_t length);
|
std::deque<SharedHandle<DHTNode> > extractNodes(const unsigned char* src, size_t length);
|
||||||
|
|
||||||
|
@ -82,12 +82,12 @@ public:
|
||||||
virtual ~DHTMessageFactoryImpl();
|
virtual ~DHTMessageFactoryImpl();
|
||||||
|
|
||||||
virtual SharedHandle<DHTMessage>
|
virtual SharedHandle<DHTMessage>
|
||||||
createQueryMessage(const bencode::BDE& dict,
|
createQueryMessage(const BDE& dict,
|
||||||
const std::string& ipaddr, uint16_t port);
|
const std::string& ipaddr, uint16_t port);
|
||||||
|
|
||||||
virtual SharedHandle<DHTMessage>
|
virtual SharedHandle<DHTMessage>
|
||||||
createResponseMessage(const std::string& messageType,
|
createResponseMessage(const std::string& messageType,
|
||||||
const bencode::BDE& dict,
|
const BDE& dict,
|
||||||
const std::string& ipaddr, uint16_t port);
|
const std::string& ipaddr, uint16_t port);
|
||||||
|
|
||||||
virtual SharedHandle<DHTMessage>
|
virtual SharedHandle<DHTMessage>
|
||||||
|
@ -106,7 +106,7 @@ public:
|
||||||
|
|
||||||
SharedHandle<DHTMessage>
|
SharedHandle<DHTMessage>
|
||||||
createFindNodeReplyMessage(const SharedHandle<DHTNode>& remoteNode,
|
createFindNodeReplyMessage(const SharedHandle<DHTNode>& remoteNode,
|
||||||
const bencode::BDE& dict,
|
const BDE& dict,
|
||||||
const std::string& transactionID);
|
const std::string& transactionID);
|
||||||
|
|
||||||
|
|
||||||
|
@ -128,7 +128,7 @@ public:
|
||||||
|
|
||||||
SharedHandle<DHTMessage>
|
SharedHandle<DHTMessage>
|
||||||
createGetPeersReplyMessageWithNodes(const SharedHandle<DHTNode>& remoteNode,
|
createGetPeersReplyMessageWithNodes(const SharedHandle<DHTNode>& remoteNode,
|
||||||
const bencode::BDE& dict,
|
const BDE& dict,
|
||||||
const std::string& transactionID);
|
const std::string& transactionID);
|
||||||
|
|
||||||
virtual SharedHandle<DHTMessage>
|
virtual SharedHandle<DHTMessage>
|
||||||
|
@ -139,7 +139,7 @@ public:
|
||||||
|
|
||||||
SharedHandle<DHTMessage>
|
SharedHandle<DHTMessage>
|
||||||
createGetPeersReplyMessageWithValues(const SharedHandle<DHTNode>& remoteNode,
|
createGetPeersReplyMessageWithValues(const SharedHandle<DHTNode>& remoteNode,
|
||||||
const bencode::BDE& dict,
|
const BDE& dict,
|
||||||
const std::string& transactionID);
|
const std::string& transactionID);
|
||||||
|
|
||||||
virtual SharedHandle<DHTMessage>
|
virtual SharedHandle<DHTMessage>
|
||||||
|
|
|
@ -74,9 +74,9 @@ SharedHandle<DHTMessage> DHTMessageReceiver::receiveMessage()
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
bool isReply = false;
|
bool isReply = false;
|
||||||
const bencode::BDE dict = bencode::decode(data, length);
|
const BDE dict = bencode::decode(data, length);
|
||||||
if(dict.isDict()) {
|
if(dict.isDict()) {
|
||||||
const bencode::BDE& y = dict[DHTMessage::Y];
|
const BDE& y = dict[DHTMessage::Y];
|
||||||
if(y.isString()) {
|
if(y.isString()) {
|
||||||
if(y.s() == DHTResponseMessage::R || y.s() == DHTUnknownMessage::E) {
|
if(y.s() == DHTResponseMessage::R || y.s() == DHTUnknownMessage::E) {
|
||||||
isReply = true;
|
isReply = true;
|
||||||
|
|
|
@ -69,10 +69,10 @@ void DHTMessageTracker::addMessage(const SharedHandle<DHTMessage>& message, cons
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> >
|
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> >
|
||||||
DHTMessageTracker::messageArrived(const bencode::BDE& dict,
|
DHTMessageTracker::messageArrived(const BDE& dict,
|
||||||
const std::string& ipaddr, uint16_t port)
|
const std::string& ipaddr, uint16_t port)
|
||||||
{
|
{
|
||||||
const bencode::BDE& tid = dict[DHTMessage::T];
|
const BDE& tid = dict[DHTMessage::T];
|
||||||
if(!tid.isString()) {
|
if(!tid.isString()) {
|
||||||
throw DlAbortEx(StringFormat("Malformed DHT message. From:%s:%u",
|
throw DlAbortEx(StringFormat("Malformed DHT message. From:%s:%u",
|
||||||
ipaddr.c_str(), port).str());
|
ipaddr.c_str(), port).str());
|
||||||
|
|
|
@ -51,9 +51,7 @@ class DHTRoutingTable;
|
||||||
class DHTMessageFactory;
|
class DHTMessageFactory;
|
||||||
class DHTMessageTrackerEntry;
|
class DHTMessageTrackerEntry;
|
||||||
class Logger;
|
class Logger;
|
||||||
namespace bencode {
|
|
||||||
class BDE;
|
class BDE;
|
||||||
} // namespace bencode
|
|
||||||
|
|
||||||
class DHTMessageTracker {
|
class DHTMessageTracker {
|
||||||
private:
|
private:
|
||||||
|
@ -79,7 +77,7 @@ public:
|
||||||
SharedHandle<DHTMessageCallback>());
|
SharedHandle<DHTMessageCallback>());
|
||||||
|
|
||||||
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> >
|
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> >
|
||||||
messageArrived(const bencode::BDE& dict,
|
messageArrived(const BDE& dict,
|
||||||
const std::string& ipaddr, uint16_t port);
|
const std::string& ipaddr, uint16_t port);
|
||||||
|
|
||||||
void handleTimeout();
|
void handleTimeout();
|
||||||
|
|
|
@ -58,10 +58,10 @@ void DHTPingMessage::doReceivedAction()
|
||||||
_dispatcher->addMessageToQueue(reply);
|
_dispatcher->addMessageToQueue(reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
bencode::BDE DHTPingMessage::getArgument()
|
BDE DHTPingMessage::getArgument()
|
||||||
{
|
{
|
||||||
bencode::BDE aDict = bencode::BDE::dict();
|
BDE aDict = BDE::dict();
|
||||||
aDict[DHTMessage::ID] = bencode::BDE(_localNode->getID(), DHT_ID_LENGTH);
|
aDict[DHTMessage::ID] = BDE(_localNode->getID(), DHT_ID_LENGTH);
|
||||||
return aDict;
|
return aDict;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,7 +50,7 @@ public:
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual bencode::BDE getArgument();
|
virtual BDE getArgument();
|
||||||
|
|
||||||
virtual std::string getMessageType() const;
|
virtual std::string getMessageType() const;
|
||||||
|
|
||||||
|
|
|
@ -56,10 +56,10 @@ DHTPingReplyMessage::~DHTPingReplyMessage() {}
|
||||||
|
|
||||||
void DHTPingReplyMessage::doReceivedAction() {}
|
void DHTPingReplyMessage::doReceivedAction() {}
|
||||||
|
|
||||||
bencode::BDE DHTPingReplyMessage::getResponse()
|
BDE DHTPingReplyMessage::getResponse()
|
||||||
{
|
{
|
||||||
bencode::BDE rDict = bencode::BDE::dict();
|
BDE rDict = BDE::dict();
|
||||||
rDict[DHTMessage::ID] = bencode::BDE(_id, DHT_ID_LENGTH);
|
rDict[DHTMessage::ID] = BDE(_id, DHT_ID_LENGTH);
|
||||||
return rDict;
|
return rDict;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,7 +53,7 @@ public:
|
||||||
|
|
||||||
virtual void doReceivedAction();
|
virtual void doReceivedAction();
|
||||||
|
|
||||||
virtual bencode::BDE getResponse();
|
virtual BDE getResponse();
|
||||||
|
|
||||||
virtual std::string getMessageType() const;
|
virtual std::string getMessageType() const;
|
||||||
|
|
||||||
|
|
|
@ -55,7 +55,7 @@ std::string DHTQueryMessage::getType() const
|
||||||
return Q;
|
return Q;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DHTQueryMessage::fillMessage(bencode::BDE& msgDict)
|
void DHTQueryMessage::fillMessage(BDE& msgDict)
|
||||||
{
|
{
|
||||||
msgDict[Q] = getMessageType();
|
msgDict[Q] = getMessageType();
|
||||||
msgDict[A] = getArgument();
|
msgDict[A] = getArgument();
|
||||||
|
|
|
@ -52,9 +52,9 @@ public:
|
||||||
|
|
||||||
virtual std::string getType() const;
|
virtual std::string getType() const;
|
||||||
|
|
||||||
virtual void fillMessage(bencode::BDE& msgDict);
|
virtual void fillMessage(BDE& msgDict);
|
||||||
|
|
||||||
virtual bencode::BDE getArgument() = 0;
|
virtual BDE getArgument() = 0;
|
||||||
|
|
||||||
virtual bool isReply() const;
|
virtual bool isReply() const;
|
||||||
|
|
||||||
|
|
|
@ -53,7 +53,7 @@ std::string DHTResponseMessage::getType() const
|
||||||
return R;
|
return R;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DHTResponseMessage::fillMessage(bencode::BDE& msgDict)
|
void DHTResponseMessage::fillMessage(BDE& msgDict)
|
||||||
{
|
{
|
||||||
msgDict[R] = getResponse();
|
msgDict[R] = getResponse();
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,9 +52,9 @@ public:
|
||||||
|
|
||||||
virtual std::string getType() const;
|
virtual std::string getType() const;
|
||||||
|
|
||||||
virtual void fillMessage(bencode::BDE& msgDict);
|
virtual void fillMessage(BDE& msgDict);
|
||||||
|
|
||||||
virtual bencode::BDE getResponse() = 0;
|
virtual BDE getResponse() = 0;
|
||||||
|
|
||||||
virtual bool isReply() const;
|
virtual bool isReply() const;
|
||||||
|
|
||||||
|
|
|
@ -204,31 +204,31 @@ DefaultBtAnnounce::processAnnounceResponse(const unsigned char* trackerResponse,
|
||||||
size_t trackerResponseLength)
|
size_t trackerResponseLength)
|
||||||
{
|
{
|
||||||
logger->debug("Now processing tracker response.");
|
logger->debug("Now processing tracker response.");
|
||||||
const bencode::BDE dict =
|
const BDE dict =
|
||||||
bencode::decode(trackerResponse, trackerResponseLength);
|
bencode::decode(trackerResponse, trackerResponseLength);
|
||||||
if(!dict.isDict()) {
|
if(!dict.isDict()) {
|
||||||
throw DlAbortEx(MSG_NULL_TRACKER_RESPONSE);
|
throw DlAbortEx(MSG_NULL_TRACKER_RESPONSE);
|
||||||
}
|
}
|
||||||
const bencode::BDE& failure = dict[BtAnnounce::FAILURE_REASON];
|
const BDE& failure = dict[BtAnnounce::FAILURE_REASON];
|
||||||
if(failure.isString()) {
|
if(failure.isString()) {
|
||||||
throw DlAbortEx
|
throw DlAbortEx
|
||||||
(StringFormat(EX_TRACKER_FAILURE, failure.s().c_str()).str());
|
(StringFormat(EX_TRACKER_FAILURE, failure.s().c_str()).str());
|
||||||
}
|
}
|
||||||
const bencode::BDE& warn = dict[BtAnnounce::WARNING_MESSAGE];
|
const BDE& warn = dict[BtAnnounce::WARNING_MESSAGE];
|
||||||
if(warn.isString()) {
|
if(warn.isString()) {
|
||||||
logger->warn(MSG_TRACKER_WARNING_MESSAGE, warn.s().c_str());
|
logger->warn(MSG_TRACKER_WARNING_MESSAGE, warn.s().c_str());
|
||||||
}
|
}
|
||||||
const bencode::BDE& tid = dict[BtAnnounce::TRACKER_ID];
|
const BDE& tid = dict[BtAnnounce::TRACKER_ID];
|
||||||
if(tid.isString()) {
|
if(tid.isString()) {
|
||||||
trackerId = tid.s();
|
trackerId = tid.s();
|
||||||
logger->debug("Tracker ID:%s", trackerId.c_str());
|
logger->debug("Tracker ID:%s", trackerId.c_str());
|
||||||
}
|
}
|
||||||
const bencode::BDE& ival = dict[BtAnnounce::INTERVAL];
|
const BDE& ival = dict[BtAnnounce::INTERVAL];
|
||||||
if(ival.isInteger() && ival.i() > 0) {
|
if(ival.isInteger() && ival.i() > 0) {
|
||||||
interval = ival.i();
|
interval = ival.i();
|
||||||
logger->debug("Interval:%d", interval);
|
logger->debug("Interval:%d", interval);
|
||||||
}
|
}
|
||||||
const bencode::BDE& mival = dict[BtAnnounce::MIN_INTERVAL];
|
const BDE& mival = dict[BtAnnounce::MIN_INTERVAL];
|
||||||
if(mival.isInteger() && mival.i() > 0) {
|
if(mival.isInteger() && mival.i() > 0) {
|
||||||
minInterval = mival.i();
|
minInterval = mival.i();
|
||||||
logger->debug("Min interval:%d", minInterval);
|
logger->debug("Min interval:%d", minInterval);
|
||||||
|
@ -239,17 +239,17 @@ DefaultBtAnnounce::processAnnounceResponse(const unsigned char* trackerResponse,
|
||||||
// Use interval as a minInterval if minInterval is not supplied.
|
// Use interval as a minInterval if minInterval is not supplied.
|
||||||
minInterval = interval;
|
minInterval = interval;
|
||||||
}
|
}
|
||||||
const bencode::BDE& comp = dict[BtAnnounce::COMPLETE];
|
const BDE& comp = dict[BtAnnounce::COMPLETE];
|
||||||
if(comp.isInteger()) {
|
if(comp.isInteger()) {
|
||||||
complete = comp.i();
|
complete = comp.i();
|
||||||
logger->debug("Complete:%d", complete);
|
logger->debug("Complete:%d", complete);
|
||||||
}
|
}
|
||||||
const bencode::BDE& incomp = dict[BtAnnounce::INCOMPLETE];
|
const BDE& incomp = dict[BtAnnounce::INCOMPLETE];
|
||||||
if(incomp.isInteger()) {
|
if(incomp.isInteger()) {
|
||||||
incomplete = incomp.i();
|
incomplete = incomp.i();
|
||||||
logger->debug("Incomplete:%d", incomplete);
|
logger->debug("Incomplete:%d", incomplete);
|
||||||
}
|
}
|
||||||
const bencode::BDE& peerData = dict[BtAnnounce::PEERS];
|
const BDE& peerData = dict[BtAnnounce::PEERS];
|
||||||
if(peerData.isNone()) {
|
if(peerData.isNone()) {
|
||||||
logger->info(MSG_NO_PEER_LIST_RECEIVED);
|
logger->info(MSG_NO_PEER_LIST_RECEIVED);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -111,13 +111,13 @@ void DefaultBtContext::extractPieceHash(const std::string& hashData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefaultBtContext::extractFileEntries(const bencode::BDE& infoDict,
|
void DefaultBtContext::extractFileEntries(const BDE& infoDict,
|
||||||
const std::string& defaultName,
|
const std::string& defaultName,
|
||||||
const std::string& overrideName,
|
const std::string& overrideName,
|
||||||
const std::deque<std::string>& urlList)
|
const std::deque<std::string>& urlList)
|
||||||
{
|
{
|
||||||
if(overrideName.empty()) {
|
if(overrideName.empty()) {
|
||||||
const bencode::BDE& nameData = infoDict[BtContext::C_NAME];
|
const BDE& nameData = infoDict[BtContext::C_NAME];
|
||||||
if(nameData.isString()) {
|
if(nameData.isString()) {
|
||||||
name = nameData.s();
|
name = nameData.s();
|
||||||
} else {
|
} else {
|
||||||
|
@ -126,34 +126,34 @@ void DefaultBtContext::extractFileEntries(const bencode::BDE& infoDict,
|
||||||
} else {
|
} else {
|
||||||
name = overrideName;
|
name = overrideName;
|
||||||
}
|
}
|
||||||
const bencode::BDE& filesList = infoDict[BtContext::C_FILES];
|
const BDE& filesList = infoDict[BtContext::C_FILES];
|
||||||
if(filesList.isList()) {
|
if(filesList.isList()) {
|
||||||
uint64_t length = 0;
|
uint64_t length = 0;
|
||||||
off_t offset = 0;
|
off_t offset = 0;
|
||||||
// multi-file mode
|
// multi-file mode
|
||||||
fileMode = BtContext::MULTI;
|
fileMode = BtContext::MULTI;
|
||||||
for(bencode::BDE::List::const_iterator itr = filesList.listBegin();
|
for(BDE::List::const_iterator itr = filesList.listBegin();
|
||||||
itr != filesList.listEnd(); ++itr) {
|
itr != filesList.listEnd(); ++itr) {
|
||||||
const bencode::BDE& fileDict = *itr;
|
const BDE& fileDict = *itr;
|
||||||
if(!fileDict.isDict()) {
|
if(!fileDict.isDict()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const bencode::BDE& fileLengthData = fileDict[BtContext::C_LENGTH];
|
const BDE& fileLengthData = fileDict[BtContext::C_LENGTH];
|
||||||
if(!fileLengthData.isInteger()) {
|
if(!fileLengthData.isInteger()) {
|
||||||
throw DlAbortEx(StringFormat(MSG_MISSING_BT_INFO,
|
throw DlAbortEx(StringFormat(MSG_MISSING_BT_INFO,
|
||||||
BtContext::C_LENGTH.c_str()).str());
|
BtContext::C_LENGTH.c_str()).str());
|
||||||
}
|
}
|
||||||
length += fileLengthData.i();
|
length += fileLengthData.i();
|
||||||
|
|
||||||
const bencode::BDE& pathList = fileDict[BtContext::C_PATH];
|
const BDE& pathList = fileDict[BtContext::C_PATH];
|
||||||
if(!pathList.isList() || pathList.empty()) {
|
if(!pathList.isList() || pathList.empty()) {
|
||||||
throw DlAbortEx("Path is empty.");
|
throw DlAbortEx("Path is empty.");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> pathelem(pathList.size());
|
std::vector<std::string> pathelem(pathList.size());
|
||||||
std::transform(pathList.listBegin(), pathList.listEnd(), pathelem.begin(),
|
std::transform(pathList.listBegin(), pathList.listEnd(), pathelem.begin(),
|
||||||
std::mem_fun_ref(&bencode::BDE::s));
|
std::mem_fun_ref(&BDE::s));
|
||||||
std::string path =
|
std::string path =
|
||||||
name+"/"+Util::joinPath(pathelem.begin(), pathelem.end());
|
name+"/"+Util::joinPath(pathelem.begin(), pathelem.end());
|
||||||
// Split path with '/' again because each pathList element can
|
// Split path with '/' again because each pathList element can
|
||||||
|
@ -174,7 +174,7 @@ void DefaultBtContext::extractFileEntries(const bencode::BDE& infoDict,
|
||||||
} else {
|
} else {
|
||||||
// single-file mode;
|
// single-file mode;
|
||||||
fileMode = BtContext::SINGLE;
|
fileMode = BtContext::SINGLE;
|
||||||
const bencode::BDE& lengthData = infoDict[BtContext::C_LENGTH];
|
const BDE& lengthData = infoDict[BtContext::C_LENGTH];
|
||||||
if(!lengthData.isInteger()) {
|
if(!lengthData.isInteger()) {
|
||||||
throw DlAbortEx(StringFormat(MSG_MISSING_BT_INFO,
|
throw DlAbortEx(StringFormat(MSG_MISSING_BT_INFO,
|
||||||
BtContext::C_LENGTH.c_str()).str());
|
BtContext::C_LENGTH.c_str()).str());
|
||||||
|
@ -203,7 +203,7 @@ void DefaultBtContext::extractFileEntries(const bencode::BDE& infoDict,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefaultBtContext::extractAnnounceURI(const bencode::BDE& announceData)
|
void DefaultBtContext::extractAnnounceURI(const BDE& announceData)
|
||||||
{
|
{
|
||||||
// Assumed announceData is string
|
// Assumed announceData is string
|
||||||
std::deque<std::string> urls;
|
std::deque<std::string> urls;
|
||||||
|
@ -211,19 +211,19 @@ void DefaultBtContext::extractAnnounceURI(const bencode::BDE& announceData)
|
||||||
announceTiers.push_back(AnnounceTierHandle(new AnnounceTier(urls)));
|
announceTiers.push_back(AnnounceTierHandle(new AnnounceTier(urls)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefaultBtContext::extractAnnounceList(const bencode::BDE& announceList)
|
void DefaultBtContext::extractAnnounceList(const BDE& announceList)
|
||||||
{
|
{
|
||||||
// Assumed announceList is string
|
// Assumed announceList is string
|
||||||
for(bencode::BDE::List::const_iterator itr = announceList.listBegin();
|
for(BDE::List::const_iterator itr = announceList.listBegin();
|
||||||
itr != announceList.listEnd(); ++itr) {
|
itr != announceList.listEnd(); ++itr) {
|
||||||
const bencode::BDE& elemList = *itr;
|
const BDE& elemList = *itr;
|
||||||
if(!elemList.isList()) {
|
if(!elemList.isList()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::deque<std::string> urls;
|
std::deque<std::string> urls;
|
||||||
for(bencode::BDE::List::const_iterator elemItr = elemList.listBegin();
|
for(BDE::List::const_iterator elemItr = elemList.listBegin();
|
||||||
elemItr != elemList.listEnd(); ++elemItr) {
|
elemItr != elemList.listEnd(); ++elemItr) {
|
||||||
const bencode::BDE& url = (*elemItr);
|
const BDE& url = (*elemItr);
|
||||||
if(url.isString()) {
|
if(url.isString()) {
|
||||||
urls.push_back(Util::trim(url.s()));
|
urls.push_back(Util::trim(url.s()));
|
||||||
}
|
}
|
||||||
|
@ -235,13 +235,13 @@ void DefaultBtContext::extractAnnounceList(const bencode::BDE& announceList)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefaultBtContext::extractAnnounce(const bencode::BDE& rootDict)
|
void DefaultBtContext::extractAnnounce(const BDE& rootDict)
|
||||||
{
|
{
|
||||||
const bencode::BDE& announceList = rootDict[BtContext::C_ANNOUNCE_LIST];
|
const BDE& announceList = rootDict[BtContext::C_ANNOUNCE_LIST];
|
||||||
if(announceList.isList()) {
|
if(announceList.isList()) {
|
||||||
extractAnnounceList(announceList);
|
extractAnnounceList(announceList);
|
||||||
} else {
|
} else {
|
||||||
const bencode::BDE& announce = rootDict[BtContext::C_ANNOUNCE];
|
const BDE& announce = rootDict[BtContext::C_ANNOUNCE];
|
||||||
if(announce.isString()) {
|
if(announce.isString()) {
|
||||||
extractAnnounceURI(announce);
|
extractAnnounceURI(announce);
|
||||||
}
|
}
|
||||||
|
@ -249,10 +249,10 @@ void DefaultBtContext::extractAnnounce(const bencode::BDE& rootDict)
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefaultBtContext::extractUrlList(std::deque<std::string>& uris,
|
void DefaultBtContext::extractUrlList(std::deque<std::string>& uris,
|
||||||
const bencode::BDE& bde)
|
const BDE& bde)
|
||||||
{
|
{
|
||||||
if(bde.isList()) {
|
if(bde.isList()) {
|
||||||
for(bencode::BDE::List::const_iterator itr = bde.listBegin();
|
for(BDE::List::const_iterator itr = bde.listBegin();
|
||||||
itr != bde.listEnd(); ++itr) {
|
itr != bde.listEnd(); ++itr) {
|
||||||
if((*itr).isString()) {
|
if((*itr).isString()) {
|
||||||
uris.push_back((*itr).s());
|
uris.push_back((*itr).s());
|
||||||
|
@ -263,25 +263,25 @@ void DefaultBtContext::extractUrlList(std::deque<std::string>& uris,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefaultBtContext::extractNodes(const bencode::BDE& nodesList)
|
void DefaultBtContext::extractNodes(const BDE& nodesList)
|
||||||
{
|
{
|
||||||
if(!nodesList.isList()) {
|
if(!nodesList.isList()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for(bencode::BDE::List::const_iterator i = nodesList.listBegin();
|
for(BDE::List::const_iterator i = nodesList.listBegin();
|
||||||
i != nodesList.listEnd(); ++i) {
|
i != nodesList.listEnd(); ++i) {
|
||||||
const bencode::BDE& addrPairList = (*i);
|
const BDE& addrPairList = (*i);
|
||||||
if(!addrPairList.isList() || addrPairList.size() != 2) {
|
if(!addrPairList.isList() || addrPairList.size() != 2) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const bencode::BDE& hostname = addrPairList[0];
|
const BDE& hostname = addrPairList[0];
|
||||||
if(!hostname.isString()) {
|
if(!hostname.isString()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(Util::trim(hostname.s()).empty()) {
|
if(Util::trim(hostname.s()).empty()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const bencode::BDE& port = addrPairList[1];
|
const BDE& port = addrPairList[1];
|
||||||
if(!port.isInteger() || !(0 < port.i() && port.i() < 65536)) {
|
if(!port.isInteger() || !(0 < port.i() && port.i() < 65536)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -304,7 +304,7 @@ void DefaultBtContext::load(const std::string& torrentFile,
|
||||||
overrideName);
|
overrideName);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefaultBtContext::processRootDictionary(const bencode::BDE& rootDict,
|
void DefaultBtContext::processRootDictionary(const BDE& rootDict,
|
||||||
const std::string& defaultName,
|
const std::string& defaultName,
|
||||||
const std::string& overrideName)
|
const std::string& overrideName)
|
||||||
{
|
{
|
||||||
|
@ -312,7 +312,7 @@ void DefaultBtContext::processRootDictionary(const bencode::BDE& rootDict,
|
||||||
if(!rootDict.isDict()) {
|
if(!rootDict.isDict()) {
|
||||||
throw DlAbortEx("torrent file does not contain a root dictionary.");
|
throw DlAbortEx("torrent file does not contain a root dictionary.");
|
||||||
}
|
}
|
||||||
const bencode::BDE& infoDict = rootDict[BtContext::C_INFO];
|
const BDE& infoDict = rootDict[BtContext::C_INFO];
|
||||||
if(!infoDict.isDict()) {
|
if(!infoDict.isDict()) {
|
||||||
throw DlAbortEx(StringFormat(MSG_MISSING_BT_INFO,
|
throw DlAbortEx(StringFormat(MSG_MISSING_BT_INFO,
|
||||||
BtContext::C_INFO.c_str()).str());
|
BtContext::C_INFO.c_str()).str());
|
||||||
|
@ -325,7 +325,7 @@ void DefaultBtContext::processRootDictionary(const bencode::BDE& rootDict,
|
||||||
encodedInfoDict.size());
|
encodedInfoDict.size());
|
||||||
infoHashString = Util::toHex(infoHash, INFO_HASH_LENGTH);
|
infoHashString = Util::toHex(infoHash, INFO_HASH_LENGTH);
|
||||||
// calculate the number of pieces
|
// calculate the number of pieces
|
||||||
const bencode::BDE& piecesData = infoDict[BtContext::C_PIECES];
|
const BDE& piecesData = infoDict[BtContext::C_PIECES];
|
||||||
if(!piecesData.isString()) {
|
if(!piecesData.isString()) {
|
||||||
throw DlAbortEx(StringFormat(MSG_MISSING_BT_INFO,
|
throw DlAbortEx(StringFormat(MSG_MISSING_BT_INFO,
|
||||||
BtContext::C_PIECES.c_str()).str());
|
BtContext::C_PIECES.c_str()).str());
|
||||||
|
@ -338,7 +338,7 @@ void DefaultBtContext::processRootDictionary(const bencode::BDE& rootDict,
|
||||||
throw DlAbortEx("The number of pieces is 0.");
|
throw DlAbortEx("The number of pieces is 0.");
|
||||||
}
|
}
|
||||||
// retrieve piece length
|
// retrieve piece length
|
||||||
const bencode::BDE& pieceLengthData = infoDict[BtContext::C_PIECE_LENGTH];
|
const BDE& pieceLengthData = infoDict[BtContext::C_PIECE_LENGTH];
|
||||||
if(!pieceLengthData.isInteger()) {
|
if(!pieceLengthData.isInteger()) {
|
||||||
throw DlAbortEx(StringFormat(MSG_MISSING_BT_INFO,
|
throw DlAbortEx(StringFormat(MSG_MISSING_BT_INFO,
|
||||||
BtContext::C_PIECE_LENGTH.c_str()).str());
|
BtContext::C_PIECE_LENGTH.c_str()).str());
|
||||||
|
@ -347,7 +347,7 @@ void DefaultBtContext::processRootDictionary(const bencode::BDE& rootDict,
|
||||||
// retrieve piece hashes
|
// retrieve piece hashes
|
||||||
extractPieceHash(piecesData.s(), PIECE_HASH_LENGTH);
|
extractPieceHash(piecesData.s(), PIECE_HASH_LENGTH);
|
||||||
// private flag
|
// private flag
|
||||||
const bencode::BDE& privateData = infoDict[BtContext::C_PRIVATE];
|
const BDE& privateData = infoDict[BtContext::C_PRIVATE];
|
||||||
if(privateData.isInteger()) {
|
if(privateData.isInteger()) {
|
||||||
_private = (privateData.i() == 1);
|
_private = (privateData.i() == 1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,10 +43,7 @@ namespace aria2 {
|
||||||
|
|
||||||
class Randomizer;
|
class Randomizer;
|
||||||
class Logger;
|
class Logger;
|
||||||
|
|
||||||
namespace bencode {
|
|
||||||
class BDE;
|
class BDE;
|
||||||
}
|
|
||||||
|
|
||||||
#define INFO_HASH_LENGTH 20
|
#define INFO_HASH_LENGTH 20
|
||||||
#define PIECE_HASH_LENGTH 20
|
#define PIECE_HASH_LENGTH 20
|
||||||
|
@ -74,19 +71,19 @@ private:
|
||||||
|
|
||||||
void clear();
|
void clear();
|
||||||
void extractPieceHash(const std::string& hashData, size_t hashLength);
|
void extractPieceHash(const std::string& hashData, size_t hashLength);
|
||||||
void extractFileEntries(const bencode::BDE& infoDic,
|
void extractFileEntries(const BDE& infoDic,
|
||||||
const std::string& defaultName,
|
const std::string& defaultName,
|
||||||
const std::string& overrideName,
|
const std::string& overrideName,
|
||||||
const std::deque<std::string>& urlList);
|
const std::deque<std::string>& urlList);
|
||||||
void extractAnnounceURI(const bencode::BDE& announceData);
|
void extractAnnounceURI(const BDE& announceData);
|
||||||
void extractAnnounceList(const bencode::BDE& announceListData);
|
void extractAnnounceList(const BDE& announceListData);
|
||||||
void extractAnnounce(const bencode::BDE& rootDict);
|
void extractAnnounce(const BDE& rootDict);
|
||||||
|
|
||||||
void extractUrlList(std::deque<std::string>& uris, const bencode::BDE& obj);
|
void extractUrlList(std::deque<std::string>& uris, const BDE& obj);
|
||||||
|
|
||||||
void extractNodes(const bencode::BDE& nodes);
|
void extractNodes(const BDE& nodes);
|
||||||
|
|
||||||
void processRootDictionary(const bencode::BDE& rootDic,
|
void processRootDictionary(const BDE& rootDic,
|
||||||
const std::string& defaultName,
|
const std::string& defaultName,
|
||||||
const std::string& overrideName);
|
const std::string& overrideName);
|
||||||
|
|
||||||
|
|
|
@ -55,14 +55,14 @@ HandshakeExtensionMessage::~HandshakeExtensionMessage() {}
|
||||||
|
|
||||||
std::string HandshakeExtensionMessage::getBencodedData()
|
std::string HandshakeExtensionMessage::getBencodedData()
|
||||||
{
|
{
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
if(!_clientVersion.empty()) {
|
if(!_clientVersion.empty()) {
|
||||||
dict["v"] = _clientVersion;
|
dict["v"] = _clientVersion;
|
||||||
}
|
}
|
||||||
if(_tcpPort > 0) {
|
if(_tcpPort > 0) {
|
||||||
dict["p"] = _tcpPort;
|
dict["p"] = _tcpPort;
|
||||||
}
|
}
|
||||||
bencode::BDE extDict = bencode::BDE::dict();
|
BDE extDict = BDE::dict();
|
||||||
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;
|
||||||
|
@ -133,21 +133,21 @@ 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());
|
||||||
const bencode::BDE dict = bencode::decode(data+1, length-1);
|
const BDE dict = bencode::decode(data+1, length-1);
|
||||||
if(!dict.isDict()) {
|
if(!dict.isDict()) {
|
||||||
throw DlAbortEx("Unexpected payload format for extended message handshake");
|
throw DlAbortEx("Unexpected payload format for extended message handshake");
|
||||||
}
|
}
|
||||||
const bencode::BDE& port = dict["p"];
|
const BDE& port = dict["p"];
|
||||||
if(port.isInteger() && 0 < port.i() && port.i() < 65536) {
|
if(port.isInteger() && 0 < port.i() && port.i() < 65536) {
|
||||||
msg->_tcpPort = port.i();
|
msg->_tcpPort = port.i();
|
||||||
}
|
}
|
||||||
const bencode::BDE& version = dict["v"];
|
const BDE& version = dict["v"];
|
||||||
if(version.isString()) {
|
if(version.isString()) {
|
||||||
msg->_clientVersion = version.s();
|
msg->_clientVersion = version.s();
|
||||||
}
|
}
|
||||||
const bencode::BDE& extDict = dict["m"];
|
const BDE& extDict = dict["m"];
|
||||||
if(extDict.isDict()) {
|
if(extDict.isDict()) {
|
||||||
for(bencode::BDE::Dict::const_iterator i = extDict.dictBegin();
|
for(BDE::Dict::const_iterator i = extDict.dictBegin();
|
||||||
i != extDict.dictEnd(); ++i) {
|
i != extDict.dictEnd(); ++i) {
|
||||||
if((*i).second.isInteger()) {
|
if((*i).second.isInteger()) {
|
||||||
msg->_extensions[(*i).first] = (*i).second.i();
|
msg->_extensions[(*i).first] = (*i).second.i();
|
||||||
|
|
|
@ -205,7 +205,8 @@ SRCS = Socket.h\
|
||||||
HttpServer.cc HttpServer.h\
|
HttpServer.cc HttpServer.h\
|
||||||
PieceSelector.h\
|
PieceSelector.h\
|
||||||
LongestSequencePieceSelector.cc LongestSequencePieceSelector.h\
|
LongestSequencePieceSelector.cc LongestSequencePieceSelector.h\
|
||||||
bitfield.h
|
bitfield.h\
|
||||||
|
BDE.cc BDE.h
|
||||||
|
|
||||||
if HAVE_POSIX_FALLOCATE
|
if HAVE_POSIX_FALLOCATE
|
||||||
SRCS += FallocFileAllocationIterator.cc FallocFileAllocationIterator.h
|
SRCS += FallocFileAllocationIterator.cc FallocFileAllocationIterator.h
|
||||||
|
|
|
@ -409,7 +409,7 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
|
||||||
HttpServerCommand.h HttpServerResponseCommand.cc \
|
HttpServerCommand.h HttpServerResponseCommand.cc \
|
||||||
HttpServerResponseCommand.h HttpServer.cc HttpServer.h \
|
HttpServerResponseCommand.h HttpServer.cc HttpServer.h \
|
||||||
PieceSelector.h LongestSequencePieceSelector.cc \
|
PieceSelector.h LongestSequencePieceSelector.cc \
|
||||||
LongestSequencePieceSelector.h bitfield.h \
|
LongestSequencePieceSelector.h bitfield.h BDE.cc BDE.h \
|
||||||
FallocFileAllocationIterator.cc FallocFileAllocationIterator.h \
|
FallocFileAllocationIterator.cc FallocFileAllocationIterator.h \
|
||||||
EpollEventPoll.cc EpollEventPoll.h TLSContext.h \
|
EpollEventPoll.cc EpollEventPoll.h TLSContext.h \
|
||||||
LibgnutlsTLSContext.cc LibgnutlsTLSContext.h \
|
LibgnutlsTLSContext.cc LibgnutlsTLSContext.h \
|
||||||
|
@ -804,14 +804,15 @@ am__objects_23 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \
|
||||||
SelectEventPoll.$(OBJEXT) HttpListenCommand.$(OBJEXT) \
|
SelectEventPoll.$(OBJEXT) HttpListenCommand.$(OBJEXT) \
|
||||||
HttpServerCommand.$(OBJEXT) \
|
HttpServerCommand.$(OBJEXT) \
|
||||||
HttpServerResponseCommand.$(OBJEXT) HttpServer.$(OBJEXT) \
|
HttpServerResponseCommand.$(OBJEXT) HttpServer.$(OBJEXT) \
|
||||||
LongestSequencePieceSelector.$(OBJEXT) $(am__objects_1) \
|
LongestSequencePieceSelector.$(OBJEXT) BDE.$(OBJEXT) \
|
||||||
$(am__objects_2) $(am__objects_3) $(am__objects_4) \
|
$(am__objects_1) $(am__objects_2) $(am__objects_3) \
|
||||||
$(am__objects_5) $(am__objects_6) $(am__objects_7) \
|
$(am__objects_4) $(am__objects_5) $(am__objects_6) \
|
||||||
$(am__objects_8) $(am__objects_9) $(am__objects_10) \
|
$(am__objects_7) $(am__objects_8) $(am__objects_9) \
|
||||||
$(am__objects_11) $(am__objects_12) $(am__objects_13) \
|
$(am__objects_10) $(am__objects_11) $(am__objects_12) \
|
||||||
$(am__objects_14) $(am__objects_15) $(am__objects_16) \
|
$(am__objects_13) $(am__objects_14) $(am__objects_15) \
|
||||||
$(am__objects_17) $(am__objects_18) $(am__objects_19) \
|
$(am__objects_16) $(am__objects_17) $(am__objects_18) \
|
||||||
$(am__objects_20) $(am__objects_21) $(am__objects_22)
|
$(am__objects_19) $(am__objects_20) $(am__objects_21) \
|
||||||
|
$(am__objects_22)
|
||||||
am_libaria2c_a_OBJECTS = $(am__objects_23)
|
am_libaria2c_a_OBJECTS = $(am__objects_23)
|
||||||
libaria2c_a_OBJECTS = $(am_libaria2c_a_OBJECTS)
|
libaria2c_a_OBJECTS = $(am_libaria2c_a_OBJECTS)
|
||||||
am__installdirs = "$(DESTDIR)$(bindir)"
|
am__installdirs = "$(DESTDIR)$(bindir)"
|
||||||
|
@ -1139,14 +1140,15 @@ SRCS = Socket.h SocketCore.cc SocketCore.h BinaryStream.h Command.cc \
|
||||||
HttpServerCommand.h HttpServerResponseCommand.cc \
|
HttpServerCommand.h HttpServerResponseCommand.cc \
|
||||||
HttpServerResponseCommand.h HttpServer.cc HttpServer.h \
|
HttpServerResponseCommand.h HttpServer.cc HttpServer.h \
|
||||||
PieceSelector.h LongestSequencePieceSelector.cc \
|
PieceSelector.h LongestSequencePieceSelector.cc \
|
||||||
LongestSequencePieceSelector.h bitfield.h $(am__append_1) \
|
LongestSequencePieceSelector.h bitfield.h BDE.cc BDE.h \
|
||||||
$(am__append_2) $(am__append_3) $(am__append_4) \
|
$(am__append_1) $(am__append_2) $(am__append_3) \
|
||||||
$(am__append_5) $(am__append_6) $(am__append_7) \
|
$(am__append_4) $(am__append_5) $(am__append_6) \
|
||||||
$(am__append_8) $(am__append_9) $(am__append_10) \
|
$(am__append_7) $(am__append_8) $(am__append_9) \
|
||||||
$(am__append_11) $(am__append_12) $(am__append_13) \
|
$(am__append_10) $(am__append_11) $(am__append_12) \
|
||||||
$(am__append_14) $(am__append_15) $(am__append_16) \
|
$(am__append_13) $(am__append_14) $(am__append_15) \
|
||||||
$(am__append_17) $(am__append_18) $(am__append_19) \
|
$(am__append_16) $(am__append_17) $(am__append_18) \
|
||||||
$(am__append_20) $(am__append_21) $(am__append_22)
|
$(am__append_19) $(am__append_20) $(am__append_21) \
|
||||||
|
$(am__append_22)
|
||||||
noinst_LIBRARIES = libaria2c.a
|
noinst_LIBRARIES = libaria2c.a
|
||||||
libaria2c_a_SOURCES = $(SRCS)
|
libaria2c_a_SOURCES = $(SRCS)
|
||||||
aria2c_LDADD = libaria2c.a @LIBINTL@ @ALLOCA@ @LIBGNUTLS_LIBS@\
|
aria2c_LDADD = libaria2c.a @LIBINTL@ @ALLOCA@ @LIBGNUTLS_LIBS@\
|
||||||
|
@ -1251,6 +1253,7 @@ distclean-compile:
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AuthConfig.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AuthConfig.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AuthConfigFactory.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AuthConfigFactory.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AutoSaveCommand.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AutoSaveCommand.Po@am__quote@
|
||||||
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BDE.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BNode.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BNode.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Base64.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Base64.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BitfieldMan.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BitfieldMan.Po@am__quote@
|
||||||
|
|
|
@ -45,7 +45,7 @@ namespace aria2 {
|
||||||
class PeerListProcessor {
|
class PeerListProcessor {
|
||||||
public:
|
public:
|
||||||
template<typename OutputIterator>
|
template<typename OutputIterator>
|
||||||
void extractPeer(const bencode::BDE& peerData, OutputIterator dest)
|
void extractPeer(const BDE& peerData, OutputIterator dest)
|
||||||
{
|
{
|
||||||
if(peerData.isList()) {
|
if(peerData.isList()) {
|
||||||
extractPeerFromList(peerData, dest);
|
extractPeerFromList(peerData, dest);
|
||||||
|
@ -55,18 +55,18 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename OutputIterator>
|
template<typename OutputIterator>
|
||||||
void extractPeerFromList(const bencode::BDE& peerData, OutputIterator dest)
|
void extractPeerFromList(const BDE& peerData, OutputIterator dest)
|
||||||
{
|
{
|
||||||
for(bencode::BDE::List::const_iterator itr = peerData.listBegin();
|
for(BDE::List::const_iterator itr = peerData.listBegin();
|
||||||
itr != peerData.listEnd(); ++itr) {
|
itr != peerData.listEnd(); ++itr) {
|
||||||
const bencode::BDE& peerDict = *itr;
|
const BDE& peerDict = *itr;
|
||||||
if(!peerDict.isDict()) {
|
if(!peerDict.isDict()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
static const std::string IP = "ip";
|
static const std::string IP = "ip";
|
||||||
static const std::string PORT = "port";
|
static const std::string PORT = "port";
|
||||||
const bencode::BDE& ip = peerDict[IP];
|
const BDE& ip = peerDict[IP];
|
||||||
const bencode::BDE& port = peerDict[PORT];
|
const BDE& port = peerDict[PORT];
|
||||||
if(!ip.isString() || !port.isInteger() ||
|
if(!ip.isString() || !port.isInteger() ||
|
||||||
!(0 < port.i() && port.i() < 65536)) {
|
!(0 < port.i() && port.i() < 65536)) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -77,7 +77,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename OutputIterator>
|
template<typename OutputIterator>
|
||||||
void extractPeerFromCompact(const bencode::BDE& peerData, OutputIterator dest)
|
void extractPeerFromCompact(const BDE& peerData, OutputIterator dest)
|
||||||
{
|
{
|
||||||
size_t length = peerData.s().size();
|
size_t length = peerData.s().size();
|
||||||
if(length%6 == 0) {
|
if(length%6 == 0) {
|
||||||
|
|
|
@ -62,7 +62,7 @@ std::string UTPexExtensionMessage::getBencodedData()
|
||||||
std::pair<std::string, std::string> droppedPeerPair =
|
std::pair<std::string, std::string> droppedPeerPair =
|
||||||
createCompactPeerListAndFlag(_droppedPeers);
|
createCompactPeerListAndFlag(_droppedPeers);
|
||||||
|
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["added"] = freshPeerPair.first;
|
dict["added"] = freshPeerPair.first;
|
||||||
dict["added.f"] = freshPeerPair.second;
|
dict["added.f"] = freshPeerPair.second;
|
||||||
dict["dropped"] = droppedPeerPair.first;
|
dict["dropped"] = droppedPeerPair.first;
|
||||||
|
@ -172,14 +172,14 @@ UTPexExtensionMessage::create(const unsigned char* data, size_t len)
|
||||||
}
|
}
|
||||||
UTPexExtensionMessageHandle msg(new UTPexExtensionMessage(*data));
|
UTPexExtensionMessageHandle msg(new UTPexExtensionMessage(*data));
|
||||||
|
|
||||||
const bencode::BDE dict = bencode::decode(data+1, len-1);
|
const BDE dict = bencode::decode(data+1, len-1);
|
||||||
if(dict.isDict()) {
|
if(dict.isDict()) {
|
||||||
PeerListProcessor proc;
|
PeerListProcessor proc;
|
||||||
const bencode::BDE& added = dict["added"];
|
const BDE& added = dict["added"];
|
||||||
if(added.isString()) {
|
if(added.isString()) {
|
||||||
proc.extractPeerFromCompact(added, std::back_inserter(msg->_freshPeers));
|
proc.extractPeerFromCompact(added, std::back_inserter(msg->_freshPeers));
|
||||||
}
|
}
|
||||||
const bencode::BDE& dropped = dict["dropped"];
|
const BDE& dropped = dict["dropped"];
|
||||||
if(dropped.isString()) {
|
if(dropped.isString()) {
|
||||||
proc.extractPeerFromCompact(dropped,
|
proc.extractPeerFromCompact(dropped,
|
||||||
std::back_inserter(msg->_droppedPeers));
|
std::back_inserter(msg->_droppedPeers));
|
||||||
|
|
185
src/bencode.cc
185
src/bencode.cc
|
@ -38,195 +38,12 @@
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#include "StringFormat.h"
|
#include "StringFormat.h"
|
||||||
#include "Util.h"
|
#include "RecoverableException.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
namespace bencode {
|
namespace bencode {
|
||||||
|
|
||||||
const BDE BDE::none;
|
|
||||||
|
|
||||||
BDE::BDE():_type(TYPE_NONE) {}
|
|
||||||
|
|
||||||
BDE::BDE(Integer integer):_type(TYPE_INTEGER),
|
|
||||||
_bobject(new BInteger(integer)) {}
|
|
||||||
|
|
||||||
|
|
||||||
BDE::BDE(const std::string& string):_type(TYPE_STRING),
|
|
||||||
_bobject(new BString(std::string(string))) {}
|
|
||||||
|
|
||||||
BDE::BDE(const char* cstring):_type(TYPE_STRING),
|
|
||||||
_bobject(new BString(std::string(cstring))) {}
|
|
||||||
|
|
||||||
BDE::BDE(const char* data, size_t length):
|
|
||||||
_type(TYPE_STRING),
|
|
||||||
_bobject(new BString(std::string(&data[0], &data[length]))) {}
|
|
||||||
|
|
||||||
BDE::BDE(const unsigned char* data, size_t length):
|
|
||||||
_type(TYPE_STRING),
|
|
||||||
_bobject(new BString(std::string(&data[0], &data[length]))) {}
|
|
||||||
|
|
||||||
BDE BDE::dict()
|
|
||||||
{
|
|
||||||
BDE bde;
|
|
||||||
bde._type = TYPE_DICT;
|
|
||||||
bde._bobject.reset(new BDict());
|
|
||||||
return bde;
|
|
||||||
}
|
|
||||||
|
|
||||||
BDE BDE::list()
|
|
||||||
{
|
|
||||||
BDE bde;
|
|
||||||
bde._type = TYPE_LIST;
|
|
||||||
bde._bobject.reset(new BList());
|
|
||||||
return bde;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test for Null data
|
|
||||||
bool BDE::isNone() const
|
|
||||||
{
|
|
||||||
return _type == TYPE_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Integer Interface
|
|
||||||
|
|
||||||
bool BDE::isInteger() const
|
|
||||||
{
|
|
||||||
return _type == TYPE_INTEGER;
|
|
||||||
}
|
|
||||||
|
|
||||||
BDE::Integer BDE::i() const
|
|
||||||
{
|
|
||||||
return _bobject->i();
|
|
||||||
}
|
|
||||||
|
|
||||||
// String Interface
|
|
||||||
|
|
||||||
bool BDE::isString() const
|
|
||||||
{
|
|
||||||
return _type == TYPE_STRING;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string& BDE::s() const
|
|
||||||
{
|
|
||||||
return _bobject->s();
|
|
||||||
}
|
|
||||||
|
|
||||||
const unsigned char* BDE::uc() const
|
|
||||||
{
|
|
||||||
return _bobject->uc();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Dictionary Interface
|
|
||||||
|
|
||||||
bool BDE::isDict() const
|
|
||||||
{
|
|
||||||
return _type == TYPE_DICT;
|
|
||||||
}
|
|
||||||
|
|
||||||
BDE& BDE::operator[](const std::string& key)
|
|
||||||
{
|
|
||||||
return _bobject->operator[](key);
|
|
||||||
}
|
|
||||||
|
|
||||||
const BDE& BDE::operator[](const std::string& key) const
|
|
||||||
{
|
|
||||||
if(_bobject->containsKey(key)) {
|
|
||||||
return _bobject->operator[](key);
|
|
||||||
} else {
|
|
||||||
return none;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool BDE::containsKey(const std::string& key) const
|
|
||||||
{
|
|
||||||
return _bobject->containsKey(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
void BDE::removeKey(const std::string& key)
|
|
||||||
{
|
|
||||||
_bobject->removeKey(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
BDE::Dict::iterator BDE::dictBegin()
|
|
||||||
{
|
|
||||||
return _bobject->dictBegin();
|
|
||||||
}
|
|
||||||
|
|
||||||
BDE::Dict::const_iterator BDE::dictBegin() const
|
|
||||||
{
|
|
||||||
return _bobject->dictBegin();
|
|
||||||
}
|
|
||||||
|
|
||||||
BDE::Dict::iterator BDE::dictEnd()
|
|
||||||
{
|
|
||||||
return _bobject->dictEnd();
|
|
||||||
}
|
|
||||||
|
|
||||||
BDE::Dict::const_iterator BDE::dictEnd() const
|
|
||||||
{
|
|
||||||
return _bobject->dictEnd();
|
|
||||||
}
|
|
||||||
|
|
||||||
// List Interface
|
|
||||||
|
|
||||||
bool BDE::isList() const
|
|
||||||
{
|
|
||||||
return _type == TYPE_LIST;
|
|
||||||
}
|
|
||||||
|
|
||||||
void BDE::append(const BDE& bde)
|
|
||||||
{
|
|
||||||
_bobject->append(bde);
|
|
||||||
}
|
|
||||||
|
|
||||||
void BDE::operator<<(const BDE& bde)
|
|
||||||
{
|
|
||||||
_bobject->operator<<(bde);
|
|
||||||
}
|
|
||||||
|
|
||||||
BDE& BDE::operator[](size_t index)
|
|
||||||
{
|
|
||||||
return _bobject->operator[](index);
|
|
||||||
}
|
|
||||||
|
|
||||||
const BDE& BDE::operator[](size_t index) const
|
|
||||||
{
|
|
||||||
return _bobject->operator[](index);
|
|
||||||
}
|
|
||||||
|
|
||||||
BDE::List::iterator BDE::listBegin()
|
|
||||||
{
|
|
||||||
return _bobject->listBegin();
|
|
||||||
}
|
|
||||||
|
|
||||||
BDE::List::const_iterator BDE::listBegin() const
|
|
||||||
{
|
|
||||||
return _bobject->listBegin();
|
|
||||||
}
|
|
||||||
|
|
||||||
BDE::List::iterator BDE::listEnd()
|
|
||||||
{
|
|
||||||
return _bobject->listEnd();
|
|
||||||
}
|
|
||||||
|
|
||||||
BDE::List::const_iterator BDE::listEnd() const
|
|
||||||
{
|
|
||||||
return _bobject->listEnd();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Callable from List and Dict
|
|
||||||
size_t BDE::size() const
|
|
||||||
{
|
|
||||||
return _bobject->size();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Callable from List and Dict
|
|
||||||
bool BDE::empty() const
|
|
||||||
{
|
|
||||||
return _bobject->empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
static BDE decodeiter(std::istream& ss);
|
static BDE decodeiter(std::istream& ss);
|
||||||
|
|
||||||
static void checkdelim(std::istream& ss, const char delim = ':')
|
static void checkdelim(std::istream& ss, const char delim = ':')
|
||||||
|
|
386
src/bencode.h
386
src/bencode.h
|
@ -38,398 +38,14 @@
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <map>
|
|
||||||
#include <deque>
|
|
||||||
#include <iosfwd>
|
#include <iosfwd>
|
||||||
|
|
||||||
#include "SharedHandle.h"
|
#include "BDE.h"
|
||||||
#include "A2STR.h"
|
|
||||||
#include "RecoverableException.h"
|
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
namespace bencode {
|
namespace bencode {
|
||||||
|
|
||||||
class BDE;
|
|
||||||
|
|
||||||
class BDE {
|
|
||||||
public:
|
|
||||||
typedef std::map<std::string, BDE> Dict;
|
|
||||||
typedef std::deque<BDE> List;
|
|
||||||
typedef int64_t Integer;
|
|
||||||
private:
|
|
||||||
enum TYPE{
|
|
||||||
TYPE_NONE,
|
|
||||||
TYPE_INTEGER,
|
|
||||||
TYPE_STRING,
|
|
||||||
TYPE_DICT,
|
|
||||||
TYPE_LIST,
|
|
||||||
};
|
|
||||||
|
|
||||||
class BObject {
|
|
||||||
public:
|
|
||||||
virtual ~BObject() {}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Integer Interface
|
|
||||||
|
|
||||||
// Returns Integer.
|
|
||||||
virtual Integer i() const
|
|
||||||
{
|
|
||||||
throw RecoverableException("Not Integer");
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////
|
|
||||||
// String Interface
|
|
||||||
|
|
||||||
// Returns std::string.
|
|
||||||
virtual const std::string& s() const
|
|
||||||
{
|
|
||||||
throw RecoverableException("Not String");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns std::string.data() casted to unsigned char*.
|
|
||||||
// Use s().size() to get length.
|
|
||||||
virtual const unsigned char* uc() const
|
|
||||||
{
|
|
||||||
throw RecoverableException("Not String");
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Dictionary Interface
|
|
||||||
|
|
||||||
// Returns the reference to BDE object associated with given key.
|
|
||||||
// If the key is not found, new pair with that key is created
|
|
||||||
// using default values, which is then returned. In other words,
|
|
||||||
// this is the same behavior of std::map's operator[].
|
|
||||||
virtual BDE& operator[](const std::string& key)
|
|
||||||
{
|
|
||||||
throw RecoverableException("Not Dict");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns true if the given key is found in dict.
|
|
||||||
virtual bool containsKey(const std::string& key) const
|
|
||||||
{
|
|
||||||
throw RecoverableException("Not Dict");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Removes specified key from dict.
|
|
||||||
virtual void removeKey(const std::string& key)
|
|
||||||
{
|
|
||||||
throw RecoverableException("Not Dict");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns a read/write iterator that points to the first pair in
|
|
||||||
// the dict.
|
|
||||||
virtual Dict::iterator dictBegin()
|
|
||||||
{
|
|
||||||
throw RecoverableException("Not Dict");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns a read/write read-only iterator that points to one past
|
|
||||||
// the last pair in the dict.
|
|
||||||
virtual Dict::iterator dictEnd()
|
|
||||||
{
|
|
||||||
throw RecoverableException("Not Dict");
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////
|
|
||||||
// List Interface
|
|
||||||
|
|
||||||
// Appends given bde to list.
|
|
||||||
virtual void append(const BDE& bde)
|
|
||||||
{
|
|
||||||
throw RecoverableException("Not List");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Alias for append()
|
|
||||||
virtual void operator<<(const BDE& bde)
|
|
||||||
{
|
|
||||||
throw RecoverableException("Not List");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns the reference of the object at the given index.
|
|
||||||
virtual BDE& operator[](size_t index)
|
|
||||||
{
|
|
||||||
throw RecoverableException("Not List");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns a read/write iterator that points to the first object
|
|
||||||
// in list.
|
|
||||||
virtual List::iterator listBegin()
|
|
||||||
{
|
|
||||||
throw RecoverableException("Not List");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns a read/write iterator that points to the one past the
|
|
||||||
// last object in list.
|
|
||||||
virtual List::iterator listEnd()
|
|
||||||
{
|
|
||||||
throw RecoverableException("Not List");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns size of list or dict.
|
|
||||||
virtual size_t size() const
|
|
||||||
{
|
|
||||||
throw RecoverableException("Neither Dict nor List");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns true if size of list or dict is 0.
|
|
||||||
virtual bool empty() const
|
|
||||||
{
|
|
||||||
throw RecoverableException("Neither Dict nor List");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class BInteger : public BObject {
|
|
||||||
private:
|
|
||||||
Integer _integer;
|
|
||||||
public:
|
|
||||||
BInteger(Integer i):_integer(i) {}
|
|
||||||
|
|
||||||
virtual BDE::Integer i() const
|
|
||||||
{
|
|
||||||
return _integer;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class BString : public BObject {
|
|
||||||
private:
|
|
||||||
std::string _string;
|
|
||||||
public:
|
|
||||||
BString(const std::string& string):_string(string) {}
|
|
||||||
|
|
||||||
virtual const std::string& s() const
|
|
||||||
{
|
|
||||||
return _string;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual const unsigned char* uc() const
|
|
||||||
{
|
|
||||||
return reinterpret_cast<const unsigned char*>(_string.data());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class BDict : public BObject {
|
|
||||||
private:
|
|
||||||
Dict _dict;
|
|
||||||
public:
|
|
||||||
|
|
||||||
virtual BDE& operator[](const std::string& key)
|
|
||||||
{
|
|
||||||
return _dict[key];
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual bool containsKey(const std::string& key) const
|
|
||||||
{
|
|
||||||
return _dict.find(key) != _dict.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void removeKey(const std::string& key)
|
|
||||||
{
|
|
||||||
_dict.erase(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual BDE::Dict::iterator dictBegin()
|
|
||||||
{
|
|
||||||
return _dict.begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual BDE::Dict::iterator dictEnd()
|
|
||||||
{
|
|
||||||
return _dict.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual size_t size() const
|
|
||||||
{
|
|
||||||
return _dict.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual bool empty() const
|
|
||||||
{
|
|
||||||
return _dict.empty();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class BList : public BObject {
|
|
||||||
private:
|
|
||||||
List _list;
|
|
||||||
public:
|
|
||||||
virtual void append(const BDE& bde)
|
|
||||||
{
|
|
||||||
_list.push_back(bde);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void operator<<(const BDE& bde)
|
|
||||||
{
|
|
||||||
_list.push_back(bde);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual BDE& operator[](size_t index)
|
|
||||||
{
|
|
||||||
return _list[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual BDE::List::iterator listBegin()
|
|
||||||
{
|
|
||||||
return _list.begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual BDE::List::iterator listEnd()
|
|
||||||
{
|
|
||||||
return _list.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual size_t size() const
|
|
||||||
{
|
|
||||||
return _list.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual bool empty() const
|
|
||||||
{
|
|
||||||
return _list.empty();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
TYPE _type;
|
|
||||||
|
|
||||||
SharedHandle<BObject> _bobject;
|
|
||||||
public:
|
|
||||||
BDE();
|
|
||||||
|
|
||||||
static BDE dict();
|
|
||||||
|
|
||||||
static BDE list();
|
|
||||||
|
|
||||||
static const BDE none;
|
|
||||||
|
|
||||||
// Test for Null data
|
|
||||||
// Return true if the type of this object is None.
|
|
||||||
bool isNone() const;
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Integer Interface
|
|
||||||
|
|
||||||
BDE(Integer integer);
|
|
||||||
|
|
||||||
// Returns true if the type of this object is Integer.
|
|
||||||
bool isInteger() const;
|
|
||||||
|
|
||||||
// Returns Integer. Requires this object to be Integer.
|
|
||||||
Integer i() const;
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
|
||||||
// String Interface
|
|
||||||
|
|
||||||
BDE(const std::string& string);
|
|
||||||
|
|
||||||
// Made explicit to avoid ambiguity with BDE(Integer).
|
|
||||||
explicit BDE(const char* cstring);
|
|
||||||
|
|
||||||
BDE(const char* data, size_t length);
|
|
||||||
|
|
||||||
BDE(const unsigned char* data, size_t length);
|
|
||||||
|
|
||||||
// Returns true if the type of this object is String.
|
|
||||||
bool isString() const;
|
|
||||||
|
|
||||||
// Returns std::string. Requires this object to be String
|
|
||||||
const std::string& s() const;
|
|
||||||
|
|
||||||
// Returns std::string.data() casted to unsigned char*.
|
|
||||||
// Use s().size() to get length.
|
|
||||||
const unsigned char* uc() const;
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Dictionary Interface
|
|
||||||
|
|
||||||
// Returns true if the type of this object is Dict.
|
|
||||||
bool isDict() const;
|
|
||||||
|
|
||||||
// Returns the reference to BDE object associated with given key.
|
|
||||||
// If the key is not found, new pair with that key is created using default
|
|
||||||
// values, which is then returned. In other words, this is the same behavior
|
|
||||||
// of std::map's operator[].
|
|
||||||
// Requires this object to be Dict.
|
|
||||||
BDE& operator[](const std::string& key);
|
|
||||||
|
|
||||||
// Returns the const reference to BDE ojbect associated with given key.
|
|
||||||
// If the key is not found, BDE::none is returned.
|
|
||||||
// Requires this object to be Dict.
|
|
||||||
const BDE& operator[](const std::string& key) const;
|
|
||||||
|
|
||||||
// Returns true if the given key is found in dict.
|
|
||||||
// Requires this object to be Dict.
|
|
||||||
bool containsKey(const std::string& key) const;
|
|
||||||
|
|
||||||
// Removes specified key from dict.
|
|
||||||
// Requires this object to be Dict.
|
|
||||||
void removeKey(const std::string& key);
|
|
||||||
|
|
||||||
// Returns a read/write iterator that points to the first pair in the dict.
|
|
||||||
// Requires this object to be Dict.
|
|
||||||
Dict::iterator dictBegin();
|
|
||||||
|
|
||||||
// Returns a read/write read-only iterator that points to the first pair in
|
|
||||||
// the dict.
|
|
||||||
// Requires this object to be Dict.
|
|
||||||
Dict::const_iterator dictBegin() const;
|
|
||||||
|
|
||||||
// Returns a read/write read-only iterator that points to one past the last
|
|
||||||
// pair in the dict.
|
|
||||||
// Requires this object to be Dict.
|
|
||||||
Dict::iterator dictEnd();
|
|
||||||
|
|
||||||
// Returns a read/write read-only iterator that points to one past the last
|
|
||||||
// pair in the dict.
|
|
||||||
// Requires this object to be Dict.
|
|
||||||
Dict::const_iterator dictEnd() const;
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
|
||||||
// List Interface
|
|
||||||
|
|
||||||
// Returns true if the type of this object is List.
|
|
||||||
bool isList() const;
|
|
||||||
|
|
||||||
// Appends given bde to list. Required the type of this object to be List.
|
|
||||||
void append(const BDE& bde);
|
|
||||||
|
|
||||||
// Alias for append()
|
|
||||||
void operator<<(const BDE& bde);
|
|
||||||
|
|
||||||
// Returns the reference of the object at the given index. Required this
|
|
||||||
// object to be List.
|
|
||||||
BDE& operator[](size_t index);
|
|
||||||
|
|
||||||
// Returns the const reference of the object at the given index.
|
|
||||||
// Required this object to be List.
|
|
||||||
const BDE& operator[](size_t index) const;
|
|
||||||
|
|
||||||
// Returns a read/write iterator that points to the first object in list.
|
|
||||||
// Required this object to be List.
|
|
||||||
List::iterator listBegin();
|
|
||||||
|
|
||||||
// Returns a read/write read-only iterator that points to the first object
|
|
||||||
// in list. Required this object to be List.
|
|
||||||
List::const_iterator listBegin() const;
|
|
||||||
|
|
||||||
// Returns a read/write iterator that points to the one past the last object
|
|
||||||
// in list. Required this object to be List.
|
|
||||||
List::iterator listEnd();
|
|
||||||
|
|
||||||
// Returns a read/write read-only iterator that points to the one past the
|
|
||||||
// last object in list. Required this object to be List.
|
|
||||||
List::const_iterator listEnd() const;
|
|
||||||
|
|
||||||
// For List type: Returns size of list.
|
|
||||||
// For Dict type: Returns size of dict.
|
|
||||||
size_t size() const;
|
|
||||||
|
|
||||||
// For List type: Returns true if size of list is 0.
|
|
||||||
// For Dict type: Returns true if size of dict is 0.
|
|
||||||
bool empty() const;
|
|
||||||
};
|
|
||||||
|
|
||||||
BDE decode(std::istream& in);
|
BDE decode(std::istream& in);
|
||||||
|
|
||||||
// Decode the data in s.
|
// Decode the data in s.
|
||||||
|
|
|
@ -44,7 +44,7 @@ CPPUNIT_TEST_SUITE_REGISTRATION( AnnounceListTest );
|
||||||
|
|
||||||
void AnnounceListTest::testSingleElementList() {
|
void AnnounceListTest::testSingleElementList() {
|
||||||
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
|
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
|
||||||
const bencode::BDE announcesList = bencode::decode(peersString);
|
const BDE announcesList = bencode::decode(peersString);
|
||||||
|
|
||||||
// ANNOUNCE_LIST
|
// ANNOUNCE_LIST
|
||||||
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
|
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
|
||||||
|
@ -90,7 +90,7 @@ void AnnounceListTest::testSingleElementList() {
|
||||||
|
|
||||||
void AnnounceListTest::testMultiElementList() {
|
void AnnounceListTest::testMultiElementList() {
|
||||||
std::string peersString = "ll8:tracker18:tracker28:tracker3ee";
|
std::string peersString = "ll8:tracker18:tracker28:tracker3ee";
|
||||||
const bencode::BDE announcesList = bencode::decode(peersString);
|
const BDE announcesList = bencode::decode(peersString);
|
||||||
|
|
||||||
// ANNOUNCE_LIST
|
// ANNOUNCE_LIST
|
||||||
// [ [ tracker1, tracker2, tracker3 ] ]
|
// [ [ tracker1, tracker2, tracker3 ] ]
|
||||||
|
@ -123,7 +123,7 @@ void AnnounceListTest::testMultiElementList() {
|
||||||
|
|
||||||
void AnnounceListTest::testSingleAndMulti() {
|
void AnnounceListTest::testSingleAndMulti() {
|
||||||
std::string peersString = "ll8:tracker18:tracker2el8:tracker3ee";
|
std::string peersString = "ll8:tracker18:tracker2el8:tracker3ee";
|
||||||
const bencode::BDE announcesList = bencode::decode(peersString);
|
const BDE announcesList = bencode::decode(peersString);
|
||||||
|
|
||||||
// ANNOUNCE_LIST
|
// ANNOUNCE_LIST
|
||||||
// [ [ tracker1, tracker2 ], [ tracker3 ] ]
|
// [ [ tracker1, tracker2 ], [ tracker3 ] ]
|
||||||
|
@ -149,7 +149,7 @@ void AnnounceListTest::testSingleAndMulti() {
|
||||||
|
|
||||||
void AnnounceListTest::testNoGroup() {
|
void AnnounceListTest::testNoGroup() {
|
||||||
std::string peersString = "llee";
|
std::string peersString = "llee";
|
||||||
const bencode::BDE announcesList = bencode::decode(peersString);
|
const BDE announcesList = bencode::decode(peersString);
|
||||||
|
|
||||||
AnnounceList announceList(announcesList);
|
AnnounceList announceList(announcesList);
|
||||||
|
|
||||||
|
@ -158,7 +158,7 @@ void AnnounceListTest::testNoGroup() {
|
||||||
|
|
||||||
void AnnounceListTest::testNextEventIfAfterStarted() {
|
void AnnounceListTest::testNextEventIfAfterStarted() {
|
||||||
std::string peersString = "ll8:tracker1ee";
|
std::string peersString = "ll8:tracker1ee";
|
||||||
const bencode::BDE announcesList = bencode::decode(peersString);
|
const BDE announcesList = bencode::decode(peersString);
|
||||||
|
|
||||||
// ANNOUNCE_LIST
|
// ANNOUNCE_LIST
|
||||||
// [ [ tracker1 ] ]
|
// [ [ tracker1 ] ]
|
||||||
|
@ -178,7 +178,7 @@ void AnnounceListTest::testNextEventIfAfterStarted() {
|
||||||
|
|
||||||
void AnnounceListTest::testEvent() {
|
void AnnounceListTest::testEvent() {
|
||||||
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
|
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
|
||||||
const bencode::BDE announcesList = bencode::decode(peersString);
|
const BDE announcesList = bencode::decode(peersString);
|
||||||
|
|
||||||
// ANNOUNCE_LIST
|
// ANNOUNCE_LIST
|
||||||
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
|
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
|
||||||
|
@ -202,7 +202,7 @@ void AnnounceListTest::testEvent() {
|
||||||
|
|
||||||
void AnnounceListTest::testCountStoppedAllowedTier() {
|
void AnnounceListTest::testCountStoppedAllowedTier() {
|
||||||
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
|
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
|
||||||
const bencode::BDE announcesList = bencode::decode(peersString);
|
const BDE announcesList = bencode::decode(peersString);
|
||||||
|
|
||||||
// ANNOUNCE_LIST
|
// ANNOUNCE_LIST
|
||||||
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
|
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
|
||||||
|
@ -229,7 +229,7 @@ void AnnounceListTest::testCountStoppedAllowedTier() {
|
||||||
|
|
||||||
void AnnounceListTest::testCountCompletedAllowedTier() {
|
void AnnounceListTest::testCountCompletedAllowedTier() {
|
||||||
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
|
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
|
||||||
const bencode::BDE announcesList = bencode::decode(peersString);
|
const BDE announcesList = bencode::decode(peersString);
|
||||||
|
|
||||||
// ANNOUNCE_LIST
|
// ANNOUNCE_LIST
|
||||||
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
|
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
|
||||||
|
|
|
@ -0,0 +1,149 @@
|
||||||
|
#include "BDE.h"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
#include <cppunit/extensions/HelperMacros.h>
|
||||||
|
|
||||||
|
#include "Util.h"
|
||||||
|
|
||||||
|
namespace aria2 {
|
||||||
|
|
||||||
|
class BDETest:public CppUnit::TestFixture {
|
||||||
|
|
||||||
|
CPPUNIT_TEST_SUITE(BDETest);
|
||||||
|
CPPUNIT_TEST(testString);
|
||||||
|
CPPUNIT_TEST(testInteger);
|
||||||
|
CPPUNIT_TEST(testDict);
|
||||||
|
CPPUNIT_TEST(testDictIter);
|
||||||
|
CPPUNIT_TEST(testList);
|
||||||
|
CPPUNIT_TEST(testListIter);
|
||||||
|
CPPUNIT_TEST_SUITE_END();
|
||||||
|
private:
|
||||||
|
|
||||||
|
public:
|
||||||
|
void testString();
|
||||||
|
void testInteger();
|
||||||
|
void testDict();
|
||||||
|
void testDictIter();
|
||||||
|
void testList();
|
||||||
|
void testListIter();
|
||||||
|
void testDecode();
|
||||||
|
void testEncode();
|
||||||
|
};
|
||||||
|
|
||||||
|
CPPUNIT_TEST_SUITE_REGISTRATION( BDETest );
|
||||||
|
|
||||||
|
void BDETest::testString()
|
||||||
|
{
|
||||||
|
BDE s(std::string("aria2"));
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), s.s());
|
||||||
|
|
||||||
|
unsigned char dataWithNull[] = { 0xf0, '\0', 0x0f };
|
||||||
|
BDE sWithNull(dataWithNull, sizeof(dataWithNull));
|
||||||
|
CPPUNIT_ASSERT(memcmp(dataWithNull, sWithNull.s().c_str(),
|
||||||
|
sizeof(dataWithNull)) == 0);
|
||||||
|
|
||||||
|
BDE zero("");
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string(""), zero.s());
|
||||||
|
|
||||||
|
const unsigned char uc[] = { 0x08, 0x19, 0x2a, 0x3b };
|
||||||
|
BDE data(uc, sizeof(uc));
|
||||||
|
CPPUNIT_ASSERT_EQUAL(Util::toHex(uc, sizeof(uc)),
|
||||||
|
Util::toHex(data.uc(), data.s().size()));
|
||||||
|
}
|
||||||
|
|
||||||
|
void BDETest::testInteger()
|
||||||
|
{
|
||||||
|
BDE integer(INT64_MAX);
|
||||||
|
CPPUNIT_ASSERT_EQUAL(INT64_MAX, integer.i());
|
||||||
|
}
|
||||||
|
|
||||||
|
void BDETest::testDict()
|
||||||
|
{
|
||||||
|
BDE dict = BDE::dict();
|
||||||
|
CPPUNIT_ASSERT(dict.empty());
|
||||||
|
|
||||||
|
dict["ki"] = 7;
|
||||||
|
dict["ks"] = std::string("abc");
|
||||||
|
|
||||||
|
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), dict.size());
|
||||||
|
CPPUNIT_ASSERT(dict.containsKey("ki"));
|
||||||
|
CPPUNIT_ASSERT_EQUAL(static_cast<BDE::Integer>(7), dict["ki"].i());
|
||||||
|
CPPUNIT_ASSERT(dict.containsKey("ks"));
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("abc"), dict["ks"].s());
|
||||||
|
|
||||||
|
CPPUNIT_ASSERT(dict["kn"].isNone()); // This adds kn key with default value.
|
||||||
|
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), dict.size());
|
||||||
|
CPPUNIT_ASSERT(dict.containsKey("kn"));
|
||||||
|
|
||||||
|
const BDE& ref = dict;
|
||||||
|
ref["kn2"]; // This doesn't add kn2 key.
|
||||||
|
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), ref.size());
|
||||||
|
CPPUNIT_ASSERT(!ref.containsKey("kn2"));
|
||||||
|
|
||||||
|
dict.removeKey("kn");
|
||||||
|
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), dict.size());
|
||||||
|
CPPUNIT_ASSERT(!dict.containsKey("kn"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void BDETest::testDictIter()
|
||||||
|
{
|
||||||
|
BDE dict = BDE::dict();
|
||||||
|
dict["alpha2"] = std::string("alpha2");
|
||||||
|
dict["charlie"] = std::string("charlie");
|
||||||
|
dict["bravo"] = std::string("bravo");
|
||||||
|
dict["alpha"] = std::string("alpha");
|
||||||
|
|
||||||
|
BDE::Dict::iterator i = dict.dictBegin();
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("alpha"), (*i++).first);
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("alpha2"), (*i++).first);
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("bravo"), (*i++).first);
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("charlie"), (*i++).first);
|
||||||
|
CPPUNIT_ASSERT(dict.dictEnd() == i);
|
||||||
|
|
||||||
|
const BDE& ref = dict;
|
||||||
|
BDE::Dict::const_iterator ci = ref.dictBegin();
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("alpha"), (*ci++).first);
|
||||||
|
std::advance(ci, 3);
|
||||||
|
CPPUNIT_ASSERT(ref.dictEnd() == ci);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BDETest::testList()
|
||||||
|
{
|
||||||
|
BDE list = BDE::list();
|
||||||
|
CPPUNIT_ASSERT(list.empty());
|
||||||
|
list << 7;
|
||||||
|
list << std::string("aria2");
|
||||||
|
|
||||||
|
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), list.size());
|
||||||
|
CPPUNIT_ASSERT_EQUAL(static_cast<BDE::Integer>(7), list[0].i());
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), list[1].s());
|
||||||
|
|
||||||
|
const BDE& ref = list;
|
||||||
|
CPPUNIT_ASSERT_EQUAL(static_cast<BDE::Integer>(7), ref[0].i());
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), ref[1].s());
|
||||||
|
}
|
||||||
|
|
||||||
|
void BDETest::testListIter()
|
||||||
|
{
|
||||||
|
BDE list = BDE::list();
|
||||||
|
list << std::string("alpha2");
|
||||||
|
list << std::string("charlie");
|
||||||
|
list << std::string("bravo");
|
||||||
|
list << std::string("alpha");
|
||||||
|
|
||||||
|
BDE::List::iterator i = list.listBegin();
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("alpha2"), (*i++).s());
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("charlie"), (*i++).s());
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("bravo"), (*i++).s());
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("alpha"), (*i++).s());
|
||||||
|
CPPUNIT_ASSERT(list.listEnd() == i);
|
||||||
|
|
||||||
|
const BDE& ref = list;
|
||||||
|
BDE::List::const_iterator ci = ref.listBegin();
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("alpha2"), (*ci++).s());
|
||||||
|
std::advance(ci, 3);
|
||||||
|
CPPUNIT_ASSERT(ref.listEnd() == ci);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace aria2
|
|
@ -1,11 +1,7 @@
|
||||||
#include "bencode.h"
|
#include "bencode.h"
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
#include <cppunit/extensions/HelperMacros.h>
|
#include <cppunit/extensions/HelperMacros.h>
|
||||||
|
|
||||||
#include "Util.h"
|
|
||||||
#include "RecoverableException.h"
|
#include "RecoverableException.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
@ -13,155 +9,29 @@ namespace aria2 {
|
||||||
class BencodeTest:public CppUnit::TestFixture {
|
class BencodeTest:public CppUnit::TestFixture {
|
||||||
|
|
||||||
CPPUNIT_TEST_SUITE(BencodeTest);
|
CPPUNIT_TEST_SUITE(BencodeTest);
|
||||||
CPPUNIT_TEST(testString);
|
|
||||||
CPPUNIT_TEST(testInteger);
|
|
||||||
CPPUNIT_TEST(testDict);
|
|
||||||
CPPUNIT_TEST(testDictIter);
|
|
||||||
CPPUNIT_TEST(testList);
|
|
||||||
CPPUNIT_TEST(testListIter);
|
|
||||||
CPPUNIT_TEST(testDecode);
|
CPPUNIT_TEST(testDecode);
|
||||||
CPPUNIT_TEST(testEncode);
|
CPPUNIT_TEST(testEncode);
|
||||||
CPPUNIT_TEST_SUITE_END();
|
CPPUNIT_TEST_SUITE_END();
|
||||||
private:
|
private:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void testString();
|
|
||||||
void testInteger();
|
|
||||||
void testDict();
|
|
||||||
void testDictIter();
|
|
||||||
void testList();
|
|
||||||
void testListIter();
|
|
||||||
void testDecode();
|
void testDecode();
|
||||||
void testEncode();
|
void testEncode();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
CPPUNIT_TEST_SUITE_REGISTRATION( BencodeTest );
|
CPPUNIT_TEST_SUITE_REGISTRATION( BencodeTest );
|
||||||
|
|
||||||
void BencodeTest::testString()
|
|
||||||
{
|
|
||||||
bencode::BDE s(std::string("aria2"));
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), s.s());
|
|
||||||
|
|
||||||
unsigned char dataWithNull[] = { 0xf0, '\0', 0x0f };
|
|
||||||
bencode::BDE sWithNull(dataWithNull, sizeof(dataWithNull));
|
|
||||||
CPPUNIT_ASSERT(memcmp(dataWithNull, sWithNull.s().c_str(),
|
|
||||||
sizeof(dataWithNull)) == 0);
|
|
||||||
|
|
||||||
bencode::BDE zero("");
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string(""), zero.s());
|
|
||||||
|
|
||||||
const unsigned char uc[] = { 0x08, 0x19, 0x2a, 0x3b };
|
|
||||||
bencode::BDE data(uc, sizeof(uc));
|
|
||||||
CPPUNIT_ASSERT_EQUAL(Util::toHex(uc, sizeof(uc)),
|
|
||||||
Util::toHex(data.uc(), data.s().size()));
|
|
||||||
}
|
|
||||||
|
|
||||||
void BencodeTest::testInteger()
|
|
||||||
{
|
|
||||||
bencode::BDE integer(INT64_MAX);
|
|
||||||
CPPUNIT_ASSERT_EQUAL(INT64_MAX, integer.i());
|
|
||||||
}
|
|
||||||
|
|
||||||
void BencodeTest::testDict()
|
|
||||||
{
|
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
|
||||||
CPPUNIT_ASSERT(dict.empty());
|
|
||||||
|
|
||||||
dict["ki"] = 7;
|
|
||||||
dict["ks"] = std::string("abc");
|
|
||||||
|
|
||||||
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), dict.size());
|
|
||||||
CPPUNIT_ASSERT(dict.containsKey("ki"));
|
|
||||||
CPPUNIT_ASSERT_EQUAL(static_cast<bencode::BDE::Integer>(7), dict["ki"].i());
|
|
||||||
CPPUNIT_ASSERT(dict.containsKey("ks"));
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("abc"), dict["ks"].s());
|
|
||||||
|
|
||||||
CPPUNIT_ASSERT(dict["kn"].isNone()); // This adds kn key with default value.
|
|
||||||
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), dict.size());
|
|
||||||
CPPUNIT_ASSERT(dict.containsKey("kn"));
|
|
||||||
|
|
||||||
const bencode::BDE& ref = dict;
|
|
||||||
ref["kn2"]; // This doesn't add kn2 key.
|
|
||||||
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), ref.size());
|
|
||||||
CPPUNIT_ASSERT(!ref.containsKey("kn2"));
|
|
||||||
|
|
||||||
dict.removeKey("kn");
|
|
||||||
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), dict.size());
|
|
||||||
CPPUNIT_ASSERT(!dict.containsKey("kn"));
|
|
||||||
}
|
|
||||||
|
|
||||||
void BencodeTest::testDictIter()
|
|
||||||
{
|
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
|
||||||
dict["alpha2"] = std::string("alpha2");
|
|
||||||
dict["charlie"] = std::string("charlie");
|
|
||||||
dict["bravo"] = std::string("bravo");
|
|
||||||
dict["alpha"] = std::string("alpha");
|
|
||||||
|
|
||||||
bencode::BDE::Dict::iterator i = dict.dictBegin();
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("alpha"), (*i++).first);
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("alpha2"), (*i++).first);
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("bravo"), (*i++).first);
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("charlie"), (*i++).first);
|
|
||||||
CPPUNIT_ASSERT(dict.dictEnd() == i);
|
|
||||||
|
|
||||||
const bencode::BDE& ref = dict;
|
|
||||||
bencode::BDE::Dict::const_iterator ci = ref.dictBegin();
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("alpha"), (*ci++).first);
|
|
||||||
std::advance(ci, 3);
|
|
||||||
CPPUNIT_ASSERT(ref.dictEnd() == ci);
|
|
||||||
}
|
|
||||||
|
|
||||||
void BencodeTest::testList()
|
|
||||||
{
|
|
||||||
bencode::BDE list = bencode::BDE::list();
|
|
||||||
CPPUNIT_ASSERT(list.empty());
|
|
||||||
list << 7;
|
|
||||||
list << std::string("aria2");
|
|
||||||
|
|
||||||
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), list.size());
|
|
||||||
CPPUNIT_ASSERT_EQUAL(static_cast<bencode::BDE::Integer>(7), list[0].i());
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), list[1].s());
|
|
||||||
|
|
||||||
const bencode::BDE& ref = list;
|
|
||||||
CPPUNIT_ASSERT_EQUAL(static_cast<bencode::BDE::Integer>(7), ref[0].i());
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), ref[1].s());
|
|
||||||
}
|
|
||||||
|
|
||||||
void BencodeTest::testListIter()
|
|
||||||
{
|
|
||||||
bencode::BDE list = bencode::BDE::list();
|
|
||||||
list << std::string("alpha2");
|
|
||||||
list << std::string("charlie");
|
|
||||||
list << std::string("bravo");
|
|
||||||
list << std::string("alpha");
|
|
||||||
|
|
||||||
bencode::BDE::List::iterator i = list.listBegin();
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("alpha2"), (*i++).s());
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("charlie"), (*i++).s());
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("bravo"), (*i++).s());
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("alpha"), (*i++).s());
|
|
||||||
CPPUNIT_ASSERT(list.listEnd() == i);
|
|
||||||
|
|
||||||
const bencode::BDE& ref = list;
|
|
||||||
bencode::BDE::List::const_iterator ci = ref.listBegin();
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("alpha2"), (*ci++).s());
|
|
||||||
std::advance(ci, 3);
|
|
||||||
CPPUNIT_ASSERT(ref.listEnd() == ci);
|
|
||||||
}
|
|
||||||
|
|
||||||
void BencodeTest::testDecode()
|
void BencodeTest::testDecode()
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
// string, integer and list in dict
|
// string, integer and list in dict
|
||||||
bencode::BDE dict =
|
BDE dict =
|
||||||
bencode::decode("d4:name5:aria24:sizei12345678900e5:filesl3:bin3:docee");
|
bencode::decode("d4:name5:aria24:sizei12345678900e5:filesl3:bin3:docee");
|
||||||
CPPUNIT_ASSERT(dict.isDict());
|
CPPUNIT_ASSERT(dict.isDict());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), dict["name"].s());
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), dict["name"].s());
|
||||||
CPPUNIT_ASSERT_EQUAL(static_cast<bencode::BDE::Integer>(12345678900LL),
|
CPPUNIT_ASSERT_EQUAL(static_cast<BDE::Integer>(12345678900LL),
|
||||||
dict["size"].i());
|
dict["size"].i());
|
||||||
bencode::BDE list = dict["files"];
|
BDE list = dict["files"];
|
||||||
CPPUNIT_ASSERT(list.isList());
|
CPPUNIT_ASSERT(list.isList());
|
||||||
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), list.size());
|
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), list.size());
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("bin"), list[0].s());
|
CPPUNIT_ASSERT_EQUAL(std::string("bin"), list[0].s());
|
||||||
|
@ -169,31 +39,31 @@ void BencodeTest::testDecode()
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
// dict in list
|
// dict in list
|
||||||
bencode::BDE list = bencode::decode("ld1:ki123eee");
|
BDE list = bencode::decode("ld1:ki123eee");
|
||||||
CPPUNIT_ASSERT(list.isList());
|
CPPUNIT_ASSERT(list.isList());
|
||||||
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), list.size());
|
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), list.size());
|
||||||
bencode::BDE dict = list[0];
|
BDE dict = list[0];
|
||||||
CPPUNIT_ASSERT(dict.isDict());
|
CPPUNIT_ASSERT(dict.isDict());
|
||||||
CPPUNIT_ASSERT_EQUAL(static_cast<bencode::BDE::Integer>(123),
|
CPPUNIT_ASSERT_EQUAL(static_cast<BDE::Integer>(123),
|
||||||
dict["k"].i());
|
dict["k"].i());
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
// empty key is allowed
|
// empty key is allowed
|
||||||
bencode::BDE s = bencode::decode("d0:1:ve");
|
BDE s = bencode::decode("d0:1:ve");
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
// empty string
|
// empty string
|
||||||
bencode::BDE s = bencode::decode("0:");
|
BDE s = bencode::decode("0:");
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string(""), s.s());
|
CPPUNIT_ASSERT_EQUAL(std::string(""), s.s());
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
// empty dict
|
// empty dict
|
||||||
bencode::BDE d = bencode::decode("de");
|
BDE d = bencode::decode("de");
|
||||||
CPPUNIT_ASSERT(d.empty());
|
CPPUNIT_ASSERT(d.empty());
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
// empty list
|
// empty list
|
||||||
bencode::BDE l = bencode::decode("le");
|
BDE l = bencode::decode("le");
|
||||||
CPPUNIT_ASSERT(l.empty());
|
CPPUNIT_ASSERT(l.empty());
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
|
@ -267,7 +137,7 @@ void BencodeTest::testDecode()
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
// ignore trailing garbage at the end of the input.
|
// ignore trailing garbage at the end of the input.
|
||||||
bencode::BDE s = bencode::decode("5:aria2trail");
|
BDE s = bencode::decode("5:aria2trail");
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), s.s());
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), s.s());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -275,12 +145,12 @@ void BencodeTest::testDecode()
|
||||||
void BencodeTest::testEncode()
|
void BencodeTest::testEncode()
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["name"] = std::string("aria2");
|
dict["name"] = std::string("aria2");
|
||||||
dict["loc"] = 80000;
|
dict["loc"] = 80000;
|
||||||
dict["files"] = bencode::BDE::list();
|
dict["files"] = BDE::list();
|
||||||
dict["files"] << std::string("aria2c");
|
dict["files"] << std::string("aria2c");
|
||||||
dict["attrs"] = bencode::BDE::dict();
|
dict["attrs"] = BDE::dict();
|
||||||
dict["attrs"]["license"] = std::string("GPL");
|
dict["attrs"]["license"] = std::string("GPL");
|
||||||
|
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("d"
|
CPPUNIT_ASSERT_EQUAL(std::string("d"
|
||||||
|
|
|
@ -60,13 +60,13 @@ void DHTAnnouncePeerMessageTest::testGetBencodedMessage()
|
||||||
|
|
||||||
std::string msgbody = msg.getBencodedMessage();
|
std::string msgbody = msg.getBencodedMessage();
|
||||||
|
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["t"] = transactionID;
|
dict["t"] = transactionID;
|
||||||
dict["y"] = bencode::BDE("q");
|
dict["y"] = BDE("q");
|
||||||
dict["q"] = bencode::BDE("announce_peer");
|
dict["q"] = BDE("announce_peer");
|
||||||
bencode::BDE aDict = bencode::BDE::dict();
|
BDE aDict = BDE::dict();
|
||||||
aDict["id"] = bencode::BDE(localNode->getID(), DHT_ID_LENGTH);
|
aDict["id"] = BDE(localNode->getID(), DHT_ID_LENGTH);
|
||||||
aDict["info_hash"] = bencode::BDE(infoHash, DHT_ID_LENGTH);
|
aDict["info_hash"] = BDE(infoHash, DHT_ID_LENGTH);
|
||||||
aDict["port"] = port;
|
aDict["port"] = port;
|
||||||
aDict["token"] = token;
|
aDict["token"] = token;
|
||||||
dict["a"] = aDict;
|
dict["a"] = aDict;
|
||||||
|
|
|
@ -39,11 +39,11 @@ void DHTAnnouncePeerReplyMessageTest::testGetBencodedMessage()
|
||||||
|
|
||||||
std::string msgbody = msg.getBencodedMessage();
|
std::string msgbody = msg.getBencodedMessage();
|
||||||
|
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["t"] = transactionID;
|
dict["t"] = transactionID;
|
||||||
dict["y"] = bencode::BDE("r");
|
dict["y"] = BDE("r");
|
||||||
bencode::BDE rDict = bencode::BDE::dict();
|
BDE rDict = BDE::dict();
|
||||||
rDict["id"] = bencode::BDE(localNode->getID(), DHT_ID_LENGTH);
|
rDict["id"] = BDE(localNode->getID(), DHT_ID_LENGTH);
|
||||||
dict["r"] = rDict;
|
dict["r"] = rDict;
|
||||||
|
|
||||||
CPPUNIT_ASSERT_EQUAL(bencode::encode(dict), msgbody);
|
CPPUNIT_ASSERT_EQUAL(bencode::encode(dict), msgbody);
|
||||||
|
|
|
@ -61,13 +61,13 @@ void DHTFindNodeMessageTest::testGetBencodedMessage()
|
||||||
|
|
||||||
std::string msgbody = msg.getBencodedMessage();
|
std::string msgbody = msg.getBencodedMessage();
|
||||||
|
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["t"] = transactionID;
|
dict["t"] = transactionID;
|
||||||
dict["y"] = bencode::BDE("q");
|
dict["y"] = BDE("q");
|
||||||
dict["q"] = bencode::BDE("find_node");
|
dict["q"] = BDE("find_node");
|
||||||
bencode::BDE aDict = bencode::BDE::dict();
|
BDE aDict = BDE::dict();
|
||||||
aDict["id"] = bencode::BDE(localNode->getID(), DHT_ID_LENGTH);
|
aDict["id"] = BDE(localNode->getID(), DHT_ID_LENGTH);
|
||||||
aDict["target"] = bencode::BDE(targetNode->getID(), DHT_ID_LENGTH);
|
aDict["target"] = BDE(targetNode->getID(), DHT_ID_LENGTH);
|
||||||
dict["a"] = aDict;
|
dict["a"] = aDict;
|
||||||
|
|
||||||
CPPUNIT_ASSERT_EQUAL(bencode::encode(dict), msgbody);
|
CPPUNIT_ASSERT_EQUAL(bencode::encode(dict), msgbody);
|
||||||
|
|
|
@ -56,11 +56,11 @@ void DHTFindNodeReplyMessageTest::testGetBencodedMessage()
|
||||||
|
|
||||||
std::string msgbody = msg.getBencodedMessage();
|
std::string msgbody = msg.getBencodedMessage();
|
||||||
|
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["t"] = transactionID;
|
dict["t"] = transactionID;
|
||||||
dict["y"] = bencode::BDE("r");
|
dict["y"] = BDE("r");
|
||||||
bencode::BDE rDict = bencode::BDE::dict();
|
BDE rDict = BDE::dict();
|
||||||
rDict["id"] = bencode::BDE(localNode->getID(), DHT_ID_LENGTH);
|
rDict["id"] = BDE(localNode->getID(), DHT_ID_LENGTH);
|
||||||
rDict["nodes"] = compactNodeInfo;
|
rDict["nodes"] = compactNodeInfo;
|
||||||
dict["r"] = rDict;
|
dict["r"] = rDict;
|
||||||
|
|
||||||
|
|
|
@ -80,13 +80,13 @@ void DHTGetPeersMessageTest::testGetBencodedMessage()
|
||||||
|
|
||||||
std::string msgbody = msg.getBencodedMessage();
|
std::string msgbody = msg.getBencodedMessage();
|
||||||
|
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["t"] = transactionID;
|
dict["t"] = transactionID;
|
||||||
dict["y"] = bencode::BDE("q");
|
dict["y"] = BDE("q");
|
||||||
dict["q"] = bencode::BDE("get_peers");
|
dict["q"] = BDE("get_peers");
|
||||||
bencode::BDE aDict = bencode::BDE::dict();
|
BDE aDict = BDE::dict();
|
||||||
aDict["id"] = bencode::BDE(localNode->getID(), DHT_ID_LENGTH);
|
aDict["id"] = BDE(localNode->getID(), DHT_ID_LENGTH);
|
||||||
aDict["info_hash"] = bencode::BDE(infoHash, DHT_ID_LENGTH);
|
aDict["info_hash"] = BDE(infoHash, DHT_ID_LENGTH);
|
||||||
dict["a"] = aDict;
|
dict["a"] = aDict;
|
||||||
|
|
||||||
CPPUNIT_ASSERT_EQUAL(Util::urlencode(bencode::encode(dict)),
|
CPPUNIT_ASSERT_EQUAL(Util::urlencode(bencode::encode(dict)),
|
||||||
|
|
|
@ -42,11 +42,11 @@ void DHTGetPeersReplyMessageTest::testGetBencodedMessage()
|
||||||
|
|
||||||
DHTGetPeersReplyMessage msg(localNode, remoteNode, token, transactionID);
|
DHTGetPeersReplyMessage msg(localNode, remoteNode, token, transactionID);
|
||||||
|
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["t"] = transactionID;
|
dict["t"] = transactionID;
|
||||||
dict["y"] = bencode::BDE("r");
|
dict["y"] = BDE("r");
|
||||||
bencode::BDE rDict = bencode::BDE::dict();
|
BDE rDict = BDE::dict();
|
||||||
rDict["id"] = bencode::BDE(localNode->getID(), DHT_ID_LENGTH);
|
rDict["id"] = BDE(localNode->getID(), DHT_ID_LENGTH);
|
||||||
rDict["token"] = token;
|
rDict["token"] = token;
|
||||||
dict["r"] = rDict;
|
dict["r"] = rDict;
|
||||||
{
|
{
|
||||||
|
@ -77,12 +77,12 @@ void DHTGetPeersReplyMessageTest::testGetBencodedMessage()
|
||||||
rDict.removeKey("nodes");
|
rDict.removeKey("nodes");
|
||||||
{
|
{
|
||||||
std::deque<SharedHandle<Peer> > peers;
|
std::deque<SharedHandle<Peer> > peers;
|
||||||
bencode::BDE valuesList = bencode::BDE::list();
|
BDE valuesList = BDE::list();
|
||||||
for(size_t i = 0; i < 4; ++i) {
|
for(size_t i = 0; i < 4; ++i) {
|
||||||
SharedHandle<Peer> peer(new Peer("192.168.0."+Util::uitos(i+1), 6881+i));
|
SharedHandle<Peer> peer(new Peer("192.168.0."+Util::uitos(i+1), 6881+i));
|
||||||
unsigned char buffer[6];
|
unsigned char buffer[6];
|
||||||
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port));
|
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port));
|
||||||
valuesList << bencode::BDE(buffer, sizeof(buffer));
|
valuesList << BDE(buffer, sizeof(buffer));
|
||||||
peers.push_back(peer);
|
peers.push_back(peer);
|
||||||
}
|
}
|
||||||
rDict["values"] = valuesList;
|
rDict["values"] = valuesList;
|
||||||
|
|
|
@ -79,12 +79,12 @@ CPPUNIT_TEST_SUITE_REGISTRATION(DHTMessageFactoryImplTest);
|
||||||
|
|
||||||
void DHTMessageFactoryImplTest::testCreatePingMessage()
|
void DHTMessageFactoryImplTest::testCreatePingMessage()
|
||||||
{
|
{
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
dict["t"] = BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
||||||
dict["y"] = bencode::BDE("q");
|
dict["y"] = BDE("q");
|
||||||
dict["q"] = bencode::BDE("ping");
|
dict["q"] = BDE("ping");
|
||||||
bencode::BDE aDict = bencode::BDE::dict();
|
BDE aDict = BDE::dict();
|
||||||
aDict["id"] = bencode::BDE(remoteNodeID, DHT_ID_LENGTH);
|
aDict["id"] = BDE(remoteNodeID, DHT_ID_LENGTH);
|
||||||
dict["a"] = aDict;
|
dict["a"] = aDict;
|
||||||
|
|
||||||
SharedHandle<DHTPingMessage> m
|
SharedHandle<DHTPingMessage> m
|
||||||
|
@ -102,11 +102,11 @@ void DHTMessageFactoryImplTest::testCreatePingMessage()
|
||||||
|
|
||||||
void DHTMessageFactoryImplTest::testCreatePingReplyMessage()
|
void DHTMessageFactoryImplTest::testCreatePingReplyMessage()
|
||||||
{
|
{
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
dict["t"] = BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
||||||
dict["y"] = bencode::BDE("r");
|
dict["y"] = BDE("r");
|
||||||
bencode::BDE rDict = bencode::BDE::dict();
|
BDE rDict = BDE::dict();
|
||||||
rDict["id"] = bencode::BDE(remoteNodeID, DHT_ID_LENGTH);
|
rDict["id"] = BDE(remoteNodeID, DHT_ID_LENGTH);
|
||||||
dict["r"] = rDict;
|
dict["r"] = rDict;
|
||||||
|
|
||||||
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
|
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
|
||||||
|
@ -127,15 +127,15 @@ void DHTMessageFactoryImplTest::testCreatePingReplyMessage()
|
||||||
|
|
||||||
void DHTMessageFactoryImplTest::testCreateFindNodeMessage()
|
void DHTMessageFactoryImplTest::testCreateFindNodeMessage()
|
||||||
{
|
{
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
dict["t"] = BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
||||||
dict["y"] = bencode::BDE("q");
|
dict["y"] = BDE("q");
|
||||||
dict["q"] = bencode::BDE("find_node");
|
dict["q"] = BDE("find_node");
|
||||||
bencode::BDE aDict = bencode::BDE::dict();
|
BDE aDict = BDE::dict();
|
||||||
aDict["id"] = bencode::BDE(remoteNodeID, DHT_ID_LENGTH);
|
aDict["id"] = BDE(remoteNodeID, DHT_ID_LENGTH);
|
||||||
unsigned char targetNodeID[DHT_ID_LENGTH];
|
unsigned char targetNodeID[DHT_ID_LENGTH];
|
||||||
memset(targetNodeID, 0x11, DHT_ID_LENGTH);
|
memset(targetNodeID, 0x11, DHT_ID_LENGTH);
|
||||||
aDict["target"] = bencode::BDE(targetNodeID, DHT_ID_LENGTH);
|
aDict["target"] = BDE(targetNodeID, DHT_ID_LENGTH);
|
||||||
dict["a"] = aDict;
|
dict["a"] = aDict;
|
||||||
|
|
||||||
SharedHandle<DHTFindNodeMessage> m
|
SharedHandle<DHTFindNodeMessage> m
|
||||||
|
@ -156,11 +156,11 @@ void DHTMessageFactoryImplTest::testCreateFindNodeMessage()
|
||||||
void DHTMessageFactoryImplTest::testCreateFindNodeReplyMessage()
|
void DHTMessageFactoryImplTest::testCreateFindNodeReplyMessage()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
dict["t"] = BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
||||||
dict["y"] = bencode::BDE("r");
|
dict["y"] = BDE("r");
|
||||||
bencode::BDE rDict = bencode::BDE::dict();
|
BDE rDict = BDE::dict();
|
||||||
rDict["id"] = bencode::BDE(remoteNodeID, DHT_ID_LENGTH);
|
rDict["id"] = BDE(remoteNodeID, DHT_ID_LENGTH);
|
||||||
std::string compactNodeInfo;
|
std::string compactNodeInfo;
|
||||||
SharedHandle<DHTNode> nodes[8];
|
SharedHandle<DHTNode> nodes[8];
|
||||||
for(size_t i = 0; i < DHTBucket::K; ++i) {
|
for(size_t i = 0; i < DHTBucket::K; ++i) {
|
||||||
|
@ -202,15 +202,15 @@ void DHTMessageFactoryImplTest::testCreateFindNodeReplyMessage()
|
||||||
|
|
||||||
void DHTMessageFactoryImplTest::testCreateGetPeersMessage()
|
void DHTMessageFactoryImplTest::testCreateGetPeersMessage()
|
||||||
{
|
{
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
dict["t"] = BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
||||||
dict["y"] = bencode::BDE("q");
|
dict["y"] = BDE("q");
|
||||||
dict["q"] = bencode::BDE("get_peers");
|
dict["q"] = BDE("get_peers");
|
||||||
bencode::BDE aDict = bencode::BDE::dict();
|
BDE aDict = BDE::dict();
|
||||||
aDict["id"] = bencode::BDE(remoteNodeID, DHT_ID_LENGTH);
|
aDict["id"] = BDE(remoteNodeID, DHT_ID_LENGTH);
|
||||||
unsigned char infoHash[DHT_ID_LENGTH];
|
unsigned char infoHash[DHT_ID_LENGTH];
|
||||||
memset(infoHash, 0x11, DHT_ID_LENGTH);
|
memset(infoHash, 0x11, DHT_ID_LENGTH);
|
||||||
aDict["info_hash"] = bencode::BDE(infoHash, DHT_ID_LENGTH);
|
aDict["info_hash"] = BDE(infoHash, DHT_ID_LENGTH);
|
||||||
dict["a"] = aDict;
|
dict["a"] = aDict;
|
||||||
|
|
||||||
SharedHandle<DHTGetPeersMessage> m
|
SharedHandle<DHTGetPeersMessage> m
|
||||||
|
@ -231,11 +231,11 @@ void DHTMessageFactoryImplTest::testCreateGetPeersMessage()
|
||||||
void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_nodes()
|
void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_nodes()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
dict["t"] = BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
||||||
dict["y"] = bencode::BDE("r");
|
dict["y"] = BDE("r");
|
||||||
bencode::BDE rDict = bencode::BDE::dict();
|
BDE rDict = BDE::dict();
|
||||||
rDict["id"] = bencode::BDE(remoteNodeID, DHT_ID_LENGTH);
|
rDict["id"] = BDE(remoteNodeID, DHT_ID_LENGTH);
|
||||||
std::string compactNodeInfo;
|
std::string compactNodeInfo;
|
||||||
SharedHandle<DHTNode> nodes[8];
|
SharedHandle<DHTNode> nodes[8];
|
||||||
for(size_t i = 0; i < DHTBucket::K; ++i) {
|
for(size_t i = 0; i < DHTBucket::K; ++i) {
|
||||||
|
@ -251,7 +251,7 @@ void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_nodes()
|
||||||
std::string(&buf[0], &buf[sizeof(buf)]);
|
std::string(&buf[0], &buf[sizeof(buf)]);
|
||||||
}
|
}
|
||||||
rDict["nodes"] = compactNodeInfo;
|
rDict["nodes"] = compactNodeInfo;
|
||||||
rDict["token"] = bencode::BDE("token");
|
rDict["token"] = BDE("token");
|
||||||
dict["r"] = rDict;
|
dict["r"] = rDict;
|
||||||
|
|
||||||
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
|
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
|
||||||
|
@ -280,24 +280,24 @@ void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_nodes()
|
||||||
void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_values()
|
void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_values()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
dict["t"] = BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
||||||
dict["y"] = bencode::BDE("r");
|
dict["y"] = BDE("r");
|
||||||
bencode::BDE rDict = bencode::BDE::dict();
|
BDE rDict = BDE::dict();
|
||||||
rDict["id"] = bencode::BDE(remoteNodeID, DHT_ID_LENGTH);
|
rDict["id"] = BDE(remoteNodeID, DHT_ID_LENGTH);
|
||||||
|
|
||||||
std::deque<SharedHandle<Peer> > peers;
|
std::deque<SharedHandle<Peer> > peers;
|
||||||
bencode::BDE valuesList = bencode::BDE::list();
|
BDE valuesList = BDE::list();
|
||||||
for(size_t i = 0; i < 4; ++i) {
|
for(size_t i = 0; i < 4; ++i) {
|
||||||
SharedHandle<Peer> peer(new Peer("192.168.0."+Util::uitos(i+1), 6881+i));
|
SharedHandle<Peer> peer(new Peer("192.168.0."+Util::uitos(i+1), 6881+i));
|
||||||
unsigned char buffer[6];
|
unsigned char buffer[6];
|
||||||
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buffer, peer->ipaddr,
|
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buffer, peer->ipaddr,
|
||||||
peer->port));
|
peer->port));
|
||||||
valuesList << bencode::BDE(buffer, sizeof(buffer));
|
valuesList << BDE(buffer, sizeof(buffer));
|
||||||
peers.push_back(peer);
|
peers.push_back(peer);
|
||||||
}
|
}
|
||||||
rDict["values"] = valuesList;
|
rDict["values"] = valuesList;
|
||||||
rDict["token"] = bencode::BDE("token");
|
rDict["token"] = BDE("token");
|
||||||
dict["r"] = rDict;
|
dict["r"] = rDict;
|
||||||
|
|
||||||
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
|
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
|
||||||
|
@ -326,15 +326,15 @@ void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_values()
|
||||||
void DHTMessageFactoryImplTest::testCreateAnnouncePeerMessage()
|
void DHTMessageFactoryImplTest::testCreateAnnouncePeerMessage()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
dict["t"] = BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
||||||
dict["y"] = bencode::BDE("q");
|
dict["y"] = BDE("q");
|
||||||
dict["q"] = bencode::BDE("announce_peer");
|
dict["q"] = BDE("announce_peer");
|
||||||
bencode::BDE aDict = bencode::BDE::dict();
|
BDE aDict = BDE::dict();
|
||||||
aDict["id"] = bencode::BDE(remoteNodeID, DHT_ID_LENGTH);
|
aDict["id"] = BDE(remoteNodeID, DHT_ID_LENGTH);
|
||||||
unsigned char infoHash[DHT_ID_LENGTH];
|
unsigned char infoHash[DHT_ID_LENGTH];
|
||||||
memset(infoHash, 0x11, DHT_ID_LENGTH);
|
memset(infoHash, 0x11, DHT_ID_LENGTH);
|
||||||
aDict["info_hash"] = bencode::BDE(infoHash, DHT_ID_LENGTH);
|
aDict["info_hash"] = BDE(infoHash, DHT_ID_LENGTH);
|
||||||
std::string token = "ffff";
|
std::string token = "ffff";
|
||||||
uint16_t port = 6881;
|
uint16_t port = 6881;
|
||||||
aDict["port"] = port;
|
aDict["port"] = port;
|
||||||
|
@ -363,11 +363,11 @@ void DHTMessageFactoryImplTest::testCreateAnnouncePeerMessage()
|
||||||
|
|
||||||
void DHTMessageFactoryImplTest::testCreateAnnouncePeerReplyMessage()
|
void DHTMessageFactoryImplTest::testCreateAnnouncePeerReplyMessage()
|
||||||
{
|
{
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
dict["t"] = BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
||||||
dict["y"] = bencode::BDE("r");
|
dict["y"] = BDE("r");
|
||||||
bencode::BDE rDict = bencode::BDE::dict();
|
BDE rDict = BDE::dict();
|
||||||
rDict["id"] = bencode::BDE(remoteNodeID, DHT_ID_LENGTH);
|
rDict["id"] = BDE(remoteNodeID, DHT_ID_LENGTH);
|
||||||
dict["r"] = rDict;
|
dict["r"] = rDict;
|
||||||
|
|
||||||
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
|
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
|
||||||
|
@ -388,12 +388,12 @@ void DHTMessageFactoryImplTest::testCreateAnnouncePeerReplyMessage()
|
||||||
|
|
||||||
void DHTMessageFactoryImplTest::testReceivedErrorMessage()
|
void DHTMessageFactoryImplTest::testReceivedErrorMessage()
|
||||||
{
|
{
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["t"] = bencode::BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
dict["t"] = BDE(transactionID, DHT_TRANSACTION_ID_LENGTH);
|
||||||
dict["y"] = bencode::BDE("e");
|
dict["y"] = BDE("e");
|
||||||
bencode::BDE list = bencode::BDE::list();
|
BDE list = BDE::list();
|
||||||
list << 404;
|
list << 404;
|
||||||
list << bencode::BDE("Not found");
|
list << BDE("Not found");
|
||||||
dict["e"] = list;
|
dict["e"] = list;
|
||||||
|
|
||||||
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
|
SharedHandle<DHTNode> remoteNode(new DHTNode(remoteNodeID));
|
||||||
|
|
|
@ -76,7 +76,7 @@ void DHTMessageTrackerTest::testMessageArrived()
|
||||||
tracker.addMessage(m3);
|
tracker.addMessage(m3);
|
||||||
|
|
||||||
{
|
{
|
||||||
bencode::BDE resDict = bencode::BDE::dict();
|
BDE resDict = BDE::dict();
|
||||||
resDict["t"] = m2->getTransactionID();
|
resDict["t"] = m2->getTransactionID();
|
||||||
|
|
||||||
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> > p =
|
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> > p =
|
||||||
|
@ -90,7 +90,7 @@ void DHTMessageTrackerTest::testMessageArrived()
|
||||||
CPPUNIT_ASSERT_EQUAL((size_t)2, tracker.countEntry());
|
CPPUNIT_ASSERT_EQUAL((size_t)2, tracker.countEntry());
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
bencode::BDE resDict = bencode::BDE::dict();
|
BDE resDict = BDE::dict();
|
||||||
resDict["t"] = m3->getTransactionID();
|
resDict["t"] = m3->getTransactionID();
|
||||||
|
|
||||||
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> > p =
|
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> > p =
|
||||||
|
@ -103,7 +103,7 @@ void DHTMessageTrackerTest::testMessageArrived()
|
||||||
CPPUNIT_ASSERT_EQUAL((size_t)1, tracker.countEntry());
|
CPPUNIT_ASSERT_EQUAL((size_t)1, tracker.countEntry());
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
bencode::BDE resDict = bencode::BDE::dict();
|
BDE resDict = BDE::dict();
|
||||||
resDict["t"] = m1->getTransactionID();
|
resDict["t"] = m1->getTransactionID();
|
||||||
|
|
||||||
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> > p =
|
std::pair<SharedHandle<DHTMessage>, SharedHandle<DHTMessageCallback> > p =
|
||||||
|
|
|
@ -57,12 +57,12 @@ void DHTPingMessageTest::testGetBencodedMessage()
|
||||||
|
|
||||||
std::string msgbody = msg.getBencodedMessage();
|
std::string msgbody = msg.getBencodedMessage();
|
||||||
|
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["t"] = transactionID;
|
dict["t"] = transactionID;
|
||||||
dict["y"] = bencode::BDE("q");
|
dict["y"] = BDE("q");
|
||||||
dict["q"] = bencode::BDE("ping");
|
dict["q"] = BDE("ping");
|
||||||
bencode::BDE aDict = bencode::BDE::dict();
|
BDE aDict = BDE::dict();
|
||||||
aDict["id"] = bencode::BDE(localNode->getID(), DHT_ID_LENGTH);
|
aDict["id"] = BDE(localNode->getID(), DHT_ID_LENGTH);
|
||||||
dict["a"] = aDict;
|
dict["a"] = aDict;
|
||||||
|
|
||||||
CPPUNIT_ASSERT_EQUAL(bencode::encode(dict), msgbody);
|
CPPUNIT_ASSERT_EQUAL(bencode::encode(dict), msgbody);
|
||||||
|
|
|
@ -42,11 +42,11 @@ void DHTPingReplyMessageTest::testGetBencodedMessage()
|
||||||
|
|
||||||
std::string msgbody = msg.getBencodedMessage();
|
std::string msgbody = msg.getBencodedMessage();
|
||||||
|
|
||||||
bencode::BDE dict = bencode::BDE::dict();
|
BDE dict = BDE::dict();
|
||||||
dict["t"] = transactionID;
|
dict["t"] = transactionID;
|
||||||
dict["y"] = bencode::BDE("r");
|
dict["y"] = BDE("r");
|
||||||
bencode::BDE rDict = bencode::BDE::dict();
|
BDE rDict = BDE::dict();
|
||||||
rDict["id"] = bencode::BDE(id, DHT_ID_LENGTH);
|
rDict["id"] = BDE(id, DHT_ID_LENGTH);
|
||||||
dict["r"] = rDict;
|
dict["r"] = rDict;
|
||||||
|
|
||||||
CPPUNIT_ASSERT_EQUAL(bencode::encode(dict), msgbody);
|
CPPUNIT_ASSERT_EQUAL(bencode::encode(dict), msgbody);
|
||||||
|
|
|
@ -68,7 +68,8 @@ aria2c_SOURCES = AllTest.cc\
|
||||||
RarestPieceSelectorTest.cc\
|
RarestPieceSelectorTest.cc\
|
||||||
LongestSequencePieceSelectorTest.cc\
|
LongestSequencePieceSelectorTest.cc\
|
||||||
a2algoTest.cc\
|
a2algoTest.cc\
|
||||||
bitfieldTest.cc
|
bitfieldTest.cc\
|
||||||
|
BDETest.cc
|
||||||
|
|
||||||
if HAVE_POSIX_FALLOCATE
|
if HAVE_POSIX_FALLOCATE
|
||||||
aria2c_SOURCES += FallocFileAllocationIteratorTest.cc
|
aria2c_SOURCES += FallocFileAllocationIteratorTest.cc
|
||||||
|
|
|
@ -194,7 +194,7 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \
|
||||||
SimpleDNSCacheTest.cc DownloadHelperTest.cc BencodeTest.cc \
|
SimpleDNSCacheTest.cc DownloadHelperTest.cc BencodeTest.cc \
|
||||||
SequentialPickerTest.cc RarestPieceSelectorTest.cc \
|
SequentialPickerTest.cc RarestPieceSelectorTest.cc \
|
||||||
LongestSequencePieceSelectorTest.cc a2algoTest.cc \
|
LongestSequencePieceSelectorTest.cc a2algoTest.cc \
|
||||||
bitfieldTest.cc FallocFileAllocationIteratorTest.cc \
|
bitfieldTest.cc BDETest.cc FallocFileAllocationIteratorTest.cc \
|
||||||
GZipDecoderTest.cc Sqlite3MozCookieParserTest.cc \
|
GZipDecoderTest.cc Sqlite3MozCookieParserTest.cc \
|
||||||
MessageDigestHelperTest.cc \
|
MessageDigestHelperTest.cc \
|
||||||
IteratableChunkChecksumValidatorTest.cc \
|
IteratableChunkChecksumValidatorTest.cc \
|
||||||
|
@ -365,9 +365,9 @@ am_aria2c_OBJECTS = AllTest.$(OBJEXT) TestUtil.$(OBJEXT) \
|
||||||
SequentialPickerTest.$(OBJEXT) \
|
SequentialPickerTest.$(OBJEXT) \
|
||||||
RarestPieceSelectorTest.$(OBJEXT) \
|
RarestPieceSelectorTest.$(OBJEXT) \
|
||||||
LongestSequencePieceSelectorTest.$(OBJEXT) \
|
LongestSequencePieceSelectorTest.$(OBJEXT) \
|
||||||
a2algoTest.$(OBJEXT) bitfieldTest.$(OBJEXT) $(am__objects_1) \
|
a2algoTest.$(OBJEXT) bitfieldTest.$(OBJEXT) BDETest.$(OBJEXT) \
|
||||||
$(am__objects_2) $(am__objects_3) $(am__objects_4) \
|
$(am__objects_1) $(am__objects_2) $(am__objects_3) \
|
||||||
$(am__objects_5) $(am__objects_6)
|
$(am__objects_4) $(am__objects_5) $(am__objects_6)
|
||||||
aria2c_OBJECTS = $(am_aria2c_OBJECTS)
|
aria2c_OBJECTS = $(am_aria2c_OBJECTS)
|
||||||
am__DEPENDENCIES_1 =
|
am__DEPENDENCIES_1 =
|
||||||
aria2c_DEPENDENCIES = ../src/libaria2c.a ../src/download_helper.o \
|
aria2c_DEPENDENCIES = ../src/libaria2c.a ../src/download_helper.o \
|
||||||
|
@ -592,7 +592,7 @@ aria2c_SOURCES = AllTest.cc TestUtil.cc TestUtil.h SocketCoreTest.cc \
|
||||||
SimpleDNSCacheTest.cc DownloadHelperTest.cc BencodeTest.cc \
|
SimpleDNSCacheTest.cc DownloadHelperTest.cc BencodeTest.cc \
|
||||||
SequentialPickerTest.cc RarestPieceSelectorTest.cc \
|
SequentialPickerTest.cc RarestPieceSelectorTest.cc \
|
||||||
LongestSequencePieceSelectorTest.cc a2algoTest.cc \
|
LongestSequencePieceSelectorTest.cc a2algoTest.cc \
|
||||||
bitfieldTest.cc $(am__append_1) $(am__append_2) \
|
bitfieldTest.cc BDETest.cc $(am__append_1) $(am__append_2) \
|
||||||
$(am__append_3) $(am__append_4) $(am__append_5) \
|
$(am__append_3) $(am__append_4) $(am__append_5) \
|
||||||
$(am__append_6)
|
$(am__append_6)
|
||||||
|
|
||||||
|
@ -688,6 +688,7 @@ distclean-compile:
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AlphaNumberDecoratorTest.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AlphaNumberDecoratorTest.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AnnounceListTest.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AnnounceListTest.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AuthConfigFactoryTest.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/AuthConfigFactoryTest.Po@am__quote@
|
||||||
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BDETest.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BNodeTest.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BNodeTest.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Base64Test.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Base64Test.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BencodeTest.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BencodeTest.Po@am__quote@
|
||||||
|
|
|
@ -17,7 +17,7 @@ public:
|
||||||
virtual ~MockDHTMessageFactory() {}
|
virtual ~MockDHTMessageFactory() {}
|
||||||
|
|
||||||
virtual SharedHandle<DHTMessage>
|
virtual SharedHandle<DHTMessage>
|
||||||
createQueryMessage(const bencode::BDE& dict,
|
createQueryMessage(const BDE& dict,
|
||||||
const std::string& ipaddr, uint16_t port)
|
const std::string& ipaddr, uint16_t port)
|
||||||
{
|
{
|
||||||
return SharedHandle<DHTMessage>();
|
return SharedHandle<DHTMessage>();
|
||||||
|
@ -25,7 +25,7 @@ public:
|
||||||
|
|
||||||
virtual SharedHandle<DHTMessage>
|
virtual SharedHandle<DHTMessage>
|
||||||
createResponseMessage(const std::string& messageType,
|
createResponseMessage(const std::string& messageType,
|
||||||
const bencode::BDE& dict,
|
const BDE& dict,
|
||||||
const std::string& ipaddr, uint16_t port)
|
const std::string& ipaddr, uint16_t port)
|
||||||
{
|
{
|
||||||
SharedHandle<DHTNode> remoteNode(new DHTNode());
|
SharedHandle<DHTNode> remoteNode(new DHTNode());
|
||||||
|
|
|
@ -35,7 +35,7 @@ void PeerListProcessorTest::testExtractPeerFromList() {
|
||||||
PeerListProcessor proc;
|
PeerListProcessor proc;
|
||||||
std::string peersString = "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-000000000000004:porti2006eeee";
|
std::string peersString = "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-000000000000004:porti2006eeee";
|
||||||
|
|
||||||
const bencode::BDE dict = bencode::decode(peersString);
|
const BDE dict = bencode::decode(peersString);
|
||||||
|
|
||||||
std::deque<SharedHandle<Peer> > peers;
|
std::deque<SharedHandle<Peer> > peers;
|
||||||
proc.extractPeerFromList(dict["peers"], std::back_inserter(peers));
|
proc.extractPeerFromList(dict["peers"], std::back_inserter(peers));
|
||||||
|
@ -49,7 +49,7 @@ void PeerListProcessorTest::testExtract2PeersFromList() {
|
||||||
PeerListProcessor proc;
|
PeerListProcessor proc;
|
||||||
std::string peersString = "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-000000000000004:porti65535eed2:ip11:192.168.0.27:peer id20:aria2-000000000000004:porti2007eeee";
|
std::string peersString = "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-000000000000004:porti65535eed2:ip11:192.168.0.27:peer id20:aria2-000000000000004:porti2007eeee";
|
||||||
|
|
||||||
const bencode::BDE dict = bencode::decode(peersString);
|
const BDE dict = bencode::decode(peersString);
|
||||||
|
|
||||||
std::deque<SharedHandle<Peer> > peers;
|
std::deque<SharedHandle<Peer> > peers;
|
||||||
proc.extractPeerFromList(dict["peers"], std::back_inserter(peers));
|
proc.extractPeerFromList(dict["peers"], std::back_inserter(peers));
|
||||||
|
|
Loading…
Reference in New Issue