Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions adb_client/src/mdns/mdns_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ use std::{
collections::HashSet,
fmt::Display,
net::{IpAddr, Ipv4Addr, Ipv6Addr},
num::NonZeroU16,
};

use mdns_sd::{ResolvedService, ScopedIp};

use crate::RustADBError;

/// Represent a device found from mdns search
#[derive(Debug)]
pub struct MDNSDevice {
/// Full device address when resolved
pub fullname: String,
/// Device IP addresses
addresses: HashSet<IpAddr>,
/// Device port
port: NonZeroU16,
}

impl MDNSDevice {
Expand All @@ -22,6 +27,12 @@ impl MDNSDevice {
self.addresses.clone()
}

/// Return the port of this device
#[must_use]
pub fn port(&self) -> NonZeroU16 {
self.port
}

/// Return all IPv4 addresses linked to this device
#[must_use]
pub fn ipv4_addresses(&self) -> HashSet<Ipv4Addr> {
Expand Down Expand Up @@ -49,18 +60,27 @@ impl MDNSDevice {
}
}

impl From<Box<ResolvedService>> for MDNSDevice {
fn from(value: Box<ResolvedService>) -> Self {
Self {
impl TryFrom<Box<ResolvedService>> for MDNSDevice {
type Error = RustADBError;

fn try_from(value: Box<ResolvedService>) -> Result<Self, Self::Error> {
let fullname = value.fullname.clone();
Ok(Self {
fullname: value.fullname,
port: NonZeroU16::new(value.port).ok_or(RustADBError::UnknownDeviceState(format!(
"device {} has a non-u16 port: {}",
fullname.clone(),
value.port
)))?,
addresses: value.addresses.iter().map(ScopedIp::to_ip_addr).collect(),
}
})
}
}

impl Display for MDNSDevice {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Device fullname: {}", self.fullname)?;
writeln!(f, "Device port: {}", self.port)?;
writeln!(f, "IPv4 Addresses: {:?}", self.ipv4_addresses())?;
write!(f, "IPv6 Addresses: {:?}", self.ipv6_addresses())?;

Expand Down
9 changes: 6 additions & 3 deletions adb_client/src/mdns/mdns_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ impl MDNSDiscoveryService {
loop {
while let Ok(event) = receiver.recv() {
if let ServiceEvent::ServiceResolved(service_info) = event {
sender
.send(MDNSDevice::from(service_info))
.map_err(|_| RustADBError::SendError)?;
match MDNSDevice::try_from(service_info) {
Ok(device) => {
sender.send(device).map_err(|_| RustADBError::SendError)?;
}
Err(e) => log::error!("got error with device: {e}"),
}
}
}
}
Expand Down
Loading