Class SSEFaultHandler
Defined in File sse_fault_handler.hpp
Nested Relationships
Nested Types
Class Documentation
-
class SSEFaultHandler
Handler for Server-Sent Events (SSE) fault streaming.
Provides real-time fault event notifications via SSE at:
GET /faults/stream
Events streamed:
fault_confirmed: When a fault transitions to CONFIRMED status
fault_cleared: When a fault is manually cleared
fault_updated: When fault data changes (occurrence_count, sources)
Features:
Multi-client support (multiple browsers can connect simultaneously)
Keepalive every 30 seconds to prevent connection timeout
Automatic reconnection support via Last-Event-ID header
Replay buffer of up to 100 most recent events for reconnecting clients; when the buffer is full, older events are discarded (FIFO), so clients that are disconnected for long periods may miss some events
Public Functions
Construct SSE fault handler with shared context.
- Parameters:
ctx – The shared handler context
client_tracker – Shared SSE client counter (across all SSE handlers)
Test-only constructor that overrides the keepalive interval.
- Parameters:
ctx – The shared handler context
client_tracker – Shared SSE client counter (across all SSE handlers)
keepalive_interval – Must be positive; non-positive values fall back to the 30s default.
-
~SSEFaultHandler()
Destructor - cleanup subscription.
-
SSEFaultHandler(const SSEFaultHandler&) = delete
-
SSEFaultHandler &operator=(const SSEFaultHandler&) = delete
-
SSEFaultHandler(SSEFaultHandler&&) = delete
-
SSEFaultHandler &operator=(SSEFaultHandler&&) = delete
-
http::Result<http::SseStream> sse_stream(const http::TypedRequest &req)
Handle GET /faults/stream - SSE stream endpoint (typed RouteRegistry).
Returns a
SseStreamwhosenext_eventcallback the framework drives via cpp-httplib’s chunked content provider. On limit-exceeded the factory returnstl::unexpected(ErrorInfo)with HTTP 503; the framework renders a SOVD GenericError.Events streamed:
event: fault_confirmed data: {"event_type":"fault_confirmed","fault":{...},"timestamp":1234567890.123} event: fault_cleared data: {"event_type":"fault_cleared","fault":{...},"timestamp":1234567890.456}
-
void handle_stream(const httplib::Request &req, httplib::Response &res)
Legacy SSE entry point - drives the chunked content provider on
resdirectly.Retained for the in-process unit test fixture (
test_sse_fault_handler), which exercises the streaming loop without spinning up the typed router. The framework-registered route usessse_streamviareg.sse; this overload wraps the same logic and additionally sets the legacy headers (Cache-Control / Connection / X-Accel-Buffering) that the framework wires automatically for the typed path.
-
size_t connected_clients() const
Get the number of currently connected SSE clients.
-
void request_shutdown()
Signal shutdown so in-flight chunked-content-provider loops exit.
Call this BEFORE stopping the HTTP server. The server thread’s join waits for active request lambdas to return; the SSE lambda sleeps on queue_cv_ until keepalive (30s) so without an early signal the join can exceed the launch_testing shutdown budget (5s SIGINT + 10s SIGTERM) and the process ends up SIGKILLed (exit -9). Setting the flag and notifying wakes the lambda, it returns false, and the http thread exits promptly. Safe to call more than once.