harmony/harmony_inventory_agent/src/local_presence/discover.rs
Jean-Gabriel Gill-Couture 701d8cfab9 feat: automatically discover inventory (#127)
## Fully automated inventory gathering now works!

Boot up harmony_inventory_agent with `cargo run -p harmony_inventory_agent`
Launch the DiscoverInventoryAgentScore , currently available this way :

`RUST_LOG=info cargo run -p example-cli -- -f Discover -y`

And you will have automatically all hosts saved to the database. Run `cargo sqlx setup` if you have not done it yet.

Co-authored-by: Ian Letourneau <ian@noma.to>
Reviewed-on: https://git.nationtech.io/NationTech/harmony/pulls/127
Co-authored-by: Jean-Gabriel Gill-Couture <jg@nationtech.io>
Co-committed-by: Jean-Gabriel Gill-Couture <jg@nationtech.io>
2025-08-31 22:45:07 +00:00

40 lines
1.3 KiB
Rust

use log::{debug, error};
use mdns_sd::{ServiceDaemon, ServiceEvent};
use crate::local_presence::SERVICE_NAME;
pub type DiscoveryEvent = ServiceEvent;
pub async fn discover_agents<F>(timeout: Option<u64>, on_event: F)
where
F: FnOnce(DiscoveryEvent) -> Result<(), String> + Send + 'static + Copy,
{
// 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");
tokio::task::spawn_blocking(move || {
while let Ok(event) = receiver.recv() {
if let Err(e) = on_event(event.clone()) {
error!("Event callback failed : {e}");
}
match event {
ServiceEvent::ServiceResolved(resolved) => {
debug!("Resolved a new service: {}", resolved.fullname);
}
other_event => {
debug!("Received other event: {:?}", &other_event);
}
}
}
});
if let Some(timeout) = timeout {
tokio::time::sleep(std::time::Duration::from_secs(timeout)).await;
mdns.shutdown().unwrap();
}
}