Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement basic server and test probe #12

Merged
merged 15 commits into from
Apr 24, 2023
Merged
10 changes: 5 additions & 5 deletions crates/composer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ version = "0.1.0"
edition = "2021"

[dependencies]
bincode = "1.3.3"
color-eyre = "0.6.2"
eyre = "0.6.8"
rodio = { version = "0.17.1", features = ["symphonia-wav"] }
serde = { version = "1.0.160", features = ["serde_derive"] }
bincode = "1"
color-eyre = "0.6"
eyre = "0.6"
rodio = { version = "0.17", features = ["symphonia-wav"] }
serde = { version = "1", features = ["derive"] }
12 changes: 8 additions & 4 deletions crates/composer/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use eyre::Result;
use serde::{Deserialize, Serialize};
use std::net::UdpSocket;
use std::net::{ToSocketAddrs, UdpSocket};

pub const DEFAULT_SERVER_ADDRESS: &str = "localhost:8888";

Expand All @@ -14,15 +14,19 @@ pub struct Client {
}

impl Client {
pub fn new(server_address: Option<&str>) -> Result<Self> {
pub fn try_default() -> Result<Self> {
Self::new(DEFAULT_SERVER_ADDRESS)
}

pub fn new(server_address: impl ToSocketAddrs) -> Result<Self> {
let socket = UdpSocket::bind("localhost:0")?;
skywhale marked this conversation as resolved.
Show resolved Hide resolved
socket.connect(server_address.unwrap_or(DEFAULT_SERVER_ADDRESS))?;
socket.connect(server_address)?;

Ok(Self { socket })
}

pub fn send(&self, event: &Event) -> Result<()> {
let data = bincode::serialize(&event)?;
let data = bincode::serialize(event)?;
self.socket.send(&data)?;
Ok(())
}
Expand Down
39 changes: 22 additions & 17 deletions crates/composer/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use composer::api::Event;
use composer::api::DEFAULT_SERVER_ADDRESS;
use eyre::Result;
use rodio::{source::Source, Decoder, OutputStream};
use rodio::{source::Source, Decoder, OutputStream, OutputStreamHandle};
use std::fs::File;
use std::io::BufReader;
use std::net::UdpSocket;
Expand All @@ -15,23 +15,28 @@ fn main() -> Result<()> {
let (_stream, stream_handle) = OutputStream::try_default()?;

loop {
if let Err(err) = {
// Should be more than enough.
let mut buf = [0; 1000];
let (number_of_bytes, _) = socket.recv_from(&mut buf)?;

let event: Event = bincode::deserialize(&buf)?;
println!("Received an event ({number_of_bytes} bytes): {:?}", event);

// FIXME: do the decoding and file reading outside the loop
let file = BufReader::new(File::open("src/sound_samples/click.wav")?);
let source = Decoder::new(file)?;
stream_handle.play_raw(source.convert_samples())?;

Ok::<(), eyre::Error>(())
} {
eprintln!("Could not process data-gram: {:?}", err);
if let Err(err) = handle_datagram(&socket, &stream_handle) {
eprintln!(
"Could not process datagram. Ignoring and continuing. {:?}",
err
);
continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: the continue isn't needed anymore.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed in 4b30780

}
}
}

fn handle_datagram(socket: &UdpSocket, output_stream: &OutputStreamHandle) -> Result<()> {
// Size up to max normal network packet size
let mut buf = [0; 1500];
let (number_of_bytes, _) = socket.recv_from(&mut buf)?;

let event: Event = bincode::deserialize(&buf)?;
strohel marked this conversation as resolved.
Show resolved Hide resolved
println!("Received an event ({number_of_bytes} bytes): {:?}", event);

// FIXME: do the decoding and file reading outside the loop
let file = BufReader::new(File::open("src/sound_samples/click.wav")?);
let source = Decoder::new(file)?;
output_stream.play_raw(source.convert_samples())?;

Ok(())
}
4 changes: 2 additions & 2 deletions crates/test_probe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ version = "0.1.0"
edition = "2021"

[dependencies]
color-eyre = "0.6.2"
color-eyre = "0.6"
composer = { path = "../composer" }
eyre = "0.6.8"
eyre = "0.6"
35 changes: 16 additions & 19 deletions crates/test_probe/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,24 @@ fn main() -> Result<()> {

// Get server address, if given
let args: Vec<String> = env::args().collect();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: not super urgent, but I like using clap for all the argument handling so we get all the bells and whistles like usage strings, --help menu, etc :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do this in a sperate PR. I filled an issue for myself #16

let arg = args.get(1);
let server_address: Option<&str> = arg.map(|x: &String| x.as_str());
let client = match args.get(1) {
Some(address) => Client::new(address)?,
None => Client::try_default()?,
};

let client = Client::new(server_address)?;
let event = Event::TestTick;
let delays = {
let min_delay = 5;
let max_delay = 250;
let step = 10;
// Tick fast, then slow, and then fast again
(min_delay..max_delay)
.step_by(step)
.chain((min_delay..max_delay).step_by(step).rev())
};

loop {
for delay in delays.clone() {
if let Err(err) = client.send(&event) {
eprintln!("Could not send event {:?}", err)
};
thread::sleep(Duration::from_millis(delay.try_into().unwrap()));
}
// u64 taken by `from_millis` doesn't implement DoubleEndedIterator needed by `rev`
// Use u32 explicitly and convert to u64.
let slowdown = (5u32..200).step_by(5);
let speedup = slowdown.clone().rev();

for delay_ms in speedup.chain(slowdown).cycle() {
if let Err(err) = client.send(&event) {
eprintln!("Could not send event {:?}", err)
};
thread::sleep(Duration::from_millis(delay_ms.into()));
}

unreachable!()
strohel marked this conversation as resolved.
Show resolved Hide resolved
}