1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use file_protocol::{FileProtocol, FileProtocolConfig, ProtocolError, State};
use file_service::recv_loop;
use kubos_system::Config as ServiceConfig;
use rand::{thread_rng, Rng};
use std::fs::{File, OpenOptions};
use std::io::prelude::*;
use std::thread;
use std::time::Duration;
use tempfile::TempDir;
macro_rules! service_new {
($port:expr, $chunk_size:expr) => {{
thread::spawn(move || {
recv_loop(&ServiceConfig::new_from_str(
"file-transfer-service",
&format!(
r#"
[file-transfer-service]
storage_dir = "service"
chunk_size = {}
hold_count = 5
[file-transfer-service.addr]
ip = "127.0.0.1"
port = {}
"#,
$chunk_size, $port
),
))
.unwrap();
});
thread::sleep(Duration::new(1, 0));
}};
}
fn main() {
let test_dir = TempDir::new().expect("Failed to create test dir");
let test_dir_str = test_dir.path().to_str().unwrap();
let source = format!("{}/source", test_dir_str);
let dest = format!("{}/dest", test_dir_str);
let service_port = 8006;
let mut file = OpenOptions::new()
.create(true)
.write(true)
.append(true)
.open(source.clone())
.unwrap();
for _ in 0..100 {
let mut contents = [0u8; 1_000_000];
thread_rng().fill(&mut contents[..]);
file.write(&contents).unwrap();
}
service_new!(service_port, 4096);
let result = download(
"127.0.0.1",
&format!("127.0.0.1:{}", service_port),
&source,
&dest,
Some("client".to_owned()),
4096,
);
assert!(result.is_ok());
let mut source_file = File::open(source).unwrap();
let mut dest_file = File::open(dest).unwrap();
for num in 0..24415 {
let mut source_buf = [0u8; 4096];
let mut dest_buf = [0u8; 4096];
source_file.read(&mut source_buf).unwrap();
dest_file.read(&mut dest_buf).unwrap();
assert_eq!(&source_buf[..], &dest_buf[..], "Chunk mismatch: {}", num);
}
}
pub fn download(
host_ip: &str,
remote_addr: &str,
source_path: &str,
target_path: &str,
prefix: Option<String>,
chunk_size: u32,
) -> Result<(), ProtocolError> {
let hold_count = 5;
let f_config = FileProtocolConfig::new(prefix, chunk_size as usize, hold_count);
let f_protocol = FileProtocol::new(host_ip, remote_addr, f_config);
let channel = f_protocol.generate_channel()?;
f_protocol.send_import(channel, source_path)?;
let reply = match f_protocol.recv(None) {
Ok(message) => message,
Err(error) => return Err(error),
};
let state = f_protocol.process_message(
reply,
&State::StartReceive {
path: target_path.to_string(),
},
)?;
Ok(f_protocol.message_engine(|d| f_protocol.recv(Some(d)), Duration::from_secs(2), &state)?)
}