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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
#include "commander.hpp"
#include "logging.hpp"
#include "message.hpp"
#include "optional.hpp"
#include <spdlog/fmt/ostr.h>
namespace wanda
{
commander::commander(asio::io_service & service, std::filesystem::path socket, listener & listener)
: m_service{service}
, m_endpoint{socket.string()}
, m_socket{service}
, m_listener{listener}
{
}
void commander::start()
{
m_socket.async_connect(m_endpoint, [&](auto const & error) {
if (error)
{
get_logger()->error("error while connecting to control interface: '{}'", error.message());
}
else
{
get_logger()->info("establishing connection to wanda deamon");
m_connection = wanda::make_control_connection(std::move(m_socket));
m_connection->add(this);
m_connection->start();
m_connection->send({message_source_controller, message_command_hello, message_argument_hello});
}
});
}
void commander::stop()
{
get_logger()->info("closing control connection");
m_connection->close();
}
void commander::send(command command)
{
using namespace wanda::std_ext;
if (!m_connection || m_connection->current_state() != control_connection::state::established)
{
get_logger()->error("tried to send command without an established connection");
m_listener.on_error(*this, "tried to send command without an established connection");
return;
}
with(command.message(), [&](auto const & message) { m_connection->send(message); }) ||
[&] { get_logger()->error("unknown command"); };
}
void commander::on_error(control_connection::pointer connection, std::error_code error)
{
get_logger()->error("control interface communication error: '{}'", error.message());
}
void commander::on_received(wanda::control_connection::pointer connection, message message)
{
if (auto state = connection->current_state(); message.command == "HELLO" && state == control_connection::state::fresh)
{
get_logger()->info("connection to wanda deamon successfully established");
connection->update(control_connection::state::established);
m_listener.on_connected(*this);
}
else
{
get_logger()->error("unexpected message: '{}'", message);
m_listener.on_error(*this, "unexpected message '" + static_cast<std::string>(message) + '\'');
}
}
} // namespace wanda
|