| 399 | }; |
| 400 | |
| 401 | std::string control_socket_t::parse_command(const std::string &command) { |
| 402 | // DEBUG("Parse command {}", command); |
| 403 | json_t js; |
| 404 | try { |
| 405 | js = json_t::parse(command); |
| 406 | } catch (const std::exception &e) { |
| 407 | return json_t{{"error", e.what()}}.dump(); |
| 408 | } |
| 409 | std::string method = js["method"]; |
| 410 | json_t retdata = {{"id", js["id"]}}; |
| 411 | try { |
| 412 | for (const auto &cmd : COMMANDS) { |
| 413 | if (cmd.name == method) { |
| 414 | auto res = cmd.func(*this, js["params"]); |
| 415 | retdata["result"] = res; |
| 416 | |
| 417 | return retdata.dump(); |
| 418 | } |
| 419 | } |
| 420 | // if matches the regex (^d*\..*), its a command to a peer |
| 421 | std::smatch match; |
| 422 | if (std::regex_match(method, match, PEER_COMMAND_RE)) { |
| 423 | auto peer_id = std::stoi(match[1]); |
| 424 | auto cmd = match[2]; |
| 425 | // DEBUG("Peer command: {} -> {}", peer_id, cmd.to_string()); |
| 426 | auto peer = router->get_peer_by_id(peer_id); |
| 427 | if (peer) { |
| 428 | auto res = peer->command(cmd, js["params"]); |
| 429 | if (res.contains("error")) { |
| 430 | retdata["error"] = res["error"]; |
| 431 | } else { |
| 432 | retdata["result"] = res; |
| 433 | } |
| 434 | } else { |
| 435 | retdata["error"] = FMT::format("Unknown peer '{}'", peer_id); |
| 436 | } |
| 437 | return retdata.dump(); |
| 438 | } |
| 439 | |
| 440 | retdata["error"] = FMT::format("Unknown method '{}'", method); |
| 441 | ERROR("Error running method: {}", std::string(retdata["error"])); |
| 442 | return retdata.dump(); |
| 443 | } catch (const std::exception &e) { |
| 444 | ERROR("Error running method: {}", e.what()); |
| 445 | retdata["error"] = e.what(); |
| 446 | return retdata.dump(); |
| 447 | } |
| 448 | } |
| 449 | } // namespace rtpmididns |
nothing calls this directly
no test coverage detected