Fix frequent interested/not interested message transmission

This commit is contained in:
Tatsuhiro Tsujikawa 2016-07-09 18:13:50 +09:00
parent 570d46725f
commit 41df1607f6
7 changed files with 2 additions and 83 deletions

View file

@ -69,29 +69,6 @@ void BtInterestedMessage::doReceivedAction()
} }
} }
bool BtInterestedMessage::sendPredicate() const
{
return !getPeer()->amInterested();
}
namespace {
struct ThisProgressUpdate : public ProgressUpdate {
ThisProgressUpdate(std::shared_ptr<Peer> peer) : peer(std::move(peer)) {}
virtual void update(size_t length, bool complete) CXX11_OVERRIDE
{
if (complete) {
peer->amInterested(true);
}
}
std::shared_ptr<Peer> peer;
};
} // namespace
std::unique_ptr<ProgressUpdate> BtInterestedMessage::getProgressUpdate()
{
return make_unique<ThisProgressUpdate>(getPeer());
}
void BtInterestedMessage::setPeerStorage(PeerStorage* peerStorage) void BtInterestedMessage::setPeerStorage(PeerStorage* peerStorage)
{ {
peerStorage_ = peerStorage; peerStorage_ = peerStorage;

View file

@ -59,10 +59,6 @@ public:
virtual void doReceivedAction() CXX11_OVERRIDE; virtual void doReceivedAction() CXX11_OVERRIDE;
virtual bool sendPredicate() const CXX11_OVERRIDE;
virtual std::unique_ptr<ProgressUpdate> getProgressUpdate() CXX11_OVERRIDE;
void setPeerStorage(PeerStorage* peerStorage); void setPeerStorage(PeerStorage* peerStorage);
}; };

View file

@ -65,29 +65,6 @@ void BtNotInterestedMessage::doReceivedAction()
} }
} }
bool BtNotInterestedMessage::sendPredicate() const
{
return getPeer()->amInterested();
}
namespace {
struct ThisProgressUpdate : public ProgressUpdate {
ThisProgressUpdate(std::shared_ptr<Peer> peer) : peer(std::move(peer)) {}
virtual void update(size_t length, bool complete) CXX11_OVERRIDE
{
if (complete) {
peer->amInterested(false);
}
}
std::shared_ptr<Peer> peer;
};
} // namespace
std::unique_ptr<ProgressUpdate> BtNotInterestedMessage::getProgressUpdate()
{
return make_unique<ThisProgressUpdate>(getPeer());
}
void BtNotInterestedMessage::setPeerStorage(PeerStorage* peerStorage) void BtNotInterestedMessage::setPeerStorage(PeerStorage* peerStorage)
{ {
peerStorage_ = peerStorage; peerStorage_ = peerStorage;

View file

@ -59,10 +59,6 @@ public:
virtual void doReceivedAction() CXX11_OVERRIDE; virtual void doReceivedAction() CXX11_OVERRIDE;
virtual bool sendPredicate() const CXX11_OVERRIDE;
virtual std::unique_ptr<ProgressUpdate> getProgressUpdate() CXX11_OVERRIDE;
void setPeerStorage(PeerStorage* peerStorage); void setPeerStorage(PeerStorage* peerStorage);
}; };

View file

@ -363,6 +363,7 @@ void DefaultBtInteractive::decideInterest()
if (pieceStorage_->hasMissingPiece(peer_)) { if (pieceStorage_->hasMissingPiece(peer_)) {
if (!peer_->amInterested()) { if (!peer_->amInterested()) {
A2_LOG_DEBUG(fmt(MSG_PEER_INTERESTED, cuid_)); A2_LOG_DEBUG(fmt(MSG_PEER_INTERESTED, cuid_));
peer_->amInterested(true);
dispatcher_->addMessageToQueue( dispatcher_->addMessageToQueue(
messageFactory_->createInterestedMessage()); messageFactory_->createInterestedMessage());
} }
@ -370,6 +371,7 @@ void DefaultBtInteractive::decideInterest()
else { else {
if (peer_->amInterested()) { if (peer_->amInterested()) {
A2_LOG_DEBUG(fmt(MSG_PEER_NOT_INTERESTED, cuid_)); A2_LOG_DEBUG(fmt(MSG_PEER_NOT_INTERESTED, cuid_));
peer_->amInterested(false);
dispatcher_->addMessageToQueue( dispatcher_->addMessageToQueue(
messageFactory_->createNotInterestedMessage()); messageFactory_->createNotInterestedMessage());
} }

View file

@ -17,7 +17,6 @@ class BtInterestedMessageTest : public CppUnit::TestFixture {
CPPUNIT_TEST(testCreate); CPPUNIT_TEST(testCreate);
CPPUNIT_TEST(testCreateMessage); CPPUNIT_TEST(testCreateMessage);
CPPUNIT_TEST(testDoReceivedAction); CPPUNIT_TEST(testDoReceivedAction);
CPPUNIT_TEST(testOnSendComplete);
CPPUNIT_TEST(testToString); CPPUNIT_TEST(testToString);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
@ -25,7 +24,6 @@ public:
void testCreate(); void testCreate();
void testCreateMessage(); void testCreateMessage();
void testDoReceivedAction(); void testDoReceivedAction();
void testOnSendComplete();
void testToString(); void testToString();
}; };
@ -90,18 +88,6 @@ void BtInterestedMessageTest::testDoReceivedAction()
CPPUNIT_ASSERT_EQUAL(1, peerStorage->getNumChokeExecuted()); CPPUNIT_ASSERT_EQUAL(1, peerStorage->getNumChokeExecuted());
} }
void BtInterestedMessageTest::testOnSendComplete()
{
BtInterestedMessage msg;
std::shared_ptr<Peer> peer(new Peer("host", 6969));
peer->allocateSessionResource(1_k, 1_m);
msg.setPeer(peer);
CPPUNIT_ASSERT(!peer->amInterested());
std::shared_ptr<ProgressUpdate> pu(msg.getProgressUpdate());
pu->update(0, true);
CPPUNIT_ASSERT(peer->amInterested());
}
void BtInterestedMessageTest::testToString() void BtInterestedMessageTest::testToString()
{ {
BtInterestedMessage msg; BtInterestedMessage msg;

View file

@ -17,7 +17,6 @@ class BtNotInterestedMessageTest : public CppUnit::TestFixture {
CPPUNIT_TEST(testCreate); CPPUNIT_TEST(testCreate);
CPPUNIT_TEST(testCreateMessage); CPPUNIT_TEST(testCreateMessage);
CPPUNIT_TEST(testDoReceivedAction); CPPUNIT_TEST(testDoReceivedAction);
CPPUNIT_TEST(testOnSendComplete);
CPPUNIT_TEST(testToString); CPPUNIT_TEST(testToString);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
@ -25,7 +24,6 @@ public:
void testCreate(); void testCreate();
void testCreateMessage(); void testCreateMessage();
void testDoReceivedAction(); void testDoReceivedAction();
void testOnSendComplete();
void testToString(); void testToString();
}; };
@ -91,19 +89,6 @@ void BtNotInterestedMessageTest::testDoReceivedAction()
CPPUNIT_ASSERT_EQUAL(1, peerStorage->getNumChokeExecuted()); CPPUNIT_ASSERT_EQUAL(1, peerStorage->getNumChokeExecuted());
} }
void BtNotInterestedMessageTest::testOnSendComplete()
{
std::shared_ptr<Peer> peer(new Peer("host", 6969));
peer->allocateSessionResource(1_k, 1_m);
peer->amInterested(true);
BtNotInterestedMessage msg;
msg.setPeer(peer);
CPPUNIT_ASSERT(peer->amInterested());
std::shared_ptr<ProgressUpdate> pu(msg.getProgressUpdate());
pu->update(0, true);
CPPUNIT_ASSERT(!peer->amInterested());
}
void BtNotInterestedMessageTest::testToString() void BtNotInterestedMessageTest::testToString()
{ {
BtNotInterestedMessage msg; BtNotInterestedMessage msg;