Files
Library/tests/plugins/ServicePluginV1.cpp
T
faumarayandClaude Fable 5 7b6cd7341a Plugins provide services: hot-reload + state-saving via the registry
Re-expresses the old FSM-plugin hot-plug through the service layer,
reusing the unchanged Plugin module (PluginManager, v2 serialize/
deserialize ABI, FileWatcher, atomic-rename install).

Service/PluginService.hpp: PluginServiceBinding<I> resolves a plugin's
exported service (extern "C" void_plugin_service(instance) -> IService*)
via PluginManager::raw_symbol and registers it under interface I. The
registry entry pins the plugin instance (and its mapped .so) so in-flight
callers never see a freed object. enable_hot_reload() re-binds on each
reload; the FSMs resolve I through their context, so a running FSM picks
up the new implementation on its next service<I>() call with no code
patched. I must derive IService (single inheritance) so the host recovers
it from IService* by static_cast — no dynamic_cast across RTLD_LOCAL.

Tests: ServicePluginV1/V2 (the plugin object is the service: IPlugin +
ICalc : IService; v2 migrates v1 state via Json::MigrationChain).
FsmServiceHotReload.cpp: an Orchestrator-launched FSM accumulates through
the plugin service while a reloader thread flips v1<->v2 under load —
state survives every migration, the FSM runs to completion, no crash; and
a deterministic reload test proving the implementation swaps (version
1->2) with the running total migrated.

Docs: CLAUDE.md/README updated for the new Fsm, Service, and Orchestrator
(StateMachine row removed). 965 tests green, ASan+UBSan clean, stable
across runs, -Werror clean.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-14 00:47:47 +03:00

117 lines
2.8 KiB
C++

#include "ServiceInterface.hpp"
#include <Void/Data/Reflect.hpp>
#include <Void/Plugin/Plugin.hpp>
#include <atomic>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <string>
// Service plugin, schema v1. The plugin object is the service: it derives
// IPlugin (for the loader) and ICalc (the host interface, which derives
// IService). State (the running total) survives reloads via the v2 state
// ABI, serialized with the reflection JSON serializer.
namespace
{
struct StateV1 {
int total = 0;
};
class CalcPlugin final : public Void::Plugin::IPlugin,
public VoidTest::ICalc
{
public:
// IPlugin
[[nodiscard]] std::string_view name() const noexcept override
{
return "CalcPlugin";
}
// IService
[[nodiscard]] std::string_view service_name() const override
{
return "CalcPlugin";
}
// ICalc
int accumulate(int delta) override
{
return m_total.fetch_add(delta, std::memory_order_relaxed) +
delta;
}
[[nodiscard]] int version() const override { return 1; }
std::atomic<int> m_total{0};
};
} // namespace
template <> struct Void::Json::StateTraits<StateV1> {
static constexpr std::uint32_t version = 1;
};
extern "C" {
void* void_plugin_create() { return new CalcPlugin; }
void void_plugin_destroy(void* p) { delete static_cast<CalcPlugin*>(p); }
Void::Plugin::PluginInfo void_plugin_info()
{
return {
.abi_version = Void::Plugin::ABI_VERSION,
.name = "CalcPlugin",
.plugin_version = "1.0.0",
.description = "Calc service plugin (schema v1)",
};
}
// Hand the host the IService subobject of the plugin's ICalc.
Void::Service::IService* void_plugin_service(void* instance)
{
auto* self = static_cast<CalcPlugin*>(instance);
return static_cast<Void::Service::IService*>(
static_cast<VoidTest::ICalc*>(self));
}
std::uint32_t void_plugin_state_version() { return 1; }
const char* void_plugin_serialize_state(void* instance)
{
const auto* self = static_cast<CalcPlugin*>(instance);
const StateV1 state{.total = self->m_total.load(std::memory_order_relaxed)};
const auto json =
Void::Json::serialize(Void::Json::to_versioned_json(state));
char* out = static_cast<char*>(std::malloc(json.size() + 1));
if (out != nullptr) {
std::memcpy(out, json.c_str(), json.size() + 1);
}
return out;
}
void void_plugin_free_string(const char* s)
{
std::free(const_cast<char*>(s)); // NOLINT
}
int void_plugin_deserialize_state(void* instance, const char* json,
std::uint32_t /*from_version*/)
{
auto* self = static_cast<CalcPlugin*>(instance);
const auto parsed = Void::Json::parse(json);
if (!parsed.has_value()) {
return 1;
}
const auto state = Void::Json::from_versioned_json<StateV1>(*parsed);
if (!state.has_value()) {
return 2;
}
self->m_total.store(state->total, std::memory_order_relaxed);
return 0;
}
} // extern "C"