forked from NationTech/harmony
This includes Id, IpAddress, Url and some other heavily used types
35 lines
1.1 KiB
Rust
35 lines
1.1 KiB
Rust
use mdns_sd::{ServiceDaemon, ServiceEvent};
|
|
|
|
use crate::local_presence::SERVICE_NAME;
|
|
|
|
pub type DiscoveryEvent = ServiceEvent;
|
|
|
|
pub fn discover_agents(timeout: Option<u64>, on_event: impl Fn(DiscoveryEvent) + Send + 'static) {
|
|
// Create a new mDNS daemon.
|
|
let mdns = ServiceDaemon::new().expect("Failed to create mDNS daemon");
|
|
|
|
// Start browsing for the service type.
|
|
// The receiver will be a stream of events.
|
|
let receiver = mdns.browse(SERVICE_NAME).expect("Failed to browse");
|
|
|
|
std::thread::spawn(move || {
|
|
while let Ok(event) = receiver.recv() {
|
|
on_event(event.clone());
|
|
match event {
|
|
ServiceEvent::ServiceResolved(resolved) => {
|
|
println!("Resolved a new service: {}", resolved.fullname);
|
|
}
|
|
other_event => {
|
|
println!("Received other event: {:?}", &other_event);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
if let Some(timeout) = timeout {
|
|
// Gracefully shutdown the daemon.
|
|
std::thread::sleep(std::time::Duration::from_secs(timeout));
|
|
mdns.shutdown().unwrap();
|
|
}
|
|
}
|