Wire MqttPublisher into Reflector event handlers

This commit is contained in:
Audric committed 2026-04-08 20:32:02 +02:00
1 parent 89f8c07ccc
commit 5b7a3dd8cd
4 files changed
+103 -1

No files matched your search

+81 -1
View File
@@ -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) ||
+12
View File
@@ -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<uint8_t>& 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<SatelliteLink*> 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);
@@ -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
{
+8
View File
@@ -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)
{