Skip to content

Commit 6f609cc

Browse files
committed
feat: add initial NDN binding implementation
1 parent 8f97d77 commit 6f609cc

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed

lib/binding_ndn.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2024 Contributors to the Eclipse Foundation. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
//
5+
// SPDX-License-Identifier: BSD-3-Clause
6+
7+
/// Protocol binding for Named Data Networking (NDN). Follows
8+
/// an experimental [NDN Protocol Binding specification].
9+
///
10+
/// [NDN Protocol Binding specification]: https://github.com/JKRhb/wot-binding-templates/tree/ndn-binding
11+
library binding_ndn;
12+
13+
export "src/binding_ndn/ndn_consumer_factory.dart";
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2024 Contributors to the Eclipse Foundation. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
//
5+
// SPDX-License-Identifier: BSD-3-Clause
6+
7+
import "package:meta/meta.dart";
8+
9+
import "ndn_consumer.dart";
10+
11+
@immutable
12+
13+
/// Configuration class used by [NdnClient]s.
14+
class NdnConfig {
15+
///
16+
const NdnConfig({
17+
this.faceUri,
18+
});
19+
20+
/// [Uri] of the local NDN Forwarding Daemon (NFD).
21+
///
22+
/// If `null`, then the default [Uri] will be used, connecting the
23+
/// [NdnClient] to the NFD via a Unix Socket.
24+
final Uri? faceUri;
25+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2024 Contributors to the Eclipse Foundation. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
//
5+
// SPDX-License-Identifier: BSD-3-Clause
6+
7+
import "package:dart_ndn/dart_ndn.dart";
8+
import "package:meta/meta.dart";
9+
10+
import "../../core.dart";
11+
import "ndn_config.dart";
12+
13+
/// A WoT [ProtocolClient] acting as an NDN consumer.
14+
@immutable
15+
class NdnClient implements ProtocolClient {
16+
/// Creates a new [NdnClient] from an NDN [_consumer].
17+
const NdnClient(this._consumer);
18+
19+
/// Asynchronously creates a new [NdnClient] from an [NdnConfig].
20+
static Future<NdnClient> create(NdnConfig config) async {
21+
final consumer = await Consumer.create(config.faceUri);
22+
return NdnClient(consumer);
23+
}
24+
25+
final Consumer _consumer;
26+
27+
@override
28+
Stream<DiscoveryContent> discoverDirectly(
29+
Uri uri, {
30+
bool disableMulticast = false,
31+
}) {
32+
// TODO: implement discoverDirectly
33+
throw UnimplementedError();
34+
}
35+
36+
@override
37+
Stream<DiscoveryContent> discoverWithCoreLinkFormat(Uri uri) {
38+
// TODO: implement discoverWithCoreLinkFormat
39+
throw UnimplementedError();
40+
}
41+
42+
@override
43+
Future<Content> invokeResource(AugmentedForm form, Content content) {
44+
// TODO: implement invokeResource
45+
throw UnimplementedError();
46+
}
47+
48+
@override
49+
Future<Content> readResource(AugmentedForm form) async {
50+
// TODO: Add Name.fromUri
51+
final name = Name.fromString(form.href.path);
52+
53+
final result = await _consumer.expressInterest(name);
54+
55+
switch (result) {
56+
case DataReceived(:final data):
57+
final iterable = [data.content ?? <int>[]];
58+
59+
return Content(form.contentType, Stream.fromIterable(iterable));
60+
default:
61+
throw Exception("TODO");
62+
}
63+
}
64+
65+
@override
66+
Future<Content> requestThingDescription(Uri url) {
67+
// TODO: implement requestThingDescription
68+
throw UnimplementedError();
69+
}
70+
71+
@override
72+
Future<void> stop() async {
73+
await _consumer.shutdown();
74+
}
75+
76+
@override
77+
Future<Subscription> subscribeResource(
78+
AugmentedForm form, {
79+
required void Function(Content content) next,
80+
void Function(Exception error)? error,
81+
required void Function() complete,
82+
}) {
83+
// TODO: implement subscribeResource
84+
throw UnimplementedError();
85+
}
86+
87+
@override
88+
Future<void> writeResource(AugmentedForm form, Content content) {
89+
// TODO: implement writeResource
90+
throw UnimplementedError();
91+
}
92+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2024 Contributors to the Eclipse Foundation. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
//
5+
// SPDX-License-Identifier: BSD-3-Clause
6+
7+
import "../../core.dart";
8+
import "ndn_config.dart";
9+
import "ndn_consumer.dart";
10+
11+
/// A [ProtocolClientFactory] that produces
12+
class NdnConsumerFactory implements ProtocolClientFactory {
13+
NdnConsumerFactory(this.ndnConfig);
14+
15+
/// The [NdnConfig] acting as the blueprint for creating
16+
final NdnConfig ndnConfig;
17+
18+
@override
19+
Future<ProtocolClient> createClient() async {
20+
return NdnClient.create(ndnConfig);
21+
}
22+
23+
@override
24+
bool destroy() {
25+
return true;
26+
}
27+
28+
@override
29+
bool init() {
30+
return true;
31+
}
32+
33+
@override
34+
Set<String> get schemes => {"ndn"};
35+
36+
@override
37+
bool supportsOperation(OperationType operationType, String? subprotocol) {
38+
return operationType == OperationType.readproperty && subprotocol == null;
39+
}
40+
}

0 commit comments

Comments
 (0)