diff --git a/Dockerfile b/Dockerfile index 2b915c1..9652bf7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,7 +14,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ WORKDIR /src COPY src/ src/ -RUN cmake -S src -B build -DLOCAL_STATE_DIR=/var \ +RUN cmake -S src -B build -DLOCAL_STATE_DIR=/var -DUSE_ALSA=OFF \ && cmake --build build diff --git a/src/svxlink/reflector/Reflector.cpp b/src/svxlink/reflector/Reflector.cpp index b1507be..4962e69 100644 --- a/src/svxlink/reflector/Reflector.cpp +++ b/src/svxlink/reflector/Reflector.cpp @@ -242,7 +242,8 @@ Reflector::Reflector(void) : m_srv(0), m_udp_sock(0), m_tg_for_v1_clients(1), m_random_qsy_lo(0), 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_csrs_dir("csrs/"), m_certs_dir("certs/"), m_pki_dir("pki/"), + m_sat_cleanup_timer(0, Async::Timer::TYPE_ONESHOT) { TGHandler::instance()->talkerUpdated.connect( mem_fun(*this, &Reflector::onTalkerUpdated)); @@ -268,6 +269,9 @@ Reflector::Reflector(void) << std::endl; } }); + m_sat_cleanup_timer.setEnable(false); + m_sat_cleanup_timer.expired.connect( + sigc::mem_fun(*this, &Reflector::processSatelliteCleanup)); m_status["nodes"] = Json::Value(Json::objectValue); } /* Reflector::Reflector */ @@ -1646,18 +1650,31 @@ void Reflector::httpRequestReceived(Async::HttpServerConnection *con, } m_status["cluster_tgs"] = cluster_arr; - if (!m_satellite_con_map.empty()) { Json::Value sats(Json::objectValue); for (auto& kv : m_satellite_con_map) { auto* sat = kv.second; + // Skip satellites pending cleanup (heartbeat timed out) + if (std::find(m_sat_cleanup_pending.begin(), + m_sat_cleanup_pending.end(), + sat) != m_sat_cleanup_pending.end()) + { + continue; + } if (!sat->satelliteId().empty()) { sats[sat->satelliteId()] = sat->statusJson(); } } - m_status["satellites"] = sats; + if (sats.empty()) + { + m_status.removeMember("satellites"); + } + else + { + m_status["satellites"] = sats; + } } std::ostringstream os; @@ -2060,6 +2077,8 @@ void Reflector::satelliteConnected(Async::FramedTcpConnection* con) std::cout << "SAT: Inbound connection from " << con->remoteHost() << ":" << con->remotePort() << std::endl; auto* link = new SatelliteLink(this, con, m_satellite_secret); + link->linkFailed.connect( + sigc::mem_fun(*this, &Reflector::onSatelliteLinkFailed)); m_satellite_con_map[con] = link; } /* Reflector::satelliteConnected */ @@ -2078,6 +2097,42 @@ void Reflector::satelliteDisconnected(Async::FramedTcpConnection* con, } /* Reflector::satelliteDisconnected */ +void Reflector::onSatelliteLinkFailed(SatelliteLink* link) +{ + m_sat_cleanup_pending.push_back(link); + m_sat_cleanup_timer.setEnable(false); + m_sat_cleanup_timer.setTimeout(0); + m_sat_cleanup_timer.setEnable(true); +} /* Reflector::onSatelliteLinkFailed */ + + +void Reflector::processSatelliteCleanup(Async::Timer* t) +{ + std::vector pending; + pending.swap(m_sat_cleanup_pending); + + for (SatelliteLink* link : pending) + { + Async::FramedTcpConnection* con = nullptr; + for (auto& kv : m_satellite_con_map) + { + if (kv.second == link) + { + con = kv.first; + break; + } + } + if (con == nullptr) continue; + + std::cout << "SAT: Cleaning up timed-out satellite '" + << link->satelliteId() << "'" << std::endl; + delete link; + m_satellite_con_map.erase(con); + con->disconnect(); + } +} /* Reflector::processSatelliteCleanup */ + + void Reflector::forwardSatelliteAudioToTrunks(uint32_t tg, const std::string& callsign) { diff --git a/src/svxlink/reflector/Reflector.h b/src/svxlink/reflector/Reflector.h index 6bd0229..78b979e 100644 --- a/src/svxlink/reflector/Reflector.h +++ b/src/svxlink/reflector/Reflector.h @@ -306,6 +306,8 @@ class Reflector : public sigc::trackable FramedTcpServer* m_sat_srv = nullptr; std::string m_satellite_secret; std::map m_satellite_con_map; + std::vector m_sat_cleanup_pending; + Async::Timer m_sat_cleanup_timer; Reflector(const Reflector&); Reflector& operator=(const Reflector&); @@ -334,6 +336,8 @@ class Reflector : public sigc::trackable void satelliteConnected(Async::FramedTcpConnection* con); void satelliteDisconnected(Async::FramedTcpConnection* con, Async::FramedTcpConnection::DisconnectReason reason); + void onSatelliteLinkFailed(SatelliteLink* link); + void processSatelliteCleanup(Async::Timer* t); bool loadCertificateFiles(void); bool loadServerCertificateFiles(void); bool generateKeyFile(Async::SslKeypair& pkey, const std::string& keyfile); diff --git a/src/svxlink/reflector/SatelliteLink.cpp b/src/svxlink/reflector/SatelliteLink.cpp index 037a79e..2c775c9 100644 --- a/src/svxlink/reflector/SatelliteLink.cpp +++ b/src/svxlink/reflector/SatelliteLink.cpp @@ -280,6 +280,7 @@ void SatelliteLink::heartbeatTick(Async::Timer* t) { cerr << "*** ERROR[SAT '" << m_satellite_id << "']: Heartbeat timeout — disconnecting" << endl; - m_con->disconnect(); + m_heartbeat_timer.setEnable(false); + linkFailed(this); } } /* SatelliteLink::heartbeatTick */ diff --git a/src/svxlink/reflector/SatelliteLink.h b/src/svxlink/reflector/SatelliteLink.h index f698b37..f1b6f01 100644 --- a/src/svxlink/reflector/SatelliteLink.h +++ b/src/svxlink/reflector/SatelliteLink.h @@ -30,6 +30,13 @@ class SatelliteLink : public sigc::trackable const std::string& satelliteId(void) const { return m_satellite_id; } Json::Value statusJson(void) const; + /** + * Emitted when the satellite link has failed (heartbeat timeout). + * The Reflector must handle cleanup — the SatelliteLink stops its own + * timer but does NOT call m_con->disconnect() itself. + */ + sigc::signal linkFailed; + // Events from local clients or trunk peers → send down to satellite void onParentTalkerStart(uint32_t tg, const std::string& callsign); void onParentTalkerStop(uint32_t tg);