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
pull/1/head
Tatsuhiro Tsujikawa 2008-11-03 12:04:57 +00:00
parent 8fab0ff216
commit 6d01f8f94f
6 changed files with 55 additions and 9 deletions

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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:
{

View File

@ -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() {

View File

@ -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_