summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cli.rs2
-rw-r--r--src/upnp.rs43
2 files changed, 40 insertions, 5 deletions
diff --git a/src/cli.rs b/src/cli.rs
index 2251e39..e680a18 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -35,10 +35,8 @@ impl Controller {
}
pub fn read(&mut self) -> u8 {
- print!("Hit a key! ");
self.stdout.lock().flush().unwrap();
self.reader.read_exact(&mut self.buffer).unwrap();
- println!("You have hit: {:?}", self.buffer);
self.buffer[0]
}
diff --git a/src/upnp.rs b/src/upnp.rs
index 556de8b..7a228b6 100644
--- a/src/upnp.rs
+++ b/src/upnp.rs
@@ -1,9 +1,14 @@
+extern crate hyper;
extern crate regex;
use self::regex::Regex;
+use self::hyper::Client;
+use self::hyper::rt::{self, Future, Stream};
use std::collections::HashMap;
use std::net::UdpSocket;
use std::str;
+use std::io::{self, Write};
+
const MULTICAST_ADDRESS: &'static str = "239.255.255.250:1900";
@@ -20,13 +25,14 @@ User-Agent: slingr/0.1.0\r\n\r\n";
// CONNECTION_MANAGER
// }
+#[derive(Debug)]
pub struct Device {
usn: 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})";
-const XML_REGEX: &'static str = r"Location: (http(s?)://.*)";
+const XML_REGEX: &'static str = r"Location: (http(s?)://[\w\.:/]*)";
fn parse_udp_message(msg: &str) -> Device {
let usn_re = Regex::new(USN_REGEX).unwrap();
@@ -39,6 +45,35 @@ fn parse_udp_message(msg: &str) -> Device {
}
}
+fn fetch_url(url: hyper::Uri) -> impl Future<Item=(), Error=()> {
+ let client = Client::new();
+
+ client
+ // Fetch the url...
+ .get(url)
+ // And then, if we get a response back...
+ .and_then(|res| {
+ println!("Response: {}", res.status());
+ println!("Headers: {:#?}", res.headers());
+
+ // The body is a stream, and for_each returns a new Future
+ // when the stream is finished, and calls the closure on
+ // each chunk of the body...
+ res.into_body().for_each(|chunk| {
+ io::stdout().write_all(&chunk)
+ .map_err(|e| panic!("example expects stdout is open, error={}", e))
+ })
+ })
+ // If all good, just tell the user...
+ .map(|_| {
+ println!("\n\nDone.");
+ })
+ // If there was an error, let the user know...
+ .map_err(|err| {
+ eprintln!("Error {}", err);
+ })
+}
+
pub fn discover() {
println!("Discovering...");
let socket = UdpSocket::bind("[::]:0").unwrap();
@@ -53,8 +88,10 @@ pub fn discover() {
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);
+ let d = parse_udp_message(s);
+ println!("Got: {:?}\n\n", d);
+ rt::run(fetch_url(d.xml.parse::<hyper::Uri>().unwrap()));
+ break;
}
}