aboutsummaryrefslogtreecommitdiff
path: root/src/commander.cpp
blob: 762cd8317d21ed0f7e4aa20caa191a0717a1e6b1 (plain)
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
#include "commander.hpp"
#include "message.hpp"

#include <iostream>

namespace wanda
{
commander::commander(boost::asio::io_service &service, std::filesystem::path socket)
    : m_service{service},
      m_endpoint{socket.string()},
      m_socket{service}
{
}

void commander::start()
{
    std::clog << "[commander::start] Starting commander on socket '" << m_endpoint.path() << "'\n";
    m_socket.async_connect(m_endpoint, [&](auto const &error) {
        if (error)
        {
            std::cerr << "[commander::start] error occured: " << error.message() << '\n';
        }
        else
        {
            std::clog << "[commander::start] Control connection established\n";
            m_connection = wanda::make_control_connection(std::move(m_socket));
            m_connection->add(this);
            m_connection->start();
            send({"C", "HELLO", "1.0.0"});
        }
    });
}

void commander::send(message message)
{
    if (m_connection)
    {
        std::clog << "[commander::send] sending message: " << message << '\n';
        m_connection->send(std::move(message));
    }
}

void commander::on_error(wanda::control_connection::pointer connection, boost::system::error_code error)
{
    std::cerr << "[commander::on_error] error occured: " << error.message() << '\n';
}

void commander::on_received(wanda::control_connection::pointer connection, message message)
{
    std::clog << "[commander::on_receive] Received message: " << message << '\n';
}

} // namespace wanda