Browse Source

Improve addNode functionality when addNode is called during network startup.

cl-refactor
subtly 10 years ago
parent
commit
7d341bd253
  1. 16
      libp2p/Host.cpp
  2. 7
      libp2p/Host.h
  3. 2
      libwebthree/WebThree.cpp
  4. 2
      libwebthree/WebThree.h
  5. 2
      test/peer.cpp

16
libp2p/Host.cpp

@ -383,11 +383,12 @@ string Host::pocHost()
void Host::addNode(NodeId const& _node, bi::address const& _addr, unsigned short _udpNodePort, unsigned short _tcpPeerPort)
{
// TODO: p2p clean this up (bring tested acceptor code over from network branch)
while (isWorking() && !m_run)
this_thread::sleep_for(chrono::milliseconds(50));
if (!m_run)
return;
// return if network is stopped while waiting on Host::run() or nodeTable to start
while (!haveNetwork())
if(isWorking())
this_thread::sleep_for(chrono::milliseconds(50));
else
return;
if (_tcpPeerPort < 30300 || _tcpPeerPort > 30305)
cwarn << "Non-standard port being recorded: " << _tcpPeerPort;
@ -636,8 +637,9 @@ void Host::startedWorking()
else
clog(NetNote) << "p2p.start.notice id:" << id().abridged() << "TCP Listen port is invalid or unavailable.";
m_nodeTable.reset(new NodeTable(m_ioService, m_alias, bi::address::from_string(listenAddress()), listenPort()));
m_nodeTable->setEventHandler(new HostNodeTableHandler(*this));
shared_ptr<NodeTable> nodeTable(new NodeTable(m_ioService, m_alias, bi::address::from_string(listenAddress()), listenPort()));
nodeTable->setEventHandler(new HostNodeTableHandler(*this));
m_nodeTable = nodeTable;
restoreNetwork(&m_restoreNetwork);
clog(NetNote) << "p2p.started id:" << id().abridged();

7
libp2p/Host.h

@ -141,9 +141,12 @@ public:
/// Resets acceptor, socket, and IO service. Called by deallocator.
void stop();
/// @returns if network is running.
bool isStarted() const { return m_run; }
/// @returns if network has been started.
bool isStarted() const { return isWorking(); }
/// @returns if network is started and interactive.
bool haveNetwork() const { return m_run && !!m_nodeTable; }
NodeId id() const { return m_alias.pub(); }
/// Validates and starts peer session, taking ownership of _io. Disconnects and returns false upon error.

2
libwebthree/WebThree.cpp

@ -74,7 +74,7 @@ WebThreeDirect::~WebThreeDirect()
void WebThreeDirect::setNetworkPreferences(p2p::NetworkPreferences const& _n, bool _dropPeers)
{
auto had = haveNetwork();
auto had = isNetworkStarted();
if (had)
stopNetwork();
m_net.setNetworkPreferences(_n, _dropPeers);

2
libwebthree/WebThree.h

@ -163,7 +163,7 @@ public:
/// Sets the ideal number of peers.
void setIdealPeerCount(size_t _n) override;
bool haveNetwork() const override { return m_net.isStarted(); }
bool haveNetwork() const override { return m_net.haveNetwork(); }
void setNetworkPreferences(p2p::NetworkPreferences const& _n, bool _dropPeers = false) override;

2
test/peer.cpp

@ -75,7 +75,7 @@ BOOST_AUTO_TEST_CASE(save_nodes)
h->setIdealPeerCount(10);
// starting host is required so listenport is available
h->start();
while (!h->isStarted())
while (!h->haveNetwork())
this_thread::sleep_for(chrono::milliseconds(2));
hosts.push_back(h);
}

Loading…
Cancel
Save