From 43c7aff79e4ea4a88313fcc81f3281b304d95f0d Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Sat, 1 Oct 2016 12:08:22 +0200 Subject: All CMix communication is now wrapped in a CMixMessage. --- node/nextnode.cpp | 5 ----- node/nextnode.hpp | 25 +++++++++++++++++++++++-- node/node.cpp | 15 ++------------- 3 files changed, 25 insertions(+), 20 deletions(-) (limited to 'node') diff --git a/node/nextnode.cpp b/node/nextnode.cpp index f6c4454..8c5b7eb 100644 --- a/node/nextnode.cpp +++ b/node/nextnode.cpp @@ -8,11 +8,6 @@ NextNode::NextNode(tcp::socket&& socket) : client(std::move(socket)) {} -void NextNode::send(std::string message) -{ - client.send(message); -} - void NextNode::async_connect(std::string next_host, std::string next_port, std::function on_connect) { client.async_connect(next_host, next_port, on_connect); diff --git a/node/nextnode.hpp b/node/nextnode.hpp index 6550445..9601ab5 100644 --- a/node/nextnode.hpp +++ b/node/nextnode.hpp @@ -2,12 +2,28 @@ #include "client.hpp" +#include "cmix.pb.h" + #include /*! * \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(T,F,NAME) \ +inline void message_setter(cmix_proto::CMixMessage& m, cmix_proto::T##NAME const& v) { \ + *m.mutable_##F##NAME() = v; \ +} \ + +MESSAGE_SETTER(I,i,nitialization) + +#undef MESSAGE_SETTER + /*! * \brief The NextNode class represents the next node in the network, will only be sent to. */ @@ -23,9 +39,14 @@ public: /*! * \brief send - * \param message is the string that is send to the next node. + * \param v The CMixMessage type we try to send and first have to wrap in a CMixMessage. */ - void send(std::string message); + template + void send(T v) { + cmix_proto::CMixMessage m; + message_setter(m, v); + client.send(m.SerializeAsString()); + } /*! * \brief async_connect diff --git a/node/node.cpp b/node/node.cpp index 761419b..53dd30c 100644 --- a/node/node.cpp +++ b/node/node.cpp @@ -53,13 +53,7 @@ void Node::start_initialisation() { cmix_proto::Initialization init; init.set_public_share(keypair.pub, keypair.pub_len); - cmix_proto::CMixMessage message; - *message.mutable_initialization() = init; - - std::string message_str; - message.SerializeToString(&message_str); - - next_node.send(message_str); + next_node.send(init); for(auto&& client : clients) { client.receive(std::bind(&Node::handle_message, this, std::placeholders::_1)); @@ -118,12 +112,7 @@ void Node::handle_initialization(const cmix_proto::Initialization& init) cmix_proto::Initialization init; init.set_public_share(new_shared.data(), new_shared.size()); - cmix_proto::CMixMessage message; - *message.mutable_initialization() = init; - - std::string message_str; - message.SerializeToString(&message_str); - next_node.send(message_str); + next_node.send(init); mpz_clear(shared); mpz_clear(my_share); -- cgit v1.2.3-70-g09d2