Sfoglia il codice sorgente

2008-11-03 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

	Execute choking algorithm when BtInterestedMessage arrives from
	unchoked peer.
	* src/BtInterestedMessage.cc
	* src/BtInterestedMessage.h
	* src/DefaultBtMessageFactory.cc
	* test/BtInterestedMessageTest.cc
	* test/MockPeerStorage.h
Tatsuhiro Tsujikawa 17 anni fa
parent
commit
6d01f8f94f

+ 10 - 0
ChangeLog

@@ -1,3 +1,13 @@
+2008-11-03  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
+
+	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  <tujikawa at rednoah dot com>
 
 	AuthConfigFactory is now part of DownloadEngine.

+ 10 - 0
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 = peerStorage;
+}
+
 } // namespace aria2

+ 5 - 0
src/BtInterestedMessage.h

@@ -39,6 +39,7 @@
 
 namespace aria2 {
 
+class PeerStorage;
 class BtInterestedMessage;
 
 typedef SharedHandle<BtInterestedMessage> BtInterestedMessageHandle;
@@ -47,6 +48,8 @@ class BtInterestedMessage : public SimpleBtMessage {
 private:
   unsigned char* msg;
 
+  SharedHandle<PeerStorage> _peerStorage;
+
   static size_t MESSAGE_LENGTH;
 
 public:
@@ -73,6 +76,8 @@ public:
   virtual bool sendPredicate() const;
 
   virtual void onSendComplete();
+
+  void setPeerStorage(const SharedHandle<PeerStorage>& peerStorage);
 };
 
 } // namespace aria2

+ 6 - 1
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<BtInterestedMessage> m =
+	  BtInterestedMessage::create(data, dataLength);
+	m->setPeerStorage(_peerStorage);
+	msg = m;
+      }
       break;
     case BtNotInterestedMessage::ID:
       {

+ 10 - 5
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<MockPeerStorage> 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() {

+ 14 - 3
test/MockPeerStorage.h

@@ -2,9 +2,11 @@
 #define _D_MOCK_PEER_STORAGE_H_
 
 #include "PeerStorage.h"
-#include "Peer.h"
+
 #include <algorithm>
 
+#include "Peer.h"
+
 namespace aria2 {
 
 class MockPeerStorage : public PeerStorage {
@@ -12,8 +14,9 @@ private:
   TransferStat stat;
   std::deque<SharedHandle<Peer> > peers;
   std::deque<SharedHandle<Peer> > activePeers;
+  int _numChokeExecuted;
 public:
-  MockPeerStorage() {}
+  MockPeerStorage():_numChokeExecuted(0) {}
   virtual ~MockPeerStorage() {}
 
   virtual bool addPeer(const SharedHandle<Peer>& 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_