From 4d067676ea5ec37faecb0e80b984a7404f2baa09 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Mon, 5 Nov 2012 17:15:27 -0800 Subject: [PATCH] Make it possible to track peers by 64-bit peer ID. --- src/ConnectionPool.cpp | 24 +++++++++++++++++++++++- src/ConnectionPool.h | 7 ++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/ConnectionPool.cpp b/src/ConnectionPool.cpp index 93fce5842..278ad5293 100644 --- a/src/ConnectionPool.cpp +++ b/src/ConnectionPool.cpp @@ -110,6 +110,21 @@ bool ConnectionPool::savePeer(const std::string& strIp, int iPort, char code) return bNew; } +Peer::pointer ConnectionPool::getPeerById(const uint64& id) +{ + boost::mutex::scoped_lock sl(mPeerLock); + boost::unordered_map::iterator it = mPeerIdMap.find(id); + if (it == mPeerIdMap.end()) + return Peer::pointer(); + return it->second; +} + +bool ConnectionPool::hasPeer(const uint64& id) +{ + boost::mutex::scoped_lock sl(mPeerLock); + return mPeerIdMap.find(id) != mPeerIdMap.end(); +} + // An available peer is one we had no trouble connect to last time and that we are not currently knowingly connected or connecting // too. // @@ -412,6 +427,9 @@ bool ConnectionPool::peerConnected(Peer::ref peer, const RippleAddress& naPeer, mConnectedMap[naPeer] = peer; bNew = true; + + assert(peer->getPeerId() != 0); + mPeerIdMap.insert(std::make_pair(peer->getPeerId(), peer)); } // Found in map, already connected. else if (!strIP.empty()) @@ -450,11 +468,12 @@ bool ConnectionPool::peerConnected(Peer::ref peer, const RippleAddress& naPeer, // We maintain a map of public key to peer for connected and verified peers. Maintain it. void ConnectionPool::peerDisconnected(Peer::ref peer, const RippleAddress& naPeer) { + boost::mutex::scoped_lock sl(mPeerLock); + if (naPeer.isValid()) { boost::unordered_map::iterator itCm; - boost::mutex::scoped_lock sl(mPeerLock); itCm = mConnectedMap.find(naPeer); @@ -482,6 +501,9 @@ void ConnectionPool::peerDisconnected(Peer::ref peer, const RippleAddress& naPee { //cLog(lsINFO) << "Pool: disconnected: anonymous: " << peer->getIP() << " " << peer->getPort(); } + + assert(peer->getPeerId() != 0); + mPeerIdMap.erase(peer->getPeerId()); } // Schedule for immediate scanning, if not already scheduled. diff --git a/src/ConnectionPool.h b/src/ConnectionPool.h index e3801ef73..dbc6cd8b2 100644 --- a/src/ConnectionPool.h +++ b/src/ConnectionPool.h @@ -19,7 +19,7 @@ private: boost::mutex mPeerLock; uint64 mLastPeer; - typedef std::pair naPeer; + typedef std::pair naPeer; typedef std::pair pipPeer; // Peers we are connecting with and non-thin peers we are connected to. @@ -33,6 +33,9 @@ private: // Peers we have the public key for. boost::unordered_map mConnectedMap; + // Connections with have a 64-bit identifier + boost::unordered_map mPeerIdMap; + boost::asio::ssl::context mCtx; Peer::pointer mScanning; @@ -94,6 +97,8 @@ public: // Peer 64-bit ID function uint64 assignPeerId(); + Peer::pointer getPeerById(const uint64& id); + bool hasPeer(const uint64& id); // // Scanning