From d5e53a4c7564dc785b65c92fc393c991cab7a5b3 Mon Sep 17 00:00:00 2001 From: faumaray Date: Sun, 14 Jun 2026 00:41:56 +0300 Subject: [PATCH] =?UTF-8?q?Add=20Fsm::Orchestrator=20=E2=80=94=20owns=20th?= =?UTF-8?q?e=20registry,=20launches/supervises=20FSMs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Orchestrator (Fsm module, which already depends on Service): holds a ServiceRegistry, exposes services() for providers, start_services() to run lifecycle on_start hooks, and launch(args...) which constructs an FSM, injects the registry as its context (set_services), starts it, and supervises it via a type-erased FsmHandle (status/stop). stop_all() stops every supervised FSM then the services (balanced exactly once against start_services so the explicit call + destructor don't double-fire on_stop). FSMs resolve dependencies purely by interface through the injected registry, so the orchestrator wires graphs of services + state machines with no concrete coupling. 3 tests: service injection into a launched FSM, launch + supervise + stop_all with a worker-backed FSM hitting a shared service, and service start/stop lifecycle ordering. 963 green. Co-Authored-By: Claude Fable 5 --- include/Void/Fsm/Orchestrator.hpp | 172 ++++++++++++++++++++++ tests/standalone/Service/Orchestrator.cpp | 157 ++++++++++++++++++++ 2 files changed, 329 insertions(+) create mode 100644 include/Void/Fsm/Orchestrator.hpp create mode 100644 tests/standalone/Service/Orchestrator.cpp diff --git a/include/Void/Fsm/Orchestrator.hpp b/include/Void/Fsm/Orchestrator.hpp new file mode 100644 index 0000000..52e8707 --- /dev/null +++ b/include/Void/Fsm/Orchestrator.hpp @@ -0,0 +1,172 @@ +#pragma once + +#include +#include + +#include +#include +#include +#include +#include + +namespace Void::Fsm +{ + namespace Detail + { + // Type-erased supervised FSM. Models wrap a concrete Machine + // subclass; the orchestrator only needs lifecycle + status. + class FsmConcept + { + public: + + virtual ~FsmConcept() = default; + virtual void stop() = 0; + [[nodiscard]] virtual Status status() const = 0; + }; + + template class FsmModel final : public FsmConcept + { + public: + + explicit FsmModel(std::unique_ptr fsm) + : m_fsm(std::move(fsm)) + { + } + + void stop() override { m_fsm->stop(); } + [[nodiscard]] Status status() const override + { + return m_fsm->status(); + } + + [[nodiscard]] F& get() noexcept { return *m_fsm; } + + private: + + std::unique_ptr m_fsm; + }; + } // namespace Detail + + /// A non-owning supervision handle to a launched FSM: query status and + /// stop it. The Orchestrator owns the FSM's lifetime. + class FsmHandle + { + public: + + FsmHandle() = default; + + void stop() const + { + if (m_impl) { + m_impl->stop(); + } + } + + [[nodiscard]] Status status() const + { + return m_impl ? m_impl->status() : Status::Idle; + } + + [[nodiscard]] bool finished() const + { + const auto s = status(); + return s == Status::Succeeded || s == Status::Failed; + } + + [[nodiscard]] explicit operator bool() const noexcept + { + return m_impl != nullptr; + } + + private: + + friend class Orchestrator; + explicit FsmHandle(std::shared_ptr impl) + : m_impl(std::move(impl)) + { + } + + std::shared_ptr m_impl; + }; + + // ── Orchestrator ────────────────────────────────────────────────────── + // Owns a ServiceRegistry and supervises a set of FSM instances. Each + // launched FSM is injected with the registry as its context, so its + // states resolve dependencies by interface (no concrete coupling), and + // is started immediately. stop_all() winds everything down. + class Orchestrator + { + public: + + Orchestrator() = default; + ~Orchestrator() { stop_all(); } + + Orchestrator(const Orchestrator&) = delete; + Orchestrator& operator=(const Orchestrator&) = delete; + + [[nodiscard]] Service::ServiceRegistry& services() noexcept + { + return m_registry; + } + + /// Call on_start() on lifecycle services. Call before launching + /// FSMs that expect started services. + void start_services() + { + m_registry.start_all(); + m_services_started = true; + m_services_stopped = false; + } + + /// Construct F, inject the registry, start it, and supervise. + /// F must derive Fsm::Machine<...> (provides set_services / + /// start / stop / status). + template + FsmHandle launch(Args&&... args) + { + auto fsm = std::make_unique(std::forward(args)...); + fsm->set_services(m_registry); + fsm->start(); + auto model = + std::make_shared>(std::move(fsm)); + std::lock_guard lock{m_mutex}; + m_running.push_back(model); + return FsmHandle{model}; + } + + /// Stop and release every supervised FSM, then stop services. + void stop_all() + { + std::vector> running; + { + std::lock_guard lock{m_mutex}; + running.swap(m_running); + } + for (auto& m : running) { + m->stop(); + } + running.clear(); + // Balance start_services() exactly once (stop_all may run + // explicitly and again from the destructor). + if (m_services_started && !m_services_stopped) { + m_registry.stop_all(); + m_services_stopped = true; + } + } + + [[nodiscard]] std::size_t running_count() const + { + std::lock_guard lock{m_mutex}; + return m_running.size(); + } + + private: + + Service::ServiceRegistry m_registry; + mutable std::mutex m_mutex; + std::vector> m_running; + bool m_services_started{false}; + bool m_services_stopped{false}; + }; + +} // namespace Void::Fsm diff --git a/tests/standalone/Service/Orchestrator.cpp b/tests/standalone/Service/Orchestrator.cpp new file mode 100644 index 0000000..13c2f70 --- /dev/null +++ b/tests/standalone/Service/Orchestrator.cpp @@ -0,0 +1,157 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +using Void::Coro::Task; +using Void::Fsm::MachineOptions; +using Void::Fsm::Orchestrator; +using Void::Fsm::Status; +using Void::Fsm::Step; + +namespace +{ + // A service the FSMs resolve by interface. + class ICounter + { + public: + + virtual ~ICounter() = default; + virtual int inc() = 0; + [[nodiscard]] virtual int value() const = 0; + }; + + class AtomicCounter final : public Void::Service::IService, public ICounter + { + public: + + explicit AtomicCounter(std::vector* log) : m_log(log) + { + } + [[nodiscard]] std::string_view service_name() const override + { + return "Counter"; + } + void on_start() override + { + if (m_log) { + m_log->push_back("counter-start"); + } + } + void on_stop() override + { + if (m_log) { + m_log->push_back("counter-stop"); + } + } + int inc() override + { + return m_v.fetch_add(1, std::memory_order_relaxed) + 1; + } + [[nodiscard]] int value() const override + { + return m_v.load(std::memory_order_relaxed); + } + + std::atomic m_v{0}; + std::vector* m_log; + }; + + // An FSM that bumps the counter service N times then succeeds. + class Worker : public Void::Fsm::Machine + { + public: + + int times; + + struct Run : State { + Task run() override + { + auto& m = static_cast(machine()); + int last = 0; + for (int i = 0; i < m.times; ++i) { + last = service().inc(); + } + co_return succeed(last); + } + }; + + explicit Worker(int t) + : Void::Fsm::Machine(MachineOptions{.own_worker = false}), + times(t) + { + initial(); + } + }; +} // namespace + +Void::Testing::Test orchestrator_injects_services_into_fsm() +{ + Orchestrator orch; + orch.services().provide(std::make_shared(nullptr)); + + // An embedder FSM driven directly, using the orchestrator's registry — + // confirms the same injection path launch() uses. + Worker w{3}; + w.set_services(orch.services()); + Void::Testing::expect_eq(*w.run_blocking(), 3); +} + +Void::Testing::Test orchestrator_launches_and_supervises() +{ + // Worker-backed FSM so the orchestrator's start() spins a worker and + // the FSM runs to completion on its own. + struct AsyncWorker : Void::Fsm::Machine { + struct Run : State { + Task run() override + { + int last = 0; + for (int i = 0; i < 5; ++i) { + last = service().inc(); + } + co_return succeed(last); + } + }; + AsyncWorker() { initial(); } // own worker + }; + + Orchestrator orch; + auto counter = std::make_shared(nullptr); + orch.services().provide(counter); + + auto h = orch.launch(); + Void::Testing::expect(static_cast(h)); + Void::Testing::expect_eq(orch.running_count(), std::size_t{1}); + + for (int i = 0; i < 200 && !h.finished(); ++i) { + std::this_thread::sleep_for(std::chrono::milliseconds{2}); + } + Void::Testing::expect(h.status() == Status::Succeeded); + Void::Testing::expect_eq(counter->value(), 5); + + orch.stop_all(); + Void::Testing::expect_eq(orch.running_count(), std::size_t{0}); +} + +Void::Testing::Test orchestrator_service_lifecycle() +{ + std::vector log; + { + Orchestrator orch; + orch.services().provide( + std::make_shared(&log)); + orch.start_services(); + orch.stop_all(); + } + Void::Testing::expect_eq(log.size(), std::size_t{2}); + Void::Testing::expect_eq(log[0], std::string{"counter-start"}); + Void::Testing::expect_eq(log[1], std::string{"counter-stop"}); +}