twin: expose partner roster in /status (fixes #3)
Reflector::refreshStatus() now emits a m_status["twin"] object (host, port, connection/hello state, prefix, peer_id, priorities) mirroring the existing trunks[] pattern. To populate the partner's connected-stations list, TwinLink now carries MsgTrunkNodeList (type 121) over the [TWIN] socket — reusing the trunk wire message, no new protocol type. Local roster changes flow via Reflector::sendNodeListToAllPeers() -> TwinLink::onLocalNodeListUpdated(); incoming lists are sanitized and cached in m_partner_nodes, surfaced as twin.nodes. Without this, [TWIN]-paired reflectors could not observe each other's rosters from /status — the data was not on the wire at all (TwinLink only carried talker state).
This commit is contained in:
1 parent
4bbd53172a
commit
fa6f137e1c
4 files changed
+214
-2
No files matched your search
@@ -1678,6 +1678,15 @@ void Reflector::refreshStatus(void)
|
|||||||
}
|
}
|
||||||
m_status["trunks"] = trunks;
|
m_status["trunks"] = trunks;
|
||||||
|
|
||||||
|
if (m_twin_link != nullptr)
|
||||||
|
{
|
||||||
|
m_status["twin"] = m_twin_link->statusJson();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_status.removeMember("twin");
|
||||||
|
}
|
||||||
|
|
||||||
Json::Value cluster_arr(Json::arrayValue);
|
Json::Value cluster_arr(Json::arrayValue);
|
||||||
for (uint32_t tg : m_cluster_tgs)
|
for (uint32_t tg : m_cluster_tgs)
|
||||||
{
|
{
|
||||||
@@ -3133,6 +3142,11 @@ void Reflector::sendNodeListToAllPeers(void)
|
|||||||
link->sendNodeList(nodes);
|
link->sendNodeList(nodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (m_twin_link != nullptr)
|
||||||
|
{
|
||||||
|
m_twin_link->onLocalNodeListUpdated(nodes);
|
||||||
|
}
|
||||||
|
|
||||||
if (m_mqtt != nullptr)
|
if (m_mqtt != nullptr)
|
||||||
{
|
{
|
||||||
m_mqtt->publishLocalNodes(nodes);
|
m_mqtt->publishLocalNodes(nodes);
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@@ -70,6 +71,44 @@ using namespace Async;
|
|||||||
using namespace sigc;
|
using namespace sigc;
|
||||||
|
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
// Byte-bound and strip control chars / ':' from an identifier coming over
|
||||||
|
// the wire. Matches the sanitizer used in TrunkLink for node-list entries;
|
||||||
|
// the twin partner is HMAC-authenticated but wire data still flows into
|
||||||
|
// logs and /status JSON, so keep the same hygiene.
|
||||||
|
std::string sanitizeIdent(const std::string& in, size_t max_len)
|
||||||
|
{
|
||||||
|
std::string out;
|
||||||
|
out.reserve(std::min(in.size(), max_len));
|
||||||
|
for (char c : in)
|
||||||
|
{
|
||||||
|
unsigned char u = static_cast<unsigned char>(c);
|
||||||
|
if (u < 0x20 || u == 0x7f) continue;
|
||||||
|
if (c == ':') continue;
|
||||||
|
out += c;
|
||||||
|
if (out.size() >= max_len) break;
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string sanitizeText(const std::string& in, size_t max_bytes)
|
||||||
|
{
|
||||||
|
std::string out;
|
||||||
|
out.reserve(std::min(in.size(), max_bytes));
|
||||||
|
for (char c : in)
|
||||||
|
{
|
||||||
|
unsigned char u = static_cast<unsigned char>(c);
|
||||||
|
if (u < 0x20 || u == 0x7f) continue;
|
||||||
|
out += c;
|
||||||
|
if (out.size() >= max_bytes) break;
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
} /* anonymous namespace */
|
||||||
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
*
|
*
|
||||||
* TwinLink public methods
|
* TwinLink public methods
|
||||||
@@ -268,6 +307,14 @@ void TwinLink::onExternalTrunkTalkerStop(uint32_t tg,
|
|||||||
} /* TwinLink::onExternalTrunkTalkerStop */
|
} /* TwinLink::onExternalTrunkTalkerStop */
|
||||||
|
|
||||||
|
|
||||||
|
void TwinLink::onLocalNodeListUpdated(
|
||||||
|
const std::vector<MsgTrunkNodeList::NodeEntry>& nodes)
|
||||||
|
{
|
||||||
|
if (!isActive()) return;
|
||||||
|
sendMsg(MsgTrunkNodeList(nodes));
|
||||||
|
} /* TwinLink::onLocalNodeListUpdated */
|
||||||
|
|
||||||
|
|
||||||
bool TwinLink::isActive(void) const
|
bool TwinLink::isActive(void) const
|
||||||
{
|
{
|
||||||
return (m_con.isConnected() && m_ob_hello_received) ||
|
return (m_con.isConnected() && m_ob_hello_received) ||
|
||||||
@@ -275,6 +322,41 @@ bool TwinLink::isActive(void) const
|
|||||||
} /* TwinLink::isActive */
|
} /* TwinLink::isActive */
|
||||||
|
|
||||||
|
|
||||||
|
Json::Value TwinLink::statusJson(void) const
|
||||||
|
{
|
||||||
|
Json::Value obj(Json::objectValue);
|
||||||
|
obj["host"] = m_peer_host;
|
||||||
|
obj["port"] = m_peer_port;
|
||||||
|
obj["connected"] = isActive();
|
||||||
|
obj["outbound_connected"] = m_con.isConnected();
|
||||||
|
obj["outbound_hello"] = m_ob_hello_received;
|
||||||
|
obj["inbound_connected"] = (m_inbound_con != nullptr);
|
||||||
|
obj["inbound_hello"] = m_ib_hello_received;
|
||||||
|
obj["local_prefix"] = m_local_prefix;
|
||||||
|
obj["peer_id"] = m_peer_id_received;
|
||||||
|
obj["priority"] = static_cast<Json::UInt>(m_priority);
|
||||||
|
obj["peer_priority"] = static_cast<Json::UInt>(m_peer_priority);
|
||||||
|
|
||||||
|
Json::Value nodes_arr(Json::arrayValue);
|
||||||
|
for (const auto& n : m_partner_nodes)
|
||||||
|
{
|
||||||
|
Json::Value entry(Json::objectValue);
|
||||||
|
entry["callsign"] = n.callsign;
|
||||||
|
entry["tg"] = static_cast<Json::UInt>(n.tg);
|
||||||
|
if (n.lat != 0.0f || n.lon != 0.0f)
|
||||||
|
{
|
||||||
|
entry["lat"] = n.lat;
|
||||||
|
entry["lon"] = n.lon;
|
||||||
|
}
|
||||||
|
if (!n.qth_name.empty()) entry["qth_name"] = n.qth_name;
|
||||||
|
nodes_arr.append(entry);
|
||||||
|
}
|
||||||
|
obj["nodes"] = nodes_arr;
|
||||||
|
|
||||||
|
return obj;
|
||||||
|
} /* TwinLink::statusJson */
|
||||||
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
*
|
*
|
||||||
* TwinLink private methods
|
* TwinLink private methods
|
||||||
@@ -370,6 +452,9 @@ void TwinLink::onFrameReceived(FramedTcpConnection* con,
|
|||||||
case MsgTrunkFlush::TYPE:
|
case MsgTrunkFlush::TYPE:
|
||||||
handleMsgTrunkFlush(ss);
|
handleMsgTrunkFlush(ss);
|
||||||
break;
|
break;
|
||||||
|
case MsgTrunkNodeList::TYPE:
|
||||||
|
handleMsgTrunkNodeList(ss);
|
||||||
|
break;
|
||||||
case MsgTwinExtTalkerStart::TYPE:
|
case MsgTwinExtTalkerStart::TYPE:
|
||||||
handleMsgTwinExtTalkerStart(ss);
|
handleMsgTwinExtTalkerStart(ss);
|
||||||
break;
|
break;
|
||||||
@@ -529,6 +614,48 @@ void TwinLink::handleMsgTwinExtTalkerStop(std::istream& is)
|
|||||||
} /* TwinLink::handleMsgTwinExtTalkerStop */
|
} /* TwinLink::handleMsgTwinExtTalkerStop */
|
||||||
|
|
||||||
|
|
||||||
|
void TwinLink::handleMsgTrunkNodeList(std::istream& is)
|
||||||
|
{
|
||||||
|
MsgTrunkNodeList msg;
|
||||||
|
if (!msg.unpack(is))
|
||||||
|
{
|
||||||
|
geulog::error("twin", "TWIN: Failed to unpack MsgTrunkNodeList");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<MsgTrunkNodeList::NodeEntry> sanitized;
|
||||||
|
sanitized.reserve(msg.nodes().size());
|
||||||
|
unsigned dropped = 0;
|
||||||
|
for (const auto& n : msg.nodes())
|
||||||
|
{
|
||||||
|
MsgTrunkNodeList::NodeEntry e;
|
||||||
|
e.callsign = sanitizeIdent(n.callsign, 32);
|
||||||
|
if (e.callsign.empty())
|
||||||
|
{
|
||||||
|
++dropped;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
e.tg = n.tg;
|
||||||
|
e.qth_name = sanitizeText(n.qth_name, 64);
|
||||||
|
if (std::isfinite(n.lat) && std::isfinite(n.lon) &&
|
||||||
|
n.lat >= -90.0f && n.lat <= 90.0f &&
|
||||||
|
n.lon >= -180.0f && n.lon <= 180.0f)
|
||||||
|
{
|
||||||
|
e.lat = n.lat;
|
||||||
|
e.lon = n.lon;
|
||||||
|
}
|
||||||
|
sanitized.push_back(std::move(e));
|
||||||
|
}
|
||||||
|
if (dropped > 0)
|
||||||
|
{
|
||||||
|
geulog::warn("twin", "TWIN: dropped ", dropped,
|
||||||
|
" node list entrie(s) with empty/invalid callsign");
|
||||||
|
}
|
||||||
|
|
||||||
|
m_partner_nodes = std::move(sanitized);
|
||||||
|
} /* TwinLink::handleMsgTrunkNodeList */
|
||||||
|
|
||||||
|
|
||||||
void TwinLink::heartbeatTick(Async::Timer* /*t*/)
|
void TwinLink::heartbeatTick(Async::Timer* /*t*/)
|
||||||
{
|
{
|
||||||
// Outbound heartbeat
|
// Outbound heartbeat
|
||||||
|
|||||||
@@ -43,6 +43,8 @@ logic — the pair shares everything.
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include <json/json.h>
|
||||||
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
*
|
*
|
||||||
@@ -56,6 +58,15 @@ logic — the pair shares everything.
|
|||||||
#include <AsyncTimer.h>
|
#include <AsyncTimer.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Local Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include "ReflectorMsg.h"
|
||||||
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
*
|
*
|
||||||
* Forward declarations
|
* Forward declarations
|
||||||
@@ -63,8 +74,6 @@ logic — the pair shares everything.
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
class Reflector;
|
class Reflector;
|
||||||
class MsgTrunkHello;
|
|
||||||
class ReflectorMsg;
|
|
||||||
|
|
||||||
class TwinLink
|
class TwinLink
|
||||||
{
|
{
|
||||||
@@ -92,12 +101,19 @@ class TwinLink
|
|||||||
void onExternalTrunkTalkerStop(uint32_t tg,
|
void onExternalTrunkTalkerStop(uint32_t tg,
|
||||||
const std::string& peer_id);
|
const std::string& peer_id);
|
||||||
|
|
||||||
|
// Called by Reflector when the local node roster changes, to mirror
|
||||||
|
// our connected-stations list to the twin partner.
|
||||||
|
void onLocalNodeListUpdated(
|
||||||
|
const std::vector<MsgTrunkNodeList::NodeEntry>& nodes);
|
||||||
|
|
||||||
bool isActive(void) const;
|
bool isActive(void) const;
|
||||||
const std::string& partnerHost(void) const { return m_peer_host; }
|
const std::string& partnerHost(void) const { return m_peer_host; }
|
||||||
uint16_t partnerPort(void) const { return m_peer_port; }
|
uint16_t partnerPort(void) const { return m_peer_port; }
|
||||||
const std::string& secret(void) const { return m_secret; }
|
const std::string& secret(void) const { return m_secret; }
|
||||||
const std::string& localPrefix(void) const { return m_local_prefix; }
|
const std::string& localPrefix(void) const { return m_local_prefix; }
|
||||||
|
|
||||||
|
Json::Value statusJson(void) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
using FramedTcpClient =
|
using FramedTcpClient =
|
||||||
Async::TcpPrioClient<Async::FramedTcpConnection>;
|
Async::TcpPrioClient<Async::FramedTcpConnection>;
|
||||||
@@ -124,6 +140,7 @@ class TwinLink
|
|||||||
bool m_ib_hello_received;
|
bool m_ib_hello_received;
|
||||||
unsigned m_ib_hb_tx_cnt;
|
unsigned m_ib_hb_tx_cnt;
|
||||||
unsigned m_ib_hb_rx_cnt;
|
unsigned m_ib_hb_rx_cnt;
|
||||||
|
std::vector<MsgTrunkNodeList::NodeEntry> m_partner_nodes;
|
||||||
|
|
||||||
// No copy
|
// No copy
|
||||||
TwinLink(const TwinLink&);
|
TwinLink(const TwinLink&);
|
||||||
@@ -140,6 +157,7 @@ class TwinLink
|
|||||||
void handleMsgTrunkAudio(std::istream& is);
|
void handleMsgTrunkAudio(std::istream& is);
|
||||||
void handleMsgTrunkFlush(std::istream& is);
|
void handleMsgTrunkFlush(std::istream& is);
|
||||||
void handleMsgTrunkHeartbeat(void);
|
void handleMsgTrunkHeartbeat(void);
|
||||||
|
void handleMsgTrunkNodeList(std::istream& is);
|
||||||
void handleMsgTwinExtTalkerStart(std::istream& is);
|
void handleMsgTwinExtTalkerStart(std::istream& is);
|
||||||
void handleMsgTwinExtTalkerStop(std::istream& is);
|
void handleMsgTwinExtTalkerStop(std::istream& is);
|
||||||
void heartbeatTick(Async::Timer*);
|
void heartbeatTick(Async::Timer*);
|
||||||
|
|||||||
@@ -628,6 +628,59 @@ class TestTwinIntegration(unittest.TestCase):
|
|||||||
sender.close()
|
sender.close()
|
||||||
sat.close()
|
sat.close()
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
|
# Test 11: Twin partner's node roster appears in /status.twin.nodes
|
||||||
|
# ------------------------------------------------------------------
|
||||||
|
def test_11_twin_partner_nodelist_in_status(self):
|
||||||
|
"""A client authenticated on ref1 appears in ref2's /status under
|
||||||
|
twin.nodes, and disappears after it disconnects.
|
||||||
|
|
||||||
|
Exercises the roster path added for issue #3:
|
||||||
|
ref1 client auth -> Reflector::sendNodeListToAllPeers
|
||||||
|
-> TwinLink::onLocalNodeListUpdated (MsgTrunkNodeList over [TWIN])
|
||||||
|
-> ref2.handleMsgTrunkNodeList -> m_partner_nodes
|
||||||
|
-> /status.twin.nodes
|
||||||
|
"""
|
||||||
|
client = ClientPeer()
|
||||||
|
try:
|
||||||
|
ref1_client_port = T.twin_mapped_client_port("ref1")
|
||||||
|
client.connect(HOST, ref1_client_port)
|
||||||
|
client.authenticate(callsign=CLIENT_CALLSIGN,
|
||||||
|
password=CLIENT_PASSWORD)
|
||||||
|
|
||||||
|
def callsign_on_twin():
|
||||||
|
status = get_status(*_http("ref2"))
|
||||||
|
twin = status.get("twin", {})
|
||||||
|
nodes = twin.get("nodes", [])
|
||||||
|
return any(n.get("callsign") == CLIENT_CALLSIGN for n in nodes)
|
||||||
|
|
||||||
|
wait_until(
|
||||||
|
callsign_on_twin,
|
||||||
|
timeout=10.0,
|
||||||
|
interval=0.25,
|
||||||
|
msg=f"{CLIENT_CALLSIGN} did not appear in ref2 /status.twin.nodes "
|
||||||
|
f"after login on ref1 — MsgTrunkNodeList may not be "
|
||||||
|
f"flowing over the [TWIN] link",
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
client.close()
|
||||||
|
|
||||||
|
# After disconnect, ref1 re-publishes its (now-empty) roster;
|
||||||
|
# ref2.twin.nodes should drop the callsign.
|
||||||
|
def callsign_gone_from_twin():
|
||||||
|
status = get_status(*_http("ref2"))
|
||||||
|
twin = status.get("twin", {})
|
||||||
|
nodes = twin.get("nodes", [])
|
||||||
|
return not any(n.get("callsign") == CLIENT_CALLSIGN for n in nodes)
|
||||||
|
|
||||||
|
wait_until(
|
||||||
|
callsign_gone_from_twin,
|
||||||
|
timeout=10.0,
|
||||||
|
interval=0.25,
|
||||||
|
msg=f"{CLIENT_CALLSIGN} still in ref2 /status.twin.nodes after "
|
||||||
|
f"disconnect from ref1",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def docker_compose_output(*args) -> str:
|
def docker_compose_output(*args) -> str:
|
||||||
"""Return the stdout+stderr of a docker-compose command as text."""
|
"""Return the stdout+stderr of a docker-compose command as text."""
|
||||||
|
|||||||
Reference in new issue
Block a user