Event dispatching

This commit is contained in:
Crom (Thibaut CHARLES) 2019-01-30 23:11:51 +01:00
parent d49728d842
commit 140a094b15
Signed by: tcharles
GPG Key ID: 45A3D5F880B9E6D0
6 changed files with 91 additions and 23 deletions

View File

@ -1,15 +1,16 @@
extern crate libloading;
mod message; mod message;
mod monitor; mod monitor;
mod output; mod output;
mod plugins; mod plugins;
// use libloading::{Library, Symbol}; use std::thread;
use std::sync::mpsc;
use crate::monitor::Monitor; use crate::monitor::Monitor;
use crate::output::Output; use crate::output::Output;
use crate::message::Message; use crate::message::Message;
fn main() { fn main() {
// let lib = Library::new("/home/crom/.gitprojects/rnetmon/plugins/output/stdout/target/debug/deps/libstdout.so").unwrap(); // let lib = Library::new("/home/crom/.gitprojects/rnetmon/plugins/output/stdout/target/debug/deps/libstdout.so").unwrap();
@ -17,17 +18,53 @@ fn main() {
// let handle_message: Symbol<unsafe fn()> = lib.get(b"handle_message").unwrap(); // let handle_message: Symbol<unsafe fn()> = lib.get(b"handle_message").unwrap();
// handle_message(); // handle_message();
// } // }
// let msg = Message{
// emitter: "me".to_string(),
// level: 10,
// msg_type: "string".to_string(),
// text: "Consectetur duis do dolor deserunt est minim dolore tempor et non duis.".to_string(),
// };
let (mon_sender, mon_receiver) = mpsc::channel();
let msg = Message{ // let mut monitors: Vec<(Box<Monitor>, thread::JoinHandle<_>)> = vec![];
emitter: "me".to_string(), let mut monitor_threads: Vec<thread::JoinHandle<_>> = vec![];
level: 10, {
msg_type: "string".to_string(), let snd = mon_sender.clone();
text: "Consectetur duis do dolor deserunt est minim dolore tempor et non duis.".to_string(), let mon = plugins::monitor::tester::Tester::new();
}; let thread = thread::spawn(move|| {
loop {
mon.run(&snd);
}
});
monitor_threads.push(thread)
// monitor_threads.push((mon, thread));
}
let monitor_threads = monitor_threads;
let input = plugins::monitor::tester::Tester::new();
let out = plugins::output::stdout::Stdout::new(); let mut output_senders: Vec<mpsc::Sender<Message>> = vec![];
out.on_message(&msg); let mut output_threads: Vec<thread::JoinHandle<_>> = vec![];
{
let (out_sender, out_receiver) = mpsc::channel();
output_senders.push(out_sender);
let thread = thread::spawn(move|| {
loop {
let message = out_receiver.recv().unwrap();
println!("{:?}", message);
}
});
output_threads.push(thread);
}
let output_senders = output_senders;
let output_threads = output_threads;
// Dispatch messages
loop {
let message = mon_receiver.recv().unwrap();
for out in &output_senders {
out.send(message.clone()).unwrap();
}
}
} }

View File

@ -1,9 +1,21 @@
#[derive(Debug)] #[derive(Debug)]
#[derive(Clone)]
pub struct Message { pub struct Message {
pub emitter: String, pub emitter: String,
pub level: i32, pub level: i32,
pub msg_type: String, pub msg_type: String,
pub text: String, pub text: String,
} }
// impl Clone for Message {
// fn clone(&self) -> Self {
// Message{
// emitter: self.emitter.clone(),
// level: self.level,
// msg_type: self.msg_type.clone(),
// text: self.text.clone(),
// }
// }
// }

View File

@ -1,7 +1,9 @@
use crate::message::Message; use crate::message::Message;
pub use std::sync::mpsc;
pub trait Monitor { pub trait Monitor {
fn new() -> Self; fn new() -> Self where Self: Sized;
fn run(&self); fn run(&self, sender: &mpsc::Sender<Message>);
} }

View File

@ -1,7 +1,8 @@
use crate::message::Message; use crate::message::Message;
use std::sync::mpsc;
pub trait Output { pub trait Output {
fn new() -> Self; fn new(receiver: mpsc::Receiver<Message>) -> Self;
fn on_message(&self, message: &Message); fn on_message(&self, message: Message);
} }

View File

@ -1,6 +1,8 @@
use crate::message::Message;
pub use crate::monitor::Monitor; pub use crate::monitor::Monitor;
pub use crate::message::Message;
pub use std::sync::mpsc;
#[derive(Debug)] #[derive(Debug)]
pub struct Tester { pub struct Tester {
@ -11,7 +13,19 @@ impl Monitor for Tester {
fn new() -> Self { fn new() -> Self {
Tester{} Tester{}
} }
fn run(&self) { fn run(&self, sender: &mpsc::Sender<Message>) {
loop {
println!("-- Sending message");
sender.send(Message{
emitter: "me".to_string(),
level: 10,
msg_type: "string".to_string(),
text: "Consectetur duis do dolor deserunt est minim dolore tempor et non duis.".to_string(),
}).unwrap();
std::thread::sleep(std::time::Duration::from_millis(2000));
}
} }
} }

View File

@ -1,18 +1,20 @@
use crate::message::Message; use crate::message::Message;
pub use crate::output::Output; pub use crate::output::Output;
use std::sync::mpsc;
#[derive(Debug)] #[derive(Debug)]
pub struct Stdout { pub struct Stdout {
receiver: mpsc::Receiver<Message>,
} }
impl Output for Stdout { impl Output for Stdout {
fn new() -> Self { fn new(receiver: mpsc::Receiver<Message>) -> Self {
Stdout{} Stdout{
receiver: receiver
}
} }
fn on_message(&self, message: Message){
fn on_message(&self, message: &Message){
println!("{:?}", message);
} }
} }