diff --git a/ChangeLog b/ChangeLog index f92c01fc..bc108e31 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,59 @@ +2008-02-10 Tatsuhiro Tsujikawa + + 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 + 2008-02-09 Tatsuhiro Tsujikawa Added std namespace to copy, remove_copy, back_inserter, 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) {} + +void DefaultPeerStorage::onReturningPeer(const SharedHandle& 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); + + void onReturningPeer(const SharedHandle& 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 #include -#include 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; +} + +int32_t Peer::usedBy() const +{ + return _cuid; +} + +bool Peer::unused() const +{ + return _cuid == 0; +} + +void Peer::allocateSessionResource(int32_t pieceLength, int64_t totalLength) +{ + delete _res; + _res = new PeerSessionResource(pieceLength, totalLength); + _res->getPeerStat().downloadStart(); +} + +void Peer::releaseSessionResource() +{ + delete _res; + _res = 0; +} + +bool Peer::isActive() const +{ + return _res != 0; +} + +void Peer::setPeerId(const unsigned char* peerId) +{ + memcpy(_peerId, peerId, PEER_ID_LENGTH); +} + +const unsigned char* Peer::getPeerId() const +{ + return _peerId; +} + +void Peer::resetStatus() { + _cuid = 0; +} + +bool Peer::amChoking() const +{ + assert(_res); + return _res->amChoking(); +} + +void Peer::amChoking(bool b) const +{ + assert(_res); + _res->amChoking(b); +} + +// localhost is interested in this peer +bool Peer::amInterested() const +{ + assert(_res); + return _res->amInterested(); +} + +void Peer::amInterested(bool b) const +{ + assert(_res); + _res->amInterested(b); +} + +// this peer is choking localhost +bool Peer::peerChoking() const +{ + assert(_res); + return _res->peerChoking(); +} + +void Peer::peerChoking(bool b) const +{ + assert(_res); + _res->peerChoking(b); +} + +// this peer is interested in localhost +bool Peer::peerInterested() const +{ + assert(_res); + return _res->peerInterested(); +} + +void Peer::peerInterested(bool b) +{ + 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) { - peerStat.updateUploadLength(bytes); - sessionUploadLength += bytes; + assert(_res); + _res->updateUploadLength(bytes); } void Peer::updateDownloadLength(int32_t bytes) { - peerStat.updateDownloadLength(bytes); - sessionDownloadLength += bytes; + assert(_res); + _res->updateDownloadLength(bytes); } void Peer::updateSeeder() { - assert(_bitfield); - if(_bitfield->isAllBitSet()) { + assert(_res); + if(_res->hasAllPieces()) { _seeder = true; } } void Peer::updateBitfield(int32_t index, int32_t operation) { - assert(_bitfield); - if(operation == 1) { - _bitfield->setBit(index); - } else if(operation == 0) { - _bitfield->unsetBit(index); - } + assert(_res); + _res->updateBitfield(index, operation); updateSeeder(); } int32_t Peer::calculateUploadSpeed() { - return peerStat.calculateUploadSpeed(); + assert(_res); + return _res->getPeerStat().calculateUploadSpeed(); } int32_t Peer::calculateUploadSpeed(const struct timeval& now) { - return peerStat.calculateUploadSpeed(now); + assert(_res); + return _res->getPeerStat().calculateUploadSpeed(now); } int32_t Peer::calculateDownloadSpeed() { - return peerStat.calculateDownloadSpeed(); + assert(_res); + return _res->getPeerStat().calculateDownloadSpeed(); } int32_t Peer::calculateDownloadSpeed(const struct timeval& now) { - return peerStat.calculateDownloadSpeed(now); + assert(_res); + return _res->getPeerStat().calculateDownloadSpeed(now); } int64_t Peer::getSessionUploadLength() const { - return sessionUploadLength; + assert(_res); + return _res->uploadLength(); } int64_t Peer::getSessionDownloadLength() const { - return sessionDownloadLength; + assert(_res); + return _res->downloadLength(); } -void Peer::activate() -{ - peerStat.downloadStart(); - active = true; -} - -void Peer::deactivate() -{ - peerStat.downloadStop(); - active = false; -} - -bool Peer::isActive() const -{ - return active; -} - -void Peer::setPeerId(const unsigned char* peerId) -{ - memcpy(this->peerId, peerId, PEER_ID_LENGTH); -} - -const unsigned char* Peer::getPeerId() const -{ - return this->peerId; -} - 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& 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 peerAllowedIndexSet; - // fast index set which localhost has sent to a peer. - std::deque 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& 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; } + int32_t getLatency() const; - const std::string& getId() const { - return id; - } + uint8_t getExtensionMessageID(const std::string& name) const; - 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; - } - - uint8_t getExtensionMessageID(const std::string& name); - - 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 @@ +/* */ +#include "PeerSessionResource.h" +#include "BitfieldManFactory.h" +#include "BitfieldMan.h" +#include + +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& PeerSessionResource::peerAllowedIndexSet() const +{ + return _peerAllowedIndexSet; +} + +template +bool PeerSessionResource::indexIncluded(const std::deque& 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& 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 @@ +/* */ +#ifndef _D_PEER_SESSION_RESOURCE_H_ +#define _D_PEER_SESSION_RESOURCE_H_ + +#include "common.h" +#include "BtConstants.h" +#include "PeerStat.h" +#include +#include + +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 _peerAllowedIndexSet; + // fast index set which localhost has sent to a peer. + std::deque _amAllowedIndexSet; + bool _extendedMessagingEnabled; + Extensions _extensions; + bool _dhtEnabled; + PeerStat _peerStat; + int32_t _latency; + int64_t _uploadLength; + int64_t _downloadLength; + + template + bool indexIncluded(const std::deque& 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& 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& 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 PeerStatHandle; -typedef std::deque 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 >::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 >::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()); } } diff --git a/test/BtAllowedFastMessageTest.cc b/test/BtAllowedFastMessageTest.cc index 605e7951..7c50060a 100644 --- a/test/BtAllowedFastMessageTest.cc +++ b/test/BtAllowedFastMessageTest.cc @@ -71,6 +71,7 @@ void BtAllowedFastMessageTest::testDoReceivedAction() { BtAllowedFastMessage msg; msg.setIndex(1); SharedHandle peer = new Peer("localhost", 6969); + peer->allocateSessionResource(1024, 1024*1024); peer->setFastExtensionEnabled(true); msg.setPeer(peer); CPPUNIT_ASSERT(!peer->isInPeerAllowedIndexSet(1)); @@ -88,6 +89,7 @@ void BtAllowedFastMessageTest::testOnSendComplete() { BtAllowedFastMessage msg; msg.setIndex(1); SharedHandle peer = new Peer("localhost", 6969); + peer->allocateSessionResource(1024, 1024*1024); peer->setFastExtensionEnabled(true); msg.setPeer(peer); CPPUNIT_ASSERT(!peer->isInAmAllowedIndexSet(1)); diff --git a/test/BtBitfieldMessageTest.cc b/test/BtBitfieldMessageTest.cc index 50bc23d2..c50e09de 100644 --- a/test/BtBitfieldMessageTest.cc +++ b/test/BtBitfieldMessageTest.cc @@ -71,7 +71,7 @@ void BtBitfieldMessageTest::testGetMessage() { void BtBitfieldMessageTest::testDoReceivedAction() { SharedHandle peer = new Peer("host1", 6969); - peer->allocateBitfield(16*1024, 16*16*1024); + peer->allocateSessionResource(16*1024, 16*16*1024); BtBitfieldMessage msg; msg.setPeer(peer); unsigned char bitfield[] = { 0xff, 0xff }; diff --git a/test/BtCancelMessageTest.cc b/test/BtCancelMessageTest.cc index d9a06e7b..4af683ce 100644 --- a/test/BtCancelMessageTest.cc +++ b/test/BtCancelMessageTest.cc @@ -39,7 +39,7 @@ public: btContext->setInfoHash((const unsigned char*)"12345678901234567890"); BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(), new PeerObjectCluster()); - PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getId(), new PeerObject()); + PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), new PeerObject()); } void testCreate(); diff --git a/test/BtChokeMessageTest.cc b/test/BtChokeMessageTest.cc index 353542b5..f77327c4 100644 --- a/test/BtChokeMessageTest.cc +++ b/test/BtChokeMessageTest.cc @@ -36,11 +36,12 @@ public: void setUp() { BtRegistry::unregisterAll(); peer = new Peer("host", 6969); + peer->allocateSessionResource(1024, 1024*1024); btContext = new MockBtContext(); btContext->setInfoHash((const unsigned char*)"12345678901234567890"); BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(), new PeerObjectCluster()); - PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getId(), new PeerObject()); + PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), new PeerObject()); } void testCreate(); @@ -124,7 +125,7 @@ void BtChokeMessageTest::testDoReceivedAction() { msg.doReceivedAction(); CPPUNIT_ASSERT(dispatcher->doChokedActionCalled); - CPPUNIT_ASSERT(peer->peerChoking); + CPPUNIT_ASSERT(peer->peerChoking()); } void BtChokeMessageTest::testOnSendComplete() { @@ -138,7 +139,7 @@ void BtChokeMessageTest::testOnSendComplete() { msg.onSendComplete(); CPPUNIT_ASSERT(dispatcher->doChokingActionCalled); - CPPUNIT_ASSERT(peer->amChoking); + CPPUNIT_ASSERT(peer->amChoking()); } void BtChokeMessageTest::testToString() { diff --git a/test/BtExtendedMessageTest.cc b/test/BtExtendedMessageTest.cc index 9eeedfca..0401e768 100644 --- a/test/BtExtendedMessageTest.cc +++ b/test/BtExtendedMessageTest.cc @@ -49,7 +49,7 @@ CPPUNIT_TEST_SUITE_REGISTRATION(BtExtendedMessageTest); void BtExtendedMessageTest::testCreate() { SharedHandle peer = new Peer("192.168.0.1", 6969); - peer->setExtension("charlie", 1); + peer->allocateSessionResource(1024, 1024*1024); SharedHandle ctx = new MockBtContext(); unsigned char infohash[20]; memset(infohash, 0, sizeof(infohash)); @@ -61,7 +61,7 @@ void BtExtendedMessageTest::testCreate() { SharedHandle peerObject = new PeerObject(); peerObject->extensionMessageFactory = exmsgFactory; - PEER_OBJECT_CLUSTER(ctx)->registerHandle(peer->getId(), peerObject); + PEER_OBJECT_CLUSTER(ctx)->registerHandle(peer->getID(), peerObject); // payload:{4:name3:foo}->11bytes std::string payload = "4:name3:foo"; diff --git a/test/BtHaveAllMessageTest.cc b/test/BtHaveAllMessageTest.cc index a5c28781..b8d8945f 100644 --- a/test/BtHaveAllMessageTest.cc +++ b/test/BtHaveAllMessageTest.cc @@ -61,7 +61,7 @@ void BtHaveAllMessageTest::testGetMessage() { void BtHaveAllMessageTest::testDoReceivedAction() { BtHaveAllMessage msg; SharedHandle peer = new Peer("host", 6969); - peer->allocateBitfield(16*1024, 256*1024); + peer->allocateSessionResource(16*1024, 256*1024); peer->setFastExtensionEnabled(true); msg.setPeer(peer); diff --git a/test/BtHaveMessageTest.cc b/test/BtHaveMessageTest.cc index 4ffa8274..abf59286 100644 --- a/test/BtHaveMessageTest.cc +++ b/test/BtHaveMessageTest.cc @@ -66,7 +66,7 @@ void BtHaveMessageTest::testGetMessage() { void BtHaveMessageTest::testDoReceivedAction() { SharedHandle peer = new Peer("host", 6969); - peer->allocateBitfield(16*1024, 256*1024); + peer->allocateSessionResource(16*1024, 256*1024); BtHaveMessage msg; msg.setIndex(1); msg.setPeer(peer); diff --git a/test/BtHaveNoneMessageTest.cc b/test/BtHaveNoneMessageTest.cc index 8bad7343..6d9df006 100644 --- a/test/BtHaveNoneMessageTest.cc +++ b/test/BtHaveNoneMessageTest.cc @@ -63,6 +63,7 @@ void BtHaveNoneMessageTest::testGetMessage() { void BtHaveNoneMessageTest::testDoReceivedAction() { BtHaveNoneMessage msg; SharedHandle peer = new Peer("host", 6969); + peer->allocateSessionResource(1024, 1024*1024); peer->setFastExtensionEnabled(true); msg.setPeer(peer); msg.doReceivedAction(); diff --git a/test/BtInterestedMessageTest.cc b/test/BtInterestedMessageTest.cc index 091f51dc..77ba2ff6 100644 --- a/test/BtInterestedMessageTest.cc +++ b/test/BtInterestedMessageTest.cc @@ -65,19 +65,21 @@ void BtInterestedMessageTest::testGetMessage() { void BtInterestedMessageTest::testDoReceivedAction() { BtInterestedMessage msg; SharedHandle peer = new Peer("host", 6969); + peer->allocateSessionResource(1024, 1024*1024); msg.setPeer(peer); - CPPUNIT_ASSERT(!peer->peerInterested); + CPPUNIT_ASSERT(!peer->peerInterested()); msg.doReceivedAction(); - CPPUNIT_ASSERT(peer->peerInterested); + CPPUNIT_ASSERT(peer->peerInterested()); } void BtInterestedMessageTest::testOnSendComplete() { BtInterestedMessage msg; SharedHandle peer = new Peer("host", 6969); + peer->allocateSessionResource(1024, 1024*1024); msg.setPeer(peer); - CPPUNIT_ASSERT(!peer->amInterested); + CPPUNIT_ASSERT(!peer->amInterested()); msg.onSendComplete(); - CPPUNIT_ASSERT(peer->amInterested); + CPPUNIT_ASSERT(peer->amInterested()); } void BtInterestedMessageTest::testToString() { diff --git a/test/BtNotInterestedMessageTest.cc b/test/BtNotInterestedMessageTest.cc index 41a9f275..476a836a 100644 --- a/test/BtNotInterestedMessageTest.cc +++ b/test/BtNotInterestedMessageTest.cc @@ -64,22 +64,24 @@ void BtNotInterestedMessageTest::testGetMessage() { void BtNotInterestedMessageTest::testDoReceivedAction() { SharedHandle peer = new Peer("host", 6969); - peer->peerInterested = true; + peer->allocateSessionResource(1024, 1024*1024); + peer->peerInterested(true); BtNotInterestedMessage msg; msg.setPeer(peer); - CPPUNIT_ASSERT(peer->peerInterested); + CPPUNIT_ASSERT(peer->peerInterested()); msg.doReceivedAction(); - CPPUNIT_ASSERT(!peer->peerInterested); + CPPUNIT_ASSERT(!peer->peerInterested()); } void BtNotInterestedMessageTest::testOnSendComplete() { SharedHandle peer = new Peer("host", 6969); - peer->amInterested = true; + peer->allocateSessionResource(1024, 1024*1024); + peer->amInterested(true); BtNotInterestedMessage msg; msg.setPeer(peer); - CPPUNIT_ASSERT(peer->amInterested); + CPPUNIT_ASSERT(peer->amInterested()); msg.onSendComplete(); - CPPUNIT_ASSERT(!peer->amInterested); + CPPUNIT_ASSERT(!peer->amInterested()); } void BtNotInterestedMessageTest::testToString() { diff --git a/test/BtPieceMessageTest.cc b/test/BtPieceMessageTest.cc index 7683a6d1..19cc95f7 100644 --- a/test/BtPieceMessageTest.cc +++ b/test/BtPieceMessageTest.cc @@ -88,9 +88,11 @@ public: btContext->setTotalLength(256*1024); peer = new Peer("host", 6969); + peer->allocateSessionResource(btContext->getPieceLength(), + btContext->getTotalLength()); BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(), new PeerObjectCluster()); - PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getId(), new PeerObject()); + PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), new PeerObject()); btMessageDispatcher = new MockBtMessageDispatcher(); PEER_OBJECT(btContext, peer)->btMessageDispatcher = btMessageDispatcher; PEER_OBJECT(btContext, peer)->btMessageFactory = new MockBtMessageFactory2(); diff --git a/test/BtRejectMessageTest.cc b/test/BtRejectMessageTest.cc index 2119acf2..3242da2d 100644 --- a/test/BtRejectMessageTest.cc +++ b/test/BtRejectMessageTest.cc @@ -76,12 +76,13 @@ public: void setUp() { BtRegistry::unregisterAll(); peer = new Peer("host", 6969); + peer->allocateSessionResource(1024, 1024*1024); SharedHandle btContext = new MockBtContext(); btContext->setInfoHash((const unsigned char*)"12345678901234567890"); BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(), new PeerObjectCluster()); - PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getId(), new PeerObject()); + PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), new PeerObject()); dispatcher = new MockBtMessageDispatcher2(); PEER_OBJECT(btContext, peer)->btMessageDispatcher = dispatcher; diff --git a/test/BtRequestMessageTest.cc b/test/BtRequestMessageTest.cc index 33bc3f10..349f42cc 100644 --- a/test/BtRequestMessageTest.cc +++ b/test/BtRequestMessageTest.cc @@ -108,10 +108,11 @@ public: pieceStorage); peer = new Peer("host", 6969); - + peer->allocateSessionResource(btContext->getPieceLength(), + btContext->getTotalLength()); BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(), new PeerObjectCluster()); - PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getId(), new PeerObject()); + PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), new PeerObject()); dispatcher = new MockBtMessageDispatcher(); @@ -177,7 +178,7 @@ void BtRequestMessageTest::testGetMessage() { } void BtRequestMessageTest::testDoReceivedAction_hasPieceAndAmNotChoking() { - peer->amChoking = false; + peer->amChoking(false); msg->doReceivedAction(); CPPUNIT_ASSERT_EQUAL((size_t)1, dispatcher->messageQueue.size()); @@ -189,7 +190,7 @@ void BtRequestMessageTest::testDoReceivedAction_hasPieceAndAmNotChoking() { } void BtRequestMessageTest::testDoReceivedAction_hasPieceAndAmChokingAndFastExtensionEnabled() { - peer->amChoking = true; + peer->amChoking(true); peer->setFastExtensionEnabled(true); msg->doReceivedAction(); @@ -202,7 +203,7 @@ void BtRequestMessageTest::testDoReceivedAction_hasPieceAndAmChokingAndFastExten } void BtRequestMessageTest::testDoReceivedAction_hasPieceAndAmChokingAndFastExtensionDisabled() { - peer->amChoking = true; + peer->amChoking(true); msg->doReceivedAction(); CPPUNIT_ASSERT_EQUAL((size_t)0, dispatcher->messageQueue.size()); @@ -210,7 +211,7 @@ void BtRequestMessageTest::testDoReceivedAction_hasPieceAndAmChokingAndFastExten void BtRequestMessageTest::testDoReceivedAction_doesntHavePieceAndFastExtensionEnabled() { msg->setIndex(2); - peer->amChoking = false; + peer->amChoking(false); peer->setFastExtensionEnabled(true); msg->doReceivedAction(); @@ -224,7 +225,7 @@ void BtRequestMessageTest::testDoReceivedAction_doesntHavePieceAndFastExtensionE void BtRequestMessageTest::testDoReceivedAction_doesntHavePieceAndFastExtensionDisabled() { msg->setIndex(2); - peer->amChoking = false; + peer->amChoking(false); msg->doReceivedAction(); CPPUNIT_ASSERT_EQUAL((size_t)0, dispatcher->messageQueue.size()); diff --git a/test/BtUnchokeMessageTest.cc b/test/BtUnchokeMessageTest.cc index 29cac162..9931909c 100644 --- a/test/BtUnchokeMessageTest.cc +++ b/test/BtUnchokeMessageTest.cc @@ -63,24 +63,26 @@ void BtUnchokeMessageTest::testGetMessage() { void BtUnchokeMessageTest::testDoReceivedAction() { SharedHandle peer = new Peer("host", 6969); - peer->peerChoking = true; + peer->allocateSessionResource(1024, 1024*1024); + peer->peerChoking(true); BtUnchokeMessage msg; msg.setPeer(peer); - CPPUNIT_ASSERT(peer->peerChoking); + CPPUNIT_ASSERT(peer->peerChoking()); msg.doReceivedAction(); - CPPUNIT_ASSERT(!peer->peerChoking); + CPPUNIT_ASSERT(!peer->peerChoking()); } void BtUnchokeMessageTest::testOnSendComplete() { SharedHandle peer = new Peer("host", 6969); - peer->amChoking = true; + peer->allocateSessionResource(1024, 1024*1024); + peer->amChoking(true); BtUnchokeMessage msg; msg.setPeer(peer); - CPPUNIT_ASSERT(peer->amChoking); + CPPUNIT_ASSERT(peer->amChoking()); msg.onSendComplete(); - CPPUNIT_ASSERT(!peer->amChoking); + CPPUNIT_ASSERT(!peer->amChoking()); } void BtUnchokeMessageTest::testToString() { diff --git a/test/DefaultBtMessageDispatcherTest.cc b/test/DefaultBtMessageDispatcherTest.cc index 253b4681..707ee718 100644 --- a/test/DefaultBtMessageDispatcherTest.cc +++ b/test/DefaultBtMessageDispatcherTest.cc @@ -134,6 +134,8 @@ public: btContext = new DefaultBtContext(); btContext->load("test.torrent"); peer = new Peer("192.168.0.1", 6969); + peer->allocateSessionResource(btContext->getPieceLength(), + btContext->getTotalLength()); peerStorage = new MockPeerStorage(); pieceStorage = new MockPieceStorage(); BtRegistry::unregisterAll(); @@ -147,7 +149,7 @@ public: SharedHandle peerObject = new PeerObject(); peerObject->btMessageFactory = new MockBtMessageFactory2(); - PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getId(), peerObject); + PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), peerObject); btMessageDispatcher = new DefaultBtMessageDispatcher(); btMessageDispatcher->setCuid(1); @@ -328,7 +330,7 @@ void DefaultBtMessageDispatcherTest::testCheckRequestSlotAndDoNecessaryThing_tim CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->getMessageQueue().size()); CPPUNIT_ASSERT_EQUAL((size_t)0, btMessageDispatcher->getRequestSlots().size()); CPPUNIT_ASSERT_EQUAL(false, piece->isBlockUsed(0)); - CPPUNIT_ASSERT_EQUAL(true, peer->snubbing); + CPPUNIT_ASSERT_EQUAL(true, peer->snubbing()); } void DefaultBtMessageDispatcherTest::testCheckRequestSlotAndDoNecessaryThing_completeBlock() { diff --git a/test/DefaultBtMessageFactoryTest.cc b/test/DefaultBtMessageFactoryTest.cc index 3268b400..fb08ea6b 100644 --- a/test/DefaultBtMessageFactoryTest.cc +++ b/test/DefaultBtMessageFactoryTest.cc @@ -40,6 +40,7 @@ public: _btContext = btContext; _peer = new Peer("192.168.0.1", 6969); + _peer->allocateSessionResource(1024, 1024*1024); _peer->setExtendedMessagingEnabled(true); SharedHandle exmsgFactory = @@ -49,7 +50,7 @@ public: SharedHandle peerObject = new PeerObject(); peerObject->extensionMessageFactory = exmsgFactory; - PEER_OBJECT_CLUSTER(_btContext)->registerHandle(_peer->getId(), peerObject); + PEER_OBJECT_CLUSTER(_btContext)->registerHandle(_peer->getID(), peerObject); } void tearDown() diff --git a/test/DefaultBtRequestFactoryTest.cc b/test/DefaultBtRequestFactoryTest.cc index c53b5dec..b537d8ef 100644 --- a/test/DefaultBtRequestFactoryTest.cc +++ b/test/DefaultBtRequestFactoryTest.cc @@ -97,7 +97,7 @@ public: new PeerObjectCluster()); SharedHandle peerObject = new PeerObject(); peerObject->btMessageFactory = new MockBtMessageFactory2(); - PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getId(), peerObject); + PEER_OBJECT_CLUSTER(btContext)->registerHandle(peer->getID(), peerObject); btRequestFactory = new DefaultBtRequestFactory(); btRequestFactory->setBtContext(btContext); diff --git a/test/DefaultExtensionMessageFactoryTest.cc b/test/DefaultExtensionMessageFactoryTest.cc index e9d310fc..bd1ec75e 100644 --- a/test/DefaultExtensionMessageFactoryTest.cc +++ b/test/DefaultExtensionMessageFactoryTest.cc @@ -39,6 +39,7 @@ public: btRuntime); _peer = new Peer("192.168.0.1", 6969); + _peer->allocateSessionResource(1024, 1024*1024); _peer->setExtension("ut_pex", 1); } diff --git a/test/DefaultPeerStorageTest.cc b/test/DefaultPeerStorageTest.cc index b2874d8c..d1c5209e 100644 --- a/test/DefaultPeerStorageTest.cc +++ b/test/DefaultPeerStorageTest.cc @@ -84,7 +84,7 @@ void DefaultPeerStorageTest::testDeleteUnusedPeer() { ps.addPeer(peer1); ps.addPeer(peer2); - peer2->cuid = 1; + peer2->usedBy(1); ps.deleteUnusedPeer(3); @@ -117,7 +117,7 @@ void DefaultPeerStorageTest::testAddPeer() { SharedHandle peer4(new Peer("192.168.0.4", 6889)); - peer1->cuid = 1; + peer1->usedBy(1); CPPUNIT_ASSERT(ps.addPeer(peer4)); // peer2 was deleted. While peer1 is oldest, its cuid is not 0. CPPUNIT_ASSERT_EQUAL((int32_t)3, ps.countPeer()); @@ -142,7 +142,7 @@ void DefaultPeerStorageTest::testGetUnusedPeer() { CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), ps.getUnusedPeer()->ipaddr); - peer1->cuid = 1; + peer1->usedBy(1); CPPUNIT_ASSERT(ps.getUnusedPeer().isNull()); @@ -164,7 +164,7 @@ void DefaultPeerStorageTest::testIsPeerAvailable() { CPPUNIT_ASSERT_EQUAL(true, ps.isPeerAvailable()); - peer1->cuid = 1; + peer1->usedBy(1); CPPUNIT_ASSERT_EQUAL(false, ps.isPeerAvailable()); @@ -188,7 +188,8 @@ void DefaultPeerStorageTest::testActivatePeer() { CPPUNIT_ASSERT_EQUAL((size_t)0, ps.getActivePeers().size()); - peer1->activate(); + peer1->allocateSessionResource(btContext->getPieceLength(), + btContext->getTotalLength()); CPPUNIT_ASSERT_EQUAL((size_t)1, ps.getActivePeers().size()); } @@ -201,7 +202,11 @@ void DefaultPeerStorageTest::testReturnPeer() DefaultPeerStorage ps(btContext, option); SharedHandle peer1(new Peer("192.168.0.1", 0)); + peer1->allocateSessionResource(btContext->getPieceLength(), + btContext->getTotalLength()); SharedHandle peer2(new Peer("192.168.0.2", 6889)); + peer2->allocateSessionResource(btContext->getPieceLength(), + btContext->getTotalLength()); SharedHandle peer3(new Peer("192.168.0.1", 6889)); ps.addPeer(peer1); ps.addPeer(peer2); diff --git a/test/DefaultPieceStorageTest.cc b/test/DefaultPieceStorageTest.cc index f953ea91..a220933f 100644 --- a/test/DefaultPieceStorageTest.cc +++ b/test/DefaultPieceStorageTest.cc @@ -43,8 +43,8 @@ public: btContext = new DefaultBtContext(); btContext->load("test.torrent"); peer = new Peer("192.168.0.1", 6889); - peer->allocateBitfield(btContext->getPieceLength(), - btContext->getTotalLength()); + peer->allocateSessionResource(btContext->getPieceLength(), + btContext->getTotalLength()); option = new Option(); } diff --git a/test/HandshakeExtensionMessageTest.cc b/test/HandshakeExtensionMessageTest.cc index b68c4e5d..1a1e6057 100644 --- a/test/HandshakeExtensionMessageTest.cc +++ b/test/HandshakeExtensionMessageTest.cc @@ -74,6 +74,7 @@ void HandshakeExtensionMessageTest::testToString() void HandshakeExtensionMessageTest::testDoReceivedAction() { SharedHandle peer = new Peer("192.168.0.1", 0); + peer->allocateSessionResource(1024, 1024*1024); HandshakeExtensionMessage msg; msg.setClientVersion("aria2"); msg.setTCPPort(6889); diff --git a/test/Makefile.am b/test/Makefile.am index afab249f..dda77038 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -91,6 +91,7 @@ aria2c_SOURCES += BtAllowedFastMessageTest.cc\ MetaFileUtilTest.cc\ ByteArrayDiskWriterTest.cc\ PeerTest.cc\ + PeerSessionResourceTest.cc\ PeerMessageUtilTest.cc\ ShareRatioSeedCriteriaTest.cc\ BtRegistryTest.cc\ diff --git a/test/Makefile.in b/test/Makefile.in index 192c346e..f5d4e185 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -76,6 +76,7 @@ check_PROGRAMS = $(am__EXEEXT_1) @ENABLE_BITTORRENT_TRUE@ MetaFileUtilTest.cc\ @ENABLE_BITTORRENT_TRUE@ ByteArrayDiskWriterTest.cc\ @ENABLE_BITTORRENT_TRUE@ PeerTest.cc\ +@ENABLE_BITTORRENT_TRUE@ PeerSessionResourceTest.cc\ @ENABLE_BITTORRENT_TRUE@ PeerMessageUtilTest.cc\ @ENABLE_BITTORRENT_TRUE@ ShareRatioSeedCriteriaTest.cc\ @ENABLE_BITTORRENT_TRUE@ BtRegistryTest.cc\ @@ -186,7 +187,8 @@ am__aria2c_SOURCES_DIST = AllTest.cc SocketCoreTest.cc \ DefaultPeerListProcessorTest.cc AnnounceListTest.cc \ DefaultPeerStorageTest.cc MockPeerStorage.h DataTest.cc \ DictionaryTest.cc ListTest.cc MetaFileUtilTest.cc \ - ByteArrayDiskWriterTest.cc PeerTest.cc PeerMessageUtilTest.cc \ + ByteArrayDiskWriterTest.cc PeerTest.cc \ + PeerSessionResourceTest.cc PeerMessageUtilTest.cc \ ShareRatioSeedCriteriaTest.cc BtRegistryTest.cc \ BtDependencyTest.cc BtPostDownloadHandlerTest.cc \ DownloadHandlerFactoryTest.cc TimeSeedCriteriaTest.cc \ @@ -245,6 +247,7 @@ am__aria2c_SOURCES_DIST = AllTest.cc SocketCoreTest.cc \ @ENABLE_BITTORRENT_TRUE@ MetaFileUtilTest.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ ByteArrayDiskWriterTest.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ PeerTest.$(OBJEXT) \ +@ENABLE_BITTORRENT_TRUE@ PeerSessionResourceTest.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ PeerMessageUtilTest.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ ShareRatioSeedCriteriaTest.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ BtRegistryTest.$(OBJEXT) \ @@ -718,6 +721,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PStringBuildVisitorTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ParameterizedStringParserTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerMessageUtilTest.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerSessionResourceTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PeerTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/PieceTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/RequestGroupManTest.Po@am__quote@ diff --git a/test/PeerSessionResourceTest.cc b/test/PeerSessionResourceTest.cc new file mode 100644 index 00000000..15a33b32 --- /dev/null +++ b/test/PeerSessionResourceTest.cc @@ -0,0 +1,254 @@ +#include "PeerSessionResource.h" +#include "Exception.h" +#include "Util.h" +#include + +namespace aria2 { + +class PeerSessionResourceTest:public CppUnit::TestFixture { + + CPPUNIT_TEST_SUITE(PeerSessionResourceTest); + CPPUNIT_TEST(testPeerAllowedIndexSetContains); + CPPUNIT_TEST(testAmAllowedIndexSetContains); + CPPUNIT_TEST(testHasAllPieces); + CPPUNIT_TEST(testHasPiece); + CPPUNIT_TEST(testUpdateUploadLength); + CPPUNIT_TEST(testUpdateDownloadLength); + CPPUNIT_TEST(testUpdateLatency); + CPPUNIT_TEST(testExtendedMessageEnabled); + CPPUNIT_TEST(testGetExtensionMessageID); + CPPUNIT_TEST(testFastExtensionEnabled); + CPPUNIT_TEST(testSnubbing); + CPPUNIT_TEST(testAmChoking); + CPPUNIT_TEST(testAmInterested); + CPPUNIT_TEST(testPeerChoking); + CPPUNIT_TEST(testPeerInterested); + CPPUNIT_TEST(testChokingRequired); + CPPUNIT_TEST(testOptUnchoking); + CPPUNIT_TEST(testShouldBeChoking); + CPPUNIT_TEST_SUITE_END(); +public: + void setUp() {} + + void tearDown() {} + + void testPeerAllowedIndexSetContains(); + void testAmAllowedIndexSetContains(); + void testHasAllPieces(); + void testHasPiece(); + void testUpdateUploadLength(); + void testUpdateDownloadLength(); + void testUpdateLatency(); + void testExtendedMessageEnabled(); + void testGetExtensionMessageID(); + void testFastExtensionEnabled(); + void testSnubbing(); + void testAmChoking(); + void testAmInterested(); + void testPeerChoking(); + void testPeerInterested(); + void testChokingRequired(); + void testOptUnchoking(); + void testShouldBeChoking(); +}; + + +CPPUNIT_TEST_SUITE_REGISTRATION(PeerSessionResourceTest); + +void PeerSessionResourceTest::testPeerAllowedIndexSetContains() +{ + PeerSessionResource res(1024, 1024*1024); + + res.addPeerAllowedIndex(567); + res.addPeerAllowedIndex(789); + + CPPUNIT_ASSERT(res.peerAllowedIndexSetContains(567)); + CPPUNIT_ASSERT(res.peerAllowedIndexSetContains(789)); + CPPUNIT_ASSERT(!res.peerAllowedIndexSetContains(123)); +} + +void PeerSessionResourceTest::testAmAllowedIndexSetContains() +{ + PeerSessionResource res(1024, 1024*1024); + + res.addAmAllowedIndex(567); + res.addAmAllowedIndex(789); + + CPPUNIT_ASSERT(res.amAllowedIndexSetContains(567)); + CPPUNIT_ASSERT(res.amAllowedIndexSetContains(789)); + CPPUNIT_ASSERT(!res.amAllowedIndexSetContains(123)); +} + +void PeerSessionResourceTest::testHasAllPieces() +{ + PeerSessionResource res(1024, 1024*1024); + + CPPUNIT_ASSERT(!res.hasAllPieces()); + res.markSeeder(); + CPPUNIT_ASSERT(res.hasAllPieces()); +} + +void PeerSessionResourceTest::testHasPiece() +{ + PeerSessionResource res(1024, 1024*1024); + + CPPUNIT_ASSERT(!res.hasPiece(300)); + res.updateBitfield(300, 1); + CPPUNIT_ASSERT(res.hasPiece(300)); + res.updateBitfield(300, 0); + CPPUNIT_ASSERT(!res.hasPiece(300)); +} + +void PeerSessionResourceTest::testUpdateUploadLength() +{ + PeerSessionResource res(1024, 1024*1024); + + CPPUNIT_ASSERT_EQUAL((int64_t)0, res.uploadLength()); + res.updateUploadLength(100); + res.updateUploadLength(200); + CPPUNIT_ASSERT_EQUAL((int64_t)300, res.uploadLength()); +} + +void PeerSessionResourceTest::testUpdateDownloadLength() +{ + PeerSessionResource res(1024, 1024*1024); + + CPPUNIT_ASSERT_EQUAL((int64_t)0, res.downloadLength()); + res.updateDownloadLength(100); + res.updateDownloadLength(200); + CPPUNIT_ASSERT_EQUAL((int64_t)300, res.downloadLength()); +} + +void PeerSessionResourceTest::testUpdateLatency() +{ + PeerSessionResource res(1024, 1024*1024); + + CPPUNIT_ASSERT_EQUAL(1500, res.latency()); + res.updateLatency(1000); + CPPUNIT_ASSERT_EQUAL(1100, res.latency()); +} + +void PeerSessionResourceTest::testExtendedMessageEnabled() +{ + PeerSessionResource res(1024, 1024*1024); + + CPPUNIT_ASSERT(!res.extendedMessagingEnabled()); + res.extendedMessagingEnabled(true); + CPPUNIT_ASSERT(res.extendedMessagingEnabled()); + res.extendedMessagingEnabled(false); + CPPUNIT_ASSERT(!res.extendedMessagingEnabled()); +} + +void PeerSessionResourceTest::testGetExtensionMessageID() +{ + PeerSessionResource res(1024, 1024*1024); + + res.addExtension("a2", 9); + CPPUNIT_ASSERT_EQUAL((uint8_t)9, res.getExtensionMessageID("a2")); + CPPUNIT_ASSERT_EQUAL((uint8_t)0, res.getExtensionMessageID("non")); + + CPPUNIT_ASSERT_EQUAL(std::string("a2"), res.getExtensionName(9)); + CPPUNIT_ASSERT_EQUAL(std::string(""), res.getExtensionName(10)); +} + +void PeerSessionResourceTest::testFastExtensionEnabled() +{ + PeerSessionResource res(1024, 1024*1024); + + CPPUNIT_ASSERT(!res.fastExtensionEnabled()); + res.fastExtensionEnabled(true); + CPPUNIT_ASSERT(res.fastExtensionEnabled()); + res.fastExtensionEnabled(false); + CPPUNIT_ASSERT(!res.fastExtensionEnabled()); +} + +void PeerSessionResourceTest::testSnubbing() +{ + PeerSessionResource res(1024, 1024*1024); + + CPPUNIT_ASSERT(!res.snubbing()); + res.snubbing(true); + CPPUNIT_ASSERT(res.snubbing()); + res.snubbing(false); + CPPUNIT_ASSERT(!res.snubbing()); +} + +void PeerSessionResourceTest::testAmChoking() +{ + PeerSessionResource res(1024, 1024*1024); + + CPPUNIT_ASSERT(res.amChoking()); + res.amChoking(false); + CPPUNIT_ASSERT(!res.amChoking()); + res.amChoking(true); + CPPUNIT_ASSERT(res.amChoking()); +} + +void PeerSessionResourceTest::testAmInterested() +{ + PeerSessionResource res(1024, 1024*1024); + + CPPUNIT_ASSERT(!res.amInterested()); + res.amInterested(true); + CPPUNIT_ASSERT(res.amInterested()); + res.amInterested(false); + CPPUNIT_ASSERT(!res.amInterested()); +} + +void PeerSessionResourceTest::testPeerChoking() +{ + PeerSessionResource res(1024, 1024*1024); + + CPPUNIT_ASSERT(res.peerChoking()); + res.peerChoking(false); + CPPUNIT_ASSERT(!res.peerChoking()); + res.peerChoking(true); + CPPUNIT_ASSERT(res.peerChoking()); +} + +void PeerSessionResourceTest::testPeerInterested() +{ + PeerSessionResource res(1024, 1024*1024); + + CPPUNIT_ASSERT(!res.peerInterested()); + res.peerInterested(true); + CPPUNIT_ASSERT(res.peerInterested()); + res.peerInterested(false); + CPPUNIT_ASSERT(!res.peerInterested()); +} + +void PeerSessionResourceTest::testChokingRequired() +{ + PeerSessionResource res(1024, 1024*1024); + + CPPUNIT_ASSERT(res.chokingRequired()); + res.chokingRequired(false); + CPPUNIT_ASSERT(!res.chokingRequired()); + res.chokingRequired(true); + CPPUNIT_ASSERT(res.chokingRequired()); +} + +void PeerSessionResourceTest::testOptUnchoking() +{ + PeerSessionResource res(1024, 1024*1024); + + CPPUNIT_ASSERT(!res.optUnchoking()); + res.optUnchoking(true); + CPPUNIT_ASSERT(res.optUnchoking()); + res.optUnchoking(false); + CPPUNIT_ASSERT(!res.optUnchoking()); +} + +void PeerSessionResourceTest::testShouldBeChoking() +{ + PeerSessionResource res(1024, 1024*1024); + + CPPUNIT_ASSERT(res.shouldBeChoking()); + res.chokingRequired(false); + CPPUNIT_ASSERT(!res.shouldBeChoking()); + res.chokingRequired(true); + res.optUnchoking(true); + CPPUNIT_ASSERT(!res.shouldBeChoking()); +} + +} // namespace aria2 diff --git a/test/PeerTest.cc b/test/PeerTest.cc index 48b1957a..3e41bb3f 100644 --- a/test/PeerTest.cc +++ b/test/PeerTest.cc @@ -18,6 +18,7 @@ public: void setUp() { peer = new Peer("localhost", 6969); + peer->allocateSessionResource(1024, 1024*1024); } void testPeerAllowedIndexSet(); @@ -43,7 +44,7 @@ void PeerTest::testAmAllowedIndexSet() { void PeerTest::testGetId() { CPPUNIT_ASSERT_EQUAL(std::string("f05897fc14a41cb3400e283e189158656d7184da"), - peer->getId()); + peer->getID()); } void PeerTest::testOperatorEqual() diff --git a/test/UTPexExtensionMessageTest.cc b/test/UTPexExtensionMessageTest.cc index 4ff2af07..a4c77371 100644 --- a/test/UTPexExtensionMessageTest.cc +++ b/test/UTPexExtensionMessageTest.cc @@ -72,7 +72,7 @@ void UTPexExtensionMessageTest::testGetBencodedData() { UTPexExtensionMessage msg(1); SharedHandle p1 = new Peer("192.168.0.1", 6881); - p1->allocateBitfield(256*1024, 1024*1024); + p1->allocateSessionResource(256*1024, 1024*1024); p1->setAllBitfield(); msg.addFreshPeer(p1);// added seeder, check add.f flag SharedHandle p2 = new Peer("10.1.1.2", 9999); @@ -105,7 +105,7 @@ void UTPexExtensionMessageTest::testToString() { UTPexExtensionMessage msg(1); SharedHandle p1 = new Peer("192.168.0.1", 6881); - p1->allocateBitfield(256*1024, 1024*1024); + p1->allocateSessionResource(256*1024, 1024*1024); p1->setAllBitfield(); msg.addFreshPeer(p1);// added seeder, check add.f flag SharedHandle p2 = new Peer("10.1.1.2", 9999); @@ -121,7 +121,7 @@ void UTPexExtensionMessageTest::testDoReceivedAction() { UTPexExtensionMessage msg(1); SharedHandle p1 = new Peer("192.168.0.1", 6881); - p1->allocateBitfield(256*1024, 1024*1024); + p1->allocateSessionResource(256*1024, 1024*1024); p1->setAllBitfield(); msg.addFreshPeer(p1);// added seeder, check add.f flag SharedHandle p2 = new Peer("10.1.1.2", 9999);