~jojo/jojos-hue

b241cb59e570564d648928ec8605b6b66fea4533 — JoJo 2 years ago 862f967
Remove redundant/unusud UDP socket on server (7018)
1 files changed, 24 insertions(+), 21 deletions(-)

M server/src/main.rs
M server/src/main.rs => server/src/main.rs +24 -21
@@ 1,6 1,6 @@
use audiovis::AudioVis;
use byteorder::{ByteOrder, LittleEndian};
use noise::{NoiseFn, OpenSimplex, Perlin, Seedable};
use noise::{NoiseFn, Perlin, Seedable};
use palette::{FromColor, Hsl, Hsv, Hue, RgbHue, Shade, Srgb};
use std::{
    collections::{hash_map::DefaultHasher, HashMap},


@@ 79,26 79,26 @@ async fn main() -> tide::Result<()> {
    let (heartbeat_tx, heartbeat_rx) = channel();
    let (cmd_tx, cmd_rx) = sync_channel(8);

    thread::spawn(move || {
        let listener = UdpSocket::bind("0.0.0.0:7017").unwrap();
        loop {
            let mut msg = [0; 13];
            if let Some((_, addr)) = listener.recv_from(&mut msg).ok() {
                if let Some(beat) = parse_heartbeat(&msg, addr) {
                    heartbeat_tx.send(beat).unwrap();
                } else {
                    println!(
                        "Malformed message from {}, {:?} / {:?}",
                        addr,
                        String::from_utf8_lossy(&msg),
                        msg
                    )
                }
    let socket = Arc::new(UdpSocket::bind("0.0.0.0:7017").unwrap());
    let socket2 = socket.clone();

    thread::spawn(move || loop {
        let mut msg = [0; 13];
        if let Ok((_, addr)) = socket.recv_from(&mut msg) {
            if let Some(beat) = parse_heartbeat(&msg, addr) {
                heartbeat_tx.send(beat).unwrap();
            } else {
                println!(
                    "Malformed message from {}, {:?} / {:?}",
                    addr,
                    String::from_utf8_lossy(&msg),
                    msg
                )
            }
        }
    });

    thread::spawn(move || controller(heartbeat_rx, cmd_rx));
    thread::spawn(move || controller(socket2, heartbeat_rx, cmd_rx));

    let mut web = tide::new();
    web.at("/").serve_file("static/index.html")?;


@@ 134,8 134,11 @@ async fn proc_handler(req: Request<()>, tx: SyncSender<QueryCmd>) -> tide::Resul
    Ok("cool, ty".into())
}

fn controller(heartbeat_rx: Receiver<Heartbeat>, cmd_rx: Receiver<QueryCmd>) {
    let sender = Arc::new(UdpSocket::bind("0.0.0.0:7018").unwrap());
fn controller(
    socket: Arc<UdpSocket>,
    heartbeat_rx: Receiver<Heartbeat>,
    cmd_rx: Receiver<QueryCmd>,
) {
    let mut clients = HashMap::<SocketAddr, (Instant, Sender<_>)>::new();
    let mut tprev = Instant::now();
    let mut tcmd = Instant::now();


@@ 149,7 152,7 @@ fn controller(heartbeat_rx: Receiver<Heartbeat>, cmd_rx: Receiver<QueryCmd>) {
                    println!("Registered client {} with {} LEDs", addr, nleds);
                    clients.insert(
                        addr,
                        (Instant::now(), setup_client(addr, nleds, sender.clone())),
                        (Instant::now(), setup_client(addr, nleds, socket.clone())),
                    );
                }
            }


@@ 179,7 182,7 @@ fn controller(heartbeat_rx: Receiver<Heartbeat>, cmd_rx: Receiver<QueryCmd>) {
        }
        let res = cmd.render_cmd(base, tcmd.elapsed().as_secs_f64());
        for (_, tx) in clients.values() {
            tx.send(res.clone()).ok();
            tx.send(res).ok();
        }
        let dt = tprev.elapsed();
        let period = Duration::from_secs_f32(1.0 / FPS);