summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2008-02-09 17:14:40 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2008-02-09 17:14:40 +0000
commitc064a2cd9e9034d6cd77bfdd70948cbef193d2cc (patch)
tree692e9d54e8032af919514f0d8608286ce47b74b9 /src
parent04a70520139efcfb287e329ae97e51ddf77605c6 (diff)
2008-02-10 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Extract the Peer class's member variables, which are only needed after PeerInteractionCommand, into PeerSessionResource class. This class is instantiated in PeerInteractionCommand class's ctor and released in its dtor. This will make Peer class lightweight and uses less memory for peers which are not connected and wait in the queue. * src/PeerChokeCommand.cc * src/PeerSessionResource.{h, cc} * src/PeerInteractionCommand.cc * src/PeerAbstractCommand.cc: Note: 0 is given to onAbort() function. * src/DefaultBtInteractive.cc * src/BtPieceMessage.cc * src/BtInterestedMessage.cc * src/BtUnchokeMessage.cc * src/DefaultPeerStorage.{h, cc} * src/PeerInitiateConnectionCommand.cc * src/ActivePeerConnectionCommand.cc * src/BtNotInterestedMessage.cc * src/DefaultBtMessageDispatcher.cc * src/BtChokeMessage.cc * src/BtRequestMessage.cc * src/Peer.{h, cc} * src/BtRegistry.h * src/TrackerWatcherCommand.cc * src/PeerReceiveHandshakeCommand.cc * test/BtExtendedMessageTest.cc * test/BtAllowedFastMessageTest.cc * test/BtCancelMessageTest.cc * test/DefaultPieceStorageTest.cc * test/BtBitfieldMessageTest.cc * test/BtHaveMessageTest.cc * test/BtNotInterestedMessageTest.cc * test/BtRequestMessageTest.cc * test/PeerSessionResourceTest.cc * test/DefaultBtMessageDispatcherTest.cc * test/PeerTest.cc * test/BtInterestedMessageTest.cc * test/BtRejectMessageTest.cc * test/BtChokeMessageTest.cc * test/DefaultPeerStorageTest.cc * test/BtHaveNoneMessageTest.cc * test/BtHaveAllMessageTest.cc * test/DefaultExtensionMessageFactoryTest.cc * test/BtUnchokeMessageTest.cc * test/DefaultBtMessageFactoryTest.cc * test/HandshakeExtensionMessageTest.cc * test/UTPexExtensionMessageTest.cc * test/DefaultBtRequestFactoryTest.cc * test/BtPieceMessageTest.cc Removed typedef PeerStats. * src/PeerStat.h * src/SegmentMan.cc
Diffstat (limited to 'src')
-rw-r--r--src/ActivePeerConnectionCommand.cc4
-rw-r--r--src/BtChokeMessage.cc6
-rw-r--r--src/BtInterestedMessage.cc6
-rw-r--r--src/BtNotInterestedMessage.cc6
-rw-r--r--src/BtPieceMessage.cc2
-rw-r--r--src/BtRegistry.h2
-rw-r--r--src/BtRequestMessage.cc4
-rw-r--r--src/BtUnchokeMessage.cc6
-rw-r--r--src/DefaultBtInteractive.cc14
-rw-r--r--src/DefaultBtMessageDispatcher.cc2
-rw-r--r--src/DefaultPeerStorage.cc19
-rw-r--r--src/DefaultPeerStorage.h3
-rw-r--r--src/Makefile.am3
-rw-r--r--src/Makefile.in46
-rw-r--r--src/Peer.cc384
-rw-r--r--src/Peer.h173
-rw-r--r--src/PeerAbstractCommand.cc2
-rw-r--r--src/PeerChokeCommand.cc20
-rw-r--r--src/PeerInitiateConnectionCommand.cc4
-rw-r--r--src/PeerInteractionCommand.cc18
-rw-r--r--src/PeerReceiveHandshakeCommand.cc4
-rw-r--r--src/PeerSessionResource.cc316
-rw-r--r--src/PeerSessionResource.h186
-rw-r--r--src/PeerStat.h1
-rw-r--r--src/SegmentMan.cc5
-rw-r--r--src/TrackerWatcherCommand.cc6
26 files changed, 940 insertions, 302 deletions
diff --git a/src/ActivePeerConnectionCommand.cc b/src/ActivePeerConnectionCommand.cc
index 360a06a2..cae26834 100644
--- a/src/ActivePeerConnectionCommand.cc
+++ b/src/ActivePeerConnectionCommand.cc
@@ -87,9 +87,9 @@ void ActivePeerConnectionCommand::connectToPeer(const PeerHandle& peer)
if(peer.isNull()) {
return;
}
- peer->cuid = CUIDCounterSingletonHolder::instance()->newID();
+ peer->usedBy(CUIDCounterSingletonHolder::instance()->newID());
PeerInitiateConnectionCommand* command =
- new PeerInitiateConnectionCommand(peer->cuid, _requestGroup, peer, e, btContext);
+ new PeerInitiateConnectionCommand(peer->usedBy(), _requestGroup, peer, e, btContext);
e->commands.push_back(command);
logger->info(MSG_CONNECTING_TO_PEER,
cuid, peer->ipaddr.c_str());
diff --git a/src/BtChokeMessage.cc b/src/BtChokeMessage.cc
index 7b9eda8a..cac5a8b9 100644
--- a/src/BtChokeMessage.cc
+++ b/src/BtChokeMessage.cc
@@ -55,13 +55,13 @@ BtChokeMessageHandle BtChokeMessage::create(const unsigned char* data, int32_t d
}
void BtChokeMessage::doReceivedAction() {
- peer->peerChoking = true;
+ peer->peerChoking(true);
dispatcher->doChokedAction();
requestFactory->doChokedAction();
}
bool BtChokeMessage::sendPredicate() const {
- return !peer->amChoking;
+ return !peer->amChoking();
}
int32_t BtChokeMessage::MESSAGE_LENGTH = 5;
@@ -84,7 +84,7 @@ int32_t BtChokeMessage::getMessageLength() {
}
void BtChokeMessage::onSendComplete() {
- peer->amChoking = true;
+ peer->amChoking(true);
dispatcher->doChokingAction();
}
diff --git a/src/BtInterestedMessage.cc b/src/BtInterestedMessage.cc
index 0acdd354..f6f824a1 100644
--- a/src/BtInterestedMessage.cc
+++ b/src/BtInterestedMessage.cc
@@ -53,11 +53,11 @@ BtInterestedMessageHandle BtInterestedMessage::create(const unsigned char* data,
}
void BtInterestedMessage::doReceivedAction() {
- peer->peerInterested = true;
+ peer->peerInterested(true);
}
bool BtInterestedMessage::sendPredicate() const {
- return !peer->amInterested;
+ return !peer->amInterested();
}
int32_t BtInterestedMessage::MESSAGE_LENGTH = 5;
@@ -80,7 +80,7 @@ int32_t BtInterestedMessage::getMessageLength() {
}
void BtInterestedMessage::onSendComplete() {
- peer->amInterested = true;
+ peer->amInterested(true);
}
std::string BtInterestedMessage::toString() const {
diff --git a/src/BtNotInterestedMessage.cc b/src/BtNotInterestedMessage.cc
index 79436a2f..47794312 100644
--- a/src/BtNotInterestedMessage.cc
+++ b/src/BtNotInterestedMessage.cc
@@ -53,11 +53,11 @@ BtNotInterestedMessageHandle BtNotInterestedMessage::create(const unsigned char*
}
void BtNotInterestedMessage::doReceivedAction() {
- peer->peerInterested = false;
+ peer->peerInterested(false);
}
bool BtNotInterestedMessage::sendPredicate() const {
- return peer->amInterested;
+ return peer->amInterested();
}
int32_t BtNotInterestedMessage::MESSAGE_LENGTH = 5;
@@ -80,7 +80,7 @@ int32_t BtNotInterestedMessage::getMessageLength() {
}
void BtNotInterestedMessage::onSendComplete() {
- peer->amInterested = false;
+ peer->amInterested(false);
}
std::string BtNotInterestedMessage::toString() const {
diff --git a/src/BtPieceMessage.cc b/src/BtPieceMessage.cc
index 4c899145..d33c79b0 100644
--- a/src/BtPieceMessage.cc
+++ b/src/BtPieceMessage.cc
@@ -82,7 +82,7 @@ void BtPieceMessage::doReceivedAction() {
blockLength);
peer->updateDownloadLength(blockLength);
if(!RequestSlot::isNull(slot)) {
- peer->snubbing = false;
+ peer->snubbing(false);
peer->updateLatency(slot.getLatencyInMillis());
PieceHandle piece = pieceStorage->getPiece(index);
int64_t offset =
diff --git a/src/BtRegistry.h b/src/BtRegistry.h
index 10770150..70b3f29c 100644
--- a/src/BtRegistry.h
+++ b/src/BtRegistry.h
@@ -134,7 +134,7 @@ BtRegistry::getBtProgressInfoFile(btContext->getInfoHashAsString())
BtRegistry::getPeerObjectCluster(btContext->getInfoHashAsString())
#define PEER_OBJECT(btContext, peer) \
-PEER_OBJECT_CLUSTER(btContext)->getHandle(peer->getId())
+PEER_OBJECT_CLUSTER(btContext)->getHandle(peer->getID())
#define BT_MESSAGE_DISPATCHER(btContext, peer) \
PEER_OBJECT(btContext, peer)->btMessageDispatcher
diff --git a/src/BtRequestMessage.cc b/src/BtRequestMessage.cc
index a07f83c3..e8b2d810 100644
--- a/src/BtRequestMessage.cc
+++ b/src/BtRequestMessage.cc
@@ -64,8 +64,8 @@ BtRequestMessageHandle BtRequestMessage::create(const unsigned char* data, int32
void BtRequestMessage::doReceivedAction() {
if(pieceStorage->hasPiece(index) &&
- (!peer->amChoking ||
- peer->amChoking && peer->isInAmAllowedIndexSet(index))) {
+ (!peer->amChoking() ||
+ peer->amChoking() && peer->isInAmAllowedIndexSet(index))) {
BtMessageHandle msg = messageFactory->createPieceMessage(index,
begin,
length);
diff --git a/src/BtUnchokeMessage.cc b/src/BtUnchokeMessage.cc
index 949888f5..18146fff 100644
--- a/src/BtUnchokeMessage.cc
+++ b/src/BtUnchokeMessage.cc
@@ -53,11 +53,11 @@ BtUnchokeMessageHandle BtUnchokeMessage::create(const unsigned char* data, int32
}
void BtUnchokeMessage::doReceivedAction() {
- peer->peerChoking = false;
+ peer->peerChoking(false);
}
bool BtUnchokeMessage::sendPredicate() const {
- return peer->amChoking;
+ return peer->amChoking();
}
int32_t BtUnchokeMessage::MESSAGE_LENGTH = 5;
@@ -80,7 +80,7 @@ int32_t BtUnchokeMessage::getMessageLength() {
}
void BtUnchokeMessage::onSendComplete() {
- peer->amChoking = false;
+ peer->amChoking(false);
}
std::string BtUnchokeMessage::toString() const {
diff --git a/src/DefaultBtInteractive.cc b/src/DefaultBtInteractive.cc
index f1b2a695..80f59a10 100644
--- a/src/DefaultBtInteractive.cc
+++ b/src/DefaultBtInteractive.cc
@@ -188,11 +188,11 @@ void DefaultBtInteractive::addAllowedFastMessageToQueue() {
void DefaultBtInteractive::decideChoking() {
if(peer->shouldBeChoking()) {
- if(!peer->amChoking) {
+ if(!peer->amChoking()) {
dispatcher->addMessageToQueue(messageFactory->createChokeMessage());
}
} else {
- if(peer->amChoking) {
+ if(peer->amChoking()) {
dispatcher->addMessageToQueue(messageFactory->createUnchokeMessage());
}
}
@@ -247,12 +247,12 @@ size_t DefaultBtInteractive::receiveMessages() {
floodingStat.incKeepAliveCount();
break;
case BtChokeMessage::ID:
- if(!peer->peerChoking) {
+ if(!peer->peerChoking()) {
floodingStat.incChokeUnchokeCount();
}
break;
case BtUnchokeMessage::ID:
- if(peer->peerChoking) {
+ if(peer->peerChoking()) {
floodingStat.incChokeUnchokeCount();
}
break;
@@ -267,13 +267,13 @@ size_t DefaultBtInteractive::receiveMessages() {
void DefaultBtInteractive::decideInterest() {
if(pieceStorage->hasMissingPiece(peer)) {
- if(!peer->amInterested) {
+ if(!peer->amInterested()) {
logger->debug(MSG_PEER_INTERESTED, cuid);
dispatcher->
addMessageToQueue(messageFactory->createInterestedMessage());
}
} else {
- if(peer->amInterested) {
+ if(peer->amInterested()) {
logger->debug(MSG_PEER_NOT_INTERESTED, cuid);
dispatcher->
addMessageToQueue(messageFactory->createNotInterestedMessage());
@@ -283,7 +283,7 @@ void DefaultBtInteractive::decideInterest() {
void DefaultBtInteractive::fillPiece(int maxPieceNum) {
if(pieceStorage->hasMissingPiece(peer)) {
- if(peer->peerChoking) {
+ if(peer->peerChoking()) {
if(peer->isFastExtensionEnabled()) {
while(btRequestFactory->countTargetPiece() < maxPieceNum) {
PieceHandle piece = pieceStorage->getMissingFastPiece(peer);
diff --git a/src/DefaultBtMessageDispatcher.cc b/src/DefaultBtMessageDispatcher.cc
index 9d9ad8d8..8f5a0b5f 100644
--- a/src/DefaultBtMessageDispatcher.cc
+++ b/src/DefaultBtMessageDispatcher.cc
@@ -197,7 +197,7 @@ void DefaultBtMessageDispatcher::checkRequestSlotAndDoNecessaryThing()
cuid,
slot.getBlockIndex());
piece->cancelBlock(slot.getBlockIndex());
- peer->snubbing = true;
+ peer->snubbing(true);
itr = requestSlots.erase(itr);
} else if(piece->hasBlock(slot.getBlockIndex())) {
logger->debug(MSG_DELETING_REQUEST_SLOT_ACQUIRED,
diff --git a/src/DefaultPeerStorage.cc b/src/DefaultPeerStorage.cc
index a00409c1..c79a4dca 100644
--- a/src/DefaultPeerStorage.cc
+++ b/src/DefaultPeerStorage.cc
@@ -106,7 +106,7 @@ const Peers& DefaultPeerStorage::getPeers() {
class FindFinePeer {
public:
bool operator()(const PeerHandle& peer) const {
- return peer->cuid == 0 && peer->isGood();
+ return peer->unused() && peer->isGood();
}
};
@@ -184,9 +184,9 @@ public:
if(peer->isActive()) {
_stat.downloadSpeed += peer->calculateDownloadSpeed(_now);
_stat.uploadSpeed += peer->calculateUploadSpeed(_now);
+ _stat.sessionDownloadLength += peer->getSessionDownloadLength();
+ _stat.sessionUploadLength += peer->getSessionUploadLength();
}
- _stat.sessionDownloadLength += peer->getSessionDownloadLength();
- _stat.sessionUploadLength += peer->getSessionUploadLength();
}
const TransferStat& getTransferStat() { return _stat; }
@@ -206,7 +206,7 @@ void DefaultPeerStorage::deleteUnusedPeer(int32_t delSize) {
for(Peers::reverse_iterator itr = peers.rbegin();
itr != peers.rend(); ++itr) {
const PeerHandle& p = *itr;
- if(p->cuid == 0 && delSize > 0) {
+ if(p->unused() && delSize > 0) {
// Update removedPeerSession******Length
onErasingPeer(p);
delSize--;
@@ -217,10 +217,14 @@ void DefaultPeerStorage::deleteUnusedPeer(int32_t delSize) {
peers = temp;
}
-void DefaultPeerStorage::onErasingPeer(const PeerHandle& peer)
+void DefaultPeerStorage::onErasingPeer(const SharedHandle<Peer>& peer) {}
+
+void DefaultPeerStorage::onReturningPeer(const SharedHandle<Peer>& peer)
{
- removedPeerSessionDownloadLength += peer->getSessionDownloadLength();
- removedPeerSessionUploadLength += peer->getSessionUploadLength();
+ if(peer->isActive()) {
+ removedPeerSessionDownloadLength += peer->getSessionDownloadLength();
+ removedPeerSessionUploadLength += peer->getSessionUploadLength();
+ }
}
void DefaultPeerStorage::returnPeer(const PeerHandle& peer)
@@ -229,6 +233,7 @@ void DefaultPeerStorage::returnPeer(const PeerHandle& peer)
if(itr == peers.end()) {
logger->debug("Cannot find peer %s:%u in PeerStorage.", peer->ipaddr.c_str(), peer->port);
} else {
+ onReturningPeer(peer);
if((*itr)->port == 0) {
onErasingPeer(*itr);
peers.erase(itr);
diff --git a/src/DefaultPeerStorage.h b/src/DefaultPeerStorage.h
index ae4d52ec..40148cec 100644
--- a/src/DefaultPeerStorage.h
+++ b/src/DefaultPeerStorage.h
@@ -98,6 +98,9 @@ public:
void deleteUnusedPeer(int32_t delSize);
void onErasingPeer(const SharedHandle<Peer>& peer);
+
+ void onReturningPeer(const SharedHandle<Peer>& peer);
+
};
} // namespace aria2
diff --git a/src/Makefile.am b/src/Makefile.am
index f549a8b0..80f96b4b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -143,7 +143,8 @@ SRCS = Socket.h\
CopyDiskAdaptor.cc CopyDiskAdaptor.h\
DirectDiskAdaptor.cc DirectDiskAdaptor.h\
MultiDiskAdaptor.cc MultiDiskAdaptor.h\
- Peer.cc Peer.h\
+ Peer.cc\
+ PeerSessionResource.cc\
BtRegistry.cc BtRegistry.h\
MultiFileAllocationIterator.cc MultiFileAllocationIterator.h\
PeerConnection.cc PeerConnection.h\
diff --git a/src/Makefile.in b/src/Makefile.in
index 33b0e6cc..d715506a 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -349,13 +349,13 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
DiskAdaptor.cc DiskAdaptor.h AbstractSingleDiskAdaptor.cc \
AbstractSingleDiskAdaptor.h CopyDiskAdaptor.cc \
CopyDiskAdaptor.h DirectDiskAdaptor.cc DirectDiskAdaptor.h \
- MultiDiskAdaptor.cc MultiDiskAdaptor.h Peer.cc Peer.h \
- BtRegistry.cc BtRegistry.h MultiFileAllocationIterator.cc \
- MultiFileAllocationIterator.h PeerConnection.cc \
- PeerConnection.h ByteArrayDiskWriter.cc ByteArrayDiskWriter.h \
- ByteArrayDiskWriterFactory.cc ServerHost.cc HelpItem.cc \
- TaggedItem.cc TagContainer.cc HelpItemFactory.cc \
- SingleFileDownloadContext.cc \
+ MultiDiskAdaptor.cc MultiDiskAdaptor.h Peer.cc \
+ PeerSessionResource.cc BtRegistry.cc BtRegistry.h \
+ MultiFileAllocationIterator.cc MultiFileAllocationIterator.h \
+ PeerConnection.cc PeerConnection.h ByteArrayDiskWriter.cc \
+ ByteArrayDiskWriter.h ByteArrayDiskWriterFactory.cc \
+ ServerHost.cc HelpItem.cc TaggedItem.cc TagContainer.cc \
+ HelpItemFactory.cc SingleFileDownloadContext.cc \
IteratableChunkChecksumValidator.cc \
IteratableChunkChecksumValidator.h \
IteratableChecksumValidator.cc IteratableChecksumValidator.h \
@@ -368,7 +368,7 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
PeerMessageUtil.h PeerAbstractCommand.cc PeerAbstractCommand.h \
PeerInitiateConnectionCommand.cc \
PeerInitiateConnectionCommand.h PeerInteractionCommand.cc \
- PeerInteractionCommand.h PeerListenCommand.cc \
+ PeerInteractionCommand.h Peer.h PeerListenCommand.cc \
PeerListenCommand.h RequestSlot.cc RequestSlot.h \
TrackerWatcherCommand.cc TrackerWatcherCommand.h \
PeerChokeCommand.cc PeerChokeCommand.h SeedCriteria.h \
@@ -670,9 +670,9 @@ am__objects_14 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \
StreamCheckIntegrityEntry.$(OBJEXT) DiskAdaptor.$(OBJEXT) \
AbstractSingleDiskAdaptor.$(OBJEXT) CopyDiskAdaptor.$(OBJEXT) \
DirectDiskAdaptor.$(OBJEXT) MultiDiskAdaptor.$(OBJEXT) \
- Peer.$(OBJEXT) BtRegistry.$(OBJEXT) \
- MultiFileAllocationIterator.$(OBJEXT) PeerConnection.$(OBJEXT) \
- ByteArrayDiskWriter.$(OBJEXT) \
+ Peer.$(OBJEXT) PeerSessionResource.$(OBJEXT) \
+ BtRegistry.$(OBJEXT) MultiFileAllocationIterator.$(OBJEXT) \
+ PeerConnection.$(OBJEXT) ByteArrayDiskWriter.$(OBJEXT) \
ByteArrayDiskWriterFactory.$(OBJEXT) ServerHost.$(OBJEXT) \
HelpItem.$(OBJEXT) TaggedItem.$(OBJEXT) TagContainer.$(OBJEXT) \
HelpItemFactory.$(OBJEXT) SingleFileDownloadContext.$(OBJEXT) \
@@ -996,17 +996,18 @@ SRCS = Socket.h SocketCore.cc SocketCore.h Command.cc Command.h \
DiskAdaptor.cc DiskAdaptor.h AbstractSingleDiskAdaptor.cc \
AbstractSingleDiskAdaptor.h CopyDiskAdaptor.cc \
CopyDiskAdaptor.h DirectDiskAdaptor.cc DirectDiskAdaptor.h \
- MultiDiskAdaptor.cc MultiDiskAdaptor.h Peer.cc Peer.h \
- BtRegistry.cc BtRegistry.h MultiFileAllocationIterator.cc \
- MultiFileAllocationIterator.h PeerConnection.cc \
- PeerConnection.h ByteArrayDiskWriter.cc ByteArrayDiskWriter.h \
- ByteArrayDiskWriterFactory.cc ServerHost.cc HelpItem.cc \
- TaggedItem.cc TagContainer.cc HelpItemFactory.cc \
- SingleFileDownloadContext.cc $(am__append_1) $(am__append_2) \
- $(am__append_3) $(am__append_4) $(am__append_5) \
- $(am__append_6) $(am__append_7) $(am__append_8) \
- $(am__append_9) $(am__append_10) $(am__append_11) \
- $(am__append_12) $(am__append_13)
+ MultiDiskAdaptor.cc MultiDiskAdaptor.h Peer.cc \
+ PeerSessionResource.cc BtRegistry.cc BtRegistry.h \
+ MultiFileAllocationIterator.cc MultiFileAllocationIterator.h \
+ PeerConnection.cc PeerConnection.h ByteArrayDiskWriter.cc \
+ ByteArrayDiskWriter.h ByteArrayDiskWriterFactory.cc \
+ ServerHost.cc HelpItem.cc TaggedItem.cc TagContainer.cc \
+ HelpItemFactory.cc SingleFileDownloadContext.cc \
+ $(am__append_1) $(am__append_2) $(am__append_3) \
+ $(am__append_4) $(am__append_5) $(am__append_6) \
+ $(am__append_7) $(am__append_8) $(am__append_9) \
+ $(am__append_10) $(am__append_11) $(am__append_12) \
+ $(am__append_13)
noinst_LIBRARIES = libaria2c.a
libaria2c_a_SOURCES = $(SRCS)
aria2c_LDADD = libaria2c.a @LIBINTL@ @ALLOCA@ @LIBGNUTLS_LIBS@\
@@ -1306,6 +1307,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerListenCommand.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerMessageUtil.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerReceiveHandshakeCommand.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerSessionResource.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Piece.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PieceHashCheckIntegrityEntry.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PieceHashMetalinkParserState.Po@am__quote@
diff --git a/src/Peer.cc b/src/Peer.cc
index 521812eb..793297b3 100644
--- a/src/Peer.cc
+++ b/src/Peer.cc
@@ -33,15 +33,13 @@
*/
/* copyright --> */
#include "Peer.h"
-#include "BitfieldManFactory.h"
-#include "BitfieldMan.h"
#include "Util.h"
+#include "PeerSessionResource.h"
#ifdef ENABLE_MESSAGE_DIGEST
# include "MessageDigestHelper.h"
#endif // ENABLE_MESSAGE_DIGEST
#include <cstring>
#include <cassert>
-#include <algorithm>
namespace aria2 {
@@ -50,12 +48,9 @@ namespace aria2 {
Peer::Peer(std::string ipaddr, uint16_t port):
ipaddr(ipaddr),
port(port),
- _bitfield(0),
- sessionUploadLength(0),
- sessionDownloadLength(0),
- active(false),
_badConditionStartTime(0),
- _seeder(false)
+ _seeder(false),
+ _res(0)
{
resetStatus();
std::string idSeed = ipaddr+":"+Util::itos(port);
@@ -68,7 +63,7 @@ Peer::Peer(std::string ipaddr, uint16_t port):
Peer::~Peer()
{
- delete _bitfield;
+ releaseSessionResource();
}
bool Peer::operator==(const Peer& p)
@@ -81,208 +76,303 @@ bool Peer::operator!=(const Peer& p)
return !(*this == p);
}
-void Peer::allocateBitfield(int32_t pieceLength, int64_t totalLength)
+const std::string& Peer::getID() const
{
- delete _bitfield;
- _bitfield = BitfieldManFactory::getFactoryInstance()->createBitfieldMan(pieceLength, totalLength);
+ return id;
}
-void Peer::deallocateBitfield()
+void Peer::usedBy(int32_t cuid)
{
- delete _bitfield;
- _bitfield = 0;
+ _cuid = cuid;
}
-void Peer::updateUploadLength(int32_t bytes)
+int32_t Peer::usedBy() const
{
- peerStat.updateUploadLength(bytes);
- sessionUploadLength += bytes;
+ return _cuid;
}
-void Peer::updateDownloadLength(int32_t bytes)
+bool Peer::unused() const
{
- peerStat.updateDownloadLength(bytes);
- sessionDownloadLength += bytes;
+ return _cuid == 0;
}
-void Peer::updateSeeder()
+void Peer::allocateSessionResource(int32_t pieceLength, int64_t totalLength)
{
- assert(_bitfield);
- if(_bitfield->isAllBitSet()) {
- _seeder = true;
- }
+ delete _res;
+ _res = new PeerSessionResource(pieceLength, totalLength);
+ _res->getPeerStat().downloadStart();
}
-void Peer::updateBitfield(int32_t index, int32_t operation) {
- assert(_bitfield);
- if(operation == 1) {
- _bitfield->setBit(index);
- } else if(operation == 0) {
- _bitfield->unsetBit(index);
- }
- updateSeeder();
+void Peer::releaseSessionResource()
+{
+ delete _res;
+ _res = 0;
}
-int32_t Peer::calculateUploadSpeed()
+bool Peer::isActive() const
{
- return peerStat.calculateUploadSpeed();
+ return _res != 0;
}
-int32_t Peer::calculateUploadSpeed(const struct timeval& now)
+void Peer::setPeerId(const unsigned char* peerId)
{
- return peerStat.calculateUploadSpeed(now);
+ memcpy(_peerId, peerId, PEER_ID_LENGTH);
}
-int32_t Peer::calculateDownloadSpeed()
+const unsigned char* Peer::getPeerId() const
{
- return peerStat.calculateDownloadSpeed();
+ return _peerId;
}
-int32_t Peer::calculateDownloadSpeed(const struct timeval& now)
+void Peer::resetStatus() {
+ _cuid = 0;
+}
+
+bool Peer::amChoking() const
{
- return peerStat.calculateDownloadSpeed(now);
+ assert(_res);
+ return _res->amChoking();
}
-int64_t Peer::getSessionUploadLength() const
+void Peer::amChoking(bool b) const
{
- return sessionUploadLength;
+ assert(_res);
+ _res->amChoking(b);
}
-int64_t Peer::getSessionDownloadLength() const
+// localhost is interested in this peer
+bool Peer::amInterested() const
{
- return sessionDownloadLength;
+ assert(_res);
+ return _res->amInterested();
}
-void Peer::activate()
+void Peer::amInterested(bool b) const
{
- peerStat.downloadStart();
- active = true;
+ assert(_res);
+ _res->amInterested(b);
}
-void Peer::deactivate()
+// this peer is choking localhost
+bool Peer::peerChoking() const
{
- peerStat.downloadStop();
- active = false;
+ assert(_res);
+ return _res->peerChoking();
}
-bool Peer::isActive() const
+void Peer::peerChoking(bool b) const
{
- return active;
+ assert(_res);
+ _res->peerChoking(b);
}
-void Peer::setPeerId(const unsigned char* peerId)
+// this peer is interested in localhost
+bool Peer::peerInterested() const
{
- memcpy(this->peerId, peerId, PEER_ID_LENGTH);
+ assert(_res);
+ return _res->peerInterested();
}
-const unsigned char* Peer::getPeerId() const
+void Peer::peerInterested(bool b)
{
- return this->peerId;
+ assert(_res);
+ _res->peerInterested(b);
}
+ // this peer should be choked
+bool Peer::chokingRequired() const
+{
+ assert(_res);
+ return _res->chokingRequired();
+}
+
+void Peer::chokingRequired(bool b)
+{
+ assert(_res);
+ _res->chokingRequired(b);
+}
+
+// this peer is eligible for unchoking optionally.
+bool Peer::optUnchoking() const
+{
+ assert(_res);
+ return _res->optUnchoking();
+}
+
+void Peer::optUnchoking(bool b)
+{
+ assert(_res);
+ _res->optUnchoking(b);
+}
+
+// this peer is snubbing.
+bool Peer::snubbing() const
+{
+ assert(_res);
+ return _res->snubbing();
+}
+
+void Peer::snubbing(bool b)
+{
+ assert(_res);
+ _res->snubbing(b);
+}
+
+void Peer::updateUploadLength(int32_t bytes)
+{
+ assert(_res);
+ _res->updateUploadLength(bytes);
+}
+
+void Peer::updateDownloadLength(int32_t bytes)
+{
+ assert(_res);
+ _res->updateDownloadLength(bytes);
+}
+
+void Peer::updateSeeder()
+{
+ assert(_res);
+ if(_res->hasAllPieces()) {
+ _seeder = true;
+ }
+}
+
+void Peer::updateBitfield(int32_t index, int32_t operation) {
+ assert(_res);
+ _res->updateBitfield(index, operation);
+ updateSeeder();
+}
+
+int32_t Peer::calculateUploadSpeed()
+{
+ assert(_res);
+ return _res->getPeerStat().calculateUploadSpeed();
+}
+
+int32_t Peer::calculateUploadSpeed(const struct timeval& now)
+{
+ assert(_res);
+ return _res->getPeerStat().calculateUploadSpeed(now);
+}
+
+int32_t Peer::calculateDownloadSpeed()
+{
+ assert(_res);
+ return _res->getPeerStat().calculateDownloadSpeed();
+}
+
+int32_t Peer::calculateDownloadSpeed(const struct timeval& now)
+{
+ assert(_res);
+ return _res->getPeerStat().calculateDownloadSpeed(now);
+}
+
+int64_t Peer::getSessionUploadLength() const
+{
+ assert(_res);
+ return _res->uploadLength();
+}
+
+int64_t Peer::getSessionDownloadLength() const
+{
+ assert(_res);
+ return _res->downloadLength();
+}
+
void Peer::setBitfield(const unsigned char* bitfield, int32_t bitfieldLength)
{
- assert(_bitfield);
- _bitfield->setBitfield(bitfield, bitfieldLength);
+ assert(_res);
+ _res->setBitfield(bitfield, bitfieldLength);
updateSeeder();
}
const unsigned char* Peer::getBitfield() const
{
- assert(_bitfield);
- return _bitfield->getBitfield();
+ assert(_res);
+ return _res->getBitfield();
}
int32_t Peer::getBitfieldLength() const
{
- assert(_bitfield);
- return _bitfield->getBitfieldLength();
+ assert(_res);
+ return _res->getBitfieldLength();
}
-#define THRESHOLD 1024*1024*2
-
bool Peer::shouldBeChoking() const {
- if(optUnchoking) {
- return false;
- }
- return chokingRequired;
+ assert(_res);
+ return _res->shouldBeChoking();
}
bool Peer::hasPiece(int32_t index) const {
- assert(_bitfield);
- return _bitfield->isBitSet(index);
-}
-
-void Peer::resetStatus() {
- tryCount = 0;
- cuid = 0;
- amChoking = true;
- amInterested = false;
- peerChoking = true;
- peerInterested = false;
- chokingRequired = true;
- optUnchoking = false;
- snubbing = false;
- fastExtensionEnabled = false;
- _extendedMessagingEnabled = false;
- _extensions.clear();
- _dhtEnabled = false;
- latency = DEFAULT_LATENCY;
- peerAllowedIndexSet.clear();
- amAllowedIndexSet.clear();
- peerStat.reset();
+ assert(_res);
+ return _res->hasPiece(index);
}
void Peer::setFastExtensionEnabled(bool enabled)
{
- fastExtensionEnabled = enabled;
+ assert(_res);
+ return _res->fastExtensionEnabled(enabled);
}
bool Peer::isFastExtensionEnabled() const
{
- return fastExtensionEnabled;
+ assert(_res);
+ return _res->fastExtensionEnabled();
}
-int32_t Peer::countPeerAllowedIndexSet() const
+size_t Peer::countPeerAllowedIndexSet() const
{
- return peerAllowedIndexSet.size();
+ assert(_res);
+ return _res->peerAllowedIndexSet().size();
}
const std::deque<int32_t>& Peer::getPeerAllowedIndexSet() const
{
- return peerAllowedIndexSet;
+ assert(_res);
+ return _res->peerAllowedIndexSet();
}
-bool Peer::isInPeerAllowedIndexSet(int32_t index) const {
- return std::find(peerAllowedIndexSet.begin(), peerAllowedIndexSet.end(),
- index) != peerAllowedIndexSet.end();
+bool Peer::isInPeerAllowedIndexSet(int32_t index) const
+{
+ assert(_res);
+ return _res->peerAllowedIndexSetContains(index);
}
-void Peer::addPeerAllowedIndex(int32_t index) {
- if(!isInPeerAllowedIndexSet(index)) {
- peerAllowedIndexSet.push_back(index);
- }
+void Peer::addPeerAllowedIndex(int32_t index)
+{
+ assert(_res);
+ _res->addPeerAllowedIndex(index);
}
-bool Peer::isInAmAllowedIndexSet(int32_t index) const {
- return std::find(amAllowedIndexSet.begin(), amAllowedIndexSet.end(),
- index) != amAllowedIndexSet.end();
+bool Peer::isInAmAllowedIndexSet(int32_t index) const
+{
+ assert(_res);
+ return _res->amAllowedIndexSetContains(index);
}
-void Peer::addAmAllowedIndex(int32_t index) {
- if(!isInAmAllowedIndexSet(index)) {
- amAllowedIndexSet.push_back(index);
- }
+void Peer::addAmAllowedIndex(int32_t index)
+{
+ assert(_res);
+ _res->addAmAllowedIndex(index);
}
void Peer::setAllBitfield() {
- assert(_bitfield);
- _bitfield->setAllBit();
+ assert(_res);
+ _res->markSeeder();
_seeder = true;
}
-void Peer::updateLatency(int32_t latency) {
- this->latency = (this->latency*20+latency*80)/200;
+void Peer::updateLatency(int32_t latency)
+{
+ assert(_res);
+ _res->updateLatency(latency);
+}
+
+int32_t Peer::getLatency() const
+{
+ assert(_res);
+ return _res->latency();
}
void Peer::startBadCondition()
@@ -295,31 +385,61 @@ bool Peer::isGood() const
return _badConditionStartTime.elapsed(BAD_CONDITION_INTERVAL);
}
-uint8_t Peer::getExtensionMessageID(const std::string& name)
+uint8_t Peer::getExtensionMessageID(const std::string& name) const
{
- Extensions::const_iterator itr = _extensions.find(name);
- if(itr == _extensions.end()) {
- return 0;
- } else {
- return (*itr).second;
- }
+ assert(_res);
+ return _res->getExtensionMessageID(name);
}
-std::string Peer::getExtensionName(uint8_t id)
+std::string Peer::getExtensionName(uint8_t id) const
{
- for(Extensions::const_iterator itr = _extensions.begin();
- itr != _extensions.end(); ++itr) {
- const Extensions::value_type& p = *itr;
- if(p.second == id) {
- return p.first;
- }
- }
- return "";
+ assert(_res);
+ return _res->getExtensionName(id);
}
void Peer::setExtension(const std::string& name, uint8_t id)
{
- _extensions[name] = id;
+ assert(_res);
+ _res->addExtension(name, id);
+}
+
+void Peer::setExtendedMessagingEnabled(bool enabled)
+{
+ assert(_res);
+ _res->extendedMessagingEnabled(enabled);
+}
+
+bool Peer::isExtendedMessagingEnabled() const
+{
+ assert(_res);
+ return _res->extendedMessagingEnabled();
+}
+
+void Peer::setDHTEnabled(bool enabled)
+{
+ assert(_res);
+ _res->dhtEnabled(enabled);
+}
+
+bool Peer::isDHTEnabled() const
+{
+ assert(_res);
+ return _res->dhtEnabled();
+}
+
+bool Peer::isSeeder() const
+{
+ return _seeder;
+}
+
+const Time& Peer::getFirstContactTime() const
+{
+ return _firstContactTime;
+}
+
+const Time& Peer::getBadConditionStartTime() const
+{
+ return _badConditionStartTime;
}
} // namespace aria2
diff --git a/src/Peer.h b/src/Peer.h
index 9f26f615..819c7f30 100644
--- a/src/Peer.h
+++ b/src/Peer.h
@@ -45,7 +45,7 @@
namespace aria2 {
-class BitfieldMan;
+class PeerSessionResource;
class Peer {
public:
@@ -54,36 +54,24 @@ public:
// If it is unknown, for example, localhost accepted the incoming connection
// from this peer, set port to 0.
uint16_t port;
- bool amChoking;
- bool amInterested;
- bool peerChoking;
- bool peerInterested;
- int32_t tryCount;
- int32_t cuid;
- bool chokingRequired;
- bool optUnchoking;
- bool snubbing;
private:
- unsigned char peerId[PEER_ID_LENGTH];
- BitfieldMan* _bitfield;
- bool fastExtensionEnabled;
- // fast index set which a peer has sent to localhost.
- std::deque<int32_t> peerAllowedIndexSet;
- // fast index set which localhost has sent to a peer.
- std::deque<int32_t> amAllowedIndexSet;
- bool _extendedMessagingEnabled;
- Extensions _extensions;
- bool _dhtEnabled;
- PeerStat peerStat;
- int64_t sessionUploadLength;
- int64_t sessionDownloadLength;
- int32_t latency;
- bool active;
std::string id;
+
+ int32_t _cuid;
+
+ unsigned char _peerId[PEER_ID_LENGTH];
+
Time _firstContactTime;
+
Time _badConditionStartTime;
+
bool _seeder;
+ PeerSessionResource* _res;
+
+ // Before calling updateSeeder(), make sure that
+ // allocateSessionResource() is called and _res is created.
+ // Otherwise assertion fails.
void updateSeeder();
public:
Peer(std::string ipaddr, uint16_t port);
@@ -96,6 +84,74 @@ public:
void resetStatus();
+ void usedBy(int32_t cuid);
+
+ int32_t usedBy() const;
+
+ bool unused() const;
+
+ // Returns true iff _res != 0.
+ bool isActive() const;
+
+ void setPeerId(const unsigned char* peerId);
+
+ const unsigned char* getPeerId() const;
+
+ bool isSeeder() const;
+
+ const std::string& getID() const;
+
+ void startBadCondition();
+
+ bool isGood() const;
+
+ void allocateSessionResource(int32_t pieceLength, int64_t totalLength);
+
+ void releaseSessionResource();
+
+ const Time& getFirstContactTime() const;
+
+ const Time& getBadConditionStartTime() const;
+
+ // Before calling following member functions, make sure that
+ // allocateSessionResource() is called and _res is created.
+ // Otherwise assertion fails.
+
+ // localhost is choking this peer
+ bool amChoking() const;
+
+ void amChoking(bool b) const;
+
+ // localhost is interested in this peer
+ bool amInterested() const;
+
+ void amInterested(bool b) const;
+
+ // this peer is choking localhost
+ bool peerChoking() const;
+
+ void peerChoking(bool b) const;
+
+ // this peer is interested in localhost
+ bool peerInterested() const;
+
+ void peerInterested(bool b);
+
+ // this peer should be choked
+ bool chokingRequired() const;
+
+ void chokingRequired(bool b);
+
+ // this peer is eligible for unchoking optionally.
+ bool optUnchoking() const;
+
+ void optUnchoking(bool b);
+
+ // this peer is snubbing.
+ bool snubbing() const;
+
+ void snubbing(bool b);
+
void updateUploadLength(int32_t bytes);
void updateDownloadLength(int32_t bytes);
@@ -123,16 +179,6 @@ public:
* Returns the number of bytes downloaded from the remote host.
*/
int64_t getSessionDownloadLength() const;
-
- void activate();
-
- void deactivate();
-
- bool isActive() const;
-
- void setPeerId(const unsigned char* peerId);
-
- const unsigned char* getPeerId() const;
void setBitfield(const unsigned char* bitfield, int32_t bitfieldLength);
@@ -153,73 +199,36 @@ public:
bool isFastExtensionEnabled() const;
void addPeerAllowedIndex(int32_t index);
+
bool isInPeerAllowedIndexSet(int32_t index) const;
- int32_t countPeerAllowedIndexSet() const;
+ size_t countPeerAllowedIndexSet() const;
const std::deque<int32_t>& getPeerAllowedIndexSet() const;
void addAmAllowedIndex(int32_t index);
+
bool isInAmAllowedIndexSet(int32_t index) const;
- void setExtendedMessagingEnabled(bool enabled)
- {
- _extendedMessagingEnabled = enabled;
- }
+ void setExtendedMessagingEnabled(bool enabled);
- bool isExtendedMessagingEnabled() const
- {
- return _extendedMessagingEnabled;
- }
+ bool isExtendedMessagingEnabled() const;
- void setDHTEnabled(bool enabled)
- {
- _dhtEnabled = enabled;
- }
+ void setDHTEnabled(bool enabled);
- bool isDHTEnabled() const
- {
- return _dhtEnabled;
- }
+ bool isDHTEnabled() const;
bool shouldBeChoking() const;
bool hasPiece(int32_t index) const;
- bool isSeeder() const
- {
- return _seeder;
- }
-
void updateLatency(int32_t latency);
- int32_t getLatency() const { return latency; }
-
- const std::string& getId() const {
- return id;
- }
-
- void startBadCondition();
-
- bool isGood() const;
-
- void allocateBitfield(int32_t pieceLength, int64_t totalLength);
-
- void deallocateBitfield();
-
- Time getFirstContactTime() const
- {
- return _firstContactTime;
- }
-
- Time getBadConditionStartTime() const
- {
- return _badConditionStartTime;
- }
+ int32_t getLatency() const;
- uint8_t getExtensionMessageID(const std::string& name);
+ uint8_t getExtensionMessageID(const std::string& name) const;
- std::string getExtensionName(uint8_t id);
+ std::string getExtensionName(uint8_t id) const;
void setExtension(const std::string& name, uint8_t id);
};
diff --git a/src/PeerAbstractCommand.cc b/src/PeerAbstractCommand.cc
index 42fb8e9c..6ec06fd2 100644
--- a/src/PeerAbstractCommand.cc
+++ b/src/PeerAbstractCommand.cc
@@ -74,7 +74,7 @@ PeerAbstractCommand::~PeerAbstractCommand() {
bool PeerAbstractCommand::execute() {
if(exitBeforeExecute()) {
- peer->resetStatus();
+ onAbort(0);
return true;
}
try {
diff --git a/src/PeerChokeCommand.cc b/src/PeerChokeCommand.cc
index 1edc827c..2575a43a 100644
--- a/src/PeerChokeCommand.cc
+++ b/src/PeerChokeCommand.cc
@@ -64,7 +64,7 @@ class ChokePeer {
public:
ChokePeer() {}
void operator()(PeerHandle& peer) {
- peer->chokingRequired = true;
+ peer->chokingRequired(true);
}
};
@@ -76,13 +76,13 @@ void PeerChokeCommand::optUnchokingPeer(Peers& peers) const {
int32_t optUnchokCount = 1;
for(Peers::iterator itr = peers.begin(); itr != peers.end(); itr++) {
Peers::value_type peer = *itr;
- if(optUnchokCount > 0 && !peer->snubbing) {
+ if(optUnchokCount > 0 && !peer->snubbing()) {
optUnchokCount--;
- peer->optUnchoking = true;
+ peer->optUnchoking(true);
logger->debug("opt, unchoking %s, download speed=%d",
peer->ipaddr.c_str(), peer->calculateDownloadSpeed());
} else {
- peer->optUnchoking = false;
+ peer->optUnchoking(false);
}
}
}
@@ -125,10 +125,10 @@ bool PeerChokeCommand::execute() {
int32_t unchokingCount = 4;//peers.size() >= 4 ? 4 : peers.size();
for(Peers::iterator itr = peers.begin(); itr != peers.end() && unchokingCount > 0; ) {
PeerHandle peer = *itr;
- if(peer->peerInterested && !peer->snubbing) {
+ if(peer->peerInterested() && !peer->snubbing()) {
unchokingCount--;
- peer->chokingRequired = false;
- peer->optUnchoking = false;
+ peer->chokingRequired(false);
+ peer->optUnchoking(false);
itr = peers.erase(itr);
if(pieceStorage->downloadFinished()) {
logger->debug("cat01, unchoking %s, upload speed=%d",
@@ -145,9 +145,9 @@ bool PeerChokeCommand::execute() {
}
for(Peers::iterator itr = peers.begin(); itr != peers.end(); ) {
PeerHandle peer = *itr;
- if(!peer->peerInterested && !peer->snubbing) {
- peer->chokingRequired = false;
- peer->optUnchoking = false;
+ if(!peer->peerInterested() && !peer->snubbing()) {
+ peer->chokingRequired(false);
+ peer->optUnchoking(false);
itr = peers.erase(itr);
if(pieceStorage->downloadFinished()) {
logger->debug("cat02, unchoking %s, upload speed=%d",
diff --git a/src/PeerInitiateConnectionCommand.cc b/src/PeerInitiateConnectionCommand.cc
index a2d0079d..500e9acb 100644
--- a/src/PeerInitiateConnectionCommand.cc
+++ b/src/PeerInitiateConnectionCommand.cc
@@ -91,9 +91,9 @@ bool PeerInitiateConnectionCommand::executeInternal() {
bool PeerInitiateConnectionCommand::prepareForNextPeer(int wait) {
if(peerStorage->isPeerAvailable() && btRuntime->lessThanEqMinPeer()) {
PeerHandle peer = peerStorage->getUnusedPeer();
- peer->cuid = CUIDCounterSingletonHolder::instance()->newID();
+ peer->usedBy(CUIDCounterSingletonHolder::instance()->newID());
PeerInitiateConnectionCommand* command =
- new PeerInitiateConnectionCommand(peer->cuid,
+ new PeerInitiateConnectionCommand(peer->usedBy(),
_requestGroup,
peer,
e,
diff --git a/src/PeerInteractionCommand.cc b/src/PeerInteractionCommand.cc
index 87e6bf52..4152b619 100644
--- a/src/PeerInteractionCommand.cc
+++ b/src/PeerInteractionCommand.cc
@@ -152,11 +152,10 @@ PeerInteractionCommand::PeerInteractionCommand(int32_t cuid,
peerObject->btRequestFactory = reqFactory;
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));
- peer->activate();
- peer->allocateBitfield(btContext->getPieceLength(), btContext->getTotalLength());
+ peer->allocateSessionResource(btContext->getPieceLength(), btContext->getTotalLength());
maxDownloadSpeedLimit = e->option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT);
@@ -164,9 +163,8 @@ PeerInteractionCommand::PeerInteractionCommand(int32_t cuid,
}
PeerInteractionCommand::~PeerInteractionCommand() {
- peer->deallocateBitfield();
- peer->deactivate();
- PEER_OBJECT_CLUSTER(btContext)->unregisterHandle(peer->getId());
+ peer->releaseSessionResource();
+ PEER_OBJECT_CLUSTER(btContext)->unregisterHandle(peer->getID());
btRuntime->decreaseConnections();
//logger->debug("CUID#%d - unregistered message factory using ID:%s",
@@ -217,8 +215,8 @@ bool PeerInteractionCommand::executeInternal() {
if(btInteractive->countReceivedMessageInIteration() > 0) {
updateKeepAlive();
}
- if(peer->amInterested && !peer->peerChoking ||
- peer->peerInterested && !peer->amChoking) {
+ if(peer->amInterested() && !peer->peerChoking() ||
+ peer->peerInterested() && !peer->amChoking()) {
if(maxDownloadSpeedLimit > 0) {
TransferStat stat = peerStorage->calculateStat();
if(maxDownloadSpeedLimit < stat.downloadSpeed) {
@@ -246,9 +244,9 @@ bool PeerInteractionCommand::executeInternal() {
bool PeerInteractionCommand::prepareForNextPeer(int32_t wait) {
if(peerStorage->isPeerAvailable() && btRuntime->lessThanEqMinPeer()) {
PeerHandle peer = peerStorage->getUnusedPeer();
- peer->cuid = CUIDCounterSingletonHolder::instance()->newID();
+ peer->usedBy(CUIDCounterSingletonHolder::instance()->newID());
PeerInitiateConnectionCommand* command =
- new PeerInitiateConnectionCommand(peer->cuid,
+ new PeerInitiateConnectionCommand(peer->usedBy(),
_requestGroup,
peer,
e,
diff --git a/src/PeerReceiveHandshakeCommand.cc b/src/PeerReceiveHandshakeCommand.cc
index 924a8cfb..7d55fa9f 100644
--- a/src/PeerReceiveHandshakeCommand.cc
+++ b/src/PeerReceiveHandshakeCommand.cc
@@ -88,7 +88,7 @@ bool PeerReceiveHandshakeCommand::executeInternal()
BT_RUNTIME(btContext)->getConnections() < MAX_PEERS) {
if(PEER_STORAGE(btContext)->addPeer(peer)) {
- peer->cuid = cuid;
+ peer->usedBy(cuid);
PeerInteractionCommand* command =
new PeerInteractionCommand(cuid,
@@ -100,7 +100,7 @@ bool PeerReceiveHandshakeCommand::executeInternal()
PeerInteractionCommand::RECEIVER_WAIT_HANDSHAKE,
_peerConnection);
e->commands.push_back(command);
- logger->debug(MSG_INCOMING_PEER_CONNECTION, cuid, peer->cuid);
+ logger->debug(MSG_INCOMING_PEER_CONNECTION, cuid, peer->usedBy());
}
}
return true;
diff --git a/src/PeerSessionResource.cc b/src/PeerSessionResource.cc
new file mode 100644
index 00000000..9bbdacca
--- /dev/null
+++ b/src/PeerSessionResource.cc
@@ -0,0 +1,316 @@
+/* <!-- 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 "PeerSessionResource.h"
+#include "BitfieldManFactory.h"
+#include "BitfieldMan.h"
+#include <algorithm>
+
+namespace aria2 {
+
+PeerSessionResource::PeerSessionResource(int32_t pieceLength, int64_t totalLength):
+ _amChoking(true),
+ _amInterested(false),
+ _peerChoking(true),
+ _peerInterested(false),
+ _chokingRequired(true),
+ _optUnchoking(false),
+ _snubbing(false),
+ _bitfieldMan(BitfieldManFactory::getFactoryInstance()->createBitfieldMan(pieceLength, totalLength)),
+ _fastExtensionEnabled(false),
+ _extendedMessagingEnabled(false),
+ _dhtEnabled(false),
+ _latency(DEFAULT_LATENCY),
+ _uploadLength(0),
+ _downloadLength(0)
+{}
+
+PeerSessionResource::~PeerSessionResource()
+{
+ delete _bitfieldMan;
+}
+
+bool PeerSessionResource::amChoking() const
+{
+ return _amChoking;
+}
+
+void PeerSessionResource::amChoking(bool b)
+{
+ _amChoking = b;
+}
+
+bool PeerSessionResource::amInterested() const
+{
+ return _amInterested;
+}
+
+void PeerSessionResource::amInterested(bool b)
+{
+ _amInterested = b;
+}
+
+bool PeerSessionResource::peerChoking() const
+{
+ return _peerChoking;
+}
+
+void PeerSessionResource::peerChoking(bool b)
+{
+ _peerChoking = b;
+}
+
+bool PeerSessionResource::peerInterested() const
+{
+ return _peerInterested;
+}
+
+void PeerSessionResource::peerInterested(bool b)
+{
+ _peerInterested = b;
+}
+
+bool PeerSessionResource::chokingRequired() const
+{
+ return _chokingRequired;
+}
+
+void PeerSessionResource::chokingRequired(bool b)
+{
+ _chokingRequired = b;
+}
+
+bool PeerSessionResource::optUnchoking() const
+{
+ return _optUnchoking;
+}
+
+void PeerSessionResource::optUnchoking(bool b)
+{
+ _optUnchoking = b;
+}
+
+bool PeerSessionResource::shouldBeChoking() const
+{
+ if(_optUnchoking) {
+ return false;
+ }
+ return _chokingRequired;
+}
+
+bool PeerSessionResource::snubbing() const
+{
+ return _snubbing;
+}
+
+void PeerSessionResource::snubbing(bool b)
+{
+ _snubbing = b;
+}
+
+bool PeerSessionResource::hasAllPieces() const
+{
+ return _bitfieldMan->isAllBitSet();
+}
+
+void PeerSessionResource::updateBitfield(int32_t index, int32_t operation)
+{
+ if(operation == 1) {
+ _bitfieldMan->setBit(index);
+ } else if(operation == 0) {
+ _bitfieldMan->unsetBit(index);
+ }
+}
+
+void PeerSessionResource::setBitfield(const unsigned char* bitfield, size_t bitfieldLength)
+{
+ _bitfieldMan->setBitfield(bitfield, bitfieldLength);
+}
+
+const unsigned char* PeerSessionResource::getBitfield() const
+{
+ return _bitfieldMan->getBitfield();
+}
+
+size_t PeerSessionResource::getBitfieldLength() const
+{
+ return _bitfieldMan->getBitfieldLength();
+}
+
+bool PeerSessionResource::hasPiece(int32_t index) const
+{
+ return _bitfieldMan->isBitSet(index);
+}
+
+void PeerSessionResource::markSeeder()
+{
+ _bitfieldMan->setAllBit();
+}
+
+bool PeerSessionResource::fastExtensionEnabled() const
+{
+ return _fastExtensionEnabled;
+}
+
+void PeerSessionResource::fastExtensionEnabled(bool b)
+{
+ _fastExtensionEnabled = b;
+}
+
+const std::deque<int32_t>& PeerSessionResource::peerAllowedIndexSet() const
+{
+ return _peerAllowedIndexSet;
+}
+
+template<typename T>
+bool PeerSessionResource::indexIncluded(const std::deque<T>& c, T index) const
+{
+ return std::find(c.begin(), c.end(), index) != c.end();
+}
+
+void PeerSessionResource::addPeerAllowedIndex(int32_t index)
+{
+ if(!indexIncluded(_peerAllowedIndexSet, index)) {
+ _peerAllowedIndexSet.push_back(index);
+ }
+}
+
+bool PeerSessionResource::peerAllowedIndexSetContains(int32_t index) const
+{
+ return indexIncluded(_peerAllowedIndexSet, index);
+}
+
+const std::deque<int32_t>& PeerSessionResource::amAllowedIndexSet() const
+{
+ return _amAllowedIndexSet;
+}
+
+void PeerSessionResource::addAmAllowedIndex(int32_t index)
+{
+ if(!indexIncluded(_amAllowedIndexSet, index)) {
+ _amAllowedIndexSet.push_back(index);
+ }
+}
+
+bool PeerSessionResource::amAllowedIndexSetContains(int32_t index) const
+{
+ return indexIncluded(_amAllowedIndexSet, index);
+}
+
+bool PeerSessionResource::extendedMessagingEnabled() const
+{
+ return _extendedMessagingEnabled;
+}
+
+void PeerSessionResource::extendedMessagingEnabled(bool b)
+{
+ _extendedMessagingEnabled = b;
+}
+
+uint8_t
+PeerSessionResource::getExtensionMessageID(const std::string& name) const
+{
+ Extensions::const_iterator itr = _extensions.find(name);
+ if(itr == _extensions.end()) {
+ return 0;
+ } else {
+ return (*itr).second;
+ }
+}
+
+std::string PeerSessionResource::getExtensionName(uint8_t id) const
+{
+ for(Extensions::const_iterator itr = _extensions.begin();
+ itr != _extensions.end(); ++itr) {
+ const Extensions::value_type& p = *itr;
+ if(p.second == id) {
+ return p.first;
+ }
+ }
+ return "";
+}
+
+void PeerSessionResource::addExtension(const std::string& name, uint8_t id)
+{
+ _extensions[name] = id;
+}
+
+bool PeerSessionResource::dhtEnabled() const
+{
+ return _dhtEnabled;
+}
+
+void PeerSessionResource::dhtEnabled(bool b)
+{
+ _dhtEnabled = b;
+}
+
+PeerStat& PeerSessionResource::getPeerStat()
+{
+ return _peerStat;
+}
+
+int32_t PeerSessionResource::latency() const
+{
+ return _latency;
+}
+
+void PeerSessionResource::updateLatency(int32_t latency)
+{
+ _latency = _latency*0.2+latency*0.8;
+}
+
+int64_t PeerSessionResource::uploadLength() const
+{
+ return _uploadLength;
+}
+
+void PeerSessionResource::updateUploadLength(int32_t bytes)
+{
+ _peerStat.updateUploadLength(bytes);
+ _uploadLength += bytes;
+}
+
+int64_t PeerSessionResource::downloadLength() const
+{
+ return _downloadLength;
+}
+
+void PeerSessionResource::updateDownloadLength(int32_t bytes)
+{
+ _peerStat.updateDownloadLength(bytes);
+ _downloadLength += bytes;
+}
+
+} // namespace aria2
diff --git a/src/PeerSessionResource.h b/src/PeerSessionResource.h
new file mode 100644
index 00000000..c4ede68e
--- /dev/null
+++ b/src/PeerSessionResource.h
@@ -0,0 +1,186 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2006 Tatsuhiro Tsujikawa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you
+ * do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_PEER_SESSION_RESOURCE_H_
+#define _D_PEER_SESSION_RESOURCE_H_
+
+#include "common.h"
+#include "BtConstants.h"
+#include "PeerStat.h"
+#include <string>
+#include <deque>
+
+namespace aria2 {
+
+class BitfieldMan;
+
+class PeerSessionResource {
+private:
+ // localhost is choking this peer
+ bool _amChoking;
+ // localhost is interested in this peer
+ bool _amInterested;
+ // this peer is choking localhost
+ bool _peerChoking;
+ // this peer is interested in localhost
+ bool _peerInterested;
+ // choking this peer is requested
+ bool _chokingRequired;
+ // this peer is eligible for *optional* unchoking.
+ bool _optUnchoking;
+ // this peer is snubbing.
+ bool _snubbing;
+
+ BitfieldMan* _bitfieldMan;
+ bool _fastExtensionEnabled;
+ // fast index set which a peer has sent to localhost.
+ std::deque<int32_t> _peerAllowedIndexSet;
+ // fast index set which localhost has sent to a peer.
+ std::deque<int32_t> _amAllowedIndexSet;
+ bool _extendedMessagingEnabled;
+ Extensions _extensions;
+ bool _dhtEnabled;
+ PeerStat _peerStat;
+ int32_t _latency;
+ int64_t _uploadLength;
+ int64_t _downloadLength;
+
+ template<typename T>
+ bool indexIncluded(const std::deque<T>& c, T index) const;
+public:
+ PeerSessionResource(int32_t pieceLength, int64_t totalLength);
+
+ ~PeerSessionResource();
+
+ // localhost is choking this peer
+ bool amChoking() const;
+
+ void amChoking(bool b);
+
+ // localhost is interested in this peer
+ bool amInterested() const;
+
+ void amInterested(bool b);
+
+ // this peer is choking localhost
+ bool peerChoking() const;
+
+ void peerChoking(bool b);
+
+ // this peer is interested in localhost
+ bool peerInterested() const;
+
+ void peerInterested(bool b);
+
+ // this peer should be choked
+ bool chokingRequired() const;
+
+ void chokingRequired(bool b);
+
+ // this peer is eligible for unchoking optionally.
+ bool optUnchoking() const;
+
+ void optUnchoking(bool b);
+
+ bool shouldBeChoking() const;
+
+ // this peer is snubbing.
+ bool snubbing() const;
+
+ void snubbing(bool b);
+
+ bool hasAllPieces() const;
+
+ void updateBitfield(int32_t index, int32_t operation);
+
+ void setBitfield(const unsigned char* bitfield, size_t bitfieldLength);
+
+ const unsigned char* getBitfield() const;
+
+ size_t getBitfieldLength() const;
+
+ bool hasPiece(int32_t index) const;
+
+ void markSeeder();
+
+ bool fastExtensionEnabled() const;
+
+ void fastExtensionEnabled(bool b);
+
+ // fast index set which a peer has sent to localhost.
+ const std::deque<int32_t>& peerAllowedIndexSet() const;
+
+ void addPeerAllowedIndex(int32_t index);
+
+ bool peerAllowedIndexSetContains(int32_t index) const;
+
+ // fast index set which localhost has sent to a peer.
+ const std::deque<int32_t>& amAllowedIndexSet() const;
+
+ void addAmAllowedIndex(int32_t index);
+
+ bool amAllowedIndexSetContains(int32_t index) const;
+
+ bool extendedMessagingEnabled() const;
+
+ void extendedMessagingEnabled(bool b);
+
+ uint8_t getExtensionMessageID(const std::string& name) const;
+
+ std::string getExtensionName(uint8_t id) const;
+
+ void addExtension(const std::string& name, uint8_t id);
+
+ bool dhtEnabled() const;
+
+ void dhtEnabled(bool b);
+
+ PeerStat& getPeerStat();
+
+ int32_t latency() const;
+
+ void updateLatency(int32_t l);
+
+ int64_t uploadLength() const;
+
+ void updateUploadLength(int32_t bytes);
+
+ int64_t downloadLength() const;
+
+ void updateDownloadLength(int32_t bytes);
+};
+
+} // namespace aria2
+
+#endif // _D_PEER_SESSION_RESOURCE_H_
diff --git a/src/PeerStat.h b/src/PeerStat.h
index 1a38b47e..7e830f91 100644
--- a/src/PeerStat.h
+++ b/src/PeerStat.h
@@ -137,7 +137,6 @@ public:
};
typedef SharedHandle<PeerStat> PeerStatHandle;
-typedef std::deque<PeerStatHandle> PeerStats;
} // namespace aria2
diff --git a/src/SegmentMan.cc b/src/SegmentMan.cc
index d6b1a62b..d8eddb0e 100644
--- a/src/SegmentMan.cc
+++ b/src/SegmentMan.cc
@@ -250,7 +250,7 @@ void SegmentMan::registerPeerStat(const PeerStatHandle& peerStat) {
PeerStatHandle SegmentMan::getPeerStat(int32_t cuid) const
{
- for(PeerStats::const_iterator itr = peerStats.begin(); itr != peerStats.end(); ++itr) {
+ for(std::deque<SharedHandle<PeerStat> >::const_iterator itr = peerStats.begin(); itr != peerStats.end(); ++itr) {
const PeerStatHandle& peerStat = *itr;
if(peerStat->getCuid() == cuid) {
return peerStat;
@@ -261,8 +261,7 @@ PeerStatHandle SegmentMan::getPeerStat(int32_t cuid) const
int32_t SegmentMan::calculateDownloadSpeed() const {
int32_t speed = 0;
- for(PeerStats::const_iterator itr = peerStats.begin();
- itr != peerStats.end(); itr++) {
+ for(std::deque<SharedHandle<PeerStat> >::const_iterator itr = peerStats.begin(); itr != peerStats.end(); itr++) {
const PeerStatHandle& peerStat = *itr;
if(peerStat->getStatus() == PeerStat::ACTIVE) {
speed += peerStat->calculateDownloadSpeed();
diff --git a/src/TrackerWatcherCommand.cc b/src/TrackerWatcherCommand.cc
index f6ff1e4d..af740333 100644
--- a/src/TrackerWatcherCommand.cc
+++ b/src/TrackerWatcherCommand.cc
@@ -147,15 +147,15 @@ void TrackerWatcherCommand::processTrackerResponse(const std::string& trackerRes
if(peer.isNull()) {
break;
}
- peer->cuid = CUIDCounterSingletonHolder::instance()->newID();
+ peer->usedBy(CUIDCounterSingletonHolder::instance()->newID());
PeerInitiateConnectionCommand* command =
- new PeerInitiateConnectionCommand(peer->cuid,
+ new PeerInitiateConnectionCommand(peer->usedBy(),
_requestGroup,
peer,
e,
btContext);
e->commands.push_back(command);
- logger->debug("CUID#%d - Adding new command CUID#%d", cuid, peer->cuid);
+ logger->debug("CUID#%d - Adding new command CUID#%d", cuid, peer->usedBy());
}
}