From 2654a2072319551402ca361f6b24b2756da5f430 Mon Sep 17 00:00:00 2001 From: Walker Crouse Date: Thu, 19 Aug 2021 18:07:55 -0400 Subject: [PATCH 1/2] Update browser for new ServiceType Signed-off-by: Walker Crouse --- README.md | 4 ++-- examples/Cargo.lock | 2 +- examples/browser/src/main.rs | 4 ++-- zeroconf/src/browser.rs | 6 +++--- zeroconf/src/lib.rs | 4 ++-- zeroconf/src/linux/browser.rs | 12 ++++++++---- zeroconf/src/tests/service_test.rs | 2 +- 7 files changed, 19 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index af2e82a..4a7fd01 100644 --- a/README.md +++ b/README.md @@ -85,11 +85,11 @@ fn on_service_registered( use std::any::Any; use std::sync::Arc; use std::time::Duration; -use zeroconf::{MdnsBrowser, ServiceDiscovery}; use zeroconf::prelude::*; +use zeroconf::{MdnsBrowser, ServiceDiscovery, ServiceType}; fn main() { - let mut browser = MdnsBrowser::new("_http._tcp"); + let mut browser = MdnsBrowser::new(ServiceType::new("http", "tcp").unwrap()); browser.set_service_discovered_callback(Box::new(on_service_discovered)); diff --git a/examples/Cargo.lock b/examples/Cargo.lock index 7476f86..219b55b 100644 --- a/examples/Cargo.lock +++ b/examples/Cargo.lock @@ -502,7 +502,7 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "zeroconf" -version = "0.7.2" +version = "0.8.2" dependencies = [ "avahi-sys", "bonjour-sys", diff --git a/examples/browser/src/main.rs b/examples/browser/src/main.rs index 7248169..b62117c 100644 --- a/examples/browser/src/main.rs +++ b/examples/browser/src/main.rs @@ -1,11 +1,11 @@ use std::any::Any; use std::sync::Arc; use std::time::Duration; -use zeroconf::{MdnsBrowser, ServiceDiscovery}; use zeroconf::prelude::*; +use zeroconf::{MdnsBrowser, ServiceDiscovery, ServiceType}; fn main() { - let mut browser = MdnsBrowser::new("_http._tcp"); + let mut browser = MdnsBrowser::new(ServiceType::new("http", "tcp").unwrap()); browser.set_service_discovered_callback(Box::new(on_service_discovered)); diff --git a/zeroconf/src/browser.rs b/zeroconf/src/browser.rs index 6b92524..0cf1bf9 100644 --- a/zeroconf/src/browser.rs +++ b/zeroconf/src/browser.rs @@ -1,13 +1,13 @@ //! Trait definition for cross-platform browser -use crate::{EventLoop, NetworkInterface, Result, TxtRecord}; +use crate::{EventLoop, NetworkInterface, Result, ServiceType, TxtRecord}; use std::any::Any; use std::sync::Arc; /// Interface for interacting with underlying mDNS implementation service browsing capabilities. pub trait TMdnsBrowser { /// Creates a new `MdnsBrowser` that browses for the specified `kind` (e.g. `_http._tcp`) - fn new(kind: &str) -> Self; + fn new(service_type: ServiceType) -> Self; /// Sets the network interface on which to browse for services on. /// @@ -49,7 +49,7 @@ pub type ServiceDiscoveredCallback = dyn Fn(Result, Option Self { + fn new(service_type: ServiceType) -> Self { Self { client: None, poll: None, browser: None, - kind: c_string!(kind.to_string()), + kind: c_string!(service_type.to_string()), context: Box::into_raw(Box::default()), interface_index: constants::AVAHI_IF_UNSPEC, } @@ -268,7 +272,7 @@ unsafe fn handle_resolver_found( let result = ServiceDiscovery::builder() .name(name.to_string()) - .kind(kind.to_string()) + .service_type(ServiceType::from_str(kind)?) .domain(domain.to_string()) .host_name(host_name.to_string()) .address(address) diff --git a/zeroconf/src/tests/service_test.rs b/zeroconf/src/tests/service_test.rs index 4b98230..14ef61b 100644 --- a/zeroconf/src/tests/service_test.rs +++ b/zeroconf/src/tests/service_test.rs @@ -25,7 +25,7 @@ fn service_register_is_browsable() { service.set_txt_record(txt.clone()); service.set_registered_callback(Box::new(|_, context| { - let mut browser = MdnsBrowser::new("_http._tcp"); + let mut browser = MdnsBrowser::new(ServiceType::new("http", "tcp").unwrap()); let context = context .as_ref() From 1b054cd7453570d85a4ce9833838e9b75b1b4288 Mon Sep 17 00:00:00 2001 From: Walker Crouse Date: Thu, 19 Aug 2021 18:15:13 -0400 Subject: [PATCH 2/2] Update Bonjour browser for ServiceType Signed-off-by: Walker Crouse --- zeroconf/src/macos/browser.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/zeroconf/src/macos/browser.rs b/zeroconf/src/macos/browser.rs index 1e868b4..6b9bda4 100644 --- a/zeroconf/src/macos/browser.rs +++ b/zeroconf/src/macos/browser.rs @@ -7,7 +7,7 @@ use super::txt_record_ref::ManagedTXTRecordRef; use super::{bonjour_util, constants}; use crate::ffi::{c_str, AsRaw, FromRaw}; use crate::prelude::*; -use crate::{EventLoop, NetworkInterface, Result, TxtRecord}; +use crate::{EventLoop, NetworkInterface, Result, ServiceType, TxtRecord}; use crate::{ServiceDiscoveredCallback, ServiceDiscovery}; use bonjour_sys::{DNSServiceErrorType, DNSServiceFlags, DNSServiceRef}; use libc::{c_char, c_uchar, c_void, sockaddr_in}; @@ -16,6 +16,7 @@ use std::ffi::CString; use std::fmt::{self, Formatter}; use std::net::IpAddr; use std::ptr; +use std::str::FromStr; use std::sync::{Arc, Mutex}; #[derive(Debug)] @@ -27,10 +28,10 @@ pub struct BonjourMdnsBrowser { } impl TMdnsBrowser for BonjourMdnsBrowser { - fn new(kind: &str) -> Self { + fn new(service_type: ServiceType) -> Self { Self { service: Arc::default(), - kind: c_string!(kind), + kind: c_string!(service_type.to_string()), interface_index: constants::BONJOUR_IF_UNSPEC, context: Box::into_raw(Box::default()), } @@ -267,7 +268,7 @@ unsafe fn handle_get_address_info( let result = ServiceDiscovery::builder() .name(ctx.resolved_name.take().unwrap()) - .kind(ctx.resolved_kind.take().unwrap()) + .service_type(ServiceType::from_str(&ctx.resolved_kind.take().unwrap())?) .domain(domain) .host_name(hostname) .address(ip)