diff --git a/ChangeLog b/ChangeLog index 3dc048d1..d7301ea8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2008-11-03 Tatsuhiro Tsujikawa + + Execute choking algorithm when BtInterestedMessage arrives from + unchoked peer. + * src/BtInterestedMessage.cc + * src/BtInterestedMessage.h + * src/DefaultBtMessageFactory.cc + * test/BtInterestedMessageTest.cc + * test/MockPeerStorage.h + 2008-11-03 Tatsuhiro Tsujikawa AuthConfigFactory is now part of DownloadEngine. diff --git a/src/BtInterestedMessage.cc b/src/BtInterestedMessage.cc index 0e44d9d3..0224f747 100644 --- a/src/BtInterestedMessage.cc +++ b/src/BtInterestedMessage.cc @@ -39,6 +39,7 @@ #include "Peer.h" #include "BtContext.h" #include "StringFormat.h" +#include "PeerStorage.h" namespace aria2 { @@ -58,6 +59,9 @@ BtInterestedMessageHandle BtInterestedMessage::create(const unsigned char* data, void BtInterestedMessage::doReceivedAction() { peer->peerInterested(true); + if(!peer->amChoking()) { + _peerStorage->executeChoke(); + } } bool BtInterestedMessage::sendPredicate() const { @@ -92,4 +96,10 @@ std::string BtInterestedMessage::toString() const { return INTERESTED; } +void BtInterestedMessage::setPeerStorage +(const SharedHandle& peerStorage) +{ + _peerStorage = peerStorage; +} + } // namespace aria2 diff --git a/src/BtInterestedMessage.h b/src/BtInterestedMessage.h index 9088b259..08727f09 100644 --- a/src/BtInterestedMessage.h +++ b/src/BtInterestedMessage.h @@ -39,6 +39,7 @@ namespace aria2 { +class PeerStorage; class BtInterestedMessage; typedef SharedHandle BtInterestedMessageHandle; @@ -47,6 +48,8 @@ class BtInterestedMessage : public SimpleBtMessage { private: unsigned char* msg; + SharedHandle _peerStorage; + static size_t MESSAGE_LENGTH; public: @@ -73,6 +76,8 @@ public: virtual bool sendPredicate() const; virtual void onSendComplete(); + + void setPeerStorage(const SharedHandle& peerStorage); }; } // namespace aria2 diff --git a/src/DefaultBtMessageFactory.cc b/src/DefaultBtMessageFactory.cc index 6cba336f..e480ecad 100644 --- a/src/DefaultBtMessageFactory.cc +++ b/src/DefaultBtMessageFactory.cc @@ -96,7 +96,12 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL msg = BtUnchokeMessage::create(data, dataLength); break; case BtInterestedMessage::ID: - msg = BtInterestedMessage::create(data, dataLength); + { + SharedHandle m = + BtInterestedMessage::create(data, dataLength); + m->setPeerStorage(_peerStorage); + msg = m; + } break; case BtNotInterestedMessage::ID: { diff --git a/test/BtInterestedMessageTest.cc b/test/BtInterestedMessageTest.cc index 28226106..37c73040 100644 --- a/test/BtInterestedMessageTest.cc +++ b/test/BtInterestedMessageTest.cc @@ -6,6 +6,7 @@ #include "PeerMessageUtil.h" #include "Peer.h" +#include "MockPeerStorage.h" namespace aria2 { @@ -18,12 +19,7 @@ class BtInterestedMessageTest:public CppUnit::TestFixture { CPPUNIT_TEST(testOnSendComplete); CPPUNIT_TEST(testToString); CPPUNIT_TEST_SUITE_END(); -private: - public: - void setUp() { - } - void testCreate(); void testGetMessage(); void testDoReceivedAction(); @@ -71,9 +67,18 @@ void BtInterestedMessageTest::testDoReceivedAction() { peer->allocateSessionResource(1024, 1024*1024); msg.setPeer(peer); + SharedHandle peerStorage(new MockPeerStorage()); + + msg.setPeerStorage(peerStorage); + CPPUNIT_ASSERT(!peer->peerInterested()); msg.doReceivedAction(); CPPUNIT_ASSERT(peer->peerInterested()); + CPPUNIT_ASSERT_EQUAL(0, peerStorage->getNumChokeExecuted()); + + peer->amChoking(false); + msg.doReceivedAction(); + CPPUNIT_ASSERT_EQUAL(1, peerStorage->getNumChokeExecuted()); } void BtInterestedMessageTest::testOnSendComplete() { diff --git a/test/MockPeerStorage.h b/test/MockPeerStorage.h index b79fdd07..5a5973e7 100644 --- a/test/MockPeerStorage.h +++ b/test/MockPeerStorage.h @@ -2,9 +2,11 @@ #define _D_MOCK_PEER_STORAGE_H_ #include "PeerStorage.h" -#include "Peer.h" + #include +#include "Peer.h" + namespace aria2 { class MockPeerStorage : public PeerStorage { @@ -12,8 +14,9 @@ private: TransferStat stat; std::deque > peers; std::deque > activePeers; + int _numChokeExecuted; public: - MockPeerStorage() {} + MockPeerStorage():_numChokeExecuted(0) {} virtual ~MockPeerStorage() {} virtual bool addPeer(const SharedHandle& peer) { @@ -63,7 +66,15 @@ public: return false; } - virtual void executeChoke() {} + virtual void executeChoke() + { + ++_numChokeExecuted; + } + + int getNumChokeExecuted() const + { + return _numChokeExecuted; + } }; #endif // _D_MOCK_PEER_STORAGE_H_