diff --git a/src/svxlink/reflector/Reflector.cpp b/src/svxlink/reflector/Reflector.cpp index aa65e0d..bab7dfc 100644 --- a/src/svxlink/reflector/Reflector.cpp +++ b/src/svxlink/reflector/Reflector.cpp @@ -245,7 +245,8 @@ Reflector::Reflector(void) m_random_qsy_hi(0), m_random_qsy_tg(0), m_http_server(0), m_cmd_pty(0), m_keys_dir("private/"), m_pending_csrs_dir("pending_csrs/"), m_csrs_dir("csrs/"), m_certs_dir("certs/"), m_pki_dir("pki/"), - m_sat_cleanup_timer(0, Async::Timer::TYPE_ONESHOT) + m_sat_cleanup_timer(0, Async::Timer::TYPE_ONESHOT), + m_mqtt_status_timer(1000, Async::Timer::TYPE_ONESHOT) { TGHandler::instance()->talkerUpdated.connect( mem_fun(*this, &Reflector::onTalkerUpdated)); @@ -280,6 +281,10 @@ Reflector::Reflector(void) Reflector::~Reflector(void) { + m_mqtt_status_timer.setEnable(false); + delete m_mqtt; + m_mqtt = nullptr; + delete m_http_server; m_http_server = 0; delete m_udp_sock; @@ -471,6 +476,33 @@ bool Reflector::initialize(Async::Config &cfg) initTrunkServer(); initSatelliteServer(); + // Initialize MQTT publisher if [MQTT] section is configured + std::string mqtt_host; + if (cfg.getValue("MQTT", "HOST", mqtt_host)) + { + m_mqtt = new MqttPublisher(cfg); + if (!m_mqtt->initialize()) + { + cerr << "*** WARNING: MQTT publisher failed to initialize, continuing without MQTT" << endl; + delete m_mqtt; + m_mqtt = nullptr; + } + else + { + // Set up periodic status publishing + unsigned status_interval = 1000; + cfg.getValue("MQTT", "STATUS_INTERVAL", status_interval); + m_mqtt_status_timer.setTimeout(status_interval); + m_mqtt_status_timer.setEnable(true); + m_mqtt_status_timer.expired.connect( + [this](Async::Timer*) + { + m_mqtt->publishFullStatus(m_status); + m_mqtt_status_timer.setEnable(true); + }); + } + } + return true; } /* Reflector::initialize */ @@ -1014,6 +1046,10 @@ void Reflector::clientDisconnected(Async::FramedTcpConnection *con, m_status["nodes"].removeMember(client->callsign()); broadcastMsg(MsgNodeLeft(client->callsign()), ReflectorClient::ExceptFilter(client)); + if (m_mqtt != nullptr) + { + m_mqtt->onClientDisconnected(client->callsign()); + } } //Application::app().runTask([=]{ delete client; }); delete client; @@ -1473,6 +1509,10 @@ void Reflector::onTalkerUpdated(uint32_t tg, ReflectorClient* old_talker, ReflectorClient::mkAndFilter( ReflectorClient::TgFilter(tg), ReflectorClient::ExceptFilter(old_talker))); + if (m_mqtt != nullptr) + { + m_mqtt->onTalkerStop(tg, old_talker->callsign(), false); + } } if (new_talker != 0) { @@ -1488,6 +1528,10 @@ void Reflector::onTalkerUpdated(uint32_t tg, ReflectorClient* old_talker, { broadcastMsg(MsgTalkerStartV1(new_talker->callsign()), v1_client_filter); } + if (m_mqtt != nullptr) + { + m_mqtt->onTalkerStart(tg, new_talker->callsign(), false); + } } // In satellite mode, forward to parent instead of trunk peers @@ -2500,6 +2544,10 @@ void Reflector::onTrunkTalkerUpdated(uint32_t tg, ReflectorClient::TgMonitorFilter(tg)))); broadcastUdpMsg(MsgUdpFlushSamples(), ReflectorClient::TgFilter(tg)); + if (m_mqtt != nullptr) + { + m_mqtt->onTalkerStop(tg, old_cs, true); + } } if (!new_cs.empty()) { @@ -2510,6 +2558,10 @@ void Reflector::onTrunkTalkerUpdated(uint32_t tg, ReflectorClient::mkOrFilter( ReflectorClient::TgFilter(tg), ReflectorClient::TgMonitorFilter(tg)))); + if (m_mqtt != nullptr) + { + m_mqtt->onTalkerStart(tg, new_cs, true); + } } // Forward trunk talker events to connected satellites @@ -2527,6 +2579,34 @@ void Reflector::onTrunkTalkerUpdated(uint32_t tg, } /* Reflector::onTrunkTalkerUpdated */ +void Reflector::onClientAuthenticated(const std::string& callsign, + uint32_t tg, const std::string& ip) +{ + if (m_mqtt != nullptr) + { + m_mqtt->onClientConnected(callsign, tg, ip); + } +} /* Reflector::onClientAuthenticated */ + + +void Reflector::onTrunkStateChanged(const std::string& section, + const std::string& direction, bool up, + const std::string& host, uint16_t port) +{ + if (m_mqtt != nullptr) + { + if (up) + { + m_mqtt->onTrunkUp(section, direction, host, port); + } + else + { + m_mqtt->onTrunkDown(section, direction); + } + } +} /* Reflector::onTrunkStateChanged */ + + bool Reflector::loadCertificateFiles(void) { if (!buildPath("GLOBAL", "CERT_PKI_DIR", SVX_LOCAL_STATE_DIR, m_pki_dir) || diff --git a/src/svxlink/reflector/Reflector.h b/src/svxlink/reflector/Reflector.h index 97f8084..15ef8cb 100644 --- a/src/svxlink/reflector/Reflector.h +++ b/src/svxlink/reflector/Reflector.h @@ -67,6 +67,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include "TrunkLink.h" #include "SatelliteLink.h" #include "SatelliteClient.h" +#include "MqttPublisher.h" /**************************************************************************** @@ -253,6 +254,13 @@ class Reflector : public sigc::trackable const std::vector& audio); void forwardFlushToSatellitesExcept(SatelliteLink* except, uint32_t tg); + void onClientAuthenticated(const std::string& callsign, uint32_t tg, + const std::string& ip); + void onTrunkStateChanged(const std::string& section, + const std::string& direction, bool up, + const std::string& host = "", + uint16_t port = 0); + protected: private: @@ -317,6 +325,10 @@ class Reflector : public sigc::trackable std::vector m_sat_cleanup_pending; Async::Timer m_sat_cleanup_timer; + // MQTT publishing + MqttPublisher* m_mqtt = nullptr; + Async::Timer m_mqtt_status_timer; + Reflector(const Reflector&); Reflector& operator=(const Reflector&); void clientConnected(Async::FramedTcpConnection *con); diff --git a/src/svxlink/reflector/ReflectorClient.cpp b/src/svxlink/reflector/ReflectorClient.cpp index d971738..6896404 100644 --- a/src/svxlink/reflector/ReflectorClient.cpp +++ b/src/svxlink/reflector/ReflectorClient.cpp @@ -1344,6 +1344,8 @@ void ReflectorClient::connectionAuthenticated(const std::string& callsign) } m_reflector->broadcastMsg(MsgNodeJoined(m_callsign), ExceptFilter(this)); + m_reflector->onClientAuthenticated(m_callsign, currentTG(), + m_con->remoteHost().toString()); } else { diff --git a/src/svxlink/reflector/TrunkLink.cpp b/src/svxlink/reflector/TrunkLink.cpp index a2ae8d0..a1ed929 100644 --- a/src/svxlink/reflector/TrunkLink.cpp +++ b/src/svxlink/reflector/TrunkLink.cpp @@ -353,6 +353,9 @@ void TrunkLink::acceptInboundConnection(Async::FramedTcpConnection* con, cout << m_section << ": Accepted inbound from " << con->remoteHost() << ":" << con->remotePort() << " peer='" << hello.id() << "' priority=" << m_peer_priority << endl; + m_reflector->onTrunkStateChanged(m_section, "inbound", true, + con->remoteHost().toString(), + con->remotePort()); if (m_debug) { @@ -384,6 +387,7 @@ void TrunkLink::onInboundDisconnected(Async::FramedTcpConnection* con, } cout << m_section << ": Inbound trunk connection lost" << endl; + m_reflector->onTrunkStateChanged(m_section, "inbound", false); if (m_debug) { @@ -475,6 +479,9 @@ void TrunkLink::onConnected(void) { cout << m_section << ": Outbound connected to " << m_con.remoteHost() << ":" << m_con.remotePort() << endl; + m_reflector->onTrunkStateChanged(m_section, "outbound", true, + m_con.remoteHost().toString(), + m_con.remotePort()); if (m_debug) { @@ -498,6 +505,7 @@ void TrunkLink::onDisconnected(TcpConnection* con, { cout << m_section << ": Outbound disconnected: " << TcpConnection::disconnectReasonStr(reason) << endl; + m_reflector->onTrunkStateChanged(m_section, "outbound", false); if (m_debug) {