Skip to content

Commit

Permalink
fixup! feat: implement unicast DNS-SD
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 27, 2024
1 parent 16ba671 commit c068f2a
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions lib/src/core/implementation/thing_discovery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class ThingDiscovery extends Stream<ThingDescription>
);
}

Map<String, String> _parseTxtRecords(String txtRecords) {
Map<String, String> _parseMdnsTxtRecords(String txtRecords) {
final recordsList = txtRecords
.split("\n")
.map((property) => property.split("="))
Expand All @@ -253,6 +253,29 @@ class ThingDiscovery extends Stream<ThingDescription>
return Map.fromEntries(recordsList);
}

static String _trimTxtRecord(String txtRecord) {
final startIndex = txtRecord.startsWith('"') ? 1 : 0;

final length = txtRecord.length;
final endIndex = txtRecord.endsWith('"') ? length - 1 : length;

return txtRecord.substring(startIndex, endIndex);
}

Map<String, String> _parseTxtRecords(List<RRecord>? txtRecords) {
final entries = txtRecords
?.map((txtRecord) => txtRecord.data)
.map(_trimTxtRecord)
.map((e) => e.split("="))
.where((element) => element.length == 2)
.map((txtRecord) {
return MapEntry(txtRecord[0], txtRecord[1]);
}) ??
[];

return Map.fromEntries(entries);
}

Future<Map<String, String>?> _lookupTxtRecords(
MDnsClient client,
String domainName,
Expand All @@ -267,7 +290,7 @@ class ThingDiscovery extends Stream<ThingDescription>
return null;
}

return _parseTxtRecords(firstTxtRecord);
return _parseMdnsTxtRecords(firstTxtRecord);
}

Stream<ThingDescription> _discoverUsingDnsSd(String name) async* {
Expand Down Expand Up @@ -309,20 +332,13 @@ class ThingDiscovery extends Stream<ThingDescription>
) ??
[];

final txtRecord = txtRecords.firstOrNull;

if (txtRecord == null) {
continue;
}

// FIXME: Add parsing of multiple TXT records
final parsedTxtRecord = _parseTxtRecords(txtRecord.data);
final parsedTxtRecords = _parseTxtRecords(txtRecords);

final uri = Uri(
host: target,
port: port,
path: parsedTxtRecord["td"],
scheme: parsedTxtRecord["scheme"] ?? defaultScheme,
path: parsedTxtRecords["td"],
scheme: parsedTxtRecords["scheme"] ?? defaultScheme,
);

final duplicate = !discoveredUris.add(uri);
Expand All @@ -331,7 +347,7 @@ class ThingDiscovery extends Stream<ThingDescription>
continue;
}

final type = parsedTxtRecord["type"] ?? defaultType;
final type = parsedTxtRecords["type"] ?? defaultType;

switch (type) {
case "Thing":
Expand Down

0 comments on commit c068f2a

Please sign in to comment.