Skip to content

Commit

Permalink
fix: allow multiple URI schemes during discovery
Browse files Browse the repository at this point in the history
This commit addresses a potential problem which could arise
if the scheme of a URI obtained via CoRE Resource Discovery
was not the same as the URI scheme of the original discovery URI.
This was caused by the fact that only one ProtocolClient for
the original URI scheme was initialized.

This commit addresses the issue by introducing a cache for clients
which is expanded if more clients for different URI schemes are needed
during discovery. In the stop method, all clients are cleaned up
simultaneously.
  • Loading branch information
JKRhb committed Aug 22, 2022
1 parent 48829ed commit 73faf66
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions lib/src/core/thing_discovery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ class DiscoveryException implements Exception {
class ThingDiscovery extends Stream<ThingDescription>
implements scripting_api.ThingDiscovery {
/// Creates a new [ThingDiscovery] object with a given [thingFilter].
ThingDiscovery(this.thingFilter, this._servient)
: _client = _servient.clientFor(thingFilter.url.scheme) {
ThingDiscovery(this.thingFilter, this._servient) {
_stream = _start();
}

final Servient _servient;

final Map<String, ProtocolClient> _clients = {};

bool _active = true;

@override
Expand All @@ -48,8 +49,6 @@ class ThingDiscovery extends Stream<ThingDescription>

late final Stream<ThingDescription> _stream;

final ProtocolClient _client;

Stream<ThingDescription> _start() async* {
final discoveryMethod = thingFilter.method;

Expand All @@ -68,9 +67,23 @@ class ThingDiscovery extends Stream<ThingDescription>
}
}

ProtocolClient _clientForUriScheme(Uri uri) {
final uriScheme = uri.scheme;
var client = _clients[uriScheme];

if (client == null) {
client = _servient.clientFor(uriScheme);
_clients[uriScheme] = client;
}

return client;
}

@override
Future<void> stop() async {
await _client.stop();
final stopFutures = _clients.values.map((client) => client.stop());
await Future.wait(stopFutures);
_clients.clear();
_active = false;
}

Expand All @@ -89,7 +102,9 @@ class ThingDiscovery extends Stream<ThingDescription>
}

Stream<ThingDescription> _discoverDirectly(Uri uri) async* {
yield* _client
final client = _clientForUriScheme(uri);

yield* client
.discoverDirectly(uri, disableMulticast: true)
.asyncMap((content) => _decodeThingDescription(content, uri));
}
Expand Down Expand Up @@ -149,8 +164,10 @@ class ThingDiscovery extends Stream<ThingDescription>
) async* {
final Set<Uri> discoveredUris = {};
final discoveryUri = uri.toLinkFormatDiscoveryUri(resourceType);
final client = _clientForUriScheme(uri);

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

try {
Expand Down

0 comments on commit 73faf66

Please sign in to comment.