I do not know
This commit is contained in:
parent
82862137a5
commit
5de07db02a
1 changed files with 84 additions and 31 deletions
115
src/main.rs
115
src/main.rs
|
@ -1,20 +1,22 @@
|
|||
use std::fs::{File, Metadata, self};
|
||||
use std::time::Instant;
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::io::{Read, Write, self};
|
||||
use std::io::{Read, Write, self, BufReader};
|
||||
use std::env::{self};
|
||||
|
||||
struct FileInfo
|
||||
{
|
||||
file:Option<File>,
|
||||
reader:Option<BufReader<File>>,
|
||||
location:String,
|
||||
bytes:Vec<u8>,
|
||||
size:usize,
|
||||
bytes_current:Vec<u8>,
|
||||
size_current:usize,
|
||||
size_total:Option<usize>,
|
||||
metadata:Option<Metadata>,
|
||||
}
|
||||
impl FileInfo
|
||||
{
|
||||
fn reading_operations(&mut self)
|
||||
fn reading_operations(&mut self, stream:&mut TcpStream)
|
||||
{
|
||||
self.read_metadata();
|
||||
match self.metadata
|
||||
|
@ -23,10 +25,12 @@ impl FileInfo
|
|||
{
|
||||
if Metadata::is_file(metadata)
|
||||
{
|
||||
self.size_total = Some(Metadata::len(&metadata) as usize);
|
||||
self.read_file();
|
||||
}
|
||||
else if Metadata::is_symlink(metadata)
|
||||
{
|
||||
self.size_total = Some(Metadata::len(&metadata) as usize);
|
||||
self.read_file();
|
||||
}
|
||||
else
|
||||
|
@ -37,49 +41,105 @@ impl FileInfo
|
|||
}
|
||||
}
|
||||
None =>
|
||||
{
|
||||
println!("Error: Read Metadata -> {}", self.location);
|
||||
}
|
||||
}
|
||||
match self.size_total
|
||||
{
|
||||
Some(ref mut size_total) =>
|
||||
{
|
||||
|
||||
self.file_to_byte(stream)
|
||||
}
|
||||
None =>
|
||||
{
|
||||
println!("Error: Read Size -> {}", self.location);
|
||||
}
|
||||
}
|
||||
|
||||
self.file_to_byte();
|
||||
}
|
||||
fn writing_operations(&mut self)
|
||||
fn writing_operations(&mut self, stream:&mut TcpStream)
|
||||
{
|
||||
self.write_file();
|
||||
}
|
||||
fn read_metadata(&mut self)
|
||||
{
|
||||
//use match, there is a chance to fail creation. don't pass with just some
|
||||
self.metadata = Some(fs::metadata(&self.location).expect("Error: Read Metadata"));
|
||||
}
|
||||
fn read_file(&mut self)
|
||||
{
|
||||
self.file = Some(File::open(&self.location).expect("Error: Open File"));
|
||||
}
|
||||
fn file_to_byte(&mut self)
|
||||
match File::open(&self.location)
|
||||
{
|
||||
Ok(file) =>
|
||||
{
|
||||
self.file = Some(file);
|
||||
self.reader = Some(BufReader::new(file));
|
||||
}
|
||||
Err(err_val) =>
|
||||
{
|
||||
println!("Error: Open File -> {} | Error: {}", self.location, err_val);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
fn file_to_byte(&mut self, stream:&mut TcpStream)
|
||||
{
|
||||
self.bytes_current.clear();
|
||||
match self.file
|
||||
{
|
||||
Some(ref mut file_descriptor) =>
|
||||
{
|
||||
match File::read_to_end(file_descriptor, &mut self.bytes)
|
||||
//replace with readbuffer ?
|
||||
match self.reader
|
||||
{
|
||||
Ok(size) =>
|
||||
Some(reader) =>
|
||||
{
|
||||
self.size = size;
|
||||
println!("Done: Read -> {} bytes", size);
|
||||
// I don't know what to do
|
||||
}
|
||||
Err(err_val) => println!("Error: Read {}", err_val),
|
||||
None =>
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
match File::read_exact(file_descriptor, &mut self.bytes_current)
|
||||
{
|
||||
Ok(_) =>
|
||||
{
|
||||
self.size_current = self.size_current+self.bytes_current.len();
|
||||
//need to send data, or call here from connection
|
||||
match stream.write_all(&self.bytes_current)
|
||||
{
|
||||
Ok(_) =>
|
||||
{
|
||||
// We will track when we stop later
|
||||
}
|
||||
Err(err_val) =>
|
||||
{
|
||||
println!("Error: Send -> {} | Error: {}", self.location, err_val)
|
||||
}
|
||||
}
|
||||
if self.size_total == Some(self.size_current)
|
||||
{
|
||||
println!("Done: Read -> {} | Done: Read -> {} bytes", self.location, self.size_current);
|
||||
}
|
||||
else
|
||||
{
|
||||
self.file_to_byte(stream);
|
||||
}
|
||||
}
|
||||
Err(err_val) => println!("Error: Read -> {} | Error: {}", self.location, err_val),
|
||||
}
|
||||
}
|
||||
None =>
|
||||
{
|
||||
println!("Error: File None");
|
||||
println!("Error: File None -> {}", self.location);
|
||||
}
|
||||
}
|
||||
}
|
||||
fn write_file(&mut self)
|
||||
{
|
||||
//use match, there is a chance to fail creation. don't pass with just some
|
||||
self.file = Some(File::create(&self.location).expect("Error: Create File"));
|
||||
match self.file
|
||||
{
|
||||
|
@ -89,7 +149,7 @@ impl FileInfo
|
|||
{
|
||||
Ok(_) =>
|
||||
{
|
||||
self.size = self.bytes.len();
|
||||
self.size_total = self.bytes.len();
|
||||
println!("Done: Write -> {} bytes", &mut self.size);
|
||||
}
|
||||
Err(err_val) => println!("Error: Write {}", err_val),
|
||||
|
@ -148,11 +208,8 @@ impl Connection
|
|||
return;
|
||||
}
|
||||
file_info.bytes = data;
|
||||
let start_disk_time = Instant::now();
|
||||
println!("Passed: Network -> {:#?}", start_disk_time.duration_since(start_time));
|
||||
FileInfo::writing_operations(file_info);
|
||||
FileInfo::writing_operations(file_info, &mut stream);
|
||||
let finish_time = Instant::now();
|
||||
println!("Passed: Write -> {:#?}", finish_time.duration_since(start_disk_time));
|
||||
println!("Passed: Total -> {:#?}", finish_time.duration_since(start_time));
|
||||
}
|
||||
Err(e) =>
|
||||
|
@ -190,16 +247,12 @@ impl Connection
|
|||
}
|
||||
match TcpStream::connect(address)
|
||||
{
|
||||
Ok(mut socket) =>
|
||||
Ok(mut stream) =>
|
||||
{
|
||||
let start_time = Instant::now();
|
||||
println!("Connected");
|
||||
FileInfo::reading_operations(file_info);
|
||||
let start_network_time = Instant::now();
|
||||
println!("Passed: Read -> {:#?}", start_network_time.duration_since(start_time));
|
||||
socket.write_all(&file_info.bytes).unwrap();
|
||||
FileInfo::reading_operations(file_info, &mut stream);
|
||||
let finish_time = Instant::now();
|
||||
println!("Passed: Network -> {:#?}", finish_time.duration_since(start_network_time));
|
||||
println!("Passed: Total -> {:#?}", finish_time.duration_since(start_time));
|
||||
}
|
||||
Err(e) =>
|
||||
|
@ -231,14 +284,14 @@ fn main()
|
|||
//First we should check folder structure and validation then make connection.
|
||||
println!("Hello, world!");
|
||||
|
||||
let bytes:Vec<u8> = vec![];
|
||||
let size:usize = 0 as usize;
|
||||
let mut data = FileInfo
|
||||
{
|
||||
file:None,
|
||||
reader:None,
|
||||
location:take_arg(),
|
||||
bytes:bytes,
|
||||
size:size,
|
||||
bytes_current:Vec::with_capacity(1024),
|
||||
size_current:0 as usize,
|
||||
size_total:None,
|
||||
metadata:None,
|
||||
};
|
||||
match &take_string("Input: Server 's', Client 'c'".to_string())[0..1]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue