Merge pull request #13 from windy1/feature/browser-service-type

Add ServiceType to browser
This commit is contained in:
Walker Crouse 2021-08-19 18:20:44 -04:00 committed by GitHub
commit 7bc0ebf994
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 24 additions and 19 deletions

View File

@ -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));

2
examples/Cargo.lock generated
View File

@ -502,7 +502,7 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "zeroconf"
version = "0.7.2"
version = "0.8.2"
dependencies = [
"avahi-sys",
"bonjour-sys",

View File

@ -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));

View File

@ -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<ServiceDiscovery>, Option<Arc
)]
pub struct ServiceDiscovery {
name: String,
kind: String,
service_type: ServiceType,
domain: String,
host_name: String,
address: String,

View File

@ -75,11 +75,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));
//!

View File

@ -14,7 +14,10 @@ use super::{
use crate::ffi::{c_str, AsRaw, FromRaw};
use crate::prelude::*;
use crate::Result;
use crate::{EventLoop, NetworkInterface, ServiceDiscoveredCallback, ServiceDiscovery, TxtRecord};
use crate::{
EventLoop, NetworkInterface, ServiceDiscoveredCallback, ServiceDiscovery, ServiceType,
TxtRecord,
};
use avahi_sys::{
AvahiAddress, AvahiBrowserEvent, AvahiClient, AvahiClientFlags, AvahiClientState, AvahiIfIndex,
AvahiLookupResultFlags, AvahiProtocol, AvahiResolverEvent, AvahiServiceBrowser,
@ -23,6 +26,7 @@ use avahi_sys::{
use libc::{c_char, c_void};
use std::any::Any;
use std::ffi::CString;
use std::str::FromStr;
use std::sync::Arc;
use std::{fmt, ptr};
@ -37,12 +41,12 @@ pub struct AvahiMdnsBrowser {
}
impl TMdnsBrowser for AvahiMdnsBrowser {
fn new(kind: &str) -> 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)

View File

@ -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)

View File

@ -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()