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