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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
use bytes::buf::{Buf, BufMut};
use bytes::BytesMut;
use log::{debug, trace};
use tokio_util::codec::*;
#[derive(Default)]
pub struct MllpCodec {}
impl MllpCodec {
const BLOCK_HEADER: u8 = 0x0B;
const BLOCK_FOOTER: [u8; 2] = [0x1C, 0x0D];
pub fn new() -> Self {
MllpCodec {}
}
#[cfg(feature = "noncompliance")]
fn get_footer_position(src: &BytesMut) -> Option<usize> {
let mut iter = src.iter().enumerate().peekable();
loop {
let cur = iter.next();
let next = iter.peek();
match (cur, next) {
(Some((i, cur_ele)), Some((_, next_ele))) => {
if cur_ele == &MllpCodec::BLOCK_FOOTER[0]
&& *next_ele == &MllpCodec::BLOCK_FOOTER[1]
{
trace!("MLLP: Found footer at index {}", i);
return Some(i);
}
}
(_, None) => {
trace!("MLLP: Unable to find footer...");
return None;
}
_ => {}
}
}
}
#[cfg(not(feature = "noncompliance"))]
fn get_footer_position(src: &BytesMut) -> Option<usize> {
let mut iter = src.iter().rev().enumerate().peekable();
loop {
let cur = iter.next();
let next = iter.peek();
match (cur, next) {
(Some((_, cur_ele)), Some((i, next_ele))) => {
if cur_ele == &MllpCodec::BLOCK_FOOTER[1]
&& *next_ele == &MllpCodec::BLOCK_FOOTER[0]
{
let index = src.len() - i - 1;
trace!("MLLP: Found footer at index {}", index);
return Some(index);
}
}
(_, None) => {
trace!("MLLP: Unable to find footer...");
return None;
}
_ => {}
}
}
}
}
impl Encoder<BytesMut> for MllpCodec {
type Error = std::io::Error;
fn encode(&mut self, event: BytesMut, dst: &mut BytesMut) -> Result<(), Self::Error> {
dst.reserve(event.len() + 3);
dst.put_u8(MllpCodec::BLOCK_HEADER);
dst.put_slice(&event);
dst.put_slice(&MllpCodec::BLOCK_FOOTER);
debug!("MLLP: Encoded value for send: '{:?}'", dst);
Ok(())
}
}
impl Decoder for MllpCodec {
type Item = BytesMut;
type Error = std::io::Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
if let Some(start_offset) = src.iter().position(|b| *b == MllpCodec::BLOCK_HEADER) {
if let Some(end_offset) = MllpCodec::get_footer_position(src) {
let mut result = src
.split_to(end_offset + 2)
.split_to(end_offset);
result.advance(start_offset + 1);
return Ok(Some(result));
}
}
Ok(None)
}
}
#[cfg(test)]
mod tests {
use super::*;
use bytes::Bytes;
fn wrap_for_mllp(s: &str) -> Bytes {
Bytes::from(format!("\x0B{}\x1C\x0D", s))
}
fn wrap_for_mllp_mut(s: &str) -> BytesMut {
BytesMut::from(format!("\x0B{}\x1C\x0D", s).as_str())
}
#[test]
fn can_construct_without_error() {
let _m = MllpCodec::new();
}
#[test]
fn implements_default() {
let _m = MllpCodec::default();
}
#[test]
fn wraps_simple_data() {
let data = BytesMut::from("abcd");
let mut m = MllpCodec::new();
let mut output_buf = BytesMut::with_capacity(64);
match m.encode(data, &mut output_buf) {
Ok(()) => {}
_ => panic!("Non OK value returned from encode"),
}
let encoded_msg = output_buf.freeze();
println!("Encoded: {:?}", encoded_msg);
assert_eq!(encoded_msg, wrap_for_mllp("abcd"));
}
#[test]
fn find_footer_location() {
let data = wrap_for_mllp_mut("abcd");
let result = MllpCodec::get_footer_position(&data);
assert_eq!(result, Some(5));
}
#[test]
fn missing_footer_detected() {
let data = BytesMut::from("no footer");
let result = MllpCodec::get_footer_position(&data);
assert_eq!(result, None);
}
#[test]
fn ensure_decoder_finds_simple_message() {
let mut data = wrap_for_mllp_mut("abcd");
let mut m = MllpCodec::new();
let result = m.decode(&mut data);
println!("simple message result: {:?}", result);
match result {
Ok(None) => panic!("Failed to find a simple message!"),
Ok(Some(message)) => {
assert_eq!(&message[..], b"abcd");
}
Err(err) => panic!("Error looking for simple message: {:?}", err),
}
}
#[test]
fn ensure_data_after_end_is_ignored() {
let mut data = BytesMut::from("\x0BTest Data\x1C\x0DMore Data");
let mut m = MllpCodec::new();
let result = m.decode(&mut data);
match result {
Ok(Some(message)) => {
assert_eq!(&message[..], b"Test Data");
}
_ => panic!("Failure for message with illegal trailing data"),
}
}
#[test]
fn ensure_no_data_is_left_on_the_stream() {
let mut data = BytesMut::from("\x0BTest Data\x1C\x0D");
let mut m = MllpCodec::new();
let _result = m.decode(&mut data);
assert_eq!(
data.len(),
0,
"Decoder left data sitting in the buffer after read!"
);
}
#[test]
fn ensure_buffer_is_reset_per_message() {
let mut mllp = MllpCodec::new();
let mut data1 = wrap_for_mllp_mut("Test Data");
let mut data2 = wrap_for_mllp_mut("This is different");
let result = mllp.decode(&mut data1);
match result {
Ok(Some(message)) => {
assert_eq!(&message[..], b"Test Data");
}
_ => panic!("Error decoding second message"),
}
let result = mllp.decode(&mut data2);
match result {
Ok(Some(message)) => {
assert_eq!(&message[..], b"This is different");
}
_ => panic!("Error decoding second message"),
}
}
#[test]
fn test_real_message() {
let mut mllp = MllpCodec::new();
let mut data = wrap_for_mllp_mut("MSH|^~\\&|ZIS|1^AHospital|||200405141144||¶ADT^A01|20041104082400|P|2.3|||AL|NE|||8859/15|¶EVN|A01|20041104082400.0000+0100|20041104082400¶PID||\"\"|10||Vries^Danny^D.^^de||19951202|M|||Rembrandlaan^7^Leiden^^7301TH^\"\"^^P||\"\"|\"\"||\"\"|||||||\"\"|\"\"¶PV1||I|3w^301^\"\"^01|S|||100^van den Berg^^A.S.^^\"\"^dr|\"\"||9||||H||||20041104082400.0000+0100");
let result = mllp.decode(&mut data);
match result {
Ok(Some(message)) => {
assert_eq!(message.len(), 338);
}
_ => panic!("Error decoding second message"),
}
}
#[cfg(feature = "noncompliance")]
mod noncompliance_tests {
use super::*;
#[test]
fn test_parsing_multiple_messages() {
let mut mllp = MllpCodec::new();
let mut data = wrap_for_mllp_mut("MSH|^~\\&|ZIS|1^AHospital|||200405141144||¶ADT^A01|20041104082400|P|2.3|||AL|NE|||8859/15|¶EVN|A01|20041104082400.0000+0100|20041104082400¶PID||\"\"|10||Vries^Danny^D.^^de||19951202|M|||Rembrandlaan^7^Leiden^^7301TH^\"\"^^P||\"\"|\"\"||\"\"|||||||\"\"|\"\"¶PV1||I|3w^301^\"\"^01|S|||100^van den Berg^^A.S.^^\"\"^dr|\"\"||9||||H||||20041104082400.0000+0100");
let bytes = data.clone().iter().map(|s| s.to_owned()).collect::<Vec<u8>>();
data.extend_from_slice(&bytes[..]);
data.extend_from_slice(&bytes[..]);
let result = mllp.decode(&mut data);
match result {
Ok(Some(message)) => {
assert_eq!(message.len(), 338);
assert_eq!(data.len(), (message.len() * 2) + 6);
}
_ => assert!(false),
}
let result = mllp.decode(&mut data);
match result {
Ok(Some(message)) => {
assert_eq!(message.len(), 338);
assert_eq!(data.len(), message.len() + 3);
}
_ => assert!(false),
}
}
}
}