Skip to content

Commit

Permalink
feat: Improve the mdns support
Browse files Browse the repository at this point in the history
- Do not try to instantiate it if the thing is listening on localhost.
- Do not pass again unspec if that's the ip selected (e.g. 0.0.0.0 on
  ipv4)
- Make the example take the address as argument to make simpler to test
  it.
  • Loading branch information
lu-zero committed May 9, 2024
1 parent ebb9ab0 commit 653aac0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ keywords = ["wot", "WebofThings"]

[dependencies]
wot-td = "0.3.1"
mdns-sd = "0.10.1"
mdns-sd = "0.11.0"
thiserror = "1.0"
if-addrs = "0.11.0"
hostname = "0.3"
hostname = "0.4"
axum = "0.7.2"
serde = "1.0.141"
serde_json = "1.0.83"
Expand Down
8 changes: 6 additions & 2 deletions examples/thing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ impl ExtendableThing for A {

#[tokio::main]
async fn main() {
let addr = std::env::args()
.nth(1)
.unwrap_or_else(|| "0.0.0.0:8080".into());

let servient = Servient::builder("TestThing")
.ext(A {})
.finish_extend()
.http_bind("127.0.0.1:8080".parse().unwrap())
.http_bind(addr.parse().unwrap())
.property("hello", |b| {
b.ext(())
.ext_interaction(())
Expand Down Expand Up @@ -66,7 +70,7 @@ async fn main() {
.build_servient()
.unwrap();

eprintln!("Listening to 127.0.0.1:8080");
eprintln!("Listening to {addr}");
dbg!(&servient.router);

println!("Running the servient for 10 seconds.");
Expand Down
19 changes: 13 additions & 6 deletions src/advertise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ pub struct ServiceBuilder<'a> {
name: String,
}

fn normalize_hostname(mut hostname: String) -> String {
if !hostname.ends_with(".local") {
hostname.push_str(".local");
}
if !hostname.ends_with('.') {
hostname.push('.')
}
hostname
}

impl<'a> ServiceBuilder<'a> {
fn new(ad: &'a Advertiser, name: impl Into<String>) -> ServiceBuilder<'a> {
Self {
Expand Down Expand Up @@ -121,7 +131,7 @@ impl<'a> ServiceBuilder<'a> {

/// The hostname used by the MDNS daemon.
pub fn hostname(mut self, host: impl Into<String>) -> Self {
self.hostname = host.into();
self.hostname = normalize_hostname(host.into());

self
}
Expand Down Expand Up @@ -175,10 +185,7 @@ impl Advertiser {
pub fn new() -> Result<Self> {
let mdns = ServiceDaemon::new()?;

let mut hostname = hostname::get()?.to_string_lossy().to_string();
if !hostname.ends_with(".local") {
hostname.push_str(".local");
}
let hostname = normalize_hostname(hostname::get()?.to_string_lossy().to_string());

let ips = if_addrs::get_if_addrs()?
.iter()
Expand Down Expand Up @@ -224,7 +231,7 @@ mod test {
let props = info.get_properties();
assert_eq!(props.get_property_val_str("td"), Some(WELL_KNOWN));
assert_eq!(props.get_property_val_str("type"), Some("Thing"));
assert_eq!(info.get_hostname(), "testhost.");
assert_eq!(info.get_hostname(), "testhost.local.");
},
);
}
Expand Down
19 changes: 14 additions & 5 deletions src/servient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,20 @@ impl Servient<Nil> {
impl<O: ExtendableThing> Servient<O> {
/// Start a listening server and advertise for it.
pub async fn serve(&self) -> Result<(), Error> {
self.sd
.add_service(&self.name)
.thing_type(self.thing_type)
.port(self.http_addr.port())
.build()?;
let ip = self.http_addr.ip();

if !ip.is_loopback() {
let mut ad = self
.sd
.add_service(&self.name)
.thing_type(self.thing_type)
.port(self.http_addr.port());

if !ip.is_unspecified() {
ad = ad.ips([ip]);
}
ad.build()?;
}

let listener = tokio::net::TcpListener::bind(&self.http_addr)
.await
Expand Down

0 comments on commit 653aac0

Please sign in to comment.