Skip to content

Commit e435646

Browse files
committed
Proof of concept udp client/server for viz
1 parent e438934 commit e435646

File tree

7 files changed

+130
-1
lines changed

7 files changed

+130
-1
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ members = [
2424
"patches",
2525
"player",
2626
"utils",
27+
"viz-udp-app",
28+
"viz-udp-app-lib",
2729
"widgets",
2830
]

interactive/src/window.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ impl WindowRunning {
222222
Event::MouseButtonUp { mouse_btn, .. } => {
223223
self.window.input.set_mouse_button(mouse_btn, false);
224224
}
225-
226225
_ => (),
227226
}
228227
}

viz-udp-app-lib/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "viz_udp_app_lib"
3+
version = "0.1.0"
4+
edition = "2024"
5+
description = "Library for launching and interacting with instances of caw_viz_udp_app"
6+
authors = ["Stephen Sherratt <[email protected]>"]
7+
license = "MIT"
8+
homepage = "https://github.com/gridbugs/caw.git"
9+
repository = "https://github.com/gridbugs/caw.git"
10+
documentation = "https://docs.rs/viz_udp_app_lib"
11+
12+
[dependencies]
13+
anyhow = "1.0"
14+
15+
[[example]]
16+
name = "viz_udp_app_lib_handshake"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use std::{
2+
net::{Ipv4Addr, UdpSocket},
3+
process::Command,
4+
time::Duration,
5+
};
6+
7+
// TODO
8+
const PROGRAM_NAME: &str = "/home/s/src/caw/target/debug/caw_viz_udp_app";
9+
10+
fn main() -> anyhow::Result<()> {
11+
let socket = UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0))?;
12+
println!("Our address: {:?}", socket.local_addr()?);
13+
let mut command = Command::new(PROGRAM_NAME);
14+
command.args(["--server".to_string(), socket.local_addr()?.to_string()]);
15+
let _child = command.spawn()?;
16+
let mut buf = [0];
17+
let (size, client_addr) = socket.recv_from(&mut buf)?;
18+
// TODO: expect a magic number here
19+
assert_eq!(size, 1);
20+
assert_eq!(buf, [42]);
21+
println!("Client address: {:?}", client_addr);
22+
socket.connect(client_addr)?;
23+
let mut i = 0;
24+
loop {
25+
// TODO: gracefully shut down if the client dissapears
26+
assert_eq!(socket.send(&[i])?, 1);
27+
i = i.wrapping_add(1);
28+
std::thread::sleep(Duration::from_millis(100));
29+
}
30+
}

viz-udp-app-lib/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pub fn add(left: u64, right: u64) -> u64 {
2+
left + right
3+
}
4+
5+
#[cfg(test)]
6+
mod tests {
7+
use super::*;
8+
9+
#[test]
10+
fn it_works() {
11+
let result = add(2, 2);
12+
assert_eq!(result, 4);
13+
}
14+
}

viz-udp-app/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "caw_viz_udp_app"
3+
version = "0.1.0"
4+
edition = "2024"
5+
description = "App for visualizing audio data from the caw synthesizer framework"
6+
authors = ["Stephen Sherratt <[email protected]>"]
7+
license = "MIT"
8+
homepage = "https://github.com/gridbugs/caw.git"
9+
repository = "https://github.com/gridbugs/caw.git"
10+
documentation = "https://docs.rs/caw_viz_udp_app"
11+
12+
[dependencies]
13+
anyhow = "1.0"
14+
sdl2 = { version = "0.37" }
15+
line_2d = "0.5"
16+
clap = { version = "4", features = ["derive"] }

viz-udp-app/src/main.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use std::net::{Ipv4Addr, UdpSocket};
2+
3+
use anyhow::anyhow;
4+
use clap::Parser;
5+
use sdl2::{event::Event, pixels::Color};
6+
7+
#[derive(Parser)]
8+
struct Args {
9+
#[arg(long)]
10+
server: String,
11+
#[arg(long, default_value_t = 640)]
12+
width: u32,
13+
#[arg(long, default_value_t = 480)]
14+
height: u32,
15+
}
16+
17+
fn main() -> anyhow::Result<()> {
18+
let args = Args::parse();
19+
let sdl_context = sdl2::init().map_err(|e| anyhow!(e))?;
20+
let video_subsystem = sdl_context.video().map_err(|e| anyhow!(e))?;
21+
let window = video_subsystem
22+
.window("CAW Visualization", args.width, args.height)
23+
.build()?;
24+
let mut canvas = window
25+
.into_canvas()
26+
.target_texture()
27+
.present_vsync()
28+
.build()?;
29+
let mut event_pump = sdl_context.event_pump().map_err(|e| anyhow!(e))?;
30+
let socket = UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0))?;
31+
socket.connect(args.server)?;
32+
// TODO: send a magic number here
33+
assert_eq!(socket.send(&[42])?, 1);
34+
socket.set_nonblocking(true)?;
35+
loop {
36+
for event in event_pump.poll_iter() {
37+
if let Event::Quit { .. } = event {
38+
std::process::exit(0);
39+
}
40+
}
41+
// TODO: larger buffer
42+
let mut buf = [0];
43+
if let Ok(size) = socket.recv(&mut buf) {
44+
// TODO: check for "would block" errors here
45+
assert_eq!(size, 1);
46+
println!("{}", buf[0]);
47+
}
48+
canvas.set_draw_color(Color::RGB(0, 0, 0));
49+
canvas.clear();
50+
canvas.present();
51+
}
52+
}

0 commit comments

Comments
 (0)