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
use crate::{Frame, Loss};
use anyhow::Result;
use clap::Parser;
use rand::{rngs::SmallRng, Rng, SeedableRng};
use socket2::{Domain, Protocol, Socket, Type};
use std::{
    fs::File,
    io::{BufReader, ErrorKind, Read, Seek},
    net::{Ipv4Addr, SocketAddr},
    time::Duration,
};

/// Stabilizer stream source options
#[derive(Parser, Debug, Clone)]
pub struct SourceOpts {
    /// The local IP to receive streaming data on.
    #[arg(short, long, default_value = "0.0.0.0")]
    ip: std::net::Ipv4Addr,

    /// The UDP port to receive streaming data on.
    #[arg(short, long, default_value_t = 9293)]
    port: u16,

    /// Use frames from the given file
    #[arg(short, long)]
    file: Option<String>,

    /// Frame size in file (8 + n_batches*n_channel*batch_size)
    #[arg(short, long, default_value_t = 8 + 30 * 2 * 6 * 4)]
    frame_size: usize,

    /// On a file, wrap around and repeat
    #[arg(short, long)]
    repeat: bool,

    /// Single le f32 raw trace, architecture dependent endianess
    #[arg(short, long)]
    single: Option<String>,

    /// Power law noise with psd f^noise.
    #[arg(short, long)]
    noise: Option<i32>,
}

#[derive(Debug)]
enum Data {
    Udp(Socket),
    File(BufReader<File>),
    Single(BufReader<File>),
    Noise((SmallRng, Vec<f64>)),
}

pub struct Source {
    opts: SourceOpts,
    data: Data,
    loss: Loss,
}

impl Source {
    pub fn new(opts: SourceOpts) -> Result<Self> {
        let data = if let Some(noise) = opts.noise {
            Data::Noise((
                SmallRng::seed_from_u64(0x7654321),
                vec![0.0; noise.unsigned_abs() as _],
            ))
        } else if let Some(file) = &opts.file {
            Data::File(BufReader::with_capacity(1 << 20, File::open(file)?))
        } else if let Some(single) = &opts.single {
            Data::Single(BufReader::with_capacity(1 << 20, File::open(single)?))
        } else {
            log::info!("Binding to {}:{}", opts.ip, opts.port);
            let socket = Socket::new(Domain::IPV4, Type::DGRAM, Some(Protocol::UDP))?;
            socket.set_read_timeout(Some(Duration::from_millis(1000)))?;
            socket.set_recv_buffer_size(1 << 20)?;
            socket.set_reuse_address(true)?;
            if opts.ip.is_multicast() {
                socket.join_multicast_v4(&opts.ip, &Ipv4Addr::UNSPECIFIED)?;
            }
            socket.bind(&SocketAddr::new(opts.ip.into(), opts.port).into())?;
            Data::Udp(socket)
        };
        Ok(Self {
            opts,
            data,
            loss: Loss::default(),
        })
    }

    pub fn get(&mut self) -> Result<Vec<Vec<f32>>> {
        Ok(match &mut self.data {
            Data::Noise((rng, state)) => {
                vec![(0..1024)
                    .zip(rng.sample_iter(rand::distributions::Open01))
                    .map(|(_, mut x)| {
                        x = (x + 0.5) * 6.0f64.sqrt();
                        let diff = self.opts.noise.unwrap() > 0;
                        for s in state.iter_mut() {
                            (x, *s) = if diff { (x - *s, x) } else { (*s, x + *s) };
                        }
                        x as _
                    })
                    .collect()]
            }
            Data::File(fil) => loop {
                let mut buf = [0u8; 2048];
                match fil.read_exact(&mut buf[..self.opts.frame_size]) {
                    Ok(()) => {
                        let frame = Frame::from_bytes(&buf[..self.opts.frame_size])?;
                        self.loss.update(&frame);
                        break frame.data.traces().into();
                    }
                    Err(e) if e.kind() == ErrorKind::UnexpectedEof && self.opts.repeat => {
                        fil.seek(std::io::SeekFrom::Start(0))?;
                    }
                    Err(e) => Err(e)?,
                }
            },
            Data::Single(fil) => loop {
                let mut buf = [0u8; 2048];
                match fil.read(&mut buf[..]) {
                    Ok(len) => {
                        if len == 0 && self.opts.repeat {
                            fil.seek(std::io::SeekFrom::Start(0))?;
                            continue;
                        }
                        let v: &[[u8; 4]] = bytemuck::cast_slice(&buf[..len / 4 * 4]);
                        break vec![v.iter().map(|b| f32::from_le_bytes(*b)).collect()];
                    }
                    Err(e) => Err(e)?,
                }
            },
            Data::Udp(socket) => {
                let mut buf = [0u8; 2048];
                let len = socket.recv(unsafe { core::mem::transmute(&mut buf[..]) })?; // meh
                let frame = Frame::from_bytes(&buf[..len])?;
                self.loss.update(&frame);
                frame.data.traces().into()
            }
        })
    }

    pub fn finish(&self) {
        self.loss.analyze()
    }
}