Use traits and implement them

This commit is contained in:
Silvio Braendle 2025-01-27 12:09:02 +01:00
parent 6c74513457
commit 4c801733c4
No known key found for this signature in database
GPG key ID: 553A5A37BA69D8AE
4 changed files with 29 additions and 15 deletions

View file

@ -8,9 +8,7 @@ pub struct BindError {
type Result<T> = std::result::Result<T, BindError>; type Result<T> = std::result::Result<T, BindError>;
pub struct ClientHandler; pub trait ClientHandler {
impl ClientHandler {
fn handle_message(msg: Message) { fn handle_message(msg: Message) {
match msg { match msg {
Message::PlaySound(play_sound) => { Message::PlaySound(play_sound) => {
@ -18,8 +16,18 @@ impl ClientHandler {
} }
} }
} }
}
pub fn listen(port: u16) -> Result<()> { pub trait Listener : ClientHandler {
fn listen(port: u16) -> Result<()>;
}
pub struct NetworkListener;
impl ClientHandler for NetworkListener {}
impl Listener for NetworkListener {
fn listen(port: u16) -> Result<()> {
let bind_addr = format!("0.0.0.0:{}", port); let bind_addr = format!("0.0.0.0:{}", port);
let socket = UdpSocket::bind(bind_addr); let socket = UdpSocket::bind(bind_addr);
let socket = match socket { let socket = match socket {
@ -39,7 +47,7 @@ impl ClientHandler {
println!("Received {} bytes from {}", amt, src); println!("Received {} bytes from {}", amt, src);
let data = &mut data[..amt]; let data = &mut data[..amt];
let msg = bincode::deserialize::<Message>(data).expect("Failed to deserialize"); let msg = bincode::deserialize::<Message>(data).expect("Failed to deserialize");
ClientHandler::handle_message(msg); NetworkListener::handle_message(msg);
} }
} }
} }

View file

@ -8,20 +8,26 @@ use rodio::{Decoder, Source};
use crate::protocol::{Message, PlaySound}; use crate::protocol::{Message, PlaySound};
pub struct Dispatcher; pub trait Dispatcher {
fn handle_dispatch(addrs: Vec<String>, sound_path: String);
}
impl Dispatcher { pub struct NetworkDispatcher;
impl NetworkDispatcher {
fn load_sound(path: String) -> Decoder<BufReader<File>> { fn load_sound(path: String) -> Decoder<BufReader<File>> {
let file = BufReader::new(File::open(path).unwrap()); let file = BufReader::new(File::open(path).unwrap());
Decoder::new(file).unwrap() Decoder::new(file).unwrap()
} }
}
pub fn handle_dispatch_sample(addrs: Vec<String>, sound_path: String) { impl Dispatcher for NetworkDispatcher {
fn handle_dispatch(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();
let source = Dispatcher::load_sound(sound_path).buffered(); let source = NetworkDispatcher::load_sound(sound_path).buffered();
let channels = source.channels(); let channels = source.channels();
let sample_rate = source.sample_rate(); let sample_rate = source.sample_rate();

View file

@ -1,6 +1,6 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use client_handler::ClientHandler; use client_handler::{Listener, NetworkListener};
use std::thread; use std::thread;
use ui_app::UIApp; use ui_app::UIApp;
@ -20,13 +20,13 @@ fn main() -> eframe::Result {
}; };
thread::spawn(move || { thread::spawn(move || {
let result = ClientHandler::listen(3000); let result = NetworkListener::listen(3000);
match result { match result {
Ok(_) => {} Ok(_) => {}
Err(e) => { Err(e) => {
eprintln!("Failed to bind: {}", e.message); eprintln!("Failed to bind: {}", e.message);
// Try with port 3001 // Try with port 3001
let result = ClientHandler::listen(3001); let result = NetworkListener::listen(3001);
match result { match result {
Ok(_) => {} Ok(_) => {}
Err(e) => { Err(e) => {

View file

@ -1,4 +1,4 @@
use crate::dispatcher::Dispatcher; use crate::dispatcher::{Dispatcher, NetworkDispatcher};
use eframe::egui; use eframe::egui;
use rfd::FileDialog; use rfd::FileDialog;
use std::path::PathBuf; use std::path::PathBuf;
@ -30,7 +30,7 @@ impl eframe::App for UIApp {
ui.text_edit_singleline(address); ui.text_edit_singleline(address);
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( NetworkDispatcher::handle_dispatch(
vec![address.clone()], vec![address.clone()],
path.to_str().unwrap().to_owned(), path.to_str().unwrap().to_owned(),
); );
@ -66,7 +66,7 @@ impl eframe::App for UIApp {
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( NetworkDispatcher::handle_dispatch(
self.addresses.clone(), self.addresses.clone(),
path.to_str().unwrap().to_owned(), path.to_str().unwrap().to_owned(),
); );