feat: scaffold project framework with engine, node skeleton, and passthrough node

- CMake build system with vcpkg dependency management
- libdmf-node: Node interface, port definitions, WebSocket control
  server, node runner with MXL lifecycle management
- libdmf-engine: Graph model (nodes/edges CRUD, serialization),
  FlowManager (UUID + V210 NMOS flow definitions), ProcessManager
  (fork/exec node processes), ApiServer (REST API for graph control)
- Passthrough node: reads V210 grains from MXL, copies, writes to MXL
- Unit tests for Graph and FlowManager (6 cases, 21 assertions)
- MXL SDK as git submodule (symlinked from extern/mxl)
This commit is contained in:
Johanness
2026-05-26 01:32:50 +03:00
parent ea0eaf8d48
commit 5b5ffa1308
30 changed files with 1547 additions and 0 deletions
+184
View File
@@ -0,0 +1,184 @@
#include <dmf-node/node_runner.hpp>
#include <dmf-node/control_server.hpp>
#include <mxl/flow.h>
#include <mxl/mxl.h>
#include <nlohmann/json.hpp>
#include <spdlog/spdlog.h>
#include <cstring>
#include <csignal>
#include <filesystem>
#include <thread>
namespace dmf_node {
static std::atomic<bool> g_running{true};
static void signal_handler(int /*signum*/) {
g_running = false;
}
bool NodeRunner::parse_args(int argc, char* argv[]) {
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if ((arg == "--node-id" || arg == "-n") && i + 1 < argc) {
node_id_ = argv[++i];
} else if ((arg == "--control-port" || arg == "-p") && i + 1 < argc) {
control_port_ = static_cast<uint16_t>(std::stoi(argv[++i]));
} else if ((arg == "--mxl-domain" || arg == "-d") && i + 1 < argc) {
mxl_domain_ = argv[++i];
} else if ((arg == "--config" || arg == "-c") && i + 1 < argc) {
config_str_ = argv[++i];
} else if (arg == "--help" || arg == "-h") {
spdlog::info("Usage: {} [options]", argv[0]);
spdlog::info(" --node-id, -n Node instance ID");
spdlog::info(" --control-port, -p WebSocket control port");
spdlog::info(" --mxl-domain, -d MXL domain path (default: /dev/shm/mxl)");
spdlog::info(" --config, -c Node configuration JSON");
return false;
}
}
if (node_id_.empty()) {
spdlog::error("--node-id is required");
return false;
}
if (control_port_ == 0) {
spdlog::error("--control-port is required");
return false;
}
return true;
}
int NodeRunner::exec(std::unique_ptr<Node> node) {
std::signal(SIGINT, signal_handler);
std::signal(SIGTERM, signal_handler);
spdlog::info("Starting node '{}' type='{}'", node_id_, node->type());
if (!std::filesystem::exists(mxl_domain_)) {
spdlog::error("MXL domain path does not exist: {}", mxl_domain_);
return 1;
}
mxl_instance_ = mxlCreateInstance(mxl_domain_.c_str(), nullptr);
if (!mxl_instance_) {
spdlog::error("Failed to create MXL instance on domain: {}", mxl_domain_);
return 1;
}
spdlog::info("MXL instance created on domain: {}", mxl_domain_);
mxlGarbageCollectFlows(mxl_instance_);
if (!config_str_.empty()) {
try {
auto cfg = nlohmann::json::parse(config_str_);
node->configure(cfg);
} catch (const nlohmann::json::parse_error& e) {
spdlog::error("Failed to parse config JSON: {}", e.what());
mxlDestroyInstance(mxl_instance_);
return 1;
}
}
auto control_server = std::make_unique<ControlServer>(control_port_, [](const nlohmann::json& /*event*/) {});
auto mk_ports = [](const std::vector<PortDef>& ports) {
auto arr = nlohmann::json::array();
for (const auto& p : ports) {
arr.push_back({{"id", p.id}, {"direction", p.direction == PortDirection::Input ? "input" : "output"}, {"media_type", static_cast<int>(p.media_type)}});
}
return arr;
};
control_server->register_command("add_writer", [&, w = std::unordered_map<std::string, mxlFlowWriter>{}](const nlohmann::json& msg) mutable {
auto flow_id = msg["flow_id"].get<std::string>();
auto port_id = msg["port_id"].get<std::string>();
auto flow_def = msg["flow_def"].dump();
mxlFlowWriter writer = nullptr;
bool created = false;
mxlFlowConfigInfo config_info{};
auto status = mxlCreateFlowWriter(mxl_instance_, flow_def.c_str(), nullptr, &writer, &config_info, &created);
if (status != MXL_STATUS_OK || !writer) {
spdlog::error("Failed to create flow writer for flow {}: status={}", flow_id, static_cast<int>(status));
return;
}
spdlog::info("Created flow writer on port '{}' flow {} (created={})", port_id, flow_id, created);
node->on_add_writer(port_id, writer);
});
control_server->register_command("add_reader", [&](const nlohmann::json& msg) {
auto flow_id = msg["flow_id"].get<std::string>();
auto port_id = msg["port_id"].get<std::string>();
mxlFlowReader reader = nullptr;
auto status = mxlCreateFlowReader(mxl_instance_, flow_id.c_str(), nullptr, &reader);
if (status != MXL_STATUS_OK || !reader) {
spdlog::error("Failed to create flow reader for flow {}: status={}", flow_id, static_cast<int>(status));
return;
}
spdlog::info("Created flow reader on port '{}' flow {}", port_id, flow_id);
node->on_add_reader(port_id, reader);
});
control_server->register_command("remove_writer", [&](const nlohmann::json& msg) {
auto port_id = msg["port_id"].get<std::string>();
node->on_remove_writer(port_id);
spdlog::info("Removed writer on port '{}'", port_id);
});
control_server->register_command("remove_reader", [&](const nlohmann::json& msg) {
auto port_id = msg["port_id"].get<std::string>();
node->on_remove_reader(port_id);
spdlog::info("Removed reader on port '{}'", port_id);
});
control_server->register_command("configure", [&](const nlohmann::json& msg) {
if (msg.contains("params")) {
node->configure(msg["params"]);
spdlog::info("Reconfigured node '{}'", node_id_);
}
});
control_server->register_command("shutdown", [&](const nlohmann::json& /*msg*/) {
spdlog::info("Shutdown command received");
g_running = false;
});
nlohmann::json ready_event;
ready_event["event"] = "ready";
ready_event["type"] = node->type();
ready_event["node_id"] = node_id_;
ready_event["ports"] = mk_ports(node->input_ports());
for (const auto& p : node->output_ports()) {
ready_event["ports"].push_back({{"id", p.id}, {"direction", "output"}, {"media_type", static_cast<int>(p.media_type)}});
}
control_server->send_event(ready_event);
running_ = true;
spdlog::info("Node '{}' entering process loop", node_id_);
while (g_running && running_) {
control_server->poll(1);
node->process();
static int status_counter = 0;
if (++status_counter % 100 == 0) {
nlohmann::json status_event;
status_event["event"] = "status";
status_event["node_id"] = node_id_;
status_event["data"] = node->status();
control_server->send_event(status_event);
}
}
spdlog::info("Node '{}' shutting down", node_id_);
mxlDestroyInstance(mxl_instance_);
return 0;
}
} // namespace dmf_node