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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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, $down_port:expr, $chunk_size:expr, $storage_dir:expr) => {{
thread::spawn(move || {
recv_loop(
&ServiceConfig::new_from_str(
"file-transfer-service",
&format!(
r#"
[file-transfer-service]
storage_dir = "{}"
transfer_chunk_size = {}
hash_chunk_size = {}
hold_count = 5
downlink_ip = "127.0.0.1"
downlink_port = {}
[file-transfer-service.addr]
ip = "127.0.0.1"
port = {}
"#,
$storage_dir,
$chunk_size,
$chunk_size * 64,
$down_port,
$port
),
)
.unwrap(),
)
.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 = 7006;
let down_port = 6006;
{
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_all(&contents).unwrap();
}
}
let storage_dir = format!("{}/service", test_dir_str);
service_new!(service_port, down_port, 4096, storage_dir);
let result = upload(
"127.0.0.1",
down_port,
&format!("127.0.0.1:{}", service_port),
&source,
&dest,
Some(format!("{}/client", test_dir_str)),
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];
let _ = source_file.read(&mut source_buf).unwrap();
let _ = dest_file.read(&mut dest_buf).unwrap();
assert_eq!(&source_buf[..], &dest_buf[..], "Chunk mismatch: {}", num);
}
}
pub fn upload(
host_ip: &str,
host_port: u16,
remote_addr: &str,
source_path: &str,
target_path: &str,
prefix: Option<String>,
chunk_size: u32,
) -> Result<String, ProtocolError> {
let hold_count = 20;
let f_config = FileProtocolConfig::new(
prefix,
chunk_size as usize,
hold_count,
1,
None,
(chunk_size as usize) * 64,
);
let f_protocol =
FileProtocol::new(&format!("{}:{}", host_ip, host_port), remote_addr, f_config);
let (hash, num_chunks, mode) = f_protocol.initialize_file(&source_path)?;
let channel = f_protocol.generate_channel()?;
f_protocol.send_metadata(channel, &hash, num_chunks)?;
f_protocol.send_export(channel, &hash, &target_path, mode)?;
f_protocol.message_engine(
|d| f_protocol.recv(Some(d)),
Duration::from_secs(2),
&State::Transmitting,
)?;
Ok(hash.to_owned())
}