148 lines
4.8 KiB
Python
148 lines
4.8 KiB
Python
"""Control-plane handshake + heartbeat over a unix socket (plan todo 5).
|
|
|
|
The control side carries ONLY handshake/config/calibration/heartbeat messages
|
|
(never per-frame data, which travels through the shared-memory FrameContract).
|
|
This module serialises the frozen `ControlMessage*` ctypes structs and provides
|
|
a tiny non-blocking server the tracker runs alongside the publish loop: it
|
|
accepts a consumer connection, sends a handshake once, and emits a heartbeat
|
|
carrying the last published sequence as frames are published.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import ctypes
|
|
import socket
|
|
import threading
|
|
import time
|
|
from typing import Optional
|
|
|
|
from .contract import (
|
|
CONTRACT_VERSION,
|
|
ControlMessageHeader,
|
|
ControlMessageType,
|
|
HandshakeMessage,
|
|
HeartbeatMessage,
|
|
)
|
|
|
|
|
|
def now_micros() -> int:
|
|
return time.monotonic_ns() // 1000
|
|
|
|
|
|
def build_handshake(client_version: int = CONTRACT_VERSION, capabilities: int = 0) -> bytes:
|
|
msg = HandshakeMessage()
|
|
msg.header.message_type = ControlMessageType.HANDSHAKE_VERSION
|
|
msg.header.contract_version = CONTRACT_VERSION
|
|
msg.header.payload_size = ctypes.sizeof(HandshakeMessage)
|
|
msg.header.reserved = 0
|
|
msg.client_version = client_version
|
|
msg.capabilities = capabilities
|
|
return bytes(msg)
|
|
|
|
|
|
def build_heartbeat(sequence: int, timestamp_micros: Optional[int] = None) -> bytes:
|
|
msg = HeartbeatMessage()
|
|
msg.header.message_type = ControlMessageType.HEARTBEAT
|
|
msg.header.contract_version = CONTRACT_VERSION
|
|
msg.header.payload_size = ctypes.sizeof(HeartbeatMessage)
|
|
msg.header.reserved = 0
|
|
msg.timestamp_micros = now_micros() if timestamp_micros is None else timestamp_micros
|
|
msg.sequence = sequence
|
|
return bytes(msg)
|
|
|
|
|
|
def parse_header(buf: bytes) -> ControlMessageHeader:
|
|
return ControlMessageHeader.from_buffer_copy(buf[: ctypes.sizeof(ControlMessageHeader)])
|
|
|
|
|
|
def parse_message(buf: bytes):
|
|
header = parse_header(buf)
|
|
if header.message_type == ControlMessageType.HANDSHAKE_VERSION:
|
|
return HandshakeMessage.from_buffer_copy(buf[: ctypes.sizeof(HandshakeMessage)])
|
|
if header.message_type == ControlMessageType.HEARTBEAT:
|
|
return HeartbeatMessage.from_buffer_copy(buf[: ctypes.sizeof(HeartbeatMessage)])
|
|
return header
|
|
|
|
|
|
class ControlPlaneServer:
|
|
"""Accepts one consumer on a unix socket; sends handshake then heartbeats.
|
|
|
|
Non-blocking by design: `send_heartbeat` is a no-op until a consumer
|
|
connects, so the publish loop never stalls on the control plane.
|
|
"""
|
|
|
|
def __init__(self, socket_path: str) -> None:
|
|
self.socket_path = socket_path
|
|
self._listener: Optional[socket.socket] = None
|
|
self._conn: Optional[socket.socket] = None
|
|
self._lock = threading.Lock()
|
|
self._stop = threading.Event()
|
|
self._thread: Optional[threading.Thread] = None
|
|
|
|
def start(self) -> None:
|
|
import os
|
|
|
|
try:
|
|
os.unlink(self.socket_path)
|
|
except FileNotFoundError:
|
|
pass
|
|
self._listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
self._listener.bind(self.socket_path)
|
|
self._listener.listen(1)
|
|
self._listener.settimeout(0.5)
|
|
self._thread = threading.Thread(target=self._accept_loop, name="control-accept", daemon=True)
|
|
self._thread.start()
|
|
|
|
def _accept_loop(self) -> None:
|
|
while not self._stop.is_set():
|
|
try:
|
|
conn, _ = self._listener.accept()
|
|
except socket.timeout:
|
|
continue
|
|
except OSError:
|
|
break
|
|
with self._lock:
|
|
old = self._conn
|
|
self._conn = conn
|
|
if old is not None:
|
|
old.close()
|
|
try:
|
|
conn.sendall(build_handshake())
|
|
except OSError:
|
|
with self._lock:
|
|
if self._conn is conn:
|
|
self._conn = None
|
|
conn.close()
|
|
|
|
def send_heartbeat(self, sequence: int) -> bool:
|
|
with self._lock:
|
|
conn = self._conn
|
|
if conn is None:
|
|
return False
|
|
try:
|
|
conn.sendall(build_heartbeat(sequence))
|
|
return True
|
|
except OSError:
|
|
with self._lock:
|
|
if self._conn is conn:
|
|
self._conn = None
|
|
conn.close()
|
|
return False
|
|
|
|
def close(self) -> None:
|
|
self._stop.set()
|
|
if self._listener is not None:
|
|
self._listener.close()
|
|
with self._lock:
|
|
if self._conn is not None:
|
|
self._conn.close()
|
|
self._conn = None
|
|
if self._thread is not None:
|
|
self._thread.join(timeout=1.0)
|
|
import os
|
|
|
|
try:
|
|
os.unlink(self.socket_path)
|
|
except FileNotFoundError:
|
|
pass
|