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:
@@ -0,0 +1,169 @@
|
||||
#include <dmf-node/control_server.hpp>
|
||||
#include <dmf-node/node.hpp>
|
||||
|
||||
#include <libwebsockets.h>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace dmf_node {
|
||||
|
||||
struct ControlServerData {
|
||||
StatusCallback on_event;
|
||||
std::unordered_map<std::string, CommandHandler> commands;
|
||||
std::mutex send_mutex;
|
||||
std::vector<std::string> send_queue;
|
||||
struct lws* client_wsi = nullptr;
|
||||
};
|
||||
|
||||
struct ControlServer::Impl {
|
||||
uint16_t port;
|
||||
std::unique_ptr<ControlServerData> data;
|
||||
struct lws_context* context = nullptr;
|
||||
};
|
||||
|
||||
static int callback_ws(struct lws* wsi, enum lws_callback_reasons reason,
|
||||
void* user, void* in, size_t len);
|
||||
|
||||
static const struct lws_protocols protocols[] = {
|
||||
{
|
||||
"dmf-control",
|
||||
callback_ws,
|
||||
sizeof(ControlServerData*),
|
||||
65536,
|
||||
},
|
||||
{nullptr, nullptr, 0, 0},
|
||||
};
|
||||
|
||||
static int callback_ws(struct lws* wsi, enum lws_callback_reasons reason,
|
||||
void* user, void* in, size_t len) {
|
||||
auto** ppdata = static_cast<ControlServerData**>(user);
|
||||
ControlServerData* data = ppdata ? *ppdata : nullptr;
|
||||
|
||||
switch (reason) {
|
||||
case LWS_CALLBACK_ESTABLISHED: {
|
||||
auto* vhost = lws_get_vhost(wsi);
|
||||
data = static_cast<ControlServerData*>(lws_vhost_user(vhost));
|
||||
if (ppdata) {
|
||||
*ppdata = data;
|
||||
}
|
||||
if (data) {
|
||||
data->client_wsi = wsi;
|
||||
}
|
||||
spdlog::info("Control WS: client connected");
|
||||
break;
|
||||
}
|
||||
case LWS_CALLBACK_RECEIVE: {
|
||||
if (!data) {
|
||||
auto* vhost = lws_get_vhost(wsi);
|
||||
data = static_cast<ControlServerData*>(lws_vhost_user(vhost));
|
||||
}
|
||||
if (!data) {
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
auto msg = nlohmann::json::parse(static_cast<char*>(in), static_cast<char*>(in) + len);
|
||||
if (!msg.contains("cmd")) {
|
||||
spdlog::warn("Control WS: message missing 'cmd' field");
|
||||
break;
|
||||
}
|
||||
auto cmd = msg["cmd"].get<std::string>();
|
||||
auto it = data->commands.find(cmd);
|
||||
if (it != data->commands.end()) {
|
||||
it->second(msg);
|
||||
} else {
|
||||
spdlog::warn("Control WS: unknown command '{}'", cmd);
|
||||
}
|
||||
} catch (const nlohmann::json::parse_error& e) {
|
||||
spdlog::warn("Control WS: JSON parse error: {}", e.what());
|
||||
}
|
||||
break;
|
||||
}
|
||||
case LWS_CALLBACK_SERVER_WRITEABLE: {
|
||||
if (!data) {
|
||||
auto* vhost = lws_get_vhost(wsi);
|
||||
data = static_cast<ControlServerData*>(lws_vhost_user(vhost));
|
||||
}
|
||||
if (!data) {
|
||||
break;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(data->send_mutex);
|
||||
while (!data->send_queue.empty()) {
|
||||
auto& msg = data->send_queue.back();
|
||||
std::vector<uint8_t> buf(LWS_PRE + msg.size());
|
||||
std::memcpy(buf.data() + LWS_PRE, msg.data(), msg.size());
|
||||
lws_write(wsi, buf.data() + LWS_PRE, msg.size(), LWS_WRITE_TEXT);
|
||||
data->send_queue.pop_back();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case LWS_CALLBACK_CLOSED: {
|
||||
if (data) {
|
||||
data->client_wsi = nullptr;
|
||||
}
|
||||
spdlog::info("Control WS: client disconnected");
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
ControlServer::ControlServer(uint16_t port, StatusCallback on_event)
|
||||
: impl_(std::make_unique<Impl>()) {
|
||||
impl_->port = port;
|
||||
impl_->data = std::make_unique<ControlServerData>();
|
||||
impl_->data->on_event = std::move(on_event);
|
||||
|
||||
struct lws_context_creation_info info;
|
||||
std::memset(&info, 0, sizeof(info));
|
||||
info.port = port;
|
||||
info.protocols = protocols;
|
||||
info.user = impl_->data.get();
|
||||
info.gid = -1;
|
||||
info.uid = -1;
|
||||
|
||||
impl_->context = lws_create_context(&info);
|
||||
if (!impl_->context) {
|
||||
spdlog::error("Failed to create WS context on port {}", port);
|
||||
throw std::runtime_error("Failed to create WS context");
|
||||
}
|
||||
spdlog::info("Control WS: listening on port {}", port);
|
||||
}
|
||||
|
||||
ControlServer::~ControlServer() {
|
||||
if (impl_->context) {
|
||||
lws_context_destroy(impl_->context);
|
||||
}
|
||||
}
|
||||
|
||||
void ControlServer::register_command(const std::string& cmd, CommandHandler handler) {
|
||||
impl_->data->commands[cmd] = std::move(handler);
|
||||
}
|
||||
|
||||
void ControlServer::send_event(const nlohmann::json& event) {
|
||||
auto data = event.dump();
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(impl_->data->send_mutex);
|
||||
impl_->data->send_queue.push_back(data);
|
||||
}
|
||||
if (impl_->data->client_wsi) {
|
||||
lws_callback_on_writable(impl_->data->client_wsi);
|
||||
}
|
||||
}
|
||||
|
||||
void ControlServer::poll(int timeout_ms) {
|
||||
lws_service(impl_->context, timeout_ms);
|
||||
}
|
||||
|
||||
} // namespace dmf_node
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user