Class SSEStreamProxy

Inheritance Relationships

Base Type

Class Documentation

class SSEStreamProxy : public ros2_medkit_gateway::StreamProxy

SSE (Server-Sent Events) implementation of StreamProxy.

Connects to a peer gateway’s SSE endpoint using cpp-httplib and parses the SSE text/event-stream format into StreamEvent objects. Runs a background reader thread that invokes the registered callback for each parsed event.

Thread safety: connected_ and should_stop_ are atomic. The callback is set before open() and not modified afterwards.

Public Functions

SSEStreamProxy(const std::string &peer_url, const std::string &path, const std::string &peer_name = "")

Construct an SSEStreamProxy.

Parameters:
  • peer_url – Base URL of the peer gateway (e.g., “http://localhost:8081”)

  • path – SSE endpoint path (e.g., “/api/v1/components/abc/faults/sse”)

  • peer_name – Human-readable name for the peer (used in StreamEvent::peer_name)

~SSEStreamProxy() override
virtual void open() override

Start the streaming connection (non-blocking, spawns reader thread)

virtual void close() override

Stop the streaming connection and join the reader thread.

virtual bool is_connected() const override

Check if the streaming connection is currently active.

virtual void on_event(std::function<void(const StreamEvent&)> cb) override

Register a callback for incoming stream events.

Public Static Functions

static std::vector<StreamEvent> parse_sse_data(const std::string &raw, const std::string &peer)

Parse raw SSE text/event-stream data into StreamEvent objects.

SSE format: events are separated by blank lines. Each event consists of field lines: “event:”, “data:”, “id:”. Multiple “data:” lines are joined with newlines.

This is a pure function suitable for unit testing without networking.

Parameters:
  • raw – Raw SSE text data

  • peer – Peer name to set on each parsed event

Returns:

Vector of parsed StreamEvent objects