feat(inventory agent): Local presence advertisement and discovery now works! Must be within the same LAN to share the multicast address though

This commit is contained in:
2025-08-29 11:22:44 -04:00
parent b857412151
commit 05e7b8075c
10 changed files with 154 additions and 30 deletions

View File

@@ -12,4 +12,6 @@ log.workspace = true
env_logger.workspace = true
tokio.workspace = true
thiserror.workspace = true
mdns-sd = "0.14.1"
# mdns-sd = "0.14.1"
mdns-sd = { git = "https://github.com/jggc/mdns-sd.git", branch = "patch-1" }
local-ip-address = "0.6.5"

View File

@@ -1,2 +1,2 @@
pub mod local_presence;
mod hwinfo;
pub mod local_presence;

View File

@@ -1,8 +1,11 @@
use log::{error, info, warn};
use log::{debug, error, info, warn};
use mdns_sd::{ServiceDaemon, ServiceInfo};
use std::collections::HashMap;
use crate::{hwinfo::PhysicalHost, local_presence::{PresenceError, SERVICE_NAME, VERSION}};
use crate::{
hwinfo::PhysicalHost,
local_presence::{PresenceError, SERVICE_NAME, VERSION},
};
/// Advertises the agent's presence on the local network.
///
@@ -17,7 +20,10 @@ pub fn advertise(service_port: u16) -> Result<(), PresenceError> {
}
};
let instance_name = format!("inventory-agent-{}", host_id.clone().unwrap_or("unknown".to_string()));
let instance_name = format!(
"inventory-agent-{}",
host_id.clone().unwrap_or("unknown".to_string())
);
let spawned_msg = format!("Spawned local presence advertisement task for '{instance_name}'.");
@@ -43,11 +49,23 @@ pub fn advertise(service_port: u16) -> Result<(), PresenceError> {
}
props.insert("version".to_string(), VERSION.to_string());
let local_ip: Box<dyn mdns_sd::AsIpAddrs> = match local_ip_address::local_ip() {
Ok(ip) => Box::new(ip),
Err(e) => {
error!(
"Could not figure out local ip, mdns will have to try to figure it out by itself : {e}"
);
Box::new(())
}
};
debug!("Using local ip {:?}", local_ip.as_ip_addrs());
let service_info = ServiceInfo::new(
SERVICE_NAME,
&instance_name,
&format!("{}.local.", instance_name),
(), // Let the daemon determine the host IPs
local_ip,
service_port,
Some(props),
)
@@ -64,7 +82,6 @@ pub fn advertise(service_port: u16) -> Result<(), PresenceError> {
return;
}
};
});
info!("{spawned_msg}");

View File

@@ -16,7 +16,7 @@ pub fn discover_agents(timeout: Option<u64>, on_event: fn(&DiscoveryEvent)) {
while let Ok(event) = receiver.recv() {
on_event(&event);
match event {
ServiceEvent::ServiceData(resolved) => {
ServiceEvent::ServiceResolved(resolved) => {
println!("Resolved a new service: {}", resolved.fullname);
}
other_event => {
@@ -27,8 +27,8 @@ pub fn discover_agents(timeout: Option<u64>, on_event: fn(&DiscoveryEvent)) {
});
if let Some(timeout) = timeout {
// Gracefully shutdown the daemon.
std::thread::sleep(std::time::Duration::from_secs(timeout));
mdns.shutdown().unwrap();
// Gracefully shutdown the daemon.
std::thread::sleep(std::time::Duration::from_secs(timeout));
mdns.shutdown().unwrap();
}
}

View File

@@ -8,9 +8,6 @@ use crate::hwinfo::PhysicalHost;
mod hwinfo;
mod local_presence;
#[get("/inventory")]
async fn inventory() -> impl Responder {
log::info!("Received inventory request");
@@ -32,7 +29,9 @@ async fn main() -> std::io::Result<()> {
env_logger::init();
let port = env::var("HARMONY_INVENTORY_AGENT_PORT").unwrap_or_else(|_| "8080".to_string());
let port = port.parse::<u16>().expect(&format!("Invalid port number, cannot parse to u16 {port}"));
let port = port
.parse::<u16>()
.expect(&format!("Invalid port number, cannot parse to u16 {port}"));
let bind_addr = format!("0.0.0.0:{}", port);
log::info!("Starting inventory agent on {}", bind_addr);