diff options
Diffstat (limited to 'client')
| -rw-r--r-- | client/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | client/cmixclient.cpp | 38 | ||||
| -rw-r--r-- | client/cmixclient.hpp | 9 | ||||
| -rw-r--r-- | client/node.cpp | 23 | ||||
| -rw-r--r-- | client/node.hpp | 72 |
5 files changed, 15 insertions, 128 deletions
diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index e04bf6a..489a136 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -3,7 +3,6 @@ find_package(Boost COMPONENTS system program_options REQUIRED) add_executable(client main.cpp cmixclient.hpp cmixclient.cpp - node.hpp node.cpp ) if(WIN32) diff --git a/client/cmixclient.cpp b/client/cmixclient.cpp index 195c5e0..1ffd904 100644 --- a/client/cmixclient.cpp +++ b/client/cmixclient.cpp @@ -2,15 +2,16 @@ #include "cmixclient.hpp" void CMixClient::key_exchange(int i) { - shared_keys.resize(network_connections.size()); + BOOST_LOG_TRIVIAL(trace) << "Sending KeyExchange for node: " << i; + shared_keys.resize(network_details.size()); cmix_proto::KeyExchange ke; ke.set_public_key(keypair.pub, keypair.pub_len); - network_connections[i].send(ke); + network_connections[i].async_send(ke); cmix_proto::Bye bye; - network_connections[i].send(bye); + network_connections[i].async_send(bye); } void CMixClient::initialize_connections() { @@ -20,25 +21,14 @@ void CMixClient::initialize_connections() { auto handler = [this, i]() mutable { cmix_proto::ImAClient imaclient; BOOST_LOG_TRIVIAL(trace) << "sending imaclient to node: " << i; - network_connections.at(i).send(imaclient); + 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); - - } -} - -cmix_proto::CMixMessage CMixClient::parse_cmix_message(std::vector<uint8_t> const& buffer) -{ - cmix_proto::CMixMessage message; - if(!message.ParseFromArray(buffer.data(), buffer.size())) { - BOOST_LOG_TRIVIAL(error) << "Received something which was not a CMixMessage"; - throw std::runtime_error("Network communication was disrupted in a unrecoverable way."); } - return message; } void CMixClient::handle_key_exchange(int node_id, cmix_proto::KeyExchange const& ke) @@ -46,19 +36,8 @@ void CMixClient::handle_key_exchange(int node_id, cmix_proto::KeyExchange const& shared_keys[node_id] = api.derive_shared_key(keypair, reinterpret_cast<uint8_t const*>(ke.public_key().c_str()), false); } -void CMixClient::handle_message(int node_id, std::vector<uint8_t> const& message_buffer) +void CMixClient::handle_message(int node_id, cmix_proto::CMixMessage message) { - cmix_proto::CMixMessage message; - try { - message = parse_cmix_message(message_buffer); - } catch(std::runtime_error const& e) { - for(auto&& connection : network_connections) { - connection.close(); - } - io_service.stop(); - return; - } - switch(message.contents_case()) { case cmix_proto::CMixMessage::ContentsCase::kKeyexchange: { handle_key_exchange(node_id, *message.mutable_keyexchange()); @@ -85,6 +64,11 @@ CMixClient::CMixClient(std::vector<NodeDetails> details) initialize_connections(); } +CMixClient::~CMixClient() +{ + api.free_key_pair(keypair); +} + void CMixClient::run() { io_service.run(); } diff --git a/client/cmixclient.hpp b/client/cmixclient.hpp index 5c6405a..056e2b3 100644 --- a/client/cmixclient.hpp +++ b/client/cmixclient.hpp @@ -1,6 +1,6 @@ #pragma once -#include "node.hpp" +#include "protobufclient.hpp" #include "api.h" #include "logging.hpp" @@ -21,7 +21,7 @@ class CMixClient { boost::asio::io_service io_service; std::vector<NodeDetails> network_details; - std::vector<Node> network_connections; + std::vector<ProtobufClient<SendReceive>> network_connections; Api api; KeyPair keypair; @@ -31,14 +31,13 @@ class CMixClient { void initialize_connections(); - cmix_proto::CMixMessage parse_cmix_message(std::vector<uint8_t> const& buffer); - void handle_key_exchange(int node_id, cmix_proto::KeyExchange const& ke); - void handle_message(int node_id, std::vector<uint8_t> const& message_buffer); + void handle_message(int node_id, cmix_proto::CMixMessage message); public: CMixClient(std::vector<NodeDetails> details); + ~CMixClient(); void run(); };
\ No newline at end of file diff --git a/client/node.cpp b/client/node.cpp deleted file mode 100644 index 46375f4..0000000 --- a/client/node.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "node.hpp" - -#include "logging.hpp" - -using namespace boost::asio::ip; - -Node::Node(tcp::socket&& socket) -: client(std::move(socket)) -{} - -void Node::async_connect(std::string next_host, std::string next_port, std::function<void ()> on_connect) -{ - client.async_connect(next_host, next_port, on_connect); -} - -void Node::receive(std::function<void (const std::vector<uint8_t>&)> receive_handler) { - client.receive(receive_handler); -} - -void Node::close() -{ - client.close(); -} diff --git a/client/node.hpp b/client/node.hpp deleted file mode 100644 index afcf56a..0000000 --- a/client/node.hpp +++ /dev/null @@ -1,72 +0,0 @@ -#pragma once - -#include "client.hpp" - -#include "cmix.pb.h" - -#include <boost/asio/ip/tcp.hpp> - -/*! - * \file - */ - -/*! - * MESSAGE_SETTER is a boilerplate macro that generates a setter function for our CMix - * protobuf messages, This because there are seperate functions for each to type to use. - * And there seems no way to solve this using templates. - */ -#define MESSAGE_SETTER(TYPE, NAME) \ -inline void message_setter(cmix_proto::CMixMessage& m, cmix_proto::TYPE const& v) { \ - *m.mutable_##NAME() = v; \ -} \ - -MESSAGE_SETTER(ImAClient, imaclient) -MESSAGE_SETTER(Bye, bye) -MESSAGE_SETTER(KeyExchange, keyexchange) - -#undef MESSAGE_SETTER - -/*! - * \brief The Node class represents a node in the network. - */ -class Node -{ - Client client; -public: - /*! - * \brief Node - * \param socket an rvalue reference to the socket it takes ownership and uses to communicate with the next node in the network. - */ - Node(boost::asio::ip::tcp::socket&& socket); - - /*! - * \brief send - * \param v The CMixMessage type we try to send and first have to wrap in a CMixMessage. - */ - template <typename T> - void send(T v) { - cmix_proto::CMixMessage m; - message_setter(m, v); - client.send(m.SerializeAsString()); - } - - /*! - * \brief receive - * \param receive_handler - */ - void receive(std::function<void(std::vector<uint8_t> const&)> receive_handler); - - /*! - * \brief async_connect - * \param next_host The host of the next node. - * \param next_port The port of the next node. - * \param on_connect The callback to call when the connect was succesfull. - */ - void async_connect(std::string next_host, std::string next_port, std::function<void()> on_connect); - - /*! - * \brief close This function closes the underlying socket connection. - */ - void close(); -}; - |
