Files
project-vesper/src/Communication/CommandBus/CommandBus.hpp
T
bonaminandClaude Sonnet 4.6 6294044569 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>
2026-02-28 22:04:51 +02:00

60 lines
2.2 KiB
C++

#pragma once
#include <Arduino.h>
#include <vector>
#include <memory>
#include <map>
#include "CommandEnvelope.hpp"
#include "CommandResult.hpp"
#include "../Handlers/ICommandHandler.hpp"
#include "../Transport/ITransport.hpp"
// ---------------------------------------------------------------------------
// CommandBus
//
// Registry + dispatcher. Replaces the 700-line if/else chain in CommandHandler.
//
// Usage:
// // Registration (once, in CommunicationRouter::registerHandlers())
// _bus.registerHandler(std::make_unique<PlaybackHandler>(_player));
// _bus.addTransport(&_mqtt);
//
// // Dispatch (called from each transport's message callback)
// _bus.dispatch(envelope);
//
// // Broadcast event to all transports
// _bus.broadcast(CommandResult::info("bell.strike", "Bell 2 fired"));
// ---------------------------------------------------------------------------
class CommandBus {
public:
// Register a handler. All command names reported by handler->commands()
// are mapped to this handler instance. Duplicate command names log a
// warning and the later registration wins.
void registerHandler(std::unique_ptr<ICommandHandler> handler);
// Add a transport that receives broadcasts and participates in health checks.
void addTransport(ITransport* transport);
// Dispatch an envelope to the registered handler (O(1) hash-map lookup).
// Calls envelope.reply() exactly once — with an error result if no handler
// is registered for envelope.command.
void dispatch(CommandEnvelope& envelope);
// Broadcast a serialised result to all registered transports.
// Each transport's broadcast() implementation decides how to fan it out
// (e.g. WebSocket sends to all connected clients; HTTP is a no-op).
void broadcast(const CommandResult& result);
private:
// Owned storage for handler instances
std::vector<std::unique_ptr<ICommandHandler>> _handlers;
// Command name → handler pointer (non-owning, points into _handlers)
// std::map used instead of unordered_map — Arduino String has no std::hash specialisation
std::map<String, ICommandHandler*> _registry;
// Non-owning references to transports (owned by CommunicationRouter)
std::vector<ITransport*> _transports;
};