Files
Aethera/web_server/src/Graph_Session.cpp
T
2026-08-21 10:16:16 +08:00

128 lines
23 KiB
C++

#include "Graph_Session.hpp" /* 图对象异步会话。 */
#include <asio/co_spawn.hpp>
#include <asio/error_code.hpp>
#include <asio/experimental/concurrent_channel.hpp>
#include <asio/redirect_error.hpp>
#include <asio/strand.hpp>
#include <asio/use_awaitable.hpp>
#include <render_2D/plottable/Plottables.hpp>
#include <render_2D/scene/Render_Scene_2D.hpp>
#include <render_3D/Render_3D.hpp>
#include <algorithm>
#include <array>
#include <atomic>
#include <cmath>
#include <cstring>
#include <mutex>
#include <numbers>
#include <span>
#include <stdexcept>
#include <unordered_map>
#include <utility>
#include <variant>
namespace aethera::web {
namespace {
using namespace render_2d;
using namespace render_3d;
using Scene_2D = Impl<Render_Scene_2D>;
using Frequency_Axis_Object = Impl<Frequency_Axis>;
using Numeric_Axis_Object = Impl<Numeric_Axis>;
using Time_Axis_Object = Impl<Time_Axis>;
template <typename Object, typename... Arguments>
std::unique_ptr<Object> build(Arguments&&... arguments) { typename Object::Builder builder(std::forward<Arguments>(arguments)...); auto result = builder.build(); if (!result) throw std::logic_error("gallery graph dependency graph is invalid"); return std::move(result).value(); }
template <typename Axis>
void configure_axis(Axis* axis, Axis_Orientation orientation, Point_F position, Axis_Pixel_Length length, Size canvas) { axis->template set<&Abs_Axis::Prop::orientation>(orientation); axis->template set<&Abs_Axis::Prop::position>(position); axis->template set<&Abs_Axis::Prop::pixel_length>(length); axis->template set<&Abs_Axis::Prop::canvas_size>(canvas); }
template <typename Integer>
void append_binary(std::string& output, Integer value) { const auto start = output.size(); output.resize(start + sizeof(Integer)); std::memcpy(output.data() + start, &value, sizeof(Integer)); }
std::string encode_frame(Image_View image, std::uint64_t sequence) { std::string output; output.reserve(24 + static_cast<std::size_t>(image.width) * image.height * 4); append_binary(output, std::uint32_t{0x41544852}); append_binary(output, std::uint16_t{1}); append_binary(output, std::uint16_t{}); append_binary(output, static_cast<std::uint32_t>(image.width)); append_binary(output, static_cast<std::uint32_t>(image.height)); append_binary(output, sequence); for (int y = 0; y < image.height; ++y) { const auto* row = reinterpret_cast<const std::uint8_t*>(image.data + static_cast<std::ptrdiff_t>(y) * image.stride); for (int x = 0; x < image.width; ++x) { const auto* pixel = row + x * 4; output.push_back(static_cast<char>(pixel[2])); output.push_back(static_cast<char>(pixel[1])); output.push_back(static_cast<char>(pixel[0])); output.push_back(static_cast<char>(pixel[3])); } } return output; }
std::string encode_frame(const render_3d::Pixel_Frame& frame, std::uint64_t sequence) { std::string output; output.reserve(24 + frame.rgba8.size()); append_binary(output, std::uint32_t{0x41544852}); append_binary(output, std::uint16_t{1}); append_binary(output, std::uint16_t{}); append_binary(output, frame.extent.width); append_binary(output, frame.extent.height); append_binary(output, sequence); output.append(reinterpret_cast<const char*>(frame.rgba8.data()), frame.rgba8.size()); return output; }
struct State_Query { Graph_Session::State_Handler handler{}; };
struct Prop_Query { Graph_Session::Prop_Handler handler{}; };
struct Prop_Patch { nlohmann::json patch{}; Graph_Session::Prop_Handler handler{}; };
using Session_Event = std::variant<Graph_Event, State_Query, Prop_Query, Prop_Patch>;
struct Plot_2D {
std::unique_ptr<Scene_2D> scene{}; /* 最终二维 Scene。 */
std::unique_ptr<Frequency_Axis_Object> frequency{}; /* 频率轴;不用时仍为空。 */
std::unique_ptr<Numeric_Axis_Object> horizontal{}; /* 星座图水平数值轴。 */
std::unique_ptr<Numeric_Axis_Object> vertical{}; /* 功率或星座图垂直轴。 */
std::unique_ptr<Time_Axis_Object> time{}; /* 时间轴;不用时为空。 */
std::unique_ptr<Root> plot{}; /* 具体 Plottable 的唯一所有权。 */
std::function<void(double)> update{}; /* 根据浏览器时钟更新权威 Prop。 */
std::function<nlohmann::json()> state{}; /* 直接遍历具体图完整 State 的即时 HTTP 文档。 */
std::function<nlohmann::json()> prop{};
std::function<nlohmann::json(const nlohmann::json&)> patch_prop{};
};
template <typename Definition, auto... Members, typename Object>
void bind_prop_api(Plot_2D& plot, Object* object) {
using Prop = typename Definition::Prop;
plot.prop = [object] { return adminive::model_to_json<nlohmann::json>(static_cast<const Prop&>(object->template read_prop<typename Definition::Base_Tag>()), true); };
plot.state = [object] { return adminive::model_to_json<nlohmann::json>(static_cast<const typename Definition::State&>(object->template read_state<typename Definition::Base_Tag>()), true); };
plot.patch_prop = [object](const nlohmann::json& patch) { Prop updated = static_cast<const Prop&>(object->template read_prop<typename Definition::Base_Tag>()); const auto result = adminive::apply_frontend_patch<nlohmann::json>(updated, patch); if (!result.success) return result.to_json<nlohmann::json>(); ([&] { if (object->template get<Members>() != updated.*Members) object->template set<Members>(updated.*Members); }(), ...); auto output = result.to_json<nlohmann::json>(); output["prop"] = adminive::model_to_json<nlohmann::json>(static_cast<const Prop&>(object->template read_prop<typename Definition::Base_Tag>()), true); return output; };
}
Plot_2D make_plot_2d(std::string_view id) {
Plot_2D result;
const Size canvas{720, 420};
result.scene = build<Scene_2D>();
result.scene->set<&Render_Scene_2D::Prop::viewport>(canvas);
result.scene->set<&Render_Scene_2D::Prop::background>(Color{7, 13, 24, 255});
result.scene->activate_view();
auto make_frequency = [&] { result.frequency = build<Frequency_Axis_Object>(); configure_axis(result.frequency.get(), Axis_Orientation::horizontal, Point_F{64.0, 370.0}, 620.0, canvas); result.frequency->set<&Numeric_Axis::Prop::coordinate_range>(Axis_Range{0.0, 100.0}); };
auto make_vertical = [&](Axis_Range range) {
result.vertical = build<Numeric_Axis_Object>();
configure_axis(result.vertical.get(), Axis_Orientation::vertical, {64.0, 370.0}, -320.0, canvas); result.vertical->set<&Numeric_Axis::Prop::coordinate_range>(range);
};
if (id == "spectrum") {
make_frequency(); make_vertical({-110.0, 0.0}); auto object = build<Impl<Spectrum>>(result.scene.get(), result.frequency.get(), result.vertical.get()); object->set<&Spectrum::Prop::frequency_range>(Axis_Range{0.0, 100.0}); object->set<&Spectrum::Prop::max_hold_visible>(true); auto* raw = object.get(); raw->mark_dirty<Prepare_Data_Tag>(); raw->mark_dirty<Paint_Tag>(); bind_prop_api<Spectrum, &Spectrum::Prop::center_frequency, &Spectrum::Prop::partition_count, &Spectrum::Prop::max_hold_visible, &Spectrum::Prop::min_hold_visible, &Spectrum::Prop::max_marker_visible, &Spectrum::Prop::min_marker_visible, &Spectrum::Prop::sweep_region_visible, &Spectrum::Prop::visible_range_only, &Spectrum::Prop::frequency_range, &Spectrum::Prop::sweep_frequency_range, &Spectrum::Prop::partition_mode, &Spectrum::Prop::interpolation_mode, &Spectrum::Prop::max_brush, &Spectrum::Prop::current_brush, &Spectrum::Prop::min_brush, &Spectrum::Prop::max_pen, &Spectrum::Prop::current_pen, &Spectrum::Prop::min_pen, &Spectrum::Prop::selected_marker_pen, &Spectrum::Prop::marker_pen, &Spectrum::Prop::middle_frequency_pen, &Spectrum::Prop::sweep_region_brush, &Spectrum::Prop::custom_markers, &Spectrum::Prop::selected_marker>(result, raw); result.update = [raw](double time) { std::array<double, 256> samples{}; for (std::size_t i = 0; i < samples.size(); ++i) { const double x = static_cast<double>(i) / samples.size(); samples[i] = -92.0 + 54.0 * std::exp(-180.0 * std::pow(x - 0.28 - 0.03 * std::sin(time * 0.001), 2.0)) + 42.0 * std::exp(-260.0 * std::pow(x - 0.68, 2.0)) + 2.5 * std::sin(i * 0.31 + time * 0.004); } raw->update_samples(samples); }; result.plot = std::move(object);
} else if (id == "frequency_trace") {
result.time = build<Time_Axis_Object>(); configure_axis(result.time.get(), Axis_Orientation::horizontal, {64.0, 370.0}, 620.0, canvas); make_vertical({-1.2, 1.2}); auto object = build<Impl<Frequency_Trace>>(result.scene.get(), result.time.get(), result.vertical.get()); auto* raw = object.get(); raw->mark_dirty<Prepare_Data_Tag>(); raw->mark_dirty<Paint_Tag>(); bind_prop_api<Frequency_Trace, &Frequency_Trace::Prop::partition_count, &Frequency_Trace::Prop::pen, &Frequency_Trace::Prop::partition_mode, &Frequency_Trace::Prop::samples>(result, raw); auto tick = std::make_shared<std::uint64_t>(); result.update = [raw, tick](double time) { raw->append_sample((*tick)++, std::sin(time * 0.0025) * 0.8 + std::sin(time * 0.0007) * 0.2); }; result.plot = std::move(object);
} else if (id == "sweep_spectrum") {
make_frequency(); make_vertical({-110.0, 0.0}); auto object = build<Impl<Sweep_Spectrum>>(result.scene.get(), result.frequency.get(), result.vertical.get()); object->set<&Sweep_Spectrum::Prop::frequency_range>(Axis_Range{0.0, 100.0}); auto* raw = object.get(); raw->mark_dirty<Prepare_Data_Tag>(); raw->mark_dirty<Paint_Tag>(); bind_prop_api<Sweep_Spectrum, &Sweep_Spectrum::Prop::bins_per_block, &Sweep_Spectrum::Prop::block_count, &Sweep_Spectrum::Prop::partition_count, &Sweep_Spectrum::Prop::visible_range_only, &Sweep_Spectrum::Prop::frequency_range, &Sweep_Spectrum::Prop::partition_mode, &Sweep_Spectrum::Prop::pen, &Sweep_Spectrum::Prop::current_frequency_pen, &Sweep_Spectrum::Prop::interpolation_mode, &Sweep_Spectrum::Prop::blocks>(result, raw); result.update = [raw](double time) { std::array<double, 64> values{}; for (std::size_t i = 0; i < values.size(); ++i) values[i] = -90.0 + 35.0 * std::sin(i * 0.08 + time * 0.002); raw->append_block(values); }; result.plot = std::move(object);
} else if (id == "afterglow") {
make_frequency(); make_vertical({-110.0, 0.0}); auto object = build<Impl<Afterglow>>(result.scene.get(), result.frequency.get(), result.vertical.get()); object->set<&Afterglow::Prop::frequency_range>(Axis_Range{0.0, 100.0}); object->set<&Afterglow::Prop::power_range>(Axis_Range{-110.0, 0.0}); object->set<&Afterglow::Prop::power_point_size>(96); auto* raw = object.get(); raw->mark_dirty<Prepare_Data_Tag>(); raw->mark_dirty<Paint_Tag>(); bind_prop_api<Afterglow, &Afterglow::Prop::frequency_point_size, &Afterglow::Prop::power_point_size, &Afterglow::Prop::partition_count, &Afterglow::Prop::interpolate, &Afterglow::Prop::attenuation_rate, &Afterglow::Prop::frequency_range, &Afterglow::Prop::power_range, &Afterglow::Prop::partition_mode, &Afterglow::Prop::color_map, &Afterglow::Prop::spectra>(result, raw); result.update = [raw](double time) { std::array<double, 192> values{}; for (std::size_t i = 0; i < values.size(); ++i) values[i] = -95.0 + 62.0 * std::exp(-220.0 * std::pow(static_cast<double>(i) / values.size() - 0.5 - 0.18 * std::sin(time * 0.0008), 2.0)); raw->append_spectrum(values); }; result.plot = std::move(object);
} else if (id == "waterfall") {
make_frequency(); result.time = build<Time_Axis_Object>(); configure_axis(result.time.get(), Axis_Orientation::vertical, {64.0, 370.0}, -320.0, canvas); auto object = build<Impl<Waterfall>>(result.scene.get(), result.frequency.get(), result.time.get()); object->set<&Waterfall::Prop::frequency_range>(Axis_Range{0.0, 100.0}); object->set<&Waterfall::Prop::power_range>(Axis_Range{-110.0, 0.0}); auto* raw = object.get(); raw->mark_dirty<Prepare_Data_Tag>(); raw->mark_dirty<Paint_Tag>(); bind_prop_api<Waterfall, &Waterfall::Prop::tooltip_enabled, &Waterfall::Prop::tooltip_font, &Waterfall::Prop::tooltip_text_pen, &Waterfall::Prop::tooltip_background_brush, &Waterfall::Prop::frequency_bin_count, &Waterfall::Prop::partition_count, &Waterfall::Prop::visible_range_only, &Waterfall::Prop::frequency_range, &Waterfall::Prop::power_range, &Waterfall::Prop::partition_mode, &Waterfall::Prop::interpolation_mode, &Waterfall::Prop::color_map, &Waterfall::Prop::rows>(result, raw); auto tick = std::make_shared<std::uint64_t>(); result.update = [raw, tick](double time) { std::array<double, 192> values{}; for (std::size_t i = 0; i < values.size(); ++i) values[i] = -100.0 + 70.0 * std::exp(-240.0 * std::pow(static_cast<double>(i) / values.size() - 0.5 - 0.22 * std::sin(time * 0.0006), 2.0)); raw->append_row((*tick)++, values); }; result.plot = std::move(object);
} else if (id == "constellation") {
result.horizontal = build<Numeric_Axis_Object>(); configure_axis(result.horizontal.get(), Axis_Orientation::horizontal, {64.0, 370.0}, 620.0, canvas); result.horizontal->set<&Numeric_Axis::Prop::coordinate_range>(Axis_Range{-1.2, 1.2}); make_vertical({-1.2, 1.2}); auto object = build<Impl<Constellation_Diagram>>(result.scene.get(), result.horizontal.get(), result.vertical.get()); object->set<&Constellation_Diagram::Prop::i_range>(Axis_Range{-1.2, 1.2}); object->set<&Constellation_Diagram::Prop::q_range>(Axis_Range{-1.2, 1.2}); auto* raw = object.get(); raw->mark_dirty<Prepare_Data_Tag>(); raw->mark_dirty<Paint_Tag>(); bind_prop_api<Constellation_Diagram, &Constellation_Diagram::Prop::point_lifetime_ms, &Constellation_Diagram::Prop::type, &Constellation_Diagram::Prop::phase_offset_radians, &Constellation_Diagram::Prop::i_range, &Constellation_Diagram::Prop::q_range, &Constellation_Diagram::Prop::point_color, &Constellation_Diagram::Prop::anchor_color, &Constellation_Diagram::Prop::points>(result, raw); result.update = [raw](double time) { const double phase = time * 0.003; raw->append_point({std::cos(phase) * 0.82 + 0.04 * std::sin(phase * 7.0), std::sin(phase) * 0.82 + 0.04 * std::cos(phase * 5.0)}); }; result.plot = std::move(object);
} else {
result.horizontal = build<Numeric_Axis_Object>(); configure_axis(result.horizontal.get(), Axis_Orientation::horizontal, {64.0, 370.0}, 620.0, canvas); result.horizontal->set<&Numeric_Axis::Prop::coordinate_range>(Axis_Range{0.0, 100.0}); make_vertical({0.0, 100.0}); auto object = build<Impl<Selection_Rectangle_Overlay>>(result.scene.get(), result.horizontal.get(), result.vertical.get()); auto* raw = object.get(); raw->mark_dirty<Prepare_Data_Tag>(); raw->mark_dirty<Paint_Tag>(); bind_prop_api<Selection_Rectangle_Overlay, &Selection_Rectangle_Overlay::Prop::label_font, &Selection_Rectangle_Overlay::Prop::label_pen, &Selection_Rectangle_Overlay::Prop::selection_brush, &Selection_Rectangle_Overlay::Prop::selection_border_pen, &Selection_Rectangle_Overlay::Prop::selected_regions>(result, raw); result.update = [](double) {}; result.plot = std::move(object);
}
return result;
}
struct Plot_3D {
std::unique_ptr<Impl<Point_Visual>> visual{}; /* 点图元权威对象。 */
std::unique_ptr<Impl<Render_Scene_3D>> scene{}; /* 异步 Datoviz Scene。 */
};
Plot_3D make_plot_3d() { Plot_3D result; result.visual = build<Impl<Point_Visual>>(); static_cast<void>(result.visual->update_items({render_3d::Point{.position = {-0.55F, -0.2F, 0.0F}, .color = Color::red_color(), .diameter_px = 24.0F}, render_3d::Point{.position = {0.0F, 0.5F, 0.0F}, .color = Color::green_color(), .diameter_px = 30.0F}, render_3d::Point{.position = {0.55F, -0.1F, 0.0F}, .color = Color{42, 120, 255, 255}, .diameter_px = 26.0F}})); result.visual->advance(); result.scene = build<Impl<Render_Scene_3D>>(result.visual.get()); result.scene->activate_view(); return result; }
std::variant<Plot_2D, Plot_3D> make_plot(const Graph_Descriptor& graph) { if (graph.three_dimensional) return std::variant<Plot_2D, Plot_3D>{std::in_place_type<Plot_3D>, make_plot_3d()}; return std::variant<Plot_2D, Plot_3D>{std::in_place_type<Plot_2D>, make_plot_2d(graph.id)}; }
}
struct Graph_Session::Private {
asio::strand<asio::any_io_executor> strand; /* 图对象串行访问域。 */
asio::experimental::concurrent_channel<void(asio::error_code, Session_Event)> events; /* 浏览器事件与 HTTP 状态读取队列。 */
Graph_Descriptor descriptor; /* Catalog 中的不可变图描述。 */
std::variant<Plot_2D, Plot_3D> plot; /* 具体二维或三维 Scene 所有权。 */
std::mutex handlers_mutex; /* 保护跨 Drogon 线程的连接集合。 */
std::unordered_map<const void*, Frame_Handler> handlers; /* 当前订阅该图像素帧的 WebSocket。 */
std::atomic_uint64_t frame_sequence{}; /* 传输协议的完成帧序号。 */
explicit Private(asio::any_io_executor executor, const Graph_Descriptor& graph) : strand(asio::make_strand(std::move(executor))), events(strand, 32), descriptor(graph), plot(make_plot(graph)) {}
void publish(std::string pixels) { std::vector<Frame_Handler> outputs; { std::lock_guard lock(handlers_mutex); outputs.reserve(handlers.size()); for (const auto& [owner, handler] : handlers) outputs.push_back(handler); } for (auto& output : outputs) output(pixels); }
nlohmann::json state_document() const { if (const auto* value = std::get_if<Plot_2D>(&plot)) return value->state(); const auto& state = std::get<Plot_3D>(plot).visual->read_state<Point_Visual::Base_Tag>(); return {{"prepare_dirty", state.prepare_dirty}, {"paint_dirty", state.paint_dirty}, {"prepare_executed", state.prepare_executed}, {"paint_executed", state.paint_executed}, {"prepare_graph_rebuilt", state.prepare_graph_rebuilt}, {"paint_graph_rebuilt", state.paint_graph_rebuilt}, {"prepare_task_count", state.prepare_task_count}, {"paint_task_count", state.paint_task_count}, {"prepare_execution_time_ns", state.prepare_execution_time_ns}, {"paint_execution_time_ns", state.paint_execution_time_ns}, {"item_count", state.item_count}, {"prepared_item_count", state.prepared_item_count}, {"prepared_revision", state.prepared_revision}}; }
nlohmann::json prop_document() const { if (const auto* value = std::get_if<Plot_2D>(&plot)) return value->prop(); return nlohmann::json::object(); }
nlohmann::json patch_prop(const nlohmann::json& patch) { if (auto* value = std::get_if<Plot_2D>(&plot)) return value->patch_prop(patch); return {{"success", true}, {"prop", nlohmann::json::object()}}; }
};
Graph_Session::Graph_Session(std::unique_ptr<Private> private_data) : d(std::move(private_data)) {}
std::shared_ptr<Graph_Session> Graph_Session::create(asio::any_io_executor executor, const Graph_Descriptor& descriptor) { auto result = std::shared_ptr<Graph_Session>(new Graph_Session(std::make_unique<Private>(std::move(executor), descriptor))); result->start(); return result; }
Graph_Session::~Graph_Session() { d->events.close(); }
void Graph_Session::start() { auto self = shared_from_this(); if (auto* plot = std::get_if<Plot_2D>(&d->plot)) plot->scene->set_frame_callback([weak = weak_from_this()](Image_View image) { if (auto owner = weak.lock()) { const auto sequence = owner->d->frame_sequence.fetch_add(1, std::memory_order_acq_rel) + 1; owner->d->publish(encode_frame(image, sequence)); } }); else std::get<Plot_3D>(d->plot).scene->set_frame_callback([weak = weak_from_this()](std::shared_ptr<const render_3d::Pixel_Frame> frame) { if (auto owner = weak.lock()) { const auto sequence = owner->d->frame_sequence.fetch_add(1, std::memory_order_acq_rel) + 1; owner->d->publish(encode_frame(*frame, sequence)); } }); asio::co_spawn(d->strand, [self]() -> asio::awaitable<void> { for (;;) { asio::error_code error; auto event = co_await self->d->events.async_receive(asio::redirect_error(asio::use_awaitable, error)); if (error) co_return; if (auto* state = std::get_if<State_Query>(&event)) { state->handler(self->d->state_document()); continue; } if (auto* prop = std::get_if<Prop_Query>(&event)) { prop->handler(self->d->prop_document()); continue; } if (auto* patch = std::get_if<Prop_Patch>(&event)) { patch->handler(self->d->patch_prop(patch->patch)); continue; } const auto value = std::get<Graph_Event>(event); if (auto* plot = std::get_if<Plot_2D>(&self->d->plot)) { const Size viewport{static_cast<int>(std::clamp(value.width, 160U, 1920U)), static_cast<int>(std::clamp(value.height, 120U, 1080U))}; plot->scene->set<&Render_Scene_2D::Prop::viewport>(viewport); if (plot->frequency) plot->frequency->set<&Abs_Axis::Prop::canvas_size>(viewport); if (plot->horizontal) plot->horizontal->set<&Abs_Axis::Prop::canvas_size>(viewport); if (plot->vertical) plot->vertical->set<&Abs_Axis::Prop::canvas_size>(viewport); if (plot->time) plot->time->set<&Abs_Axis::Prop::canvas_size>(viewport); plot->update(value.time_milliseconds); plot->scene->render(); } else { auto& plot_3d = std::get<Plot_3D>(self->d->plot); plot_3d.scene->set<&Render_Scene_3D::Prop::viewport>(render_3d::Extent{std::clamp(value.width, 160U, 1920U), std::clamp(value.height, 120U, 1080U)}); plot_3d.scene->render(); } } }, [](std::exception_ptr exception) { if (exception) std::rethrow_exception(exception); }); }
void Graph_Session::attach(const void* owner, Frame_Handler handler) { { std::lock_guard lock(d->handlers_mutex); d->handlers.insert_or_assign(owner, std::move(handler)); } submit({}); }
void Graph_Session::detach(const void* owner) { std::lock_guard lock(d->handlers_mutex); d->handlers.erase(owner); }
void Graph_Session::submit(Graph_Event event) { static_cast<void>(d->events.try_send(asio::error_code{}, Session_Event{event})); }
void Graph_Session::async_state(State_Handler handler) { if (!d->events.try_send(asio::error_code{}, Session_Event{State_Query{std::move(handler)}})) throw std::runtime_error("graph state queue is unavailable"); }
void Graph_Session::async_prop(Prop_Handler handler) { if (!d->events.try_send(asio::error_code{}, Session_Event{Prop_Query{std::move(handler)}})) throw std::runtime_error("graph prop queue is unavailable"); }
void Graph_Session::async_patch_prop(nlohmann::json patch, Prop_Handler handler) { if (!d->events.try_send(asio::error_code{}, Session_Event{Prop_Patch{std::move(patch), std::move(handler)}})) throw std::runtime_error("graph prop queue is unavailable"); }
const Graph_Descriptor& Graph_Session::descriptor() const noexcept { return d->descriptor; }
struct Graph_Registry::Private { asio::any_io_executor executor; std::mutex mutex; std::unordered_map<std::string, std::shared_ptr<Graph_Session>> sessions; explicit Private(asio::any_io_executor value) : executor(std::move(value)) {} };
Graph_Registry::Graph_Registry(asio::any_io_executor executor) : d(std::make_unique<Private>(std::move(executor))) {}
Graph_Registry::~Graph_Registry() = default;
std::shared_ptr<Graph_Session> Graph_Registry::acquire(std::string_view graph_id) { const auto* descriptor = find_graph(graph_id); if (!descriptor) return {}; std::lock_guard lock(d->mutex); auto& session = d->sessions[std::string(graph_id)]; if (!session) session = Graph_Session::create(d->executor, *descriptor); return session; }
}