diff --git a/src/svxlink/reflector/TGHandler.cpp b/src/svxlink/reflector/TGHandler.cpp index 071e0c3..78c1bf4 100644 --- a/src/svxlink/reflector/TGHandler.cpp +++ b/src/svxlink/reflector/TGHandler.cpp @@ -299,12 +299,14 @@ void TGHandler::setTrunkTalkerForTG(uint32_t tg, const std::string& callsign) void TGHandler::clearTrunkTalkerForTG(uint32_t tg) { + m_trunk_talker_peer_ids.erase(tg); setTrunkTalkerForTG(tg, ""); } /* TGHandler::clearTrunkTalkerForTG */ void TGHandler::clearAllTrunkTalkers(void) { + m_trunk_talker_peer_ids.clear(); std::vector keys; for (auto& kv : m_trunk_talkers) { @@ -312,11 +314,53 @@ void TGHandler::clearAllTrunkTalkers(void) } for (uint32_t tg : keys) { - clearTrunkTalkerForTG(tg); + setTrunkTalkerForTG(tg, ""); } } /* TGHandler::clearAllTrunkTalkers */ +void TGHandler::setTrunkTalkerForTGViaPeer(uint32_t tg, + const std::string& callsign, + const std::string& peer_id) +{ + setTrunkTalkerForTG(tg, callsign); + if (callsign.empty()) + { + 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(); ) + { + if (it->second == peer_id) + { + uint32_t tg = it->first; + it = m_trunk_talker_peer_ids.erase(it); + setTrunkTalkerForTG(tg, ""); + } + else + { + ++it; + } + } +} /* TGHandler::clearTrunkTalkersForPeer */ + + +std::string TGHandler::peerIdForTG(uint32_t tg) const +{ + auto it = m_trunk_talker_peer_ids.find(tg); + return (it != m_trunk_talker_peer_ids.end()) ? it->second : std::string(); +} /* TGHandler::peerIdForTG */ + + std::string TGHandler::trunkTalkerForTG(uint32_t tg) const { auto it = m_trunk_talkers.find(tg); diff --git a/src/svxlink/reflector/TGHandler.h b/src/svxlink/reflector/TGHandler.h index a2d1847..7554777 100644 --- a/src/svxlink/reflector/TGHandler.h +++ b/src/svxlink/reflector/TGHandler.h @@ -174,6 +174,28 @@ class TGHandler : public sigc::trackable return m_trunk_talkers; } + /** + * @brief Like setTrunkTalkerForTG, but also records which external + * peer (by TRUNK section id) holds the TG. Used by the twin + * link so state can be cleaned up per-peer on disconnect. + * + * Passing an empty callsign clears the talker for this TG. + */ + void setTrunkTalkerForTGViaPeer(uint32_t tg, + const std::string& callsign, + const std::string& peer_id); + + /** + * @brief Clear all trunk-talker entries attributed to a given peer + * (e.g. when that peer's trunk disconnects). + */ + void clearTrunkTalkersForPeer(const std::string& peer_id); + + /** + * @brief Return the peer_id that holds the TG, or "" if none. + */ + std::string peerIdForTG(uint32_t tg) const; + bool allowTgSelection(ReflectorClient *client, uint32_t tg); bool allowTgMonitoring(ReflectorClient *client, uint32_t tg); @@ -221,6 +243,7 @@ class TGHandler : public sigc::trackable IdMap m_id_map; ClientMap m_client_map; std::map m_trunk_talkers; + std::map m_trunk_talker_peer_ids; // tg -> peer_id Async::Timer m_timeout_timer; unsigned m_sql_timeout; unsigned m_sql_timeout_blocktime;