expose a sender on the Module so we can send events directly to the module

This commit is contained in:
Adrian Malacoda 2018-02-25 02:20:26 -06:00
parent 6acffb59cc
commit 9295b603aa
5 changed files with 28 additions and 6 deletions

View File

@ -1,5 +1,6 @@
pub mod filter;
use toml::value::Table;
use {Message, Channel, User};
#[derive(Debug)]
@ -14,7 +15,9 @@ pub enum Event {
UserKick { channel: Channel, user: User }, // A usre is kicked from a channel
UserBan { channel: Channel, user: User }, // A user is banned from a channel
TopicChange { channel: Channel } // Channel topic is changed
TopicChange { channel: Channel }, // Channel topic is changed,
Configure { configuration: Table } // Request to reconfigure a module
}
#[derive(Debug)]

View File

@ -104,6 +104,7 @@ impl Tenquestionmarks {
module.run(Box::new(mapped_sender), module_receiver);
info!("Event loop thread for \"{}\" is exiting", key);
});
module.set_sender(&module_sender);
(&key[..], module_sender)
}).collect();

View File

@ -37,8 +37,8 @@ fn main () {
Ok(tqm) => {
info!("tenquestionmarks initialized successfully");
crossbeam::scope(|scope| {
//scope.spawn(|| {
// tqm.reconfigure(&read_config_from_file(&config_file_name));
//scope.spawn(move || {
// tqm_mut.reconfigure(&read_config_from_file(&config_file_name));
//});
tqm.run();

View File

@ -15,6 +15,8 @@ use modules::echobox::EchoboxModule;
use modules::autolink::AutolinkModule;
use modules::logger::LoggerModule;
use std::sync::{Arc, Mutex};
pub struct ModuleLoader {
types: BTreeMap<&'static str, fn(&Table, &Table) -> Box<EventLoop>>
}
@ -68,7 +70,8 @@ impl ModuleLoader {
Some(constructor) => Result::Ok(Module {
module_type: module_type.to_owned(),
config: module_configuration.clone(),
event_loop: constructor(general_configuration, module_configuration)
event_loop: constructor(general_configuration, module_configuration),
sender: Mutex::new(None)
}),
None => Result::Err(ModuleLoaderError { message: format!("No such module type: {}", module_type) })
}

View File

@ -12,8 +12,8 @@ pub mod loader;
use event::{Event, Envelope};
use std::sync::Arc;
use std::sync::mpsc::Receiver;
use std::sync::{Mutex, Arc};
use std::sync::mpsc::{Sender, Receiver};
use transformable_channels::mpsc::ExtSender;
use toml::value::Table;
@ -21,6 +21,7 @@ use toml::value::Table;
pub struct Module {
event_loop: Box<EventLoop>,
module_type: String,
sender: Mutex<Option<Sender<Arc<Envelope>>>>,
pub config: Table,
}
@ -34,6 +35,20 @@ impl Module {
self.config = configuration;
}
pub fn send (&self, event: Envelope) {
if let Ok(sender_lock) = self.sender.lock() {
if let Some(ref sender) = *sender_lock {
sender.send(Arc::new(event));
}
}
}
pub fn set_sender (&self, sender: &Sender<Arc<Envelope>>) {
if let Ok(ref mut sender_lock) = self.sender.lock() {
**sender_lock = Some(sender.clone());
}
}
pub fn parents (&self) -> Vec<String> {
self.config.get("parents")
.and_then(|value| value.as_array())