2008-06-17 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

Filled up pieces based on the number of missing blocks, rather 
than
	simplay the number of piece * block length.
	* src/BtRequestFactory.h
	* src/DefaultBtInteractive.cc
	* src/DefaultBtInteractive.h
	* src/DefaultBtRequestFactory.cc
	* src/DefaultBtRequestFactory.h
	* src/Piece.cc
	* src/Piece.h
	* test/BitfieldManTest.cc
	* test/DefaultBtRequestFactoryTest.cc
	* test/MockBtRequestFactory.h
pull/1/head
Tatsuhiro Tsujikawa 2008-06-17 11:43:29 +00:00
parent 09502fc3db
commit dfe2f99345
11 changed files with 87 additions and 15 deletions

View File

@ -1,3 +1,18 @@
2008-06-17 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Filled up pieces based on the number of missing blocks, rather than
simplay the number of piece * block length.
* src/BtRequestFactory.h
* src/DefaultBtInteractive.cc
* src/DefaultBtInteractive.h
* src/DefaultBtRequestFactory.cc
* src/DefaultBtRequestFactory.h
* src/Piece.cc
* src/Piece.h
* test/BitfieldManTest.cc
* test/DefaultBtRequestFactoryTest.cc
* test/MockBtRequestFactory.h
2008-06-17 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Made _epEvents a member variable.

View File

@ -56,6 +56,8 @@ public:
virtual size_t countTargetPiece() = 0;
virtual size_t countMissingBlock() = 0;
virtual void removeCompletedPiece() = 0;
virtual void doChokedAction() = 0;

View File

@ -288,26 +288,32 @@ void DefaultBtInteractive::decideInterest() {
}
}
void DefaultBtInteractive::fillPiece(size_t maxPieceNum) {
void DefaultBtInteractive::fillPiece(size_t maxMissingBlock) {
if(pieceStorage->hasMissingPiece(peer)) {
size_t numMissingBlock = btRequestFactory->countMissingBlock();
if(peer->peerChoking()) {
if(peer->isFastExtensionEnabled()) {
while(btRequestFactory->countTargetPiece() < maxPieceNum) {
while(numMissingBlock < maxMissingBlock) {
PieceHandle piece = pieceStorage->getMissingFastPiece(peer);
if(piece.isNull()) {
break;
} else {
btRequestFactory->addTargetPiece(piece);
numMissingBlock += piece->countMissingBlock();
}
}
}
} else {
while(btRequestFactory->countTargetPiece() < maxPieceNum) {
while(numMissingBlock < maxMissingBlock) {
PieceHandle piece = pieceStorage->getMissingPiece(peer);
if(piece.isNull()) {
break;
} else {
btRequestFactory->addTargetPiece(piece);
numMissingBlock += piece->countMissingBlock();
}
}
}
@ -323,14 +329,7 @@ void DefaultBtInteractive::addRequests() {
} else {
MAX_PENDING_REQUEST = 6;
}
size_t pieceNum;
if(pieceStorage->isEndGame()) {
pieceNum = 1;
} else {
size_t blocks = DIV_FLOOR(btContext->getPieceLength(), Piece::BLOCK_LENGTH);
pieceNum = DIV_FLOOR(MAX_PENDING_REQUEST, blocks);
}
fillPiece(pieceNum);
fillPiece(MAX_PENDING_REQUEST);
size_t reqNumToCreate =
MAX_PENDING_REQUEST <= dispatcher->countOutstandingRequest() ?

View File

@ -122,7 +122,7 @@ private:
void checkHave();
void sendKeepAlive();
void decideInterest();
void fillPiece(size_t maxPieceNum);
void fillPiece(size_t maxMissingBlock);
void addRequests();
void detectMessageFlooding();
void checkActiveInteraction();

View File

@ -186,6 +186,30 @@ void DefaultBtRequestFactory::createRequestMessagesOnEndGame
}
}
class CountMissingBlock
{
private:
size_t _numMissingBlock;
public:
CountMissingBlock():_numMissingBlock(0) {}
size_t getNumMissingBlock()
{
return _numMissingBlock;
}
void operator()(const SharedHandle<Piece>& piece)
{
_numMissingBlock += piece->countMissingBlock();
}
};
size_t DefaultBtRequestFactory::countMissingBlock()
{
return std::for_each(pieces.begin(), pieces.end(),
CountMissingBlock()).getNumMissingBlock();
}
std::deque<SharedHandle<Piece> >& DefaultBtRequestFactory::getTargetPieces()
{
return pieces;

View File

@ -72,6 +72,8 @@ public:
return pieces.size();
}
virtual size_t countMissingBlock();
virtual void removeCompletedPiece();
virtual void doChokedAction();

View File

@ -162,6 +162,11 @@ size_t Piece::countCompleteBlock() const
return bitfield->countBlock()-bitfield->countMissingBlock();
}
size_t Piece::countMissingBlock() const
{
return bitfield->countMissingBlock();
}
bool Piece::hasBlock(size_t blockIndex) const
{
return bitfield->isBitSet(blockIndex);

View File

@ -95,6 +95,8 @@ public:
size_t countCompleteBlock() const;
size_t countMissingBlock() const;
bool hasBlock(size_t blockIndex) const;
/**

View File

@ -26,6 +26,7 @@ class BitfieldManTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testGetMissingIndex_noarg);
CPPUNIT_TEST(testGetMissingUnusedIndex_noarg);
CPPUNIT_TEST(testCountFilteredBlock);
CPPUNIT_TEST(testCountMissingBlock);
CPPUNIT_TEST_SUITE_END();
private:
SharedHandle<Randomizer> fixedNumberRandomizer;
@ -59,6 +60,7 @@ public:
void testGetMissingUnusedLength();
void testSetBitRange();
void testCountFilteredBlock();
void testCountMissingBlock();
};
@ -741,4 +743,14 @@ void BitfieldManTest::testCountFilteredBlock()
CPPUNIT_ASSERT_EQUAL((size_t)0, bt.countFilteredBlock());
}
void BitfieldManTest::testCountMissingBlock()
{
BitfieldMan bt(1024, 1024*10);
CPPUNIT_ASSERT_EQUAL((size_t)10, bt.countMissingBlock());
bt.setBit(1);
CPPUNIT_ASSERT_EQUAL((size_t)9, bt.countMissingBlock());
bt.setAllBit();
CPPUNIT_ASSERT_EQUAL((size_t)0, bt.countMissingBlock());
}
} // namespace aria2

View File

@ -123,9 +123,18 @@ public:
CPPUNIT_TEST_SUITE_REGISTRATION(DefaultBtRequestFactoryTest);
void DefaultBtRequestFactoryTest::testAddTargetPiece() {
SharedHandle<Piece> piece(new Piece(0, 16*1024));
btRequestFactory->addTargetPiece(piece);
CPPUNIT_ASSERT_EQUAL((size_t)1, btRequestFactory->countTargetPiece());
{
SharedHandle<Piece> piece(new Piece(0, 16*1024*10));
btRequestFactory->addTargetPiece(piece);
CPPUNIT_ASSERT_EQUAL((size_t)1, btRequestFactory->countTargetPiece());
}
{
SharedHandle<Piece> piece(new Piece(1, 16*1024*9));
piece->completeBlock(0);
btRequestFactory->addTargetPiece(piece);
CPPUNIT_ASSERT_EQUAL((size_t)2, btRequestFactory->countTargetPiece());
}
CPPUNIT_ASSERT_EQUAL((size_t)18, btRequestFactory->countMissingBlock());
}
void DefaultBtRequestFactoryTest::testRemoveCompletedPiece() {

View File

@ -17,6 +17,8 @@ public:
virtual size_t countTargetPiece() { return 0; }
virtual size_t countMissingBlock() { return 0; }
virtual void removeCompletedPiece() {}
virtual void doChokedAction() {}