#include "node.hpp" #include "logging.hpp" using namespace boost::asio::ip; Node::Node(ListenSettings const& listen_settings, NodeNetworkSettings network_settings) : io_service() , server(io_service, listen_settings, [this](boost::asio::ip::tcp::socket&& socket){accept_handler(std::move(socket));}) , clients() , network_settings(network_settings) , next_node(tcp::socket(io_service)) , api(get_curve25519_implementation()) , keypair(api.create_key_pair()) , network_pub_key() { auto on_connect = [this, network_settings](){ BOOST_LOG_TRIVIAL(trace) << "is first: " << std::boolalpha << network_settings.is_first; if(network_settings.is_first) { start_initialisation(); } }; next_node.connect(network_settings.next_host, network_settings.next_port, on_connect); } Node::~Node() { api.free_key_pair(keypair); } void Node::run() { io_service.run(); } void Node::accept_handler(boost::asio::ip::tcp::socket&& socket) { clients.emplace_back(std::move(socket)); auto it = --clients.end(); clients.back().on_done( [this, it]() { clients.erase(it); } ); } void Node::start_initialisation() { initialization init; init.set_public_share(keypair.pub, keypair.pub_len); BOOST_LOG_TRIVIAL(trace) << "length of keypair.pub: " << keypair.pub_len; std::string message; init.SerializeToString(&message); BOOST_LOG_TRIVIAL(trace) << init.DebugString(); next_node.send(message); auto f = [this](std::vector bytes) { network_pub_key = bytes; if(network_settings.is_first) { start_precomputation(); } }; BOOST_LOG_TRIVIAL(trace) << "number of clients: " << clients.size(); for(auto&& client : clients) { client.receive(f); } } void Node::start_precomputation() { }