aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/client/client.hpp
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2019-11-30 01:38:08 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2019-12-01 05:23:11 +0300
commit90bd600c5025ede4db99122f13dfb07b27de46ae (patch)
tree0b581b43e5fcb54114c6c373352ed6ab5fcd61dc /client/client.hpp
downloadmath-server-90bd600c5025ede4db99122f13dfb07b27de46ae.tar.gz
math-server-90bd600c5025ede4db99122f13dfb07b27de46ae.zip
initial commit
Diffstat (limited to '')
-rw-r--r--client/client.hpp52
1 files changed, 52 insertions, 0 deletions
diff --git a/client/client.hpp b/client/client.hpp
new file mode 100644
index 0000000..139ebb2
--- /dev/null
+++ b/client/client.hpp
@@ -0,0 +1,52 @@
+#pragma once
+
+#include "input.hpp"
+#include "settings.hpp"
+#include "transport.hpp"
+
+#include <iostream>
+#include <string>
+#include <utility>
+
+namespace math::client {
+
+class Client {
+public:
+ explicit Client(const Settings& settings)
+ : Client{make_input_reader(settings), make_transport(settings)}
+ { }
+
+ Client(input::ReaderPtr&& input_reader, TransportPtr&& transport)
+ : m_input_reader{std::move(input_reader)}
+ , m_transport{std::move(transport)}
+ { }
+
+ void run() {
+ m_input_reader->for_each_input([this] (const std::string& input) {
+ m_transport->send_query(input, [] (const std::string& reply) {
+ std::cout << reply << '\n';
+ });
+ return true;
+ });
+ }
+
+private:
+ static input::ReaderPtr make_input_reader(const Settings& settings) {
+ if (settings.input_from_string()) {
+ return input::make_string_reader(settings.m_input);
+ }
+ if (settings.input_from_files()) {
+ return input::make_file_reader(settings.m_files);
+ }
+ return input::make_console_reader();
+ }
+
+ static TransportPtr make_transport(const Settings& settings) {
+ return make_blocking_network_transport(settings.m_host, settings.m_port);
+ }
+
+ const input::ReaderPtr m_input_reader;
+ TransportPtr m_transport;
+};
+
+}