Skip to content

Commit 6c415fe

Browse files
authored
Merge pull request #83 from eclipse-thingweb/client-factories
feat(servient): improve handling of client factories
2 parents 3be4625 + 568bb89 commit 6c415fe

12 files changed

+136
-30
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ import 'package:dart_wot/dart_wot.dart';
4747
4848
Future<void> main(List<String> args) async {
4949
final CoapClientFactory coapClientFactory = CoapClientFactory();
50-
final servient = Servient()..addClientFactory(coapClientFactory);
50+
final servient = Servient(
51+
protocolClients: [coapClientFactory]
52+
);
5153
final wot = await servient.start();
5254
5355
final thingDescriptionJson = '''

example/coap_discovery.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Future<void> handleThingDescription(
3030
}
3131

3232
Future<void> main(List<String> args) async {
33-
final servient = Servient()..addClientFactory(CoapClientFactory());
33+
final servient = Servient(clientFactories: [CoapClientFactory()]);
3434

3535
final wot = await servient.start();
3636
final uri = Uri.parse('coap://plugfest.thingweb.io:5683/testthing');

example/coap_dns_sd_discovery.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ void handleThingDescription(ThingDescription thingDescription) =>
1212
print('Discovered TD with title "${thingDescription.title}".');
1313

1414
Future<void> main(List<String> args) async {
15-
final servient = Servient()..addClientFactory(CoapClientFactory());
15+
final servient = Servient(
16+
clientFactories: [
17+
CoapClientFactory(),
18+
],
19+
);
1620

1721
final wot = await servient.start();
1822
final uri = Uri.parse('_wot._udp.local');

example/coaps_readproperty.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ Future<void> main(List<String> args) async {
3535
),
3636
pskCredentialsCallback: _pskCredentialsCallback,
3737
);
38-
final servient = Servient()..addClientFactory(coapClientFactory);
38+
39+
final servient = Servient(
40+
clientFactories: [
41+
coapClientFactory,
42+
],
43+
);
3944

4045
final wot = await servient.start();
4146

example/complex_example.dart

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,19 @@ Future<BasicCredentials?> basicCredentialsCallback(
112112
}
113113

114114
Future<void> main() async {
115-
const coapConfig = CoapConfig(blocksize: 64);
116-
final CoapClientFactory coapClientFactory = CoapClientFactory(
117-
coapConfig: coapConfig,
115+
final coapClientFactory = CoapClientFactory(
116+
coapConfig: const CoapConfig(blocksize: 64),
118117
);
119-
final HttpClientFactory httpClientFactory =
118+
119+
final httpClientFactory =
120120
HttpClientFactory(basicCredentialsCallback: basicCredentialsCallback);
121-
final servient = Servient()
122-
..addClientFactory(coapClientFactory)
123-
..addClientFactory(httpClientFactory);
121+
122+
final servient = Servient(
123+
clientFactories: [
124+
coapClientFactory,
125+
httpClientFactory,
126+
],
127+
);
124128
final wot = await servient.start();
125129

126130
final thingDescription = ThingDescription(thingDescriptionJson);

example/core_link_format_discovery.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const propertyName = 'status';
1212
const actionName = 'toggle';
1313

1414
Future<void> main(List<String> args) async {
15-
final servient = Servient()..addClientFactory(CoapClientFactory());
15+
final servient = Servient(clientFactories: [CoapClientFactory()]);
1616

1717
final wot = await servient.start();
1818

example/example.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@ Future<BasicCredentials?> basicCredentialsCallback(
2323
}
2424

2525
Future<void> main(List<String> args) async {
26-
final CoapClientFactory coapClientFactory = CoapClientFactory();
27-
final HttpClientFactory httpClientFactory =
26+
final coapClientFactory = CoapClientFactory();
27+
final httpClientFactory =
2828
HttpClientFactory(basicCredentialsCallback: basicCredentialsCallback);
29-
final MqttClientFactory mqttClientFactory = MqttClientFactory();
30-
final servient = Servient()
31-
..addClientFactory(coapClientFactory)
32-
..addClientFactory(httpClientFactory)
33-
..addClientFactory(mqttClientFactory);
29+
final mqttClientFactory = MqttClientFactory();
30+
31+
final servient = Servient(
32+
clientFactories: [
33+
coapClientFactory,
34+
httpClientFactory,
35+
mqttClientFactory,
36+
],
37+
);
38+
3439
final wot = await servient.start();
3540

3641
const thingDescriptionJson = '''

example/http_basic_authentication.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,14 @@ Future<BasicCredentials?> basicCredentialsCallback(
6868
/// Illustrates the usage of both the basic and the automatic security scheme,
6969
/// with a server supporting basic authentication.
7070
Future<void> main(List<String> args) async {
71-
final HttpClientFactory httpClientFactory =
72-
HttpClientFactory(basicCredentialsCallback: basicCredentialsCallback);
73-
final servient = Servient()..addClientFactory(httpClientFactory);
71+
final httpClientFactory = HttpClientFactory(
72+
basicCredentialsCallback: basicCredentialsCallback,
73+
);
74+
final servient = Servient(
75+
clientFactories: [
76+
httpClientFactory,
77+
],
78+
);
7479
final wot = await servient.start();
7580

7681
final thingDescription = ThingDescription(thingDescriptionJson);

lib/src/core/servient.dart

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,24 @@ class ServientException implements Exception {
3737
class Servient {
3838
/// Creates a new [Servient].
3939
///
40-
/// A custom [contentSerdes] can be passed that supports other media types
41-
/// than the default ones.
40+
/// The [Servient] can be preconfigured with a [List] of
41+
/// [ProtocolClientFactory]s.
42+
/// However, it is also possible to dynamically [addClientFactory]s and
43+
/// [removeClientFactory]s at runtime.
44+
///
45+
/// If you want to support a custom media type not already included in the
46+
/// [ContentSerdes] class, a custom [contentSerdes] object can be passed as an
47+
/// argument.
4248
Servient({
49+
List<ProtocolClientFactory>? clientFactories,
4350
ServerSecurityCallback? serverSecurityCallback,
4451
ContentSerdes? contentSerdes,
4552
}) : contentSerdes = contentSerdes ?? ContentSerdes(),
46-
_serverSecurityCallback = serverSecurityCallback;
53+
_serverSecurityCallback = serverSecurityCallback {
54+
for (final clientFactory in clientFactories ?? <ProtocolClientFactory>[]) {
55+
addClientFactory(clientFactory);
56+
}
57+
}
4758

4859
final List<ProtocolServer> _servers = [];
4960
final Map<String, ProtocolClientFactory> _clientFactories = {};
@@ -184,13 +195,21 @@ class Servient {
184195
List<String> get clientSchemes =>
185196
_clientFactories.keys.toList(growable: false);
186197

187-
/// Adds a new [clientFactory] to this [Servient.]
198+
/// Adds a new [clientFactory] to this [Servient].
188199
void addClientFactory(ProtocolClientFactory clientFactory) {
189200
for (final scheme in clientFactory.schemes) {
190201
_clientFactories[scheme] = clientFactory;
191202
}
192203
}
193204

205+
/// Removes a [ProtocolClientFactory] matching the given [scheme] from this
206+
/// [Servient], if present.
207+
///
208+
/// If a [ProtocolClientFactory] was removed, the method returns it, otherwise
209+
/// the return value is `null`.
210+
ProtocolClientFactory? removeClientFactory(String scheme) =>
211+
_clientFactories.remove(scheme);
212+
194213
/// Checks whether a [ProtocolClient] is avaiable for a given [scheme].
195214
bool hasClientFor(String scheme) => _clientFactories.containsKey(scheme);
196215

test/binding_http/http_test.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,14 @@ void main() {
128128
]) async =>
129129
bearerCredentialsStore[uri.host];
130130

131-
final servient = Servient()
132-
..addClientFactory(
131+
final servient = Servient(
132+
clientFactories: [
133133
HttpClientFactory(
134134
basicCredentialsCallback: basicCredentialsCallback,
135135
bearerCredentialsCallback: bearerCredentialsCallback,
136136
),
137-
);
137+
],
138+
);
138139
final wot = await servient.start();
139140

140141
final consumedThing = await wot.consume(parsedTd);

0 commit comments

Comments
 (0)