diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..76417a2 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,45 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'church_controller'", + "cargo": { + "args": [ + "build", + "--bin=church_controller", + "--package=church_controller" + ], + "filter": { + "name": "church_controller", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'church_controller'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=church_controller", + "--package=church_controller" + ], + "filter": { + "name": "church_controller", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e4453ab..2b8f4cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,7 +50,7 @@ fn main() { while !control_c_called_flag_rx.get() { match from_socket_rx.recv_timeout(Duration::from_millis(100)) { Ok(message) => { - println!("{}", message); + println!("main recieved: {}", message); let json = serde_json::from_str(&message).unwrap(); let update = StateUpdate::json_to_state_update(json); if update == StateUpdate::UpdateClient { @@ -94,6 +94,7 @@ fn setup_control_c(mut control_c_flag_tx: sync_flag::SyncFlagTx) { } fn update_all(state: &StreamState, socket: &Socket) { + println!("updating all"); socket.send(StateUpdate::StreamRunning(state.stream_running).to_json().to_string()); socket.send(StateUpdate::StreamSoundToggleOn(state.stream_is_muted).to_json().to_string()); socket.send(StateUpdate::ToggleComputerSoundOn(state.computer_sound_is_on).to_json().to_string()); diff --git a/src/modules/external_interface.rs b/src/modules/external_interface.rs index 9657c87..ff68058 100644 --- a/src/modules/external_interface.rs +++ b/src/modules/external_interface.rs @@ -106,7 +106,7 @@ impl Hotkeys { }; } - pub fn toggle_media_play_pause(&self, _value: bool) { + pub fn toggle_media_play_pause(&self) { if cfg!(target_os = "windows") { Command::new(String::from(AHK_FILES_FOLDER) + "music_toggle.exe") .arg(self.hotkeys["windows"]["propresenter_re"].to_string()) @@ -139,5 +139,5 @@ fn hotkeys() { hk.send_obs(String::from("a hotkey")); hk.toggle_computer_sound(true); hk.toggle_stream_sound(true); - hk.toggle_media_play_pause(false); + hk.toggle_media_play_pause(); } \ No newline at end of file diff --git a/src/modules/message_handler.rs b/src/modules/message_handler.rs index a2cebf3..2955bfb 100644 --- a/src/modules/message_handler.rs +++ b/src/modules/message_handler.rs @@ -12,7 +12,10 @@ pub trait MessageHandler { //the first one goes to impl MessageHandler for StreamState { fn handle_update(&mut self, update: StateUpdate, hotkey_handler: &Hotkeys) -> (Option, Option>) { - self.update(update.clone()); + + if update != StateUpdate::UpdateClient{ + self.update(update.clone()); + } if self.debug_mode { return (None, None) @@ -80,7 +83,7 @@ impl MessageHandler for StreamState { }, StateUpdate::StreamSoundToggleOn(value) => {hotkey_handler.toggle_stream_sound(value); return (Some(update), None)}, StateUpdate::ToggleComputerSoundOn(value) => {hotkey_handler.toggle_computer_sound(value); return (Some(update), None)}, - StateUpdate::ComputerMediaDoPause(value) => {hotkey_handler.toggle_media_play_pause(value); return (Some(update), None)}, + StateUpdate::ComputerMediaDoPause => {hotkey_handler.toggle_media_play_pause(); return (Some(update), None)}, StateUpdate::UpdateClient => {}, StateUpdate::StreamRunning(_) => {}, //_ => {} diff --git a/src/modules/socket_handler.rs b/src/modules/socket_handler.rs index bdf6652..6e5e5a4 100644 --- a/src/modules/socket_handler.rs +++ b/src/modules/socket_handler.rs @@ -35,7 +35,8 @@ impl Socket { let mut streams = thread_owned_streams.lock().unwrap(); streams.push(Arc::clone(&stream)); //pass off a clone of the thread-passable pointer - Socket::handle_client(stream.as_ref(), messenger_tx.clone(), thread_stop_flag.clone()); + drop(streams); + Socket::handle_client(Arc::clone(&stream), messenger_tx.clone(), thread_stop_flag.clone()); } thread::sleep(Duration::from_millis(100)); } @@ -50,11 +51,11 @@ impl Socket { } } - pub fn handle_client(mut stream: &TcpStream, update_tx: Sender, program_shutdown_flag: sync_flag::SyncFlagRx) { + pub fn handle_client(stream: Arc, update_tx: Sender, program_shutdown_flag: sync_flag::SyncFlagRx) { let mut buffer = [0; 1024]; stream.set_read_timeout(Some(Duration::from_millis(100))).expect("Could not set a read timeout"); while program_shutdown_flag.get() { - match stream.read(&mut buffer) { + match stream.as_ref().read(&mut buffer) { Err(_) => {}, Ok(read_size) => { //Tcp is supposed to have a 0 byte read if closed by client @@ -79,11 +80,16 @@ impl Socket { } pub fn send(&self, message: String) { - let streams = self.socket_txs.lock().unwrap(); - for socket_tx in streams.iter(){ - let mut tx = socket_tx.as_ref(); - tx.write(message.clone().as_bytes()).unwrap(); + let mut streams = self.socket_txs.lock().unwrap(); + for i in 0..streams.len(){ + let mut tx = streams.get(i).unwrap().as_ref(); + + match tx.write(message.clone().as_bytes()) { + Err(_) => {streams.remove(i); continue;}, + Ok(_) => {}, + } tx.flush().unwrap(); + println!("sent"); } } } \ No newline at end of file diff --git a/src/modules/stream_states/state_update.rs b/src/modules/stream_states/state_update.rs index b26509a..8670357 100644 --- a/src/modules/stream_states/state_update.rs +++ b/src/modules/stream_states/state_update.rs @@ -8,7 +8,7 @@ pub enum StateUpdate { StreamRunning(bool), StreamSoundToggleOn(bool), ToggleComputerSoundOn(bool), - ComputerMediaDoPause(bool), + ComputerMediaDoPause, ChangeSceneOnChangeSlide(bool), SceneIsAugmented(bool), TimerCanRun(bool), @@ -66,7 +66,7 @@ impl StateUpdate { //Extra Toggles "Toggle_Computer_Volume" => {StateUpdate::ToggleComputerSoundOn(string_to_bool(incoming_json["data"].as_str().unwrap()))}, "Toggle_Stream_Volume" => {StateUpdate::StreamSoundToggleOn(string_to_bool(incoming_json["data"].as_str().unwrap()))}, - "Media_Pause_Play" => {StateUpdate::ComputerMediaDoPause(string_to_bool(incoming_json["data"].as_str().unwrap()))}, + "Media_Pause_Play" => {StateUpdate::ComputerMediaDoPause}, "Timer_Text" => {StateUpdate::TimerText(incoming_json["data"].as_str().unwrap().to_string())} "all" => {StateUpdate::UpdateClient}, @@ -107,8 +107,8 @@ impl StateUpdate { ("SubScene", scene.to_string())}, StateUpdate::Scene(scene) => { ("Scene", scene.to_string())}, - StateUpdate::ComputerMediaDoPause(is_true) => { - ("Toggle_Computer_Volume", is_true.to_string())}, + StateUpdate::ComputerMediaDoPause => { + ("Toggle_Computer_Volume", "".to_string())}, StateUpdate::ChangeSlide(value) => { match value { SlideChange::Next => {("Next_Slide", "".to_string())}, diff --git a/src/modules/stream_states/stream_states_class.rs b/src/modules/stream_states/stream_states_class.rs index d9612d1..9f627ab 100644 --- a/src/modules/stream_states/stream_states_class.rs +++ b/src/modules/stream_states/stream_states_class.rs @@ -69,9 +69,9 @@ impl StreamState { StateUpdate::SceneIsAugmented(new_val) => { self.scene_is_augmented = new_val; self.change_scene(&Scenes::Augmented)}, - StateUpdate::UpdateClient => todo!(), - StateUpdate::ComputerMediaDoPause(do_pause) => {self.computer_sound_is_on = do_pause;}, - StateUpdate::ChangeSlide(_value) => {panic!("Stream_states_class is not supposed to get this update type");}, + StateUpdate::ComputerMediaDoPause => {}, + StateUpdate::ChangeSlide(_value) => {panic!("Stream_states_class is not supposed to get this update type");}, + StateUpdate::UpdateClient => {}, } } diff --git a/src/tests/state_update_tests.rs b/src/tests/state_update_tests.rs index 78646d3..50c2645 100644 --- a/src/tests/state_update_tests.rs +++ b/src/tests/state_update_tests.rs @@ -50,11 +50,7 @@ fn test_json_to_state_update() { assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str( "{\"type\": \"update\", \"update\": \"Media_Pause_Play\", \"data\": \"true\"}" - ).unwrap()), StateUpdate::ComputerMediaDoPause(true)); - - assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str( - "{\"type\": \"update\", \"update\": \"Media_Pause_Play\", \"data\": \"false\"}" - ).unwrap()), StateUpdate::ComputerMediaDoPause(false)); + ).unwrap()), StateUpdate::ComputerMediaDoPause); assert_eq!(StateUpdate::json_to_state_update(serde_json::from_str( "{\"type\": \"update\", \"update\": \"SubScene\", \"data\": \"Camera_None\"}"