Getting proper CLI parsing for copy working, since it's tricky

This commit is contained in:
Joseph Ferano 2018-12-11 15:49:09 -04:00
parent 14121fed8d
commit 8cf792c468
3 changed files with 73 additions and 16 deletions

View File

@ -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();
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: String::from("Somefile"), size: 32 }).unwrap()),
&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<String> = 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 }
}

View File

@ -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<Block> = Vec::new();
// Divide the blocks up into the amount of nodes available
add_blocks_to_inode(&conn, &file.name, &blocks);
let mut nodes: Vec<AvailableNodes> = Vec::new();
for dn in get_data_nodes(&conn) {
nodes.push(AvailableNodes {

View File

@ -16,7 +16,7 @@ pub enum PacketType {
NodeRegistration,
ListFiles,
PutFile,
GetFiles,
GetFile,
AddDataBlocks,
ShutdownDataNode,
Success,