From 8cf792c46855d488272abc7a1e25f0de88845f2c Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Tue, 11 Dec 2018 15:49:09 -0400 Subject: [PATCH] Getting proper CLI parsing for copy working, since it's tricky --- src/bin/copy.rs | 84 ++++++++++++++++++++++++++++++++++++-------- src/bin/meta_data.rs | 3 ++ src/lib.rs | 2 +- 3 files changed, 73 insertions(+), 16 deletions(-) diff --git a/src/bin/copy.rs b/src/bin/copy.rs index 7f9672a..238cec3 100644 --- a/src/bin/copy.rs +++ b/src/bin/copy.rs @@ -5,23 +5,32 @@ extern crate serde_json; extern crate serde_derive; use a03::*; -use std::net::{TcpListener, TcpStream, Shutdown}; -use std::thread; -use std::io::Read; +use std::net::{TcpStream, Shutdown}; use std::io::Write; -use std::fs::File; use std::fs; fn main() { - let mut stream = TcpStream::connect("localhost:6770").unwrap(); - serde_json::to_writer( - &mut stream, - &Packet { - p_type: PacketType::PutFile, - json: Some(serde_json::to_string( - &PutFile { name: String::from("Somefile"), size: 32 }).unwrap()), - }) - .unwrap(); + let args = get_cli_args(); + let mut stream = TcpStream::connect(args.endpoint).unwrap(); + if args.is_copy_to_dfs { + serde_json::to_writer( + &mut stream, + &Packet { + p_type: PacketType::GetFile, + json: Some(serde_json::to_string( + &PutFile { name: args.filepath, size: 32 }).unwrap()), + }) + .unwrap(); + } else { + serde_json::to_writer( + &mut stream, + &Packet { + p_type: PacketType::PutFile, + json: Some(serde_json::to_string( + &PutFile { name: args.filepath, size: 32 }).unwrap()), + }) + .unwrap(); + } println!("Sent file"); stream.flush().unwrap(); stream.shutdown(Shutdown::Write).unwrap(); @@ -31,10 +40,55 @@ fn main() { println!("Chunk ID: {}", f.chunk_id); } -// std::process::exit(0); -// let mut file = fs::read("/home/joe/Downloads/ideaIU-2018.3.tar.gz").unwrap(); + let mut file = fs::read(args.filename).expect("File not found!"); + println!("{} bytes", file.len()); // let mut stream = TcpStream::connect("localhost:6771").unwrap(); // stream.write(&file).unwrap(); // stream.flush().unwrap(); // stream.shutdown(Shutdown::Write).unwrap(); } + +#[derive(Debug)] +pub struct CliArgs { + pub endpoint: String, + pub filepath: String, + pub filename: String, + pub is_copy_to_dfs: bool, +} + +pub fn get_cli_args() -> CliArgs { + let mut args: Vec = std::env::args().skip(1).collect(); + if args.len() < 2 { + panic!("Requires 2 arguments; IP:PORT:FILEPATH and a Local filename/filepath") + } + let endpoint_arg: String = args.get(0).unwrap().clone(); + + let endpoint; + let filepath; + let filename; + let is_copy_to_dfs; + + if endpoint_arg.contains(":") { + let splits: Vec<&str> = endpoint_arg.split(':').collect(); + if splits.len() < 3 { + panic!("Incorrect endpoint argument format! Please provide IP:PORT:FILE"); + } + endpoint = format!("{}:{}", splits[0], splits[1]); + filepath = String::from(splits[2]); + filename = args.get(1).unwrap().clone(); + is_copy_to_dfs = true; + } else { + let endpoint_arg: String = args.get(1).unwrap().clone(); + let splits: Vec<&str> = endpoint_arg.split(':').collect(); + if splits.len() < 3 { + panic!("Incorrect endpoint argument format! Please provide IP:PORT:FILE"); + } + endpoint = format!("{}:{}", splits[0], splits[1]); + filepath = String::from(splits[2]); + filename = args.get(0).unwrap().clone(); + is_copy_to_dfs = false; + } + + CliArgs { endpoint, filepath, filename, is_copy_to_dfs } +} + diff --git a/src/bin/meta_data.rs b/src/bin/meta_data.rs index c51cc90..f0b0df9 100644 --- a/src/bin/meta_data.rs +++ b/src/bin/meta_data.rs @@ -37,6 +37,9 @@ fn main() { fn put_file(stream: &mut TcpStream, conn: &Connection, message: &str) { let file : PutFile = serde_json::from_str(message).unwrap(); add_file(&conn, &file.name, file.size as i32); + let mut blocks: Vec = Vec::new(); + // Divide the blocks up into the amount of nodes available + add_blocks_to_inode(&conn, &file.name, &blocks); let mut nodes: Vec = Vec::new(); for dn in get_data_nodes(&conn) { nodes.push(AvailableNodes { diff --git a/src/lib.rs b/src/lib.rs index bd6a7ae..93e15c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ pub enum PacketType { NodeRegistration, ListFiles, PutFile, - GetFiles, + GetFile, AddDataBlocks, ShutdownDataNode, Success,