#include "cmixclient.hpp" void CMixClient::key_exchange(int i) { BOOST_LOG_TRIVIAL(trace) << "Sending KeyExchange for node: " << i; cmix_proto::KeyExchange ke; ke.set_public_key(keypair.pub, keypair.pub_len); network_connections.at(i).async_send(ke); network_connections.at(i).async_receive([i, this](cmix_proto::CMixMessage message) { handle_message(i, message); }); } void CMixClient::initialize_connections() { network_connections.reserve(network_details.size()); data.resize(network_details.size()); for(int i = 0; i < network_details.size(); ++i) { auto handler = [this, i]() mutable { cmix_proto::ImAClient imaclient; imaclient.set_id("A"); BOOST_LOG_TRIVIAL(trace) << "sending imaclient to node: " << i; network_connections.at(i).async_send(imaclient); key_exchange(i); }; network_connections.emplace_back(boost::asio::ip::tcp::socket(io_service)); network_connections.back().async_connect(network_details[i].host, network_details[i].port, handler); } } void CMixClient::handle_key_exchange(int node_id, cmix_proto::KeyExchange const& ke) { data.at(node_id).shared_value = api.derive_shared_key(keypair, reinterpret_cast(ke.public_key().c_str()), false); cmix_proto::Bye bye; network_connections.at(node_id).async_send(bye); } void CMixClient::handle_message(int node_id, cmix_proto::CMixMessage message) { switch(message.contents_case()) { case cmix_proto::CMixMessage::ContentsCase::kKeyexchange: { BOOST_LOG_TRIVIAL(trace) << "handling keyexchange"; handle_key_exchange(node_id, *message.mutable_keyexchange()); return; } case cmix_proto::CMixMessage::ContentsCase::kBye: { BOOST_LOG_TRIVIAL(trace) << "handling bye"; network_connections.at(node_id).close(); if(std::all_of(network_connections.begin(), network_connections.end(), [](SenderReceiver const& c) { return c.is_open(); })) { break; } else { return; } } default: { BOOST_LOG_TRIVIAL(error) << "Received unknown message"; } } io_service.stop(); } CMixClient::CMixClient(std::vector details) : io_service() , network_details(details) , network_connections() , api(get_implementation()) , keypair(api.create_key_pair()) { initialize_connections(); } CMixClient::~CMixClient() { api.free_key_pair(keypair); } void CMixClient::run() { io_service.run(); }