diff --git a/src/svxlink/reflector/Reflector.cpp b/src/svxlink/reflector/Reflector.cpp index 8340f95..bb7d9b5 100644 --- a/src/svxlink/reflector/Reflector.cpp +++ b/src/svxlink/reflector/Reflector.cpp @@ -2224,7 +2224,7 @@ void Reflector::initTrunkLinks(void) // Collect local prefixes for cluster TG validation std::string local_prefix_str; m_cfg->getValue("GLOBAL", "LOCAL_PREFIX", local_prefix_str); - std::vector all_prefixes; + m_local_prefixes.clear(); { std::istringstream ss(local_prefix_str); std::string token; @@ -2232,9 +2232,10 @@ void Reflector::initTrunkLinks(void) { token.erase(0, token.find_first_not_of(" \t")); token.erase(token.find_last_not_of(" \t") + 1); - if (!token.empty()) all_prefixes.push_back(token); + if (!token.empty()) m_local_prefixes.push_back(token); } } + std::vector all_prefixes = m_local_prefixes; // If Redis is configured, load trunk peer definitions from Redis and // synthesize them into m_cfg so the existing trunk initialization @@ -2291,6 +2292,9 @@ void Reflector::initTrunkLinks(void) } } + // Cache the mesh-wide prefix set for owner-relay decisions. + m_all_prefixes = all_prefixes; + // Second pass: create trunk links with full prefix knowledge for (const auto& section : trunk_sections) { @@ -2878,6 +2882,80 @@ std::vector Reflector::collectAllTrunkPrefixes(void) const } +bool Reflector::isLocalTG(uint32_t tg) const +{ + const std::string s = std::to_string(tg); + + size_t best_local_len = 0; + for (const auto& prefix : m_local_prefixes) + { + if (s.size() >= prefix.size() && + s.compare(0, prefix.size(), prefix) == 0 && + prefix.size() > best_local_len) + { + best_local_len = prefix.size(); + } + } + if (best_local_len == 0) return false; + + for (const auto& prefix : m_all_prefixes) + { + if (prefix.size() > best_local_len && + s.size() >= prefix.size() && + s.compare(0, prefix.size(), prefix) == 0) + { + return false; // a longer (remote) prefix claims this TG + } + } + return true; +} /* Reflector::isLocalTG */ + + +void Reflector::forwardTrunkAudioToOtherTrunks(const TrunkLink* src, + uint32_t tg, + const std::vector& audio) +{ + for (auto* link : m_trunk_links) + { + if (link == src) continue; + link->onLocalAudio(tg, audio); + } +} /* Reflector::forwardTrunkAudioToOtherTrunks */ + + +void Reflector::forwardTrunkFlushToOtherTrunks(const TrunkLink* src, + uint32_t tg) +{ + for (auto* link : m_trunk_links) + { + if (link == src) continue; + link->onLocalFlush(tg); + } +} /* Reflector::forwardTrunkFlushToOtherTrunks */ + + +void Reflector::forwardTrunkTalkerStartToOtherTrunks( + const TrunkLink* src, uint32_t tg, const std::string& callsign) +{ + for (auto* link : m_trunk_links) + { + if (link == src) continue; + link->onLocalTalkerStart(tg, callsign); + } +} /* Reflector::forwardTrunkTalkerStartToOtherTrunks */ + + +void Reflector::forwardTrunkTalkerStopToOtherTrunks(const TrunkLink* src, + uint32_t tg) +{ + for (auto* link : m_trunk_links) + { + if (link == src) continue; + link->onLocalTalkerStop(tg); + } +} /* Reflector::forwardTrunkTalkerStopToOtherTrunks */ + + bool Reflector::addTrunkLink(const std::string& section) { if (m_redis == nullptr) { @@ -2917,6 +2995,9 @@ bool Reflector::addTrunkLink(const std::string& section) // Rebroadcast the full prefix set to every link (including the new one). auto all_prefixes = collectAllTrunkPrefixes(); + all_prefixes.insert(all_prefixes.begin(), + m_local_prefixes.begin(), m_local_prefixes.end()); + m_all_prefixes = all_prefixes; for (auto* l : m_trunk_links) l->setAllPrefixes(all_prefixes); geulog::info("trunk", "Added trunk link: ", section, " (", p.host, @@ -2938,6 +3019,9 @@ bool Reflector::removeTrunkLink(const std::string& section) // Rebroadcast the remaining prefix set so peers drop the removed one. auto all_prefixes = collectAllTrunkPrefixes(); + all_prefixes.insert(all_prefixes.begin(), + m_local_prefixes.begin(), m_local_prefixes.end()); + m_all_prefixes = all_prefixes; for (auto* l : m_trunk_links) l->setAllPrefixes(all_prefixes); geulog::info("trunk", "Removed trunk link: ", section); diff --git a/src/svxlink/reflector/Reflector.h b/src/svxlink/reflector/Reflector.h index 9449880..3f694f2 100644 --- a/src/svxlink/reflector/Reflector.h +++ b/src/svxlink/reflector/Reflector.h @@ -246,6 +246,24 @@ class Reflector : public sigc::trackable bool isClusterTG(uint32_t tg) const { return m_cluster_tgs.count(tg) > 0; } bool isSatelliteMode(void) const { return m_is_satellite; } + // True iff this reflector owns the TG (i.e., our local prefix is the + // longest matching prefix across all known prefixes in the mesh). + // Used to gate owner-relay fanout of trunk-received audio. + bool isLocalTG(uint32_t tg) const; + + // Owner-relay fanout: when we receive trunk traffic for a TG we own, + // forward to every other trunk peer except `src`. Each peer's TrunkLink + // applies its own shared/cluster/interest filter, so links with no + // reason to care drop the forward. + void forwardTrunkAudioToOtherTrunks(const TrunkLink* src, uint32_t tg, + const std::vector& audio); + void forwardTrunkFlushToOtherTrunks(const TrunkLink* src, uint32_t tg); + void forwardTrunkTalkerStartToOtherTrunks(const TrunkLink* src, + uint32_t tg, + const std::string& callsign); + void forwardTrunkTalkerStopToOtherTrunks(const TrunkLink* src, + uint32_t tg); + RedisStore* redisStore(void) const { return m_redis; } // Callbacks for SatelliteLink to forward satellite events to trunk peers @@ -337,6 +355,12 @@ class Reflector : public sigc::trackable Json::Value m_status; std::vector m_trunk_links; + // Prefix bookkeeping for owner-relay decisions. m_local_prefixes holds + // prefixes owned by this reflector (from GLOBAL/LOCAL_PREFIX); + // m_all_prefixes is local + every peer's REMOTE_PREFIX. Populated in + // initTrunkLinks() and refreshed by addTrunkLink/removeTrunkLink. + std::vector m_local_prefixes; + std::vector m_all_prefixes; // Tracks last-seen callsign set per peer_id so we can DEL dropped // callsigns from Redis when a peer's node list shrinks. std::map> m_peer_node_cache; diff --git a/src/svxlink/reflector/TrunkLink.cpp b/src/svxlink/reflector/TrunkLink.cpp index 36e4e50..39c0735 100644 --- a/src/svxlink/reflector/TrunkLink.cpp +++ b/src/svxlink/reflector/TrunkLink.cpp @@ -971,6 +971,15 @@ void TrunkLink::handleMsgTrunkTalkerStart(std::istream& is) m_peer_interested_tgs[local_tg] = std::time(nullptr); TGHandler::instance()->setTrunkTalkerForTG(local_tg, msg.callsign()); m_reflector->notifyExternalTrunkTalkerStart(local_tg, m_section, msg.callsign()); + + // Owner-relay: if we own this TG, propagate the talker-start to every + // other interested trunk peer so their local clients and our mesh-wide + // audience learn about the remote talker. + if (m_reflector->isLocalTG(local_tg)) + { + m_reflector->forwardTrunkTalkerStartToOtherTrunks(this, local_tg, + msg.callsign()); + } } /* TrunkLink::handleMsgTrunkTalkerStart */ @@ -999,6 +1008,12 @@ void TrunkLink::handleMsgTrunkTalkerStop(std::istream& is) m_peer_active_tgs.erase(local_tg); TGHandler::instance()->clearTrunkTalkerForTG(local_tg); m_reflector->notifyExternalTrunkTalkerStop(local_tg, m_section); + + // Owner-relay: propagate the talker-stop to every other interested peer. + if (m_reflector->isLocalTG(local_tg)) + { + m_reflector->forwardTrunkTalkerStopToOtherTrunks(this, local_tg); + } } /* TrunkLink::handleMsgTrunkTalkerStop */ @@ -1046,6 +1061,13 @@ void TrunkLink::handleMsgTrunkAudio(std::istream& is) // Forward trunk audio to connected satellites m_reflector->forwardAudioToSatellitesExcept(nullptr, local_tg, msg.audio()); + + // Owner-relay: when the TG is ours, fan the audio out to every other + // trunk peer that has interest (so mesh-wide audience hears the talker). + if (m_reflector->isLocalTG(local_tg)) + { + m_reflector->forwardTrunkAudioToOtherTrunks(this, local_tg, msg.audio()); + } } /* TrunkLink::handleMsgTrunkAudio */ @@ -1075,6 +1097,13 @@ void TrunkLink::handleMsgTrunkFlush(std::istream& is) // Forward trunk flush to connected satellites m_reflector->forwardFlushToSatellitesExcept(nullptr, local_tg); + + // Owner-relay: when the TG is ours, fan the flush out to every other + // trunk peer so their clients see end-of-stream as well. + if (m_reflector->isLocalTG(local_tg)) + { + m_reflector->forwardTrunkFlushToOtherTrunks(this, local_tg); + } } /* TrunkLink::handleMsgTrunkFlush */