From cffc27417a37fc2d737f69b19ea2895ca5a2a2c9 Mon Sep 17 00:00:00 2001 From: subtly Date: Fri, 26 Dec 2014 05:38:27 +0100 Subject: [PATCH] inline << operator, mute logging --- libp2p/NodeTable.cpp | 16 +++++++++++----- libp2p/NodeTable.h | 4 ++-- test/net.cpp | 14 +++++++------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index 64aabc2bb..4cae6af0b 100644 --- a/libp2p/NodeTable.cpp +++ b/libp2p/NodeTable.cpp @@ -247,12 +247,18 @@ void NodeTable::noteNode(Public const& _pubk, bi::udp::endpoint const& _endpoint { node.reset(new NodeEntry(m_node, id, _pubk, _endpoint)); m_nodes[id] = node; +// clog(NodeTableMessageSummary) << "Adding node to cache: " << id; } else + { node = n->second; +// clog(NodeTableMessageSummary) << "Found node in cache: " << id; + } } - noteNode(node); + // todo: why is this necessary? + if (!!node) + noteNode(node); } void NodeTable::noteNode(std::shared_ptr _n) @@ -335,7 +341,7 @@ void NodeTable::onReceived(UDPSocketFace*, bi::udp::endpoint const& _from, bytes switch (itemCount) { case 1: { - clog(NodeTableMessageSummary) << "Received Pong from " << _from.address().to_string() << ":" << _from.port(); +// clog(NodeTableMessageSummary) << "Received Pong from " << _from.address().to_string() << ":" << _from.port(); Pong in = Pong::fromBytesConstRef(_from, rlpBytes); // whenever a pong is received, first check if it's in m_evictions @@ -347,13 +353,13 @@ void NodeTable::onReceived(UDPSocketFace*, bi::udp::endpoint const& _from, bytes if (rlp[0].isList()) { Neighbors in = Neighbors::fromBytesConstRef(_from, rlpBytes); - clog(NodeTableMessageSummary) << "Received " << in.nodes.size() << " Neighbors from " << _from.address().to_string() << ":" << _from.port(); +// clog(NodeTableMessageSummary) << "Received " << in.nodes.size() << " Neighbors from " << _from.address().to_string() << ":" << _from.port(); for (auto n: in.nodes) noteNode(n.node, bi::udp::endpoint(bi::address::from_string(n.ipAddress), n.port)); } else { - clog(NodeTableMessageSummary) << "Received FindNode from " << _from.address().to_string() << ":" << _from.port(); +// clog(NodeTableMessageSummary) << "Received FindNode from " << _from.address().to_string() << ":" << _from.port(); FindNode in = FindNode::fromBytesConstRef(_from, rlpBytes); std::vector> nearest = findNearest(in.target); @@ -369,7 +375,7 @@ void NodeTable::onReceived(UDPSocketFace*, bi::udp::endpoint const& _from, bytes case 3: { - clog(NodeTableMessageSummary) << "Received PingNode from " << _from.address().to_string() << ":" << _from.port(); +// clog(NodeTableMessageSummary) << "Received PingNode from " << _from.address().to_string() << ":" << _from.port(); PingNode in = PingNode::fromBytesConstRef(_from, rlpBytes); Pong p(_from); diff --git a/libp2p/NodeTable.h b/libp2p/NodeTable.h index d8e886c31..32a700d73 100644 --- a/libp2p/NodeTable.h +++ b/libp2p/NodeTable.h @@ -197,7 +197,7 @@ protected: boost::asio::deadline_timer m_evictionCheckTimer; ///< Timer for handling node evictions. }; -std::ostream& operator<<(std::ostream& _out, NodeTable const& _nodeTable) +inline std::ostream& operator<<(std::ostream& _out, NodeTable const& _nodeTable) { _out << _nodeTable.root().address() << "\t" << "0\t" << _nodeTable.root().endpoint.udp.address() << ":" << _nodeTable.root().endpoint.udp.port() << std::endl; auto s = _nodeTable.state(); @@ -205,7 +205,7 @@ std::ostream& operator<<(std::ostream& _out, NodeTable const& _nodeTable) _out << n.address() << "\t" << n.distance << "\t" << n.endpoint.udp.address() << ":" << n.endpoint.udp.port() << std::endl; return _out; } - + /** * Ping packet: Check if node is alive. * PingNode is cached and regenerated after expiration - t, where t is timeout. diff --git a/test/net.cpp b/test/net.cpp index 4c45475f3..d9b5d1579 100644 --- a/test/net.cpp +++ b/test/net.cpp @@ -53,14 +53,14 @@ struct TestNodeTable: public NodeTable /// Constructor using NodeTable::NodeTable; - static std::vector> createTestNodes(int _count = 16) + static std::vector> createTestNodes(unsigned _count) { std::vector> ret; asserts(_count < 1000); static uint16_t s_basePort = 30500; ret.clear(); - for (auto i = 0; i < _count; i++) + for (unsigned i = 0; i < _count; i++) { KeyPair k = KeyPair::create(); ret.push_back(make_pair(k,s_basePort+i)); @@ -75,7 +75,7 @@ struct TestNodeTable: public NodeTable for (auto& n: _testNodes) { ping(bi::udp::endpoint(ourIp, n.second)); - this_thread::sleep_for(chrono::milliseconds(5)); + this_thread::sleep_for(chrono::milliseconds(2)); } } @@ -104,7 +104,7 @@ struct TestNodeTable: public NodeTable */ struct TestNodeTableHost: public TestHost { - TestNodeTableHost(): m_alias(KeyPair::create()), nodeTable(new TestNodeTable(m_io, m_alias)), testNodes(TestNodeTable::createTestNodes()) {}; + TestNodeTableHost(unsigned _count = 8): m_alias(KeyPair::create()), nodeTable(new TestNodeTable(m_io, m_alias)), testNodes(TestNodeTable::createTestNodes(_count)) {}; ~TestNodeTableHost() { m_io.stop(); stopWorking(); } void setup() { for (auto n: testNodes) nodeTables.push_back(make_shared(m_io,n.first,n.second)); } @@ -135,7 +135,7 @@ public: BOOST_AUTO_TEST_CASE(test_neighbors_packet) { KeyPair k = KeyPair::create(); - std::vector> testNodes(TestNodeTable::createTestNodes()); + std::vector> testNodes(TestNodeTable::createTestNodes(16)); bi::udp::endpoint to(boost::asio::ip::address::from_string("127.0.0.1"), 30000); Neighbors out(to); @@ -171,7 +171,7 @@ BOOST_AUTO_TEST_CASE(test_findnode_neighbors) BOOST_AUTO_TEST_CASE(kademlia) { - TestNodeTableHost node; + TestNodeTableHost node(12); node.start(); node.nodeTable->join(); // ideally, joining with empty node table logs warning we can check for node.setup(); @@ -179,7 +179,7 @@ BOOST_AUTO_TEST_CASE(kademlia) clog << "NodeTable:\n" << *node.nodeTable.get() << endl; node.pingAll(); - this_thread::sleep_for(chrono::milliseconds(4000)); + this_thread::sleep_for(chrono::milliseconds(1000)); clog << "NodeTable:\n" << *node.nodeTable.get() << endl; node.nodeTable->reset();