From 375799fa79d1af76f33299acc20a11a167a021f8 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 17 Aug 2023 12:32:50 +0200 Subject: project: restructure libraries and build env --- source/lib/control/src/commander.cpp | 79 ++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 source/lib/control/src/commander.cpp (limited to 'source/lib/control/src/commander.cpp') diff --git a/source/lib/control/src/commander.cpp b/source/lib/control/src/commander.cpp new file mode 100644 index 0000000..3db2c59 --- /dev/null +++ b/source/lib/control/src/commander.cpp @@ -0,0 +1,79 @@ +#include "wanda/control/commander.hpp" + +#include "wanda/proto/message.hpp" +#include "wanda/proto/version.hpp" +#include "wanda/std_ext/optional.hpp" +#include "wanda/system/logging.hpp" + +#include + +namespace wanda::control +{ + 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) + { + system::get_logger()->error("error while connecting to control interface: '{}'", error.message()); + } + else + { + system::get_logger()->info("establishing connection to wanda deamon"); + m_connection = wanda::control::make_connection(std::move(m_socket)); + m_connection->add(this); + m_connection->start(); + m_connection->send({proto::message_source_controller, proto::message_command_hello, proto::version}); + } + }); + } + + void commander::stop() + { + system::get_logger()->info("closing control connection"); + m_connection->close(); + } + + void commander::send(proto::command command) + { + using namespace wanda::std_ext; + + if (!m_connection || m_connection->current_state() != connection::state::established) + { + system::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); }) || + [&] { system::get_logger()->error("unknown command"); }; + } + + void commander::on_error(connection::pointer connection, std::error_code error) + { + system::get_logger()->error("control interface communication error: '{}'", error.message()); + } + + void commander::on_received(connection::pointer connection, proto::message message) + { + if (auto state = connection->current_state(); message.command == "HELLO" && state == connection::state::fresh) + { + system::get_logger()->info("connection to wanda deamon successfully established"); + connection->update(connection::state::established); + m_listener.on_connected(*this); + } + else + { + system::get_logger()->error("unexpected message: '{}'", message); + m_listener.on_error(*this, "unexpected message '" + static_cast(message) + '\''); + } + } + +} // namespace wanda::control \ No newline at end of file -- cgit v1.2.3