changed message_handler to be a trait

This commit is contained in:
Nickiel12 2021-12-16 15:39:36 -08:00
parent 2167806355
commit ef37f875ec
4 changed files with 47 additions and 55 deletions

View file

@ -1,31 +1,21 @@
use std::sync::mpsc::{Receiver}; use super::stream_states::{enums::StateUpdate, stream_states_class::StreamState};
use super::stream_states::{stream_states_class::StreamStates, enums::StateUpdate};
pub enum StateMessage { pub enum StateMessage {
StateUpdateContainer(StateUpdate), StateUpdate(StateUpdate),
GetStates,
CloseListener, CloseListener,
} }
pub trait MessageListnerThread { pub trait MessageHandler {
fn listen(self, rx: Receiver<StateMessage>) -> std::thread::JoinHandle<()>; fn handle_update(&mut self, update: StateUpdate) -> ();
fn get_states(&self) -> StreamState;
} }
#[derive(Debug)] impl MessageHandler for StreamState {
pub struct MessageHandler { fn handle_update(&mut self, update: StateUpdate) {
pub state: StreamStates, self.update(update.clone());
} }
fn get_states(&self) -> StreamState{
impl MessageHandler { self.clone()
pub fn new() -> Self {
return MessageHandler {
state: StreamStates::new(),
} }
} }
pub fn handle_update(&mut self, update: StateUpdate) {
self.state.update(update.clone());
}
}

View file

@ -1,7 +1,7 @@
use super::enums::{self, StateUpdate}; use super::enums::{self, StateUpdate};
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct StreamStates { pub struct StreamState {
pub stream_running: bool, pub stream_running: bool,
pub stream_is_muted: bool, pub stream_is_muted: bool,
pub computer_sound_is_on: bool, pub computer_sound_is_on: bool,
@ -18,9 +18,9 @@ pub struct StreamStates {
screen_sub_scene: enums::Scenes, screen_sub_scene: enums::Scenes,
} }
impl Default for StreamStates { impl Default for StreamState {
fn default() -> Self { fn default() -> Self {
StreamStates { StreamState {
stream_running: false, stream_running: false,
stream_is_muted: false, stream_is_muted: false,
computer_sound_is_on: true, computer_sound_is_on: true,
@ -39,9 +39,9 @@ impl Default for StreamStates {
} }
} }
impl StreamStates { impl StreamState {
pub fn new() -> Self { pub fn new() -> Self {
StreamStates{..Default::default()} StreamState{..Default::default()}
} }
pub fn update(&mut self, update: StateUpdate) { pub fn update(&mut self, update: StateUpdate) {
@ -76,10 +76,10 @@ impl StreamStates {
match scene { match scene {
enums::Scenes::CameraDefault | enums::Scenes::CameraWithUpperRight | enums::Scenes::CameraDefault | enums::Scenes::CameraWithUpperRight |
enums::Scenes::CameraWithLargeUpperRight | enums::Scenes::CameraWithLowerRight enums::Scenes::CameraWithLargeUpperRight | enums::Scenes::CameraWithLowerRight
=> {StreamStates::set_camera_scene(self, scene)}, => {StreamState::set_camera_scene(self, scene)},
enums::Scenes::ScreenDefault | enums::Scenes::ScreenWithUpperRight | enums::Scenes::ScreenDefault | enums::Scenes::ScreenWithUpperRight |
enums::Scenes::ScreenWithLowerRight enums::Scenes::ScreenWithLowerRight
=> {StreamStates::set_screen_scene(self, scene)}, => {StreamState::set_screen_scene(self, scene)},
enums::Scenes::Augmented => {self.current_scene = *scene;} enums::Scenes::Augmented => {self.current_scene = *scene;}
} }
} }

View file

@ -1,35 +1,36 @@
use std::{sync::mpsc::{self, Receiver}, thread::{self, JoinHandle}}; use std::{sync::mpsc::{self, Receiver}, thread::{self, JoinHandle}};
use crate::modules::{message_handler::{MessageHandler, StateMessage}, stream_states::{enums::{StateUpdate, Scenes}}}; use crate::modules::{message_handler::{MessageHandler, StateMessage}, stream_states::{enums::{StateUpdate, Scenes}, stream_states_class::StreamState}};
#[test] #[test]
fn create_message_handler() { fn does_stream_state_implement_message_handler() {
MessageHandler::new(); let mut state = StreamState::new();
state.handle_update(StateUpdate::ComputerSoundIsOn(false));
assert_eq!(state.computer_sound_is_on, false);
} }
#[test] #[test]
fn message_hander_can_handle_basic_message() { fn message_hander_can_handle_basic_message() {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
fn listen(mut handler: MessageHandler, rx: Receiver<StateMessage>) -> JoinHandle<()>{ fn listen(mut handler: StreamState, rx: Receiver<StateMessage>) -> JoinHandle<()>{
thread::spawn(move || { thread::spawn(move || {
for message in rx { for message in rx {
match message { match message {
StateMessage::StateUpdateContainer(update) => {handler.handle_update(update)}, StateMessage::StateUpdate(update) => {handler.handle_update(update)},
StateMessage::GetStates => {panic!("Why is getstates being called in this test?");},
StateMessage::CloseListener => {break;}, StateMessage::CloseListener => {break;},
}; };
} }
assert_eq!(handler.state.computer_sound_is_on, false); assert_eq!(handler.computer_sound_is_on, false);
}) })
} }
let handler = MessageHandler::new(); let handler = StreamState::new();
let join_handle = listen(handler, rx); let join_handle = listen(handler, rx);
tx.send(StateMessage::StateUpdateContainer(StateUpdate::ComputerSoundIsOn(false))).unwrap(); tx.send(StateMessage::StateUpdate(StateUpdate::ComputerSoundIsOn(false))).unwrap();
thread::sleep(std::time::Duration::from_millis(1000)); thread::sleep(std::time::Duration::from_millis(1000));
tx.send(StateMessage::CloseListener).unwrap(); tx.send(StateMessage::CloseListener).unwrap();
join_handle.join().unwrap(); join_handle.join().unwrap();
@ -40,29 +41,30 @@ fn message_handler_can_handle_multiple_messages() {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
fn listen(mut handler: MessageHandler, rx: Receiver<StateMessage>) -> JoinHandle<()>{ fn listen(mut handler: StreamState, rx: Receiver<StateMessage>) -> JoinHandle<()>{
thread::spawn(move || { thread::spawn(move || {
for message in rx { for message in rx {
match message { match message {
StateMessage::StateUpdateContainer(update) => {handler.handle_update(update)}, StateMessage::StateUpdate(update) => {handler.handle_update(update)},
StateMessage::GetStates => {panic!("Why is getstates being called in this test?");},
StateMessage::CloseListener => {break;}, StateMessage::CloseListener => {break;},
}; };
} }
assert_eq!(handler.state.computer_sound_is_on, false); assert_eq!(handler.computer_sound_is_on, false);
assert_eq!(handler.state.scene_is_augmented, true); assert_eq!(handler.scene_is_augmented, true);
assert_eq!(handler.state.scene_is_augmented, true); assert_eq!(handler.scene_is_augmented, true);
assert_eq!(handler.state.get_current_scene(), Scenes::Augmented); assert_eq!(handler.get_current_scene(), Scenes::Augmented);
assert_eq!(handler.state.timer_can_run, false); assert_eq!(handler.timer_can_run, false);
}) })
} }
let handler = MessageHandler::new(); let handler = StreamState::new();
let join_handle = listen(handler, rx); let join_handle = listen(handler, rx);
tx.send(StateMessage::StateUpdateContainer(StateUpdate::ComputerSoundIsOn(false))).unwrap(); tx.send(StateMessage::StateUpdate(StateUpdate::ComputerSoundIsOn(false))).unwrap();
tx.send(StateMessage::StateUpdateContainer(StateUpdate::SceneIsAugmented(true))).unwrap(); tx.send(StateMessage::StateUpdate(StateUpdate::SceneIsAugmented(true))).unwrap();
tx.send(StateMessage::StateUpdateContainer(StateUpdate::TimerCanRun(false))).unwrap(); tx.send(StateMessage::StateUpdate(StateUpdate::TimerCanRun(false))).unwrap();
thread::sleep(std::time::Duration::from_millis(1000)); thread::sleep(std::time::Duration::from_millis(1000));
tx.send(StateMessage::CloseListener).unwrap(); tx.send(StateMessage::CloseListener).unwrap();
join_handle.join().unwrap(); join_handle.join().unwrap();

View file

@ -27,7 +27,7 @@ fn has_all_enums() {
#[test] #[test]
fn create_stream_states_class() { fn create_stream_states_class() {
let stream_state = s_s::stream_states_class::StreamStates::new(); let stream_state = s_s::stream_states_class::StreamState::new();
assert_eq!(stream_state.stream_running, false); assert_eq!(stream_state.stream_running, false);
assert_eq!(stream_state.stream_is_muted, false); assert_eq!(stream_state.stream_is_muted, false);
assert_eq!(stream_state.computer_sound_is_on, true); assert_eq!(stream_state.computer_sound_is_on, true);
@ -46,7 +46,7 @@ fn create_stream_states_class() {
#[test] #[test]
fn scene_correctness(){ fn scene_correctness(){
let mut stream_state = s_s::stream_states_class::StreamStates::new(); let mut stream_state = s_s::stream_states_class::StreamState::new();
assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::CameraDefault); assert_eq!(stream_state.get_current_scene(), s_s::enums::Scenes::CameraDefault);
assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraDefault); assert_eq!(stream_state.get_current_camera_sub_scene(), s_s::enums::Scenes::CameraDefault);
assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault); assert_eq!(stream_state.get_current_screen_sub_scene(), s_s::enums::Scenes::ScreenDefault);
@ -91,7 +91,7 @@ fn scene_correctness(){
#[test] #[test]
fn test_updating() { fn test_updating() {
let mut stream_state = s_s::stream_states_class::StreamStates::new(); let mut stream_state = s_s::stream_states_class::StreamState::new();
assert_eq!(stream_state.timer_can_run, true); assert_eq!(stream_state.timer_can_run, true);
stream_state.update(StateUpdate::TimerCanRun(false)); stream_state.update(StateUpdate::TimerCanRun(false));
@ -132,7 +132,7 @@ fn can_run_in_thread() {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
let rx_thread = thread::spawn(move || { let rx_thread = thread::spawn(move || {
let mut stream_state = s_s::stream_states_class::StreamStates::new(); let mut stream_state = s_s::stream_states_class::StreamState::new();
for received in rx { for received in rx {
assert_eq!(received, StateUpdate::StreamRunning(true)); assert_eq!(received, StateUpdate::StreamRunning(true));
stream_state.update(received); stream_state.update(received);