mirror of https://github.com/aria2/aria2
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_tpull/1/head
parent
d28e6aca15
commit
a1df7a762e
202
ChangeLog
202
ChangeLog
|
@ -1,3 +1,205 @@
|
||||||
|
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-11 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
2007-01-11 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
||||||
|
|
||||||
To add RecoverableException, FatalException:
|
To add RecoverableException, FatalException:
|
||||||
|
|
3
TODO
3
TODO
|
@ -22,6 +22,7 @@
|
||||||
* Add --bt-timeout command line option.
|
* Add --bt-timeout command line option.
|
||||||
* Fix DefaultBtProgressInfoFile.cc: save(), load()
|
* Fix DefaultBtProgressInfoFile.cc: save(), load()
|
||||||
* remove blockIndex
|
* remove blockIndex
|
||||||
|
* Add an ability of seeding
|
||||||
|
|
||||||
* Add piece hash checking
|
* Add piece hash checking
|
||||||
* Stop download after selective download completes
|
* Stop download after selective download completes
|
||||||
* Add an ability of seeding
|
|
||||||
|
|
|
@ -44,6 +44,10 @@
|
||||||
#include "BtEventListener.h"
|
#include "BtEventListener.h"
|
||||||
#include "BtContext.h"
|
#include "BtContext.h"
|
||||||
#include "BtRegistry.h"
|
#include "BtRegistry.h"
|
||||||
|
#include "BtMessageDispatcher.h"
|
||||||
|
#include "PeerConnection.h"
|
||||||
|
#include "BtRequestFactory.h"
|
||||||
|
#include "BtMessageFactory.h"
|
||||||
|
|
||||||
class AbstractBtMessage : public BtMessage {
|
class AbstractBtMessage : public BtMessage {
|
||||||
protected:
|
protected:
|
||||||
|
@ -58,6 +62,14 @@ protected:
|
||||||
|
|
||||||
PeerHandle peer;
|
PeerHandle peer;
|
||||||
|
|
||||||
|
BtMessageDispatcherWeakHandle dispatcher;
|
||||||
|
|
||||||
|
BtMessageFactoryWeakHandle messageFactory;
|
||||||
|
|
||||||
|
BtRequestFactoryWeakHandle requestFactory;
|
||||||
|
|
||||||
|
PeerConnectionWeakHandle peerConnection;
|
||||||
|
|
||||||
BtMessageValidatorHandle validator;
|
BtMessageValidatorHandle validator;
|
||||||
BtEventListeners listeners;
|
BtEventListeners listeners;
|
||||||
const Logger* logger;
|
const Logger* logger;
|
||||||
|
@ -154,6 +166,22 @@ public:
|
||||||
return btContext;
|
return btContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setBtMessageDispatcher(const BtMessageDispatcherWeakHandle& dispatcher)
|
||||||
|
{
|
||||||
|
this->dispatcher = dispatcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setPeerConnection(const PeerConnectionWeakHandle& peerConnection) {
|
||||||
|
this->peerConnection = peerConnection;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setBtMessageFactory(const BtMessageFactoryWeakHandle& factory) {
|
||||||
|
this->messageFactory = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setBtRequestFactory(const BtRequestFactoryWeakHandle& factory) {
|
||||||
|
this->requestFactory = factory;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef SharedHandle<AbstractBtMessage> AbstractBtMessageHandle;
|
typedef SharedHandle<AbstractBtMessage> AbstractBtMessageHandle;
|
||||||
|
|
|
@ -43,7 +43,12 @@ BitfieldMan::BitfieldMan(uint32_t blockLength, uint64_t totalLength)
|
||||||
useBitfield(0),
|
useBitfield(0),
|
||||||
filterBitfield(0),
|
filterBitfield(0),
|
||||||
filterEnabled(false),
|
filterEnabled(false),
|
||||||
randomizer(0)
|
randomizer(0),
|
||||||
|
cachedNumMissingBlock(0),
|
||||||
|
cachedNumFilteredBlock(0),
|
||||||
|
cachedCompletedLength(0),
|
||||||
|
cachedFilteredComletedLength(0),
|
||||||
|
cachedFilteredTotalLength(0)
|
||||||
{
|
{
|
||||||
if(blockLength > 0 && totalLength > 0) {
|
if(blockLength > 0 && totalLength > 0) {
|
||||||
blocks = totalLength/blockLength+(totalLength%blockLength ? 1 : 0);
|
blocks = totalLength/blockLength+(totalLength%blockLength ? 1 : 0);
|
||||||
|
@ -52,6 +57,7 @@ BitfieldMan::BitfieldMan(uint32_t blockLength, uint64_t totalLength)
|
||||||
useBitfield = new unsigned char[bitfieldLength];
|
useBitfield = new unsigned char[bitfieldLength];
|
||||||
memset(bitfield, 0, bitfieldLength);
|
memset(bitfield, 0, bitfieldLength);
|
||||||
memset(useBitfield, 0, bitfieldLength);
|
memset(useBitfield, 0, bitfieldLength);
|
||||||
|
updateCache();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +66,12 @@ BitfieldMan::BitfieldMan(const BitfieldMan& bitfieldMan)
|
||||||
useBitfield(0),
|
useBitfield(0),
|
||||||
filterBitfield(0),
|
filterBitfield(0),
|
||||||
filterEnabled(false),
|
filterEnabled(false),
|
||||||
randomizer(0)
|
randomizer(0),
|
||||||
|
cachedNumMissingBlock(0),
|
||||||
|
cachedNumFilteredBlock(0),
|
||||||
|
cachedCompletedLength(0),
|
||||||
|
cachedFilteredComletedLength(0),
|
||||||
|
cachedFilteredTotalLength(0)
|
||||||
{
|
{
|
||||||
blockLength = bitfieldMan.blockLength;
|
blockLength = bitfieldMan.blockLength;
|
||||||
totalLength = bitfieldMan.totalLength;
|
totalLength = bitfieldMan.totalLength;
|
||||||
|
@ -78,6 +89,7 @@ BitfieldMan::BitfieldMan(const BitfieldMan& bitfieldMan)
|
||||||
filterBitfield = 0;
|
filterBitfield = 0;
|
||||||
}
|
}
|
||||||
this->randomizer = bitfieldMan.randomizer;
|
this->randomizer = bitfieldMan.randomizer;
|
||||||
|
updateCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
BitfieldMan::~BitfieldMan() {
|
BitfieldMan::~BitfieldMan() {
|
||||||
|
@ -361,6 +373,10 @@ BlockIndexes BitfieldMan::getAllMissingIndexes(const unsigned char* peerBitfield
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BitfieldMan::countMissingBlock() const {
|
uint32_t BitfieldMan::countMissingBlock() const {
|
||||||
|
return cachedNumMissingBlock;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t BitfieldMan::countMissingBlockNow() const {
|
||||||
if(filterEnabled) {
|
if(filterEnabled) {
|
||||||
unsigned char* temp = new unsigned char[bitfieldLength];
|
unsigned char* temp = new unsigned char[bitfieldLength];
|
||||||
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
for(uint32_t i = 0; i < bitfieldLength; i++) {
|
||||||
|
@ -377,12 +393,20 @@ uint32_t BitfieldMan::countMissingBlock() const {
|
||||||
|
|
||||||
uint32_t BitfieldMan::countBlock() const {
|
uint32_t BitfieldMan::countBlock() const {
|
||||||
if(filterEnabled) {
|
if(filterEnabled) {
|
||||||
return countSetBit(filterBitfield, bitfieldLength);
|
return cachedNumFilteredBlock;
|
||||||
} else {
|
} else {
|
||||||
return blocks;
|
return blocks;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint32_t BitfieldMan::countFilteredBlockNow() const {
|
||||||
|
if(filterEnabled) {
|
||||||
|
return countSetBit(filterBitfield, bitfieldLength);
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool BitfieldMan::setBitInternal(unsigned char* bitfield, int32_t index, bool on) {
|
bool BitfieldMan::setBitInternal(unsigned char* bitfield, int32_t index, bool on) {
|
||||||
if((int32_t)blocks <= index) { return false; }
|
if((int32_t)blocks <= index) { return false; }
|
||||||
unsigned char mask = 128 >> index%8;
|
unsigned char mask = 128 >> index%8;
|
||||||
|
@ -403,11 +427,15 @@ bool BitfieldMan::unsetUseBit(int32_t index) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BitfieldMan::setBit(int32_t index) {
|
bool BitfieldMan::setBit(int32_t index) {
|
||||||
return setBitInternal(bitfield, index, true);
|
bool b = setBitInternal(bitfield, index, true);
|
||||||
|
updateCache();
|
||||||
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BitfieldMan::unsetBit(int32_t index) {
|
bool BitfieldMan::unsetBit(int32_t index) {
|
||||||
return setBitInternal(bitfield, index, false);
|
bool b = setBitInternal(bitfield, index, false);
|
||||||
|
updateCache();
|
||||||
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BitfieldMan::isAllBitSet() const {
|
bool BitfieldMan::isAllBitSet() const {
|
||||||
|
@ -452,25 +480,29 @@ void BitfieldMan::setBitfield(const unsigned char* bitfield, uint32_t bitfieldLe
|
||||||
}
|
}
|
||||||
memcpy(this->bitfield, bitfield, this->bitfieldLength);
|
memcpy(this->bitfield, bitfield, this->bitfieldLength);
|
||||||
memset(this->useBitfield, 0, this->bitfieldLength);
|
memset(this->useBitfield, 0, this->bitfieldLength);
|
||||||
|
updateCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::clearAllBit() {
|
void BitfieldMan::clearAllBit() {
|
||||||
memset(this->bitfield, 0, this->bitfieldLength);
|
memset(this->bitfield, 0, this->bitfieldLength);
|
||||||
|
updateCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::setAllBit() {
|
void BitfieldMan::setAllBit() {
|
||||||
for(uint32_t i = 0; i < blocks; i++) {
|
for(uint32_t i = 0; i < blocks; i++) {
|
||||||
setBit(i);
|
setBitInternal(bitfield, i, true);
|
||||||
}
|
}
|
||||||
|
updateCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::clearAllUseBit() {
|
void BitfieldMan::clearAllUseBit() {
|
||||||
memset(this->useBitfield, 0, this->bitfieldLength);
|
memset(this->useBitfield, 0, this->bitfieldLength);
|
||||||
|
updateCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::setAllUseBit() {
|
void BitfieldMan::setAllUseBit() {
|
||||||
for(uint32_t i = 0; i < blocks; i++) {
|
for(uint32_t i = 0; i < blocks; i++) {
|
||||||
setUseBit(i);
|
setBitInternal(useBitfield, i, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -488,14 +520,17 @@ void BitfieldMan::addFilter(int64_t offset, uint64_t length) {
|
||||||
for(int i = startBlock; i <= endBlock && i < (int32_t)blocks; i++) {
|
for(int i = startBlock; i <= endBlock && i < (int32_t)blocks; i++) {
|
||||||
setFilterBit(i);
|
setFilterBit(i);
|
||||||
}
|
}
|
||||||
|
updateCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::enableFilter() {
|
void BitfieldMan::enableFilter() {
|
||||||
filterEnabled = true;
|
filterEnabled = true;
|
||||||
|
updateCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::disableFilter() {
|
void BitfieldMan::disableFilter() {
|
||||||
filterEnabled = false;
|
filterEnabled = false;
|
||||||
|
updateCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BitfieldMan::clearFilter() {
|
void BitfieldMan::clearFilter() {
|
||||||
|
@ -504,6 +539,7 @@ void BitfieldMan::clearFilter() {
|
||||||
filterBitfield = 0;
|
filterBitfield = 0;
|
||||||
}
|
}
|
||||||
filterEnabled = false;
|
filterEnabled = false;
|
||||||
|
updateCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BitfieldMan::isFilterEnabled() const {
|
bool BitfieldMan::isFilterEnabled() const {
|
||||||
|
@ -511,6 +547,10 @@ bool BitfieldMan::isFilterEnabled() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t BitfieldMan::getFilteredTotalLength() const {
|
uint64_t BitfieldMan::getFilteredTotalLength() const {
|
||||||
|
return cachedFilteredTotalLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t BitfieldMan::getFilteredTotalLengthNow() const {
|
||||||
if(!filterBitfield) {
|
if(!filterBitfield) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -553,9 +593,26 @@ uint64_t BitfieldMan::getCompletedLength(bool useFilter) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t BitfieldMan::getCompletedLength() const {
|
uint64_t BitfieldMan::getCompletedLength() const {
|
||||||
|
return cachedCompletedLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t BitfieldMan::getCompletedLengthNow() const {
|
||||||
return getCompletedLength(false);
|
return getCompletedLength(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t BitfieldMan::getFilteredCompletedLength() const {
|
uint64_t BitfieldMan::getFilteredCompletedLength() const {
|
||||||
|
return cachedFilteredComletedLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t BitfieldMan::getFilteredCompletedLengthNow() const {
|
||||||
return getCompletedLength(true);
|
return getCompletedLength(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BitfieldMan::updateCache()
|
||||||
|
{
|
||||||
|
cachedNumMissingBlock = countMissingBlockNow();
|
||||||
|
cachedNumFilteredBlock = countFilteredBlockNow();
|
||||||
|
cachedFilteredTotalLength = getFilteredTotalLengthNow();
|
||||||
|
cachedCompletedLength = getCompletedLengthNow();
|
||||||
|
cachedFilteredComletedLength = getFilteredCompletedLengthNow();
|
||||||
|
}
|
||||||
|
|
|
@ -53,6 +53,13 @@ private:
|
||||||
bool filterEnabled;
|
bool filterEnabled;
|
||||||
RandomizerHandle randomizer;
|
RandomizerHandle randomizer;
|
||||||
|
|
||||||
|
// for caching
|
||||||
|
uint32_t cachedNumMissingBlock;
|
||||||
|
uint32_t cachedNumFilteredBlock;
|
||||||
|
uint64_t cachedCompletedLength;
|
||||||
|
uint64_t cachedFilteredComletedLength;
|
||||||
|
uint64_t cachedFilteredTotalLength;
|
||||||
|
|
||||||
uint32_t countSetBit(const unsigned char* bitfield, uint32_t len) const;
|
uint32_t countSetBit(const unsigned char* bitfield, uint32_t len) const;
|
||||||
int32_t getNthBitIndex(const unsigned char bit, uint32_t nth) const;
|
int32_t getNthBitIndex(const unsigned char bit, uint32_t nth) const;
|
||||||
int32_t getMissingIndexRandomly(const unsigned char* bitfield, uint32_t len) const;
|
int32_t getMissingIndexRandomly(const unsigned char* bitfield, uint32_t len) const;
|
||||||
|
@ -92,6 +99,8 @@ public:
|
||||||
} else {
|
} else {
|
||||||
filterBitfield = 0;
|
filterBitfield = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateCache();
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -158,6 +167,10 @@ public:
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
uint32_t countMissingBlock() const;
|
uint32_t countMissingBlock() const;
|
||||||
|
/**
|
||||||
|
* affected by filter
|
||||||
|
*/
|
||||||
|
uint32_t countMissingBlockNow() const;
|
||||||
|
|
||||||
bool setUseBit(int32_t index);
|
bool setUseBit(int32_t index);
|
||||||
bool unsetUseBit(int32_t index);
|
bool unsetUseBit(int32_t index);
|
||||||
|
@ -180,6 +193,10 @@ public:
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
uint32_t countBlock() const;
|
uint32_t countBlock() const;
|
||||||
|
/**
|
||||||
|
* affected by filter
|
||||||
|
*/
|
||||||
|
uint32_t countFilteredBlockNow() const;
|
||||||
|
|
||||||
int32_t getMaxIndex() const { return blocks-1; }
|
int32_t getMaxIndex() const { return blocks-1; }
|
||||||
|
|
||||||
|
@ -204,13 +221,23 @@ public:
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
uint64_t getFilteredTotalLength() const;
|
uint64_t getFilteredTotalLength() const;
|
||||||
|
/**
|
||||||
|
* affected by filter
|
||||||
|
*/
|
||||||
|
uint64_t getFilteredTotalLengthNow() const;
|
||||||
|
|
||||||
uint64_t getCompletedLength() const;
|
uint64_t getCompletedLength() const;
|
||||||
|
|
||||||
|
uint64_t getCompletedLengthNow() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* affected by filter
|
* affected by filter
|
||||||
*/
|
*/
|
||||||
uint64_t getFilteredCompletedLength() const;
|
uint64_t getFilteredCompletedLength() const;
|
||||||
|
/**
|
||||||
|
* affected by filter
|
||||||
|
*/
|
||||||
|
uint64_t getFilteredCompletedLengthNow() const;
|
||||||
|
|
||||||
void setRandomizer(const RandomizerHandle& randomizer) {
|
void setRandomizer(const RandomizerHandle& randomizer) {
|
||||||
this->randomizer = randomizer;
|
this->randomizer = randomizer;
|
||||||
|
@ -219,6 +246,8 @@ public:
|
||||||
RandomizerHandle getRandomizer() const {
|
RandomizerHandle getRandomizer() const {
|
||||||
return randomizer;
|
return randomizer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void updateCache();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // _D_BITFIELD_MAN_H_
|
#endif // _D_BITFIELD_MAN_H_
|
||||||
|
|
|
@ -54,7 +54,7 @@ BtCancelMessageHandle BtCancelMessage::create(const unsigned char* data, uint32_
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtCancelMessage::doReceivedAction() {
|
void BtCancelMessage::doReceivedAction() {
|
||||||
BT_MESSAGE_DISPATCHER(btContext, peer)->doCancelSendingPieceAction(index, begin, length);
|
dispatcher->doCancelSendingPieceAction(index, begin, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t BtCancelMessage::MESSAGE_LENGTH = 17;
|
uint32_t BtCancelMessage::MESSAGE_LENGTH = 17;
|
||||||
|
|
|
@ -51,8 +51,8 @@ BtChokeMessageHandle BtChokeMessage::create(const unsigned char* data, uint32_t
|
||||||
|
|
||||||
void BtChokeMessage::doReceivedAction() {
|
void BtChokeMessage::doReceivedAction() {
|
||||||
peer->peerChoking = true;
|
peer->peerChoking = true;
|
||||||
BT_MESSAGE_DISPATCHER(btContext, peer)->doChokedAction();
|
dispatcher->doChokedAction();
|
||||||
BT_REQUEST_FACTORY(btContext, peer)->doChokedAction();
|
requestFactory->doChokedAction();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BtChokeMessage::sendPredicate() const {
|
bool BtChokeMessage::sendPredicate() const {
|
||||||
|
@ -80,7 +80,7 @@ uint32_t BtChokeMessage::getMessageLength() {
|
||||||
|
|
||||||
void BtChokeMessage::onSendComplete() {
|
void BtChokeMessage::onSendComplete() {
|
||||||
peer->amChoking = true;
|
peer->amChoking = true;
|
||||||
BT_MESSAGE_DISPATCHER(btContext, peer)->doChokingAction();
|
dispatcher->doChokingAction();
|
||||||
}
|
}
|
||||||
|
|
||||||
string BtChokeMessage::toString() const {
|
string BtChokeMessage::toString() const {
|
||||||
|
|
|
@ -78,5 +78,5 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef SharedHandle<BtMessageDispatcher> BtMessageDispatcherHandle;
|
typedef SharedHandle<BtMessageDispatcher> BtMessageDispatcherHandle;
|
||||||
|
typedef WeakHandle<BtMessageDispatcher> BtMessageDispatcherWeakHandle;
|
||||||
#endif // _D_BT_MESSAGE_DISPATCHER_H_
|
#endif // _D_BT_MESSAGE_DISPATCHER_H_
|
||||||
|
|
|
@ -87,5 +87,5 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef SharedHandle<BtMessageFactory> BtMessageFactoryHandle;
|
typedef SharedHandle<BtMessageFactory> BtMessageFactoryHandle;
|
||||||
|
typedef WeakHandle<BtMessageFactory> BtMessageFactoryWeakHandle;
|
||||||
#endif // _D_BT_MESSAGE_FACTORY_H_
|
#endif // _D_BT_MESSAGE_FACTORY_H_
|
||||||
|
|
|
@ -50,5 +50,6 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef SharedHandle<BtMessageReceiver> BtMessageReceiverHandle;
|
typedef SharedHandle<BtMessageReceiver> BtMessageReceiverHandle;
|
||||||
|
typedef WeakHandle<BtMessageReceiver> BtMessageReceiverWeakHandle;
|
||||||
|
|
||||||
#endif // _D_BT_MESSAGE_RECEIVER_H_
|
#endif // _D_BT_MESSAGE_RECEIVER_H_
|
||||||
|
|
|
@ -64,10 +64,9 @@ BtPieceMessageHandle BtPieceMessage::create(const unsigned char* data, uint32_t
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtPieceMessage::doReceivedAction() {
|
void BtPieceMessage::doReceivedAction() {
|
||||||
RequestSlot slot =
|
RequestSlot slot = dispatcher->getOutstandingRequest(index,
|
||||||
BT_MESSAGE_DISPATCHER(btContext, peer)->getOutstandingRequest(index,
|
begin,
|
||||||
begin,
|
blockLength);
|
||||||
blockLength);
|
|
||||||
peer->updateDownloadLength(blockLength);
|
peer->updateDownloadLength(blockLength);
|
||||||
if(!RequestSlot::isNull(slot)) {
|
if(!RequestSlot::isNull(slot)) {
|
||||||
peer->snubbing = false;
|
peer->snubbing = false;
|
||||||
|
@ -85,7 +84,7 @@ void BtPieceMessage::doReceivedAction() {
|
||||||
cuid,
|
cuid,
|
||||||
Util::toHex(piece->getBitfield(),
|
Util::toHex(piece->getBitfield(),
|
||||||
piece->getBitfieldLength()).c_str());
|
piece->getBitfieldLength()).c_str());
|
||||||
BT_MESSAGE_DISPATCHER(btContext, peer)->removeOutstandingRequest(slot);
|
dispatcher->removeOutstandingRequest(slot);
|
||||||
if(piece->pieceComplete()) {
|
if(piece->pieceComplete()) {
|
||||||
if(checkPieceHash(piece)) {
|
if(checkPieceHash(piece)) {
|
||||||
onNewPiece(piece);
|
onNewPiece(piece);
|
||||||
|
@ -134,7 +133,7 @@ void BtPieceMessage::send() {
|
||||||
sendingInProgress = true;
|
sendingInProgress = true;
|
||||||
}
|
}
|
||||||
uint32_t writtenLength
|
uint32_t writtenLength
|
||||||
= PEER_CONNECTION(btContext, peer)->sendMessage(msgHeader+getMessageHeaderLength()-leftDataLength,
|
= peerConnection->sendMessage(msgHeader+getMessageHeaderLength()-leftDataLength,
|
||||||
leftDataLength);
|
leftDataLength);
|
||||||
if(writtenLength == leftDataLength) {
|
if(writtenLength == leftDataLength) {
|
||||||
headerSent = true;
|
headerSent = true;
|
||||||
|
@ -166,7 +165,7 @@ uint32_t BtPieceMessage::sendPieceData(int64_t offset, uint32_t length) const {
|
||||||
if(pieceStorage->getDiskAdaptor()->readData(buf, BUF_SIZE, offset+i*BUF_SIZE) < (int32_t)BUF_SIZE) {
|
if(pieceStorage->getDiskAdaptor()->readData(buf, BUF_SIZE, offset+i*BUF_SIZE) < (int32_t)BUF_SIZE) {
|
||||||
throw new DlAbortEx("Failed to read data from disk.");
|
throw new DlAbortEx("Failed to read data from disk.");
|
||||||
}
|
}
|
||||||
uint32_t ws = PEER_CONNECTION(btContext, peer)->sendMessage(buf, BUF_SIZE);
|
uint32_t ws = peerConnection->sendMessage(buf, BUF_SIZE);
|
||||||
writtenLength += ws;
|
writtenLength += ws;
|
||||||
if(ws != BUF_SIZE) {
|
if(ws != BUF_SIZE) {
|
||||||
return writtenLength;
|
return writtenLength;
|
||||||
|
@ -178,7 +177,7 @@ uint32_t BtPieceMessage::sendPieceData(int64_t offset, uint32_t length) const {
|
||||||
if(pieceStorage->getDiskAdaptor()->readData(buf, rem, offset+iteration*BUF_SIZE) < rem) {
|
if(pieceStorage->getDiskAdaptor()->readData(buf, rem, offset+iteration*BUF_SIZE) < rem) {
|
||||||
throw new DlAbortEx("Failed to read data from disk.");
|
throw new DlAbortEx("Failed to read data from disk.");
|
||||||
}
|
}
|
||||||
uint32_t ws = PEER_CONNECTION(btContext, peer)->sendMessage(buf, rem);
|
uint32_t ws = peerConnection->sendMessage(buf, rem);
|
||||||
writtenLength += ws;
|
writtenLength += ws;
|
||||||
}
|
}
|
||||||
return writtenLength;
|
return writtenLength;
|
||||||
|
@ -206,7 +205,7 @@ void BtPieceMessage::onWrongPiece(const PieceHandle& piece) {
|
||||||
logger->error(MSG_GOT_WRONG_PIECE, cuid, piece->getIndex());
|
logger->error(MSG_GOT_WRONG_PIECE, cuid, piece->getIndex());
|
||||||
erasePieceOnDisk(piece);
|
erasePieceOnDisk(piece);
|
||||||
piece->clearAllBlock();
|
piece->clearAllBlock();
|
||||||
BT_REQUEST_FACTORY(btContext, peer)->removeTargetPiece(piece);
|
requestFactory->removeTargetPiece(piece);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtPieceMessage::erasePieceOnDisk(const PieceHandle& piece) {
|
void BtPieceMessage::erasePieceOnDisk(const PieceHandle& piece) {
|
||||||
|
@ -246,11 +245,10 @@ void BtPieceMessage::handleChokingEvent(const BtEventHandle& event) {
|
||||||
blockLength);
|
blockLength);
|
||||||
|
|
||||||
if(peer->isFastExtensionEnabled()) {
|
if(peer->isFastExtensionEnabled()) {
|
||||||
BtMessageHandle rej =
|
BtMessageHandle rej = messageFactory->createRejectMessage(index,
|
||||||
BT_MESSAGE_FACTORY(btContext, peer)->createRejectMessage(index,
|
begin,
|
||||||
begin,
|
blockLength);
|
||||||
blockLength);
|
dispatcher->addMessageToQueue(rej);
|
||||||
BT_MESSAGE_DISPATCHER(btContext, peer)->addMessageToQueue(rej);
|
|
||||||
}
|
}
|
||||||
invalidate = true;
|
invalidate = true;
|
||||||
}
|
}
|
||||||
|
@ -276,11 +274,10 @@ void BtPieceMessage::handleCancelSendingPieceEvent(const BtEventHandle& event) {
|
||||||
" message received. index=%d, begin=%d, length=%u",
|
" message received. index=%d, begin=%d, length=%u",
|
||||||
cuid, index, begin, blockLength);
|
cuid, index, begin, blockLength);
|
||||||
if(peer->isFastExtensionEnabled()) {
|
if(peer->isFastExtensionEnabled()) {
|
||||||
BtMessageHandle rej =
|
BtMessageHandle rej = messageFactory->createRejectMessage(index,
|
||||||
BT_MESSAGE_FACTORY(btContext, peer)->createRejectMessage(index,
|
begin,
|
||||||
begin,
|
blockLength);
|
||||||
blockLength);
|
dispatcher->addMessageToQueue(rej);
|
||||||
BT_MESSAGE_DISPATCHER(btContext, peer)->addMessageToQueue(rej);
|
|
||||||
}
|
}
|
||||||
invalidate = true;
|
invalidate = true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,6 +125,9 @@ PEER_OBJECT_CLUSTER(btContext)->getHandle(peer->getId())
|
||||||
#define BT_MESSAGE_DISPATCHER(btContext, peer) \
|
#define BT_MESSAGE_DISPATCHER(btContext, peer) \
|
||||||
PEER_OBJECT(btContext, peer)->btMessageDispatcher
|
PEER_OBJECT(btContext, peer)->btMessageDispatcher
|
||||||
|
|
||||||
|
#define BT_MESSAGE_RECEIVER(btContext, peer) \
|
||||||
|
PEER_OBJECT(btContext, peer)->btMessageReceiver
|
||||||
|
|
||||||
#define BT_MESSAGE_FACTORY(btContext, peer) \
|
#define BT_MESSAGE_FACTORY(btContext, peer) \
|
||||||
PEER_OBJECT(btContext, peer)->btMessageFactory
|
PEER_OBJECT(btContext, peer)->btMessageFactory
|
||||||
|
|
||||||
|
|
|
@ -60,12 +60,11 @@ void BtRejectMessage::doReceivedAction() {
|
||||||
}
|
}
|
||||||
// TODO Current implementation does not close a connection even if
|
// TODO Current implementation does not close a connection even if
|
||||||
// a request for this reject message has never sent.
|
// a request for this reject message has never sent.
|
||||||
RequestSlot slot =
|
RequestSlot slot = dispatcher->getOutstandingRequest(index, begin, length);
|
||||||
BT_MESSAGE_DISPATCHER(btContext, peer)->getOutstandingRequest(index, begin, length);
|
|
||||||
if(RequestSlot::isNull(slot)) {
|
if(RequestSlot::isNull(slot)) {
|
||||||
//throw new DlAbortEx("reject recieved, but it is not in the request slots.");
|
//throw new DlAbortEx("reject recieved, but it is not in the request slots.");
|
||||||
} else {
|
} else {
|
||||||
BT_MESSAGE_DISPATCHER(btContext, peer)->removeOutstandingRequest(slot);
|
dispatcher->removeOutstandingRequest(slot);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,5 +70,6 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef SharedHandle<BtRequestFactory> BtRequestFactoryHandle;
|
typedef SharedHandle<BtRequestFactory> BtRequestFactoryHandle;
|
||||||
|
typedef WeakHandle<BtRequestFactory> BtRequestFactoryWeakHandle;
|
||||||
|
|
||||||
#endif // _D_BT_REQUEST_FACTORY_H_
|
#endif // _D_BT_REQUEST_FACTORY_H_
|
||||||
|
|
|
@ -58,18 +58,16 @@ void BtRequestMessage::doReceivedAction() {
|
||||||
if(pieceStorage->hasPiece(index) &&
|
if(pieceStorage->hasPiece(index) &&
|
||||||
(!peer->amChoking ||
|
(!peer->amChoking ||
|
||||||
peer->amChoking && peer->isInAmAllowedIndexSet(index))) {
|
peer->amChoking && peer->isInAmAllowedIndexSet(index))) {
|
||||||
BtMessageHandle msg =
|
BtMessageHandle msg = messageFactory->createPieceMessage(index,
|
||||||
BT_MESSAGE_FACTORY(btContext, peer)->createPieceMessage(index,
|
begin,
|
||||||
begin,
|
length);
|
||||||
length);
|
dispatcher->addMessageToQueue(msg);
|
||||||
BT_MESSAGE_DISPATCHER(btContext, peer)->addMessageToQueue(msg);
|
|
||||||
} else {
|
} else {
|
||||||
if(peer->isFastExtensionEnabled()) {
|
if(peer->isFastExtensionEnabled()) {
|
||||||
BtMessageHandle msg =
|
BtMessageHandle msg = messageFactory->createRejectMessage(index,
|
||||||
BT_MESSAGE_FACTORY(btContext, peer)->createRejectMessage(index,
|
begin,
|
||||||
begin,
|
length);
|
||||||
length);
|
dispatcher->addMessageToQueue(msg);
|
||||||
BT_MESSAGE_DISPATCHER(btContext, peer)->addMessageToQueue(msg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,7 +104,7 @@ string BtRequestMessage::toString() const {
|
||||||
|
|
||||||
void BtRequestMessage::onQueued() {
|
void BtRequestMessage::onQueued() {
|
||||||
RequestSlot requestSlot(index, begin, length, blockIndex);
|
RequestSlot requestSlot(index, begin, length, blockIndex);
|
||||||
BT_MESSAGE_DISPATCHER(btContext, peer)->addOutstandingRequest(requestSlot);
|
dispatcher->addOutstandingRequest(requestSlot);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BtRequestMessage::BtAbortOutstandingRequestEventListener::canHandle(const BtEventHandle& event) {
|
bool BtRequestMessage::BtAbortOutstandingRequestEventListener::canHandle(const BtEventHandle& event) {
|
||||||
|
|
|
@ -43,10 +43,8 @@
|
||||||
#include "DlAbortEx.h"
|
#include "DlAbortEx.h"
|
||||||
|
|
||||||
void DefaultBtInteractive::initiateHandshake() {
|
void DefaultBtInteractive::initiateHandshake() {
|
||||||
BtMessageHandle message =
|
BtMessageHandle message = messageFactory->createHandshakeMessage(btContext->getInfoHash(),
|
||||||
BT_MESSAGE_FACTORY(btContext, peer)->
|
btContext->getPeerId());
|
||||||
createHandshakeMessage(btContext->getInfoHash(),
|
|
||||||
btContext->getPeerId());
|
|
||||||
dispatcher->addMessageToQueue(message);
|
dispatcher->addMessageToQueue(message);
|
||||||
dispatcher->sendMessages();
|
dispatcher->sendMessages();
|
||||||
}
|
}
|
||||||
|
@ -79,18 +77,17 @@ void DefaultBtInteractive::doPostHandshakeProcessing() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefaultBtInteractive::addBitfieldMessageToQueue() {
|
void DefaultBtInteractive::addBitfieldMessageToQueue() {
|
||||||
BtMessageFactoryHandle factory = BT_MESSAGE_FACTORY(btContext, peer);
|
|
||||||
if(peer->isFastExtensionEnabled()) {
|
if(peer->isFastExtensionEnabled()) {
|
||||||
if(pieceStorage->downloadFinished()) {
|
if(pieceStorage->downloadFinished()) {
|
||||||
dispatcher->addMessageToQueue(factory->createHaveAllMessage());
|
dispatcher->addMessageToQueue(messageFactory->createHaveAllMessage());
|
||||||
} else if(pieceStorage->getCompletedLength() > 0) {
|
} else if(pieceStorage->getCompletedLength() > 0) {
|
||||||
dispatcher->addMessageToQueue(factory->createBitfieldMessage());
|
dispatcher->addMessageToQueue(messageFactory->createBitfieldMessage());
|
||||||
} else {
|
} else {
|
||||||
dispatcher->addMessageToQueue(factory->createHaveNoneMessage());
|
dispatcher->addMessageToQueue(messageFactory->createHaveNoneMessage());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if(pieceStorage->getCompletedLength() > 0) {
|
if(pieceStorage->getCompletedLength() > 0) {
|
||||||
dispatcher->addMessageToQueue(factory->createBitfieldMessage());
|
dispatcher->addMessageToQueue(messageFactory->createBitfieldMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -103,8 +100,7 @@ void DefaultBtInteractive::addAllowedFastMessageToQueue() {
|
||||||
allowedFastSetSize);
|
allowedFastSetSize);
|
||||||
for(Integers::const_iterator itr = fastSet.begin();
|
for(Integers::const_iterator itr = fastSet.begin();
|
||||||
itr != fastSet.end(); itr++) {
|
itr != fastSet.end(); itr++) {
|
||||||
dispatcher->addMessageToQueue(BT_MESSAGE_FACTORY(btContext, peer)->
|
dispatcher->addMessageToQueue(messageFactory->createAllowedFastMessage(*itr));
|
||||||
createAllowedFastMessage(*itr));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -112,38 +108,35 @@ void DefaultBtInteractive::addAllowedFastMessageToQueue() {
|
||||||
void DefaultBtInteractive::decideChoking() {
|
void DefaultBtInteractive::decideChoking() {
|
||||||
if(peer->shouldBeChoking()) {
|
if(peer->shouldBeChoking()) {
|
||||||
if(!peer->amChoking) {
|
if(!peer->amChoking) {
|
||||||
dispatcher->addMessageToQueue(BT_MESSAGE_FACTORY(btContext, peer)->
|
dispatcher->addMessageToQueue(messageFactory->createChokeMessage());
|
||||||
createChokeMessage());
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if(peer->amChoking) {
|
if(peer->amChoking) {
|
||||||
dispatcher->addMessageToQueue(BT_MESSAGE_FACTORY(btContext, peer)->
|
dispatcher->addMessageToQueue(messageFactory->createUnchokeMessage());
|
||||||
createUnchokeMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefaultBtInteractive::checkHave() {
|
void DefaultBtInteractive::checkHave() {
|
||||||
BtMessageFactoryHandle factory = BT_MESSAGE_FACTORY(btContext, peer);
|
|
||||||
Integers indexes =
|
Integers indexes =
|
||||||
pieceStorage->getAdvertisedPieceIndexes(cuid, haveCheckPoint);
|
pieceStorage->getAdvertisedPieceIndexes(cuid, haveCheckPoint);
|
||||||
haveCheckPoint.reset();
|
haveCheckPoint.reset();
|
||||||
if(indexes.size() >= 20) {
|
if(indexes.size() >= 20) {
|
||||||
if(peer->isFastExtensionEnabled() && pieceStorage->downloadFinished()) {
|
if(peer->isFastExtensionEnabled() && pieceStorage->downloadFinished()) {
|
||||||
dispatcher->addMessageToQueue(factory->createHaveAllMessage());
|
dispatcher->addMessageToQueue(messageFactory->createHaveAllMessage());
|
||||||
} else {
|
} else {
|
||||||
dispatcher->addMessageToQueue(factory->createBitfieldMessage());
|
dispatcher->addMessageToQueue(messageFactory->createBitfieldMessage());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for(Integers::iterator itr = indexes.begin(); itr != indexes.end(); itr++) {
|
for(Integers::iterator itr = indexes.begin(); itr != indexes.end(); itr++) {
|
||||||
dispatcher->addMessageToQueue(factory->createHaveMessage(*itr));
|
dispatcher->addMessageToQueue(messageFactory->createHaveMessage(*itr));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefaultBtInteractive::sendKeepAlive() {
|
void DefaultBtInteractive::sendKeepAlive() {
|
||||||
if(keepAliveCheckPoint.elapsed(option->getAsInt(PREF_BT_KEEP_ALIVE_INTERVAL))) {
|
if(keepAliveCheckPoint.elapsed(keepAliveInterval)) {
|
||||||
dispatcher->addMessageToQueue(BT_MESSAGE_FACTORY(btContext, peer)->createKeepAliveMessage());
|
dispatcher->addMessageToQueue(messageFactory->createKeepAliveMessage());
|
||||||
dispatcher->sendMessages();
|
dispatcher->sendMessages();
|
||||||
keepAliveCheckPoint.reset();
|
keepAliveCheckPoint.reset();
|
||||||
}
|
}
|
||||||
|
@ -151,10 +144,9 @@ void DefaultBtInteractive::sendKeepAlive() {
|
||||||
|
|
||||||
void DefaultBtInteractive::receiveMessages() {
|
void DefaultBtInteractive::receiveMessages() {
|
||||||
for(int i = 0; i < 50; i++) {
|
for(int i = 0; i < 50; i++) {
|
||||||
int maxSpeedLimit = option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT);
|
if(maxDownloadSpeedLimit > 0) {
|
||||||
if(maxSpeedLimit > 0) {
|
|
||||||
TransferStat stat = peerStorage->calculateStat();
|
TransferStat stat = peerStorage->calculateStat();
|
||||||
if(maxSpeedLimit < stat.downloadSpeed) {
|
if(maxDownloadSpeedLimit < stat.downloadSpeed) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -190,15 +182,13 @@ void DefaultBtInteractive::decideInterest() {
|
||||||
if(!peer->amInterested) {
|
if(!peer->amInterested) {
|
||||||
logger->debug("CUID#%d - Interested in the peer", cuid);
|
logger->debug("CUID#%d - Interested in the peer", cuid);
|
||||||
dispatcher->
|
dispatcher->
|
||||||
addMessageToQueue(BT_MESSAGE_FACTORY(btContext, peer)->
|
addMessageToQueue(messageFactory->createInterestedMessage());
|
||||||
createInterestedMessage());
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if(peer->amInterested) {
|
if(peer->amInterested) {
|
||||||
logger->debug("CUID#%d - Not interested in the peer", cuid);
|
logger->debug("CUID#%d - Not interested in the peer", cuid);
|
||||||
dispatcher->
|
dispatcher->
|
||||||
addMessageToQueue(BT_MESSAGE_FACTORY(btContext, peer)->
|
addMessageToQueue(messageFactory->createNotInterestedMessage());
|
||||||
createNotInterestedMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -89,17 +89,19 @@ private:
|
||||||
BtContextHandle btContext;
|
BtContextHandle btContext;
|
||||||
PeerStorageHandle peerStorage;
|
PeerStorageHandle peerStorage;
|
||||||
PieceStorageHandle pieceStorage;
|
PieceStorageHandle pieceStorage;
|
||||||
BtMessageReceiverHandle btMessageReceiver;
|
BtMessageReceiverWeakHandle btMessageReceiver;
|
||||||
BtMessageDispatcherHandle dispatcher;
|
BtMessageDispatcherWeakHandle dispatcher;
|
||||||
BtRequestFactoryHandle btRequestFactory;
|
BtRequestFactoryWeakHandle btRequestFactory;
|
||||||
PeerConnectionHandle peerConnection;
|
PeerConnectionWeakHandle peerConnection;
|
||||||
|
BtMessageFactoryWeakHandle messageFactory;
|
||||||
const Logger* logger;
|
const Logger* logger;
|
||||||
uint32_t allowedFastSetSize;
|
uint32_t allowedFastSetSize;
|
||||||
Time haveCheckPoint;
|
Time haveCheckPoint;
|
||||||
Time keepAliveCheckPoint;
|
Time keepAliveCheckPoint;
|
||||||
Time floodingCheckPoint;
|
Time floodingCheckPoint;
|
||||||
FloodingStat floodingStat;
|
FloodingStat floodingStat;
|
||||||
const Option* option;
|
uint32_t keepAliveInterval;
|
||||||
|
uint32_t maxDownloadSpeedLimit;
|
||||||
|
|
||||||
static const uint32_t FLOODING_CHECK_INTERVAL = 5;
|
static const uint32_t FLOODING_CHECK_INTERVAL = 5;
|
||||||
|
|
||||||
|
@ -122,14 +124,12 @@ public:
|
||||||
btRequestFactory(0),
|
btRequestFactory(0),
|
||||||
peerConnection(0),
|
peerConnection(0),
|
||||||
logger(LogFactory::getInstance()),
|
logger(LogFactory::getInstance()),
|
||||||
allowedFastSetSize(10)
|
allowedFastSetSize(10),
|
||||||
{
|
keepAliveInterval(120),
|
||||||
logger->debug("DefaultBtInteractive instantiated.");
|
maxDownloadSpeedLimit(0)
|
||||||
}
|
{}
|
||||||
|
|
||||||
virtual ~DefaultBtInteractive() {
|
virtual ~DefaultBtInteractive() {}
|
||||||
logger->debug("DefaultBtInteractive deleted.");
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void initiateHandshake();
|
virtual void initiateHandshake();
|
||||||
|
|
||||||
|
@ -169,24 +169,32 @@ public:
|
||||||
this->pieceStorage = PIECE_STORAGE(btContext);
|
this->pieceStorage = PIECE_STORAGE(btContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setBtMessageReceiver(const BtMessageReceiverHandle& receiver) {
|
void setBtMessageReceiver(const BtMessageReceiverWeakHandle& receiver) {
|
||||||
this->btMessageReceiver = receiver;
|
this->btMessageReceiver = receiver;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setDispatcher(const BtMessageDispatcherHandle& dispatcher) {
|
void setDispatcher(const BtMessageDispatcherWeakHandle& dispatcher) {
|
||||||
this->dispatcher = dispatcher;
|
this->dispatcher = dispatcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setBtRequestFactory(const BtRequestFactoryHandle& factory) {
|
void setBtRequestFactory(const BtRequestFactoryWeakHandle& factory) {
|
||||||
this->btRequestFactory = factory;
|
this->btRequestFactory = factory;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setPeerConnection(const PeerConnectionHandle& peerConnection) {
|
void setPeerConnection(const PeerConnectionWeakHandle& peerConnection) {
|
||||||
this->peerConnection = peerConnection;
|
this->peerConnection = peerConnection;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setOption(const Option* option) {
|
void setKeepAliveInterval(uint32_t keepAliveInterval) {
|
||||||
this->option = option;
|
this->keepAliveInterval = keepAliveInterval;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setMaxDownloadSpeedLimit(uint32_t maxDownloadSpeedLimit) {
|
||||||
|
this->maxDownloadSpeedLimit = maxDownloadSpeedLimit;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setBtMessageFactory(const BtMessageFactoryWeakHandle& factory) {
|
||||||
|
this->messageFactory = factory;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -58,13 +58,12 @@ void DefaultBtMessageDispatcher::addMessageToQueue(const BtMessages& btMessages)
|
||||||
|
|
||||||
void DefaultBtMessageDispatcher::sendMessages() {
|
void DefaultBtMessageDispatcher::sendMessages() {
|
||||||
BtMessages tempQueue;
|
BtMessages tempQueue;
|
||||||
int32_t uploadLimit = option->getAsInt(PREF_MAX_UPLOAD_LIMIT);
|
|
||||||
while(messageQueue.size() > 0) {
|
while(messageQueue.size() > 0) {
|
||||||
BtMessageHandle msg = messageQueue.front();
|
BtMessageHandle msg = messageQueue.front();
|
||||||
messageQueue.pop_front();
|
messageQueue.pop_front();
|
||||||
if(uploadLimit > 0) {
|
if(maxUploadSpeedLimit > 0) {
|
||||||
TransferStat stat = peerStorage->calculateStat();
|
TransferStat stat = peerStorage->calculateStat();
|
||||||
if(uploadLimit < stat.getUploadSpeed() &&
|
if(maxUploadSpeedLimit < stat.getUploadSpeed() &&
|
||||||
msg->isUploading() && !msg->isSendingInProgress()) {
|
msg->isUploading() && !msg->isSendingInProgress()) {
|
||||||
tempQueue.push_back(msg);
|
tempQueue.push_back(msg);
|
||||||
continue;
|
continue;
|
||||||
|
@ -168,7 +167,7 @@ void DefaultBtMessageDispatcher::checkRequestSlotAndDoNecessaryThing()
|
||||||
itr != requestSlots.end();) {
|
itr != requestSlots.end();) {
|
||||||
RequestSlot& slot = *itr;
|
RequestSlot& slot = *itr;
|
||||||
PieceHandle piece = pieceStorage->getPiece(slot.getIndex());
|
PieceHandle piece = pieceStorage->getPiece(slot.getIndex());
|
||||||
if(slot.isTimeout(option->getAsInt(PREF_BT_REQUEST_TIMEOUT))) {
|
if(slot.isTimeout(requestTimeout)) {
|
||||||
logger->debug("CUID#%d - Deleting request slot blockIndex=%d"
|
logger->debug("CUID#%d - Deleting request slot blockIndex=%d"
|
||||||
" because of time out",
|
" because of time out",
|
||||||
cuid,
|
cuid,
|
||||||
|
@ -181,10 +180,9 @@ void DefaultBtMessageDispatcher::checkRequestSlotAndDoNecessaryThing()
|
||||||
" the block has been acquired.",
|
" the block has been acquired.",
|
||||||
cuid,
|
cuid,
|
||||||
slot.getBlockIndex());
|
slot.getBlockIndex());
|
||||||
addMessageToQueue(BT_MESSAGE_FACTORY(btContext, peer)->
|
addMessageToQueue(messageFactory->createCancelMessage(slot.getIndex(),
|
||||||
createCancelMessage(slot.getIndex(),
|
slot.getBegin(),
|
||||||
slot.getBegin(),
|
slot.getLength()));
|
||||||
slot.getLength()));
|
|
||||||
itr = requestSlots.erase(itr);
|
itr = requestSlots.erase(itr);
|
||||||
} else {
|
} else {
|
||||||
itr++;
|
itr++;
|
||||||
|
|
|
@ -42,7 +42,6 @@
|
||||||
#include "RequestSlot.h"
|
#include "RequestSlot.h"
|
||||||
#include "BtMessage.h"
|
#include "BtMessage.h"
|
||||||
#include "Peer.h"
|
#include "Peer.h"
|
||||||
#include "Option.h"
|
|
||||||
#include "LogFactory.h"
|
#include "LogFactory.h"
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
#include "BtRegistry.h"
|
#include "BtRegistry.h"
|
||||||
|
@ -55,8 +54,10 @@ private:
|
||||||
BtContextHandle btContext;
|
BtContextHandle btContext;
|
||||||
PeerStorageHandle peerStorage;
|
PeerStorageHandle peerStorage;
|
||||||
PieceStorageHandle pieceStorage;
|
PieceStorageHandle pieceStorage;
|
||||||
|
BtMessageFactoryWeakHandle messageFactory;
|
||||||
PeerHandle peer;
|
PeerHandle peer;
|
||||||
const Option* option;
|
uint32_t maxUploadSpeedLimit;
|
||||||
|
uint32_t requestTimeout;
|
||||||
const Logger* logger;
|
const Logger* logger;
|
||||||
public:
|
public:
|
||||||
DefaultBtMessageDispatcher():
|
DefaultBtMessageDispatcher():
|
||||||
|
@ -65,17 +66,12 @@ public:
|
||||||
peerStorage(0),
|
peerStorage(0),
|
||||||
pieceStorage(0),
|
pieceStorage(0),
|
||||||
peer(0),
|
peer(0),
|
||||||
option(0),
|
maxUploadSpeedLimit(0),
|
||||||
logger(LogFactory::getInstance())
|
requestTimeout(0),
|
||||||
{
|
logger(LogFactory::getInstance()) {}
|
||||||
logger->debug("DefaultBtMessageDispatcher::instantiated");
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~DefaultBtMessageDispatcher()
|
|
||||||
{
|
|
||||||
logger->debug("DefaultBtMessageDispatcher::deleted");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
virtual ~DefaultBtMessageDispatcher() {}
|
||||||
|
|
||||||
virtual void addMessageToQueue(const BtMessageHandle& btMessage);
|
virtual void addMessageToQueue(const BtMessageHandle& btMessage);
|
||||||
|
|
||||||
virtual void addMessageToQueue(const BtMessages& btMessages);
|
virtual void addMessageToQueue(const BtMessages& btMessages);
|
||||||
|
@ -132,14 +128,6 @@ public:
|
||||||
return messageQueue;
|
return messageQueue;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setOption(const Option* option) {
|
|
||||||
this->option = option;
|
|
||||||
}
|
|
||||||
|
|
||||||
const Option* getOption() const {
|
|
||||||
return option;
|
|
||||||
}
|
|
||||||
|
|
||||||
RequestSlots& getRequestSlots() {
|
RequestSlots& getRequestSlots() {
|
||||||
return requestSlots;
|
return requestSlots;
|
||||||
}
|
}
|
||||||
|
@ -157,6 +145,18 @@ public:
|
||||||
void setCuid(int32_t cuid) {
|
void setCuid(int32_t cuid) {
|
||||||
this->cuid = cuid;
|
this->cuid = cuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setMaxUploadSpeedLimit(uint32_t maxUploadSpeedLimit) {
|
||||||
|
this->maxUploadSpeedLimit = maxUploadSpeedLimit;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setRequestTimeout(uint32_t requestTimeout) {
|
||||||
|
this->requestTimeout = requestTimeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setBtMessageFactory(const BtMessageFactoryWeakHandle& factory) {
|
||||||
|
this->messageFactory = factory;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef SharedHandle<DefaultBtMessageDispatcher> DefaultBtMessageDispatcherHandle;
|
typedef SharedHandle<DefaultBtMessageDispatcher> DefaultBtMessageDispatcherHandle;
|
||||||
|
|
|
@ -172,7 +172,11 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, uint32_t dat
|
||||||
void DefaultBtMessageFactory::setCommonProperty(const AbstractBtMessageHandle& msg) {
|
void DefaultBtMessageFactory::setCommonProperty(const AbstractBtMessageHandle& msg) {
|
||||||
msg->setCuid(cuid);
|
msg->setCuid(cuid);
|
||||||
msg->setPeer(peer);
|
msg->setPeer(peer);
|
||||||
msg->setBtContext(btContext);
|
msg->setBtContext(btContext);
|
||||||
|
msg->setBtMessageDispatcher(dispatcher);
|
||||||
|
msg->setBtRequestFactory(requestFactory);
|
||||||
|
msg->setBtMessageFactory(this);
|
||||||
|
msg->setPeerConnection(peerConnection);
|
||||||
}
|
}
|
||||||
|
|
||||||
BtMessageHandle
|
BtMessageHandle
|
||||||
|
|
|
@ -47,6 +47,12 @@ private:
|
||||||
PieceStorageHandle pieceStorage;
|
PieceStorageHandle pieceStorage;
|
||||||
PeerHandle peer;
|
PeerHandle peer;
|
||||||
|
|
||||||
|
BtMessageDispatcherWeakHandle dispatcher;
|
||||||
|
|
||||||
|
BtRequestFactoryWeakHandle requestFactory;
|
||||||
|
|
||||||
|
PeerConnectionWeakHandle peerConnection;
|
||||||
|
|
||||||
void setCommonProperty(const AbstractBtMessageHandle& msg);
|
void setCommonProperty(const AbstractBtMessageHandle& msg);
|
||||||
public:
|
public:
|
||||||
DefaultBtMessageFactory():cuid(0),
|
DefaultBtMessageFactory():cuid(0),
|
||||||
|
@ -124,6 +130,20 @@ public:
|
||||||
void setCuid(int32_t cuid) {
|
void setCuid(int32_t cuid) {
|
||||||
this->cuid = cuid;
|
this->cuid = cuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setBtMessageDispatcher(const BtMessageDispatcherWeakHandle& dispatcher)
|
||||||
|
{
|
||||||
|
this->dispatcher = dispatcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setBtRequestFactory(const BtRequestFactoryWeakHandle& factory) {
|
||||||
|
this->requestFactory = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setPeerConnection(const PeerConnectionWeakHandle& connection) {
|
||||||
|
this->peerConnection = connection;
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef SharedHandle<DefaultBtMessageFactory> DefaultBtMessageFactoryHandle;
|
typedef SharedHandle<DefaultBtMessageFactory> DefaultBtMessageFactoryHandle;
|
||||||
|
|
|
@ -50,8 +50,7 @@ BtMessageHandle DefaultBtMessageReceiver::receiveHandshake(bool quickReply) {
|
||||||
if(!retval) {
|
if(!retval) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
BtHandshakeMessageHandle msg =
|
BtHandshakeMessageHandle msg = messageFactory->createHandshakeMessage(data, dataLength);
|
||||||
BT_MESSAGE_FACTORY(btContext, peer)->createHandshakeMessage(data, dataLength);
|
|
||||||
Errors errors;
|
Errors errors;
|
||||||
if(msg->validate(errors)) {
|
if(msg->validate(errors)) {
|
||||||
if(msg->isFastExtensionSupported()) {
|
if(msg->isFastExtensionSupported()) {
|
||||||
|
@ -70,8 +69,8 @@ BtMessageHandle DefaultBtMessageReceiver::receiveAndSendHandshake() {
|
||||||
|
|
||||||
void DefaultBtMessageReceiver::sendHandshake() {
|
void DefaultBtMessageReceiver::sendHandshake() {
|
||||||
BtHandshakeMessageHandle msg =
|
BtHandshakeMessageHandle msg =
|
||||||
BT_MESSAGE_FACTORY(btContext, peer)->createHandshakeMessage(btContext->getInfoHash(),
|
messageFactory->createHandshakeMessage(btContext->getInfoHash(),
|
||||||
btContext->getPeerId());
|
btContext->getPeerId());
|
||||||
dispatcher->addMessageToQueue(msg);
|
dispatcher->addMessageToQueue(msg);
|
||||||
dispatcher->sendMessages();
|
dispatcher->sendMessages();
|
||||||
}
|
}
|
||||||
|
@ -82,8 +81,7 @@ BtMessageHandle DefaultBtMessageReceiver::receiveMessage() {
|
||||||
if(!peerConnection->receiveMessage(data, dataLength)) {
|
if(!peerConnection->receiveMessage(data, dataLength)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
BtMessageHandle msg =
|
BtMessageHandle msg = messageFactory->createBtMessage(data, dataLength);
|
||||||
BT_MESSAGE_FACTORY(btContext, peer)->createBtMessage(data, dataLength);
|
|
||||||
Errors errors;
|
Errors errors;
|
||||||
if(msg->validate(errors)) {
|
if(msg->validate(errors)) {
|
||||||
return msg;
|
return msg;
|
||||||
|
|
|
@ -50,8 +50,9 @@ private:
|
||||||
bool handshakeSent;
|
bool handshakeSent;
|
||||||
BtContextHandle btContext;
|
BtContextHandle btContext;
|
||||||
PeerHandle peer;
|
PeerHandle peer;
|
||||||
PeerConnectionHandle peerConnection;
|
PeerConnectionWeakHandle peerConnection;
|
||||||
BtMessageDispatcherHandle dispatcher;
|
BtMessageDispatcherWeakHandle dispatcher;
|
||||||
|
BtMessageFactoryWeakHandle messageFactory;
|
||||||
const Logger* logger;
|
const Logger* logger;
|
||||||
|
|
||||||
void sendHandshake();
|
void sendHandshake();
|
||||||
|
@ -86,11 +87,11 @@ public:
|
||||||
return cuid;
|
return cuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setPeerConnection(const PeerConnectionHandle& peerConnection) {
|
void setPeerConnection(const PeerConnectionWeakHandle& peerConnection) {
|
||||||
this->peerConnection = peerConnection;
|
this->peerConnection = peerConnection;
|
||||||
}
|
}
|
||||||
|
|
||||||
PeerConnectionHandle getPeerConnection() const {
|
PeerConnectionWeakHandle getPeerConnection() const {
|
||||||
return peerConnection;
|
return peerConnection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,9 +111,13 @@ public:
|
||||||
return peer;
|
return peer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setDispatcher(const BtMessageDispatcherHandle& dispatcher) {
|
void setDispatcher(const BtMessageDispatcherWeakHandle& dispatcher) {
|
||||||
this->dispatcher = dispatcher;
|
this->dispatcher = dispatcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setBtMessageFactory(const BtMessageFactoryWeakHandle& factory) {
|
||||||
|
this->messageFactory = factory;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef SharedHandle<DefaultBtMessageReceiver> DefaultBtMessageReceiverHandle;
|
typedef SharedHandle<DefaultBtMessageReceiver> DefaultBtMessageReceiverHandle;
|
||||||
|
|
|
@ -84,8 +84,7 @@ BtMessages DefaultBtRequestFactory::createRequestMessages(uint32_t max) {
|
||||||
int32_t blockIndex;
|
int32_t blockIndex;
|
||||||
while(requests.size() < (size_t)max &&
|
while(requests.size() < (size_t)max &&
|
||||||
(blockIndex = piece->getMissingUnusedBlockIndex()) != -1) {
|
(blockIndex = piece->getMissingUnusedBlockIndex()) != -1) {
|
||||||
requests.push_back(BT_MESSAGE_FACTORY(btContext, peer)->
|
requests.push_back(messageFactory->createRequestMessage(piece, blockIndex));
|
||||||
createRequestMessage(piece, blockIndex));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return requests;
|
return requests;
|
||||||
|
@ -104,8 +103,7 @@ BtMessages DefaultBtRequestFactory::createRequestMessagesOnEndGame(uint32_t max)
|
||||||
int32_t blockIndex = *bitr;
|
int32_t blockIndex = *bitr;
|
||||||
if(!dispatcher->isOutstandingRequest(piece->getIndex(),
|
if(!dispatcher->isOutstandingRequest(piece->getIndex(),
|
||||||
blockIndex)) {
|
blockIndex)) {
|
||||||
requests.push_back(BT_MESSAGE_FACTORY(btContext, peer)->
|
requests.push_back(messageFactory->createRequestMessage(piece, blockIndex));
|
||||||
createRequestMessage(piece, blockIndex));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,8 @@ private:
|
||||||
BtContextHandle btContext;
|
BtContextHandle btContext;
|
||||||
PieceStorageHandle pieceStorage;
|
PieceStorageHandle pieceStorage;
|
||||||
PeerHandle peer;
|
PeerHandle peer;
|
||||||
BtMessageDispatcherHandle dispatcher;
|
BtMessageDispatcherWeakHandle dispatcher;
|
||||||
|
BtMessageFactoryWeakHandle messageFactory;
|
||||||
Pieces pieces;
|
Pieces pieces;
|
||||||
public:
|
public:
|
||||||
DefaultBtRequestFactory():
|
DefaultBtRequestFactory():
|
||||||
|
@ -117,9 +118,14 @@ public:
|
||||||
this->peer = peer;
|
this->peer = peer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setBtMessageDispatcher(const BtMessageDispatcherHandle& dispatcher) {
|
void setBtMessageDispatcher(const BtMessageDispatcherWeakHandle& dispatcher)
|
||||||
|
{
|
||||||
this->dispatcher = dispatcher;
|
this->dispatcher = dispatcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setBtMessageFactory(const BtMessageFactoryWeakHandle& factory) {
|
||||||
|
this->messageFactory = factory;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef SharedHandle<DefaultBtRequestFactory> DefaultBtRequestFactoryHandle;
|
typedef SharedHandle<DefaultBtRequestFactory> DefaultBtRequestFactoryHandle;
|
||||||
|
|
|
@ -76,5 +76,5 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef SharedHandle<PeerConnection> PeerConnectionHandle;
|
typedef SharedHandle<PeerConnection> PeerConnectionHandle;
|
||||||
|
typedef WeakHandle<PeerConnection> PeerConnectionWeakHandle;
|
||||||
#endif // _D_PEER_CONNECTION_H_
|
#endif // _D_PEER_CONNECTION_H_
|
||||||
|
|
|
@ -56,7 +56,8 @@ PeerInteractionCommand::PeerInteractionCommand(int cuid,
|
||||||
int sequence)
|
int sequence)
|
||||||
:PeerAbstractCommand(cuid, p, e, btContext, s),
|
:PeerAbstractCommand(cuid, p, e, btContext, s),
|
||||||
sequence(sequence),
|
sequence(sequence),
|
||||||
btInteractive(0)
|
btInteractive(0),
|
||||||
|
maxDownloadSpeedLimit(0)
|
||||||
{
|
{
|
||||||
// TODO move following bunch of processing to separate method, like init()
|
// TODO move following bunch of processing to separate method, like init()
|
||||||
if(sequence == INITIATOR_SEND_HANDSHAKE) {
|
if(sequence == INITIATOR_SEND_HANDSHAKE) {
|
||||||
|
@ -64,6 +65,11 @@ PeerInteractionCommand::PeerInteractionCommand(int cuid,
|
||||||
setWriteCheckSocket(socket);
|
setWriteCheckSocket(socket);
|
||||||
setTimeout(e->option->getAsInt(PREF_PEER_CONNECTION_TIMEOUT));
|
setTimeout(e->option->getAsInt(PREF_PEER_CONNECTION_TIMEOUT));
|
||||||
}
|
}
|
||||||
|
DefaultBtMessageFactoryHandle factory = new DefaultBtMessageFactory();
|
||||||
|
factory->setCuid(cuid);
|
||||||
|
factory->setBtContext(btContext);
|
||||||
|
factory->setPeer(peer);
|
||||||
|
|
||||||
PeerConnectionHandle peerConnection =
|
PeerConnectionHandle peerConnection =
|
||||||
new PeerConnection(cuid, socket, e->option);
|
new PeerConnection(cuid, socket, e->option);
|
||||||
|
|
||||||
|
@ -71,7 +77,9 @@ PeerInteractionCommand::PeerInteractionCommand(int cuid,
|
||||||
dispatcher->setCuid(cuid);
|
dispatcher->setCuid(cuid);
|
||||||
dispatcher->setPeer(peer);
|
dispatcher->setPeer(peer);
|
||||||
dispatcher->setBtContext(btContext);
|
dispatcher->setBtContext(btContext);
|
||||||
dispatcher->setOption(e->option);
|
dispatcher->setMaxUploadSpeedLimit(e->option->getAsInt(PREF_MAX_UPLOAD_LIMIT));
|
||||||
|
dispatcher->setRequestTimeout(e->option->getAsInt(PREF_BT_REQUEST_TIMEOUT));
|
||||||
|
dispatcher->setBtMessageFactory(factory);
|
||||||
|
|
||||||
DefaultBtMessageReceiverHandle receiver = new DefaultBtMessageReceiver();
|
DefaultBtMessageReceiverHandle receiver = new DefaultBtMessageReceiver();
|
||||||
receiver->setCuid(cuid);
|
receiver->setCuid(cuid);
|
||||||
|
@ -79,12 +87,14 @@ PeerInteractionCommand::PeerInteractionCommand(int cuid,
|
||||||
receiver->setBtContext(btContext);
|
receiver->setBtContext(btContext);
|
||||||
receiver->setPeerConnection(peerConnection);
|
receiver->setPeerConnection(peerConnection);
|
||||||
receiver->setDispatcher(dispatcher);
|
receiver->setDispatcher(dispatcher);
|
||||||
|
receiver->setBtMessageFactory(factory);
|
||||||
|
|
||||||
DefaultBtRequestFactoryHandle reqFactory = new DefaultBtRequestFactory();
|
DefaultBtRequestFactoryHandle reqFactory = new DefaultBtRequestFactory();
|
||||||
reqFactory->setCuid(cuid);
|
reqFactory->setCuid(cuid);
|
||||||
reqFactory->setPeer(peer);
|
reqFactory->setPeer(peer);
|
||||||
reqFactory->setBtContext(btContext);
|
reqFactory->setBtContext(btContext);
|
||||||
reqFactory->setBtMessageDispatcher(dispatcher);
|
reqFactory->setBtMessageDispatcher(dispatcher);
|
||||||
|
reqFactory->setBtMessageFactory(factory);
|
||||||
|
|
||||||
DefaultBtInteractiveHandle btInteractive = new DefaultBtInteractive();
|
DefaultBtInteractiveHandle btInteractive = new DefaultBtInteractive();
|
||||||
btInteractive->setCuid(cuid);
|
btInteractive->setCuid(cuid);
|
||||||
|
@ -94,24 +104,29 @@ PeerInteractionCommand::PeerInteractionCommand(int cuid,
|
||||||
btInteractive->setDispatcher(dispatcher);
|
btInteractive->setDispatcher(dispatcher);
|
||||||
btInteractive->setBtRequestFactory(reqFactory);
|
btInteractive->setBtRequestFactory(reqFactory);
|
||||||
btInteractive->setPeerConnection(peerConnection);
|
btInteractive->setPeerConnection(peerConnection);
|
||||||
btInteractive->setOption(e->option);
|
btInteractive->setKeepAliveInterval(e->option->getAsInt(PREF_BT_KEEP_ALIVE_INTERVAL));
|
||||||
|
btInteractive->setMaxDownloadSpeedLimit(e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT));
|
||||||
|
btInteractive->setBtMessageFactory(factory);
|
||||||
this->btInteractive = btInteractive;
|
this->btInteractive = btInteractive;
|
||||||
|
|
||||||
DefaultBtMessageFactoryHandle factory = new DefaultBtMessageFactory();
|
// reverse depends
|
||||||
factory->setCuid(cuid);
|
factory->setBtMessageDispatcher(dispatcher);
|
||||||
factory->setBtContext(btContext);
|
factory->setBtRequestFactory(reqFactory);
|
||||||
factory->setPeer(peer);
|
factory->setPeerConnection(peerConnection);
|
||||||
|
|
||||||
PeerObjectHandle peerObject = new PeerObject();
|
PeerObjectHandle peerObject = new PeerObject();
|
||||||
peerObject->btMessageDispatcher = dispatcher;
|
peerObject->btMessageDispatcher = dispatcher;
|
||||||
|
peerObject->btMessageReceiver = receiver;
|
||||||
peerObject->btMessageFactory = factory;
|
peerObject->btMessageFactory = factory;
|
||||||
peerObject->btRequestFactory = reqFactory;
|
peerObject->btRequestFactory = reqFactory;
|
||||||
peerObject->peerConnection = peerConnection;
|
peerObject->peerConnection = peerConnection;
|
||||||
|
|
||||||
PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getId(), peerObject);
|
PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getId(), peerObject);
|
||||||
|
|
||||||
setUploadLimit(e->option->getAsInt(PREF_MAX_UPLOAD_LIMIT));
|
setUploadLimit(e->option->getAsInt(PREF_MAX_UPLOAD_LIMIT));
|
||||||
peer->activate();
|
peer->activate();
|
||||||
|
|
||||||
|
maxDownloadSpeedLimit = e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
PeerInteractionCommand::~PeerInteractionCommand() {
|
PeerInteractionCommand::~PeerInteractionCommand() {
|
||||||
|
@ -171,10 +186,9 @@ bool PeerInteractionCommand::executeInternal() {
|
||||||
if(btInteractive->countPendingMessage() > 0) {
|
if(btInteractive->countPendingMessage() > 0) {
|
||||||
setNoCheck(true);
|
setNoCheck(true);
|
||||||
}
|
}
|
||||||
int maxSpeedLimit = e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT);
|
if(maxDownloadSpeedLimit > 0) {
|
||||||
if(maxSpeedLimit > 0) {
|
|
||||||
TransferStat stat = peerStorage->calculateStat();
|
TransferStat stat = peerStorage->calculateStat();
|
||||||
if(maxSpeedLimit < stat.downloadSpeed) {
|
if(maxDownloadSpeedLimit < stat.downloadSpeed) {
|
||||||
disableReadCheckSocket();
|
disableReadCheckSocket();
|
||||||
setNoCheck(true);
|
setNoCheck(true);
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,7 @@ class PeerInteractionCommand : public PeerAbstractCommand {
|
||||||
private:
|
private:
|
||||||
int sequence;
|
int sequence;
|
||||||
BtInteractiveHandle btInteractive;
|
BtInteractiveHandle btInteractive;
|
||||||
|
uint32_t maxDownloadSpeedLimit;
|
||||||
protected:
|
protected:
|
||||||
virtual bool executeInternal();
|
virtual bool executeInternal();
|
||||||
virtual bool prepareForRetry(int wait);
|
virtual bool prepareForRetry(int wait);
|
||||||
|
|
|
@ -40,17 +40,20 @@
|
||||||
#include "BtRequestFactory.h"
|
#include "BtRequestFactory.h"
|
||||||
#include "BtMessageDispatcher.h"
|
#include "BtMessageDispatcher.h"
|
||||||
#include "PeerConnection.h"
|
#include "PeerConnection.h"
|
||||||
|
#include "BtMessageReceiver.h"
|
||||||
|
|
||||||
class PeerObject {
|
class PeerObject {
|
||||||
public:
|
public:
|
||||||
PeerObject():btMessageFactory(0),
|
PeerObject():btMessageFactory(0),
|
||||||
btRequestFactory(0),
|
btRequestFactory(0),
|
||||||
btMessageDispatcher(0),
|
btMessageDispatcher(0),
|
||||||
|
btMessageReceiver(0),
|
||||||
peerConnection(0) {}
|
peerConnection(0) {}
|
||||||
|
|
||||||
BtMessageFactoryHandle btMessageFactory;
|
BtMessageFactoryHandle btMessageFactory;
|
||||||
BtRequestFactoryHandle btRequestFactory;
|
BtRequestFactoryHandle btRequestFactory;
|
||||||
BtMessageDispatcherHandle btMessageDispatcher;
|
BtMessageDispatcherHandle btMessageDispatcher;
|
||||||
|
BtMessageReceiverHandle btMessageReceiver;
|
||||||
PeerConnectionHandle peerConnection;
|
PeerConnectionHandle peerConnection;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -40,45 +40,45 @@
|
||||||
|
|
||||||
class TransferStat {
|
class TransferStat {
|
||||||
public:
|
public:
|
||||||
int downloadSpeed;
|
uint32_t downloadSpeed;
|
||||||
int uploadSpeed;
|
uint32_t uploadSpeed;
|
||||||
long long int sessionDownloadLength;
|
uint64_t sessionDownloadLength;
|
||||||
long long int sessionUploadLength;
|
uint64_t sessionUploadLength;
|
||||||
public:
|
public:
|
||||||
TransferStat():downloadSpeed(0), uploadSpeed(0),
|
TransferStat():downloadSpeed(0), uploadSpeed(0),
|
||||||
sessionDownloadLength(0), sessionUploadLength(0) {}
|
sessionDownloadLength(0), sessionUploadLength(0) {}
|
||||||
|
|
||||||
int getDownloadSpeed() const {
|
uint32_t getDownloadSpeed() const {
|
||||||
return downloadSpeed;
|
return downloadSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setDownloadSpeed(int s) { downloadSpeed = s; }
|
void setDownloadSpeed(uint32_t s) { downloadSpeed = s; }
|
||||||
|
|
||||||
int getUploadSpeed() const {
|
uint32_t getUploadSpeed() const {
|
||||||
return uploadSpeed;
|
return uploadSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setUploadSpeed(int s) { uploadSpeed = s; }
|
void setUploadSpeed(uint32_t s) { uploadSpeed = s; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the number of bytes downloaded since the program started.
|
* Returns the number of bytes downloaded since the program started.
|
||||||
* This is not the total number of bytes downloaded.
|
* This is not the total number of bytes downloaded.
|
||||||
*/
|
*/
|
||||||
long long int getSessionDownloadLength() const {
|
uint64_t getSessionDownloadLength() const {
|
||||||
return sessionDownloadLength;
|
return sessionDownloadLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setSessionDownloadLength(long long int s) { sessionDownloadLength = s; }
|
void setSessionDownloadLength(uint64_t s) { sessionDownloadLength = s; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the number of bytes uploaded since the program started.
|
* Returns the number of bytes uploaded since the program started.
|
||||||
* This is not the total number of bytes uploaded.
|
* This is not the total number of bytes uploaded.
|
||||||
*/
|
*/
|
||||||
long long int getSessionUploadLength() const {
|
uint64_t getSessionUploadLength() const {
|
||||||
return sessionUploadLength;
|
return sessionUploadLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setSessionUploadLength(long long int s) { sessionUploadLength = s; }
|
void setSessionUploadLength(uint64_t s) { sessionUploadLength = s; }
|
||||||
};
|
};
|
||||||
|
|
||||||
class PeerStorage {
|
class PeerStorage {
|
||||||
|
|
|
@ -37,69 +37,104 @@
|
||||||
|
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
|
||||||
template<class T>
|
class RefCount {
|
||||||
class SharedHandle {
|
public:
|
||||||
|
RefCount():totalRefCount(0), strongRefCount(0) {}
|
||||||
|
|
||||||
template<class T1>
|
RefCount(uint32_t totalRefCount, uint32_t strongRefCount)
|
||||||
|
:totalRefCount(totalRefCount), strongRefCount(strongRefCount) {}
|
||||||
|
|
||||||
|
uint32_t totalRefCount;
|
||||||
|
uint32_t strongRefCount;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class SharedHandle;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class WeakHandle;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class SharedHandle {
|
||||||
|
private:
|
||||||
|
template<typename T1>
|
||||||
friend std::ostream& operator<<(std::ostream& o, const SharedHandle<T1>& sp);
|
friend std::ostream& operator<<(std::ostream& o, const SharedHandle<T1>& sp);
|
||||||
|
|
||||||
template<class T1, class T2>
|
template<typename T1, typename T2>
|
||||||
friend bool operator==(const SharedHandle<T1>& t1,
|
friend bool operator==(const SharedHandle<T1>& t1,
|
||||||
const SharedHandle<T2>& t2);
|
const SharedHandle<T2>& t2);
|
||||||
|
|
||||||
template<class T1, class T2>
|
template<typename T1, typename T2>
|
||||||
friend bool operator!=(const SharedHandle<T1>& t1,
|
friend bool operator!=(const SharedHandle<T1>& t1,
|
||||||
const SharedHandle<T2>& t2);
|
const SharedHandle<T2>& t2);
|
||||||
|
|
||||||
template<class T1, class T2>
|
template<typename T1, typename T2>
|
||||||
friend bool operator<(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2);
|
friend bool operator<(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2);
|
||||||
|
|
||||||
private:
|
|
||||||
T* obj;
|
T* obj;
|
||||||
int* ucount;
|
|
||||||
public:
|
RefCount* ucount;
|
||||||
SharedHandle():obj(new T()), ucount(new int(1)) {}
|
|
||||||
SharedHandle(T* obj):obj(obj), ucount(new int(1)) {}
|
void releaseReference() {
|
||||||
SharedHandle(const SharedHandle& t):obj(t.get()), ucount(t.getRefCount()) {
|
if(--ucount->strongRefCount == 0) {
|
||||||
++*ucount;
|
delete obj;
|
||||||
|
obj = 0;
|
||||||
|
}
|
||||||
|
if(--ucount->totalRefCount == 0) {
|
||||||
|
delete ucount;
|
||||||
|
ucount = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
template<class S>
|
|
||||||
|
public:
|
||||||
|
SharedHandle():obj(new T()), ucount(new RefCount(1, 1)) {}
|
||||||
|
|
||||||
|
SharedHandle(T* obj):obj(obj), ucount(new RefCount(1, 1)) {}
|
||||||
|
|
||||||
|
SharedHandle(const SharedHandle& t):obj(t.get()), ucount(t.getRefCount()) {
|
||||||
|
++ucount->totalRefCount;
|
||||||
|
++ucount->strongRefCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename S>
|
||||||
SharedHandle(const SharedHandle<S>& t) {
|
SharedHandle(const SharedHandle<S>& t) {
|
||||||
obj = dynamic_cast<T*>(t.get());
|
obj = dynamic_cast<T*>(t.get());
|
||||||
if(obj) {
|
if(obj) {
|
||||||
ucount = t.getRefCount();
|
ucount = t.getRefCount();
|
||||||
++*ucount;
|
++ucount->totalRefCount;
|
||||||
|
++ucount->strongRefCount;
|
||||||
} else {
|
} else {
|
||||||
ucount = new int(1);
|
ucount = new RefCount(1, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
~SharedHandle() {
|
~SharedHandle() {
|
||||||
if(--*ucount == 0) {
|
releaseReference();
|
||||||
delete obj;
|
|
||||||
delete ucount;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedHandle& operator=(const SharedHandle& t) {
|
SharedHandle& operator=(const SharedHandle& t) {
|
||||||
++*t.getRefCount();
|
++t.getRefCount()->totalRefCount;
|
||||||
if(--*ucount == 0) {
|
++t.getRefCount()->strongRefCount;
|
||||||
delete obj;
|
releaseReference();
|
||||||
delete ucount;
|
|
||||||
}
|
|
||||||
obj = t.get();
|
obj = t.get();
|
||||||
ucount = t.getRefCount();
|
ucount = t.getRefCount();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
template<class S>
|
|
||||||
SharedHandle& operator=(const SharedHandle<S>& t) {
|
template<typename S>
|
||||||
++*t.getRefCount();
|
SharedHandle& operator=(const SharedHandle<S>& t) {
|
||||||
if(--*ucount == 0) {
|
T* to = dynamic_cast<T*>(t.get());
|
||||||
delete obj;
|
if(to) {
|
||||||
delete ucount;
|
++t.getRefCount()->totalRefCount;
|
||||||
|
++t.getRefCount()->strongRefCount;
|
||||||
|
releaseReference();
|
||||||
|
obj = to;
|
||||||
|
ucount = t.getRefCount();
|
||||||
|
} else {
|
||||||
|
releaseReference();
|
||||||
|
obj = 0;
|
||||||
|
ucount = new RefCount(1, 1);
|
||||||
}
|
}
|
||||||
obj = t.get();
|
|
||||||
ucount = t.getRefCount();
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,7 +146,7 @@ public:
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
int* getRefCount() const {
|
RefCount* getRefCount() const {
|
||||||
return ucount;
|
return ucount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,25 +155,169 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T>
|
template<typename T>
|
||||||
std::ostream& operator<<(std::ostream& o, const SharedHandle<T>& sp) {
|
std::ostream& operator<<(std::ostream& o, const SharedHandle<T>& sp) {
|
||||||
o << *sp.obj;
|
o << *sp.obj;
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T1, class T2>
|
template<typename T1, typename T2>
|
||||||
bool operator==(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
|
bool operator==(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
|
||||||
return *t1.obj == *t2.obj;
|
return *t1.obj == *t2.obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T1, class T2>
|
template<typename T1, typename T2>
|
||||||
bool operator!=(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
|
bool operator!=(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
|
||||||
return *t1.obj != *t2.obj;
|
return *t1.obj != *t2.obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T1, class T2>
|
template<typename T1, typename T2>
|
||||||
bool operator<(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
|
bool operator<(const SharedHandle<T1>& t1, const SharedHandle<T2>& t2) {
|
||||||
return *t1.obj < *t2.obj;
|
return *t1.obj < *t2.obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class WeakHandle {
|
||||||
|
private:
|
||||||
|
template<typename T1>
|
||||||
|
friend std::ostream& operator<<(std::ostream& o, const WeakHandle<T1>& sp);
|
||||||
|
|
||||||
|
template<typename T1, typename T2>
|
||||||
|
friend bool operator==(const WeakHandle<T1>& t1,
|
||||||
|
const WeakHandle<T2>& t2);
|
||||||
|
|
||||||
|
template<typename T1, typename T2>
|
||||||
|
friend bool operator!=(const WeakHandle<T1>& t1,
|
||||||
|
const WeakHandle<T2>& t2);
|
||||||
|
|
||||||
|
template<typename T1, typename T2>
|
||||||
|
friend bool operator<(const WeakHandle<T1>& t1, const WeakHandle<T2>& t2);
|
||||||
|
|
||||||
|
T* obj;
|
||||||
|
|
||||||
|
RefCount* ucount;
|
||||||
|
|
||||||
|
void releaseReference() {
|
||||||
|
if(--ucount->totalRefCount == 0) {
|
||||||
|
delete ucount;
|
||||||
|
ucount = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
WeakHandle():obj(0), ucount(new RefCount(1, 0)) {}
|
||||||
|
|
||||||
|
WeakHandle(T* obj):obj(obj), ucount(new RefCount(1, 1)) {}
|
||||||
|
|
||||||
|
WeakHandle(const WeakHandle& t):obj(t.get()), ucount(t.getRefCount()) {
|
||||||
|
++ucount->totalRefCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename S>
|
||||||
|
WeakHandle(const SharedHandle<S>& t):obj(t.get()), ucount(t.getRefCount()) {
|
||||||
|
obj = dynamic_cast<T*>(t.get());
|
||||||
|
if(obj) {
|
||||||
|
ucount = t.getRefCount();
|
||||||
|
++ucount->totalRefCount;
|
||||||
|
} else {
|
||||||
|
ucount = new RefCount(1, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename S>
|
||||||
|
WeakHandle(const WeakHandle<S>& t) {
|
||||||
|
obj = dynamic_cast<T*>(t.get());
|
||||||
|
if(obj) {
|
||||||
|
ucount = t.getRefCount();
|
||||||
|
++ucount->totalRefCount;
|
||||||
|
} else {
|
||||||
|
ucount = new RefCount(1, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~WeakHandle() {
|
||||||
|
releaseReference();
|
||||||
|
}
|
||||||
|
|
||||||
|
WeakHandle& operator=(const WeakHandle& t) {
|
||||||
|
++t.getRefCount()->totalRefCount;
|
||||||
|
releaseReference();
|
||||||
|
obj = t.get();
|
||||||
|
ucount = t.getRefCount();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename S>
|
||||||
|
WeakHandle& operator=(const SharedHandle<S>& t) {
|
||||||
|
T* to = dynamic_cast<T*>(t.get());
|
||||||
|
if(to) {
|
||||||
|
++t.getRefCount()->totalRefCount;
|
||||||
|
releaseReference();
|
||||||
|
obj = to;
|
||||||
|
ucount = t.getRefCount();
|
||||||
|
} else {
|
||||||
|
releaseReference();
|
||||||
|
obj = 0;
|
||||||
|
ucount = new RefCount(1, 0);
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename S>
|
||||||
|
WeakHandle& operator=(const WeakHandle<S>& t) {
|
||||||
|
T* to = dynamic_cast<T*>(t.get());
|
||||||
|
if(to) {
|
||||||
|
++t.getRefCount()->totalRefCount;
|
||||||
|
releaseReference();
|
||||||
|
obj = to;
|
||||||
|
ucount = t.getRefCount();
|
||||||
|
} else {
|
||||||
|
releaseReference();
|
||||||
|
obj = 0;
|
||||||
|
ucount = new RefCount(1, 0);
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
T* operator->() { return obj; }
|
||||||
|
|
||||||
|
T* operator->() const { return obj; }
|
||||||
|
|
||||||
|
T* get() const {
|
||||||
|
if(isNull()) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
RefCount* getRefCount() const {
|
||||||
|
return ucount;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isNull() const {
|
||||||
|
return ucount->strongRefCount == 0 || obj == 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::ostream& operator<<(std::ostream& o, const WeakHandle<T>& sp) {
|
||||||
|
o << *sp.obj;
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T1, typename T2>
|
||||||
|
bool operator==(const WeakHandle<T1>& t1, const WeakHandle<T2>& t2) {
|
||||||
|
return *t1.obj == *t2.obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T1, typename T2>
|
||||||
|
bool operator!=(const WeakHandle<T1>& t1, const WeakHandle<T2>& t2) {
|
||||||
|
return *t1.obj != *t2.obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T1, typename T2>
|
||||||
|
bool operator<(const WeakHandle<T1>& t1, const WeakHandle<T2>& t2) {
|
||||||
|
return *t1.obj < *t2.obj;
|
||||||
|
}
|
||||||
|
|
||||||
#endif // _D_SHARED_HANDLE_H_
|
#endif // _D_SHARED_HANDLE_H_
|
||||||
|
|
|
@ -53,7 +53,7 @@ void SimpleBtMessage::send() {
|
||||||
leftDataLength = getMessageLength();
|
leftDataLength = getMessageLength();
|
||||||
}
|
}
|
||||||
sendingInProgress = false;
|
sendingInProgress = false;
|
||||||
int writtenLength = PEER_CONNECTION(btContext, peer)->sendMessage(msg+msgLength-leftDataLength, leftDataLength);
|
int writtenLength = peerConnection->sendMessage(msg+msgLength-leftDataLength, leftDataLength);
|
||||||
if(writtenLength == leftDataLength) {
|
if(writtenLength == leftDataLength) {
|
||||||
onSendComplete();
|
onSendComplete();
|
||||||
} else {
|
} else {
|
||||||
|
|
17
src/Util.cc
17
src/Util.cc
|
@ -574,20 +574,11 @@ static int nbits[] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
uint32_t Util::countBit(uint32_t n) {
|
uint32_t Util::countBit(uint32_t n) {
|
||||||
/*
|
|
||||||
return
|
return
|
||||||
nbits[n & 0xffu]+
|
nbits[n&0xffu]+
|
||||||
nbits[(n >> 8) & 0xffu]+
|
nbits[(n >> 8)&0xffu]+
|
||||||
nbits[(n >> 16) & 0xffu]+
|
nbits[(n >> 16)&0xffu]+
|
||||||
nbits[(n >> 24) & 0xffu];
|
nbits[(n >> 24)&0xffu];
|
||||||
*/
|
|
||||||
uint32_t count = 0;
|
|
||||||
int size = sizeof(uint32_t);
|
|
||||||
for(int i = 0; i < size; i++) {
|
|
||||||
count += nbits[(n >> i*8) & 0xffu];
|
|
||||||
}
|
|
||||||
|
|
||||||
return count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string Util::randomAlpha(int length) {
|
string Util::randomAlpha(int length) {
|
||||||
|
|
|
@ -109,7 +109,7 @@ void BtCancelMessageTest::testDoReceivedAction() {
|
||||||
msg.setBtContext(btContext);
|
msg.setBtContext(btContext);
|
||||||
msg.setPeer(peer);
|
msg.setPeer(peer);
|
||||||
MockBtMessageDispatcher2Handle dispatcher = new MockBtMessageDispatcher2();
|
MockBtMessageDispatcher2Handle dispatcher = new MockBtMessageDispatcher2();
|
||||||
PEER_OBJECT(btContext, peer)->btMessageDispatcher = dispatcher;
|
msg.setBtMessageDispatcher(dispatcher);
|
||||||
|
|
||||||
msg.doReceivedAction();
|
msg.doReceivedAction();
|
||||||
CPPUNIT_ASSERT_EQUAL(msg.getIndex(), dispatcher->index);
|
CPPUNIT_ASSERT_EQUAL(msg.getIndex(), dispatcher->index);
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include "BtChokeMessage.h"
|
#include "BtChokeMessage.h"
|
||||||
#include "PeerMessageUtil.h"
|
#include "PeerMessageUtil.h"
|
||||||
#include "MockBtMessageDispatcher.h"
|
#include "MockBtMessageDispatcher.h"
|
||||||
|
#include "MockBtRequestFactory.h"
|
||||||
#include "MockBtContext.h"
|
#include "MockBtContext.h"
|
||||||
#include <cppunit/extensions/HelperMacros.h>
|
#include <cppunit/extensions/HelperMacros.h>
|
||||||
|
|
||||||
|
@ -56,6 +57,19 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef SharedHandle<MockBtMessageDispatcher2> MockBtMessageDispatcher2Handle;
|
typedef SharedHandle<MockBtMessageDispatcher2> MockBtMessageDispatcher2Handle;
|
||||||
|
|
||||||
|
class MockBtRequestFactory2 : public MockBtRequestFactory {
|
||||||
|
public:
|
||||||
|
bool doChokedActionCalled;
|
||||||
|
public:
|
||||||
|
MockBtRequestFactory2():doChokedActionCalled(false) {}
|
||||||
|
|
||||||
|
virtual void doChokedAction() {
|
||||||
|
doChokedActionCalled = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef SharedHandle<MockBtRequestFactory2> MockBtRequestFactory2Handle;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -99,8 +113,9 @@ void BtChokeMessageTest::testDoReceivedAction() {
|
||||||
msg.setBtContext(btContext);
|
msg.setBtContext(btContext);
|
||||||
|
|
||||||
MockBtMessageDispatcher2Handle dispatcher = new MockBtMessageDispatcher2();
|
MockBtMessageDispatcher2Handle dispatcher = new MockBtMessageDispatcher2();
|
||||||
|
msg.setBtMessageDispatcher(dispatcher);
|
||||||
PEER_OBJECT(btContext, peer)->btMessageDispatcher = dispatcher;
|
MockBtRequestFactory2Handle requestFactory = new MockBtRequestFactory2();
|
||||||
|
msg.setBtRequestFactory(requestFactory);
|
||||||
|
|
||||||
msg.doReceivedAction();
|
msg.doReceivedAction();
|
||||||
|
|
||||||
|
@ -115,8 +130,7 @@ void BtChokeMessageTest::testOnSendComplete() {
|
||||||
msg.setBtContext(btContext);
|
msg.setBtContext(btContext);
|
||||||
|
|
||||||
MockBtMessageDispatcher2Handle dispatcher = new MockBtMessageDispatcher2();
|
MockBtMessageDispatcher2Handle dispatcher = new MockBtMessageDispatcher2();
|
||||||
|
msg.setBtMessageDispatcher(dispatcher);
|
||||||
PEER_OBJECT(btContext, peer)->btMessageDispatcher = dispatcher;
|
|
||||||
|
|
||||||
msg.onSendComplete();
|
msg.onSendComplete();
|
||||||
|
|
||||||
|
|
|
@ -94,6 +94,8 @@ public:
|
||||||
msg->setBlockLength(16*1024);
|
msg->setBlockLength(16*1024);
|
||||||
msg->setBtContext(btContext);
|
msg->setBtContext(btContext);
|
||||||
msg->setPeer(peer);
|
msg->setPeer(peer);
|
||||||
|
msg->setBtMessageDispatcher(btMessageDispatcher);
|
||||||
|
msg->setBtMessageFactory(BT_MESSAGE_FACTORY(btContext, peer));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -82,7 +82,7 @@ public:
|
||||||
msg->setIndex(1);
|
msg->setIndex(1);
|
||||||
msg->setBegin(16);
|
msg->setBegin(16);
|
||||||
msg->setLength(32);
|
msg->setLength(32);
|
||||||
|
msg->setBtMessageDispatcher(dispatcher);
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -116,6 +116,8 @@ public:
|
||||||
msg->setBegin(16);
|
msg->setBegin(16);
|
||||||
msg->setLength(32);
|
msg->setLength(32);
|
||||||
msg->setBlockIndex(2);
|
msg->setBlockIndex(2);
|
||||||
|
msg->setBtMessageDispatcher(dispatcher);
|
||||||
|
msg->setBtMessageFactory(BT_MESSAGE_FACTORY(btContext, peer));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,6 @@ class DefaultBtMessageDispatcherTest:public CppUnit::TestFixture {
|
||||||
CPPUNIT_TEST_SUITE_END();
|
CPPUNIT_TEST_SUITE_END();
|
||||||
private:
|
private:
|
||||||
BtContextHandle btContext;
|
BtContextHandle btContext;
|
||||||
Option* option;
|
|
||||||
PeerHandle peer;
|
PeerHandle peer;
|
||||||
DefaultBtMessageDispatcherHandle btMessageDispatcher;
|
DefaultBtMessageDispatcherHandle btMessageDispatcher;
|
||||||
MockPeerStorageHandle peerStorage;
|
MockPeerStorageHandle peerStorage;
|
||||||
|
@ -41,9 +40,7 @@ private:
|
||||||
public:
|
public:
|
||||||
DefaultBtMessageDispatcherTest():btContext(0), peer(0), btMessageDispatcher(0) {}
|
DefaultBtMessageDispatcherTest():btContext(0), peer(0), btMessageDispatcher(0) {}
|
||||||
|
|
||||||
void tearDown() {
|
void tearDown() {}
|
||||||
delete option;
|
|
||||||
}
|
|
||||||
|
|
||||||
void testAddMessage();
|
void testAddMessage();
|
||||||
void testSendMessages();
|
void testSendMessages();
|
||||||
|
@ -132,7 +129,6 @@ public:
|
||||||
void setUp() {
|
void setUp() {
|
||||||
btContext = new DefaultBtContext();
|
btContext = new DefaultBtContext();
|
||||||
btContext->load("test.torrent");
|
btContext->load("test.torrent");
|
||||||
option = new Option();
|
|
||||||
peer = new Peer("192.168.0.1", 6969,
|
peer = new Peer("192.168.0.1", 6969,
|
||||||
btContext->getPieceLength(),
|
btContext->getPieceLength(),
|
||||||
btContext->getTotalLength());
|
btContext->getTotalLength());
|
||||||
|
@ -155,7 +151,8 @@ public:
|
||||||
btMessageDispatcher->setCuid(1);
|
btMessageDispatcher->setCuid(1);
|
||||||
btMessageDispatcher->setBtContext(btContext);
|
btMessageDispatcher->setBtContext(btContext);
|
||||||
btMessageDispatcher->setPeer(peer);
|
btMessageDispatcher->setPeer(peer);
|
||||||
btMessageDispatcher->setOption(option);
|
btMessageDispatcher->setMaxUploadSpeedLimit(0);
|
||||||
|
btMessageDispatcher->setBtMessageFactory(peerObject->btMessageFactory);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -172,7 +169,6 @@ void DefaultBtMessageDispatcherTest::testAddMessage() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefaultBtMessageDispatcherTest::testSendMessages() {
|
void DefaultBtMessageDispatcherTest::testSendMessages() {
|
||||||
option->put(PREF_MAX_UPLOAD_LIMIT, "0");
|
|
||||||
TransferStat stat;
|
TransferStat stat;
|
||||||
stat.setUploadSpeed(0);
|
stat.setUploadSpeed(0);
|
||||||
peerStorage->setStat(stat);
|
peerStorage->setStat(stat);
|
||||||
|
@ -192,7 +188,6 @@ void DefaultBtMessageDispatcherTest::testSendMessages() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefaultBtMessageDispatcherTest::testSendMessages_underUploadLimit() {
|
void DefaultBtMessageDispatcherTest::testSendMessages_underUploadLimit() {
|
||||||
option->put(PREF_MAX_UPLOAD_LIMIT, "0");
|
|
||||||
TransferStat stat;
|
TransferStat stat;
|
||||||
stat.setUploadSpeed(0);
|
stat.setUploadSpeed(0);
|
||||||
peerStorage->setStat(stat);
|
peerStorage->setStat(stat);
|
||||||
|
@ -212,7 +207,7 @@ void DefaultBtMessageDispatcherTest::testSendMessages_underUploadLimit() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefaultBtMessageDispatcherTest::testSendMessages_overUploadLimit() {
|
void DefaultBtMessageDispatcherTest::testSendMessages_overUploadLimit() {
|
||||||
option->put(PREF_MAX_UPLOAD_LIMIT, "100");
|
btMessageDispatcher->setMaxUploadSpeedLimit(100);
|
||||||
TransferStat stat;
|
TransferStat stat;
|
||||||
stat.setUploadSpeed(150);
|
stat.setUploadSpeed(150);
|
||||||
peerStorage->setStat(stat);
|
peerStorage->setStat(stat);
|
||||||
|
@ -289,13 +284,11 @@ void DefaultBtMessageDispatcherTest::testCheckRequestSlotAndDoNecessaryThing() {
|
||||||
CPPUNIT_ASSERT(BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(),
|
CPPUNIT_ASSERT(BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(),
|
||||||
pieceStorage));
|
pieceStorage));
|
||||||
|
|
||||||
option->put(PREF_BT_REQUEST_TIMEOUT, "60");
|
|
||||||
|
|
||||||
btMessageDispatcher = new DefaultBtMessageDispatcher();
|
btMessageDispatcher = new DefaultBtMessageDispatcher();
|
||||||
btMessageDispatcher->setCuid(1);
|
btMessageDispatcher->setCuid(1);
|
||||||
btMessageDispatcher->setBtContext(btContext);
|
btMessageDispatcher->setBtContext(btContext);
|
||||||
btMessageDispatcher->setPeer(peer);
|
btMessageDispatcher->setPeer(peer);
|
||||||
btMessageDispatcher->setOption(option);
|
btMessageDispatcher->setRequestTimeout(60);
|
||||||
|
|
||||||
btMessageDispatcher->addOutstandingRequest(slot);
|
btMessageDispatcher->addOutstandingRequest(slot);
|
||||||
|
|
||||||
|
@ -317,14 +310,15 @@ void DefaultBtMessageDispatcherTest::testCheckRequestSlotAndDoNecessaryThing_tim
|
||||||
pieceStorage->setPiece(piece);
|
pieceStorage->setPiece(piece);
|
||||||
CPPUNIT_ASSERT(BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(),
|
CPPUNIT_ASSERT(BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(),
|
||||||
pieceStorage));
|
pieceStorage));
|
||||||
option->put(PREF_BT_REQUEST_TIMEOUT, "60");
|
|
||||||
|
|
||||||
btMessageDispatcher = new DefaultBtMessageDispatcher();
|
btMessageDispatcher = new DefaultBtMessageDispatcher();
|
||||||
btMessageDispatcher->setCuid(1);
|
btMessageDispatcher->setCuid(1);
|
||||||
btMessageDispatcher->setBtContext(btContext);
|
btMessageDispatcher->setBtContext(btContext);
|
||||||
btMessageDispatcher->setPeer(peer);
|
btMessageDispatcher->setPeer(peer);
|
||||||
btMessageDispatcher->setOption(option);
|
btMessageDispatcher->setRequestTimeout(60);
|
||||||
|
btMessageDispatcher->setBtMessageFactory(BT_MESSAGE_FACTORY(btContext,
|
||||||
|
peer));
|
||||||
|
|
||||||
btMessageDispatcher->addOutstandingRequest(slot);
|
btMessageDispatcher->addOutstandingRequest(slot);
|
||||||
|
|
||||||
btMessageDispatcher->checkRequestSlotAndDoNecessaryThing();
|
btMessageDispatcher->checkRequestSlotAndDoNecessaryThing();
|
||||||
|
@ -346,13 +340,14 @@ void DefaultBtMessageDispatcherTest::testCheckRequestSlotAndDoNecessaryThing_com
|
||||||
CPPUNIT_ASSERT(BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(),
|
CPPUNIT_ASSERT(BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(),
|
||||||
pieceStorage));
|
pieceStorage));
|
||||||
|
|
||||||
option->put(PREF_BT_REQUEST_TIMEOUT, "60");
|
|
||||||
|
|
||||||
btMessageDispatcher = new DefaultBtMessageDispatcher();
|
btMessageDispatcher = new DefaultBtMessageDispatcher();
|
||||||
btMessageDispatcher->setCuid(1);
|
btMessageDispatcher->setCuid(1);
|
||||||
btMessageDispatcher->setBtContext(btContext);
|
btMessageDispatcher->setBtContext(btContext);
|
||||||
btMessageDispatcher->setPeer(peer);
|
btMessageDispatcher->setPeer(peer);
|
||||||
btMessageDispatcher->setOption(option);
|
btMessageDispatcher->setRequestTimeout(60);
|
||||||
|
btMessageDispatcher->setBtMessageFactory(BT_MESSAGE_FACTORY(btContext,
|
||||||
|
peer));
|
||||||
|
|
||||||
btMessageDispatcher->addOutstandingRequest(slot);
|
btMessageDispatcher->addOutstandingRequest(slot);
|
||||||
|
|
||||||
|
|
|
@ -77,6 +77,7 @@ public:
|
||||||
btRequestFactory->setBtContext(btContext);
|
btRequestFactory->setBtContext(btContext);
|
||||||
btRequestFactory->setPeer(peer);
|
btRequestFactory->setPeer(peer);
|
||||||
btRequestFactory->setBtMessageDispatcher(new MockBtMessageDispatcher());
|
btRequestFactory->setBtMessageDispatcher(new MockBtMessageDispatcher());
|
||||||
|
btRequestFactory->setBtMessageFactory(peerObject->btMessageFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
#ifndef _D_MOCK_BT_REQUEST_FACTORY_H_
|
||||||
|
#define _D_MOCK_BT_REQUEST_FACTORY_H_
|
||||||
|
|
||||||
|
#include "BtRequestFactory.h"
|
||||||
|
|
||||||
|
class MockBtRequestFactory : public BtRequestFactory {
|
||||||
|
public:
|
||||||
|
virtual ~MockBtRequestFactory() {}
|
||||||
|
|
||||||
|
virtual void addTargetPiece(const PieceHandle& piece) {}
|
||||||
|
|
||||||
|
virtual void removeTargetPiece(const PieceHandle& piece) {}
|
||||||
|
|
||||||
|
virtual void removeAllTargetPiece() {}
|
||||||
|
|
||||||
|
virtual int countTargetPiece() { return 0; }
|
||||||
|
|
||||||
|
virtual void removeCompletedPiece() {}
|
||||||
|
|
||||||
|
virtual void doChokedAction() {}
|
||||||
|
|
||||||
|
virtual BtMessages createRequestMessages(uint32_t max) { return BtMessages(); }
|
||||||
|
|
||||||
|
virtual BtMessages createRequestMessagesOnEndGame(uint32_t max) { return BtMessages(); }
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef SharedHandle<MockBtRequestFactory> MockBtRequestFactoryHandle;
|
||||||
|
typedef WeakHandle<MockBtRequestFactory> MockBtRequestFactoryWeakHandle;
|
||||||
|
|
||||||
|
#endif // _D_MOCK_BT_REQUEST_FACTORY_H_
|
Loading…
Reference in New Issue