![]() |
dfx 0.1.0
Linux-based dynamic dataflow executor
|
JSON command router for the Unix domain socket control protocol. More...
#include <dfx-server/UnixRouter.hpp>
Public Types | |
| using | Handler = std::move_only_function<bool (UnixSessionPtr, nlohmann::json, std::string)> |
| Handler type for a routed command. | |
Public Member Functions | |
| UnixRouter () | |
| Construct the router and register the request envelope schema. | |
| ~UnixRouter () | |
| Destructor. | |
| void | registerCommand (std::string command, Handler handler, bool allowOverride=false) |
| Register a command handler. | |
| bool | deregisterCommand (std::string_view command) |
| Deregister a command handler. | |
| bool | isRegistered (std::string_view command) const |
| Check whether a command is registered. | |
| bool | route (UnixSessionPtr session, nlohmann::json json) |
| Validate and route a JSON request to the appropriate command handler. | |
JSON command router for the Unix domain socket control protocol.
UnixRouter is the dispatch point for the Unix control plane (used by UnixServer and UnixSession).
It receives a decoded JSON request envelope, validates it against an internal schema, extracts the command name, and forwards execution to a registered handler.
The router also implements protocol-level guard rails:
Example:
The boolean return value is propagated back to the caller of route() and is used by the session/server to decide whether the connection should remain alive after handling the request.
| using dfx::Server::UnixRouter::Handler = std::move_only_function<bool (UnixSessionPtr, nlohmann::json, std::string)> |
Handler type for a routed command.
| session | The session that sent this request. |
| params | The parameters of the command. |
| id | The optional id that needs to be passed down to reply() / replyError() |
| dfx::Server::UnixRouter::UnixRouter | ( | ) |
Construct the router and register the request envelope schema.
The constructor registers the JSON schema used to validate incoming request envelopes.
| dfx::Server::UnixRouter::~UnixRouter | ( | ) |
Destructor.
| bool dfx::Server::UnixRouter::deregisterCommand | ( | std::string_view | command | ) |
Deregister a command handler.
Removes both the handler and the command from the internal "possible commands" list.
| command | Command name to remove. |
| bool dfx::Server::UnixRouter::isRegistered | ( | std::string_view | command | ) | const |
Check whether a command is registered.
| command | Command name. |
| void dfx::Server::UnixRouter::registerCommand | ( | std::string | command, |
| Handler | handler, | ||
| bool | allowOverride = false ) |
Register a command handler.
By default, registering an already-registered command is considered a bug and triggers an assertion. Set allowOverride to true to explicitly replace a handler (useful for tests or for composing routers in layers).
The command name is also recorded in an internal list used to produce the "Possible commands are: [...]" diagnostics for unknown commands.
| command | Command name as used in the request envelope ("command"). |
| handler | Handler to invoke when the command is routed. |
| allowOverride | If true, replaces an existing handler for the same command. |
| bool dfx::Server::UnixRouter::route | ( | UnixSessionPtr | session, |
| nlohmann::json | json ) |
Validate and route a JSON request to the appropriate command handler.
Routing steps:
| session | Session that originated the request (used to send replies). |
| json | Parsed JSON request envelope. |