aboutsummaryrefslogtreecommitdiff
path: root/source/lib/control
diff options
context:
space:
mode:
Diffstat (limited to 'source/lib/control')
-rw-r--r--source/lib/control/CMakeLists.txt43
-rw-r--r--source/lib/control/include/wanda/control/commander.hpp67
-rw-r--r--source/lib/control/include/wanda/control/connection.hpp116
-rw-r--r--source/lib/control/include/wanda/control/interface.hpp97
-rw-r--r--source/lib/control/src/commander.cpp80
-rw-r--r--source/lib/control/src/connection.cpp130
-rw-r--r--source/lib/control/src/interface.cpp160
7 files changed, 0 insertions, 693 deletions
diff --git a/source/lib/control/CMakeLists.txt b/source/lib/control/CMakeLists.txt
deleted file mode 100644
index b4a4b02..0000000
--- a/source/lib/control/CMakeLists.txt
+++ /dev/null
@@ -1,43 +0,0 @@
-cmake_path(GET CMAKE_CURRENT_SOURCE_DIR STEM LIB_NAME)
-
-file(GLOB_RECURSE LIB_HEADERS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" CONFIGURE_DEPENDS "**/*.hpp")
-file(GLOB_RECURSE LIB_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" CONFIGURE_DEPENDS "**/*.cpp")
-
-add_library("wanda-${LIB_NAME}" ${WANDA_LIBRARY_TYPE}
- ${LIB_SOURCES}
-)
-
-target_sources("wanda-${LIB_NAME}" INTERFACE
- FILE_SET HEADERS
- FILES ${LIB_HEADERS}
- BASE_DIRS "include"
-)
-
-target_include_directories("wanda-${LIB_NAME}" PUBLIC
- "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
-)
-
-target_include_directories("wanda-${LIB_NAME}" SYSTEM PUBLIC
- "$<INSTALL_INTERFACE:include>"
-)
-
-target_compile_features("wanda-${LIB_NAME}" PUBLIC
- "cxx_std_20"
-)
-
-target_link_libraries("wanda-${LIB_NAME}" PUBLIC
- "wanda::meta"
- "wanda::proto"
- "wanda::system"
-
- "Boost::headers"
- "spdlog::spdlog_header_only"
-)
-
-if(NOT WANDA_APPLICATIONS_ONLY)
- install(TARGETS "wanda-${LIB_NAME}"
- FILE_SET HEADERS
- )
-endif()
-
-add_library("wanda::${LIB_NAME}" ALIAS "wanda-${LIB_NAME}")
diff --git a/source/lib/control/include/wanda/control/commander.hpp b/source/lib/control/include/wanda/control/commander.hpp
deleted file mode 100644
index d7ca73d..0000000
--- a/source/lib/control/include/wanda/control/commander.hpp
+++ /dev/null
@@ -1,67 +0,0 @@
-#ifndef WANDA_CONTROL_COMMANDER_HPP
-#define WANDA_CONTROL_COMMANDER_HPP
-
-#include "wanda/control/connection.hpp"
-#include "wanda/proto/command.hpp"
-#include "wanda/proto/message.hpp"
-
-#include <boost/asio/io_context.hpp>
-#include <boost/asio/io_service.hpp>
-
-#include <filesystem>
-#include <memory>
-#include <optional>
-#include <string>
-#include <vector>
-
-namespace wanda::control
-{
- /**
- * @brief The remote control client
- *
- */
- struct commander : connection::listener
- {
- /**
- * @brief The interface to be implemented by remote control listeners
- */
- struct listener
- {
- virtual void on_connected(commander & commander){};
- virtual void on_response(commander & commander, std::string response){};
- virtual void on_error(commander & commander, std::string error){};
- };
-
- /**
- * @brief Construct a new commander
- */
- commander(boost::asio::io_context & service, std::filesystem::path socket, listener & listener);
-
- /**
- * @brief Start communication with the remote daemon endpoint
- */
- void start();
-
- /**
- * @brief Stop communication with the remote daemon endpoint
- */
- void stop();
-
- /**
- * @brief Send a command to the remote daemon endpoint
- */
- void send(proto::command command);
-
- void on_error(connection::pointer connection, std::error_code error) override;
- void on_received(connection::pointer connection, proto::message message) override;
-
- private:
- boost::asio::io_context & m_service;
- wanda::control::connection::protocol::endpoint m_endpoint;
- wanda::control::connection::protocol::socket m_socket;
- wanda::control::connection::pointer m_connection;
- listener & m_listener;
- };
-
-} // namespace wanda::control
-#endif \ No newline at end of file
diff --git a/source/lib/control/include/wanda/control/connection.hpp b/source/lib/control/include/wanda/control/connection.hpp
deleted file mode 100644
index 0284fd7..0000000
--- a/source/lib/control/include/wanda/control/connection.hpp
+++ /dev/null
@@ -1,116 +0,0 @@
-#ifndef WANDA_CONTROL_CONNECTION_HPP
-#define WANDA_CONTROL_CONNECTION_HPP
-
-#include "wanda/meta/keyed.hpp"
-#include "wanda/proto/message.hpp"
-
-#include <boost/asio/local/stream_protocol.hpp>
-#include <boost/asio/streambuf.hpp>
-
-#include <istream>
-#include <memory>
-#include <ostream>
-#include <set>
-#include <string>
-#include <system_error>
-
-namespace wanda::control
-{
- /**
- * @brief A connection to a remote control endpoint
- */
- struct connection : meta::keyed<connection>, std::enable_shared_from_this<connection>
- {
- using protocol = boost::asio::local::stream_protocol;
- using pointer = std::shared_ptr<connection>;
-
- /**
- * @brief The interface to be implemented by the control interface listener
- */
- struct listener
- {
- virtual void on_close(pointer connection) {}
- virtual void on_received(pointer connection, proto::message message) {}
- virtual void on_error(pointer connection, std::error_code) {}
- };
-
- /**
- * @brief A enum to describe different connection states
- */
- enum struct state : std::underlying_type_t<std::byte>
- {
- unknown, //< Connection is in an unknown state
- fresh, //< Connection is freshly created but not established
- established, //< Connection has been established
- };
-
- /**
- * @internal
- * @brief Construct a new control connection object
- *
- * @note This constructor is keyed on a private key type so it can only be constructed using the
- * #wanda::make_connection factory
- */
- connection(key, protocol::socket socket);
-
- /**
- * @brief Add the given listener to this control connection's listener set
- *
- * @returns <code>true</code> iff. the listener was not already in the listener set
- */
- bool add(listener * listener);
-
- /**
- * @brief Remove the given listener from this control connection's listener set
- *
- * @return <code>true</code> iff. the listener was previously registered with this control connection
- */
- bool remove(listener * listener);
-
- /**
- * @brief Start I/O processing for this control connection
- */
- void start();
-
- /**
- * @brief Close this control connection
- */
- void close();
-
- /**
- * @brief Send the given message to the remote endpoint
- */
- void send(proto::message message);
-
- /**
- * @brief Set the connection state to the provided state
- */
- void update(state state);
-
- /**
- * @brief Get the current connection state
- */
- state current_state() const;
-
- private:
- friend pointer make_connection(protocol::socket && socket);
-
- void perform_read();
-
- protocol::socket m_socket;
- boost::asio::streambuf m_in{};
- boost::asio::streambuf m_out{};
- std::istream m_input{&m_in};
- std::ostream m_output{&m_out};
- std::set<listener *> m_listeners{};
- state m_state{};
- };
-
- /**
- * @brief Create a new control connection
- */
- connection::pointer make_connection(connection::protocol::socket && socket);
-
-} // namespace wanda::control
-
-#endif \ No newline at end of file
diff --git a/source/lib/control/include/wanda/control/interface.hpp b/source/lib/control/include/wanda/control/interface.hpp
deleted file mode 100644
index e895b68..0000000
--- a/source/lib/control/include/wanda/control/interface.hpp
+++ /dev/null
@@ -1,97 +0,0 @@
-/**
- * @file interface.hpp
- * @author Felix Morgner (felix.morgner@gmail.com)
- * @since 1.0.0
- */
-
-#ifndef WANDA_CONTROL_INTERFACE_HPP
-#define WANDA_CONTROL_INTERFACE_HPP
-
-#include "wanda/control/connection.hpp"
-#include "wanda/meta/keyed.hpp"
-#include "wanda/proto/command.hpp"
-
-#include <boost/asio/local/stream_protocol.hpp>
-#include <boost/asio/io_context.hpp>
-#include <spdlog/spdlog.h>
-
-#include <cstddef>
-#include <filesystem>
-#include <istream>
-#include <map>
-#include <memory>
-#include <set>
-#include <string>
-#include <type_traits>
-
-namespace wanda::control
-{
- /**
- * @brief An RAII type to delete a socket file upon destruction
- */
- struct socket_deleter
- {
- ~socket_deleter();
-
- std::filesystem::path path;
- };
-
- /**
- * @brief The daemon control interface
- */
- struct interface : connection::listener, meta::keyed<interface>, std::enable_shared_from_this<interface>
- {
- using protocol = boost::asio::local::stream_protocol;
- using pointer = std::shared_ptr<interface>;
-
- /**
- * @brief The interface to be implemented by the control interface listener
- */
- struct listener
- {
- virtual void on_received(interface & interface, proto::command command){};
- };
-
- /**
- * @internal
- * @brief Construct a new control interface object
- *
- * @note This constructor is keyed on a private key type so it can only be constructed using the #wanda::make_interface factory
- */
- interface(key, boost::asio::io_context & service, protocol::endpoint endpoint, listener & listener);
-
- /**
- * @brief Start handling of controller connections
- */
- std::error_code start();
-
- /**
- * @brief Stop the control interface
- */
- std::error_code shutdown();
-
- void on_close(connection::pointer connection) override;
- void on_received(connection::pointer connection, proto::message message) override;
-
- private:
- void perform_accept();
-
- friend pointer make_interface(boost::asio::io_context & service, std::filesystem::path file, interface::listener & listener);
-
- boost::asio::io_context & m_service;
- protocol::endpoint m_endpoint;
- protocol::socket m_socket;
- protocol::acceptor m_acceptor;
- listener & m_listener;
- socket_deleter m_deleter{m_endpoint.path()};
- std::set<connection::pointer> m_connections;
- };
-
- /**
- * @brief A factory to create new #interface instances
- */
- interface::pointer make_interface(boost::asio::io_context & service, std::filesystem::path socket, interface::listener & listener);
-
-} // namespace wanda::control
-
-#endif \ No newline at end of file
diff --git a/source/lib/control/src/commander.cpp b/source/lib/control/src/commander.cpp
deleted file mode 100644
index 4490bb7..0000000
--- a/source/lib/control/src/commander.cpp
+++ /dev/null
@@ -1,80 +0,0 @@
-#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 <boost/asio/io_context.hpp>
-#include <spdlog/fmt/ostr.h>
-
-namespace wanda::control
-{
- commander::commander(boost::asio::io_context & 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<std::string>(message) + '\'');
- }
- }
-
-} // namespace wanda::control \ No newline at end of file
diff --git a/source/lib/control/src/connection.cpp b/source/lib/control/src/connection.cpp
deleted file mode 100644
index 97f41dd..0000000
--- a/source/lib/control/src/connection.cpp
+++ /dev/null
@@ -1,130 +0,0 @@
-#include "wanda/control/connection.hpp"
-
-#include "wanda/proto/message.hpp"
-
-#include <boost/asio/completion_condition.hpp>
-#include <boost/asio/read_until.hpp>
-#include <boost/asio/write.hpp>
-#include <boost/system/detail/error_code.hpp>
-#include <boost/system/error_code.hpp>
-
-#include <limits>
-
-namespace wanda::control
-{
- connection::pointer make_connection(connection::protocol::socket && socket)
- {
- return std::make_shared<connection>(connection::key{}, std::move(socket));
- }
-
- connection::connection(connection::key key, connection::protocol::socket socket)
- : keyed{key}
- , m_socket{std::move(socket)}
- {
- }
-
- bool connection::add(listener * listener)
- {
- auto [_, inserted] = m_listeners.insert(listener);
- return inserted;
- }
-
- bool connection::remove(listener * listener)
- {
- return m_listeners.erase(listener);
- }
-
- void connection::start()
- {
- if (m_state == state::unknown)
- {
- m_state = state::fresh;
- perform_read();
- }
- }
-
- void connection::send(proto::message message)
- {
- m_output << message << '\n';
- boost::asio::async_write(m_socket, m_out, boost::asio::transfer_exactly(message.size() + 1), [that = shared_from_this(), this](auto const & error, auto const length) {
- if (error)
- {
- // TODO: Handle error
- }
- else
- {
- m_out.consume(length);
- }
- });
- }
-
- void connection::close()
- {
- auto error = boost::system::error_code{};
-
- if (m_socket.cancel(error), error)
- {
- for (auto & listener : m_listeners)
- {
- listener->on_error(shared_from_this(), error);
- }
- }
-
- if (m_socket.close(error), error)
- {
- for (auto & listener : m_listeners)
- {
- listener->on_error(shared_from_this(), error);
- }
- }
-
- for (auto & listener : m_listeners)
- {
- listener->on_close(shared_from_this());
- }
- m_listeners.clear();
- }
-
- void connection::update(state state)
- {
- m_state = state;
- }
-
- connection::state connection::current_state() const
- {
- return m_state;
- }
-
- void connection::perform_read()
- {
- boost::asio::async_read_until(m_socket, m_in, '\n', [that = shared_from_this(), this](auto const & error, auto const length) {
- if (error)
- {
- for (auto & listener : m_listeners)
- {
- listener->on_error(shared_from_this(), error);
- }
- close();
- }
- else
- {
- auto msg = proto::message{};
- m_input >> msg;
- if (!m_input)
- {
- m_input.ignore(std::numeric_limits<std::streamsize>::max());
- m_input.clear();
- }
- else
- {
- for (auto & listener : m_listeners)
- {
- listener->on_received(shared_from_this(), msg);
- }
- }
- perform_read();
- }
- });
- }
-
-} // namespace wanda::control \ No newline at end of file
diff --git a/source/lib/control/src/interface.cpp b/source/lib/control/src/interface.cpp
deleted file mode 100644
index 3ebc55a..0000000
--- a/source/lib/control/src/interface.cpp
+++ /dev/null
@@ -1,160 +0,0 @@
-#include "wanda/control/interface.hpp"
-
-#include "wanda/proto/version.hpp"
-#include "wanda/std_ext/optional.hpp"
-#include "wanda/system/logging.hpp"
-
-#include <spdlog/fmt/ostr.h>
-#include <boost/system/error_code.hpp>
-#include <boost/asio/error.hpp>
-#include <unistd.h>
-
-#include <algorithm>
-#include <iterator>
-#include <string>
-#include <system_error>
-#include <utility>
-
-namespace wanda::control
-{
- // 'socket_deleter' implementation
-
- socket_deleter::~socket_deleter()
- {
- if (std::filesystem::exists(path))
- {
- std::filesystem::remove(path);
- }
- }
-
- // 'interface' implementation
-
- interface::interface(interface::key key, boost::asio::io_context & service, interface::protocol::endpoint endpoint, listener & listener)
- : keyed{key}
- , m_service{service}
- , m_endpoint{std::move(endpoint)}
- , m_socket{m_service}
- , m_acceptor{m_service}
- , m_listener{listener}
- {
- }
-
- std::error_code interface::start()
- {
- if (auto error = boost::system::error_code{}; m_acceptor.open(m_endpoint.protocol(), error), error)
- {
- return error;
- }
-
- if (auto error = boost::system::error_code{}; m_acceptor.bind(m_endpoint, error), error)
- {
- return error;
- }
-
- if (auto error = boost::system::error_code{}; m_acceptor.listen(128, error), error)
- {
- return error;
- }
- else
- {
- perform_accept();
- return error;
- }
- }
-
- std::error_code interface::shutdown()
- {
- for (auto & connection : m_connections)
- {
- connection->close();
- }
-
- auto error = boost::system::error_code{};
- return m_acceptor.close(error), error;
- }
-
- void interface::perform_accept()
- {
- m_acceptor.async_accept(m_socket, [that = shared_from_this(), this](auto const & error) {
- if (error && error != boost::asio::error::operation_aborted)
- {
- system::get_logger()->error("failed to accept connection because '{}'", error.message());
- }
- else
- {
- system::get_logger()->info("new incoming controller connection");
- auto [connection, inserted] = m_connections.insert(make_connection(std::move(m_socket)));
- if (inserted)
- {
- (*connection)->add(this);
- (*connection)->start();
- }
- perform_accept();
- }
- });
- }
-
- void interface::on_close(connection::pointer connection)
- {
- if (static_cast<char>(connection->current_state()) >= static_cast<char>(connection::state::established))
- {
- system::get_logger()->info("controller connection closed");
- }
- else
- {
- system::get_logger()->info("controller connection aborted before it could be established");
- }
- m_connections.erase(connection);
- }
-
- void interface::on_received(connection::pointer connection, proto::message message)
- {
- using namespace wanda::std_ext;
-
- if (m_connections.find(connection) == m_connections.cend())
- {
- system::get_logger()->error("received message from an unknown connection");
- return;
- }
-
- if (message.source != proto::message_source_controller)
- {
- system::get_logger()->error("received a deamon message");
- return;
- }
-
- if (auto state = connection->current_state(); message.command == proto::message_command_hello && state == connection::state::fresh)
- {
- system::get_logger()->info("controller connection established");
- if (message.argument.has_value())
- {
- system::get_logger()->info("remote controller version '{}'", *message.argument);
- }
- connection->send({proto::message_source_daemon, proto::message_command_hello, proto::version});
- connection->update(connection::state::established);
- }
- else
- {
- with(make_command(message), [&](auto const & command) {
- m_listener.on_received(*this, command);
- }) ||
- [&] { system::get_logger()->warn("ignoring unknown message '{}'", message); };
- }
- }
-
- interface::pointer make_interface(boost::asio::io_context & service, std::filesystem::path socket, interface::listener & listener)
- {
- if (std::filesystem::exists(socket))
- {
- system::get_logger()->error("socket '{}' exists", socket.native());
- return {};
- }
-
- interface::protocol::endpoint endpoint
- {
- socket.string()
- };
- return std::make_shared<interface>(interface::key{}, service, std::move(endpoint), listener);
- }
-
-} // namespace wanda::control \ No newline at end of file