Skip to content

Commit

Permalink
Add possibility to support different URI schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
JosefWN committed Apr 16, 2022
1 parent 6dd4c9e commit 5f14dc5
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 29 deletions.
1 change: 1 addition & 0 deletions lib/config/coap_config_all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:coap/coap.dart';
/// the config file to contain only those entries that override the defaults.
/// The file can't be empty, so version must as a minimum be present.
class CoapConfigAll extends DefaultCoapConfig {

@override
String get version => 'RFC7252';

Expand Down
1 change: 1 addition & 0 deletions lib/config/coap_config_default.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:coap/coap.dart';
/// the config file to contain only those entries that override the defaults.
/// The file can't be empty, so version must as a minimum be present.
class CoapConfigDefault extends DefaultCoapConfig {

@override
String get version => 'RFC7252';

Expand Down
4 changes: 2 additions & 2 deletions lib/src/coap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ class CoapClient {
if (_endpoint == null) {
final destination =
await CoapUtil.lookupHost(uri.host, addressType, null);
final socket = CoapNetworkUDP(destination, _config.defaultPort,
namespace: _namespace);
final socket = CoapINetwork.fromUri(uri,
address: destination, config: _config, namespace: _namespace);
await socket.bind();
_endpoint = CoapEndPoint(socket, _config, namespace: _namespace);
await _endpoint!.start();
Expand Down
2 changes: 1 addition & 1 deletion lib/src/coap_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class DefaultCoapConfig {
int defaultPort = CoapConstants.defaultPort;

/// The default CoAP port for secure CoAP communication (coaps).
int get defaultSecurePort => CoapConstants.defaultSecurePort;
int defaultSecurePort = CoapConstants.defaultSecurePort;

/// The port which HTTP proxy is on.
int get httpPort => 8080;
Expand Down
47 changes: 22 additions & 25 deletions lib/src/net/coap_endpoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@ part of coap;
class CoapEndPoint implements CoapIEndPoint, CoapIOutbox {
/// Instantiates a new endpoint with the
/// specified channel and configuration.
CoapEndPoint(CoapINetwork socket, DefaultCoapConfig config,
{required String namespace}) {
_config = config;
_socket = socket;
_eventBus = CoapEventBus(namespace: namespace);
_matcher = CoapMatcher(config, namespace: namespace);
_coapStack = CoapStack(config);
_currentId = config.useRandomIDStart ? Random().nextInt(1 << 16) : 0;
CoapEndPoint(this._socket, this._config, {required String namespace})
: _eventBus = CoapEventBus(namespace: namespace),
_matcher = CoapMatcher(_config, namespace: namespace),
_coapStack = CoapStack(_config),
_currentId = _config.useRandomIDStart ? Random().nextInt(1 << 16) : 0 {
subscr = _eventBus.on<CoapDataReceivedEvent>().listen(_receiveData);
}

late final CoapEventBus _eventBus;
final CoapEventBus _eventBus;

@override
String get namespace => _eventBus.namespace;

DefaultCoapConfig? _config;
final DefaultCoapConfig _config;

@override
DefaultCoapConfig get config => _config;

late int _currentId;
int _currentId;

@override
int get nextMessageId {
Expand All @@ -41,17 +42,15 @@ class CoapEndPoint implements CoapIEndPoint, CoapIOutbox {
@override
CoapInternetAddress? get destination => _socket.address;

@override
DefaultCoapConfig? get config => _config;
late CoapINetwork _socket;
late CoapStack _coapStack;
StreamSubscription? subscr;
final CoapStack _coapStack;
late final StreamSubscription subscr;

final CoapIMatcher _matcher;

late CoapIMatcher _matcher;
CoapInternetAddress? _localEndpoint;
final CoapINetwork _socket;

@override
CoapInternetAddress? get localEndpoint => _localEndpoint;
CoapInternetAddress? get localEndpoint => _socket.address;

/// Executor
CoapIExecutor executor = CoapExecutor();
Expand All @@ -61,10 +60,8 @@ class CoapEndPoint implements CoapIEndPoint, CoapIOutbox {

@override
Future<void> start() async {
_localEndpoint = _socket.address;
try {
_matcher.start();
_localEndpoint = _socket.address;
} on Exception {
stop();
rethrow;
Expand Down Expand Up @@ -103,7 +100,7 @@ class CoapEndPoint implements CoapIEndPoint, CoapIOutbox {
final data = typed.Uint8Buffer();
data.addAll(event.data);
// Return if we have no data, should not happen but be defensive
final decoder = config!.spec!.newMessageDecoder(data);
final decoder = config.spec!.newMessageDecoder(data);
if (decoder.isRequest) {
CoapRequest? request;
try {
Expand Down Expand Up @@ -210,7 +207,7 @@ class CoapEndPoint implements CoapIEndPoint, CoapIOutbox {
typed.Uint8Buffer _serializeEmpty(CoapEmptyMessage message) {
var bytes = message.bytes;
if (bytes == null) {
bytes = _config!.spec!.newMessageEncoder().encodeMessage(message);
bytes = _config.spec!.newMessageEncoder().encodeMessage(message);
message.bytes = bytes;
}
return bytes!;
Expand All @@ -219,7 +216,7 @@ class CoapEndPoint implements CoapIEndPoint, CoapIOutbox {
typed.Uint8Buffer _serializeRequest(CoapMessage message) {
var bytes = message.bytes;
if (bytes == null) {
bytes = _config!.spec!.newMessageEncoder().encodeMessage(message);
bytes = _config.spec!.newMessageEncoder().encodeMessage(message);
message.bytes = bytes;
}
return bytes!;
Expand All @@ -228,7 +225,7 @@ class CoapEndPoint implements CoapIEndPoint, CoapIOutbox {
typed.Uint8Buffer _serializeResponse(CoapMessage message) {
var bytes = message.bytes;
if (bytes == null) {
bytes = _config!.spec!.newMessageEncoder().encodeMessage(message);
bytes = _config.spec!.newMessageEncoder().encodeMessage(message);
message.bytes = bytes;
}
return bytes!;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/net/coap_iendpoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ abstract class CoapIEndPoint {
CoapInternetAddress? get destination;

/// Gets this endpoint's configuration.
DefaultCoapConfig? get config;
DefaultCoapConfig get config;

/// The next message id to use
int get nextMessageId;
Expand Down
29 changes: 29 additions & 0 deletions lib/src/network/coap_inetwork.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@

part of coap;

/// This [Exception] is thrown when an unsupported URI scheme is encountered.
class UnsupportedProtocolException implements Exception {
/// The error message of this [Exception].
String get message => 'Unsupported URI scheme $uriScheme encountered.';

/// The unsupported Uri Scheme that was encountered.
final String uriScheme;

/// Constructor.
UnsupportedProtocolException(this.uriScheme);
}

/// Abstract networking class, allows different implementations for
/// UDP, test etc.
abstract class CoapINetwork {
Expand All @@ -31,4 +43,21 @@ abstract class CoapINetwork {

/// Close the socket
void close();

/// Creates a new CoapINetwork from a given URI
static CoapINetwork fromUri(
Uri uri, {
required CoapInternetAddress? address,
required DefaultCoapConfig config,
String namespace = '',
}) {
int? port = uri.port > 0 ? uri.port : null;
switch (uri.scheme) {
case CoapConstants.uriScheme:
return CoapNetworkUDP(address, port ?? config.defaultPort,
namespace: namespace);
default:
throw UnsupportedProtocolException(uri.scheme);
}
}
}
3 changes: 3 additions & 0 deletions lib/src/specification/coap_ispec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ abstract class CoapISpec {
/// Gets the default CoAP port in this draft.
int get defaultPort;

/// Gets the default CoAPS port in this draft.
int get defaultSecurePort;

/// Encodes a CoAP message into a bytes array.
/// Returns the encoded bytes, or null if the message can not be encoded,
/// i.e. the message is not a Request, a Response or an EmptyMessage.
Expand Down
3 changes: 3 additions & 0 deletions lib/src/specification/rfcs/coap_rfc7252.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class CoapRfc7252 implements CoapISpec {
@override
int get defaultPort => 5683;

@override
int get defaultSecurePort => 5684;

@override
CoapIMessageEncoder newMessageEncoder() => CoapMessageEncoderRfc7252();

Expand Down

0 comments on commit 5f14dc5

Please sign in to comment.