237 lines
8.4 KiB
C++
237 lines
8.4 KiB
C++
#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_);
|
|
|
|
node->set_mxl_instance(mxl_instance_);
|
|
|
|
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;
|
|
};
|
|
|
|
struct FlowResource {
|
|
std::string port_id;
|
|
mxlFlowWriter writer = nullptr;
|
|
mxlFlowReader reader = nullptr;
|
|
};
|
|
std::vector<FlowResource> flow_resources;
|
|
|
|
control_server->register_command("add_writer", [&](const nlohmann::json& msg) -> nlohmann::json {
|
|
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 {{"ok", false}, {"error", "failed to create flow writer"}};
|
|
}
|
|
spdlog::info("Created flow writer on port '{}' flow {} (created={})", port_id, flow_id, created);
|
|
flow_resources.push_back({port_id, writer, nullptr});
|
|
node->set_writer_config(port_id, config_info);
|
|
node->on_add_writer(port_id, writer);
|
|
});
|
|
|
|
control_server->register_command("add_reader", [&](const nlohmann::json& msg) -> nlohmann::json {
|
|
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 {{"ok", false}, {"error", "failed to create flow reader"}};
|
|
}
|
|
spdlog::info("Created flow reader on port '{}' flow {}", port_id, flow_id);
|
|
flow_resources.push_back({port_id, nullptr, reader});
|
|
mxlFlowConfigInfo config{};
|
|
mxlFlowReaderGetConfigInfo(reader, &config);
|
|
node->set_reader_config(port_id, config);
|
|
node->on_add_reader(port_id, reader);
|
|
});
|
|
|
|
control_server->register_command("remove_writer", [&](const nlohmann::json& msg) -> nlohmann::json {
|
|
auto port_id = msg["port_id"].get<std::string>();
|
|
node->on_remove_writer(port_id);
|
|
for (auto it = flow_resources.begin(); it != flow_resources.end(); ++it) {
|
|
if (it->port_id == port_id && it->writer) {
|
|
mxlReleaseFlowWriter(mxl_instance_, it->writer);
|
|
flow_resources.erase(it);
|
|
break;
|
|
}
|
|
}
|
|
spdlog::info("Removed writer on port '{}'", port_id);
|
|
return {{"ok", true}};
|
|
});
|
|
|
|
control_server->register_command("remove_reader", [&](const nlohmann::json& msg) -> nlohmann::json {
|
|
auto port_id = msg["port_id"].get<std::string>();
|
|
node->on_remove_reader(port_id);
|
|
for (auto it = flow_resources.begin(); it != flow_resources.end(); ++it) {
|
|
if (it->port_id == port_id && it->reader) {
|
|
mxlReleaseFlowReader(mxl_instance_, it->reader);
|
|
flow_resources.erase(it);
|
|
break;
|
|
}
|
|
}
|
|
spdlog::info("Removed reader on port '{}'", port_id);
|
|
return {{"ok", true}};
|
|
});
|
|
|
|
control_server->register_command("configure", [&](const nlohmann::json& msg) -> nlohmann::json {
|
|
if (msg.contains("params")) {
|
|
node->configure(msg["params"]);
|
|
spdlog::info("Reconfigured node '{}'", node_id_);
|
|
}
|
|
return {{"ok", true}};
|
|
});
|
|
|
|
control_server->register_command("status", [&](const nlohmann::json& /*msg*/) -> nlohmann::json {
|
|
nlohmann::json resp;
|
|
resp["event"] = "status";
|
|
resp["node_id"] = node_id_;
|
|
resp["data"] = node->status();
|
|
control_server->send_event(resp);
|
|
return resp;
|
|
});
|
|
|
|
control_server->register_command("shutdown", [&](const nlohmann::json& /*msg*/) -> nlohmann::json {
|
|
spdlog::info("Shutdown command received");
|
|
g_running = false;
|
|
return {{"ok", true}};
|
|
});
|
|
|
|
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_);
|
|
|
|
std::thread process_thread([&]() {
|
|
while (g_running && running_) {
|
|
node->process();
|
|
}
|
|
});
|
|
|
|
while (g_running && running_) {
|
|
control_server->poll(10);
|
|
}
|
|
|
|
running_ = false;
|
|
process_thread.join();
|
|
|
|
spdlog::info("Node '{}' shutting down", node_id_);
|
|
|
|
for (auto& res : flow_resources) {
|
|
if (res.writer) {
|
|
mxlReleaseFlowWriter(mxl_instance_, res.writer);
|
|
}
|
|
if (res.reader) {
|
|
mxlReleaseFlowReader(mxl_instance_, res.reader);
|
|
}
|
|
}
|
|
flow_resources.clear();
|
|
|
|
mxlDestroyInstance(mxl_instance_);
|
|
return 0;
|
|
}
|
|
|
|
} // namespace dmf_node
|