encode method
Encode local node metadata into an endpoint name string.
nodeId — full UUID of this node (only the first 8 chars are encoded).
role — this node's mesh role.
groupIds — all groups this node is subscribed to (may be empty).
microMessage — optional tiny payload to broadcast connectionlessly.
Returns a pipe-delimited string under kEndpointNameMaxBytes.
Throws EndpointNameTooLongException if the combined group names and micro-message exceed the byte limit.
Implementation
String encode({
required String nodeId,
required NodeRole role,
List<String>? groupIds,
String? microMessage,
}) {
final prefix = nodeId.length >= 8 ? nodeId.substring(0, 8) : nodeId;
// Build the groups field: comma-separated or '*'
final groupsField = (groupIds != null && groupIds.isNotEmpty)
? groupIds.join(kGroupSeparator)
: '*';
final parts = [
kEndpointMagic,
kAirpassProtocolVersion.toString(),
role.code,
groupsField,
prefix,
];
// Append micro-message if provided
if (microMessage != null && microMessage.isNotEmpty) {
parts.add(microMessage);
}
final encoded = parts.join(kEndpointDelimiter);
// Validate byte length (UTF-8, not UTF-16 code units)
final byteLength = utf8.encode(encoded).length;
if (byteLength > kEndpointNameMaxBytes) {
throw EndpointNameTooLongException(
actualLength: byteLength,
maxLength: kEndpointNameMaxBytes,
encoded: encoded,
);
}
return encoded;
}