aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/client/client.hpp
blob: 3d68157898d4b0eb6ce1692f0f25fc083519b774 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) 2019 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "math-server" project.
// For details, see https://github.com/egor-tensin/math-server.
// Distributed under the MIT License.

#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;
};

} // namespace math::client