feat(mqtt): enhance neighbor discovery JSON handling

Introduce new methods to manage neighbor discovery JSON budget and
entries in MyMesh. This includes tracking the number of queried and
published neighbors, measuring JSON sizes, and handling truncation
when the buffer limit is reached. These improvements optimize the
neighbor discovery process and ensure efficient JSON message
construction for MQTT communications.
This commit is contained in:
agessaman committed 2026-07-26 19:10:51 -07:00
1 parent aba571ed7d
commit 3c5b25c2cf
10 files changed
+423 -62

No files matched your search

+22 -2
View File
@@ -111,11 +111,31 @@ int MQTTMessageBuilder::buildNeighborsMessage(
const NeighborsMessageEntry* neighbors,
int neighbor_count,
char* buffer,
size_t buffer_size
size_t buffer_size,
int total_neighbors,
int queried_neighbors,
bool truncated
) {
return MQTTPayloadBuilder::buildNeighborsMessage(
doc, origin, origin_id, timestamp, self_scopes, neighbors, neighbor_count,
buffer, buffer_size);
buffer, buffer_size, total_neighbors, queried_neighbors, truncated);
}
size_t MQTTMessageBuilder::measureNeighborsMessageBase(
const char* origin,
const char* origin_id,
const char* timestamp,
const char* self_scopes,
int total_neighbors
) {
return MQTTPayloadBuilder::measureNeighborsMessageBase(
origin, origin_id, timestamp, self_scopes, total_neighbors);
}
size_t MQTTMessageBuilder::measureNeighborsMessageEntry(
const NeighborsMessageEntry& neighbor
) {
return MQTTPayloadBuilder::measureNeighborsMessageEntry(neighbor);
}
int MQTTMessageBuilder::buildPacketJSON(
+12 -1
View File
@@ -167,8 +167,19 @@ public:
const NeighborsMessageEntry* neighbors,
int neighbor_count,
char* buffer,
size_t buffer_size
size_t buffer_size,
int total_neighbors = -1,
int queried_neighbors = -1,
bool truncated = false
);
static size_t measureNeighborsMessageBase(
const char* origin,
const char* origin_id,
const char* timestamp,
const char* self_scopes,
int total_neighbors
);
static size_t measureNeighborsMessageEntry(const NeighborsMessageEntry& neighbor);
/**
* Convert packet to JSON message
+75 -20
View File
@@ -185,6 +185,68 @@ int MQTTPayloadBuilder::buildRawMessage(
return serializeComplete(root, buffer, buffer_size);
}
static JsonArray buildNeighborsMessageBase(
JsonDocument& doc,
const char* origin,
const char* origin_id,
const char* timestamp,
const char* self_scopes,
int total_neighbors,
int queried_neighbors,
bool truncated
) {
doc.clear();
JsonObject root = doc.to<JsonObject>();
root["timestamp"] = timestamp;
root["origin"] = origin;
root["origin_id"] = origin_id;
if (total_neighbors >= 0) {
root["total_neighbors"] = total_neighbors;
root["queried_neighbors"] = queried_neighbors >= 0 ? queried_neighbors : total_neighbors;
root["truncated"] = truncated;
}
JsonObject self = root["self"].to<JsonObject>();
self["scopes"] = self_scopes ? self_scopes : "";
return root["neighbors"].to<JsonArray>();
}
static void addNeighborsMessageEntry(
JsonArray& arr,
const MQTTPayloadBuilder::NeighborsMessageEntry& neighbor
) {
JsonObject nb = arr.add<JsonObject>();
nb["pubkey"] = neighbor.pubkey_hex;
nb["snr"] = neighbor.snr;
nb["heard_secs_ago"] = neighbor.heard_secs_ago;
nb["scopes"] = neighbor.scopes ? neighbor.scopes : "";
nb["status"] = neighbor.status;
}
size_t MQTTPayloadBuilder::measureNeighborsMessageBase(
const char* origin,
const char* origin_id,
const char* timestamp,
const char* self_scopes,
int total_neighbors
) {
JsonDocument doc;
buildNeighborsMessageBase(
doc, origin, origin_id, timestamp, self_scopes,
total_neighbors, total_neighbors, false);
return measureJson(doc);
}
size_t MQTTPayloadBuilder::measureNeighborsMessageEntry(
const NeighborsMessageEntry& neighbor
) {
JsonDocument doc;
JsonArray arr = doc.to<JsonArray>();
addNeighborsMessageEntry(arr, neighbor);
JsonObject entry = arr[0];
return measureJson(entry);
}
int MQTTPayloadBuilder::buildNeighborsMessage(
JsonDocument& doc,
const char* origin,
@@ -194,38 +256,31 @@ int MQTTPayloadBuilder::buildNeighborsMessage(
const NeighborsMessageEntry* neighbors,
int neighbor_count,
char* buffer,
size_t buffer_size
size_t buffer_size,
int total_neighbors,
int queried_neighbors,
bool truncated
) {
if (!buffer || buffer_size == 0) return 0;
doc.clear();
JsonObject root = doc.to<JsonObject>();
root["timestamp"] = timestamp;
root["origin"] = origin;
root["origin_id"] = origin_id;
JsonObject self = root["self"].to<JsonObject>();
self["scopes"] = self_scopes ? self_scopes : "";
JsonArray arr = root["neighbors"].to<JsonArray>();
if (measureJson(root) >= buffer_size) return 0;
JsonArray arr = buildNeighborsMessageBase(
doc, origin, origin_id, timestamp, self_scopes,
total_neighbors, queried_neighbors, truncated);
if (doc.overflowed() || arr.isNull() || measureJson(doc) >= buffer_size) return 0;
for (int i = 0; i < neighbor_count; i++) {
JsonObject nb = arr.add<JsonObject>();
nb["pubkey"] = neighbors[i].pubkey_hex;
nb["snr"] = neighbors[i].snr;
nb["heard_secs_ago"] = neighbors[i].heard_secs_ago;
nb["scopes"] = neighbors[i].scopes ? neighbors[i].scopes : "";
nb["status"] = neighbors[i].status;
addNeighborsMessageEntry(arr, neighbors[i]);
if (doc.overflowed()) return 0;
// Entries arrive ordered most- to least-useful. Stop as soon as the next
// one would fill the fixed publish buffer, dropping the remaining tail so
// document growth stays bounded.
if (measureJson(root) >= buffer_size) {
if (measureJson(doc) >= buffer_size) {
arr.remove(arr.size() - 1);
if (total_neighbors >= 0) doc["truncated"] = true;
break;
}
}
return serializeComplete(root, buffer, buffer_size);
return serializeComplete(doc.as<JsonObject>(), buffer, buffer_size);
}
+16 -2
View File
@@ -89,7 +89,21 @@ public:
const NeighborsMessageEntry* neighbors,
int neighbor_count,
char* buffer,
size_t buffer_size
size_t buffer_size,
int total_neighbors = -1,
int queried_neighbors = -1,
bool truncated = false
);
};
// Exact serialized-size components used by paced neighbor discovery. The
// base reserves the largest progress metadata values for this snapshot;
// callers add each entry size plus one byte for commas after the first.
static size_t measureNeighborsMessageBase(
const char* origin,
const char* origin_id,
const char* timestamp,
const char* self_scopes,
int total_neighbors
);
static size_t measureNeighborsMessageEntry(const NeighborsMessageEntry& neighbor);
};
+3 -1
View File
@@ -3314,8 +3314,10 @@ bool MQTTBridge::publishNeighbors() {
// MeshRank slots reject non-packets by contract, so buildTopicForSlot
// returns false for them here and the slot is skipped.
if (buildTopicForSlot(i, MSG_NEIGHBORS, topic, sizeof(topic))) {
// Neighbor snapshots are periodically refreshed. Publish synchronously
// at QoS 0 to avoid the QoS 1 outbox, retaining where the broker allows.
bool use_retain = _slots[i].preset ? _slots[i].preset->allow_retain : false;
if (publishToSlot(i, topic, _neighbors_json_buffer, use_retain, 1)) {
if (publishToSlot(i, topic, _neighbors_json_buffer, use_retain, 0)) {
published = true;
}
}