dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
UnixRouter.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// Standard includes
12#include <functional>
13#include <string>
14
15// Third-party includes
16#include <nlohmann/json.hpp>
17
18// Project includes
19#include <dfx-utilities/StringMap.hpp>
20#include <dfx-utilities/JsonValidator.hpp>
21
22namespace dfx::Server
23{
24class UnixSession;
25using UnixSessionPtr = std::shared_ptr<UnixSession>;
26
78{
79public:
88 using Handler = std::move_only_function<bool (UnixSessionPtr, nlohmann::json, std::string)>;
89
95
98
111 void registerCommand(std::string command, Handler handler, bool allowOverride = false);
112
120 bool deregisterCommand(std::string_view command);
121
127 bool isRegistered(std::string_view command) const;
128
146 bool route(UnixSessionPtr session, nlohmann::json json);
147
148private:
149 std::vector<std::string> _possibleCommands;
151 Utils::JsonValidator _validator;
152};
153} // !namespace dfx::Server
std::move_only_function< bool(UnixSessionPtr, nlohmann::json, std::string)> Handler
Handler type for a routed command.
Definition UnixRouter.hpp:88
bool deregisterCommand(std::string_view command)
Deregister a command handler.
UnixRouter()
Construct the router and register the request envelope schema.
bool route(UnixSessionPtr session, nlohmann::json json)
Validate and route a JSON request to the appropriate command handler.
~UnixRouter()
Destructor.
void registerCommand(std::string command, Handler handler, bool allowOverride=false)
Register a command handler.
bool isRegistered(std::string_view command) const
Check whether a command is registered.
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:16
std::shared_ptr< UnixSession > UnixSessionPtr
Shared ownership pointer type for sessions.
Definition BaseCommandHandler.hpp:20
std::unordered_map< std::string, T, TransparentHash, TransparentEqual, Allocator > UnorderedStringMap
Convenience alias for an unordered map keyed by std::string with transparent lookup.
Definition StringMap.hpp:76