aboutsummaryrefslogtreecommitdiff
path: root/client/node.hpp
diff options
context:
space:
mode:
authorDennis Brentjes <d.brentjes@gmail.com>2016-10-10 15:52:14 +0200
committerDennis Brentjes <d.brentjes@gmail.com>2016-10-10 15:52:14 +0200
commitd8e48c32f8435076382543edfafbf81c223f9e87 (patch)
tree575f0f7f452c4c8800e77c7fd79b4354fed8a81c /client/node.hpp
parentfa35c9c3c71243a7f8537e5a81f7a09fa05a382e (diff)
downloadcmix-d8e48c32f8435076382543edfafbf81c223f9e87.tar.gz
cmix-d8e48c32f8435076382543edfafbf81c223f9e87.tar.bz2
cmix-d8e48c32f8435076382543edfafbf81c223f9e87.zip
Added a Client so we can start finishing up the setup phase of cMix.
Diffstat (limited to 'client/node.hpp')
-rw-r--r--client/node.hpp65
1 files changed, 65 insertions, 0 deletions
diff --git a/client/node.hpp b/client/node.hpp
new file mode 100644
index 0000000..3719223
--- /dev/null
+++ b/client/node.hpp
@@ -0,0 +1,65 @@
+#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)
+
+#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 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();
+};
+