All checks were successful
Run Check Script / check (pull_request) Successful in 59s
Co-authored-by: Jean-Gabriel Gill-Couture <jeangabriel.gc@gmail.com> Co-authored-by: Ian Letourneau <ian@noma.to> Reviewed-on: https://git.nationtech.io/NationTech/harmony/pulls/130 Reviewed-by: Ian Letourneau <ian@noma.to> Co-authored-by: Jean-Gabriel Gill-Couture <jg@nationtech.io> Co-committed-by: Jean-Gabriel Gill-Couture <jg@nationtech.io>
110 lines
4.3 KiB
Rust
110 lines
4.3 KiB
Rust
use mdns_sd::{ServiceDaemon, ServiceEvent};
|
|
|
|
use crate::SERVICE_TYPE;
|
|
|
|
pub async fn discover() {
|
|
println!("Starting Harmony Master and browsing for agents...");
|
|
|
|
// 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_TYPE).expect("Failed to browse");
|
|
|
|
println!(
|
|
"Listening for mDNS events for '{}'. Press Ctrl+C to exit.",
|
|
SERVICE_TYPE
|
|
);
|
|
|
|
std::thread::spawn(move || {
|
|
while let Ok(event) = receiver.recv() {
|
|
match event {
|
|
ServiceEvent::ServiceData(resolved) => {
|
|
println!("Resolved a new service: {}", resolved.fullname);
|
|
}
|
|
other_event => {
|
|
println!("Received other event: {:?}", &other_event);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
// Gracefully shutdown the daemon.
|
|
std::thread::sleep(std::time::Duration::from_secs(1000000));
|
|
mdns.shutdown().unwrap();
|
|
|
|
// Process events as they come in.
|
|
// while let Ok(event) = receiver.recv_async().await {
|
|
// debug!("Received event {event:?}");
|
|
// // match event {
|
|
// // ServiceEvent::ServiceFound(svc_type, fullname) => {
|
|
// // println!("\n--- Agent Discovered ---");
|
|
// // println!(" Service Name: {}", fullname());
|
|
// // // You can now resolve this service to get its IP, port, and TXT records
|
|
// // // The resolve operation is a separate network call.
|
|
// // let receiver = mdns.browse(info.get_fullname()).unwrap();
|
|
// // if let Ok(resolve_event) = receiver.recv_timeout(Duration::from_secs(2)) {
|
|
// // if let ServiceEvent::ServiceResolved(info) = resolve_event {
|
|
// // let ip = info.get_addresses().iter().next().unwrap();
|
|
// // let port = info.get_port();
|
|
// // let motherboard_id = info.get_property("id").map_or("N/A", |v| v.val_str());
|
|
// //
|
|
// // println!(" IP: {}:{}", ip, port);
|
|
// // println!(" Motherboard ID: {}", motherboard_id);
|
|
// // println!("------------------------");
|
|
// //
|
|
// // // TODO: Add this agent to your central list of discovered hosts.
|
|
// // }
|
|
// // } else {
|
|
// // println!("Could not resolve service '{}' in time.", info.get_fullname());
|
|
// // }
|
|
// // }
|
|
// // ServiceEvent::ServiceRemoved(info) => {
|
|
// // println!("\n--- Agent Removed ---");
|
|
// // println!(" Service Name: {}", info.get_fullname());
|
|
// // println!("---------------------");
|
|
// // // TODO: Remove this agent from your list.
|
|
// // }
|
|
// // _ => {
|
|
// // // We don't care about other event types for this example
|
|
// // }
|
|
// // }
|
|
// }
|
|
}
|
|
|
|
async fn _discover_example() {
|
|
use mdns_sd::{ServiceDaemon, ServiceEvent};
|
|
|
|
// Create a daemon
|
|
let mdns = ServiceDaemon::new().expect("Failed to create daemon");
|
|
|
|
// Use recently added `ServiceEvent::ServiceData`.
|
|
mdns.use_service_data(true)
|
|
.expect("Failed to use ServiceData");
|
|
|
|
// Browse for a service type.
|
|
let service_type = "_mdns-sd-my-test._udp.local.";
|
|
let receiver = mdns.browse(service_type).expect("Failed to browse");
|
|
|
|
// Receive the browse events in sync or async. Here is
|
|
// an example of using a thread. Users can call `receiver.recv_async().await`
|
|
// if running in async environment.
|
|
std::thread::spawn(move || {
|
|
while let Ok(event) = receiver.recv() {
|
|
match event {
|
|
ServiceEvent::ServiceData(resolved) => {
|
|
println!("Resolved a new service: {}", resolved.fullname);
|
|
}
|
|
other_event => {
|
|
println!("Received other event: {:?}", &other_event);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
// Gracefully shutdown the daemon.
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
|
mdns.shutdown().unwrap();
|
|
}
|