added first event manager

This commit is contained in:
Robin Müller 2022-06-10 17:55:01 +02:00
parent 30bf2ca667
commit 3d5654a5e6
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
2 changed files with 71 additions and 11 deletions

View File

@ -1,6 +1,10 @@
use num::pow; use num::pow;
#[derive(Copy, Clone, PartialEq, Debug)] pub type GroupId = u16;
pub type UniqueId = u16;
pub type EventRaw = u32;
#[derive(Copy, Clone, Debug)]
pub enum Severity { pub enum Severity {
INFO = 1, INFO = 1,
LOW = 2, LOW = 2,
@ -25,8 +29,8 @@ impl TryFrom<u8> for Severity {
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub struct Event { pub struct Event {
severity: Severity, severity: Severity,
group_id: u16, group_id: GroupId,
unique_id: u16, unique_id: UniqueId,
} }
impl Event { impl Event {
@ -41,7 +45,7 @@ impl Event {
/// next 13 bits after the severity. Therefore, the size is limited by dec 8191 hex 0x1FFF. /// next 13 bits after the severity. Therefore, the size is limited by dec 8191 hex 0x1FFF.
/// `unique_id`: Each event has a unique 16 bit ID occupying the last 16 bits of the /// `unique_id`: Each event has a unique 16 bit ID occupying the last 16 bits of the
/// raw event ID /// raw event ID
pub fn new(severity: Severity, group_id: u16, unique_id: u16) -> Option<Event> { pub fn new(severity: Severity, group_id: GroupId, unique_id: UniqueId) -> Option<Event> {
if group_id > (pow::pow(2u8 as u16, 13) - 1) { if group_id > (pow::pow(2u8 as u16, 13) - 1) {
return None; return None;
} }
@ -58,22 +62,22 @@ impl Event {
self.severity self.severity
} }
pub fn group_id(&self) -> u16 { pub fn group_id(&self) -> GroupId {
self.group_id self.group_id
} }
pub fn unique_id(&self) -> u16 { pub fn unique_id(&self) -> UniqueId {
self.unique_id self.unique_id
} }
pub fn raw(&self) -> u32 { pub fn raw(&self) -> EventRaw {
(((self.severity as u32) << 29) as u32 (((self.severity as u32) << 29) as u32
| ((self.group_id as u32) << 16) as u32 | ((self.group_id as u32) << 16) as u32
| self.unique_id as u32) as u32 | self.unique_id as u32) as EventRaw
} }
} }
impl TryFrom<u32> for Event { impl TryFrom<EventRaw> for Event {
type Error = (); type Error = ();
fn try_from(raw: u32) -> Result<Self, Self::Error> { fn try_from(raw: u32) -> Result<Self, Self::Error> {

View File

@ -1,7 +1,63 @@
use launchpad::core::events::{Event, Severity}; use launchpad::core::events::{Event, EventRaw, GroupId, Severity};
use std::sync::mpsc::channel; use launchpad::core::executable::{Executable, ExecutionType, OpResult};
use std::collections::HashMap;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::thread; use std::thread;
#[derive(PartialEq, Eq, Hash, Copy, Clone)]
enum ListenerType {
SINGLE(EventRaw),
GROUP(GroupId),
}
struct Listener {
_ltype: ListenerType,
dest: Sender<Event>,
}
pub struct EventManager {
listeners: HashMap<ListenerType, Vec<Listener>>,
event_receiver: Receiver<Event>,
}
impl Executable for EventManager {
type Error = ();
fn exec_type(&self) -> ExecutionType {
ExecutionType::Infinite
}
fn task_name(&self) -> &'static str {
"Event Manager"
}
fn periodic_op(&mut self, _op_code: i32) -> Result<OpResult, Self::Error> {
if let Ok(event) = self.event_receiver.recv() {
println!("Received event {:?}", event);
}
Ok(OpResult::Ok)
}
}
impl EventManager {
pub fn subcribe_single(&mut self, event: Event, dest: Sender<Event>) {
self.update_listeners(ListenerType::SINGLE(event.raw()), dest);
}
pub fn subscribe_group(&mut self, group_id: GroupId, dest: Sender<Event>) {
self.update_listeners(ListenerType::GROUP(group_id), dest);
}
fn update_listeners(&mut self, key: ListenerType, dest: Sender<Event>) {
if !self.listeners.contains_key(&key) {
self.listeners
.insert(key.clone(), vec![Listener { _ltype: key, dest }]);
} else {
let vec = self.listeners.get_mut(&key).unwrap();
vec.push(Listener { _ltype: key, dest });
}
}
}
fn main() { fn main() {
basic_event_test(); basic_event_test();
} }