61 lines
1.9 KiB
Rust
61 lines
1.9 KiB
Rust
// harmony-agent/src/main.rs
|
|
|
|
use log::info;
|
|
use mdns_sd::{ServiceDaemon, ServiceInfo};
|
|
use std::collections::HashMap;
|
|
|
|
use crate::SERVICE_TYPE;
|
|
|
|
// The service we are advertising.
|
|
const SERVICE_PORT: u16 = 43210; // A port for the service. It needs one, even if unused.
|
|
|
|
pub async fn advertise() {
|
|
info!("Starting Harmony Agent...");
|
|
|
|
// Get a unique ID for this machine.
|
|
let motherboard_id = "some motherboard id";
|
|
let instance_name = format!("harmony-agent-{}", motherboard_id);
|
|
info!("This agent's instance name: {}", instance_name);
|
|
info!("Advertising with ID: {}", motherboard_id);
|
|
|
|
// Create a new mDNS daemon.
|
|
let mdns = ServiceDaemon::new().expect("Failed to create mDNS daemon");
|
|
|
|
// Create a TXT record HashMap to hold our metadata.
|
|
let mut properties = HashMap::new();
|
|
properties.insert("id".to_string(), motherboard_id.to_string());
|
|
properties.insert("version".to_string(), "1.0".to_string());
|
|
|
|
// Create the service information.
|
|
// The instance name should be unique on the network.
|
|
let local_ip = local_ip_address::local_ip().unwrap();
|
|
let service_info = ServiceInfo::new(
|
|
SERVICE_TYPE,
|
|
&instance_name,
|
|
"harmony-host.local.", // A hostname for the service
|
|
local_ip,
|
|
// "0.0.0.0",
|
|
SERVICE_PORT,
|
|
Some(properties),
|
|
)
|
|
.expect("Failed to create service info");
|
|
|
|
// Register our service with the daemon.
|
|
mdns.register(service_info)
|
|
.expect("Failed to register service");
|
|
|
|
info!(
|
|
"Service '{}' registered and now being advertised.",
|
|
instance_name
|
|
);
|
|
info!("Agent is running. Press Ctrl+C to exit.");
|
|
|
|
for iface in get_if_addrs::get_if_addrs().unwrap() {
|
|
println!("{:#?}", iface);
|
|
}
|
|
|
|
// Keep the agent running indefinitely.
|
|
tokio::signal::ctrl_c().await.unwrap();
|
|
info!("Shutting down agent.");
|
|
}
|