feat(mqtt): peer-namespaced talker topics

External (peer-side) talker events move from flat talker/<tg>/start
with is_trunk=true to peer/<peer_id>/talker/<tg>/start. Local-only
talker/<tg>/start retains today's behavior.

To carry peer_id through the signal path: add peer_id as a 4th
argument to TGHandler::trunkTalkerUpdated; reorder
clearTrunkTalkerForTG / clearTrunkTalkersForPeer / clearAllTrunkTalkers
so the signal fires before m_trunk_talker_peer_ids is erased;
pre-populate m_trunk_talker_peer_ids in setTrunkTalkerForTGViaPeer
before calling the inner setter; switch TrunkLink::handleMsgPeerTalkerStart
from setTrunkTalkerForTG to setTrunkTalkerForTGViaPeer.

Reasons: dashboards subscribing across the mesh can now use
peer/+/talker/+/start to enumerate peer talkers without parsing the
is_trunk payload flag, and the namespacing aligns with the new
peer/<peer_id>/client/<call>/* topics added in Tasks 5-6.
This commit is contained in:
Audric committed 2026-04-28 19:45:08 +02:00
1 parent 81672bae23
commit af746d7534
8 files changed
+104 -33

No files matched your search

+28
View File
@@ -262,6 +262,34 @@ void MqttPublisher::onTalkerStop(uint32_t tg, const std::string& callsign,
}
void MqttPublisher::onPeerTalkerStart(const std::string& peer_id,
uint32_t tg,
const std::string& callsign)
{
Json::Value payload;
payload["callsign"] = callsign;
payload["tg"] = static_cast<Json::UInt>(tg);
Json::StreamWriterBuilder wb;
wb["indentation"] = "";
string topic = "peer/" + peer_id + "/talker/" + to_string(tg) + "/start";
publish(topic, Json::writeString(wb, payload));
}
void MqttPublisher::onPeerTalkerStop(const std::string& peer_id,
uint32_t tg,
const std::string& callsign)
{
Json::Value payload;
payload["callsign"] = callsign;
payload["tg"] = static_cast<Json::UInt>(tg);
Json::StreamWriterBuilder wb;
wb["indentation"] = "";
string topic = "peer/" + peer_id + "/talker/" + to_string(tg) + "/stop";
publish(topic, Json::writeString(wb, payload));
}
void MqttPublisher::onClientConnected(const std::string& callsign,
uint32_t tg, const std::string& ip)
{
+9 -1
View File
@@ -23,10 +23,16 @@ class MqttPublisher
bool initialize(void);
void shutdown(void);
// Talker events (local clients and trunk peers)
// Talker events (local clients)
void onTalkerStart(uint32_t tg, const std::string& callsign, bool is_trunk);
void onTalkerStop(uint32_t tg, const std::string& callsign, bool is_trunk);
// Talker events (peer/trunk side) — published to peer/<peer_id>/talker/<tg>/start|stop
void onPeerTalkerStart(const std::string& peer_id, uint32_t tg,
const std::string& callsign);
void onPeerTalkerStop(const std::string& peer_id, uint32_t tg,
const std::string& callsign);
// Client events
void onClientConnected(const std::string& callsign, uint32_t tg,
const std::string& ip);
@@ -109,6 +115,8 @@ class MqttPublisher
void shutdown(void) {}
void onTalkerStart(uint32_t, const std::string&, bool) {}
void onTalkerStop(uint32_t, const std::string&, bool) {}
void onPeerTalkerStart(const std::string&, uint32_t, const std::string&) {}
void onPeerTalkerStop(const std::string&, uint32_t, const std::string&) {}
void onClientConnected(const std::string&, uint32_t,
const std::string&) {}
void onClientDisconnected(const std::string&) {}
+4 -3
View File
@@ -2808,7 +2808,8 @@ void Reflector::forwardFlushToSatellitesExcept(SatelliteLink* except,
void Reflector::onTrunkTalkerUpdated(uint32_t tg,
std::string old_cs, std::string new_cs)
std::string old_cs, std::string new_cs,
std::string peer_id)
{
auto ge_v2_client_filter =
ReflectorClient::ProtoVerLargerOrEqualFilter(ProtoVer(2, 0));
@@ -2826,7 +2827,7 @@ void Reflector::onTrunkTalkerUpdated(uint32_t tg,
ReflectorClient::TgFilter(tg));
if (m_mqtt != nullptr)
{
m_mqtt->onTalkerStop(tg, old_cs, true);
m_mqtt->onPeerTalkerStop(peer_id, tg, old_cs);
}
if (m_redis != nullptr)
{
@@ -2844,7 +2845,7 @@ void Reflector::onTrunkTalkerUpdated(uint32_t tg,
ReflectorClient::TgMonitorFilter(tg))));
if (m_mqtt != nullptr)
{
m_mqtt->onTalkerStart(tg, new_cs, true);
m_mqtt->onPeerTalkerStart(peer_id, tg, new_cs);
}
if (m_redis != nullptr)
{
+1 -1
View File
@@ -430,7 +430,7 @@ class Reflector : public sigc::trackable
void ctrlPtyDataReceived(const void *buf, size_t count);
void cfgUpdated(const std::string& section, const std::string& tag);
void onTrunkTalkerUpdated(uint32_t tg, std::string old_cs,
std::string new_cs);
std::string new_cs, std::string peer_id);
void onRedisConfigChanged(std::string scope);
void reloadClusterTgs(void);
bool addTrunkLink(const std::string& section);
+30 -17
View File
@@ -278,6 +278,13 @@ void TGHandler::setTrunkTalkerForTG(uint32_t tg, const std::string& callsign)
{
return;
}
// Capture peer_id before any erase so stop events can carry it.
std::string peer_id;
auto pid_it = m_trunk_talker_peer_ids.find(tg);
if (pid_it != m_trunk_talker_peer_ids.end())
{
peer_id = pid_it->second;
}
if (callsign.empty())
{
m_trunk_talkers.erase(tg);
@@ -294,20 +301,24 @@ void TGHandler::setTrunkTalkerForTG(uint32_t tg, const std::string& callsign)
}
m_trunk_talkers[tg] = callsign;
}
trunkTalkerUpdated(tg, old, callsign);
trunkTalkerUpdated(tg, old, callsign, peer_id);
} /* TGHandler::setTrunkTalkerForTG */
void TGHandler::clearTrunkTalkerForTG(uint32_t tg)
{
m_trunk_talker_peer_ids.erase(tg);
// Call setTrunkTalkerForTG first so it can capture peer_id from
// m_trunk_talker_peer_ids before we erase it (the signal handler
// needs it to publish peer-namespaced MQTT topics).
setTrunkTalkerForTG(tg, "");
m_trunk_talker_peer_ids.erase(tg);
} /* TGHandler::clearTrunkTalkerForTG */
void TGHandler::clearAllTrunkTalkers(void)
{
m_trunk_talker_peer_ids.clear();
// Collect keys first, then clear via clearTrunkTalkerForTG so the signal
// fires with the peer_id still present in m_trunk_talker_peer_ids.
std::vector<uint32_t> keys;
for (auto& kv : m_trunk_talkers)
{
@@ -315,7 +326,7 @@ void TGHandler::clearAllTrunkTalkers(void)
}
for (uint32_t tg : keys)
{
setTrunkTalkerForTG(tg, "");
clearTrunkTalkerForTG(tg);
}
} /* TGHandler::clearAllTrunkTalkers */
@@ -324,33 +335,35 @@ void TGHandler::setTrunkTalkerForTGViaPeer(uint32_t tg,
const std::string& callsign,
const std::string& peer_id)
{
if (!callsign.empty())
{
// Store peer_id BEFORE firing the signal so the signal handler can read it.
m_trunk_talker_peer_ids[tg] = peer_id;
}
setTrunkTalkerForTG(tg, callsign);
if (callsign.empty())
{
// Erase AFTER the signal has fired (setTrunkTalkerForTG captures it first).
m_trunk_talker_peer_ids.erase(tg);
}
else
{
m_trunk_talker_peer_ids[tg] = peer_id;
}
} /* TGHandler::setTrunkTalkerForTGViaPeer */
void TGHandler::clearTrunkTalkersForPeer(const std::string& peer_id)
{
for (auto it = m_trunk_talker_peer_ids.begin();
it != m_trunk_talker_peer_ids.end(); )
// Collect matching TGs first so we can fire the signal while peer_id is
// still in m_trunk_talker_peer_ids (clearTrunkTalkerForTG erases afterwards).
std::vector<uint32_t> tgs_to_clear;
for (auto& kv : m_trunk_talker_peer_ids)
{
if (it->second == peer_id)
if (kv.second == peer_id)
{
uint32_t tg = it->first;
it = m_trunk_talker_peer_ids.erase(it);
setTrunkTalkerForTG(tg, "");
tgs_to_clear.push_back(kv.first);
}
else
{
++it;
}
for (uint32_t tg : tgs_to_clear)
{
clearTrunkTalkerForTG(tg);
}
} /* TGHandler::clearTrunkTalkersForPeer */
+4 -2
View File
@@ -210,9 +210,11 @@ class TGHandler : public sigc::trackable
sigc::signal<void(uint32_t)> requestAutoQsy;
// Fired when a trunk peer claims or releases a TG.
// Args: tg, old_callsign ("" = none), new_callsign ("" = none)
// Args: tg, old_callsign ("" = none), new_callsign ("" = none),
// peer_id (the peer that originated the event, "" if unknown)
sigc::signal<void(uint32_t,
std::string, std::string)> trunkTalkerUpdated;
std::string, std::string,
std::string)> trunkTalkerUpdated;
private:
static const time_t TALKER_AUDIO_TIMEOUT = 3; // Max three seconds gap
+2 -1
View File
@@ -1023,7 +1023,8 @@ void TrunkLink::handleMsgPeerTalkerStart(std::istream& is)
m_peer_active_tgs.insert(local_tg);
m_peer_interested_tgs[local_tg] = std::time(nullptr);
TGHandler::instance()->setTrunkTalkerForTG(local_tg, msg.callsign());
TGHandler::instance()->setTrunkTalkerForTGViaPeer(local_tg, msg.callsign(),
peerId());
m_reflector->notifyExternalTrunkTalkerStart(local_tg, m_section, msg.callsign());
// Owner-relay: if we own this TG, propagate the talker-start to every
+26 -8
View File
@@ -1484,12 +1484,14 @@ class TestTrunkIntegration(unittest.TestCase):
client_b.close()
def test_18_mqtt_talker_event(self):
"""MQTT publishes talker start/stop events."""
"""MQTT publishes talker start/stop events (peer-namespaced for trunk talkers)."""
received = []
connected_event = threading.Event()
def on_connect(client, userdata, flags, rc):
# Subscribe to both local talker topics and peer-namespaced talker topics
client.subscribe("svxreflector/a/talker/#")
client.subscribe("svxreflector/a/peer/#")
connected_event.set()
def on_message(client, userdata, msg):
@@ -1506,32 +1508,48 @@ class TestTrunkIntegration(unittest.TestCase):
"MQTT subscribe timed out")
time.sleep(0.5) # Let subscription settle
# Connect trunk peer and send talker start
# Connect trunk peer and send talker start.
# The peer sends trunk_id="TRUNK_TEST" in MsgPeerHello, so the
# reflector uses "TRUNK_TEST" as the peer_id for namespacing.
peer = TrunkPeer()
peer.connect(*_trunk("a"))
peer.handshake()
peer_id = "TRUNK_TEST"
tg = 1220 # Owned by reflector-a (prefix 122)
peer.send_talker_start(tg, "N0MQTT")
time.sleep(2) # Allow propagation
# Verify MQTT talker start event
# Trunk talker events now go to peer/<peer_id>/talker/<tg>/start
expected_start_topic = (
f"svxreflector/a/peer/{peer_id}/talker/{tg}/start"
)
start_events = [r for r in received
if r[0] == f"svxreflector/a/talker/{tg}/start"]
if r[0] == expected_start_topic]
self.assertTrue(len(start_events) > 0,
f"Expected MQTT talker start event on TG {tg}, "
f"Expected MQTT talker start at '{expected_start_topic}', "
f"got topics: {[r[0] for r in received]}")
self.assertEqual(start_events[0][1]["callsign"], "N0MQTT")
self.assertEqual(start_events[0][1]["source"], "trunk")
# Flat talker/<tg>/start should NOT be published for trunk talkers
flat_start_events = [r for r in received
if r[0] == f"svxreflector/a/talker/{tg}/start"]
self.assertEqual(len(flat_start_events), 0,
"Flat talker/<tg>/start must not be published for "
"trunk talkers (should use peer-namespaced topic)")
# Send talker stop and verify
peer.send_talker_stop(tg)
time.sleep(2)
expected_stop_topic = (
f"svxreflector/a/peer/{peer_id}/talker/{tg}/stop"
)
stop_events = [r for r in received
if r[0] == f"svxreflector/a/talker/{tg}/stop"]
if r[0] == expected_stop_topic]
self.assertTrue(len(stop_events) > 0,
f"Expected MQTT talker stop event on TG {tg}")
f"Expected MQTT talker stop at '{expected_stop_topic}', "
f"got topics: {[r[0] for r in received]}")
peer.close()
finally: