Add support for multiple devices

This commit is contained in:
cediackermann 2025-01-13 15:00:54 +01:00
parent 949ed2b8c7
commit 58f18e5cfd
2 changed files with 46 additions and 11 deletions

View file

@ -14,7 +14,7 @@ impl Dispatcher {
data data
} }
pub fn handle_dispatch_sample(addr: String, sound_path: String) { pub fn handle_dispatch_sample(addrs: Vec<String>, sound_path: String) {
let now = SystemTime::now() let now = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH) .duration_since(SystemTime::UNIX_EPOCH)
.unwrap() .unwrap()
@ -25,8 +25,17 @@ impl Dispatcher {
timestamp: timestamp, timestamp: timestamp,
sound_data: sound_data, sound_data: sound_data,
}); });
let mut sock = TcpStream::connect(addr).expect("Failed to connect"); for addr in addrs {
let buf = bincode::serialize(&msg).expect("Failed to serialize"); if addr.is_empty() {
sock.write_all(&buf).unwrap(); continue;
} else {
let mut sock = TcpStream::connect(addr).expect("Failed to connect");
let buf = bincode::serialize(&msg).expect("Failed to serialize");
sock.write_all(&buf).unwrap();
}
}
} }
} }

View file

@ -7,7 +7,7 @@ use crate::dispatcher::Dispatcher;
pub struct UIApp { pub struct UIApp {
file_dialog: FileDialog, file_dialog: FileDialog,
picked_file: Option<PathBuf>, picked_file: Option<PathBuf>,
address: String, addresses: Vec<String>,
} }
impl Default for UIApp { impl Default for UIApp {
@ -15,18 +15,42 @@ impl Default for UIApp {
Self { Self {
file_dialog: FileDialog::new(), file_dialog: FileDialog::new(),
picked_file: None, picked_file: None,
address: "127.0.0.1:3000".to_string(), addresses: Vec::new(),
} }
} }
} }
impl eframe::App for UIApp { impl eframe::App for UIApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
if self.addresses.is_empty() {
self.addresses.push("localhost:3000".to_owned());
}
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
ui.horizontal(|ui|{ let mut to_remove = Vec::new();
ui.label("Enter IP of target device: "); for (i, address) in self.addresses.iter_mut().enumerate() {
ui.text_edit_singleline(&mut self.address); ui.horizontal(|ui| {
}); ui.label("Enter IP of target device: ");
ui.text_edit_singleline(address);
if ui.button("Play sound").clicked() {
if let Some(path) = &self.picked_file {
Dispatcher::handle_dispatch_sample(vec![address.clone()], path.to_str().unwrap().to_owned());
}
}
if ui.button("Remove").clicked() {
to_remove.push(i);
}
});
}
for i in to_remove.iter().rev() {
self.addresses.remove(*i);
}
if ui.button("Add address").clicked() {
self.addresses.push("localhost:3000".to_owned());
}
ui.separator();
if ui.button("Pick file").clicked() { if ui.button("Pick file").clicked() {
self.file_dialog.pick_file(); self.file_dialog.pick_file();
} }
@ -35,9 +59,11 @@ impl eframe::App for UIApp {
if let Some(path) = self.file_dialog.take_picked() { if let Some(path) = self.file_dialog.take_picked() {
self.picked_file = Some(path.to_path_buf()); self.picked_file = Some(path.to_path_buf());
} }
ui.separator();
if ui.button("Play sound").clicked() { if ui.button("Play sound").clicked() {
if let Some(path) = &self.picked_file { if let Some(path) = &self.picked_file {
Dispatcher::handle_dispatch_sample(self.address.clone(), path.to_str().unwrap().to_owned()); Dispatcher::handle_dispatch_sample(self.addresses.clone(), path.to_str().unwrap().to_owned());
} }
} }