@@ -73,7 +73,8 @@ coap.PskCredentialsCallback? _createPskCallback(
7373}
7474
7575/// A [ProtocolClient] for the Constrained Application Protocol (CoAP).
76- final class CoapClient extends ProtocolClient {
76+ final class CoapClient extends ProtocolClient
77+ with DirectDiscoverer , MulticastDiscoverer , CoreLinkFormatDiscoverer {
7778 /// Creates a new [CoapClient] based on an optional [CoapConfig] .
7879 CoapClient ({
7980 CoapConfig ? coapConfig,
@@ -446,67 +447,22 @@ final class CoapClient extends ProtocolClient {
446447 @override
447448 Future <void > stop () async {}
448449
449- Stream <DiscoveryContent > _discoverFromMulticast (
450- coap.CoapClient client,
451- Uri uri,
452- ) async * {
453- final streamController = StreamController <DiscoveryContent >();
454- final multicastResponseHandler = coap.CoapMulticastResponseHandler (
455- (data) {
456- streamController.add (data.determineDiscoveryContent (uri.scheme));
457- },
458- onError: streamController.addError,
459- onDone: () async {
460- await streamController.close ();
461- },
462- );
463-
464- final content = _sendDiscoveryRequest (
465- uri,
466- coap.RequestMethod .get ,
467- form: null ,
468- accept: coap.CoapMediaType .applicationTdJson,
469- multicastResponseHandler: multicastResponseHandler,
470- );
471- unawaited (content);
472- yield * streamController.stream;
473- }
474-
475- Stream <DiscoveryContent > _discoverFromUnicast (
476- coap.CoapClient client,
477- Uri uri,
478- ) async * {
479- yield await _sendDiscoveryRequest (
480- uri,
481- coap.RequestMethod .get ,
482- form: null ,
483- accept: coap.CoapMediaType .applicationTdJson,
484- );
485- }
486-
487450 @override
488- Stream <DiscoveryContent > discoverDirectly (
489- Uri uri, {
490- bool disableMulticast = false ,
491- }) async * {
492- final client = coap.CoapClient (uri);
493-
494- if (uri.isMulticastAddress) {
495- if (! disableMulticast) {
496- yield * _discoverFromMulticast (client, uri);
497- }
498- } else {
499- yield * _discoverFromUnicast (client, uri);
500- }
501- }
451+ Future <DiscoveryContent > discoverDirectly (Uri uri) async =>
452+ _sendDiscoveryRequest (
453+ uri,
454+ coap.RequestMethod .get ,
455+ form: null ,
456+ accept: coap.CoapMediaType .applicationTdJson,
457+ );
502458
503459 @override
504460 Stream <DiscoveryContent > discoverWithCoreLinkFormat (Uri uri) async * {
505461 coap.CoapMulticastResponseHandler ? multicastResponseHandler;
506462 final streamController = StreamController <DiscoveryContent >();
507463
508464 // TODO: Replace once https://github.com/shamblett/coap/pull/129 is merged
509- if (uri.isMulticastAddress ) {
465+ if (uri.hasMulticastAddress ) {
510466 multicastResponseHandler = coap.CoapMulticastResponseHandler (
511467 (data) {
512468 streamController.add (data.determineDiscoveryContent (uri.scheme));
@@ -526,18 +482,34 @@ final class CoapClient extends ProtocolClient {
526482 multicastResponseHandler: multicastResponseHandler,
527483 );
528484
529- if (uri.isMulticastAddress ) {
485+ if (uri.hasMulticastAddress ) {
530486 yield * streamController.stream;
531487 } else {
532488 yield content;
533489 }
534490 }
535491
536492 @override
537- Future <Content > requestThingDescription (Uri url) async => _sendRequest (
538- url,
539- coap.RequestMethod .get ,
540- form: null ,
541- accept: coap.CoapMediaType .applicationTdJson,
542- );
493+ Stream <Content > discoverViaMulticast (Uri uri) async * {
494+ final streamController = StreamController <DiscoveryContent >();
495+ final multicastResponseHandler = coap.CoapMulticastResponseHandler (
496+ (data) {
497+ streamController.add (data.determineDiscoveryContent (uri.scheme));
498+ },
499+ onError: streamController.addError,
500+ onDone: () async {
501+ await streamController.close ();
502+ },
503+ );
504+
505+ final content = _sendDiscoveryRequest (
506+ uri,
507+ coap.RequestMethod .get ,
508+ form: null ,
509+ accept: coap.CoapMediaType .applicationTdJson,
510+ multicastResponseHandler: multicastResponseHandler,
511+ );
512+ unawaited (content);
513+ yield * streamController.stream;
514+ }
543515}
0 commit comments