mirror of
https://github.com/aria2/aria2.git
synced 2025-02-26 08:22:11 +00:00
Extract the Peer class's member variables, which are only needed after PeerInteractionCommand, into PeerSessionResource class. This class is instantiated in PeerInteractionCommand class's ctor and released in its dtor. This will make Peer class lightweight and uses less memory for peers which are not connected and wait in the queue. * src/PeerChokeCommand.cc * src/PeerSessionResource.{h, cc} * src/PeerInteractionCommand.cc * src/PeerAbstractCommand.cc: Note: 0 is given to onAbort() function. * src/DefaultBtInteractive.cc * src/BtPieceMessage.cc * src/BtInterestedMessage.cc * src/BtUnchokeMessage.cc * src/DefaultPeerStorage.{h, cc} * src/PeerInitiateConnectionCommand.cc * src/ActivePeerConnectionCommand.cc * src/BtNotInterestedMessage.cc * src/DefaultBtMessageDispatcher.cc * src/BtChokeMessage.cc * src/BtRequestMessage.cc * src/Peer.{h, cc} * src/BtRegistry.h * src/TrackerWatcherCommand.cc * src/PeerReceiveHandshakeCommand.cc * test/BtExtendedMessageTest.cc * test/BtAllowedFastMessageTest.cc * test/BtCancelMessageTest.cc * test/DefaultPieceStorageTest.cc * test/BtBitfieldMessageTest.cc * test/BtHaveMessageTest.cc * test/BtNotInterestedMessageTest.cc * test/BtRequestMessageTest.cc * test/PeerSessionResourceTest.cc * test/DefaultBtMessageDispatcherTest.cc * test/PeerTest.cc * test/BtInterestedMessageTest.cc * test/BtRejectMessageTest.cc * test/BtChokeMessageTest.cc * test/DefaultPeerStorageTest.cc * test/BtHaveNoneMessageTest.cc * test/BtHaveAllMessageTest.cc * test/DefaultExtensionMessageFactoryTest.cc * test/BtUnchokeMessageTest.cc * test/DefaultBtMessageFactoryTest.cc * test/HandshakeExtensionMessageTest.cc * test/UTPexExtensionMessageTest.cc * test/DefaultBtRequestFactoryTest.cc * test/BtPieceMessageTest.cc Removed typedef PeerStats. * src/PeerStat.h * src/SegmentMan.cc
124 lines
3.5 KiB
C++
124 lines
3.5 KiB
C++
#include "DefaultBtMessageFactory.h"
|
|
#include "Peer.h"
|
|
#include "PeerMessageUtil.h"
|
|
#include "BtRegistry.h"
|
|
#include "MockBtContext.h"
|
|
#include "MockExtensionMessageFactory.h"
|
|
#include "BtExtendedMessage.h"
|
|
#include "BtPortMessage.h"
|
|
#include "PeerObject.h"
|
|
#include "BtRequestFactory.h"
|
|
#include "BtMessageDispatcher.h"
|
|
#include "BtMessageReceiver.h"
|
|
#include "PeerConnection.h"
|
|
#include "Exception.h"
|
|
#include "FileEntry.h"
|
|
#include <cstring>
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
namespace aria2 {
|
|
|
|
class DefaultBtMessageFactoryTest:public CppUnit::TestFixture {
|
|
|
|
CPPUNIT_TEST_SUITE(DefaultBtMessageFactoryTest);
|
|
CPPUNIT_TEST(testCreateBtMessage_BtExtendedMessage);
|
|
CPPUNIT_TEST(testCreatePortMessage);
|
|
CPPUNIT_TEST_SUITE_END();
|
|
private:
|
|
SharedHandle<MockBtContext> _btContext;
|
|
SharedHandle<Peer> _peer;
|
|
public:
|
|
DefaultBtMessageFactoryTest():_btContext(0), _peer(0) {}
|
|
|
|
void setUp()
|
|
{
|
|
BtRegistry::unregisterAll();
|
|
SharedHandle<MockBtContext> btContext = new MockBtContext();
|
|
unsigned char infohash[20];
|
|
memset(infohash, 0, sizeof(infohash));
|
|
btContext->setInfoHash(infohash);
|
|
_btContext = btContext;
|
|
|
|
_peer = new Peer("192.168.0.1", 6969);
|
|
_peer->allocateSessionResource(1024, 1024*1024);
|
|
_peer->setExtendedMessagingEnabled(true);
|
|
|
|
SharedHandle<MockExtensionMessageFactory> exmsgFactory =
|
|
new MockExtensionMessageFactory();
|
|
BtRegistry::registerPeerObjectCluster(_btContext->getInfoHashAsString(),
|
|
new PeerObjectCluster());
|
|
SharedHandle<PeerObject> peerObject = new PeerObject();
|
|
peerObject->extensionMessageFactory = exmsgFactory;
|
|
|
|
PEER_OBJECT_CLUSTER(_btContext)->registerHandle(_peer->getID(), peerObject);
|
|
}
|
|
|
|
void tearDown()
|
|
{
|
|
BtRegistry::unregisterAll();
|
|
}
|
|
|
|
void testCreateBtMessage_BtExtendedMessage();
|
|
void testCreatePortMessage();
|
|
};
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(DefaultBtMessageFactoryTest);
|
|
|
|
void DefaultBtMessageFactoryTest::testCreateBtMessage_BtExtendedMessage()
|
|
{
|
|
|
|
DefaultBtMessageFactory factory;
|
|
factory.setBtContext(_btContext);
|
|
factory.setPeer(_peer);
|
|
|
|
// payload:{4:name3:foo}->11bytes
|
|
std::string payload = "4:name3:foo";
|
|
char msg[17];// 6+11bytes
|
|
PeerMessageUtil::createPeerMessageString((unsigned char*)msg, sizeof(msg), 13, 20);
|
|
msg[5] = 1; // Set dummy extended message ID 1
|
|
memcpy(msg+6, payload.c_str(), payload.size());
|
|
|
|
SharedHandle<BtExtendedMessage> m =
|
|
factory.createBtMessage((const unsigned char*)msg+4, sizeof(msg));
|
|
|
|
try {
|
|
// disable extended messaging
|
|
_peer->setExtendedMessagingEnabled(false);
|
|
factory.createBtMessage((const unsigned char*)msg+4, sizeof(msg));
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
|
} catch(Exception* e) {
|
|
std::cerr << *e << std::endl;
|
|
delete e;
|
|
}
|
|
}
|
|
|
|
void DefaultBtMessageFactoryTest::testCreatePortMessage()
|
|
{
|
|
DefaultBtMessageFactory factory;
|
|
factory.setBtContext(_btContext);
|
|
factory.setPeer(_peer);
|
|
|
|
{
|
|
unsigned char data[7];
|
|
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 3, 9);
|
|
PeerMessageUtil::setShortIntParam(&data[5], 6881);
|
|
try {
|
|
SharedHandle<BtPortMessage> m = factory.createBtMessage(&data[4], sizeof(data)-4);
|
|
CPPUNIT_ASSERT(!m.isNull());
|
|
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, m->getPort());
|
|
} catch(Exception* e) {
|
|
std::cerr << *e << std::endl;
|
|
std::string msg = e->getMsg();
|
|
delete e;
|
|
CPPUNIT_FAIL(msg);
|
|
}
|
|
}
|
|
{
|
|
SharedHandle<BtPortMessage> m = factory.createPortMessage(6881);
|
|
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, m->getPort());
|
|
}
|
|
}
|
|
|
|
} // namespace aria2
|