aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDennis Brentjes <d.brentjes@gmail.com>2016-08-29 15:35:26 +0200
committerDennis Brentjes <d.brentjes@gmail.com>2016-08-29 15:35:26 +0200
commit7097015fa3d2737b2e9438fdba9ec7b8f5630421 (patch)
tree1fc277780216aa9142281458967d94a1cb4fcd1f
parentcffc1ec24d7781130b600a24b9726b5168a7b053 (diff)
downloadcmix-7097015fa3d2737b2e9438fdba9ec7b8f5630421.tar.gz
cmix-7097015fa3d2737b2e9438fdba9ec7b8f5630421.tar.bz2
cmix-7097015fa3d2737b2e9438fdba9ec7b8f5630421.zip
Refactored Client to be more reusable.
Added UserClient and NodeClient classes to handle communication from and to Clients and Nodes respectively.
-rw-r--r--network-handler/CMakeLists.txt3
-rw-r--r--network-handler/client.cpp2
-rw-r--r--network-handler/client.hpp9
-rw-r--r--network-handler/networkhandler.hpp4
-rw-r--r--network-handler/nodeclient.cpp12
-rw-r--r--network-handler/nodeclient.hpp16
-rw-r--r--network-handler/nodemanager.cpp6
-rw-r--r--network-handler/nodemanager.hpp12
-rw-r--r--network-handler/userclient.cpp12
-rw-r--r--network-handler/userclient.hpp15
10 files changed, 88 insertions, 3 deletions
diff --git a/network-handler/CMakeLists.txt b/network-handler/CMakeLists.txt
index 02e02c1..9fe6e99 100644
--- a/network-handler/CMakeLists.txt
+++ b/network-handler/CMakeLists.txt
@@ -6,6 +6,9 @@ add_executable(network-handler
networkhandler.hpp networkhandler.cpp
acceptor.hpp acceptor.cpp
client.hpp client.cpp
+ userclient.hpp userclient.cpp
+ nodeclient.hpp nodeclient.cpp
+ nodemanager.hpp nodemanager.cpp
)
target_compile_options(network-handler
diff --git a/network-handler/client.cpp b/network-handler/client.cpp
index f767f6d..19b6f34 100644
--- a/network-handler/client.cpp
+++ b/network-handler/client.cpp
@@ -20,7 +20,7 @@ void Client::handle_receive(const error_code &ec, size_t read_bytes)
if(!ec) {
std::vector<uint8_t> data(std::istream_iterator<uint8_t>(is), {});
- std::cout << "Received: " << std::string(data.begin(), data.end()) << std::endl;
+ handle_message(data);
receive();
} else {
done();
diff --git a/network-handler/client.hpp b/network-handler/client.hpp
index 716e2cf..fa0c6d6 100644
--- a/network-handler/client.hpp
+++ b/network-handler/client.hpp
@@ -14,6 +14,15 @@ private:
boost::asio::streambuf buffer;
OnDoneFT done;
+ Client() = delete;
+ Client(Client const&) = delete;
+ Client(Client&&) = delete;
+
+ Client& operator=(Client const&) = delete;
+ Client& operator=(Client&&) = delete;
+
+protected:
+ virtual void handle_message(std::vector<uint8_t> message) = 0;
public:
Client(boost::asio::ip::tcp::socket&& socket);
diff --git a/network-handler/networkhandler.hpp b/network-handler/networkhandler.hpp
index 99eb4f9..058f6a9 100644
--- a/network-handler/networkhandler.hpp
+++ b/network-handler/networkhandler.hpp
@@ -1,7 +1,7 @@
#pragma once
#include "acceptor.hpp"
-#include "client.hpp"
+#include "userclient.hpp"
#include <boost/asio/io_service.hpp>
#include <boost/asio/streambuf.hpp>
@@ -26,7 +26,7 @@ class NetworkHandler
Acceptor v4_acceptor;
Acceptor v6_acceptor;
- std::list<Client> clients;
+ std::list<UserClient> clients;
public:
NetworkHandler(ListenSettings const& listen_settings);
diff --git a/network-handler/nodeclient.cpp b/network-handler/nodeclient.cpp
new file mode 100644
index 0000000..8486f4b
--- /dev/null
+++ b/network-handler/nodeclient.cpp
@@ -0,0 +1,12 @@
+#include "nodeclient.hpp"
+
+#include <iostream>
+
+NodeClient::NodeClient(boost::asio::ip::tcp::socket &&socket)
+: Client(std::move(socket))
+{}
+
+void NodeClient::handle_message(std::vector<uint8_t> message)
+{
+ std::cout << std::string(message.begin(), message.end()) << std::endl;
+}
diff --git a/network-handler/nodeclient.hpp b/network-handler/nodeclient.hpp
new file mode 100644
index 0000000..00d8450
--- /dev/null
+++ b/network-handler/nodeclient.hpp
@@ -0,0 +1,16 @@
+#pragma once
+
+#include "client.hpp"
+
+#include <boost/asio/ip/tcp.hpp>
+
+#include <vector>
+
+class NodeClient : public Client
+{
+ virtual void handle_message(std::vector<uint8_t> message) override final;
+
+public:
+ NodeClient(boost::asio::ip::tcp::socket&& socket);
+ virtual ~NodeClient() = default;
+};
diff --git a/network-handler/nodemanager.cpp b/network-handler/nodemanager.cpp
new file mode 100644
index 0000000..198a459
--- /dev/null
+++ b/network-handler/nodemanager.cpp
@@ -0,0 +1,6 @@
+#include "nodemanager.hpp"
+
+NodeManager::NodeManager()
+{
+
+}
diff --git a/network-handler/nodemanager.hpp b/network-handler/nodemanager.hpp
new file mode 100644
index 0000000..e6a42c5
--- /dev/null
+++ b/network-handler/nodemanager.hpp
@@ -0,0 +1,12 @@
+#pragma once
+
+#include "nodeclient.hpp"
+
+#include <vector>
+
+class NodeManager
+{
+ std::vector<NodeClient> clients;
+public:
+ NodeManager();
+};
diff --git a/network-handler/userclient.cpp b/network-handler/userclient.cpp
new file mode 100644
index 0000000..b35a544
--- /dev/null
+++ b/network-handler/userclient.cpp
@@ -0,0 +1,12 @@
+#include "userclient.hpp"
+
+#include <iostream>
+
+void UserClient::handle_message(std::vector<uint8_t> message)
+{
+ std::cout << std::string(message.begin(), message.end());
+}
+
+UserClient::UserClient(boost::asio::ip::tcp::socket&& socket)
+: Client(std::move(socket))
+{}
diff --git a/network-handler/userclient.hpp b/network-handler/userclient.hpp
new file mode 100644
index 0000000..94d5ec0
--- /dev/null
+++ b/network-handler/userclient.hpp
@@ -0,0 +1,15 @@
+#pragma once
+
+#include <boost/asio/ip/tcp.hpp>
+
+#include "client.hpp"
+
+class UserClient : public Client
+{
+ virtual void handle_message(std::vector<uint8_t> message) override final;
+
+public:
+ UserClient(boost::asio::ip::tcp::socket&& socket);
+ virtual ~UserClient() = default;
+
+};