aboutsummaryrefslogtreecommitdiff
path: root/node
diff options
context:
space:
mode:
authorDennis Brentjes <d.brentjes@gmail.com>2016-10-01 12:08:22 +0200
committerDennis Brentjes <d.brentjes@gmail.com>2016-10-01 12:08:22 +0200
commit43c7aff79e4ea4a88313fcc81f3281b304d95f0d (patch)
treed266ada73e9ebb025dcf46eb0eb21cfa604e80b6 /node
parent2969016625c22d9b1e73534f82aed5a4f26b602f (diff)
downloadcmix-43c7aff79e4ea4a88313fcc81f3281b304d95f0d.tar.gz
cmix-43c7aff79e4ea4a88313fcc81f3281b304d95f0d.tar.bz2
cmix-43c7aff79e4ea4a88313fcc81f3281b304d95f0d.zip
All CMix communication is now wrapped in a CMixMessage.
Diffstat (limited to 'node')
-rw-r--r--node/nextnode.cpp5
-rw-r--r--node/nextnode.hpp25
-rw-r--r--node/node.cpp15
3 files changed, 25 insertions, 20 deletions
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<void ()> 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,6 +2,8 @@
#include "client.hpp"
+#include "cmix.pb.h"
+
#include <boost/asio/ip/tcp.hpp>
/*!
@@ -9,6 +11,20 @@
*/
/*!
+ * 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.
*/
class NextNode
@@ -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 <typename T>
+ 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);