windy1 / zeroconf-rs

zeroconf is a cross-platform library that wraps underlying ZeroConf/mDNS implementations such as Bonjour or Avahi, providing an easy and idiomatic way to both register and browse services.
MIT License
77 stars 25 forks source link

Add support for sub-types (cont.) #32

Closed windy1 closed 1 year ago

windy1 commented 1 year ago

Supersedes #31

Resolves #30

Thank you @ssnover for your contribution.

This PR includes the addition of sub-types on Avahi devices which was previously lacking. I've tested this for interoperability between Avahi and Bonjour devices and it seems to be working.

Browsing a service by sub-type from Avahi published by Bonjour

macOS:

$ cd ./examples/service
$ RUST_LOG=debug cargo run -- -s printer
   Compiling zeroconf-service-example v0.1.0 (/Users/walker/code/zeroconf-rs/examples/service)
    Finished dev [unoptimized + debuginfo] target(s) in 1.17s
warning: the following packages contain code that will be rejected by a future version of Rust: nom v5.1.2
note: to see what the problems were, use the option `--future-incompat-report`, or run `cargo report future-incompatibilities --id 1`
     Running `/Users/walker/code/zeroconf-rs/examples/target/debug/zeroconf-service-example -s printer`
[2023-09-23T20:16:34Z DEBUG zeroconf::macos::service] Registering service: BonjourMdnsService { service: Mutex { data: ManagedDNSServiceRef(0x0), poisoned: false, .. }, kind: "_http._tcp,_printer", port: 8080, name: Some("zeroconf_example_service"), domain: None, host: None, interface_index: 0, txt_record: Some(TxtRecord { data: {"foo": "bar"} }), context: BonjourServiceContext { user_context: Some(Any { .. }) } }
[2023-09-23T20:16:35Z INFO  zeroconf_service_example] Service registered: ServiceRegistration { name: "zeroconf_example_service", service_type: ServiceType { name: "http", protocol: "tcp", sub_types: [] }, domain: "local" }
[2023-09-23T20:16:35Z INFO  zeroconf_service_example] Context: Mutex { data: Context { service_name: "zeroconf_example_service" }, poisoned: false, .. }

Linux:

$ cd ./examples/browser
$ RUST_LOG=debug cargo run -- -s printer
   Compiling zeroconf v0.11.1 (/home/walker/code/personal/zeroconf-rs/zeroconf)
   Compiling zeroconf-browser-example v0.1.0 (/home/walker/code/personal/zeroconf-rs/examples/browser)
    Finished dev [unoptimized + debuginfo] target(s) in 0.85s
warning: the following packages contain code that will be rejected by a future version of Rust: nom v5.1.2
note: to see what the problems were, use the option `--future-incompat-report`, or run `cargo report future-incompatibilities --id 1`
     Running `/home/walker/code/personal/zeroconf-rs/examples/target/debug/zeroconf-browser-example -s printer`
[2023-09-23T20:19:18Z DEBUG zeroconf::linux::browser] Browsing services: AvahiMdnsBrowser { client: None, poll: None, browser: None, kind: "_printer._sub._http._tcp", interface_index: -1, context: AvahiBrowserContext { client: None, resolvers: ServiceResolverSet { resolvers: {} } } }
[2023-09-23T20:19:18Z DEBUG zeroconf::linux::browser] Service resolved: ServiceDiscovery { name: "zeroconf_example_service", service_type: ServiceType { name: "http", protocol: "tcp", sub_types: [] }, domain: "local", host_name: "Walkers-MacBook-Pro.local", address: "192.168.0.80", port: 8080, txt: Some(TxtRecord { data: {"foo": "bar"} }) }
[2023-09-23T20:19:18Z INFO  zeroconf_browser_example] Service discovered: ServiceDiscovery { name: "zeroconf_example_service", service_type: ServiceType { name: "http", protocol: "tcp", sub_types: [] }, domain: "local", host_name: "Walkers-MacBook-Pro.local", address: "192.168.0.80", port: 8080, txt: Some(TxtRecord { data: {"foo": "bar"} }) }

Browsing a service by sub-type from Bonjour published by Avahi

Linux:

cd ./examples/service
$ RUST_LOG=debug cargo run -- -s printer
   Compiling zeroconf-service-example v0.1.0 (/home/walker/code/personal/zeroconf-rs/examples/service)
    Finished dev [unoptimized + debuginfo] target(s) in 0.70s
warning: the following packages contain code that will be rejected by a future version of Rust: nom v5.1.2
note: to see what the problems were, use the option `--future-incompat-report`, or run `cargo report future-incompatibilities --id 1`
     Running `/home/walker/code/personal/zeroconf-rs/examples/target/debug/zeroconf-service-example -s printer`
[2023-09-23T20:20:28Z DEBUG zeroconf::linux::service] Registering service: AvahiMdnsService { client: None, poll: None, context: AvahiServiceContext { name: Some("zeroconf_example_service"), kind: "_http._tcp", port: 8080, group: None } }
[2023-09-23T20:20:28Z DEBUG zeroconf::linux::service] Creating group
[2023-09-23T20:20:28Z DEBUG zeroconf::linux::service] Adding service: _http._tcp
[2023-09-23T20:20:28Z DEBUG zeroconf::linux::service] Adding service subtype: _printer._sub._http._tcp

Bonjour:

cd ./examples/browser
RUST_LOG=debug cargo run -- -s printer
    Finished dev [unoptimized + debuginfo] target(s) in 0.08s
warning: the following packages contain code that will be rejected by a future version of Rust: nom v5.1.2
note: to see what the problems were, use the option `--future-incompat-report`, or run `cargo report future-incompatibilities --id 1`
     Running `/Users/walker/code/zeroconf-rs/examples/target/debug/zeroconf-browser-example -s printer`
[2023-09-23T20:25:02Z DEBUG zeroconf::macos::browser] Browsing services: BonjourMdnsBrowser { service: Mutex { data: ManagedDNSServiceRef(0x0), poisoned: false, .. }, kind: "_http._tcp,_printer", interface_index: 0, context: BonjourResolverContext { resolved_name: None, resolved_kind: None, resolved_domain: None, resolved_port: 0 } }
[2023-09-23T20:25:02Z INFO  zeroconf_browser_example] Service discovered: ServiceDiscovery { name: "zeroconf_example_service", service_type: ServiceType { name: "http", protocol: "tcp", sub_types: [] }, domain: "local", host_name: "picard.local.", address: "0.0.0.0", port: 8080, txt: Some(TxtRecord { data: {"foo": "bar"} }) }

Note: When I try to browse for services with an unregistered sub-type, there are no results, as expected.

ssnover commented 1 year ago

Thanks for taking it over the finish line! Do you anticipate publishing a new minor version once this is merged?

windy1 commented 1 year ago

@ssnover Yes, certainly.

windy1 commented 1 year ago

@ssnover Wouldn't mind a review if you don't mind, otherwise I will merge.