From cada5b7f822b1fba9eddf8ba3d3688ddbfa12d11 Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Sun, 10 Dec 2017 03:10:58 -0700 Subject: [PATCH] Working hyper version --- .cargo/config | 4 +- Cargo.toml | 1 + src/lib.rs | 223 +++++++++++++++++++++++++++++++++++++++++-------- src/lib_old.rs | 154 ---------------------------------- src/main.rs | 12 +-- 5 files changed, 197 insertions(+), 197 deletions(-) delete mode 100644 src/lib_old.rs diff --git a/.cargo/config b/.cargo/config index 2dc0b72..87a55ea 100644 --- a/.cargo/config +++ b/.cargo/config @@ -3,5 +3,5 @@ linker = "/usr/bin/x86_64-w64-mingw32-gcc" ar = "/usr/x86_64-w64-mingw32/bin/ar" [target.x86_64-apple-darwin] -linker = "x86_64-apple-darwin15-clang" -ar = "x86_64-apple-darwin15-ar" +linker = "/opt/osxcross/bin/x86_64-apple-darwin15-clang" +ar = "/opt/osxcross/bin/x86_64-apple-darwin15-ar" diff --git a/Cargo.toml b/Cargo.toml index 0acdd2f..3f1e039 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,4 @@ futures = "*" hyper = "*" tokio-core = "*" walkdir = "*" +uuid = { version = "*", features = ["v4"] } diff --git a/src/lib.rs b/src/lib.rs index c71f619..932effb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,82 +1,235 @@ -extern crate walkdir; extern crate futures; extern crate hyper; extern crate tokio_core; +extern crate uuid; +extern crate walkdir; +use hyper::header::{ContentLength, ContentType}; +use tokio_core::reactor::{Core, Timeout}; +use hyper::{Client, Method, Request}; +use std::io::{stdout, Read, Write}; +use walkdir::{DirEntry, WalkDir}; use futures::{Future, Stream}; -use tokio_core::reactor::Core; -use std::io::{stdout, Write}; +use futures::future::Either; use hyper::error::Error; +use std::time::Duration; use std::result::Result; -use hyper::Client; +use std::process; +use uuid::Uuid; use std::fs; static HOST: &'static str = "http://192.168.49.1:8080"; const TIMEOUT: u64 = 3; -pub fn up(src: &std::path::Path) -> Result<(),Error> { - unimplemented!(); +pub fn up(src: &std::path::Path) -> Result<(), Error> { + conn_test()?; + + let mut core = Core::new()?; + let robot_controller = Client::new(&core.handle()); + + fn is_src_file(entry: &DirEntry) -> bool { + entry.file_name().to_str().unwrap().contains(".java") + } + + let src_tree = WalkDir::new(&src) + .into_iter() + .filter_map(|f| f.ok()) + .filter(is_src_file); + + for file in src_tree.map(|f| f.path().to_owned()) { + print!("Pushing {}...", file.display()); + stdout().flush()?; + + let mut file_data = String::new(); + fs::File::open(&file)?.read_to_string(&mut file_data)?; + + let boundary = Uuid::new_v4().simple().to_string(); + + let mut upload_request = Request::new( + Method::Post, + (HOST.to_string() + "/java/file/upload").parse()?, + ); + + let body = format!( + "--{}\nContent-Disposition: form-data; name=\"file\"; \ + filename=\"{}\"\nContent-Type: text/x-java\n\n{}\n\n--{}--", + boundary, + &file.file_name().unwrap().to_str().unwrap(), + file_data, + boundary + ); + + upload_request.headers_mut().set_raw( + "Content-Type", + "multipart/form-data; boundary=".to_string() + &boundary, + ); + + upload_request + .headers_mut() + .set(ContentLength(body.len() as u64)); + + upload_request.set_body(body); + + let upload_request = robot_controller.request(upload_request); + core.run(upload_request)?; + + println!("done"); + } + + Ok(()) } pub fn down(dest: &std::path::Path) -> Result<(), hyper::Error> { - //conn_test(); + conn_test()?; + let mut core = Core::new()?; let robot_controller = Client::new(&core.handle()); - let mut tree = String::new(); - let tree_request = robot_controller .get((HOST.to_string() + "/java/file/tree").parse()?) - .and_then(|res| res.body() - .concat2() - .map(|chunk| - String::from_utf8(chunk.to_vec()).unwrap() - ) - ); + .and_then(|res| { + res.body() + .concat2() + .map(|chunk| String::from_utf8(chunk.to_vec()).unwrap()) + }); - tree = core.run(tree_request)?; + let tree = core.run(tree_request)?; for file in tree.split("\"").filter(|file| file.contains(".java")) { print!("Pulling {}...", file); stdout().flush()?; + let filepath = dest.join(&file[1..]); fs::DirBuilder::new() .recursive(true) .create(filepath.parent().unwrap())?; + let mut file_handle = fs::File::create(&filepath)?; + let download_request = robot_controller .get((HOST.to_string() + "/java/file/download?f=/src" + file).parse()?) - .and_then(|res| res.body() - .for_each(|chunk| file_handle.write_all(&chunk).map_err(From::from)) - ); + .and_then(|res| { + res.body() + .for_each(|chunk| file_handle.write_all(&chunk).map_err(From::from)) + }); + core.run(download_request)?; + println!("done"); } Ok(()) } -pub fn build() -> Result<(),Error> { - unimplemented!(); +pub fn build() -> Result<(), Error> { + conn_test()?; + + let mut core = Core::new()?; + let robot_controller = Client::new(&core.handle()); + + let tree_request = robot_controller.get((HOST.to_string() + "/java/file/tree").parse()?); + + let build_request = robot_controller.get((HOST.to_string() + "/java/build/start").parse()?); + + core.run(tree_request.join(build_request))?; + + print!("Building..."); + stdout().flush()?; + + let mut status; + + loop { + let status_request = robot_controller + .get((HOST.to_string() + "/java/build/status").parse()?) + .and_then(|res| { + res.body() + .concat2() + .map(|chunk| String::from_utf8(chunk.to_vec()).unwrap()) + }); + + status = core.run(status_request)?; + + if status.contains("\"completed\": true") { + break; + } else { + print!("."); + stdout().flush()?; + } + } + + if status.contains("\"successful\": true") { + println!("BUILD SUCCESSFUL"); + } else { + println!("BUILD FAILED"); + + let error_request = robot_controller + .get((HOST.to_string() + "/java/build/wait").parse()?) + .and_then(|res| { + res.body() + .for_each(|chunk| std::io::stdout().write_all(&chunk).map_err(From::from)) + }); + + core.run(error_request)?; + } + + Ok(()) } -pub fn wipe() -> Result<(),Error> { - /*conn_test(); +pub fn wipe() -> Result<(), Error> { + conn_test()?; + let mut core = Core::new()?; - let phone = Client::new(&core.handle()); - let uri = - &(HOST.to_string() + "/java/file/delete") - .parse() - .expect("Parsing URI failed"); + let robot_controller = Client::new(&core.handle()); print!("Wiping all remote files..."); - stdout().flush().unwrap(); - let post = Request::new(Method.Post, uri) - .set_body("delete=[\"src\"]") - println!("done");*/ - unimplemented!(); + stdout().flush()?; + + let mut wipe_request = Request::new( + Method::Post, + (HOST.to_string() + "/java/file/delete").parse()?, + ); + + let body = "delete=[\"src\"]"; + + wipe_request + .headers_mut() + .set(ContentLength(body.len() as u64)); + wipe_request + .headers_mut() + .set(ContentType::form_url_encoded()); + + wipe_request.set_body(body); + + let wipe_request = robot_controller.request(wipe_request); + core.run(wipe_request)?; + + println!("done"); + + Ok(()) } -fn conn_test() { - unimplemented!(); +fn conn_test() -> Result<(), Error> { + let mut core = Core::new()?; + let robot_controller = Client::new(&core.handle()); + + let timeout = Timeout::new(Duration::from_secs(TIMEOUT), &core.handle())?; + + let conn_request = robot_controller + .get(HOST.parse()?) + .select2(timeout) + .map(|res| match res { + Either::B(_) => { + println!( + "Failed to reach the robot controller. Please check that your robot controller\n\ + is in \"Program & Manage\" mode and that your computer is connected to the\n\ + robot controller via wifi-direct." + ); + process::exit(1) + } + _ => (), + }); + + core.run(conn_request).unwrap_or_default(); + + Ok(()) } diff --git a/src/lib_old.rs b/src/lib_old.rs deleted file mode 100644 index 337718e..0000000 --- a/src/lib_old.rs +++ /dev/null @@ -1,154 +0,0 @@ -extern crate reqwest; -extern crate walkdir; - -use std::io::{stdout, Read, Write}; -use walkdir::{DirEntry, WalkDir}; -use std::time::Duration; -use std::process; -use reqwest::*; -use std::fs; - -static HOST: &'static str = "http://192.168.49.1:8080"; -const TIMEOUT: u64 = 3; - -pub fn up(src: &std::path::Path) { - conn_test(); - let phone = Client::new(); - - fn is_src_file(entry: &DirEntry) -> bool { - entry.file_name().to_str().unwrap().contains(".java") - } - - let src_tree = WalkDir::new(&src) - .into_iter() - .filter_map(|f| f.ok()) - .filter(is_src_file); - - for file in src_tree.map(|f| f.path().to_owned()) { - print!("Pushing {}...", file.display()); - stdout().flush().unwrap(); - let upload = multipart::Form::new() - .file("file", file) - .expect("Failed to open file for uploading."); - phone - .post(&(HOST.to_string() + "/java/file/upload")) - .multipart(upload) - .send() - .expect("HTTP request failed"); - println!("done"); - } -} - -pub fn down(dest: &std::path::Path) { - conn_test(); - let phone = Client::new(); - - let mut tree = String::new(); - - phone - .get(&(HOST.to_string() + "/java/file/tree")) - .send() - .expect("HTTP request failed") - .read_to_string(&mut tree) - .expect("Couldn't read HTTP response"); - - for file in tree.split("\"").filter(|file| file.contains(".java")) { - print!("Pulling {}...", file); - stdout().flush().unwrap(); - let filepath = dest.join(&file[1..]); - fs::DirBuilder::new() - .recursive(true) - .create(filepath.parent().unwrap()) - .expect("Creating a new directory failed"); - let mut file_handle = fs::File::create(&filepath).expect("Creating a new file failed"); - let mut file_data = String::new(); - phone - .get(&(HOST.to_string() + "/java/file/download?f=/src" + file)) - .send() - .expect("HTTP request failed") - .read_to_string(&mut file_data) - .expect("Couldn't read HTTP response"); - file_handle - .write_all(file_data.as_bytes()) - .expect("Writing to file failed"); - println!("done"); - } -} - -pub fn build() { - conn_test(); - let phone = Client::new(); - - phone - .get(&(HOST.to_string() + "/java/file/tree")) - .send() - .expect("HTTP request failed"); - phone - .get(&(HOST.to_string() + "/java/build/start")) - .send() - .expect("HTTP request failed"); - - print!("Building..."); - stdout().flush().unwrap(); - let mut status = String::new(); - - loop { - phone - .get(&(HOST.to_string() + "/java/build/status")) - .send() - .expect("HTTP request failed") - .read_to_string(&mut status) - .expect("Couldn't read HTTP response"); - - if status.contains("\"completed\": true") { - break; - } else { - print!("."); - stdout().flush().unwrap(); - } - } - - if status.contains("\"successful\": true") { - println!("BUILD SUCCESSFUL"); - } else { - println!("BUILD FAILED"); - let mut error = String::new(); - phone - .get(&(HOST.to_string() + "/java/build/wait")) - .send() - .expect("HTTP request failed") - .read_to_string(&mut error) - .expect("Couldn't read HTTP response"); - print!("{}", error); - } -} - -pub fn wipe() { - conn_test(); - let phone = Client::new(); - - print!("Wiping all remote files..."); - stdout().flush().unwrap(); - phone - .post(&(HOST.to_string() + "/java/file/delete")) - .form(&[("delete", "[\"src\"]")]) - .send() - .expect("HTTP request failed"); - println!("done"); -} - -fn conn_test() { - let phone = Client::builder() - .timeout(Duration::from_secs(TIMEOUT)) - .build() - .unwrap(); - - phone.get(HOST).send().unwrap_or_else(|_| { - println!( - "Failed to reach the robot controller. Please check that your robot controller\n\ - is in \"Program & Manage\" mode and that your computer is connected to the\n\ - robot controller via wifi-direct." - ); - process::exit(1) - }); -} diff --git a/src/main.rs b/src/main.rs index f5febf4..2a86710 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,13 +41,13 @@ fn main() { }); } 'b' => ftc_http::build().unwrap_or_else(|_| { - println!("Failed to start build on the robot controller"); - process::exit(0); - }), + println!("Failed to start build on the robot controller"); + process::exit(0); + }), 'w' => ftc_http::wipe().unwrap_or_else(|_| { - println!("Failed to wipe files on the robot controller"); - process::exit(0); - }), + println!("Failed to wipe files on the robot controller"); + process::exit(0); + }), 'v' => { version(); process::exit(0);