diff --git a/README.md b/README.md index 3270bde0..2537f107 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,9 @@ import 'package:dart_wot/dart_wot.dart'; Future main(List args) async { final CoapClientFactory coapClientFactory = CoapClientFactory(); - final servient = Servient()..addClientFactory(coapClientFactory); + final servient = Servient( + protocolClients: [coapClientFactory] + ); final wot = await servient.start(); final thingDescriptionJson = ''' diff --git a/example/coap_discovery.dart b/example/coap_discovery.dart index 494cebe8..c1371810 100644 --- a/example/coap_discovery.dart +++ b/example/coap_discovery.dart @@ -21,16 +21,20 @@ Future handleThingDescription( ThingDescription thingDescription, ) async { final consumedThing = await wot.consume(thingDescription); + await consumedThing.writeProperty(propertyName, 'Hello World!'); + var output = await consumedThing.readProperty(propertyName); await output.printValue(); + await consumedThing.writeProperty(propertyName, 'Bye World!'); + output = await consumedThing.readProperty(propertyName); await output.printValue(); } Future main(List args) async { - final servient = Servient()..addClientFactory(CoapClientFactory()); + final servient = Servient(clientFactories: [CoapClientFactory()]); final wot = await servient.start(); final uri = Uri.parse('coap://plugfest.thingweb.io:5683/testthing'); diff --git a/example/coap_dns_sd_discovery.dart b/example/coap_dns_sd_discovery.dart index 48013414..6dbb1357 100644 --- a/example/coap_dns_sd_discovery.dart +++ b/example/coap_dns_sd_discovery.dart @@ -12,7 +12,11 @@ void handleThingDescription(ThingDescription thingDescription) => print('Discovered TD with title "${thingDescription.title}".'); Future main(List args) async { - final servient = Servient()..addClientFactory(CoapClientFactory()); + final servient = Servient( + clientFactories: [ + CoapClientFactory(), + ], + ); final wot = await servient.start(); final uri = Uri.parse('_wot._udp.local'); diff --git a/example/coaps_readproperty.dart b/example/coaps_readproperty.dart index 0d323ce9..469a11f0 100644 --- a/example/coaps_readproperty.dart +++ b/example/coaps_readproperty.dart @@ -35,7 +35,12 @@ Future main(List args) async { ), pskCredentialsCallback: _pskCredentialsCallback, ); - final servient = Servient()..addClientFactory(coapClientFactory); + + final servient = Servient( + clientFactories: [ + coapClientFactory, + ], + ); final wot = await servient.start(); diff --git a/example/complex_example.dart b/example/complex_example.dart index 5868110a..b9b1e766 100644 --- a/example/complex_example.dart +++ b/example/complex_example.dart @@ -112,15 +112,19 @@ Future basicCredentialsCallback( } Future main() async { - const coapConfig = CoapConfig(blocksize: 64); - final CoapClientFactory coapClientFactory = CoapClientFactory( - coapConfig: coapConfig, + final coapClientFactory = CoapClientFactory( + coapConfig: const CoapConfig(blocksize: 64), ); - final HttpClientFactory httpClientFactory = + + final httpClientFactory = HttpClientFactory(basicCredentialsCallback: basicCredentialsCallback); - final servient = Servient() - ..addClientFactory(coapClientFactory) - ..addClientFactory(httpClientFactory); + + final servient = Servient( + clientFactories: [ + coapClientFactory, + httpClientFactory, + ], + ); final wot = await servient.start(); final thingDescription = ThingDescription(thingDescriptionJson); diff --git a/example/core_link_format_discovery.dart b/example/core_link_format_discovery.dart index 1cfcfbca..aad9a4b8 100644 --- a/example/core_link_format_discovery.dart +++ b/example/core_link_format_discovery.dart @@ -12,7 +12,7 @@ const propertyName = 'status'; const actionName = 'toggle'; Future main(List args) async { - final servient = Servient()..addClientFactory(CoapClientFactory()); + final servient = Servient(clientFactories: [CoapClientFactory()]); final wot = await servient.start(); diff --git a/example/example.dart b/example/example.dart index f81843b5..6afe1dba 100644 --- a/example/example.dart +++ b/example/example.dart @@ -23,14 +23,19 @@ Future basicCredentialsCallback( } Future main(List args) async { - final CoapClientFactory coapClientFactory = CoapClientFactory(); - final HttpClientFactory httpClientFactory = + final coapClientFactory = CoapClientFactory(); + final httpClientFactory = HttpClientFactory(basicCredentialsCallback: basicCredentialsCallback); - final MqttClientFactory mqttClientFactory = MqttClientFactory(); - final servient = Servient() - ..addClientFactory(coapClientFactory) - ..addClientFactory(httpClientFactory) - ..addClientFactory(mqttClientFactory); + final mqttClientFactory = MqttClientFactory(); + + final servient = Servient( + clientFactories: [ + coapClientFactory, + httpClientFactory, + mqttClientFactory, + ], + ); + final wot = await servient.start(); const thingDescriptionJson = ''' diff --git a/example/http_basic_authentication.dart b/example/http_basic_authentication.dart index 2b787b00..dff3a1b0 100644 --- a/example/http_basic_authentication.dart +++ b/example/http_basic_authentication.dart @@ -68,9 +68,14 @@ Future basicCredentialsCallback( /// Illustrates the usage of both the basic and the automatic security scheme, /// with a server supporting basic authentication. Future main(List args) async { - final HttpClientFactory httpClientFactory = - HttpClientFactory(basicCredentialsCallback: basicCredentialsCallback); - final servient = Servient()..addClientFactory(httpClientFactory); + final httpClientFactory = HttpClientFactory( + basicCredentialsCallback: basicCredentialsCallback, + ); + final servient = Servient( + clientFactories: [ + httpClientFactory, + ], + ); final wot = await servient.start(); final thingDescription = ThingDescription(thingDescriptionJson); diff --git a/lib/src/core/servient.dart b/lib/src/core/servient.dart index 7f0af8e9..6a5165eb 100644 --- a/lib/src/core/servient.dart +++ b/lib/src/core/servient.dart @@ -40,13 +40,15 @@ class Servient { /// A custom [contentSerdes] can be passed that supports other media types /// than the default ones. Servient({ + required List clientFactories, ServerSecurityCallback? serverSecurityCallback, ContentSerdes? contentSerdes, - }) : contentSerdes = contentSerdes ?? ContentSerdes(), + }) : _clientFactories = _processProtocolClientFactories(clientFactories), + contentSerdes = contentSerdes ?? ContentSerdes(), _serverSecurityCallback = serverSecurityCallback; final List _servers = []; - final Map _clientFactories = {}; + final Map _clientFactories; final Map _things = {}; final Map _consumedThings = {}; @@ -55,6 +57,18 @@ class Servient { /// The [ContentSerdes] object that is used for serializing/deserializing. final ContentSerdes contentSerdes; + static Map _processProtocolClientFactories( + List protocolFactories, + ) { + return Map.fromEntries( + protocolFactories.expand((protocolFactory) sync* { + for (final scheme in protocolFactory.schemes) { + yield MapEntry(scheme, protocolFactory); + } + }), + ); + } + /// Starts this [Servient] and returns a [WoT] runtime object. /// /// The [WoT] runtime can be used for consuming, procuding, and discovering @@ -184,13 +198,6 @@ class Servient { List get clientSchemes => _clientFactories.keys.toList(growable: false); - /// Adds a new [clientFactory] to this [Servient.] - void addClientFactory(ProtocolClientFactory clientFactory) { - for (final scheme in clientFactory.schemes) { - _clientFactories[scheme] = clientFactory; - } - } - /// Checks whether a [ProtocolClient] is avaiable for a given [scheme]. bool hasClientFor(String scheme) => _clientFactories.containsKey(scheme); diff --git a/test/binding_http/http_test.dart b/test/binding_http/http_test.dart index 175ae8e9..fd173bef 100644 --- a/test/binding_http/http_test.dart +++ b/test/binding_http/http_test.dart @@ -128,13 +128,14 @@ void main() { ]) async => bearerCredentialsStore[uri.host]; - final servient = Servient() - ..addClientFactory( + final servient = Servient( + clientFactories: [ HttpClientFactory( basicCredentialsCallback: basicCredentialsCallback, bearerCredentialsCallback: bearerCredentialsCallback, ), - ); + ], + ); final wot = await servient.start(); final consumedThing = await wot.consume(parsedTd); diff --git a/test/core/consumed_thing_test.dart b/test/core/consumed_thing_test.dart index 5e1bf26a..66b38962 100644 --- a/test/core/consumed_thing_test.dart +++ b/test/core/consumed_thing_test.dart @@ -284,7 +284,7 @@ void main() { final parsedTd = ThingDescription(thingDescriptionJson); - final servient = Servient()..addClientFactory(HttpClientFactory()); + final servient = Servient(clientFactories: [HttpClientFactory()]); final wot = await servient.start(); final uriVariables = {'value': 'SFRUUEJJTiBpcyBhd2Vzb21l'}; diff --git a/test/core/dart_wot_test.dart b/test/core/dart_wot_test.dart index 6abda623..c0a051bb 100644 --- a/test/core/dart_wot_test.dart +++ b/test/core/dart_wot_test.dart @@ -17,7 +17,7 @@ void main() { }); test('Parse incomplete Thing Description', () async { - final servient = Servient(); + final servient = Servient(clientFactories: []); final wot = await servient.start(); final Map exposedThingInit = { '@context': 'https://www.w3.org/2022/wot/td/v1.1',