Got the packet to correctly send data from copy to data_node
This commit is contained in:
parent
30d68cd0ee
commit
d3ebfd9638
@ -6,12 +6,13 @@ extern crate serde_derive;
|
|||||||
|
|
||||||
use a03::*;
|
use a03::*;
|
||||||
use std::net::{TcpStream, Shutdown};
|
use std::net::{TcpStream, Shutdown};
|
||||||
use std::io::Write;
|
use std::io::{Write, Read};
|
||||||
|
use std::fs::File;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = get_cli_args();
|
let args = get_cli_args();
|
||||||
let file = fs::read(args.filename).expect("File not found!");
|
let file = fs::read(&args.filename).expect("File not found!");
|
||||||
let size = file.len();
|
let size = file.len();
|
||||||
let mut stream = TcpStream::connect(args.endpoint).unwrap();
|
let mut stream = TcpStream::connect(args.endpoint).unwrap();
|
||||||
let packet_type;
|
let packet_type;
|
||||||
@ -20,46 +21,55 @@ fn main() {
|
|||||||
packet_type = PacketType::RequestWrite;
|
packet_type = PacketType::RequestWrite;
|
||||||
println!("Requesting Write of {}", args.filepath);
|
println!("Requesting Write of {}", args.filepath);
|
||||||
json = Some(serde_json::to_string(
|
json = Some(serde_json::to_string(
|
||||||
&PutFile { name: args.filepath, size: size as u32, }).unwrap())
|
&AddFile { name: args.filepath, size: size as u32, }).unwrap())
|
||||||
} else {
|
} else {
|
||||||
packet_type = PacketType::RequestRead;
|
packet_type = PacketType::RequestRead;
|
||||||
println!("Requesting Read of {}", args.filepath);
|
println!("Requesting Read of {}", args.filepath);
|
||||||
json = Some(serde_json::to_string(
|
json = Some(serde_json::to_string::<String>(&args.filepath).unwrap())
|
||||||
&GetFile { name: args.filepath, }).unwrap())
|
|
||||||
}
|
}
|
||||||
serde_json::to_writer( &mut stream, &Packet { p_type: packet_type, json, })
|
serde_json::to_writer( &mut stream, &Packet { p_type: packet_type, json, data: None, })
|
||||||
.unwrap();
|
.unwrap();
|
||||||
stream.flush().unwrap();
|
stream.flush().unwrap();
|
||||||
stream.shutdown(Shutdown::Write).unwrap();
|
stream.shutdown(Shutdown::Write).unwrap();
|
||||||
|
|
||||||
|
let mut nodes: Option<Vec<AvailableNodes>> = None;
|
||||||
match serde_json::from_reader(&mut stream) {
|
match serde_json::from_reader(&mut stream) {
|
||||||
Ok(packet @ Packet { .. }) => match packet.p_type {
|
Ok(Packet { p_type: PacketType::Success, json, .. }) =>
|
||||||
PacketType::Success => {
|
nodes = Some(serde_json::from_str::<Vec<AvailableNodes>>(&json.unwrap())
|
||||||
let nodes = serde_json::from_str::<Vec<AvailableNodes>>(&packet.json.unwrap())
|
.unwrap()),
|
||||||
.unwrap();
|
Ok(Packet { p_type: PacketType::Error, json, .. }) => {
|
||||||
for node in nodes {
|
eprintln!("Meta Data Server Error: {}", &json.unwrap());
|
||||||
println!("{}", node.chunk_id);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
PacketType::Error => {
|
|
||||||
let unwrapped = &packet.json.unwrap();
|
|
||||||
panic!("Meta Data Server Error: {}", unwrapped);
|
|
||||||
},
|
|
||||||
_ => (),
|
|
||||||
},
|
},
|
||||||
Err(e) => println!("Error parsing json {}", e.to_string()),
|
Ok(_) => {},
|
||||||
|
Err(e) => eprintln!("Error parsing json {}", e.to_string()),
|
||||||
};
|
};
|
||||||
|
let filename = &args.filename;
|
||||||
|
if args.is_copy_to_dfs {
|
||||||
|
nodes.map(|ns| send_file_to_data_nodes(&filename, &ns, &file));
|
||||||
|
} else {
|
||||||
|
nodes.map(|ns| get_file_from_data_nodes(&ns));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn send_file_to_data_nodes(filename: &String, nodes: &Vec<AvailableNodes>, file: &Vec<u8>) {
|
||||||
|
let mut stream = TcpStream::connect("localhost:6771").unwrap();
|
||||||
|
println!("Going to send a file! Bytes {}", file.len());
|
||||||
|
let chunk = Chunk {
|
||||||
|
id: nodes[0].chunk_id.clone(),
|
||||||
|
filename: filename.clone(),
|
||||||
|
};
|
||||||
|
let packet = serde_json::to_writer(
|
||||||
|
&mut stream,
|
||||||
|
&Packet {
|
||||||
|
p_type: PacketType::PutFile,
|
||||||
|
json: Some(serde_json::to_string(&chunk).unwrap()),
|
||||||
|
data: Some(file.clone()),
|
||||||
|
}).unwrap();
|
||||||
|
stream.flush().unwrap();
|
||||||
|
stream.shutdown(Shutdown::Write).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
// let files: Vec<AvailableNodes> = serde_json::from_reader(&mut stream).unwrap();
|
fn get_file_from_data_nodes(nodes: &Vec<AvailableNodes>) {
|
||||||
// for f in files {
|
|
||||||
// println!("Chunk ID: {}", f.chunk_id);
|
|
||||||
// }
|
|
||||||
// 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)]
|
#[derive(Debug)]
|
||||||
|
@ -6,9 +6,10 @@ extern crate serde_derive;
|
|||||||
|
|
||||||
use a03::*;
|
use a03::*;
|
||||||
use std::net::{TcpStream, Shutdown};
|
use std::net::{TcpStream, Shutdown};
|
||||||
use std::io::Write;
|
use std::io::{Write, Read};
|
||||||
use std::net::TcpListener;
|
use std::net::TcpListener;
|
||||||
use serde_json::from_str;
|
use serde_json::from_str;
|
||||||
|
use std::fs::File;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let endpoint = parse_endpoint_from_cli(0);
|
let endpoint = parse_endpoint_from_cli(0);
|
||||||
@ -17,23 +18,15 @@ fn main() {
|
|||||||
|
|
||||||
for stream in listener.incoming() {
|
for stream in listener.incoming() {
|
||||||
let mut stream = stream.unwrap();
|
let mut stream = stream.unwrap();
|
||||||
// let mut buf = Vec::new();
|
|
||||||
// match stream.read_to_end(&mut buf) {
|
|
||||||
// Ok(size) => {
|
|
||||||
// println!("Total bytes: {}", size);
|
|
||||||
// let mut copy = File::create("new_version").unwrap();
|
|
||||||
// copy.write_all(&buf[..]).unwrap();
|
|
||||||
// },
|
|
||||||
// Err(e) => println!("{}", e),
|
|
||||||
// }
|
|
||||||
match serde_json::from_reader(&mut stream) {
|
match serde_json::from_reader(&mut stream) {
|
||||||
Ok(packet @ Packet { .. }) => match packet.p_type {
|
Ok(Packet { p_type: PacketType::GetFile, json, data, }) =>
|
||||||
// PacketType::GetFiles => shutdown(&mut stream),
|
get(&mut stream, &json.unwrap(), &data.unwrap()),
|
||||||
// PacketType::PutFile => put(&mut stream, &packet.json.unwrap(), &mut Vec::new()),
|
Ok(Packet { p_type: PacketType::PutFile, json, data, }) =>
|
||||||
PacketType::ShutdownDataNode => shutdown(&mut stream, &endpoint),
|
put(&mut stream, &json.unwrap(), &data.unwrap()),
|
||||||
_ => (),
|
Ok(Packet { p_type: PacketType::ShutdownDataNode, .. }) =>
|
||||||
},
|
shutdown(&mut stream, &endpoint),
|
||||||
Err(e) => println!("Error parsing json: {}", e.to_string()),
|
Ok(_) => eprintln!("We don't handle this PacketType"),
|
||||||
|
Err(e) => eprintln!("Error parsing json: {}", e.to_string()),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -51,6 +44,7 @@ fn register_with_meta_server(endpoint: &String) {
|
|||||||
ip: String::from(split[0]),
|
ip: String::from(split[0]),
|
||||||
port: from_str(split[1]).unwrap() })
|
port: from_str(split[1]).unwrap() })
|
||||||
.unwrap()),
|
.unwrap()),
|
||||||
|
data: None,
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
println!("Registered myself");
|
println!("Registered myself");
|
||||||
@ -60,9 +54,17 @@ fn register_with_meta_server(endpoint: &String) {
|
|||||||
println!("{:?}", result);
|
println!("{:?}", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
//fn put(stream: &mut TcpStream, json: &String, files: &mut Vec<String>) {
|
fn put(stream: &mut TcpStream, json: &String, data: &Vec<u8>) {
|
||||||
// let files: PutFiles = serde_json::from_str(json).unwrap();
|
let chunk_id: Chunk = serde_json::from_str(json).unwrap();
|
||||||
//}
|
println!("CId: {:?}", chunk_id);
|
||||||
|
println!("Data Amount: {:?}", data.len());
|
||||||
|
let mut copy = File::create(format!("{}_{}", chunk_id.filename, chunk_id.id)).unwrap();
|
||||||
|
copy.write_all(&data[..]).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get(stream: &mut TcpStream, json: &String, data: &Vec<u8>) {
|
||||||
|
// let files: String = serde_json::from_str(json).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
fn shutdown(stream: &mut TcpStream, endpoint: &String) {
|
fn shutdown(stream: &mut TcpStream, endpoint: &String) {
|
||||||
let mut stream = TcpStream::connect("localhost:6770").unwrap();
|
let mut stream = TcpStream::connect("localhost:6770").unwrap();
|
||||||
@ -77,6 +79,7 @@ fn shutdown(stream: &mut TcpStream, endpoint: &String) {
|
|||||||
ip: String::from(split[0]),
|
ip: String::from(split[0]),
|
||||||
port: from_str(split[1]).unwrap() })
|
port: from_str(split[1]).unwrap() })
|
||||||
.unwrap()),
|
.unwrap()),
|
||||||
|
data: None,
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
println!("Unregistered myself");
|
println!("Unregistered myself");
|
||||||
|
@ -19,6 +19,7 @@ fn main() {
|
|||||||
&Packet {
|
&Packet {
|
||||||
p_type: PacketType::ListFiles,
|
p_type: PacketType::ListFiles,
|
||||||
json: None,
|
json: None,
|
||||||
|
data: None,
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
stream.flush().unwrap();
|
stream.flush().unwrap();
|
||||||
|
@ -37,14 +37,15 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn request_read(stream: &mut TcpStream, conn: &Connection, message: &str) {
|
fn request_read(stream: &mut TcpStream, conn: &Connection, message: &str) {
|
||||||
let file: GetFile = serde_json::from_str(message).unwrap();
|
let filename: String = serde_json::from_str(message).unwrap();
|
||||||
let file_info = get_file_info(&conn, &file.name);
|
let file_info = get_file_info(&conn, &filename);
|
||||||
if file_info.is_none() {
|
if file_info.is_none() {
|
||||||
match serde_json::to_writer(
|
match serde_json::to_writer(
|
||||||
stream,
|
stream,
|
||||||
&Packet {
|
&Packet {
|
||||||
p_type: PacketType::Error,
|
p_type: PacketType::Error,
|
||||||
json: Some(String::from("File not found")),
|
json: Some(String::from("File not found")),
|
||||||
|
data: None,
|
||||||
}) {
|
}) {
|
||||||
Ok(_) => println!("{}", "Copy client attempted to read non-existing file"),
|
Ok(_) => println!("{}", "Copy client attempted to read non-existing file"),
|
||||||
Err(e) => println!("{}", e),
|
Err(e) => println!("{}", e),
|
||||||
@ -63,28 +64,36 @@ fn request_read(stream: &mut TcpStream, conn: &Connection, message: &str) {
|
|||||||
}
|
}
|
||||||
match serde_json::to_writer(
|
match serde_json::to_writer(
|
||||||
stream,
|
stream,
|
||||||
&Packet { p_type: PacketType::Success, json: Some(serde_json::to_string(&nodes).unwrap()) }) {
|
&Packet {
|
||||||
|
p_type: PacketType::Success,
|
||||||
|
json: Some(serde_json::to_string(&nodes).unwrap()),
|
||||||
|
data: None,
|
||||||
|
}) {
|
||||||
Ok(_) => println!("{}", "Sent nodes with chunks"),
|
Ok(_) => println!("{}", "Sent nodes with chunks"),
|
||||||
Err(e) => println!("{}", e),
|
Err(e) => println!("{}", e),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn request_write(stream: &mut TcpStream, conn: &Connection, message: &str) {
|
fn request_write(stream: &mut TcpStream, conn: &Connection, message: &str) {
|
||||||
let file: PutFile = serde_json::from_str(message).unwrap();
|
let file: AddFile = serde_json::from_str(message).unwrap();
|
||||||
let file_already_exists = add_file(&conn, &file.name, file.size as i32);
|
// let file_already_exists = add_file(&conn, &file.name, file.size as i32);
|
||||||
|
let file_already_exists = false;
|
||||||
if file_already_exists {
|
if file_already_exists {
|
||||||
match serde_json::to_writer(
|
match serde_json::to_writer(
|
||||||
stream,
|
stream,
|
||||||
&Packet {
|
&Packet {
|
||||||
p_type: PacketType::Error,
|
p_type: PacketType::Error,
|
||||||
json: Some(String::from("File already exists, please remove before re-uploading")),
|
json: Some(String::from("File already exists, please remove before re-uploading")),
|
||||||
|
data: None,
|
||||||
}) {
|
}) {
|
||||||
Ok(_) => println!("{}", "Copy client attempted to add an existing file"),
|
Ok(_) => println!("{}", "Copy client attempted to add an existing file"),
|
||||||
Err(e) => println!("{}", e),
|
Err(e) => println!("{}", e),
|
||||||
};
|
};
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let file_info = get_file_info(&conn, &file.name).unwrap();
|
// let file_info = get_file_info(&conn, &file.name).unwrap();
|
||||||
|
let file_info = INode { id: 1, name: file.name, size: file.size };
|
||||||
|
println!("{:?}", file_info);
|
||||||
let mut blocks: Vec<Block> = Vec::new();
|
let mut blocks: Vec<Block> = Vec::new();
|
||||||
let mut nodes: Vec<AvailableNodes> = Vec::new();
|
let mut nodes: Vec<AvailableNodes> = Vec::new();
|
||||||
let dnodes = get_data_nodes(&conn);
|
let dnodes = get_data_nodes(&conn);
|
||||||
@ -103,10 +112,14 @@ fn request_write(stream: &mut TcpStream, conn: &Connection, message: &str) {
|
|||||||
chunk_id: uuid.clone(),
|
chunk_id: uuid.clone(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
add_blocks_to_inode(&conn, file_info.id, &blocks);
|
// add_blocks_to_inode(&conn, file_info.id, &blocks);
|
||||||
match serde_json::to_writer(
|
match serde_json::to_writer(
|
||||||
stream,
|
stream,
|
||||||
&Packet { p_type: PacketType::Success, json: Some(serde_json::to_string(&nodes).unwrap()) }) {
|
&Packet {
|
||||||
|
p_type: PacketType::Success,
|
||||||
|
json: Some(serde_json::to_string(&nodes).unwrap()),
|
||||||
|
data: None,
|
||||||
|
}) {
|
||||||
Ok(_) => println!("{}", "Sent nodes with chunks"),
|
Ok(_) => println!("{}", "Sent nodes with chunks"),
|
||||||
Err(e) => println!("{}", e),
|
Err(e) => println!("{}", e),
|
||||||
};
|
};
|
||||||
@ -134,7 +147,11 @@ fn node_registration(stream: &mut TcpStream, conn: &Connection, json: &String) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn report_success(stream: &mut TcpStream, message: &str) {
|
fn report_success(stream: &mut TcpStream, message: &str) {
|
||||||
match serde_json::to_writer(stream, &Packet { p_type: PacketType::Success, json: None }) {
|
match serde_json::to_writer(stream, &Packet {
|
||||||
|
p_type: PacketType::Success,
|
||||||
|
json: None,
|
||||||
|
data: None,
|
||||||
|
}) {
|
||||||
Ok(_) => println!("{}", message),
|
Ok(_) => println!("{}", message),
|
||||||
Err(e) => println!("{}", e),
|
Err(e) => println!("{}", e),
|
||||||
};
|
};
|
||||||
|
11
src/lib.rs
11
src/lib.rs
@ -28,6 +28,7 @@ pub enum PacketType {
|
|||||||
pub struct Packet {
|
pub struct Packet {
|
||||||
pub p_type: PacketType,
|
pub p_type: PacketType,
|
||||||
pub json: Option<String>,
|
pub json: Option<String>,
|
||||||
|
pub data: Option<Vec<u8>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
@ -43,7 +44,7 @@ pub struct NodeRegistration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct PutFile {
|
pub struct AddFile {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub size: u32
|
pub size: u32
|
||||||
}
|
}
|
||||||
@ -56,13 +57,11 @@ pub struct AvailableNodes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct GetFile {
|
pub struct Chunk {
|
||||||
pub name: String,
|
pub id: String,
|
||||||
|
pub filename: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct AddDataBlocks {}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct DataNode {
|
pub struct DataNode {
|
||||||
pub id: u32,
|
pub id: u32,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user