feat: add node HTTP control, engine-to-node communication, connect-input/output API
- Node control server now accepts HTTP POST /cmd for command dispatch (in addition to existing WebSocket control) - Added NodeControlClient: engine sends commands to nodes via HTTP POST - New REST endpoints: POST /api/graph/nodes/:id/connect-input - connect node input to MXL flow POST /api/graph/nodes/:id/connect-output - create new MXL flow + connect output POST /api/graph/nodes/:id/disconnect-port - remove reader/writer POST /api/graph/nodes/:id/command - send raw command to node - Flow IDs now use proper UUID v4 format (MXL requires standard UUIDs) - Flow definitions use NMOS format (urn:x-nmos:format:video) - Engine passes --mxl-domain to node processes - User-provided node IDs (via 'id' field in POST body) - End-to-end verified: testsrc → passthrough → new MXL output flow
This commit is contained in:
@@ -29,92 +29,123 @@ struct ControlServer::Impl {
|
||||
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 void dispatch_command(ControlServerData* data, const nlohmann::json& msg) {
|
||||
if (!msg.contains("cmd")) {
|
||||
spdlog::warn("Control: message missing 'cmd' field");
|
||||
return;
|
||||
}
|
||||
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: unknown command '{}'", cmd);
|
||||
}
|
||||
}
|
||||
|
||||
static const struct lws_protocols protocols[] = {
|
||||
{
|
||||
"http-only",
|
||||
callback_ws,
|
||||
sizeof(ControlServerData*),
|
||||
0,
|
||||
},
|
||||
{
|
||||
"dmf-control",
|
||||
callback_ws,
|
||||
sizeof(ControlServerData*),
|
||||
65536,
|
||||
},
|
||||
{nullptr, nullptr, 0, 0},
|
||||
static int send_http_json(struct lws* wsi, const std::string& status, const std::string& body) {
|
||||
auto hdr = "HTTP/1.1 " + status + "\r\n"
|
||||
"Content-Type: application/json\r\n"
|
||||
"Content-Length: " + std::to_string(body.size()) + "\r\n"
|
||||
"\r\n";
|
||||
std::vector<uint8_t> buf(LWS_PRE + hdr.size() + body.size());
|
||||
std::memcpy(buf.data() + LWS_PRE, hdr.data(), hdr.size());
|
||||
std::memcpy(buf.data() + LWS_PRE + hdr.size(), body.data(), body.size());
|
||||
lws_write(wsi, buf.data() + LWS_PRE, hdr.size() + body.size(), LWS_WRITE_HTTP);
|
||||
return lws_http_transaction_completed(wsi) ? -1 : 0;
|
||||
}
|
||||
|
||||
struct PerSession {
|
||||
std::string http_body;
|
||||
bool is_ws = false;
|
||||
ControlServerData* data = nullptr;
|
||||
};
|
||||
|
||||
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;
|
||||
static int callback_all(struct lws* wsi, enum lws_callback_reasons reason,
|
||||
void* user, void* in, size_t len) {
|
||||
auto* ps = static_cast<PerSession*>(user);
|
||||
|
||||
switch (reason) {
|
||||
case LWS_CALLBACK_HTTP: {
|
||||
new (ps) PerSession();
|
||||
auto* vhost = lws_get_vhost(wsi);
|
||||
ps->data = vhost ? static_cast<ControlServerData*>(lws_vhost_user(vhost)) : nullptr;
|
||||
|
||||
char* uri_ptr = nullptr;
|
||||
int uri_len = 0;
|
||||
int method = lws_http_get_uri_and_method(wsi, &uri_ptr, &uri_len);
|
||||
std::string path(uri_ptr ? uri_ptr : "", uri_len > 0 ? uri_len : 0);
|
||||
|
||||
if (path == "/cmd" && method == LWSHUMETH_POST) {
|
||||
int cl = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH);
|
||||
if (cl > 0) {
|
||||
ps->http_body.reserve(cl);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (path == "/cmd" && (method == LWSHUMETH_GET || method == -1)) {
|
||||
return send_http_json(wsi, "405 Method Not Allowed", R"({"error":"POST only"})");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
case LWS_CALLBACK_HTTP_BODY: {
|
||||
ps->http_body.append(static_cast<char*>(in), len);
|
||||
break;
|
||||
}
|
||||
case LWS_CALLBACK_HTTP_BODY_COMPLETION: {
|
||||
if (!ps->data) {
|
||||
return send_http_json(wsi, "500 Error", R"({"error":"no data"})");
|
||||
}
|
||||
try {
|
||||
auto msg = nlohmann::json::parse(ps->http_body);
|
||||
dispatch_command(ps->data, msg);
|
||||
return send_http_json(wsi, "200 OK", R"({"ok":true})");
|
||||
} catch (const nlohmann::json::parse_error& e) {
|
||||
return send_http_json(wsi, "400 Bad Request",
|
||||
nlohmann::json({{"error", e.what()}}).dump());
|
||||
}
|
||||
}
|
||||
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;
|
||||
ps->data = vhost ? static_cast<ControlServerData*>(lws_vhost_user(vhost)) : nullptr;
|
||||
ps->is_ws = true;
|
||||
if (ps->data) {
|
||||
ps->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) {
|
||||
if (!ps->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);
|
||||
}
|
||||
dispatch_command(ps->data, msg);
|
||||
} 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) {
|
||||
if (!ps->data) {
|
||||
break;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(data->send_mutex);
|
||||
while (!data->send_queue.empty()) {
|
||||
auto& msg = data->send_queue.back();
|
||||
std::lock_guard<std::mutex> lock(ps->data->send_mutex);
|
||||
while (!ps->data->send_queue.empty()) {
|
||||
auto& msg = ps->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();
|
||||
ps->data->send_queue.pop_back();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case LWS_CALLBACK_CLOSED: {
|
||||
if (data) {
|
||||
data->client_wsi = nullptr;
|
||||
if (ps->data) {
|
||||
ps->data->client_wsi = nullptr;
|
||||
}
|
||||
spdlog::info("Control WS: client disconnected");
|
||||
break;
|
||||
@@ -125,23 +156,59 @@ static int callback_ws(struct lws* wsi, enum lws_callback_reasons reason,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct lws_http_mount mount = {
|
||||
.mount_next = nullptr,
|
||||
.mountpoint = "/",
|
||||
.origin = "",
|
||||
.def = "",
|
||||
.protocol = "dmf-control",
|
||||
.cgienv = nullptr,
|
||||
.extra_mimetypes = nullptr,
|
||||
.interpret = nullptr,
|
||||
.cgi_timeout = 0,
|
||||
.cache_max_age = 0,
|
||||
.auth_mask = 0,
|
||||
.cache_reusable = 0,
|
||||
.cache_revalidate = 0,
|
||||
.cache_intermediaries = 0,
|
||||
.origin_protocol = LWSMPRO_CALLBACK,
|
||||
.mountpoint_len = 1,
|
||||
static const struct lws_protocols protocols[] = {
|
||||
{
|
||||
"http-only",
|
||||
callback_all,
|
||||
sizeof(PerSession),
|
||||
0,
|
||||
},
|
||||
{
|
||||
"dmf-control",
|
||||
callback_all,
|
||||
sizeof(PerSession),
|
||||
65536,
|
||||
},
|
||||
{nullptr, nullptr, 0, 0},
|
||||
};
|
||||
|
||||
static const struct lws_http_mount mounts[] = {
|
||||
{
|
||||
.mount_next = &mounts[1],
|
||||
.mountpoint = "/cmd",
|
||||
.origin = "",
|
||||
.def = "",
|
||||
.protocol = "http-only",
|
||||
.cgienv = nullptr,
|
||||
.extra_mimetypes = nullptr,
|
||||
.interpret = nullptr,
|
||||
.cgi_timeout = 0,
|
||||
.cache_max_age = 0,
|
||||
.auth_mask = 0,
|
||||
.cache_reusable = 0,
|
||||
.cache_revalidate = 0,
|
||||
.cache_intermediaries = 0,
|
||||
.origin_protocol = LWSMPRO_CALLBACK,
|
||||
.mountpoint_len = 4,
|
||||
},
|
||||
{
|
||||
.mount_next = nullptr,
|
||||
.mountpoint = "/",
|
||||
.origin = "",
|
||||
.def = "",
|
||||
.protocol = "dmf-control",
|
||||
.cgienv = nullptr,
|
||||
.extra_mimetypes = nullptr,
|
||||
.interpret = nullptr,
|
||||
.cgi_timeout = 0,
|
||||
.cache_max_age = 0,
|
||||
.auth_mask = 0,
|
||||
.cache_reusable = 0,
|
||||
.cache_revalidate = 0,
|
||||
.cache_intermediaries = 0,
|
||||
.origin_protocol = LWSMPRO_CALLBACK,
|
||||
.mountpoint_len = 1,
|
||||
},
|
||||
};
|
||||
|
||||
ControlServer::ControlServer(uint16_t port, StatusCallback on_event)
|
||||
@@ -154,7 +221,7 @@ ControlServer::ControlServer(uint16_t port, StatusCallback on_event)
|
||||
std::memset(&info, 0, sizeof(info));
|
||||
info.port = port;
|
||||
info.protocols = protocols;
|
||||
info.mounts = &mount;
|
||||
info.mounts = mounts;
|
||||
info.user = impl_->data.get();
|
||||
info.gid = -1;
|
||||
info.uid = -1;
|
||||
|
||||
Reference in New Issue
Block a user