feat(comms): Stage 1 — CommandBus scaffolding (headers + skeleton)
Adds the core data structures and interfaces for the comms refactor. No behaviour change — old CommandHandler is completely untouched. New files: CommandEnvelope.hpp — universal message wrapper (transport + command + reply lambda) CommandResult.hpp — standardised return type with ok/err/info factories ICommandHandler.hpp — interface every domain handler will implement ITransport.hpp — interface every transport layer will implement CommandBus.hpp/.cpp — registry + dispatcher (std::map, O(log n) lookup) Two minor corrections vs. plan: std::unordered_map → std::map (Arduino String has no std::hash) LOG_WARN → LOG_WARNING (codebase macro name) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <vector>
|
||||
#include "../CommandBus/CommandEnvelope.hpp"
|
||||
#include "../CommandBus/CommandResult.hpp"
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ICommandHandler
|
||||
//
|
||||
// Interface every domain handler implements.
|
||||
//
|
||||
// - commands() declares which command names this handler owns.
|
||||
// The CommandBus iterates this list at registration time and builds
|
||||
// its lookup map automatically — no manual registration per command.
|
||||
//
|
||||
// - execute() receives a fully-parsed CommandEnvelope and returns a
|
||||
// CommandResult. It must never call envelope.reply() directly —
|
||||
// the bus handles that after execute() returns.
|
||||
//
|
||||
// Example:
|
||||
// class PlaybackHandler : public ICommandHandler {
|
||||
// public:
|
||||
// explicit PlaybackHandler(Player& player);
|
||||
// std::vector<String> commands() const override { return { "playback" }; }
|
||||
// CommandResult execute(const CommandEnvelope& envelope) override;
|
||||
// private:
|
||||
// Player& _player;
|
||||
// };
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
class ICommandHandler {
|
||||
public:
|
||||
virtual ~ICommandHandler() = default;
|
||||
|
||||
// Returns every command name this handler is responsible for.
|
||||
// The bus registers all of them to the same handler instance.
|
||||
virtual std::vector<String> commands() const = 0;
|
||||
|
||||
// Execute the command described by envelope and return the result.
|
||||
// Must not throw. Must not call envelope.reply() — the bus owns that.
|
||||
virtual CommandResult execute(const CommandEnvelope& envelope) = 0;
|
||||
};
|
||||
Reference in New Issue
Block a user