diff --git a/src/modules/states_io_handler.rs b/src/modules/states_io_handler.rs deleted file mode 100644 index 4fbac8f..0000000 --- a/src/modules/states_io_handler.rs +++ /dev/null @@ -1,83 +0,0 @@ -use std::{sync::mpsc::{Sender, self, Receiver}, thread::{JoinHandle, self}}; - -use super::{message_handler::{MessageHandler, StateMessage}, stream_states::{stream_states_class::StreamState}}; - - -pub struct StatesIOHandler { - pub listener_join_handler: Option>, - pub message_thread_tx: Option>, - data_rx: Option>, -} - -impl StatesIOHandler { - pub fn new() -> Self { - StatesIOHandler{ - ..Default::default() - } - } - - pub fn start_socket_listener(&mut self, mut message_handler: impl MessageHandler + Send + 'static) { - let (tx, rx) = mpsc::channel::(); - let (tx_1, rx_1) = mpsc::channel::(); - self.message_thread_tx = Some(tx); - self.data_rx = Some(rx_1); - - let handle = thread::spawn(move || { - for message in rx { - match message { - StateMessage::StateUpdate(message) => { - message_handler.handle_update(message); - }, - StateMessage::GetStates => { - tx_1.send(message_handler.get_states()).unwrap(); - } - StateMessage::CloseListener => { - tx_1.send(message_handler.get_states()).unwrap(); - break; - } - } - } - - }); - self.listener_join_handler = Some(handle); - } - - pub fn get_states(&self) -> StreamState { - let tx = self.message_thread_tx.clone(); - match tx { - Some(tx) => { - tx.send(StateMessage::GetStates).unwrap(); - let rx = self.data_rx.as_ref(); - match rx { - Some(rx) => { - let message = rx.recv().unwrap(); - message - }, - None => {panic!("Trying to get data_rx before IOHandler has it!");} - } - }, - None => {panic!("trying to access states before IOHandler has a handle for transmitting");} - } - } - - pub fn close(self) -> StreamState { - let state = self.get_states(); - assert_eq!(self.listener_join_handler.and_then(|f| { - let tx = self.message_thread_tx.clone().unwrap(); - tx.send(StateMessage::CloseListener).unwrap(); - f.join().unwrap(); - Some(true) - }).unwrap(), true); - state - } -} - -impl Default for StatesIOHandler { - fn default() -> Self { - StatesIOHandler { - message_thread_tx: None, - listener_join_handler: None, - data_rx: None, - } - } -} \ No newline at end of file diff --git a/src/tests/states_io_handler_tests.rs b/src/tests/states_io_handler_tests.rs deleted file mode 100644 index 0555418..0000000 --- a/src/tests/states_io_handler_tests.rs +++ /dev/null @@ -1,67 +0,0 @@ -use std::thread; - -use crate::modules::{states_io_handler::{StatesIOHandler}, message_handler::{StateMessage}, stream_states::{stream_states_class::StreamState, state_update::StateUpdate}}; - - -#[test] -fn test_make_socket() { - let state = StreamState::new(); - let mut io_handler = StatesIOHandler::new(); - - io_handler.start_socket_listener(state); - - let tx = io_handler.message_thread_tx.clone().unwrap(); - tx.send(StateMessage::StateUpdate(StateUpdate::SceneIsAugmented(true))).unwrap(); - tx.send(StateMessage::StateUpdate(StateUpdate::StreamSoundToggleOn(true))).unwrap(); - thread::sleep(std::time::Duration::from_millis(1000)); - - let final_state = io_handler.close(); - assert_eq!(final_state.scene_is_augmented, true); - assert_eq!(final_state.stream_is_muted, true); -} - -#[test] -fn send_on_multiple_threads() { - let state = StreamState::new(); - let mut io_handler = StatesIOHandler::new(); - - io_handler.start_socket_listener(state); - - let tx1 = io_handler.message_thread_tx.clone().unwrap(); - let tx2 = io_handler.message_thread_tx.clone().unwrap(); - let tx3 = io_handler.message_thread_tx.clone().unwrap(); - - thread::spawn( move || { - tx1.send(StateMessage::StateUpdate(StateUpdate::TimerCanRun(false))).unwrap(); - }); - thread::spawn(move || { - tx2.send(StateMessage::StateUpdate(StateUpdate::ChangeSceneOnChangeSlide(false))).unwrap(); - }); - thread::spawn(move || { - tx3.send(StateMessage::StateUpdate(StateUpdate::StreamSoundToggleOn(false))).unwrap(); - tx3.send(StateMessage::StateUpdate(StateUpdate::StreamSoundToggleOn(true))).unwrap(); - }); - - thread::sleep(std::time::Duration::from_millis(1000)); - - let final_state = io_handler.close(); - - assert_eq!(final_state.timer_can_run, false); - assert_eq!(final_state.change_scene_on_change_slide_hotkey, false); - assert_eq!(final_state.stream_is_muted, true); -} - -#[test] -#[should_panic] -fn io_handler_not_started_1() { - let io_handler = StatesIOHandler::new(); - - let _state = io_handler.get_states(); -} - -#[test] -#[should_panic] -fn io_handler_not_started_2() { - let io_handler = StatesIOHandler::new(); - let _state = io_handler.close(); -} \ No newline at end of file