From 58f18e5cfdc8dee149edcda336173b5476ad6979 Mon Sep 17 00:00:00 2001 From: cediackermann Date: Mon, 13 Jan 2025 15:00:54 +0100 Subject: [PATCH] Add support for multiple devices --- src/dispatcher.rs | 17 +++++++++++++---- src/ui_app.rs | 40 +++++++++++++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/dispatcher.rs b/src/dispatcher.rs index 7e01d9a..abcc54b 100644 --- a/src/dispatcher.rs +++ b/src/dispatcher.rs @@ -14,7 +14,7 @@ impl Dispatcher { data } - pub fn handle_dispatch_sample(addr: String, sound_path: String) { + pub fn handle_dispatch_sample(addrs: Vec, sound_path: String) { let now = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .unwrap() @@ -25,8 +25,17 @@ impl Dispatcher { timestamp: timestamp, sound_data: sound_data, }); - let mut sock = TcpStream::connect(addr).expect("Failed to connect"); - let buf = bincode::serialize(&msg).expect("Failed to serialize"); - sock.write_all(&buf).unwrap(); + for addr in addrs { + if addr.is_empty() { + 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(); + } + + } + } } diff --git a/src/ui_app.rs b/src/ui_app.rs index af3acb6..75490f6 100644 --- a/src/ui_app.rs +++ b/src/ui_app.rs @@ -7,7 +7,7 @@ use crate::dispatcher::Dispatcher; pub struct UIApp { file_dialog: FileDialog, picked_file: Option, - address: String, + addresses: Vec, } impl Default for UIApp { @@ -15,18 +15,42 @@ impl Default for UIApp { Self { file_dialog: FileDialog::new(), picked_file: None, - address: "127.0.0.1:3000".to_string(), + addresses: Vec::new(), } } } impl eframe::App for UIApp { 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| { - ui.horizontal(|ui|{ - ui.label("Enter IP of target device: "); - ui.text_edit_singleline(&mut self.address); - }); + let mut to_remove = Vec::new(); + for (i, address) in self.addresses.iter_mut().enumerate() { + 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() { self.file_dialog.pick_file(); } @@ -35,9 +59,11 @@ impl eframe::App for UIApp { if let Some(path) = self.file_dialog.take_picked() { self.picked_file = Some(path.to_path_buf()); } + ui.separator(); + if ui.button("Play sound").clicked() { 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()); } }