mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-20 11:05:54 +00:00
Implement 'connectTo'.
This commit is contained in:
@@ -1,17 +1,16 @@
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
|
||||
#include "ConnectionPool.h"
|
||||
#include "Config.h"
|
||||
#include "KnownNodeList.h"
|
||||
#include "Peer.h"
|
||||
#include <boost/foreach.hpp>
|
||||
#include "Application.h"
|
||||
|
||||
using namespace boost;
|
||||
using boost::asio::ip::tcp;
|
||||
|
||||
ConnectionPool::ConnectionPool()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
{ ; }
|
||||
|
||||
|
||||
void ConnectionPool::connectToNetwork(KnownNodeList& nodeList,boost::asio::io_service& io_service)
|
||||
@@ -47,3 +46,57 @@ void ConnectionPool::relayMessage(Peer* fromPeer,PackedMessage::pointer msg)
|
||||
peer->sendPacket(msg);
|
||||
}
|
||||
}
|
||||
|
||||
bool ConnectionPool::addToMap(const uint160& hanko, Peer::pointer peer)
|
||||
{
|
||||
boost::mutex::scoped_lock sl(peerLock);
|
||||
return peerMap.insert(std::make_pair(hanko, peer)).second;
|
||||
}
|
||||
|
||||
bool ConnectionPool::delFromMap(const uint160& hanko, Peer::pointer peer)
|
||||
{
|
||||
boost::mutex::scoped_lock sl(peerLock);
|
||||
std::map<uint160, Peer::pointer>::iterator it=peerMap.find(hanko);
|
||||
if((it==peerMap.end()) || (it->first!=hanko)) return false;
|
||||
peerMap.erase(it);
|
||||
return true;
|
||||
}
|
||||
|
||||
Peer::pointer ConnectionPool::findInMap(const uint160& hanko)
|
||||
{
|
||||
boost::mutex::scoped_lock sl(peerLock);
|
||||
std::map<uint160, Peer::pointer>::iterator it=peerMap.find(hanko);
|
||||
if(it==peerMap.end()) return Peer::pointer();
|
||||
return it->second;
|
||||
}
|
||||
|
||||
bool ConnectionPool::inMap(const uint160& hanko)
|
||||
{
|
||||
boost::mutex::scoped_lock sl(peerLock);
|
||||
return peerMap.find(hanko) != peerMap.end();
|
||||
}
|
||||
|
||||
std::map<uint160, Peer::pointer> ConnectionPool::getAllConnected()
|
||||
{
|
||||
boost::mutex::scoped_lock sl(peerLock);
|
||||
return peerMap;
|
||||
}
|
||||
|
||||
bool ConnectionPool::connectTo(const std::string& host, const std::string& port)
|
||||
{
|
||||
boost::asio::ip::tcp::resolver res(theApp->getIOService());
|
||||
boost::asio::ip::tcp::resolver::query query(host.c_str(), port.c_str());
|
||||
boost::asio::ip::tcp::resolver::iterator it(res.resolve(query)), end;
|
||||
|
||||
Peer::pointer peer(Peer::create(theApp->getIOService()));
|
||||
boost::system::error_code error = boost::asio::error::host_not_found;
|
||||
while (error && (it!=end))
|
||||
{
|
||||
peer->getSocket().close();
|
||||
peer->getSocket().connect(*it++, error);
|
||||
}
|
||||
if(error) return false;
|
||||
boost::mutex::scoped_lock sl(peerLock);
|
||||
mPeers.push_back(peer);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
#ifndef __CONNECTION_POOL__
|
||||
#define __CONNECTION_POOL__
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
|
||||
#include "Peer.h"
|
||||
#include "PackedMessage.h"
|
||||
#include "types.h"
|
||||
#include <boost/asio.hpp>
|
||||
|
||||
class KnownNodeList;
|
||||
|
||||
/*
|
||||
@@ -12,7 +15,9 @@ This is the list of all the Peers we are currently connected to
|
||||
*/
|
||||
class ConnectionPool
|
||||
{
|
||||
std::vector<Peer::pointer> mPeers;
|
||||
boost::mutex peerLock;
|
||||
std::vector<Peer::pointer> mPeers; // FIXME
|
||||
std::map<uint160, Peer::pointer> peerMap;
|
||||
//std::vector<std::pair<PackedMessage::pointer,int> > mBroadcastMessages;
|
||||
|
||||
public:
|
||||
@@ -21,7 +26,14 @@ public:
|
||||
void relayMessage(Peer* fromPeer, PackedMessage::pointer msg);
|
||||
//bool isMessageKnown(PackedMessage::pointer msg);
|
||||
|
||||
// hanko->peer mapping functions
|
||||
bool inMap(const uint160& hanko);
|
||||
bool addToMap(const uint160& hanko, Peer::pointer peer);
|
||||
bool delFromMap(const uint160& hanko, Peer::pointer peer);
|
||||
Peer::pointer findInMap(const uint160& hanko);
|
||||
std::map<uint160, Peer::pointer> getAllConnected();
|
||||
|
||||
bool connectTo(const std::string& host, const std::string& port);
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user