diff options
| author | Yuval Adam <_@yuv.al> | 2018-07-11 15:55:54 +0300 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2018-07-11 15:55:54 +0300 |
| commit | fc67a4d0d3f105d6f2ee82070f79c31130443967 (patch) | |
| tree | 3a47baff618a4b23b10a908ba66052fba7aeec48 /src | |
| parent | 3da5d618b4c9248d74a500b2eba6450ff68590e8 (diff) | |
Initial implementation and test for parse_udp_message
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 4 | ||||
| -rw-r--r-- | src/upnp.rs | 58 |
2 files changed, 62 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs index 5ddb078..42208dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,7 @@ +#[cfg(test)] +#[macro_use] +extern crate indoc; + extern crate clap; extern crate colored; diff --git a/src/upnp.rs b/src/upnp.rs index b42eab3..a4628ab 100644 --- a/src/upnp.rs +++ b/src/upnp.rs @@ -1,6 +1,11 @@ +extern crate regex; + +use self::regex::Regex; +use std::collections::HashMap; 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 @@ -9,16 +14,69 @@ ST: ssdp:all\r\n MX: 3\r\n User-Agent: slingr/0.1.0\r\n\r\n"; +// enum Capabilities { +// RENDERING_CONTROL, +// AV_TRANSPORT, +// CONNECTION_MANAGER +// } + +pub struct Device { + usn: String, + host: String, + xml: String, +} + +const USN_REGEX: &'static str = r"uuid:([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})"; + +fn parse_udp_message(msg: &str) -> Device { + let re = Regex::new(USN_REGEX).unwrap(); + let usn = re.captures(msg).unwrap().get(1).unwrap().as_str().to_string(); + Device { + usn, + host: "192.168.33.44:38400".to_string(), + xml: "http://192.168.33.44:38400/deviceDescription/MediaRenderer".to_string() + } +} + pub fn discover() { println!("Discovering..."); let socket = UdpSocket::bind("[::]:0").unwrap(); socket.send_to(SEARCH_REQUEST.as_bytes(), MULTICAST_ADDRESS).expect("couldn't send data"); + let mut devices = HashMap::new(); + let d = Device {usn: "a".to_string(), host: "b".to_string(), xml: "c".to_string()}; + devices.insert("foo", d); + 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(); + let _d = parse_udp_message(s); println!("Got: {}\n\n", s); } } + +#[cfg(test)] +mod tests { + use upnp::*; + + #[test] + fn exploration() { + let msg = indoc!(r#" + HTTP/1.1 200 OK + Content-Type: text/xml; charset="utf-8" + SERVER: Linux/3.10.0 eHomeMediaCenter/1.0 + Content-Length: 0 + Cache-Control: max-age=1810 + EXT: + Date: Fri, 02 Jan 1970 22:25:35 GMT + ST: urn:schemas-upnp-org:service:ConnectionManager:1 + USN: uuid:9acb38e1-09cc-dba0-ffff-ffffe156cab7::urn:schemas-upnp-org:service:ConnectionManager:1 + Location: http://192.168.33.44:38400/deviceDescription/MediaRenderer"#); + let d = parse_udp_message(msg); + assert_eq!(d.usn, "9acb38e1-09cc-dba0-ffff-ffffe156cab7"); + assert_eq!(d.xml, "http://192.168.33.44:38400/deviceDescription/MediaRenderer"); + assert_eq!(d.host, "192.168.33.44:38400") + } +} |
