91 lines
2.5 KiB
C++
91 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Result.hpp>
|
|
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
namespace ReloadableHost
|
|
{
|
|
enum class ControlStatus : std::uint16_t {
|
|
Ok = 0,
|
|
Unauthenticated = 1,
|
|
InvalidArgument = 2,
|
|
NotFound = 3,
|
|
FailedPrecondition = 4,
|
|
Overloaded = 5,
|
|
Timeout = 6,
|
|
Internal = 7,
|
|
Unimplemented = 8
|
|
};
|
|
|
|
struct ControlRequest {
|
|
std::string command;
|
|
std::string candidate;
|
|
std::string token;
|
|
std::chrono::milliseconds deadline{0};
|
|
std::string payload;
|
|
};
|
|
|
|
struct ControlReply {
|
|
ControlStatus status{ControlStatus::Ok};
|
|
std::string body;
|
|
std::uint64_t transaction_id{0};
|
|
};
|
|
|
|
struct ControlLimits {
|
|
std::size_t max_request_bytes{64ULL * 1024ULL};
|
|
std::size_t max_reply_bytes{64ULL * 1024ULL};
|
|
};
|
|
|
|
/// Host operations exposed over the authenticated local control endpoint.
|
|
class ControlHandler
|
|
{
|
|
public:
|
|
|
|
ControlHandler() = default;
|
|
ControlHandler(const ControlHandler&) = delete;
|
|
ControlHandler& operator=(const ControlHandler&) = delete;
|
|
virtual ~ControlHandler() = default;
|
|
|
|
[[nodiscard]] virtual Void::Result::Result<std::string> inspect() = 0;
|
|
[[nodiscard]] virtual Void::Result::Result<bool> health() const = 0;
|
|
[[nodiscard]] virtual Void::Result::Result<void>
|
|
validate_candidate(std::string_view candidate) = 0;
|
|
[[nodiscard]] virtual Void::Result::Result<std::uint64_t>
|
|
reload(std::string_view candidate,
|
|
std::chrono::milliseconds deadline) = 0;
|
|
[[nodiscard]] virtual Void::Result::Result<void>
|
|
drain(std::chrono::milliseconds deadline) = 0;
|
|
[[nodiscard]] virtual Void::Result::Result<void> shutdown() = 0;
|
|
[[nodiscard]] virtual Void::Result::Result<void> recover() = 0;
|
|
};
|
|
|
|
/// Authenticated, bounded control dispatcher.
|
|
///
|
|
/// The transport (local IPC) owns framing; this validates the token in
|
|
/// constant time, enforces request/reply bounds, and maps host errors to
|
|
/// stable statuses. An empty configured token authorizes nothing.
|
|
class ControlEndpoint
|
|
{
|
|
public:
|
|
|
|
ControlEndpoint(std::string token, ControlHandler& handler,
|
|
ControlLimits limits = {});
|
|
|
|
[[nodiscard]] ControlReply handle(const ControlRequest& request);
|
|
|
|
private:
|
|
|
|
[[nodiscard]] bool authorized(std::string_view token) const noexcept;
|
|
[[nodiscard]] ControlReply dispatch(const ControlRequest& request);
|
|
|
|
std::string m_token;
|
|
ControlHandler& m_handler;
|
|
ControlLimits m_limits;
|
|
};
|
|
} // namespace ReloadableHost
|