DAMN YOU RUST

This commit is contained in:
Joseph Ferano 2018-12-10 20:00:56 -04:00
parent 817e08e89b
commit 14121fed8d
2 changed files with 21 additions and 4 deletions

View File

@ -5,14 +5,15 @@ extern crate serde_json;
extern crate serde_derive; extern crate serde_derive;
use a03::*; use a03::*;
use std::net::{TcpListener, TcpStream, Shutdown}; use std::net::{TcpListener, TcpStream, Shutdown, SocketAddrV4, Ipv4Addr};
use std::borrow::Cow; use std::borrow::Cow;
use std::thread; use std::thread;
use std::io::Read; use std::io::Read;
use std::io::Write; use std::io::Write;
fn main() { fn main() {
let mut stream = TcpStream::connect("localhost:6770").unwrap(); let endpoint = parse_endpoint_from_cli(0);
let mut stream = TcpStream::connect(endpoint).unwrap();
serde_json::to_writer( serde_json::to_writer(
&mut stream, &mut stream,
&Packet { &Packet {
@ -20,12 +21,11 @@ fn main() {
json: None, json: None,
}) })
.unwrap(); .unwrap();
println!("Message sent!");
stream.flush().unwrap(); stream.flush().unwrap();
stream.shutdown(Shutdown::Write).unwrap(); stream.shutdown(Shutdown::Write).unwrap();
let files: FilePaths = serde_json::from_reader(&mut stream).unwrap(); let files: FilePaths = serde_json::from_reader(&mut stream).unwrap();
for path in files.paths.iter() { for path in files.paths.iter() {
println!("Path: {}", path); println!("/home/{}", path);
} }
} }

View File

@ -5,6 +5,11 @@ extern crate serde_derive;
use std::borrow::Cow; use std::borrow::Cow;
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
use std::net::SocketAddrV4;
use std::str::FromStr;
//use std::
const DEFAULT_PORT: &str = "8000";
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub enum PacketType { pub enum PacketType {
@ -81,3 +86,15 @@ pub struct BlockQuery {
pub data_node: DataNode, pub data_node: DataNode,
pub chunk_id: String pub chunk_id: String
} }
pub fn parse_endpoint_from_cli(arg_index : usize) -> String {
let mut args: Vec<String> = std::env::args().skip(1).collect();
let endpoint_arg: String = args.get(arg_index).expect("No IP provided").clone();
if endpoint_arg.contains(":") {
endpoint_arg
} else {
format!("{}:{}", endpoint_arg, DEFAULT_PORT)
}
}