Skip to content

Commit d2d43ab

Browse files
committed
fixup! feat: implement unicast DNS-SD
1 parent d45a91d commit d2d43ab

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

lib/src/core/implementation/thing_discovery.dart

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ class ThingDiscovery extends Stream<ThingDescription>
243243
);
244244
}
245245

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

256+
static String _trimTxtRecord(String txtRecord) {
257+
final startIndex = txtRecord.startsWith('"') ? 1 : 0;
258+
259+
final length = txtRecord.length;
260+
final endIndex = txtRecord.endsWith('"') ? length - 1 : length;
261+
262+
return txtRecord.substring(startIndex, endIndex);
263+
}
264+
265+
Map<String, String> _parseTxtRecords(List<RRecord>? txtRecords) {
266+
final entries = txtRecords
267+
?.map((txtRecord) => txtRecord.data)
268+
.map(_trimTxtRecord)
269+
.map((e) => e.split("="))
270+
.where((element) => element.length == 2)
271+
.map((txtRecord) {
272+
return MapEntry(txtRecord[0], txtRecord[1]);
273+
}) ??
274+
[];
275+
276+
return Map.fromEntries(entries);
277+
}
278+
256279
Future<Map<String, String>?> _lookupTxtRecords(
257280
MDnsClient client,
258281
String domainName,
@@ -267,7 +290,7 @@ class ThingDiscovery extends Stream<ThingDescription>
267290
return null;
268291
}
269292

270-
return _parseTxtRecords(firstTxtRecord);
293+
return _parseMdnsTxtRecords(firstTxtRecord);
271294
}
272295

273296
Stream<ThingDescription> _discoverUsingDnsSd(String name) async* {
@@ -309,20 +332,13 @@ class ThingDiscovery extends Stream<ThingDescription>
309332
) ??
310333
[];
311334

312-
final txtRecord = txtRecords.firstOrNull;
313-
314-
if (txtRecord == null) {
315-
continue;
316-
}
317-
318-
// FIXME: Add parsing of multiple TXT records
319-
final parsedTxtRecord = _parseTxtRecords(txtRecord.data);
335+
final parsedTxtRecords = _parseTxtRecords(txtRecords);
320336

321337
final uri = Uri(
322338
host: target,
323339
port: port,
324-
path: parsedTxtRecord["td"],
325-
scheme: parsedTxtRecord["scheme"] ?? defaultScheme,
340+
path: parsedTxtRecords["td"],
341+
scheme: parsedTxtRecords["scheme"] ?? defaultScheme,
326342
);
327343

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

334-
final type = parsedTxtRecord["type"] ?? defaultType;
350+
final type = parsedTxtRecords["type"] ?? defaultType;
335351

336352
switch (type) {
337353
case "Thing":

0 commit comments

Comments
 (0)