dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
dfx::Server::Api::BaseCommandHandler Class Reference

Small helper base used to register UnixRouter commands with JSON params validation. More...

#include <dfx-server/command-handlers/BaseCommandHandler.hpp>

Inheritance diagram for dfx::Server::Api::BaseCommandHandler:
[legend]

Public Member Functions

 BaseCommandHandler ()=default
 Default constructor.

Protected Member Functions

template<typename PMF>
void registerNewCommand (UnixRouter &router, std::string command, nlohmann::json const &schema, PMF handler)
 Register a new command in the router with an associated params schema and member handler.
bool validateParams (std::string_view command, nlohmann::json const &params, std::string_view id, UnixSessionPtr session) const
 Validate params for a command and reply with errors on failure.

Detailed Description

Small helper base used to register UnixRouter commands with JSON params validation.

This class is primarily a convenience layer over dfx::Server::UnixRouter.

It provides two things:

  • a single place to own a Utils::JsonValidator and register per-command schemas,
  • a safe registration helper that wraps a handler so params are validated before the handler runs.

Despite being a base class, it is not meant for runtime polymorphism. It is a CRTP-like registration helper used by concrete command handler classes.

How to use
A typical handler:

The member function signature must match the router handler signature:

bool handler(UnixSessionPtr session, nlohmann::json params, std::string id);
std::shared_ptr< UnixSession > UnixSessionPtr
Shared ownership pointer type for sessions.
Definition BaseCommandHandler.hpp:20

Example (real usage pattern):

CommandHandlerImpl::CommandHandlerImpl(ds::UnixRouter & router)
{
registerNewCommand(router, "set_system_value", dsa::Schema::setSystemValue, &CommandHandlerImpl::_setSystemValue);
registerNewCommand(router, "get_system_value", dsa::Schema::getSystemValue, &CommandHandlerImpl::_getSystemValue);
registerNewCommand(router, "list_system_values", dsa::Schema::listSystemValues, &CommandHandlerImpl::_listSystemValues);
}
void registerNewCommand(UnixRouter &router, std::string command, nlohmann::json const &schema, PMF handler)
Register a new command in the router with an associated params schema and member handler.
Definition BaseCommandHandler.hpp:139
JSON command router for the Unix domain socket control protocol.
Definition UnixRouter.hpp:78
Schema registration and validation
Each command is associated with its own JSON schema (typically describing the params object). During routing:
  • the router calls the registered wrapper,
  • the wrapper validates params against the schema registered for that command using Utils::JsonValidator::validate(),
  • on validation failure, the session is replied to via session->replyError(errors, id) and the concrete handler is not called.

Validation is performed with the JSON pointer /params as the validation root, so error paths are reported relative to the params object.

Note
When validation fails, the wrapper returns true (i.e. "handled, keep the session alive"). This matches the router/session model where protocol errors are non-fatal to the connection.

Constructor & Destructor Documentation

◆ BaseCommandHandler()

dfx::Server::Api::BaseCommandHandler::BaseCommandHandler ( )
default

Default constructor.

Constructs an empty helper with its own Utils::JsonValidator. Concrete handlers typically register their commands during construction.

Member Function Documentation

◆ registerNewCommand()

template<typename PMF>
void dfx::Server::Api::BaseCommandHandler::registerNewCommand ( UnixRouter & router,
std::string command,
nlohmann::json const & schema,
PMF handler )
inlineprotected

Register a new command in the router with an associated params schema and member handler.

This helper enforces at compile time that handler is a pointer to a member function of a class derived from BaseCommandHandler (and publicly so).

Internally, it:

  • registers schema into this instance's internal validator under the key command,
  • registers a router handler that first validates params,
  • and only then invokes the derived member function.

The registered router handler has the signature: bool (UnixSessionPtr session, nlohmann::json params, std::string id).

Note
Ownership The wrapper captures this, so the concrete handler object must outlive the router usage.
Template Parameters
PMFPointer-to-member-function type.
Parameters
routerRouter that will dispatch incoming requests.
commandCommand name as used in the JSON request envelope.
schemaJSON schema used to validate the request params for this command.
handlerPointer to a member function of the derived handler class.

◆ validateParams()

bool dfx::Server::Api::BaseCommandHandler::validateParams ( std::string_view command,
nlohmann::json const & params,
std::string_view id,
UnixSessionPtr session ) const
protected

Validate params for a command and reply with errors on failure.

Performs schema validation using: Utils::JsonValidator::validate(command, params, "/params"_json_pointer).

On failure, replies with all validation errors via: session->replyError(validation.errors, id).

Parameters
commandCommand name used as the schema key.
paramsParameters object to validate.
idRequest identifier to echo back in the error reply.
sessionSession used to send error replies.
Return values
trueParameters are valid.
falseParameters are invalid (an error reply has been sent).

The documentation for this class was generated from the following file: