forked from dylanmckay/mdns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dylan McKay
committed
Nov 27, 2016
1 parent
60b5160
commit acab2e5
Showing
4 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
extern crate mdns; | ||
|
||
use std::time::Duration; | ||
|
||
fn main() { | ||
let duration = Duration::from_secs(5); | ||
|
||
mdns::discover("_googlecast._tcp.local", Some(duration), |response| { | ||
let addresses = response.records().filter_map(|record| { | ||
if let mdns::RecordKind::A(addr) = record.kind { Some(addr) } else { None } | ||
}); | ||
|
||
for address in addresses { | ||
println!("found Chromecast on {}", address); | ||
} | ||
}).expect("error while performing Chromecast discovery"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use {mDNS, Error, Response}; | ||
use std::time::{SystemTime, Duration}; | ||
|
||
use io; | ||
|
||
pub fn discover<F>(service_name: &str, | ||
duration: Option<Duration>, | ||
mut f: F) -> Result<(), Error> | ||
where F: FnMut(Response) { | ||
let mut io = io::Io::new()?; | ||
|
||
let mut mdns = mDNS::new(service_name)?; | ||
io.register(&mut mdns)?; | ||
|
||
let finish_at = duration.map(|duration| SystemTime::now() + duration); | ||
|
||
loop { | ||
let poll_timeout = finish_at.map(|finish_at| { | ||
finish_at.duration_since(SystemTime::now()).unwrap() | ||
}); | ||
|
||
io.poll(&mut mdns, poll_timeout)?; | ||
|
||
for response in mdns.responses() { | ||
f(response) | ||
} | ||
|
||
if let Some(finish_at) = finish_at { | ||
if SystemTime::now() >= finish_at { | ||
break; | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters