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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/// The main data read-write loop
use crate::datapipe_types::{InputReader, OutputWriter, error_root_cause};
use crate::encryption::{StreamDecryptor, StreamEncryptor};
use crate::parameters::Parameters;
use crate::reader::Reader;
use crate::writer::Writer;
use log::{error, info, warn};
use tokio::sync::mpsc::{Receiver, Sender, channel};
const QUEUE_SIZE: usize = 2048;
const RETRY_MAX: i32 = 5; // retry failed reads or writes up to this many consecutive times before stopping
async fn reader_child(mut reader: Reader, sender: Sender<Vec<u8>>) {
let mut read_retry_count = 0;
loop {
match reader.read().await {
Ok(buffer) => {
if buffer.is_empty() {
// retry a few times to make sure all of the input is read
read_retry_count += 1;
tokio::time::sleep(std::time::Duration::from_millis(5)).await;
if read_retry_count >= RETRY_MAX {
warn!("reader_child: no bytes read; stopping");
break;
}
} else {
// buffer not empty send the data
let v = buffer.to_vec();
match sender.send(v).await {
Ok(()) => {
read_retry_count = 0;
}
Err(_error) => {
warn!("reader_child: cannot send to next stage; stopping");
break;
}
}
}
}
Err(error) => {
read_retry_count += 1;
tokio::time::sleep(std::time::Duration::from_millis(5)).await;
warn!(
"reader_child: Error reading from input source: {error}; read_retry_count is {read_retry_count}"
);
if read_retry_count >= RETRY_MAX {
let error_message = format!(
"reader_child: RETRY_MAX {RETRY_MAX} reached; quitting due to repeated read errors"
);
error!("{error_message}");
break;
}
}
}
}
}
async fn decryptor_child(
mut receiver: Receiver<Vec<u8>>,
mut decryptor: StreamDecryptor,
sender: Sender<Vec<u8>>,
) {
let mut buffer: Vec<u8> = Vec::new();
let mut retry_count = 0;
loop {
match receiver.recv().await {
Some(bytes) => {
retry_count = 0;
buffer.extend_from_slice(&bytes);
match decryptor.decrypt(&mut buffer) {
Ok(plain) => match sender.send(plain).await {
Ok(()) => {}
Err(_error) => {
warn!("decryptor_child: cannot send to next stage; stopping");
break;
}
},
Err(error) => {
let error_message = format!(
"decryptor_child: error decrypting data: {}",
error_root_cause(&error)
);
error!("{error_message}");
eprintln!("{error_message}");
break;
}
}
}
None => {
retry_count += 1;
tokio::time::sleep(std::time::Duration::from_millis(5)).await;
if retry_count >= RETRY_MAX {
warn!("decryptor_child: no bytes received; stopping");
break;
}
}
}
}
}
async fn encryptor_child(
mut receiver: Receiver<Vec<u8>>,
mut encryptor: StreamEncryptor,
sender: Sender<Vec<u8>>,
) {
let mut buffer: Vec<u8> = Vec::new();
let mut retry_count = 0;
loop {
match receiver.recv().await {
Some(bytes) => {
retry_count = 0;
buffer.extend_from_slice(&bytes);
match encryptor.encrypt(&mut buffer) {
Ok(cipher) => match sender.send(cipher).await {
Ok(()) => {}
Err(_error) => {
warn!("encryptor_child: cannot send to next stage; stopping");
break;
}
},
Err(error) => {
let error_message = format!(
"encryptor_child: error encrypting data: {}",
error_root_cause(&error)
);
error!("{error_message}");
eprintln!("{error_message}");
break;
}
}
}
None => {
retry_count += 1;
tokio::time::sleep(std::time::Duration::from_millis(5)).await;
if retry_count >= RETRY_MAX {
warn!("encryptor_child: no bytes received; stopping");
break;
}
}
}
}
}
async fn writer_child(mut receiver: Receiver<Vec<u8>>, mut writers: Vec<Writer>) {
let mut write_retry_count = 0;
'writer: loop {
match receiver.recv().await {
Some(bytes) => {
if !bytes.is_empty() {
for writer in &mut writers {
match writer.write(&bytes).await {
Ok(()) => {
// if at least one writer is working, continue
write_retry_count = 0;
}
Err(error) => {
// should the count be per output sink?
let error_cause = error_root_cause(&error);
write_retry_count += 1;
warn!(
"writer_child: Error writing to output: {error_cause}; write_retry_count is {write_retry_count}"
);
if write_retry_count >= RETRY_MAX {
let error_message = format!(
"writer_child: RETRY_MAX {RETRY_MAX} reached; quitting due to repeated write errors"
);
error!("{error_message}");
eprintln!("{error_message}");
break 'writer;
}
}
}
}
}
}
None => {
// retry a few times before quitting to ensure all the output gets written
write_retry_count += 1;
tokio::time::sleep(std::time::Duration::from_millis(5)).await;
if write_retry_count >= RETRY_MAX {
warn!("writer_child: stopping");
break;
}
}
}
}
}
/// library API entry point: just supply parameters and run it
pub async fn run_data_pipe(parameters: Parameters) {
// vec to track child threads
let mut children = Vec::new();
// setup queue from reader thread to writer thread
let (reader_sender, reader_receiver) = channel::<Vec<u8>>(QUEUE_SIZE);
let Parameters {
reader,
maybe_decryptor,
maybe_encryptor,
writers,
} = parameters;
// spawn threads:
// 1) reader thread to get byte input and place in input queue
let reader_handle = tokio::spawn(reader_child(reader, reader_sender));
children.push(reader_handle);
// this is messy, but we don't want to add empty stages or unnecessary queues
// is there a better way to build a dynamic pipeline of stages?
// 2) Decryption thread (if specified) and 3) Encryption thread (if specified)
// 4) writer thread to write output queue (always)
match maybe_decryptor {
Some(decryptor) => {
match maybe_encryptor {
Some(encryptor) => {
// decryptor and encryptor
let (decryptor_sender, decryptor_receiver) = channel::<Vec<u8>>(QUEUE_SIZE);
let (encryptor_sender, encryptor_receiver) = channel::<Vec<u8>>(QUEUE_SIZE);
let decryptor_handle = tokio::spawn(decryptor_child(
reader_receiver,
decryptor,
decryptor_sender,
));
children.push(decryptor_handle);
let encryptor_handle = tokio::spawn(encryptor_child(
decryptor_receiver,
encryptor,
encryptor_sender,
));
children.push(encryptor_handle);
let writer_handle = tokio::spawn(writer_child(encryptor_receiver, writers));
children.push(writer_handle);
}
None => {
// decryptor only
let (decryptor_sender, decryptor_receiver) = channel::<Vec<u8>>(QUEUE_SIZE);
let decryptor_handle = tokio::spawn(decryptor_child(
reader_receiver,
decryptor,
decryptor_sender,
));
children.push(decryptor_handle);
let writer_handle = tokio::spawn(writer_child(decryptor_receiver, writers));
children.push(writer_handle);
}
}
}
None => {
match maybe_encryptor {
Some(encryptor) => {
// encryptor only
let (encryptor_sender, encryptor_receiver) = channel::<Vec<u8>>(QUEUE_SIZE);
let encryptor_handle = tokio::spawn(encryptor_child(
reader_receiver,
encryptor,
encryptor_sender,
));
children.push(encryptor_handle);
let writer_handle = tokio::spawn(writer_child(encryptor_receiver, writers));
children.push(writer_handle);
}
None => {
// neither decryptor nor encryptor
let writer_handle = tokio::spawn(writer_child(reader_receiver, writers));
children.push(writer_handle);
}
}
}
}
info!("main thread: waiting for child threads to finish");
for child in children {
match child.await {
Ok(()) => {}
Err(error) => {
eprintln!("{error}");
}
}
}
}