2006-12-24 06:25:21 +00:00
|
|
|
#include "DefaultBtMessageDispatcher.h"
|
2008-11-03 06:49:02 +00:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2006-12-24 06:25:21 +00:00
|
|
|
#include "Exception.h"
|
|
|
|
#include "MockPieceStorage.h"
|
|
|
|
#include "MockPeerStorage.h"
|
|
|
|
#include "MockBtMessage.h"
|
|
|
|
#include "MockBtMessageFactory.h"
|
|
|
|
#include "prefs.h"
|
|
|
|
#include "BtCancelSendingPieceEvent.h"
|
2008-04-20 00:50:22 +00:00
|
|
|
#include "BtHandshakeMessage.h"
|
2009-02-28 11:48:26 +00:00
|
|
|
#include "Option.h"
|
|
|
|
#include "RequestGroupMan.h"
|
|
|
|
#include "ServerStatMan.h"
|
|
|
|
#include "RequestGroup.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#include "DownloadContext.h"
|
|
|
|
#include "bittorrent_helper.h"
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
class DefaultBtMessageDispatcherTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(DefaultBtMessageDispatcherTest);
|
|
|
|
CPPUNIT_TEST(testAddMessage);
|
|
|
|
CPPUNIT_TEST(testSendMessages);
|
|
|
|
CPPUNIT_TEST(testSendMessages_underUploadLimit);
|
2009-02-28 11:48:26 +00:00
|
|
|
// See the comment on the definition
|
|
|
|
//CPPUNIT_TEST(testSendMessages_overUploadLimit);
|
2006-12-24 06:25:21 +00:00
|
|
|
CPPUNIT_TEST(testSendMessages_sendingInProgress);
|
|
|
|
CPPUNIT_TEST(testDoCancelSendingPieceAction);
|
|
|
|
CPPUNIT_TEST(testCheckRequestSlotAndDoNecessaryThing);
|
|
|
|
CPPUNIT_TEST(testCheckRequestSlotAndDoNecessaryThing_timeout);
|
|
|
|
CPPUNIT_TEST(testCheckRequestSlotAndDoNecessaryThing_completeBlock);
|
|
|
|
CPPUNIT_TEST(testIsSendingInProgress);
|
|
|
|
CPPUNIT_TEST(testCountOutstandingRequest);
|
|
|
|
CPPUNIT_TEST(testIsOutstandingRequest);
|
|
|
|
CPPUNIT_TEST(testGetOutstandingRequest);
|
|
|
|
CPPUNIT_TEST(testRemoveOutstandingRequest);
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
private:
|
2009-06-28 10:37:15 +00:00
|
|
|
SharedHandle<DownloadContext> _dctx;
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<Peer> peer;
|
|
|
|
SharedHandle<DefaultBtMessageDispatcher> btMessageDispatcher;
|
|
|
|
SharedHandle<MockPeerStorage> peerStorage;
|
|
|
|
SharedHandle<MockPieceStorage> pieceStorage;
|
2008-11-03 06:49:02 +00:00
|
|
|
SharedHandle<MockBtMessageFactory> _messageFactory;
|
2009-02-28 11:48:26 +00:00
|
|
|
SharedHandle<RequestGroupMan> _rgman;
|
|
|
|
SharedHandle<Option> _option;
|
|
|
|
SharedHandle<RequestGroup> _rg;
|
2006-12-24 06:25:21 +00:00
|
|
|
public:
|
2007-01-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To decrease CPU usage in bittorrent download, calculation
results in
BitfieldMan were cached and realtime fetching PeerObject was
removed
with WeakHandle introduced. Option values are set to the objects
by setter before download begins.
* src/DefaultBtRequestFactory.cc: Use messageFactory member.
* src/DefaultBtRequestFactory.h
(dispatcher): BtMessageDispatcherHandle ->
BtMessageDispatcherWeakHandle.
(messageFactory): New variable.
(setBtMessageDispatcher): BtMessageDispatcherHandle ->
BtMessageDispatcherWeakHandle.
(setBtMessageFactory): New function.
* src/DefaultBtMessageDispatcher.cc:
(sendMessages): Use maxUploadSpeedLimit instead of fetching the
value
from Option.
(checkRequestSlotAndDoNecessaryThing): Use requestTimeout
instead of
feating the value from Option.
Use messageFactory member.
* src/PeerInteractionCommand.cc
(PeerInteractionCommand): Added maxDownloadSpeedLimit.
Add reverse dependencies to factory object.
Set maxUploadSpeedLimit and requestTimeout and messageFactory to
dispatcher.
Set messageFactory to receiver.
Set keepAliveInterval and maxDownloadSpeedLimit and
messageFactory to
btInteractive.
Set receiver to peerObject.
Set maxDownloadSpeedLimit to this.
(executeInternal): Use maxDownloadSpeedLimit member.
* src/BtChokeMessage.cc
(doReceivedAction): Use dispatcher, requestFactory member.
(onSendComplete): Use dispatcher member.
* src/PeerInteractionCommand.h
(maxDownloadSpeedLimit): New variable.
* src/DefaultBtMessageReceiver.h
(peerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(dispatcher):
BtMessageDispatcherHandle -> BtMessageDispatcherWeakHandle
(messageFactory): New variable.
(setPeerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(getPeerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(setDispatcher):
BtMessageDispatcherHandle -> BtMessageDispatcherWeakHandle
(setBtMessageFactory): New function.
* src/DefaultBtInteractive.cc
(initiateHandshake): Use messageFactory member.
(addBitfieldMessageToQueue): Use messageFactory member.
(addAllowedFastMessageToQueue): Use messageFactory member.
(decideChoking): Use messageFactory member.
(checkHave): Use messageFactory member.
(sendKeepAlive): Use keepAliveInterval, messageFactory member.
(receiveMessages): Use maxDownloadSpeedLimit member.
(decideInterest): Use messageFactory member.
* src/BtRequestMessage.cc
(doReceivedAction): Use messageFactory, dispatcher member.
(onQueued): Use dispatcher member.
* src/BtPieceMessage.cc
(doReceivedAction): Use dispatcher member.
(send): Use peerConnection member.
(onWrongPiece): Use requestFactory member.
(handleChokingEvent): Use messageFactory, dispatcher member.
(handleCancelSendingPieceEvent): Use messageFactory, dispatcher
member.
* src/BtMessageDispatcher.h
(BtMessageDispatcherWeakHandle): New type definition.
* src/SimpleBtMessage.cc
(send): Use peerConnection member.
* src/BtRejectMessage.cc
(doReceivedAction): Use dispatcher member.
* src/DefaultBtMessageDispatcher.h
(Option.h): Removed include.
(messageFactory): New variable.
(option): Removed.
(maxUploadSpeedLimit): New variable.
(requestTimeout): New variable.
(DefaultBtMessageDispatcher): Removed option.
Added maxUploadSpeedLimit, requestTimeout.
(setOption): Removed.
(getOption): Removed.
(setMaxUploadSpeedLimit): New function.
(setRequestTimeout): New function.
(setBtMessageFactory): New function.
* src/DefaultBtInteractive.h
(btMessageReceiver):
BtMessageReceiverHandle -> BtMessageReceiverWeakHandle
(dispatcher):
BtMessageDispatcherHandle -> BtMessageReceiverWeakHandle
(btRequestFactory):
BtRequestFactoryHandle -> BtRequestFactoryWeakHandle
(peerConnection):
PeerConnectionHandle -> PeerConnectionWeakHandle
(messageFactory): New variable.
(option): Removed.
(keepAliveInterval): New variable.
(maxDownloadSpeedLimit): New variable.
(DefaultBtInteractive): Added keepAliveInterval,
maxDownloadSpeedLimit.
(setBtMessageReceiver):
BtMessageReceiverHandle -> BtMessageReceiverWeakHandle
(setDispatcher):
BtMessageDispatcherHandle -> BtMessageReceiverWeakHandle
(setBtRequestFactory):
BtRequestFactoryHandle -> BtRequestFactoryWeakHandle
(setPeerConnection):
PeerConnectionHandle -> PeerConnectionWeakHandle
(setOption): Removed.
(setKeepAliveInterval): New function.
(setMaxDownloadSpeedLimit): New function.
(setBtMessageFactory): New function.
* src/BitfieldMan.h
(cachedNumMissingBlock): New variable.
(cachedNumFilteredBlock): New variable.
(cachedCompletedLength): New variable.
(cachedFilteredComletedLength): New variable.
(cachedFilteredTotalLength): New variable.
(countMissingBlockNow): New function.
(countFilteredBlockNow): New function.
(getFilteredTotalLengthNow): New function.
(getCompletedLengthNow): New function.
(getFilteredCompletedLengthNow): New function.
(updateCache): New function.
* src/AbstractBtMessage.h
(BtMessageDispatcher.h): New include.
(PeerConnection.h): New include.
(BtRequestFactory.h): New include
(BtMessageFactory.h): New include.
(dispatcher): New variable.
(messageFactory): New variable.
(peerConnection: New variable.
(setBtMessageDispatcher): New function.
(setPeerConnection): New function.
(setBtMessageFactory): New function.
(setBtRequestFactory): New function.
* src/DefaultBtMessageFactory.cc
(setCommonProperty): Set dispatcher, requestFactory, this,
peerConnection to msg.
* src/BtRegistry.h
(BT_MESSAGE_RECEIVER): New macro.
* src/PeerConnection.h
(PeerConnectionWeakHandle): New type definition.
* src/BtMessageFactory.h
(BtMessageFactoryWeakHandle): New type definition.
* src/BitfieldMan.cc
(BitfieldMan): Added cachedNumMissingBlock,
cachedNumFilteredBlock,
cachedCompletedLength, cachedFilteredComletedLength,
cachedFilteredTotalLength.
Call updateCache().
(countMissingBlock): Return cachedNumMissingBlock.
(countMissingBlockNow): New function.
(countBlock): Return cachedNumFilteredBlock if filterEnabled is
true.
(countFilteredBlockNow): New function.
(setBit): Call updateCache().
(unsetBit): Call updateCache().
(setBitfield): Call updateCache().
(clearAllBit): Call updateCache().
(setAllBit): Use setBitInternal instead of setBit.
Call updateCache().
(addFilter): Call updateCache().
(enableFilter): Call updateCache().
(disableFilter): Call updateCache().
(clearFilter): Call updateCache().
(getFilteredTotalLength): Return cachedFilteredTotalLength.
(getFilteredTotalLengthNow): New function.
(getCompletedLength): Return cachedCompletedLength.
(getCompletedLengthNow): New function.
(getFilteredCompletedLength): Return
cachedFilteredComletedLength.
(getFilteredCompletedLengthNow): New function.
(updateCache): New function.
* src/BtMessageReceiver.h
(BtMessageReceiverWeakHandle): New type definition.
* src/DefaultBtMessageReceiver.cc
(receiveHandshake): Use messageFactory member.
(sendHandshake): Use messageFactory member.
(receiveMessage): Use messageFactory member.
* src/DefaultBtMessageFactory.h
(dispatcher): New variable.
(requestFactory): New variable.
(peerConnection): New variablle.
(setBtMessageDispatcher): New function.
(setBtRequestFactory): New function.
(setPeerConnection): New function.
* src/SharedHandle.h
(RefCount): New class.
(WeakHandle): New class.
* src/PeerObject.h
(BtMessageReceiver.h): New include.
(PeerObject): Added btMessageReceiver.
(btMessageReceiver): New variable.
* src/Util.cc
(countBit): Simplified.
* src/BtCancelMessage.cc
(doReceivedAction): Use dispatcher member.
* src/BtRequestFactory.h
(BtRequestFactoryWeakHandle): New type definition.
* src/PeerStorage.h
(downloadSpeed): int -> uint32_t
(uploadSpeed): int -> uint32_t
(sessionDownloadLength): long long int -> uint64_t
(sessionUploadLength): long long int -> uint64_t
2007-01-16 15:20:26 +00:00
|
|
|
void tearDown() {}
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
void testAddMessage();
|
|
|
|
void testSendMessages();
|
|
|
|
void testSendMessages_underUploadLimit();
|
|
|
|
void testSendMessages_overUploadLimit();
|
|
|
|
void testSendMessages_sendingInProgress();
|
|
|
|
void testDoCancelSendingPieceAction();
|
|
|
|
void testCheckRequestSlotAndDoNecessaryThing();
|
|
|
|
void testCheckRequestSlotAndDoNecessaryThing_timeout();
|
|
|
|
void testCheckRequestSlotAndDoNecessaryThing_completeBlock();
|
|
|
|
void testIsSendingInProgress();
|
|
|
|
void testCountOutstandingRequest();
|
|
|
|
void testIsOutstandingRequest();
|
|
|
|
void testGetOutstandingRequest();
|
|
|
|
void testRemoveOutstandingRequest();
|
|
|
|
|
|
|
|
class MockBtMessage2 : public MockBtMessage {
|
|
|
|
private:
|
|
|
|
bool onQueuedCalled;
|
|
|
|
bool sendCalled;
|
|
|
|
bool doCancelActionCalled;
|
|
|
|
public:
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string type;
|
2006-12-24 06:25:21 +00:00
|
|
|
public:
|
|
|
|
MockBtMessage2():onQueuedCalled(false),
|
2010-01-05 16:01:46 +00:00
|
|
|
sendCalled(false),
|
|
|
|
doCancelActionCalled(false)
|
2006-12-24 06:25:21 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~MockBtMessage2() {}
|
|
|
|
|
|
|
|
virtual void onQueued() {
|
|
|
|
onQueuedCalled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isOnQueuedCalled() const {
|
|
|
|
return onQueuedCalled;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void send() {
|
|
|
|
sendCalled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isSendCalled() const {
|
|
|
|
return sendCalled;
|
|
|
|
}
|
|
|
|
|
2009-04-21 14:52:04 +00:00
|
|
|
virtual void onCancelSendingPieceEvent
|
|
|
|
(const BtCancelSendingPieceEvent& event)
|
|
|
|
{
|
|
|
|
doCancelActionCalled = true;
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isDoCancelActionCalled() const {
|
|
|
|
return doCancelActionCalled;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class MockPieceStorage2 : public MockPieceStorage {
|
|
|
|
private:
|
2008-02-08 15:53:45 +00:00
|
|
|
SharedHandle<Piece> piece;
|
2006-12-24 06:25:21 +00:00
|
|
|
public:
|
2008-03-08 08:04:28 +00:00
|
|
|
virtual SharedHandle<Piece> getPiece(size_t index) {
|
2006-12-24 06:25:21 +00:00
|
|
|
return piece;
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
void setPiece(const SharedHandle<Piece>& piece) {
|
2006-12-24 06:25:21 +00:00
|
|
|
this->piece = piece;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class MockBtMessageFactory2 : public MockBtMessageFactory {
|
|
|
|
public:
|
2008-02-08 15:53:45 +00:00
|
|
|
virtual SharedHandle<BtMessage>
|
2008-03-09 12:24:01 +00:00
|
|
|
createCancelMessage(size_t index, uint32_t begin, size_t length) {
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MockBtMessage2> btMsg(new MockBtMessage2());
|
2006-12-24 06:25:21 +00:00
|
|
|
btMsg->type = "cancel";
|
|
|
|
return btMsg;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void setUp() {
|
2009-02-28 11:48:26 +00:00
|
|
|
_option.reset(new Option());
|
|
|
|
|
2009-06-29 08:42:58 +00:00
|
|
|
_rg.reset(new RequestGroup(_option));
|
2009-02-28 11:48:26 +00:00
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
_dctx.reset(new DownloadContext());
|
|
|
|
bittorrent::load("test.torrent", _dctx);
|
2009-11-28 10:12:04 +00:00
|
|
|
|
|
|
|
_rg->setDownloadContext(_dctx);
|
2009-02-28 11:48:26 +00:00
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
peer.reset(new Peer("192.168.0.1", 6969));
|
2009-06-28 10:37:15 +00:00
|
|
|
peer->allocateSessionResource
|
|
|
|
(_dctx->getPieceLength(), _dctx->getTotalLength());
|
2008-04-20 00:50:22 +00:00
|
|
|
peerStorage.reset(new MockPeerStorage());
|
|
|
|
pieceStorage.reset(new MockPieceStorage());
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2008-11-03 06:49:02 +00:00
|
|
|
_messageFactory.reset(new MockBtMessageFactory2());
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2009-02-28 11:48:26 +00:00
|
|
|
_rgman.reset(new RequestGroupMan(std::deque<SharedHandle<RequestGroup> >(),
|
2010-01-05 16:01:46 +00:00
|
|
|
0, _option.get()));
|
2009-02-28 11:48:26 +00:00
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
btMessageDispatcher.reset(new DefaultBtMessageDispatcher());
|
2006-12-24 06:25:21 +00:00
|
|
|
btMessageDispatcher->setPeer(peer);
|
2009-06-28 10:37:15 +00:00
|
|
|
btMessageDispatcher->setDownloadContext(_dctx);
|
2008-11-03 06:49:02 +00:00
|
|
|
btMessageDispatcher->setPieceStorage(pieceStorage);
|
|
|
|
btMessageDispatcher->setPeerStorage(peerStorage);
|
|
|
|
btMessageDispatcher->setBtMessageFactory(_messageFactory);
|
|
|
|
btMessageDispatcher->setCuid(1);
|
2009-02-28 11:48:26 +00:00
|
|
|
btMessageDispatcher->setRequestGroupMan(_rgman);
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(DefaultBtMessageDispatcherTest);
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcherTest::testAddMessage() {
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MockBtMessage2> msg(new MockBtMessage2());
|
2006-12-24 06:25:21 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, msg->isOnQueuedCalled());
|
|
|
|
btMessageDispatcher->addMessageToQueue(msg);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, msg->isOnQueuedCalled());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1,
|
2010-01-05 16:01:46 +00:00
|
|
|
btMessageDispatcher->getMessageQueue().size());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcherTest::testSendMessages() {
|
|
|
|
TransferStat stat;
|
|
|
|
stat.setUploadSpeed(0);
|
|
|
|
peerStorage->setStat(stat);
|
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MockBtMessage2> msg1(new MockBtMessage2());
|
2006-12-24 06:25:21 +00:00
|
|
|
msg1->setSendingInProgress(false);
|
|
|
|
msg1->setUploading(false);
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MockBtMessage2> msg2(new MockBtMessage2());
|
2006-12-24 06:25:21 +00:00
|
|
|
msg2->setSendingInProgress(false);
|
|
|
|
msg2->setUploading(false);
|
|
|
|
btMessageDispatcher->addMessageToQueue(msg1);
|
|
|
|
btMessageDispatcher->addMessageToQueue(msg2);
|
|
|
|
btMessageDispatcher->sendMessages();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(msg1->isSendCalled());
|
|
|
|
CPPUNIT_ASSERT(msg2->isSendCalled());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcherTest::testSendMessages_underUploadLimit() {
|
|
|
|
TransferStat stat;
|
|
|
|
stat.setUploadSpeed(0);
|
|
|
|
peerStorage->setStat(stat);
|
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MockBtMessage2> msg1(new MockBtMessage2());
|
2006-12-24 06:25:21 +00:00
|
|
|
msg1->setSendingInProgress(false);
|
|
|
|
msg1->setUploading(true);
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MockBtMessage2> msg2(new MockBtMessage2());
|
2006-12-24 06:25:21 +00:00
|
|
|
msg2->setSendingInProgress(false);
|
|
|
|
msg2->setUploading(true);
|
|
|
|
btMessageDispatcher->addMessageToQueue(msg1);
|
|
|
|
btMessageDispatcher->addMessageToQueue(msg2);
|
|
|
|
btMessageDispatcher->sendMessages();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(msg1->isSendCalled());
|
|
|
|
CPPUNIT_ASSERT(msg2->isSendCalled());
|
|
|
|
}
|
|
|
|
|
2009-02-28 11:48:26 +00:00
|
|
|
// TODO Because we no longer directly use PeerStorage::calculateStat()
|
|
|
|
// and Neither RequestGroup nor RequestGroupMan can be stubbed, this
|
|
|
|
// test is commented out for now.
|
|
|
|
//
|
|
|
|
// void DefaultBtMessageDispatcherTest::testSendMessages_overUploadLimit() {
|
|
|
|
// btMessageDispatcher->setMaxUploadSpeedLimit(100);
|
|
|
|
// TransferStat stat;
|
|
|
|
// stat.setUploadSpeed(150);
|
|
|
|
// peerStorage->setStat(stat);
|
|
|
|
|
|
|
|
// SharedHandle<MockBtMessage2> msg1(new MockBtMessage2());
|
|
|
|
// msg1->setSendingInProgress(false);
|
|
|
|
// msg1->setUploading(true);
|
|
|
|
// SharedHandle<MockBtMessage2> msg2(new MockBtMessage2());
|
|
|
|
// msg2->setSendingInProgress(false);
|
|
|
|
// msg2->setUploading(true);
|
|
|
|
// SharedHandle<MockBtMessage2> msg3(new MockBtMessage2());
|
|
|
|
// msg3->setSendingInProgress(false);
|
|
|
|
// msg3->setUploading(false);
|
|
|
|
|
|
|
|
// btMessageDispatcher->addMessageToQueue(msg1);
|
|
|
|
// btMessageDispatcher->addMessageToQueue(msg2);
|
|
|
|
// btMessageDispatcher->addMessageToQueue(msg3);
|
|
|
|
// btMessageDispatcher->sendMessages();
|
|
|
|
|
|
|
|
// CPPUNIT_ASSERT(!msg1->isSendCalled());
|
|
|
|
// CPPUNIT_ASSERT(!msg2->isSendCalled());
|
|
|
|
// CPPUNIT_ASSERT(msg3->isSendCalled());
|
|
|
|
|
|
|
|
// CPPUNIT_ASSERT_EQUAL((size_t)2,
|
2010-01-05 16:01:46 +00:00
|
|
|
// btMessageDispatcher->getMessageQueue().size());
|
2009-02-28 11:48:26 +00:00
|
|
|
// }
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
void DefaultBtMessageDispatcherTest::testSendMessages_sendingInProgress() {
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MockBtMessage2> msg1(new MockBtMessage2());
|
2006-12-24 06:25:21 +00:00
|
|
|
msg1->setSendingInProgress(false);
|
|
|
|
msg1->setUploading(false);
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MockBtMessage2> msg2(new MockBtMessage2());
|
2006-12-24 06:25:21 +00:00
|
|
|
msg2->setSendingInProgress(true);
|
|
|
|
msg2->setUploading(false);
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MockBtMessage2> msg3(new MockBtMessage2());
|
2006-12-24 06:25:21 +00:00
|
|
|
msg3->setSendingInProgress(false);
|
|
|
|
msg3->setUploading(false);
|
|
|
|
|
|
|
|
btMessageDispatcher->addMessageToQueue(msg1);
|
|
|
|
btMessageDispatcher->addMessageToQueue(msg2);
|
|
|
|
btMessageDispatcher->addMessageToQueue(msg3);
|
|
|
|
|
|
|
|
btMessageDispatcher->sendMessages();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(msg1->isSendCalled());
|
|
|
|
CPPUNIT_ASSERT(msg2->isSendCalled());
|
|
|
|
CPPUNIT_ASSERT(!msg3->isSendCalled());
|
|
|
|
|
2008-11-03 06:49:02 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2,
|
2010-01-05 16:01:46 +00:00
|
|
|
btMessageDispatcher->getMessageQueue().size());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcherTest::testDoCancelSendingPieceAction() {
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MockBtMessage2> msg1(new MockBtMessage2());
|
|
|
|
SharedHandle<MockBtMessage2> msg2(new MockBtMessage2());
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
btMessageDispatcher->addMessageToQueue(msg1);
|
|
|
|
btMessageDispatcher->addMessageToQueue(msg2);
|
|
|
|
|
|
|
|
btMessageDispatcher->doCancelSendingPieceAction(0, 0, 0);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, msg1->isDoCancelActionCalled());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(true, msg2->isDoCancelActionCalled());
|
|
|
|
}
|
|
|
|
|
|
|
|
int MY_PIECE_LENGTH = 16*1024;
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcherTest::testCheckRequestSlotAndDoNecessaryThing() {
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<Piece> piece(new Piece(0, MY_PIECE_LENGTH));
|
2008-05-31 05:46:16 +00:00
|
|
|
RequestSlot slot(0, 0, MY_PIECE_LENGTH, 0, piece);
|
|
|
|
|
2008-03-08 08:04:28 +00:00
|
|
|
size_t index;
|
|
|
|
CPPUNIT_ASSERT(piece->getMissingUnusedBlockIndex(index));
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, index);
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MockPieceStorage2> pieceStorage(new MockPieceStorage2());
|
2006-12-24 06:25:21 +00:00
|
|
|
pieceStorage->setPiece(piece);
|
|
|
|
|
2008-11-03 06:49:02 +00:00
|
|
|
btMessageDispatcher->setRequestTimeout(60);
|
|
|
|
btMessageDispatcher->setPieceStorage(pieceStorage);
|
2006-12-24 06:25:21 +00:00
|
|
|
btMessageDispatcher->addOutstandingRequest(slot);
|
|
|
|
|
|
|
|
btMessageDispatcher->checkRequestSlotAndDoNecessaryThing();
|
|
|
|
|
2008-11-03 06:49:02 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0,
|
2010-01-05 16:01:46 +00:00
|
|
|
btMessageDispatcher->getMessageQueue().size());
|
2008-11-03 06:49:02 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1,
|
2010-01-05 16:01:46 +00:00
|
|
|
btMessageDispatcher->getRequestSlots().size());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcherTest::testCheckRequestSlotAndDoNecessaryThing_timeout() {
|
2008-05-31 05:46:16 +00:00
|
|
|
SharedHandle<Piece> piece(new Piece(0, MY_PIECE_LENGTH));
|
|
|
|
RequestSlot slot(0, 0, MY_PIECE_LENGTH, 0, piece);
|
2006-12-24 06:25:21 +00:00
|
|
|
// make this slot timeout
|
|
|
|
slot.setDispatchedTime(0);
|
|
|
|
|
2008-03-08 08:04:28 +00:00
|
|
|
size_t index;
|
|
|
|
CPPUNIT_ASSERT(piece->getMissingUnusedBlockIndex(index));
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, index);
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MockPieceStorage2> pieceStorage(new MockPieceStorage2());
|
2006-12-24 06:25:21 +00:00
|
|
|
pieceStorage->setPiece(piece);
|
|
|
|
|
2007-01-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To decrease CPU usage in bittorrent download, calculation
results in
BitfieldMan were cached and realtime fetching PeerObject was
removed
with WeakHandle introduced. Option values are set to the objects
by setter before download begins.
* src/DefaultBtRequestFactory.cc: Use messageFactory member.
* src/DefaultBtRequestFactory.h
(dispatcher): BtMessageDispatcherHandle ->
BtMessageDispatcherWeakHandle.
(messageFactory): New variable.
(setBtMessageDispatcher): BtMessageDispatcherHandle ->
BtMessageDispatcherWeakHandle.
(setBtMessageFactory): New function.
* src/DefaultBtMessageDispatcher.cc:
(sendMessages): Use maxUploadSpeedLimit instead of fetching the
value
from Option.
(checkRequestSlotAndDoNecessaryThing): Use requestTimeout
instead of
feating the value from Option.
Use messageFactory member.
* src/PeerInteractionCommand.cc
(PeerInteractionCommand): Added maxDownloadSpeedLimit.
Add reverse dependencies to factory object.
Set maxUploadSpeedLimit and requestTimeout and messageFactory to
dispatcher.
Set messageFactory to receiver.
Set keepAliveInterval and maxDownloadSpeedLimit and
messageFactory to
btInteractive.
Set receiver to peerObject.
Set maxDownloadSpeedLimit to this.
(executeInternal): Use maxDownloadSpeedLimit member.
* src/BtChokeMessage.cc
(doReceivedAction): Use dispatcher, requestFactory member.
(onSendComplete): Use dispatcher member.
* src/PeerInteractionCommand.h
(maxDownloadSpeedLimit): New variable.
* src/DefaultBtMessageReceiver.h
(peerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(dispatcher):
BtMessageDispatcherHandle -> BtMessageDispatcherWeakHandle
(messageFactory): New variable.
(setPeerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(getPeerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(setDispatcher):
BtMessageDispatcherHandle -> BtMessageDispatcherWeakHandle
(setBtMessageFactory): New function.
* src/DefaultBtInteractive.cc
(initiateHandshake): Use messageFactory member.
(addBitfieldMessageToQueue): Use messageFactory member.
(addAllowedFastMessageToQueue): Use messageFactory member.
(decideChoking): Use messageFactory member.
(checkHave): Use messageFactory member.
(sendKeepAlive): Use keepAliveInterval, messageFactory member.
(receiveMessages): Use maxDownloadSpeedLimit member.
(decideInterest): Use messageFactory member.
* src/BtRequestMessage.cc
(doReceivedAction): Use messageFactory, dispatcher member.
(onQueued): Use dispatcher member.
* src/BtPieceMessage.cc
(doReceivedAction): Use dispatcher member.
(send): Use peerConnection member.
(onWrongPiece): Use requestFactory member.
(handleChokingEvent): Use messageFactory, dispatcher member.
(handleCancelSendingPieceEvent): Use messageFactory, dispatcher
member.
* src/BtMessageDispatcher.h
(BtMessageDispatcherWeakHandle): New type definition.
* src/SimpleBtMessage.cc
(send): Use peerConnection member.
* src/BtRejectMessage.cc
(doReceivedAction): Use dispatcher member.
* src/DefaultBtMessageDispatcher.h
(Option.h): Removed include.
(messageFactory): New variable.
(option): Removed.
(maxUploadSpeedLimit): New variable.
(requestTimeout): New variable.
(DefaultBtMessageDispatcher): Removed option.
Added maxUploadSpeedLimit, requestTimeout.
(setOption): Removed.
(getOption): Removed.
(setMaxUploadSpeedLimit): New function.
(setRequestTimeout): New function.
(setBtMessageFactory): New function.
* src/DefaultBtInteractive.h
(btMessageReceiver):
BtMessageReceiverHandle -> BtMessageReceiverWeakHandle
(dispatcher):
BtMessageDispatcherHandle -> BtMessageReceiverWeakHandle
(btRequestFactory):
BtRequestFactoryHandle -> BtRequestFactoryWeakHandle
(peerConnection):
PeerConnectionHandle -> PeerConnectionWeakHandle
(messageFactory): New variable.
(option): Removed.
(keepAliveInterval): New variable.
(maxDownloadSpeedLimit): New variable.
(DefaultBtInteractive): Added keepAliveInterval,
maxDownloadSpeedLimit.
(setBtMessageReceiver):
BtMessageReceiverHandle -> BtMessageReceiverWeakHandle
(setDispatcher):
BtMessageDispatcherHandle -> BtMessageReceiverWeakHandle
(setBtRequestFactory):
BtRequestFactoryHandle -> BtRequestFactoryWeakHandle
(setPeerConnection):
PeerConnectionHandle -> PeerConnectionWeakHandle
(setOption): Removed.
(setKeepAliveInterval): New function.
(setMaxDownloadSpeedLimit): New function.
(setBtMessageFactory): New function.
* src/BitfieldMan.h
(cachedNumMissingBlock): New variable.
(cachedNumFilteredBlock): New variable.
(cachedCompletedLength): New variable.
(cachedFilteredComletedLength): New variable.
(cachedFilteredTotalLength): New variable.
(countMissingBlockNow): New function.
(countFilteredBlockNow): New function.
(getFilteredTotalLengthNow): New function.
(getCompletedLengthNow): New function.
(getFilteredCompletedLengthNow): New function.
(updateCache): New function.
* src/AbstractBtMessage.h
(BtMessageDispatcher.h): New include.
(PeerConnection.h): New include.
(BtRequestFactory.h): New include
(BtMessageFactory.h): New include.
(dispatcher): New variable.
(messageFactory): New variable.
(peerConnection: New variable.
(setBtMessageDispatcher): New function.
(setPeerConnection): New function.
(setBtMessageFactory): New function.
(setBtRequestFactory): New function.
* src/DefaultBtMessageFactory.cc
(setCommonProperty): Set dispatcher, requestFactory, this,
peerConnection to msg.
* src/BtRegistry.h
(BT_MESSAGE_RECEIVER): New macro.
* src/PeerConnection.h
(PeerConnectionWeakHandle): New type definition.
* src/BtMessageFactory.h
(BtMessageFactoryWeakHandle): New type definition.
* src/BitfieldMan.cc
(BitfieldMan): Added cachedNumMissingBlock,
cachedNumFilteredBlock,
cachedCompletedLength, cachedFilteredComletedLength,
cachedFilteredTotalLength.
Call updateCache().
(countMissingBlock): Return cachedNumMissingBlock.
(countMissingBlockNow): New function.
(countBlock): Return cachedNumFilteredBlock if filterEnabled is
true.
(countFilteredBlockNow): New function.
(setBit): Call updateCache().
(unsetBit): Call updateCache().
(setBitfield): Call updateCache().
(clearAllBit): Call updateCache().
(setAllBit): Use setBitInternal instead of setBit.
Call updateCache().
(addFilter): Call updateCache().
(enableFilter): Call updateCache().
(disableFilter): Call updateCache().
(clearFilter): Call updateCache().
(getFilteredTotalLength): Return cachedFilteredTotalLength.
(getFilteredTotalLengthNow): New function.
(getCompletedLength): Return cachedCompletedLength.
(getCompletedLengthNow): New function.
(getFilteredCompletedLength): Return
cachedFilteredComletedLength.
(getFilteredCompletedLengthNow): New function.
(updateCache): New function.
* src/BtMessageReceiver.h
(BtMessageReceiverWeakHandle): New type definition.
* src/DefaultBtMessageReceiver.cc
(receiveHandshake): Use messageFactory member.
(sendHandshake): Use messageFactory member.
(receiveMessage): Use messageFactory member.
* src/DefaultBtMessageFactory.h
(dispatcher): New variable.
(requestFactory): New variable.
(peerConnection): New variablle.
(setBtMessageDispatcher): New function.
(setBtRequestFactory): New function.
(setPeerConnection): New function.
* src/SharedHandle.h
(RefCount): New class.
(WeakHandle): New class.
* src/PeerObject.h
(BtMessageReceiver.h): New include.
(PeerObject): Added btMessageReceiver.
(btMessageReceiver): New variable.
* src/Util.cc
(countBit): Simplified.
* src/BtCancelMessage.cc
(doReceivedAction): Use dispatcher member.
* src/BtRequestFactory.h
(BtRequestFactoryWeakHandle): New type definition.
* src/PeerStorage.h
(downloadSpeed): int -> uint32_t
(uploadSpeed): int -> uint32_t
(sessionDownloadLength): long long int -> uint64_t
(sessionUploadLength): long long int -> uint64_t
2007-01-16 15:20:26 +00:00
|
|
|
btMessageDispatcher->setRequestTimeout(60);
|
2008-11-03 06:49:02 +00:00
|
|
|
btMessageDispatcher->setPieceStorage(pieceStorage);
|
2006-12-24 06:25:21 +00:00
|
|
|
btMessageDispatcher->addOutstandingRequest(slot);
|
|
|
|
|
|
|
|
btMessageDispatcher->checkRequestSlotAndDoNecessaryThing();
|
|
|
|
|
2008-11-03 06:49:02 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0,
|
2010-01-05 16:01:46 +00:00
|
|
|
btMessageDispatcher->getMessageQueue().size());
|
2008-11-03 06:49:02 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0,
|
2010-01-05 16:01:46 +00:00
|
|
|
btMessageDispatcher->getRequestSlots().size());
|
2006-12-24 06:25:21 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(false, piece->isBlockUsed(0));
|
2008-02-09 17:14:40 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(true, peer->snubbing());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcherTest::testCheckRequestSlotAndDoNecessaryThing_completeBlock() {
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<Piece> piece(new Piece(0, MY_PIECE_LENGTH));
|
2006-12-24 06:25:21 +00:00
|
|
|
piece->completeBlock(0);
|
|
|
|
|
2008-05-31 05:46:16 +00:00
|
|
|
RequestSlot slot(0, 0, MY_PIECE_LENGTH, 0, piece);
|
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MockPieceStorage2> pieceStorage(new MockPieceStorage2());
|
2006-12-24 06:25:21 +00:00
|
|
|
pieceStorage->setPiece(piece);
|
|
|
|
|
2007-01-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To decrease CPU usage in bittorrent download, calculation
results in
BitfieldMan were cached and realtime fetching PeerObject was
removed
with WeakHandle introduced. Option values are set to the objects
by setter before download begins.
* src/DefaultBtRequestFactory.cc: Use messageFactory member.
* src/DefaultBtRequestFactory.h
(dispatcher): BtMessageDispatcherHandle ->
BtMessageDispatcherWeakHandle.
(messageFactory): New variable.
(setBtMessageDispatcher): BtMessageDispatcherHandle ->
BtMessageDispatcherWeakHandle.
(setBtMessageFactory): New function.
* src/DefaultBtMessageDispatcher.cc:
(sendMessages): Use maxUploadSpeedLimit instead of fetching the
value
from Option.
(checkRequestSlotAndDoNecessaryThing): Use requestTimeout
instead of
feating the value from Option.
Use messageFactory member.
* src/PeerInteractionCommand.cc
(PeerInteractionCommand): Added maxDownloadSpeedLimit.
Add reverse dependencies to factory object.
Set maxUploadSpeedLimit and requestTimeout and messageFactory to
dispatcher.
Set messageFactory to receiver.
Set keepAliveInterval and maxDownloadSpeedLimit and
messageFactory to
btInteractive.
Set receiver to peerObject.
Set maxDownloadSpeedLimit to this.
(executeInternal): Use maxDownloadSpeedLimit member.
* src/BtChokeMessage.cc
(doReceivedAction): Use dispatcher, requestFactory member.
(onSendComplete): Use dispatcher member.
* src/PeerInteractionCommand.h
(maxDownloadSpeedLimit): New variable.
* src/DefaultBtMessageReceiver.h
(peerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(dispatcher):
BtMessageDispatcherHandle -> BtMessageDispatcherWeakHandle
(messageFactory): New variable.
(setPeerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(getPeerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(setDispatcher):
BtMessageDispatcherHandle -> BtMessageDispatcherWeakHandle
(setBtMessageFactory): New function.
* src/DefaultBtInteractive.cc
(initiateHandshake): Use messageFactory member.
(addBitfieldMessageToQueue): Use messageFactory member.
(addAllowedFastMessageToQueue): Use messageFactory member.
(decideChoking): Use messageFactory member.
(checkHave): Use messageFactory member.
(sendKeepAlive): Use keepAliveInterval, messageFactory member.
(receiveMessages): Use maxDownloadSpeedLimit member.
(decideInterest): Use messageFactory member.
* src/BtRequestMessage.cc
(doReceivedAction): Use messageFactory, dispatcher member.
(onQueued): Use dispatcher member.
* src/BtPieceMessage.cc
(doReceivedAction): Use dispatcher member.
(send): Use peerConnection member.
(onWrongPiece): Use requestFactory member.
(handleChokingEvent): Use messageFactory, dispatcher member.
(handleCancelSendingPieceEvent): Use messageFactory, dispatcher
member.
* src/BtMessageDispatcher.h
(BtMessageDispatcherWeakHandle): New type definition.
* src/SimpleBtMessage.cc
(send): Use peerConnection member.
* src/BtRejectMessage.cc
(doReceivedAction): Use dispatcher member.
* src/DefaultBtMessageDispatcher.h
(Option.h): Removed include.
(messageFactory): New variable.
(option): Removed.
(maxUploadSpeedLimit): New variable.
(requestTimeout): New variable.
(DefaultBtMessageDispatcher): Removed option.
Added maxUploadSpeedLimit, requestTimeout.
(setOption): Removed.
(getOption): Removed.
(setMaxUploadSpeedLimit): New function.
(setRequestTimeout): New function.
(setBtMessageFactory): New function.
* src/DefaultBtInteractive.h
(btMessageReceiver):
BtMessageReceiverHandle -> BtMessageReceiverWeakHandle
(dispatcher):
BtMessageDispatcherHandle -> BtMessageReceiverWeakHandle
(btRequestFactory):
BtRequestFactoryHandle -> BtRequestFactoryWeakHandle
(peerConnection):
PeerConnectionHandle -> PeerConnectionWeakHandle
(messageFactory): New variable.
(option): Removed.
(keepAliveInterval): New variable.
(maxDownloadSpeedLimit): New variable.
(DefaultBtInteractive): Added keepAliveInterval,
maxDownloadSpeedLimit.
(setBtMessageReceiver):
BtMessageReceiverHandle -> BtMessageReceiverWeakHandle
(setDispatcher):
BtMessageDispatcherHandle -> BtMessageReceiverWeakHandle
(setBtRequestFactory):
BtRequestFactoryHandle -> BtRequestFactoryWeakHandle
(setPeerConnection):
PeerConnectionHandle -> PeerConnectionWeakHandle
(setOption): Removed.
(setKeepAliveInterval): New function.
(setMaxDownloadSpeedLimit): New function.
(setBtMessageFactory): New function.
* src/BitfieldMan.h
(cachedNumMissingBlock): New variable.
(cachedNumFilteredBlock): New variable.
(cachedCompletedLength): New variable.
(cachedFilteredComletedLength): New variable.
(cachedFilteredTotalLength): New variable.
(countMissingBlockNow): New function.
(countFilteredBlockNow): New function.
(getFilteredTotalLengthNow): New function.
(getCompletedLengthNow): New function.
(getFilteredCompletedLengthNow): New function.
(updateCache): New function.
* src/AbstractBtMessage.h
(BtMessageDispatcher.h): New include.
(PeerConnection.h): New include.
(BtRequestFactory.h): New include
(BtMessageFactory.h): New include.
(dispatcher): New variable.
(messageFactory): New variable.
(peerConnection: New variable.
(setBtMessageDispatcher): New function.
(setPeerConnection): New function.
(setBtMessageFactory): New function.
(setBtRequestFactory): New function.
* src/DefaultBtMessageFactory.cc
(setCommonProperty): Set dispatcher, requestFactory, this,
peerConnection to msg.
* src/BtRegistry.h
(BT_MESSAGE_RECEIVER): New macro.
* src/PeerConnection.h
(PeerConnectionWeakHandle): New type definition.
* src/BtMessageFactory.h
(BtMessageFactoryWeakHandle): New type definition.
* src/BitfieldMan.cc
(BitfieldMan): Added cachedNumMissingBlock,
cachedNumFilteredBlock,
cachedCompletedLength, cachedFilteredComletedLength,
cachedFilteredTotalLength.
Call updateCache().
(countMissingBlock): Return cachedNumMissingBlock.
(countMissingBlockNow): New function.
(countBlock): Return cachedNumFilteredBlock if filterEnabled is
true.
(countFilteredBlockNow): New function.
(setBit): Call updateCache().
(unsetBit): Call updateCache().
(setBitfield): Call updateCache().
(clearAllBit): Call updateCache().
(setAllBit): Use setBitInternal instead of setBit.
Call updateCache().
(addFilter): Call updateCache().
(enableFilter): Call updateCache().
(disableFilter): Call updateCache().
(clearFilter): Call updateCache().
(getFilteredTotalLength): Return cachedFilteredTotalLength.
(getFilteredTotalLengthNow): New function.
(getCompletedLength): Return cachedCompletedLength.
(getCompletedLengthNow): New function.
(getFilteredCompletedLength): Return
cachedFilteredComletedLength.
(getFilteredCompletedLengthNow): New function.
(updateCache): New function.
* src/BtMessageReceiver.h
(BtMessageReceiverWeakHandle): New type definition.
* src/DefaultBtMessageReceiver.cc
(receiveHandshake): Use messageFactory member.
(sendHandshake): Use messageFactory member.
(receiveMessage): Use messageFactory member.
* src/DefaultBtMessageFactory.h
(dispatcher): New variable.
(requestFactory): New variable.
(peerConnection): New variablle.
(setBtMessageDispatcher): New function.
(setBtRequestFactory): New function.
(setPeerConnection): New function.
* src/SharedHandle.h
(RefCount): New class.
(WeakHandle): New class.
* src/PeerObject.h
(BtMessageReceiver.h): New include.
(PeerObject): Added btMessageReceiver.
(btMessageReceiver): New variable.
* src/Util.cc
(countBit): Simplified.
* src/BtCancelMessage.cc
(doReceivedAction): Use dispatcher member.
* src/BtRequestFactory.h
(BtRequestFactoryWeakHandle): New type definition.
* src/PeerStorage.h
(downloadSpeed): int -> uint32_t
(uploadSpeed): int -> uint32_t
(sessionDownloadLength): long long int -> uint64_t
(sessionUploadLength): long long int -> uint64_t
2007-01-16 15:20:26 +00:00
|
|
|
btMessageDispatcher->setRequestTimeout(60);
|
2008-11-03 06:49:02 +00:00
|
|
|
btMessageDispatcher->setPieceStorage(pieceStorage);
|
2006-12-24 06:25:21 +00:00
|
|
|
btMessageDispatcher->addOutstandingRequest(slot);
|
|
|
|
|
|
|
|
btMessageDispatcher->checkRequestSlotAndDoNecessaryThing();
|
|
|
|
|
2008-11-03 06:49:02 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1,
|
2010-01-05 16:01:46 +00:00
|
|
|
btMessageDispatcher->getMessageQueue().size());
|
2008-11-03 06:49:02 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0,
|
2010-01-05 16:01:46 +00:00
|
|
|
btMessageDispatcher->getRequestSlots().size());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcherTest::testIsSendingInProgress() {
|
|
|
|
CPPUNIT_ASSERT(!btMessageDispatcher->isSendingInProgress());
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<MockBtMessage2> msg(new MockBtMessage2());
|
2006-12-24 06:25:21 +00:00
|
|
|
msg->setSendingInProgress(false);
|
|
|
|
btMessageDispatcher->addMessageToQueue(msg);
|
|
|
|
CPPUNIT_ASSERT(!btMessageDispatcher->isSendingInProgress());
|
|
|
|
msg->setSendingInProgress(true);
|
|
|
|
CPPUNIT_ASSERT(btMessageDispatcher->isSendingInProgress());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcherTest::testCountOutstandingRequest() {
|
|
|
|
RequestSlot slot(0, 0, MY_PIECE_LENGTH, 0);
|
|
|
|
btMessageDispatcher->addOutstandingRequest(slot);
|
2008-11-03 06:49:02 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1,
|
2010-01-05 16:01:46 +00:00
|
|
|
btMessageDispatcher->countOutstandingRequest());
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcherTest::testIsOutstandingRequest() {
|
|
|
|
RequestSlot slot(0, 0, MY_PIECE_LENGTH, 0);
|
|
|
|
btMessageDispatcher->addOutstandingRequest(slot);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(btMessageDispatcher->isOutstandingRequest(0, 0));
|
|
|
|
CPPUNIT_ASSERT(!btMessageDispatcher->isOutstandingRequest(0, 1));
|
|
|
|
CPPUNIT_ASSERT(!btMessageDispatcher->isOutstandingRequest(1, 0));
|
|
|
|
CPPUNIT_ASSERT(!btMessageDispatcher->isOutstandingRequest(1, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcherTest::testGetOutstandingRequest() {
|
|
|
|
RequestSlot slot(1, 1024, 16*1024, 10);
|
|
|
|
btMessageDispatcher->addOutstandingRequest(slot);
|
|
|
|
|
|
|
|
RequestSlot s2 = btMessageDispatcher->getOutstandingRequest(1, 1024, 16*1024);
|
|
|
|
CPPUNIT_ASSERT(!RequestSlot::isNull(s2));
|
|
|
|
|
|
|
|
RequestSlot s3 = btMessageDispatcher->getOutstandingRequest(1, 1024, 17*1024);
|
2008-10-19 14:57:58 +00:00
|
|
|
CPPUNIT_ASSERT(RequestSlot::isNull(s3));
|
2006-12-24 06:25:21 +00:00
|
|
|
|
2008-11-03 06:49:02 +00:00
|
|
|
RequestSlot s4 =
|
|
|
|
btMessageDispatcher->getOutstandingRequest(1, 2*1024, 16*1024);
|
2006-12-24 06:25:21 +00:00
|
|
|
CPPUNIT_ASSERT(RequestSlot::isNull(s4));
|
|
|
|
|
|
|
|
RequestSlot s5 = btMessageDispatcher->getOutstandingRequest(2, 1024, 16*1024);
|
|
|
|
CPPUNIT_ASSERT(RequestSlot::isNull(s5));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcherTest::testRemoveOutstandingRequest() {
|
2008-12-29 13:58:21 +00:00
|
|
|
SharedHandle<Piece> piece(new Piece(1, 1024*1024));
|
|
|
|
size_t blockIndex = 0;
|
|
|
|
CPPUNIT_ASSERT(piece->getMissingUnusedBlockIndex(blockIndex));
|
|
|
|
uint32_t begin = blockIndex*piece->getBlockLength();
|
|
|
|
size_t length = piece->getBlockLength(blockIndex);
|
|
|
|
RequestSlot slot(piece->getIndex(), begin, length, blockIndex, piece);
|
2006-12-24 06:25:21 +00:00
|
|
|
btMessageDispatcher->addOutstandingRequest(slot);
|
|
|
|
|
2008-12-29 13:58:21 +00:00
|
|
|
RequestSlot s2 = btMessageDispatcher->getOutstandingRequest
|
|
|
|
(piece->getIndex(), begin, length);
|
2006-12-24 06:25:21 +00:00
|
|
|
CPPUNIT_ASSERT(!RequestSlot::isNull(s2));
|
2008-12-29 13:58:21 +00:00
|
|
|
CPPUNIT_ASSERT(piece->isBlockUsed(blockIndex));
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
btMessageDispatcher->removeOutstandingRequest(s2);
|
|
|
|
|
2008-12-29 13:58:21 +00:00
|
|
|
RequestSlot s3 = btMessageDispatcher->getOutstandingRequest
|
|
|
|
(piece->getIndex(), begin, length);
|
2006-12-24 06:25:21 +00:00
|
|
|
CPPUNIT_ASSERT(RequestSlot::isNull(s3));
|
2008-12-29 13:58:21 +00:00
|
|
|
CPPUNIT_ASSERT(!piece->isBlockUsed(blockIndex));
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|