From 2a1ea35f7fb770e24ae7d0a3132ec4a0e0c8ea9a Mon Sep 17 00:00:00 2001 From: subtly Date: Tue, 23 Dec 2014 09:25:36 +0100 Subject: [PATCH] send/receive messages (not yet interepreted) --- libp2p/NodeTable.cpp | 13 ++++++----- libp2p/NodeTable.h | 14 ++++++------ libp2p/UDP.h | 1 - test/net.cpp | 52 +++++++++++++++++++++++++++----------------- 4 files changed, 46 insertions(+), 34 deletions(-) diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index 0bc024b03..b3c31b56b 100644 --- a/libp2p/NodeTable.cpp +++ b/libp2p/NodeTable.cpp @@ -24,9 +24,10 @@ using namespace std; using namespace dev; using namespace dev::p2p; -NodeTable::NodeTable(ba::io_service& _io, uint16_t _port): - m_node(Node(Address(), Public(), bi::udp::endpoint())), - m_socket(new nodeSocket(_io, *this, _port)), +NodeTable::NodeTable(ba::io_service& _io, KeyPair _alias, uint16_t _listenPort): + m_node(Node(_alias.address(), _alias.pub(), bi::udp::endpoint())), + m_secret(_alias.sec()), + m_socket(new NodeSocket(_io, *this, _listenPort)), m_socketPtr(m_socket.get()), m_io(_io), m_bucketRefreshTimer(m_io), @@ -281,7 +282,7 @@ void NodeTable::onReceived(UDPSocketFace*, bi::udp::endpoint const& _from, bytes { RLP rlp(_packet); - clog(NodeTableNote) << "Received message from " << _from.address().to_string() << ":" << _from.port(); + clog(NodeTableNote) << "Received X from " << _from.address().to_string() << ":" << _from.port(); // whenever a pong is received, first check if it's in m_evictions, if so, remove it Guard l(x_evictions); @@ -322,10 +323,10 @@ void NodeTable::doCheckEvictions(boost::system::error_code const& _ec) void NodeTable::doRefreshBuckets(boost::system::error_code const& _ec) { - clog(NodeTableNote) << "refreshing buckets"; if (_ec) return; - + + clog(NodeTableNote) << "refreshing buckets"; bool connected = m_socketPtr->isOpen(); bool refreshed = false; if (connected) diff --git a/libp2p/NodeTable.h b/libp2p/NodeTable.h index b29cffe7d..a27811b0b 100644 --- a/libp2p/NodeTable.h +++ b/libp2p/NodeTable.h @@ -141,9 +141,9 @@ struct Neighbors: RLPXDatagram */ class NodeTable: UDPSocketEvents, public std::enable_shared_from_this { - using nodeSocket = UDPSocket; - using timePoint = std::chrono::steady_clock::time_point; - using EvictionTimeout = std::pair,Address>; + using NodeSocket = UDPSocket; + using TimePoint = std::chrono::steady_clock::time_point; + using EvictionTimeout = std::pair,Address>; struct NodeDefaultEndpoint { @@ -179,7 +179,7 @@ class NodeTable: UDPSocketEvents, public std::enable_shared_from_this struct NodeBucket { unsigned distance; - timePoint modified; + TimePoint modified; std::list> nodes; }; @@ -206,7 +206,7 @@ public: static unsigned dist(Address const& _a, Address const& _b) { u160 d = _a ^ _b; unsigned ret; for (ret = 0; d >>= 1; ++ret) {}; return ret; } - NodeTable(ba::io_service& _io, uint16_t _port = s_defaultPort); + NodeTable(ba::io_service& _io, KeyPair _alias, uint16_t _port = s_defaultPort); ~NodeTable(); void join(); @@ -268,8 +268,8 @@ protected: Mutex x_evictions; std::deque m_evictions; ///< Eviction timeouts. - std::shared_ptr m_socket; ///< Shared pointer for our UDPSocket; ASIO requires shared_ptr. - nodeSocket* m_socketPtr; ///< Set to m_socket.get(). + std::shared_ptr m_socket; ///< Shared pointer for our UDPSocket; ASIO requires shared_ptr. + NodeSocket* m_socketPtr; ///< Set to m_socket.get(). ba::io_service& m_io; ///< Used by bucket refresh timer. boost::asio::deadline_timer m_bucketRefreshTimer; ///< Timer which schedules and enacts bucket refresh. boost::asio::deadline_timer m_evictionCheckTimer; ///< Timer for handling node evictions. diff --git a/libp2p/UDP.h b/libp2p/UDP.h index 878c21906..0db491272 100644 --- a/libp2p/UDP.h +++ b/libp2p/UDP.h @@ -166,7 +166,6 @@ bool UDPSocket::send(UDPDatagram const& _datagram) Guard l(x_sendQ); sendQ.push_back(_datagram); - clog(NoteChannel) << "qued datagram"; if (sendQ.size() == 1) doWrite(); diff --git a/test/net.cpp b/test/net.cpp index 886e17a47..d3aaae53d 100644 --- a/test/net.cpp +++ b/test/net.cpp @@ -49,21 +49,10 @@ protected: struct TestNodeTable: public NodeTable { - void generateTestNodes(int _count = 10) - { - asserts(_count < 1000); - static uint16_t s_basePort = 30500; - - m_testNodes.clear(); - for (auto i = 0; i < _count; i++) - m_testNodes.push_back(make_pair(KeyPair::create(),s_basePort++)); - } - std::vector> m_testNodes; // keypair and port - /// Constructor using NodeTable::NodeTable; - void setup() + void setup(std::vector> const& _testNodes) { /// Phase 1 test: populate with pings /// Phase 2 test: pre-populate *expected* ping-responses, send pings @@ -72,8 +61,7 @@ struct TestNodeTable: public NodeTable uint16_t ourPort = 30300; bi::udp::endpoint ourEndpoint(ourIp, ourPort); - generateTestNodes(); - for (auto& n: m_testNodes) + for (auto& n: _testNodes) ping(bi::udp::endpoint(ourIp, n.second)); // wait 1ms between each send @@ -94,8 +82,32 @@ struct TestNodeTable: public NodeTable */ struct TestNodeTableHost: public TestHost { - TestNodeTableHost(): nodeTable(new TestNodeTable(m_io)) {}; + TestNodeTableHost(): m_alias(KeyPair::create()), nodeTable(new TestNodeTable(m_io, m_alias)) {}; + + void generateTestNodes(int _count = 10) + { + asserts(_count < 1000); + static uint16_t s_basePort = 30500; + + m_testNodes.clear(); + for (auto i = 0; i < _count; i++) + { + KeyPair k = KeyPair::create(); + m_testNodes.push_back(make_pair(k,s_basePort+i)); + testNodes.push_back(make_shared(m_io,k,s_basePort+i)); + } + } + std::vector> m_testNodes; // keypair and port + + void setup() + { + generateTestNodes(); + nodeTable->setup(m_testNodes); + } + + KeyPair m_alias; shared_ptr nodeTable; + std::vector> testNodes; }; class TestUDPSocket: UDPSocketEvents, public TestHost @@ -113,11 +125,11 @@ public: BOOST_AUTO_TEST_CASE(kademlia) { -// TestNodeTableHost node; -// node.start(); -// node.nodeTable->join(); // ideally, joining with empty node table logs warning we can check for -// node.nodeTable->setup(); -// sleep(1); + TestNodeTableHost node; + node.start(); + node.nodeTable->join(); // ideally, joining with empty node table logs warning we can check for + node.setup(); + sleep(1); } BOOST_AUTO_TEST_CASE(test_txrx_one)