Skip to content

Commit

Permalink
Merge pull request #103 from eclipse-thingweb/well-known-core
Browse files Browse the repository at this point in the history
refactor: improve performance of CoRE resource discovery
  • Loading branch information
JKRhb authored Jan 18, 2024
2 parents 234a8cc + a234a99 commit 7d56965
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions lib/src/core/implementation/thing_discovery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,33 @@ class ThingDiscovery extends Stream<ThingDescription>

Stream<ThingDescription> _discoverWithCoreLinkFormat(Uri uri) async* {
// TODO: Remove additional quotes once fixed in CoAP library
yield* _performCoreLinkFormatDiscovery('"wot.thing"', uri)
.map(_discoverDirectly)
.flatten();
yield* _performCoreLinkFormatDiscovery('"wot.thing"', uri).transform(
StreamTransformer.fromBind(
(stream) async* {
await for (final uris in stream) {
final futures = uris.map(_servient.requestThingDescription);
yield* Stream.fromFutures(futures);
}
},
),
);
}

Stream<ThingDescription> _discoverfromCoreResourceDirectory(Uri uri) async* {
// TODO: Remove additional quotes once fixed in CoAP library
yield* _performCoreLinkFormatDiscovery('"core.rd-lookup-res"', uri)
.map(_discoverWithCoreLinkFormat)
.flatten();
.transform(
StreamTransformer.fromBind((stream) async* {
await for (final uris in stream) {
for (final uri in uris) {
yield* _discoverWithCoreLinkFormat(uri);
}
}
}),
);
}

Stream<Uri> _performCoreLinkFormatDiscovery(
Stream<Iterable<Uri>> _performCoreLinkFormatDiscovery(
String resourceType,
Uri uri,
) async* {
Expand All @@ -171,24 +185,13 @@ class ThingDiscovery extends Stream<ThingDescription>

await for (final coreWebLink
in client.discoverWithCoreLinkFormat(discoveryUri)) {
final Iterable<Uri> parsedUris;

try {
parsedUris = await _filterCoreWebLinks(resourceType, coreWebLink);
final parsedUris = await _filterCoreWebLinks(resourceType, coreWebLink);
yield parsedUris.where(discoveredUris.add);
} on Exception catch (exception) {
yield* Stream.error(exception);
continue;
}

for (final parsedUri in parsedUris) {
final uriAdded = discoveredUris.add(parsedUri);

if (!uriAdded) {
continue;
}

yield parsedUri;
}
}
}

Expand Down

0 comments on commit 7d56965

Please sign in to comment.