dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
BaseCommandHandler.hpp
1// SPDX-FileCopyrightText: 2025 Vincent Leroy
2// SPDX-License-Identifier: MIT
3//
4// This file is part of dfx.
5//
6// Licensed under the MIT License. See the LICENSE file in the project root
7// for full license information.
8
9#pragma once
10
11// Project includes
12#include <dfx-utilities/JsonValidator.hpp>
13#include <dfx-utilities/SFINAEHelpers.hpp>
14
15namespace dfx::Server
16{
17class UnixRouter;
18
19class UnixSession;
20using UnixSessionPtr = std::shared_ptr<UnixSession>;
21} // !namespace dfx::Server
22
24{
75{
76 using Handler = std::move_only_function<bool (UnixSessionPtr, nlohmann::json, std::string)>;
77
78public:
83 BaseCommandHandler() = default;
84
85protected:
108 template<typename PMF>
109 void registerNewCommand(UnixRouter & router, std::string command,
110 nlohmann::json const & schema, PMF handler);
111
127 bool validateParams(std::string_view command, nlohmann::json const & params,
128 std::string_view id, UnixSessionPtr session) const;
129
130private:
131 void _registerNewCommandImpl(UnixRouter & router, std::string command,
132 nlohmann::json const & schema, Handler handler);
133
134private:
135 Utils::JsonValidator _validator;
136};
137
138template<typename PMF>
139inline void BaseCommandHandler::registerNewCommand(UnixRouter & router, std::string command,
140 nlohmann::json const & schema, PMF handler)
141{
142 static_assert(std::is_member_function_pointer_v<PMF>, "The handler must be a member function pointer");
143 using C = Utils::SFINAE::memberClass_t<PMF>;
144 static_assert(std::is_base_of_v<BaseCommandHandler, C>, "The handler must be a member function of a class derived from BaseCommandHandler");
145 static_assert(std::is_convertible_v<C, BaseCommandHandler>, "The base class must inherit from BaseCommandHandler publicly");
146
147 _registerNewCommandImpl(router, command, schema,
148 [handler, this](UnixSessionPtr session, nlohmann::json params, std::string id)
149 { return std::invoke(handler, static_cast<C *>(this), session, std::move(params), std::move(id)); });
150}
151} // !namespace dfx::Server::Api
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
BaseCommandHandler()=default
Default constructor.
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.
JSON command router for the Unix domain socket control protocol.
Definition UnixRouter.hpp:78
One connected client session of a UnixServer control socket.
Definition UnixSession.hpp:76
Registry of JSON Schemas with validation helpers.
Definition JsonValidator.hpp:46
Definition BaseCommandHandler.hpp:24
Definition BaseCommandHandler.hpp:16
std::shared_ptr< UnixSession > UnixSessionPtr
Shared ownership pointer type for sessions.
Definition BaseCommandHandler.hpp:20