2006-12-24 06:25:21 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "DefaultBtMessageDispatcher.h"
|
|
|
|
#include "prefs.h"
|
|
|
|
#include "BtAbortOutstandingRequestEvent.h"
|
|
|
|
#include "BtCancelSendingPieceEvent.h"
|
|
|
|
#include "BtChokedEvent.h"
|
|
|
|
#include "BtChokingEvent.h"
|
|
|
|
#include "BtMessageFactory.h"
|
2007-07-20 17:06:21 +00:00
|
|
|
#include "message.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "BtContext.h"
|
|
|
|
#include "PeerStorage.h"
|
|
|
|
#include "PieceStorage.h"
|
|
|
|
#include "BtMessage.h"
|
|
|
|
#include "BtRegistry.h"
|
|
|
|
#include "Peer.h"
|
|
|
|
#include "Piece.h"
|
|
|
|
#include "LogFactory.h"
|
|
|
|
#include "Logger.h"
|
2008-04-13 01:25:36 +00:00
|
|
|
#include "a2functional.h"
|
2008-02-08 18:39:26 +00:00
|
|
|
#include <algorithm>
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
|
|
|
DefaultBtMessageDispatcher::DefaultBtMessageDispatcher():
|
|
|
|
cuid(0),
|
|
|
|
btContext(0),
|
|
|
|
peerStorage(0),
|
|
|
|
pieceStorage(0),
|
|
|
|
peer(0),
|
|
|
|
maxUploadSpeedLimit(0),
|
|
|
|
requestTimeout(0),
|
|
|
|
logger(LogFactory::getInstance()) {}
|
|
|
|
|
|
|
|
DefaultBtMessageDispatcher::~DefaultBtMessageDispatcher()
|
|
|
|
{
|
|
|
|
logger->debug("DefaultBtMessageDispatcher::deleted");
|
|
|
|
}
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
void DefaultBtMessageDispatcher::addMessageToQueue(const BtMessageHandle& btMessage)
|
|
|
|
{
|
|
|
|
btMessage->onQueued();
|
|
|
|
messageQueue.push_back(btMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcher::addMessageToQueue(const BtMessages& btMessages)
|
|
|
|
{
|
|
|
|
for(BtMessages::const_iterator itr = btMessages.begin(); itr != btMessages.end(); itr++) {
|
|
|
|
addMessageToQueue(*itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcher::sendMessages() {
|
|
|
|
BtMessages tempQueue;
|
|
|
|
while(messageQueue.size() > 0) {
|
|
|
|
BtMessageHandle msg = messageQueue.front();
|
|
|
|
messageQueue.pop_front();
|
2007-02-03 04:17:22 +00:00
|
|
|
if(maxUploadSpeedLimit > 0 &&
|
|
|
|
msg->isUploading() && !msg->isSendingInProgress()) {
|
2006-12-24 06:25:21 +00:00
|
|
|
TransferStat stat = peerStorage->calculateStat();
|
2007-02-03 04:17:22 +00:00
|
|
|
if(maxUploadSpeedLimit < stat.getUploadSpeed()) {
|
2006-12-24 06:25:21 +00:00
|
|
|
tempQueue.push_back(msg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg->send();
|
|
|
|
if(msg->isSendingInProgress()) {
|
|
|
|
messageQueue.push_front(msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-02-08 18:39:26 +00:00
|
|
|
std::copy(tempQueue.begin(), tempQueue.end(), std::back_inserter(messageQueue));
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel sending piece message to peer.
|
2008-03-09 12:24:01 +00:00
|
|
|
void DefaultBtMessageDispatcher::doCancelSendingPieceAction(size_t index, uint32_t begin, size_t length)
|
2006-12-24 06:25:21 +00:00
|
|
|
{
|
|
|
|
BtCancelSendingPieceEventHandle event =
|
2006-12-24 15:55:59 +00:00
|
|
|
new BtCancelSendingPieceEvent(index, begin, length);
|
2006-12-24 06:25:21 +00:00
|
|
|
|
|
|
|
BtMessages tempQueue = messageQueue;
|
|
|
|
for(BtMessages::iterator itr = tempQueue.begin(); itr != tempQueue.end(); itr++) {
|
|
|
|
(*itr)->handleEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel sending piece message to peer.
|
|
|
|
// TODO Is this method really necessary?
|
|
|
|
void DefaultBtMessageDispatcher::doCancelSendingPieceAction(const PieceHandle& piece)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// localhost cancels outstanding download requests to the peer.
|
|
|
|
void DefaultBtMessageDispatcher::doAbortOutstandingRequestAction(const PieceHandle& piece) {
|
|
|
|
for(RequestSlots::iterator itr = requestSlots.begin();
|
|
|
|
itr != requestSlots.end();) {
|
|
|
|
RequestSlot& slot = *itr;
|
|
|
|
if(slot.getIndex() == piece->getIndex()) {
|
2007-07-20 17:06:21 +00:00
|
|
|
logger->debug(MSG_DELETING_REQUEST_SLOT,
|
2006-12-24 06:25:21 +00:00
|
|
|
cuid,
|
|
|
|
slot.getIndex(),
|
|
|
|
slot.getBlockIndex());
|
|
|
|
piece->cancelBlock(slot.getBlockIndex());
|
|
|
|
itr = requestSlots.erase(itr);
|
|
|
|
} else {
|
|
|
|
itr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BtAbortOutstandingRequestEventHandle event =
|
|
|
|
new BtAbortOutstandingRequestEvent(piece);
|
|
|
|
|
|
|
|
BtMessages tempQueue = messageQueue;
|
|
|
|
for(BtMessages::iterator itr = tempQueue.begin(); itr != tempQueue.end(); ++itr) {
|
|
|
|
(*itr)->handleEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// localhost received choke message from the peer.
|
|
|
|
void DefaultBtMessageDispatcher::doChokedAction()
|
|
|
|
{
|
|
|
|
for(RequestSlots::iterator itr = requestSlots.begin();
|
|
|
|
itr != requestSlots.end();) {
|
|
|
|
RequestSlot& slot = *itr;
|
2007-01-11 16:32:31 +00:00
|
|
|
if(peer->isInPeerAllowedIndexSet(slot.getIndex())) {
|
|
|
|
itr++;
|
|
|
|
} else {
|
2007-07-20 17:06:21 +00:00
|
|
|
logger->debug(MSG_DELETING_REQUEST_SLOT_CHOKED,
|
2007-01-11 16:32:31 +00:00
|
|
|
cuid,
|
|
|
|
slot.getIndex(),
|
|
|
|
slot.getBlockIndex());
|
|
|
|
PieceHandle piece = pieceStorage->getPiece(slot.getIndex());
|
|
|
|
piece->cancelBlock(slot.getBlockIndex());
|
|
|
|
itr = requestSlots.erase(itr);
|
|
|
|
}
|
2006-12-24 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BtChokedEventHandle event = new BtChokedEvent();
|
|
|
|
|
|
|
|
BtMessages tempQueue = messageQueue;
|
|
|
|
for(BtMessages::iterator itr = tempQueue.begin(); itr != tempQueue.end(); ++itr) {
|
|
|
|
(*itr)->handleEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// localhost dispatched choke message to the peer.
|
|
|
|
void DefaultBtMessageDispatcher::doChokingAction()
|
|
|
|
{
|
|
|
|
BtChokingEventHandle event = new BtChokingEvent();
|
|
|
|
|
|
|
|
BtMessages tempQueue = messageQueue;
|
|
|
|
for(BtMessages::iterator itr = tempQueue.begin(); itr != tempQueue.end(); ++itr) {
|
|
|
|
(*itr)->handleEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcher::checkRequestSlotAndDoNecessaryThing()
|
|
|
|
{
|
|
|
|
for(RequestSlots::iterator itr = requestSlots.begin();
|
|
|
|
itr != requestSlots.end();) {
|
|
|
|
RequestSlot& slot = *itr;
|
|
|
|
PieceHandle piece = pieceStorage->getPiece(slot.getIndex());
|
2007-01-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To decrease CPU usage in bittorrent download, calculation
results in
BitfieldMan were cached and realtime fetching PeerObject was
removed
with WeakHandle introduced. Option values are set to the objects
by setter before download begins.
* src/DefaultBtRequestFactory.cc: Use messageFactory member.
* src/DefaultBtRequestFactory.h
(dispatcher): BtMessageDispatcherHandle ->
BtMessageDispatcherWeakHandle.
(messageFactory): New variable.
(setBtMessageDispatcher): BtMessageDispatcherHandle ->
BtMessageDispatcherWeakHandle.
(setBtMessageFactory): New function.
* src/DefaultBtMessageDispatcher.cc:
(sendMessages): Use maxUploadSpeedLimit instead of fetching the
value
from Option.
(checkRequestSlotAndDoNecessaryThing): Use requestTimeout
instead of
feating the value from Option.
Use messageFactory member.
* src/PeerInteractionCommand.cc
(PeerInteractionCommand): Added maxDownloadSpeedLimit.
Add reverse dependencies to factory object.
Set maxUploadSpeedLimit and requestTimeout and messageFactory to
dispatcher.
Set messageFactory to receiver.
Set keepAliveInterval and maxDownloadSpeedLimit and
messageFactory to
btInteractive.
Set receiver to peerObject.
Set maxDownloadSpeedLimit to this.
(executeInternal): Use maxDownloadSpeedLimit member.
* src/BtChokeMessage.cc
(doReceivedAction): Use dispatcher, requestFactory member.
(onSendComplete): Use dispatcher member.
* src/PeerInteractionCommand.h
(maxDownloadSpeedLimit): New variable.
* src/DefaultBtMessageReceiver.h
(peerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(dispatcher):
BtMessageDispatcherHandle -> BtMessageDispatcherWeakHandle
(messageFactory): New variable.
(setPeerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(getPeerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(setDispatcher):
BtMessageDispatcherHandle -> BtMessageDispatcherWeakHandle
(setBtMessageFactory): New function.
* src/DefaultBtInteractive.cc
(initiateHandshake): Use messageFactory member.
(addBitfieldMessageToQueue): Use messageFactory member.
(addAllowedFastMessageToQueue): Use messageFactory member.
(decideChoking): Use messageFactory member.
(checkHave): Use messageFactory member.
(sendKeepAlive): Use keepAliveInterval, messageFactory member.
(receiveMessages): Use maxDownloadSpeedLimit member.
(decideInterest): Use messageFactory member.
* src/BtRequestMessage.cc
(doReceivedAction): Use messageFactory, dispatcher member.
(onQueued): Use dispatcher member.
* src/BtPieceMessage.cc
(doReceivedAction): Use dispatcher member.
(send): Use peerConnection member.
(onWrongPiece): Use requestFactory member.
(handleChokingEvent): Use messageFactory, dispatcher member.
(handleCancelSendingPieceEvent): Use messageFactory, dispatcher
member.
* src/BtMessageDispatcher.h
(BtMessageDispatcherWeakHandle): New type definition.
* src/SimpleBtMessage.cc
(send): Use peerConnection member.
* src/BtRejectMessage.cc
(doReceivedAction): Use dispatcher member.
* src/DefaultBtMessageDispatcher.h
(Option.h): Removed include.
(messageFactory): New variable.
(option): Removed.
(maxUploadSpeedLimit): New variable.
(requestTimeout): New variable.
(DefaultBtMessageDispatcher): Removed option.
Added maxUploadSpeedLimit, requestTimeout.
(setOption): Removed.
(getOption): Removed.
(setMaxUploadSpeedLimit): New function.
(setRequestTimeout): New function.
(setBtMessageFactory): New function.
* src/DefaultBtInteractive.h
(btMessageReceiver):
BtMessageReceiverHandle -> BtMessageReceiverWeakHandle
(dispatcher):
BtMessageDispatcherHandle -> BtMessageReceiverWeakHandle
(btRequestFactory):
BtRequestFactoryHandle -> BtRequestFactoryWeakHandle
(peerConnection):
PeerConnectionHandle -> PeerConnectionWeakHandle
(messageFactory): New variable.
(option): Removed.
(keepAliveInterval): New variable.
(maxDownloadSpeedLimit): New variable.
(DefaultBtInteractive): Added keepAliveInterval,
maxDownloadSpeedLimit.
(setBtMessageReceiver):
BtMessageReceiverHandle -> BtMessageReceiverWeakHandle
(setDispatcher):
BtMessageDispatcherHandle -> BtMessageReceiverWeakHandle
(setBtRequestFactory):
BtRequestFactoryHandle -> BtRequestFactoryWeakHandle
(setPeerConnection):
PeerConnectionHandle -> PeerConnectionWeakHandle
(setOption): Removed.
(setKeepAliveInterval): New function.
(setMaxDownloadSpeedLimit): New function.
(setBtMessageFactory): New function.
* src/BitfieldMan.h
(cachedNumMissingBlock): New variable.
(cachedNumFilteredBlock): New variable.
(cachedCompletedLength): New variable.
(cachedFilteredComletedLength): New variable.
(cachedFilteredTotalLength): New variable.
(countMissingBlockNow): New function.
(countFilteredBlockNow): New function.
(getFilteredTotalLengthNow): New function.
(getCompletedLengthNow): New function.
(getFilteredCompletedLengthNow): New function.
(updateCache): New function.
* src/AbstractBtMessage.h
(BtMessageDispatcher.h): New include.
(PeerConnection.h): New include.
(BtRequestFactory.h): New include
(BtMessageFactory.h): New include.
(dispatcher): New variable.
(messageFactory): New variable.
(peerConnection: New variable.
(setBtMessageDispatcher): New function.
(setPeerConnection): New function.
(setBtMessageFactory): New function.
(setBtRequestFactory): New function.
* src/DefaultBtMessageFactory.cc
(setCommonProperty): Set dispatcher, requestFactory, this,
peerConnection to msg.
* src/BtRegistry.h
(BT_MESSAGE_RECEIVER): New macro.
* src/PeerConnection.h
(PeerConnectionWeakHandle): New type definition.
* src/BtMessageFactory.h
(BtMessageFactoryWeakHandle): New type definition.
* src/BitfieldMan.cc
(BitfieldMan): Added cachedNumMissingBlock,
cachedNumFilteredBlock,
cachedCompletedLength, cachedFilteredComletedLength,
cachedFilteredTotalLength.
Call updateCache().
(countMissingBlock): Return cachedNumMissingBlock.
(countMissingBlockNow): New function.
(countBlock): Return cachedNumFilteredBlock if filterEnabled is
true.
(countFilteredBlockNow): New function.
(setBit): Call updateCache().
(unsetBit): Call updateCache().
(setBitfield): Call updateCache().
(clearAllBit): Call updateCache().
(setAllBit): Use setBitInternal instead of setBit.
Call updateCache().
(addFilter): Call updateCache().
(enableFilter): Call updateCache().
(disableFilter): Call updateCache().
(clearFilter): Call updateCache().
(getFilteredTotalLength): Return cachedFilteredTotalLength.
(getFilteredTotalLengthNow): New function.
(getCompletedLength): Return cachedCompletedLength.
(getCompletedLengthNow): New function.
(getFilteredCompletedLength): Return
cachedFilteredComletedLength.
(getFilteredCompletedLengthNow): New function.
(updateCache): New function.
* src/BtMessageReceiver.h
(BtMessageReceiverWeakHandle): New type definition.
* src/DefaultBtMessageReceiver.cc
(receiveHandshake): Use messageFactory member.
(sendHandshake): Use messageFactory member.
(receiveMessage): Use messageFactory member.
* src/DefaultBtMessageFactory.h
(dispatcher): New variable.
(requestFactory): New variable.
(peerConnection): New variablle.
(setBtMessageDispatcher): New function.
(setBtRequestFactory): New function.
(setPeerConnection): New function.
* src/SharedHandle.h
(RefCount): New class.
(WeakHandle): New class.
* src/PeerObject.h
(BtMessageReceiver.h): New include.
(PeerObject): Added btMessageReceiver.
(btMessageReceiver): New variable.
* src/Util.cc
(countBit): Simplified.
* src/BtCancelMessage.cc
(doReceivedAction): Use dispatcher member.
* src/BtRequestFactory.h
(BtRequestFactoryWeakHandle): New type definition.
* src/PeerStorage.h
(downloadSpeed): int -> uint32_t
(uploadSpeed): int -> uint32_t
(sessionDownloadLength): long long int -> uint64_t
(sessionUploadLength): long long int -> uint64_t
2007-01-16 15:20:26 +00:00
|
|
|
if(slot.isTimeout(requestTimeout)) {
|
2007-07-20 17:06:21 +00:00
|
|
|
logger->debug(MSG_DELETING_REQUEST_SLOT_TIMEOUT,
|
2006-12-24 06:25:21 +00:00
|
|
|
cuid,
|
|
|
|
slot.getBlockIndex());
|
|
|
|
piece->cancelBlock(slot.getBlockIndex());
|
2008-02-09 17:14:40 +00:00
|
|
|
peer->snubbing(true);
|
2006-12-24 06:25:21 +00:00
|
|
|
itr = requestSlots.erase(itr);
|
|
|
|
} else if(piece->hasBlock(slot.getBlockIndex())) {
|
2007-07-20 17:06:21 +00:00
|
|
|
logger->debug(MSG_DELETING_REQUEST_SLOT_ACQUIRED,
|
2006-12-24 06:25:21 +00:00
|
|
|
cuid,
|
|
|
|
slot.getBlockIndex());
|
2007-01-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To decrease CPU usage in bittorrent download, calculation
results in
BitfieldMan were cached and realtime fetching PeerObject was
removed
with WeakHandle introduced. Option values are set to the objects
by setter before download begins.
* src/DefaultBtRequestFactory.cc: Use messageFactory member.
* src/DefaultBtRequestFactory.h
(dispatcher): BtMessageDispatcherHandle ->
BtMessageDispatcherWeakHandle.
(messageFactory): New variable.
(setBtMessageDispatcher): BtMessageDispatcherHandle ->
BtMessageDispatcherWeakHandle.
(setBtMessageFactory): New function.
* src/DefaultBtMessageDispatcher.cc:
(sendMessages): Use maxUploadSpeedLimit instead of fetching the
value
from Option.
(checkRequestSlotAndDoNecessaryThing): Use requestTimeout
instead of
feating the value from Option.
Use messageFactory member.
* src/PeerInteractionCommand.cc
(PeerInteractionCommand): Added maxDownloadSpeedLimit.
Add reverse dependencies to factory object.
Set maxUploadSpeedLimit and requestTimeout and messageFactory to
dispatcher.
Set messageFactory to receiver.
Set keepAliveInterval and maxDownloadSpeedLimit and
messageFactory to
btInteractive.
Set receiver to peerObject.
Set maxDownloadSpeedLimit to this.
(executeInternal): Use maxDownloadSpeedLimit member.
* src/BtChokeMessage.cc
(doReceivedAction): Use dispatcher, requestFactory member.
(onSendComplete): Use dispatcher member.
* src/PeerInteractionCommand.h
(maxDownloadSpeedLimit): New variable.
* src/DefaultBtMessageReceiver.h
(peerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(dispatcher):
BtMessageDispatcherHandle -> BtMessageDispatcherWeakHandle
(messageFactory): New variable.
(setPeerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(getPeerConnection): PeerConnectionHandle ->
PeerConnectionWeakHandle
(setDispatcher):
BtMessageDispatcherHandle -> BtMessageDispatcherWeakHandle
(setBtMessageFactory): New function.
* src/DefaultBtInteractive.cc
(initiateHandshake): Use messageFactory member.
(addBitfieldMessageToQueue): Use messageFactory member.
(addAllowedFastMessageToQueue): Use messageFactory member.
(decideChoking): Use messageFactory member.
(checkHave): Use messageFactory member.
(sendKeepAlive): Use keepAliveInterval, messageFactory member.
(receiveMessages): Use maxDownloadSpeedLimit member.
(decideInterest): Use messageFactory member.
* src/BtRequestMessage.cc
(doReceivedAction): Use messageFactory, dispatcher member.
(onQueued): Use dispatcher member.
* src/BtPieceMessage.cc
(doReceivedAction): Use dispatcher member.
(send): Use peerConnection member.
(onWrongPiece): Use requestFactory member.
(handleChokingEvent): Use messageFactory, dispatcher member.
(handleCancelSendingPieceEvent): Use messageFactory, dispatcher
member.
* src/BtMessageDispatcher.h
(BtMessageDispatcherWeakHandle): New type definition.
* src/SimpleBtMessage.cc
(send): Use peerConnection member.
* src/BtRejectMessage.cc
(doReceivedAction): Use dispatcher member.
* src/DefaultBtMessageDispatcher.h
(Option.h): Removed include.
(messageFactory): New variable.
(option): Removed.
(maxUploadSpeedLimit): New variable.
(requestTimeout): New variable.
(DefaultBtMessageDispatcher): Removed option.
Added maxUploadSpeedLimit, requestTimeout.
(setOption): Removed.
(getOption): Removed.
(setMaxUploadSpeedLimit): New function.
(setRequestTimeout): New function.
(setBtMessageFactory): New function.
* src/DefaultBtInteractive.h
(btMessageReceiver):
BtMessageReceiverHandle -> BtMessageReceiverWeakHandle
(dispatcher):
BtMessageDispatcherHandle -> BtMessageReceiverWeakHandle
(btRequestFactory):
BtRequestFactoryHandle -> BtRequestFactoryWeakHandle
(peerConnection):
PeerConnectionHandle -> PeerConnectionWeakHandle
(messageFactory): New variable.
(option): Removed.
(keepAliveInterval): New variable.
(maxDownloadSpeedLimit): New variable.
(DefaultBtInteractive): Added keepAliveInterval,
maxDownloadSpeedLimit.
(setBtMessageReceiver):
BtMessageReceiverHandle -> BtMessageReceiverWeakHandle
(setDispatcher):
BtMessageDispatcherHandle -> BtMessageReceiverWeakHandle
(setBtRequestFactory):
BtRequestFactoryHandle -> BtRequestFactoryWeakHandle
(setPeerConnection):
PeerConnectionHandle -> PeerConnectionWeakHandle
(setOption): Removed.
(setKeepAliveInterval): New function.
(setMaxDownloadSpeedLimit): New function.
(setBtMessageFactory): New function.
* src/BitfieldMan.h
(cachedNumMissingBlock): New variable.
(cachedNumFilteredBlock): New variable.
(cachedCompletedLength): New variable.
(cachedFilteredComletedLength): New variable.
(cachedFilteredTotalLength): New variable.
(countMissingBlockNow): New function.
(countFilteredBlockNow): New function.
(getFilteredTotalLengthNow): New function.
(getCompletedLengthNow): New function.
(getFilteredCompletedLengthNow): New function.
(updateCache): New function.
* src/AbstractBtMessage.h
(BtMessageDispatcher.h): New include.
(PeerConnection.h): New include.
(BtRequestFactory.h): New include
(BtMessageFactory.h): New include.
(dispatcher): New variable.
(messageFactory): New variable.
(peerConnection: New variable.
(setBtMessageDispatcher): New function.
(setPeerConnection): New function.
(setBtMessageFactory): New function.
(setBtRequestFactory): New function.
* src/DefaultBtMessageFactory.cc
(setCommonProperty): Set dispatcher, requestFactory, this,
peerConnection to msg.
* src/BtRegistry.h
(BT_MESSAGE_RECEIVER): New macro.
* src/PeerConnection.h
(PeerConnectionWeakHandle): New type definition.
* src/BtMessageFactory.h
(BtMessageFactoryWeakHandle): New type definition.
* src/BitfieldMan.cc
(BitfieldMan): Added cachedNumMissingBlock,
cachedNumFilteredBlock,
cachedCompletedLength, cachedFilteredComletedLength,
cachedFilteredTotalLength.
Call updateCache().
(countMissingBlock): Return cachedNumMissingBlock.
(countMissingBlockNow): New function.
(countBlock): Return cachedNumFilteredBlock if filterEnabled is
true.
(countFilteredBlockNow): New function.
(setBit): Call updateCache().
(unsetBit): Call updateCache().
(setBitfield): Call updateCache().
(clearAllBit): Call updateCache().
(setAllBit): Use setBitInternal instead of setBit.
Call updateCache().
(addFilter): Call updateCache().
(enableFilter): Call updateCache().
(disableFilter): Call updateCache().
(clearFilter): Call updateCache().
(getFilteredTotalLength): Return cachedFilteredTotalLength.
(getFilteredTotalLengthNow): New function.
(getCompletedLength): Return cachedCompletedLength.
(getCompletedLengthNow): New function.
(getFilteredCompletedLength): Return
cachedFilteredComletedLength.
(getFilteredCompletedLengthNow): New function.
(updateCache): New function.
* src/BtMessageReceiver.h
(BtMessageReceiverWeakHandle): New type definition.
* src/DefaultBtMessageReceiver.cc
(receiveHandshake): Use messageFactory member.
(sendHandshake): Use messageFactory member.
(receiveMessage): Use messageFactory member.
* src/DefaultBtMessageFactory.h
(dispatcher): New variable.
(requestFactory): New variable.
(peerConnection): New variablle.
(setBtMessageDispatcher): New function.
(setBtRequestFactory): New function.
(setPeerConnection): New function.
* src/SharedHandle.h
(RefCount): New class.
(WeakHandle): New class.
* src/PeerObject.h
(BtMessageReceiver.h): New include.
(PeerObject): Added btMessageReceiver.
(btMessageReceiver): New variable.
* src/Util.cc
(countBit): Simplified.
* src/BtCancelMessage.cc
(doReceivedAction): Use dispatcher member.
* src/BtRequestFactory.h
(BtRequestFactoryWeakHandle): New type definition.
* src/PeerStorage.h
(downloadSpeed): int -> uint32_t
(uploadSpeed): int -> uint32_t
(sessionDownloadLength): long long int -> uint64_t
(sessionUploadLength): long long int -> uint64_t
2007-01-16 15:20:26 +00:00
|
|
|
addMessageToQueue(messageFactory->createCancelMessage(slot.getIndex(),
|
|
|
|
slot.getBegin(),
|
|
|
|
slot.getLength()));
|
2006-12-24 06:25:21 +00:00
|
|
|
itr = requestSlots.erase(itr);
|
|
|
|
} else {
|
|
|
|
itr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DefaultBtMessageDispatcher::isSendingInProgress()
|
|
|
|
{
|
|
|
|
if(messageQueue.size() > 0) {
|
|
|
|
return messageQueue.front()->isSendingInProgress();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t DefaultBtMessageDispatcher::countOutstandingRequest()
|
2006-12-24 06:25:21 +00:00
|
|
|
{
|
|
|
|
return requestSlots.size();
|
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
bool DefaultBtMessageDispatcher::isOutstandingRequest(size_t index, size_t blockIndex) {
|
2006-12-24 06:25:21 +00:00
|
|
|
for(RequestSlots::const_iterator itr = requestSlots.begin();
|
|
|
|
itr != requestSlots.end(); itr++) {
|
|
|
|
const RequestSlot& slot = *itr;
|
|
|
|
if(slot.getIndex() == index && slot.getBlockIndex() == blockIndex) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
RequestSlot
|
2008-03-09 12:24:01 +00:00
|
|
|
DefaultBtMessageDispatcher::getOutstandingRequest(size_t index, uint32_t begin, size_t length)
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
|
|
|
for(RequestSlots::iterator itr = requestSlots.begin();
|
|
|
|
itr != requestSlots.end(); itr++) {
|
|
|
|
if(itr->getIndex() == index &&
|
|
|
|
itr->getBegin() == begin &&
|
|
|
|
itr->getLength() == length) {
|
|
|
|
return *itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return RequestSlot::nullSlot;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcher::removeOutstandingRequest(const RequestSlot& slot)
|
|
|
|
{
|
|
|
|
RequestSlots temp;
|
2008-02-08 18:39:26 +00:00
|
|
|
std::remove_copy(requestSlots.begin(), requestSlots.end(), std::back_inserter(temp), slot);
|
2008-02-08 15:53:45 +00:00
|
|
|
requestSlots = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcher::addOutstandingRequest(const RequestSlot& requestSlot)
|
|
|
|
{
|
|
|
|
if(!isOutstandingRequest(requestSlot.getIndex(), requestSlot.getBlockIndex())) {
|
|
|
|
requestSlots.push_back(requestSlot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-13 01:25:36 +00:00
|
|
|
size_t DefaultBtMessageDispatcher::countOutstandingUpload()
|
|
|
|
{
|
|
|
|
return std::count_if(messageQueue.begin(), messageQueue.end(),
|
|
|
|
mem_fun_sh(&BtMessage::isUploading));
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::deque<SharedHandle<BtMessage> >&
|
|
|
|
DefaultBtMessageDispatcher::getMessageQueue()
|
|
|
|
{
|
|
|
|
return messageQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::deque<RequestSlot>& DefaultBtMessageDispatcher::getRequestSlots()
|
|
|
|
{
|
|
|
|
return requestSlots;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcher::setPeer(const SharedHandle<Peer>& peer)
|
|
|
|
{
|
|
|
|
this->peer = peer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcher::setBtContext(const BtContextHandle& btContext)
|
|
|
|
{
|
|
|
|
this->btContext = btContext;
|
|
|
|
this->pieceStorage = PIECE_STORAGE(btContext);
|
|
|
|
this->peerStorage = PEER_STORAGE(btContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultBtMessageDispatcher::setBtMessageFactory(const WeakHandle<BtMessageFactory>& factory)
|
|
|
|
{
|
|
|
|
this->messageFactory = factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aria2
|