diff --git a/src/svxlink/reflector/TrunkLink.cpp b/src/svxlink/reflector/TrunkLink.cpp index be2627d..d74e39f 100644 --- a/src/svxlink/reflector/TrunkLink.cpp +++ b/src/svxlink/reflector/TrunkLink.cpp @@ -1378,6 +1378,7 @@ size_t TrunkLink::pairedClientIndex(FramedTcpClient* client) const void TrunkLink::sendMsgOnPairedOutbound(size_t idx, const ReflectorMsg& msg) { if (idx >= m_ob_cons.size()) return; + if (!m_ob_cons[idx]->isConnected()) return; ostringstream ss; ReflectorMsg header(msg.type()); if (!header.pack(ss) || !msg.pack(ss)) @@ -1405,6 +1406,7 @@ size_t TrunkLink::pairedInboundIndex(Async::FramedTcpConnection* con) const void TrunkLink::sendMsgOnPairedInbound(size_t idx, const ReflectorMsg& msg) { if (idx >= m_ib_cons.size()) return; + if (!m_ib_cons[idx]->isConnected()) return; ostringstream ss; ReflectorMsg header(msg.type()); if (!header.pack(ss) || !msg.pack(ss)) diff --git a/src/svxlink/reflector/TwinLink.cpp b/src/svxlink/reflector/TwinLink.cpp index 4681b9e..eae643b 100644 --- a/src/svxlink/reflector/TwinLink.cpp +++ b/src/svxlink/reflector/TwinLink.cpp @@ -566,6 +566,7 @@ bool TwinLink::sendMsg(const ReflectorMsg& msg) void TwinLink::sendMsgOnOutbound(const ReflectorMsg& msg) { + if (!m_con.isConnected()) return; ostringstream ss; ReflectorMsg header(msg.type()); if (!header.pack(ss) || !msg.pack(ss)) @@ -581,7 +582,7 @@ void TwinLink::sendMsgOnOutbound(const ReflectorMsg& msg) void TwinLink::sendMsgOnInbound(const ReflectorMsg& msg) { - if (m_inbound_con == nullptr) return; + if (m_inbound_con == nullptr || !m_inbound_con->isConnected()) return; ostringstream ss; ReflectorMsg header(msg.type()); if (!header.pack(ss) || !msg.pack(ss)) diff --git a/tests/test_twin.py b/tests/test_twin.py index 9913e61..cc96ed2 100644 --- a/tests/test_twin.py +++ b/tests/test_twin.py @@ -5,12 +5,12 @@ Verifies the TWIN handshake, PAIRED trunk wiring, talker-state mirroring via log inspection, and recovery after container failure. Topology used: TWIN (generate_configs.py --topology twin) - - reflector-refA (IT, LOCAL_PREFIX=222) + - reflector-refa (IT, LOCAL_PREFIX=222) - reflector-ref1 (DE twin 1, LOCAL_PREFIX=262, [TWIN] partner=ref2) - reflector-ref2 (DE twin 2, LOCAL_PREFIX=262, [TWIN] partner=ref1) - - reflector-refC (extra peer, LOCAL_PREFIX=333) + - reflector-refc (extra peer, LOCAL_PREFIX=333) - refA connects to ref1 AND ref2 via a single PAIRED=1 trunk section (TRUNK_IT_DE). + refa connects to ref1 AND ref2 via a single PAIRED=1 trunk section (TRUNK_IT_DE). ref1 and ref2 maintain a direct [TWIN] link for state mirroring. Requires: Python 3.7+, stdlib only (+ topology.py from this directory). @@ -138,17 +138,17 @@ class TestTwinIntegration(unittest.TestCase): sys.stderr.write(f"\r\033[K {G}\u2714{RST} Twin reflectors up\n") sys.stderr.flush() - # Wait for trunk connections between refA↔ref1 and refA↔ref2 + # Wait for trunk connections between refa↔ref1 and refa↔ref2 sys.stderr.write(f" {D}waiting for trunk mesh...{RST}") sys.stderr.flush() - # refA has TRUNK_IT_DE (PAIRED=1 → ref1 + ref2) - wait_for_trunk_connected(*_http("refA"), "TRUNK_IT_DE", timeout=30.0) - # ref1 and ref2 each have TRUNK_IT_DE back to refA + # refa has TRUNK_IT_DE (PAIRED=1 → ref1 + ref2) + wait_for_trunk_connected(*_http("refa"), "TRUNK_IT_DE", timeout=30.0) + # ref1 and ref2 each have TRUNK_IT_DE back to refa wait_for_trunk_connected(*_http("ref1"), "TRUNK_IT_DE", timeout=30.0) wait_for_trunk_connected(*_http("ref2"), "TRUNK_IT_DE", timeout=30.0) - # refC links - wait_for_trunk_connected(*_http("refA"), "TRUNK_IT_C", timeout=30.0) - wait_for_trunk_connected(*_http("refC"), "TRUNK_IT_C", timeout=30.0) + # refc links + wait_for_trunk_connected(*_http("refa"), "TRUNK_IT_C", timeout=30.0) + wait_for_trunk_connected(*_http("refc"), "TRUNK_IT_C", timeout=30.0) sys.stderr.write(f"\r\033[K {G}\u2714{RST} Trunk mesh connected\n") sys.stderr.flush() @@ -203,33 +203,33 @@ class TestTwinIntegration(unittest.TestCase): ) # ------------------------------------------------------------------ - # Test 3: PAIRED trunk — refA connected to BOTH ref1 and ref2 + # Test 3: PAIRED trunk — refa connected to BOTH ref1 and ref2 # ------------------------------------------------------------------ def test_03_paired_trunk_both_partners_connected(self): - """refA's TRUNK_IT_DE (PAIRED=1) reports connected=True. + """refa's TRUNK_IT_DE (PAIRED=1) reports connected=True. This confirms that TrunkLink with PAIRED=1 and two peer hosts reaches the 'active' state after both outbound connections complete their handshake (or at least one, since isActive() returns true when either outbound or inbound is ready). """ - status_a = get_status(*_http("refA")) + status_a = get_status(*_http("refa")) trunk = status_a.get("trunks", {}).get("TRUNK_IT_DE", None) self.assertIsNotNone(trunk, - "TRUNK_IT_DE not present in refA /status. " + "TRUNK_IT_DE not present in refa /status. " "Was the twin topology generated and docker-compose started?") self.assertTrue( trunk.get("connected", False), - f"TRUNK_IT_DE on refA is not connected. trunk status: {trunk}", + f"TRUNK_IT_DE on refa is not connected. trunk status: {trunk}", ) # ------------------------------------------------------------------ - # Test 4: PAIRED trunk — both pair members see refA as connected + # Test 4: PAIRED trunk — both pair members see refa as connected # ------------------------------------------------------------------ def test_04_paired_trunk_pair_members_see_refa(self): """ref1 and ref2 each see their TRUNK_IT_DE section as connected. - Confirms the return leg (pair member → refA) is also established. + Confirms the return leg (pair member → refa) is also established. """ for name in ("ref1", "ref2"): status = get_status(*_http(name)) @@ -362,17 +362,17 @@ class TestTwinIntegration(unittest.TestCase): pass # ------------------------------------------------------------------ - # Test 7: PAIRED trunk failover — kill ref1, refA still routes via ref2 + # Test 7: PAIRED trunk failover — kill ref1, refa still routes via ref2 # ------------------------------------------------------------------ def test_07_paired_trunk_failover(self): - """Killing ref1 should not disconnect refA from the DE twin pair. + """Killing ref1 should not disconnect refa from the DE twin pair. - refA uses PAIRED=1 with HOST=reflector-ref1,reflector-ref2. When - ref1 goes down, refA must still be able to exchange traffic with ref2 + refa uses PAIRED=1 with HOST=reflector-ref1,reflector-ref2. When + ref1 goes down, refa must still be able to exchange traffic with ref2 (TrunkLink D2 sticky fallback / isActive() via any live connection). Verification: after killing ref1 and waiting for the heartbeat RX - timeout (~15s), refA's TRUNK_IT_DE status must still report + timeout (~15s), refa's TRUNK_IT_DE status must still report connected=True (served by the ref2 connection). """ # Step 1: kill ref1 @@ -389,10 +389,10 @@ class TestTwinIntegration(unittest.TestCase): # Heartbeat TX interval is 10s, RX timeout is 15s → allow 20s. time.sleep(20.0) - # Step 3: refA must still report the trunk as connected (via ref2) + # Step 3: refa must still report the trunk as connected (via ref2) def refa_still_connected(): try: - status = get_status(*_http("refA")) + status = get_status(*_http("refa")) return status.get("trunks", {}).get( "TRUNK_IT_DE", {}).get("connected", False) except Exception: @@ -402,7 +402,7 @@ class TestTwinIntegration(unittest.TestCase): refa_still_connected, timeout=10.0, interval=1.0, - msg="refA TRUNK_IT_DE lost connectivity after killing ref1; " + msg="refa TRUNK_IT_DE lost connectivity after killing ref1; " "expected ref2 to keep the paired link active", ) @@ -414,7 +414,7 @@ class TestTwinIntegration(unittest.TestCase): # Also wait for trunk re-establishment wait_for_trunk_connected(*_http("ref1"), "TRUNK_IT_DE", timeout=30.0) - wait_for_trunk_connected(*_http("refA"), "TRUNK_IT_DE", + wait_for_trunk_connected(*_http("refa"), "TRUNK_IT_DE", timeout=30.0) except AssertionError: pass diff --git a/tests/topology.py b/tests/topology.py index 8c18ba0..a94419a 100644 --- a/tests/topology.py +++ b/tests/topology.py @@ -122,7 +122,7 @@ def first_prefix(prefixes) -> str: # to select this topology without touching the default above. # --------------------------------------------------------------------------- -# 4-reflector mesh: refA (Italy), ref1+ref2 (Germany twin pair), refC (extra) +# 4-reflector mesh: refa (Italy), ref1+ref2 (Germany twin pair), refc (extra) # # Keys: # prefix : list of owned prefixes @@ -131,10 +131,13 @@ def first_prefix(prefixes) -> str: # redis : enable Redis config store (optional) TWIN_REFLECTORS = { # name prefixes port-base notes - "refA": {"prefix": ["222"], "trunk_port_base": 60000}, # Italy - "ref1": {"prefix": ["262"], "trunk_port_base": 61000, "twin_of": "ref2"}, # DE twin 1 - "ref2": {"prefix": ["262"], "trunk_port_base": 62000, "twin_of": "ref1"}, # DE twin 2 - "refC": {"prefix": ["333"], "trunk_port_base": 63000}, # extra peer + # Port layout: client=base+300, trunk=base+302, twin=base+304, http=base+3080 + # Must keep http port <= 65535, so base <= ~62000. + # Names must be lowercase (Docker image tag constraint). + "refa": {"prefix": ["222"], "trunk_port_base": 45000}, # Italy + "ref1": {"prefix": ["262"], "trunk_port_base": 46000, "twin_of": "ref2"}, # DE twin 1 + "ref2": {"prefix": ["262"], "trunk_port_base": 47000, "twin_of": "ref1"}, # DE twin 2 + "refc": {"prefix": ["333"], "trunk_port_base": 48000}, # extra peer } # Explicit trunk list for the twin topology. @@ -145,24 +148,24 @@ TWIN_REFLECTORS = { # pair_members : the subset of peers that form the twin pair (needed to split sides) # # For a paired trunk the rule is: -# - non-pair side (refA): [TRUNK_IT_DE] HOST=ref1,ref2 PAIRED=1 -# - pair members (ref1, ref2): [TRUNK_IT_DE] HOST=refA (normal, no PAIRED) +# - non-pair side (refa): [TRUNK_IT_DE] HOST=ref1,ref2 PAIRED=1 +# - pair members (ref1, ref2): [TRUNK_IT_DE] HOST=refa (normal, no PAIRED) TWIN_TRUNKS = [ { "name": "IT_DE", - "peers": ["refA", "ref1", "ref2"], + "peers": ["refa", "ref1", "ref2"], "paired": True, "pair_members": ["ref1", "ref2"], }, { "name": "IT_C", - "peers": ["refA", "refC"], + "peers": ["refa", "refc"], "paired": False, "pair_members": [], }, { "name": "DE_C", - "peers": ["ref1", "ref2", "refC"], + "peers": ["ref1", "ref2", "refc"], "paired": True, "pair_members": ["ref1", "ref2"], },