-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add mdns devices discovery (#54)
* feat: add mdns devices discovery --------- Co-authored-by: Jinke <164604729+JinkeJ@users.noreply.github.com>
- Loading branch information
Showing
30 changed files
with
652 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use std::{collections::HashSet, net::IpAddr}; | ||
|
||
/// Represent a device found from mdns search | ||
#[derive(Debug)] | ||
pub struct MDNSDevice { | ||
/// Full device address when resolved | ||
pub fullname: String, | ||
/// Device IP addresses | ||
pub addresses: HashSet<IpAddr>, | ||
} | ||
|
||
impl From<mdns_sd::ServiceInfo> for MDNSDevice { | ||
fn from(value: mdns_sd::ServiceInfo) -> Self { | ||
Self { | ||
fullname: value.get_fullname().to_string(), | ||
addresses: value.get_addresses().to_owned(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use mdns_sd::{ServiceDaemon, ServiceEvent}; | ||
use std::{sync::mpsc::Sender, thread::JoinHandle}; | ||
|
||
use crate::{MDNSDevice, Result, RustADBError}; | ||
|
||
const ADB_SERVICE_NAME: &str = "_adb-tls-connect._tcp.local."; | ||
|
||
/// Structure holding responsibility over mdns discovery | ||
pub struct MDNSDiscoveryService { | ||
daemon: ServiceDaemon, | ||
thread_handle: Option<JoinHandle<Result<()>>>, | ||
} | ||
|
||
impl std::fmt::Debug for MDNSDiscoveryService { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("MDNSDiscoveryService") | ||
.field("daemon", &self.daemon.get_metrics()) | ||
.field("handle", &self.thread_handle) | ||
.finish() | ||
} | ||
} | ||
|
||
impl MDNSDiscoveryService { | ||
/// Instantiate a new discovery service to find devices over mdns | ||
pub fn new() -> Result<Self> { | ||
Ok(MDNSDiscoveryService { | ||
daemon: ServiceDaemon::new()?, | ||
thread_handle: None, | ||
}) | ||
} | ||
|
||
/// Start discovery by spawning a new thread responsible of getting events. | ||
pub fn start(&mut self, sender: Sender<MDNSDevice>) -> Result<()> { | ||
let receiver = self.daemon.browse(ADB_SERVICE_NAME)?; | ||
|
||
let handle: JoinHandle<Result<()>> = std::thread::spawn(move || loop { | ||
while let Ok(event) = receiver.recv() { | ||
match event { | ||
ServiceEvent::SearchStarted(_) | ||
| ServiceEvent::ServiceRemoved(_, _) | ||
| ServiceEvent::ServiceFound(_, _) | ||
| ServiceEvent::SearchStopped(_) => { | ||
// Ignoring these events. We are only interesting in found devices | ||
continue; | ||
} | ||
ServiceEvent::ServiceResolved(service_info) => { | ||
if let Err(e) = sender.send(MDNSDevice::from(service_info)) { | ||
return Err(e.into()); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
self.thread_handle = Some(handle); | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Shutdown discovery engines. | ||
pub fn shutdown(&mut self) -> Result<()> { | ||
match self.daemon.shutdown() { | ||
Ok(_) => Ok(()), | ||
Err(e) => match e { | ||
mdns_sd::Error::Again => { | ||
self.daemon.shutdown()?; | ||
Ok(()) | ||
} | ||
e => Err(RustADBError::MDNSError(e)), | ||
}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod mdns_device; | ||
mod mdns_discovery; | ||
|
||
pub use mdns_device::MDNSDevice; | ||
pub use mdns_discovery::MDNSDiscoveryService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.