Init Event Module #4
@ -1,4 +1,4 @@
|
|||||||
pub mod events;
|
|
||||||
pub mod event_man;
|
pub mod event_man;
|
||||||
|
pub mod events;
|
||||||
pub mod executable;
|
pub mod executable;
|
||||||
pub mod objects;
|
pub mod objects;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use std::collections::HashMap;
|
|
||||||
use crate::core::events::{Event, EventRaw, GroupId};
|
use crate::core::events::{Event, EventRaw, GroupId};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Hash, Copy, Clone)]
|
#[derive(PartialEq, Eq, Hash, Copy, Clone)]
|
||||||
enum ListenerType {
|
enum ListenerType {
|
||||||
@ -7,41 +7,60 @@ enum ListenerType {
|
|||||||
Group(GroupId),
|
Group(GroupId),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait EventRecipient {
|
pub trait EventListener {
|
||||||
type Error;
|
type Error;
|
||||||
fn send_to(&mut self, event: Event) -> Result<(), Self::Error>;
|
fn send_to(&mut self, event: Event) -> Result<(), Self::Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Listener<E> {
|
struct Listener<E> {
|
||||||
_ltype: ListenerType,
|
_ltype: ListenerType,
|
||||||
dest: Box<dyn EventRecipient<Error=E>>,
|
dest: Box<dyn EventListener<Error = E>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ReceivesEvent {
|
pub trait ReceivesAllEvent {
|
||||||
fn receive(&mut self) -> Option<Event>;
|
fn receive(&mut self) -> Option<Event>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub struct EventManager<E> {
|
pub struct EventManager<E> {
|
||||||
listeners: HashMap<ListenerType, Vec<Listener<E>>>,
|
listeners: HashMap<ListenerType, Vec<Listener<E>>>,
|
||||||
event_receiver: dyn ReceivesEvent,
|
event_receiver: Box<dyn ReceivesAllEvent>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E> EventManager<E> {
|
impl<E> EventManager<E> {
|
||||||
pub fn subcribe_single(&mut self, event: Event, dest: impl EventRecipient<Error=E> + 'static) {
|
pub fn new(event_receiver: Box<dyn ReceivesAllEvent>) -> Self {
|
||||||
|
EventManager {
|
||||||
|
listeners: HashMap::new(),
|
||||||
|
event_receiver,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn subcribe_single(&mut self, event: Event, dest: impl EventListener<Error = E> + 'static) {
|
||||||
self.update_listeners(ListenerType::Single(event.raw()), dest);
|
self.update_listeners(ListenerType::Single(event.raw()), dest);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn subscribe_group(&mut self, group_id: GroupId, dest: impl EventRecipient<Error=E> + 'static) {
|
pub fn subscribe_group(
|
||||||
|
&mut self,
|
||||||
|
group_id: GroupId,
|
||||||
|
dest: impl EventListener<Error = E> + 'static,
|
||||||
|
) {
|
||||||
self.update_listeners(ListenerType::Group(group_id), dest);
|
self.update_listeners(ListenerType::Group(group_id), dest);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_listeners (&mut self, key: ListenerType, dest: impl EventRecipient<Error=E> + 'static) {
|
fn update_listeners(
|
||||||
|
&mut self,
|
||||||
|
key: ListenerType,
|
||||||
|
dest: impl EventListener<Error = E> + 'static,
|
||||||
|
) {
|
||||||
if let std::collections::hash_map::Entry::Vacant(e) = self.listeners.entry(key) {
|
if let std::collections::hash_map::Entry::Vacant(e) = self.listeners.entry(key) {
|
||||||
e.insert(vec![Listener { _ltype: key, dest: Box::new(dest) }]);
|
e.insert(vec![Listener {
|
||||||
|
_ltype: key,
|
||||||
|
dest: Box::new(dest),
|
||||||
|
}]);
|
||||||
} else {
|
} else {
|
||||||
let vec = self.listeners.get_mut(&key).unwrap();
|
let vec = self.listeners.get_mut(&key).unwrap();
|
||||||
vec.push(Listener { _ltype: key, dest: Box::new(dest) });
|
vec.push(Listener {
|
||||||
|
_ltype: key,
|
||||||
|
dest: Box::new(dest),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,8 +96,58 @@ impl<E> EventManager<E> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use super::{EventListener, ReceivesAllEvent};
|
||||||
|
use crate::core::event_man::EventManager;
|
||||||
|
use crate::core::events::{Event, Severity};
|
||||||
|
use std::fmt::Error;
|
||||||
|
use std::sync::mpsc::{channel, Receiver, SendError, Sender};
|
||||||
|
|
||||||
|
struct EventReceiver {
|
||||||
|
mpsc_receiver: Receiver<Event>,
|
||||||
|
}
|
||||||
|
impl ReceivesAllEvent for EventReceiver {
|
||||||
|
fn receive(&mut self) -> Option<Event> {
|
||||||
|
self.mpsc_receiver.recv().ok()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct MpscEventSenderQueue {
|
||||||
|
mpsc_sender: Sender<Event>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventListener for MpscEventSenderQueue {
|
||||||
|
type Error = SendError<Event>;
|
||||||
|
|
||||||
|
fn send_to(&mut self, event: Event) -> Result<(), Self::Error> {
|
||||||
|
self.mpsc_sender.send(event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn basic_test() {
|
fn basic_test() {
|
||||||
|
let (event_sender, manager_queue) = channel();
|
||||||
|
let event_man_receiver = EventReceiver {
|
||||||
|
mpsc_receiver: manager_queue,
|
||||||
|
};
|
||||||
|
let mut event_man: EventManager<SendError<Event>> =
|
||||||
|
EventManager::new(Box::new(event_man_receiver));
|
||||||
|
let sender_0 = event_sender.clone();
|
||||||
|
let event_grp_0 = Event::new(Severity::INFO, 0, 0).unwrap();
|
||||||
|
let event_grp_1_0 = Event::new(Severity::HIGH, 1, 0).unwrap();
|
||||||
|
let event_grp_1_1 = Event::new(Severity::MEDIUM, 1, 32).unwrap();
|
||||||
|
let event_grp_1_2 = Event::new(Severity::LOW, 1, 43).unwrap();
|
||||||
|
let event_grp_2 = Event::new(Severity::HIGH, 32, 0).unwrap();
|
||||||
|
let (single_event_sender, single_event_receiver) = channel();
|
||||||
|
let single_event_listener = MpscEventSenderQueue {
|
||||||
|
mpsc_sender: single_event_sender,
|
||||||
|
};
|
||||||
|
let (group_event_sender, group_event_receiver) = channel();
|
||||||
|
let group_event_listener = MpscEventSenderQueue {
|
||||||
|
mpsc_sender: group_event_sender,
|
||||||
|
};
|
||||||
|
event_man.subscribe_group(event_grp_1_0.group_id(), group_event_listener);
|
||||||
|
event_sender.send(event_grp_0).expect("Send error occured");
|
||||||
|
// let event = single_event_receiver.recv().expect("Error receiving event");
|
||||||
|
// println!("{:?}", event);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,7 +2,6 @@ use launchpad::core::events::{Event, Severity};
|
|||||||
use std::sync::mpsc::channel;
|
use std::sync::mpsc::channel;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
basic_event_test();
|
basic_event_test();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user