Browse Source

api cleanup. connect peer when required.

cl-refactor
subtly 10 years ago
parent
commit
11d2446247
  1. 53
      libp2p/Host.cpp
  2. 10
      libp2p/Host.h

53
libp2p/Host.cpp

@ -423,28 +423,39 @@ void Host::addNode(NodeId const& _node, std::string const& _addr, unsigned short
if (m_nodeTable) m_nodeTable->addNode(Node(_node, NodeIPEndpoint(bi::udp::endpoint(addr, _udpNodePort), bi::tcp::endpoint(addr, _tcpPeerPort))));
}
void Host::requirePeer(NodeId const& _n, std::string const& _addr, unsigned short _port)
void Host::relinquishPeer(NodeId const& _node)
{
auto addr = bi::address::from_string(_addr);
Node node(_n, NodeIPEndpoint(bi::udp::endpoint(addr, _port), bi::tcp::endpoint(addr, _port)));
Guard l(x_requiredPeers);
if (m_requiredPeers.count(_node))
m_requiredPeers.erase(_node);
}
void Host::requirePeer(NodeId const& _n, std::string const& _udpAddr, unsigned short _udpPort, std::string const& _tcpAddr, unsigned short _tcpPort)
{
auto naddr = bi::address::from_string(_udpAddr);
auto paddr = _tcpAddr.empty() ? naddr : bi::address::from_string(_tcpAddr);
auto udp = bi::udp::endpoint(naddr, _udpPort);
auto tcp = bi::tcp::endpoint(paddr, _tcpPort ? _tcpPort : _udpPort);
Node node(_n, NodeIPEndpoint(udp, tcp));
if (_n)
{
// add or replace peer
shared_ptr<Peer> p;
RecursiveGuard l(x_sessions);
if (m_peers.count(_n))
p = m_peers[_n];
else
{
// TODO p2p: construct peer from node
p.reset(new Peer());
p->id = _n;
p->endpoint = NodeIPEndpoint(node.endpoint.udp, node.endpoint.tcp);
p->required = true;
m_peers[_n] = p;
RecursiveGuard l(x_sessions);
if (m_peers.count(_n))
p = m_peers[_n];
else
{
p.reset(new Peer());
p->id = _n;
p->required = true;
m_peers[_n] = p;
}
p->endpoint.udp = node.endpoint.udp;
p->endpoint.tcp = node.endpoint.tcp;
}
p->endpoint.udp = node.endpoint.udp;
p->endpoint.tcp = node.endpoint.tcp;
connect(p);
}
else if (m_nodeTable)
{
@ -456,16 +467,8 @@ void Host::requirePeer(NodeId const& _n, std::string const& _addr, unsigned shor
t->async_wait([this, _n](boost::system::error_code const& _ec)
{
if (!_ec && m_nodeTable)
if (auto n = m_nodeTable->node(_n))
if (!m_peers.count(_n))
{
shared_ptr<Peer> p(new Peer());
p->id = _n;
p->endpoint = n.endpoint;
p->required = true;
RecursiveGuard l(x_sessions);
m_peers[_n] = p;
}
if (auto n = m_nodeTable->node(_n))
requirePeer(n.id, n.endpoint.udp.address().to_string(), n.endpoint.udp.port(), n.endpoint.tcp.address().to_string(), n.endpoint.tcp.port());
});
}
}

10
libp2p/Host.h

@ -106,10 +106,10 @@ public:
void addNode(NodeId const& _node, std::string const& _addr, unsigned short _tcpPort, unsigned short _udpPort);
/// Create Peer and attempt keeping peer connected.
void requirePeer(NodeId const& _node, std::string const& _addr, unsigned short _port);
void requirePeer(NodeId const& _node, std::string const& _udpAddr, unsigned short _udpPort, std::string const& _tcpAddr = "", unsigned short _tcpPort = 0);
// /// Note peer as no longer being required.
// void relinquishPeer(NodeId const& _node);
/// Note peer as no longer being required.
void relinquishPeer(NodeId const& _node);
/// Set ideal number of peers.
void setIdealPeerCount(unsigned _n) { m_idealPeerCount = _n; }
@ -216,6 +216,10 @@ private:
/// Shared storage of Peer objects. Peers are created or destroyed on demand by the Host. Active sessions maintain a shared_ptr to a Peer;
std::map<NodeId, std::shared_ptr<Peer>> m_peers;
/// Peers we try to connect regardless of p2p network.
std::set<NodeId> m_requiredPeers;
Mutex x_requiredPeers;
std::list<std::shared_ptr<boost::asio::deadline_timer>> m_timers;
/// The nodes to which we are currently connected. Used by host to service peer requests and keepAlivePeers and for shutdown. (see run())

Loading…
Cancel
Save