summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Adam <_@yuv.al>2018-07-11 14:48:09 +0300
committerYuval Adam <_@yuv.al>2018-07-11 14:48:09 +0300
commit3da5d618b4c9248d74a500b2eba6450ff68590e8 (patch)
treefcea91f65521f7a669ba9485dfbf833c60bf642f
parent070801f2ea08e83437d2a50b472a56725deeb947 (diff)
Add UDP comms thread
-rw-r--r--src/main.rs6
-rw-r--r--src/upnp.rs12
2 files changed, 16 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs
index 872a1f7..5ddb078 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -9,6 +9,7 @@ use std::thread;
use std::sync::mpsc;
mod cli;
+mod upnp;
fn main() {
let app = App::new(env!("CARGO_PKG_NAME"))
@@ -42,8 +43,11 @@ fn main() {
process::exit(1);
}
- let (tx, rx) = mpsc::channel();
+ let _udp = thread::spawn(move || {
+ upnp::discover();
+ });
+ let (tx, rx) = mpsc::channel();
let child = thread::spawn(move || {
let mut controller = cli::Controller::init();
loop {
diff --git a/src/upnp.rs b/src/upnp.rs
index f2e4a5a..b42eab3 100644
--- a/src/upnp.rs
+++ b/src/upnp.rs
@@ -1,5 +1,7 @@
use std::net::UdpSocket;
+use std::str;
+const MULTICAST_ADDRESS: &'static str = "239.255.255.250:1900";
const SEARCH_REQUEST: &'static str = "M-SEARCH * HTTP/1.1\r\n
Host: 239.255.255.250:1900\r\n
Man: \"ssdp:discover\"\r\n
@@ -10,5 +12,13 @@ User-Agent: slingr/0.1.0\r\n\r\n";
pub fn discover() {
println!("Discovering...");
let socket = UdpSocket::bind("[::]:0").unwrap();
- socket.send_to(SEARCH_REQUEST.as_bytes(), "239.255.255.250:1900").expect("couldn't send data");
+ socket.send_to(SEARCH_REQUEST.as_bytes(), MULTICAST_ADDRESS).expect("couldn't send data");
+
+ let mut buf = [0; 1024];
+ loop {
+ let (amt, _src) = socket.recv_from(&mut buf).expect("Didn't receive data...");
+ let filled_buf = &mut buf[..amt];
+ let s = str::from_utf8(filled_buf).unwrap();
+ println!("Got: {}\n\n", s);
+ }
}