Rework architecture
This commit is contained in:
parent
e3d207f343
commit
2cbbaf0440
6 changed files with 65 additions and 19 deletions
|
|
@ -1,26 +1,28 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
network_dispatcher::NetworkDispatcher, network_handler::NetworkHandler,
|
network_dispatcher::NetworkDispatcher, network_handler::NetworkHandler,
|
||||||
sound_engine::SoundEngine,
|
sound_engine::SoundEngine,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct AppService {
|
pub struct AppService {
|
||||||
sound_engine: SoundEngine,
|
|
||||||
network_handler: NetworkHandler,
|
network_handler: NetworkHandler,
|
||||||
network_dispatcher: NetworkDispatcher,
|
network_dispatcher: NetworkDispatcher,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppService {
|
impl AppService {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
|
let sound_engine = Arc::new(SoundEngine::new());
|
||||||
Self {
|
Self {
|
||||||
sound_engine: SoundEngine::new(),
|
network_handler: NetworkHandler::new(sound_engine.clone()),
|
||||||
network_handler: NetworkHandler::new(),
|
|
||||||
network_dispatcher: NetworkDispatcher::new(),
|
network_dispatcher: NetworkDispatcher::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dispatch_sample(&self) {
|
pub fn dispatch_sample(&self) {
|
||||||
self.network_dispatcher
|
self.network_dispatcher
|
||||||
.dispatch_sound("localhost:3000".to_owned(), 1, "sound".to_owned());
|
.dispatch_sound("localhost:3000".to_owned(), 1, "sound".to_owned())
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn listen(&self) {
|
pub fn listen(&self) {
|
||||||
|
|
|
||||||
BIN
src/audios/augh.m4a
Normal file
BIN
src/audios/augh.m4a
Normal file
Binary file not shown.
|
|
@ -9,13 +9,13 @@ impl NetworkDispatcher {
|
||||||
Self {}
|
Self {}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dispatch_sound(&self, addr: String, timestamp: u64, sound_source: String) {
|
pub fn dispatch_sound(&self, addr: String, timestamp: u128, sound_source: String) -> std::io::Result<()> {
|
||||||
let msg = Message::PlaySound(PlaySound {
|
let msg = Message::PlaySound(PlaySound {
|
||||||
timestamp: timestamp,
|
timestamp: timestamp,
|
||||||
sound_source: sound_source,
|
sound_source: sound_source,
|
||||||
});
|
});
|
||||||
let mut sock = TcpStream::connect(addr).expect("Failed to connect");
|
let mut sock = TcpStream::connect(addr).expect("Failed to connect");
|
||||||
let buf = bincode::serialize(&msg).expect("Failed to serialize");
|
let buf = bincode::serialize(&msg).expect("Failed to serialize");
|
||||||
sock.write_all(&buf);
|
sock.write_all(&buf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,55 @@
|
||||||
use std::{io::Read, net::{TcpListener, TcpStream}};
|
use std::{
|
||||||
|
io::Read,
|
||||||
|
net::{TcpListener, TcpStream},
|
||||||
|
sync::Arc,
|
||||||
|
thread::sleep,
|
||||||
|
time::{Duration, SystemTime, UNIX_EPOCH},
|
||||||
|
};
|
||||||
|
|
||||||
use crate::protocol::{Message, PlaySound};
|
use crate::{
|
||||||
|
protocol::{Message, PlaySound},
|
||||||
|
sound_engine::SoundEngine,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct NetworkHandler {}
|
pub struct NetworkHandler {
|
||||||
|
sound_engine: Arc<SoundEngine>,
|
||||||
|
}
|
||||||
|
|
||||||
impl NetworkHandler {
|
impl NetworkHandler {
|
||||||
pub fn new() -> Self {
|
pub fn new(sound_engine: Arc<SoundEngine>) -> Self {
|
||||||
Self {}
|
Self { sound_engine }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_scheduled_sound(&self, play_msg: PlaySound) {
|
||||||
|
let PlaySound {
|
||||||
|
sound_source,
|
||||||
|
timestamp,
|
||||||
|
} = play_msg;
|
||||||
|
println!("Scheduled sound: {:?} at {:?}", sound_source, timestamp);
|
||||||
|
let now_micros = SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.expect("Could not get time")
|
||||||
|
.as_micros();
|
||||||
|
let wait = timestamp as i128 - now_micros as i128;
|
||||||
|
if wait <= 0 {
|
||||||
|
println!("Too late to play sound");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sleep(Duration::from_micros(wait as u64));
|
||||||
|
println!("Playing sound: {:?}", sound_source);
|
||||||
|
self.sound_engine.play_sound(sound_source);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_client(&self, mut stream: TcpStream) {
|
fn handle_client(&self, mut stream: TcpStream) {
|
||||||
println!("Handling client: {:?}", stream);
|
println!("Handling client: {:?}", stream);
|
||||||
let mut buf: Vec<u8> = Vec::new();
|
let mut buf: Vec<u8> = Vec::new();
|
||||||
stream.read_to_end(buf.as_mut()).expect("Could not read from stream");
|
stream
|
||||||
|
.read_to_end(buf.as_mut())
|
||||||
|
.expect("Could not read from stream");
|
||||||
let message = bincode::deserialize::<Message>(&buf).expect("Could not deserialize message");
|
let message = bincode::deserialize::<Message>(&buf).expect("Could not deserialize message");
|
||||||
match message {
|
match message {
|
||||||
Message::PlaySound(play_sound) => {
|
Message::PlaySound(play_sound) => {
|
||||||
let PlaySound { sound_source, timestamp } = play_sound;
|
self.handle_scheduled_sound(play_sound);
|
||||||
println!("Playing sound: {:?} at {:?}", sound_source, timestamp);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,6 @@ pub enum Message {
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct PlaySound {
|
pub struct PlaySound {
|
||||||
pub timestamp: u64,
|
pub timestamp: u128,
|
||||||
pub sound_source: String,
|
pub sound_source: String,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,23 @@
|
||||||
pub struct SoundEngine {}
|
use std::{fs::File, io::BufReader};
|
||||||
|
|
||||||
|
use rodio::{Decoder, OutputStream, OutputStreamHandle, Source};
|
||||||
|
|
||||||
|
pub struct SoundEngine {
|
||||||
|
stream_handle: OutputStreamHandle,
|
||||||
|
}
|
||||||
|
|
||||||
impl SoundEngine {
|
impl SoundEngine {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {}
|
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
|
||||||
|
Self { stream_handle }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn play_sound(&self) {
|
pub fn play_sound(&self, sound_source: String) {
|
||||||
println!("Playing sound");
|
let path = format!("./audios/{}", sound_source);
|
||||||
|
let file = BufReader::new(File::open(path).unwrap());
|
||||||
|
let source = Decoder::new(file).unwrap();
|
||||||
|
self.stream_handle
|
||||||
|
.play_raw(source.convert_samples())
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue