2008-01-06 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

Move extension from BtRuntime to ExtensionMessageFactory, because
	extension can be specified per peer, not per torrent.
	* src/DefaultBtInteractive.cc
	* src/BtRuntime.h
	* src/ExtendedMessagingAware.h
	* src/ExtensionMessageFactory.h
	* src/DefaultExtensionMessageFactory.cc
	* test/DefaultExtensionMessageFactoryTest.cc
pull/1/head
Tatsuhiro Tsujikawa 2008-01-05 15:51:21 +00:00
parent fce7b4b32f
commit e5e75ee4ed
7 changed files with 111 additions and 42 deletions

View File

@ -1,3 +1,14 @@
2008-01-06 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Move extension from BtRuntime to ExtensionMessageFactory, because
extension can be specified per peer, not per torrent.
* src/DefaultBtInteractive.cc
* src/BtRuntime.h
* src/ExtendedMessagingAware.h
* src/ExtensionMessageFactory.h
* src/DefaultExtensionMessageFactory.cc
* test/DefaultExtensionMessageFactoryTest.cc
2008-01-05 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added categorized option help. Specify category using --help option.

View File

@ -47,8 +47,6 @@ private:
bool halt;
int32_t connections;
bool _ready;
Extensions _extensions;
public:
BtRuntime():
uploadLengthAtStartup(0),
@ -56,9 +54,8 @@ public:
halt(false),
connections(0),
_ready(false)
{
_extensions["ut_pex"] = 8;
}
{}
~BtRuntime() {}
int64_t getUploadLengthAtStartup() const {
@ -94,33 +91,6 @@ public:
bool ready() { return _ready; }
void setReady(bool go) { _ready = go; }
const Extensions& getExtensions() const
{
return _extensions;
}
uint8_t getExtensionMessageID(const string& name)
{
Extensions::const_iterator itr = _extensions.find(name);
if(itr == _extensions.end()) {
return 0;
} else {
return (*itr).second;
}
}
string getExtensionName(uint8_t id)
{
for(Extensions::const_iterator itr = _extensions.begin();
itr != _extensions.end(); ++itr) {
const Extensions::value_type& p = *itr;
if(p.second == id) {
return p.first;
}
}
return "";
}
};
typedef SharedHandle<BtRuntime> BtRuntimeHandle;

View File

@ -71,8 +71,12 @@ BtMessageHandle DefaultBtInteractive::receiveHandshake(bool quickReply) {
}
if(message->isExtendedMessagingEnabled()) {
peer->setExtendedMessagingEnabled(true);
PEER_OBJECT(btContext, peer)->extensionMessageFactory =
DefaultExtensionMessageFactoryHandle factory =
new DefaultExtensionMessageFactory(btContext, peer);
if(!_utPexEnabled) {
factory->removeExtension("ut_pex");
}
PEER_OBJECT(btContext, peer)->extensionMessageFactory = factory;
logger->info(MSG_EXTENDED_MESSAGING_ENABLED, cuid);
}
@ -105,11 +109,9 @@ void DefaultBtInteractive::addHandshakeExtendedMessageToQueue()
HandshakeExtensionMessageHandle m = new HandshakeExtensionMessage();
m->setClientVersion("aria2");
m->setTCPPort(btRuntime->getListenPort());
m->setExtensions(btRuntime->getExtensions());
BtExtendedMessageHandle msg =
messageFactory->createBtExtendedMessage(m);
m->setExtensions(EXTENSION_MESSAGE_FACTORY(btContext, peer)->getExtensions());
BtExtendedMessageHandle msg = messageFactory->createBtExtendedMessage(m);
dispatcher->addMessageToQueue(msg);
}

View File

@ -65,7 +65,7 @@ DefaultExtensionMessageFactory::createMessage(const char* data, size_t length)
m->setPeer(_peer);
return m;
} else {
string extensionName = BT_RUNTIME(_btContext)->getExtensionName(extensionMessageID);
string extensionName = getExtensionName(extensionMessageID);
if(extensionName.empty()) {
throw new DlAbortEx("No extension registered for extended message ID %u",
extensionMessageID);

View File

@ -0,0 +1,85 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2006 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_EXTENDED_MESSAGING_AWARE_H_
#define _D_EXTENDED_MESSAGING_AWARE_H_
#include "common.h"
#include "BtConstants.h"
class ExtendedMessagingAware {
private:
Extensions _extensions;
public:
ExtendedMessagingAware()
{
_extensions["ut_pex"] = 8;
}
virtual ~ExtendedMessagingAware() {}
const Extensions& getExtensions() const
{
return _extensions;
}
uint8_t getExtensionMessageID(const string& name) const
{
Extensions::const_iterator itr = _extensions.find(name);
if(itr == _extensions.end()) {
return 0;
} else {
return (*itr).second;
}
}
string getExtensionName(uint8_t id) const
{
for(Extensions::const_iterator itr = _extensions.begin();
itr != _extensions.end(); ++itr) {
const Extensions::value_type& p = *itr;
if(p.second == id) {
return p.first;
}
}
return "";
}
void removeExtension(const string& name)
{
_extensions.erase(name);
}
};
#endif // _D_EXTENDED_MESSAGING_AWARE_H_

View File

@ -35,12 +35,12 @@
#ifndef _D_EXTENSION_MESSAGE_FACTORY_H_
#define _D_EXTENSION_MESSAGE_FACTORY_H_
#include "common.h"
#include "ExtendedMessagingAware.h"
class ExtensionMessage;
typedef SharedHandle<ExtensionMessage> ExtensionMessageHandle;
class ExtensionMessageFactory {
class ExtensionMessageFactory:public ExtendedMessagingAware {
public:
virtual ~ExtensionMessageFactory() {}

View File

@ -63,6 +63,7 @@ void DefaultExtensionMessageFactoryTest::testCreateMessage_unknown()
string data = string(&id[0], &id[1]);
try {
// this test fails because localhost doesn't have extension id = 255.
factory.createMessage(data.c_str(), data.size());
CPPUNIT_FAIL("exception must be thrown.");
} catch(Exception* e) {
@ -99,7 +100,7 @@ void DefaultExtensionMessageFactoryTest::testCreateMessage_UTPex()
PeerMessageUtil::createcompact(c3, "192.168.0.2", 6882);
PeerMessageUtil::createcompact(c4, "10.1.1.3",10000);
char id[1] = { BT_RUNTIME(_btContext)->getExtensionMessageID("ut_pex") };
char id[1] = { factory.getExtensionMessageID("ut_pex") };
string data = string(&id[0], &id[1])+"d5:added12:"+
string(&c1[0], &c1[6])+string(&c2[0], &c2[6])+
@ -108,6 +109,6 @@ void DefaultExtensionMessageFactoryTest::testCreateMessage_UTPex()
"e";
UTPexExtensionMessageHandle m = factory.createMessage(data.c_str(), data.size());
CPPUNIT_ASSERT_EQUAL(BT_RUNTIME(_btContext)->getExtensionMessageID("ut_pex"),
CPPUNIT_ASSERT_EQUAL(factory.getExtensionMessageID("ut_pex"),
m->getExtensionMessageID());
}