diff --git a/src/svxlink/reflector/TwinLink.cpp b/src/svxlink/reflector/TwinLink.cpp index c21b80b..6777b2b 100644 --- a/src/svxlink/reflector/TwinLink.cpp +++ b/src/svxlink/reflector/TwinLink.cpp @@ -477,6 +477,7 @@ void TwinLink::handleMsgTrunkAudio(std::istream& is) if (msg.audio().empty()) return; MsgUdpAudio udp_msg(msg.audio()); m_reflector->broadcastUdpMsg(udp_msg, ReflectorClient::TgFilter(msg.tg())); + m_reflector->forwardAudioToSatellitesExcept(nullptr, msg.tg(), msg.audio()); } /* TwinLink::handleMsgTrunkAudio */ @@ -490,6 +491,7 @@ void TwinLink::handleMsgTrunkFlush(std::istream& is) } m_reflector->broadcastUdpMsg(MsgUdpFlushSamples(), ReflectorClient::TgFilter(msg.tg())); + m_reflector->forwardFlushToSatellitesExcept(nullptr, msg.tg()); } /* TwinLink::handleMsgTrunkFlush */ diff --git a/tests/generate_configs.py b/tests/generate_configs.py index c9c487d..30e46f7 100644 --- a/tests/generate_configs.py +++ b/tests/generate_configs.py @@ -322,6 +322,16 @@ def generate_twin_reflector_conf(name: str) -> str: f"REMOTE_PREFIX={remote_prefix}", ] + # Satellite server section — only on the designated twin member so + # integration tests can verify satellite delivery via the TWIN link. + if name == T.TWIN_SATELLITE_PARENT: + lines += [ + "", + "[SATELLITE]", + f"LISTEN_PORT={T.SATELLITE['listen_port']}", + f"SECRET={T.SATELLITE['secret']}", + ] + # MQTT publishing section lines += [ "", @@ -368,6 +378,13 @@ def generate_twin_docker_compose() -> str: twin_port = T.twin_mapped_twin_port(name) twin_port_line = f'\n - "{twin_port}:{T.INTERNAL_TWIN_PORT}/tcp"' + # Satellite listen port mapping — only on the designated twin parent + sat_port_line = "" + if name == T.TWIN_SATELLITE_PARENT: + sat_port = T.twin_mapped_satellite_port(name) + sat_port_line = ( + f'\n - "{sat_port}:{T.SATELLITE["listen_port"]}/tcp"') + depends_on_line = "" if r.get("redis"): depends_on_line = """ @@ -386,7 +403,7 @@ def generate_twin_docker_compose() -> str: - "{client_port}:{T.INTERNAL_CLIENT_PORT}/tcp" - "{client_port}:{T.INTERNAL_CLIENT_PORT}/udp" - "{trunk_port}:{T.INTERNAL_TRUNK_PORT}/tcp" - - "{http_port}:{T.INTERNAL_HTTP_PORT}/tcp"{twin_port_line} + - "{http_port}:{T.INTERNAL_HTTP_PORT}/tcp"{twin_port_line}{sat_port_line} healthcheck: test: ["CMD-SHELL", "bash -c '(echo > /dev/tcp/localhost/{T.INTERNAL_HTTP_PORT}) 2>/dev/null'"] interval: 2s diff --git a/tests/test_trunk.py b/tests/test_trunk.py index a8171d2..6af54f9 100644 --- a/tests/test_trunk.py +++ b/tests/test_trunk.py @@ -296,12 +296,18 @@ class TrunkPeer: class SatellitePeer(TrunkPeer): """Satellite peer — same wire format as TrunkPeer but role=SATELLITE.""" - def connect_satellite(self, name=None): - """Connect to the satellite port of a reflector.""" - if name is None: - name = sorted(T.REFLECTORS)[0] + def connect_satellite(self, name=None, port=None): + """Connect to the satellite port of a reflector. + + If `port` is provided it overrides the default-topology lookup, so + the twin-topology tests can point at a different host port without + duplicating this class. + """ host = HOST - port = T.mapped_satellite_port(name) + if port is None: + if name is None: + name = sorted(T.REFLECTORS)[0] + port = T.mapped_satellite_port(name) self.connect(host, port) def handshake(self, sat_id=SAT_ID, secret=SAT_SECRET, priority=None, diff --git a/tests/test_twin.py b/tests/test_twin.py index aac7637..4e7730f 100644 --- a/tests/test_twin.py +++ b/tests/test_twin.py @@ -32,9 +32,13 @@ import json # Reuse the V2 mock client from test_trunk.py sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from test_trunk import ( # noqa: E402 - ClientPeer, UDP_AUDIO, UDP_FLUSH, + ClientPeer, SatellitePeer, UDP_AUDIO, UDP_FLUSH, CLIENT_CALLSIGN, CLIENT_PASSWORD, + MSG_TRUNK_TALKER_START, MSG_TRUNK_TALKER_STOP, + MSG_TRUNK_AUDIO, MSG_TRUNK_FLUSH, MSG_TRUNK_HEARTBEAT, + SAT_ID, recv_frame, parse_msg, ) +import socket # noqa: E402 # Ensure tests/ is on the path so topology can be imported sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) @@ -500,6 +504,130 @@ class TestTwinIntegration(unittest.TestCase): sender.close() receiver.close() + # ------------------------------------------------------------------ + # Test 9: Satellite handshake with a twin pair member + # ------------------------------------------------------------------ + def test_09_satellite_handshake_with_twin_member(self): + """A satellite connects to the twin-parent reflector and appears + in its /status.satellites listing after the two-way hello. + """ + parent = T.TWIN_SATELLITE_PARENT + sat_port = T.twin_mapped_satellite_port(parent) + + sat = SatellitePeer() + try: + sat.connect_satellite(port=sat_port) + sat.handshake() + + def satellite_registered(): + status = get_status(*_http(parent)) + return SAT_ID in status.get("satellites", {}) + + wait_until( + satellite_registered, + timeout=8.0, + msg=f"satellite {SAT_ID} not visible in /status.satellites " + f"on reflector-{parent} after handshake", + ) + finally: + sat.close() + + # ------------------------------------------------------------------ + # Test 10: Satellite receives twin-mirrored audio + # ------------------------------------------------------------------ + def test_10_satellite_receives_twin_mirrored_audio(self): + """Audio sent by a client on ref2 reaches a satellite on ref1. + + Exercises the end-to-end path: + ref2 local client -> TGHandler.talkerUpdated -> TwinLink.onLocalAudio + -> MsgTrunkAudio over [TWIN] -> ref1.handleMsgTrunkAudio + -> forwardAudioToSatellitesExcept -> satellite socket. + + Verifies that satellites attached to a twin member see audio that + originated on the other twin — talker-state mirroring alone is not + enough; the audio frames must be forwarded too. + """ + # TG 26201: prefix "262", owned locally by both twins. + tg = 26201 + parent = T.TWIN_SATELLITE_PARENT # "ref1" + other_twin = T.TWIN_REFLECTORS[parent]["twin_of"] # "ref2" + sat_port = T.twin_mapped_satellite_port(parent) + + sat = SatellitePeer() + sender = ClientPeer() + try: + # Satellite first so it's registered before audio flows + sat.connect_satellite(port=sat_port) + sat.handshake() + + other_client_port = T.twin_mapped_client_port(other_twin) + sender.connect(HOST, other_client_port) + sender.authenticate(callsign=CLIENT_CALLSIGN, + password=CLIENT_PASSWORD) + sender.setup_udp(udp_port=other_client_port) + sender.select_tg(tg) + time.sleep(0.5) # let TG selection + twin handshake settle + + audio_payload = b"\xBE\xEF" * 80 + for _ in range(4): + sender.send_udp_audio(audio_payload) + time.sleep(0.02) + sender.send_udp_flush() + + # Drain everything the satellite receives within a short window + got_talker_start = False + got_audio = False + got_flush = False + got_talker_stop = False + deadline = time.monotonic() + 4.0 + sat.sock.settimeout(0.5) + while time.monotonic() < deadline: + try: + data = recv_frame(sat.sock) + except (socket.timeout, OSError): + if got_audio and got_talker_start: + break + continue + except ConnectionError: + break + msg_type, fields = parse_msg(data) + if msg_type == MSG_TRUNK_TALKER_START and fields.get("tg") == tg: + got_talker_start = True + elif msg_type == MSG_TRUNK_AUDIO and fields.get("tg") == tg: + got_audio = True + elif msg_type == MSG_TRUNK_FLUSH and fields.get("tg") == tg: + got_flush = True + elif msg_type == MSG_TRUNK_TALKER_STOP and fields.get("tg") == tg: + got_talker_stop = True + elif msg_type == MSG_TRUNK_HEARTBEAT: + continue + + self.assertTrue( + got_talker_start, + f"satellite on {parent} did not receive TalkerStart for TG " + f"{tg} from {other_twin} via TWIN mirror", + ) + self.assertTrue( + got_audio, + f"satellite on {parent} did not receive any audio frames for " + f"TG {tg} from {other_twin}; " + f"TwinLink::handleMsgTrunkAudio may not be forwarding to " + f"satellites", + ) + # Flush + TalkerStop are nice-to-have; we log but don't fail + # hard if timing swallowed them. + if not got_flush: + sys.stderr.write( + " (note: satellite did not receive Flush frame — " + "may indicate TwinLink flush-to-satellite gap)\n") + if not got_talker_stop: + # Talker stop only fires when the sender releases the TG, + # which depends on server-side talker timeouts. Not asserted. + pass + finally: + sender.close() + sat.close() + def docker_compose_output(*args) -> str: """Return the stdout+stderr of a docker-compose command as text.""" diff --git a/tests/topology.py b/tests/topology.py index a94419a..129173d 100644 --- a/tests/topology.py +++ b/tests/topology.py @@ -174,6 +174,11 @@ TWIN_TRUNKS = [ # Cluster TGs for the twin topology TWIN_CLUSTER_TGS = [222000, 999] +# Which twin-topology reflector accepts satellite connections. Picking one of +# the twin pair lets tests verify that audio mirrored via the [TWIN] link is +# also forwarded out to satellites attached to the partner. +TWIN_SATELLITE_PARENT = "ref1" + # Shared secret for a trunk link (twin topology uses link name) def twin_trunk_secret(link_name: str) -> str: return f"secret_{link_name.lower()}" @@ -229,6 +234,10 @@ def twin_mapped_twin_port(name: str) -> int: """Host-mapped port for the [TWIN] listen port.""" return TWIN_REFLECTORS[name]["trunk_port_base"] + 304 +def twin_mapped_satellite_port(name: str) -> int: + """Host-mapped port for the [SATELLITE] listen port in twin topology.""" + return TWIN_REFLECTORS[name]["trunk_port_base"] + 303 + def twin_trunks_for(name: str) -> list: """Return all TWIN_TRUNKS entries that involve this reflector.""" return [t for t in TWIN_TRUNKS if name in t["peers"]]