make it (theoretically) possible to reconfigure a module. Might have some sort of file watcher thread which periodically checks to see if config file has been modified and reconfigures if necessary.
This commit is contained in:
parent
d9cfab7081
commit
a01ad46efa
@ -71,6 +71,14 @@ impl Tenquestionmarks {
|
|||||||
Result::Ok(Tenquestionmarks::with_modules(modules))
|
Result::Ok(Tenquestionmarks::with_modules(modules))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn reconfigure (&mut self, configuration: &Table) {
|
||||||
|
for (key, module_configuration) in configuration {
|
||||||
|
if let (Some(module_configuration_table), Some(ref mut module)) = (module_configuration.as_table(), self.modules.get_mut(key)) {
|
||||||
|
module.reconfigure(module_configuration_table.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn run (&self) {
|
pub fn run (&self) {
|
||||||
crossbeam::scope(|scope| {
|
crossbeam::scope(|scope| {
|
||||||
let mut dispatchers: BTreeMap<&str, Receiver<Envelope>> = BTreeMap::new();
|
let mut dispatchers: BTreeMap<&str, Receiver<Envelope>> = BTreeMap::new();
|
||||||
|
@ -29,6 +29,11 @@ impl Module {
|
|||||||
self.event_loop.run(sender, receiver);
|
self.event_loop.run(sender, receiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn reconfigure (&mut self, configuration: Table) {
|
||||||
|
self.event_loop.reconfigure(&configuration);
|
||||||
|
self.config = configuration;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn parents (&self) -> Vec<String> {
|
pub fn parents (&self) -> Vec<String> {
|
||||||
self.config.get("parents")
|
self.config.get("parents")
|
||||||
.and_then(|value| value.as_array())
|
.and_then(|value| value.as_array())
|
||||||
@ -56,4 +61,5 @@ impl Module {
|
|||||||
|
|
||||||
pub trait EventLoop : Sync {
|
pub trait EventLoop : Sync {
|
||||||
fn run (&self, _: Box<ExtSender<Event>>, _: Receiver<Arc<Envelope>>) {}
|
fn run (&self, _: Box<ExtSender<Event>>, _: Receiver<Arc<Envelope>>) {}
|
||||||
|
fn reconfigure (&mut self, _: &Table) {}
|
||||||
}
|
}
|
||||||
|
@ -18,31 +18,40 @@ pub struct RandomModule {
|
|||||||
|
|
||||||
impl RandomModule {
|
impl RandomModule {
|
||||||
pub fn new (_: &Table, configuration: &Table) -> Box<EventLoop> {
|
pub fn new (_: &Table, configuration: &Table) -> Box<EventLoop> {
|
||||||
let pattern = configuration.get("pattern")
|
Box::new(RandomModule {
|
||||||
|
pattern: RandomModule::pattern_from_config(&configuration),
|
||||||
|
responses: RandomModule::responses_from_config(&configuration)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pattern_from_config (configuration: &Table) -> Regex {
|
||||||
|
configuration.get("pattern")
|
||||||
.and_then(|value| value.as_str())
|
.and_then(|value| value.as_str())
|
||||||
.map(String::from)
|
.map(String::from)
|
||||||
.or_else(|| configuration.get("prefix")
|
.or_else(|| configuration.get("prefix")
|
||||||
.and_then(|value| value.as_str())
|
.and_then(|value| value.as_str())
|
||||||
.map(|value| format!("^{}", regex::escape(value))))
|
.map(|value| format!("^{}", regex::escape(value))))
|
||||||
.and_then(|value| Regex::new(&value).ok())
|
.and_then(|value| Regex::new(&value).ok())
|
||||||
.expect("Invalid value for pattern");
|
.expect("Invalid value for pattern")
|
||||||
|
}
|
||||||
|
|
||||||
let responses = configuration.get("responses")
|
fn responses_from_config (configuration: &Table) -> Vec<String> {
|
||||||
|
configuration.get("responses")
|
||||||
.and_then(|value| value.as_array())
|
.and_then(|value| value.as_array())
|
||||||
.map(|value| value.to_vec())
|
.map(|value| value.to_vec())
|
||||||
.unwrap_or(vec![])
|
.unwrap_or(vec![])
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|value| { String::from(value.as_str().unwrap()) })
|
.map(|value| { String::from(value.as_str().unwrap()) })
|
||||||
.collect();
|
.collect()
|
||||||
|
|
||||||
Box::new(RandomModule {
|
|
||||||
pattern: pattern,
|
|
||||||
responses: responses
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EventLoop for RandomModule {
|
impl EventLoop for RandomModule {
|
||||||
|
fn reconfigure (&mut self, configuration: &Table) {
|
||||||
|
self.pattern = RandomModule::pattern_from_config(&configuration);
|
||||||
|
self.responses = RandomModule::responses_from_config(&configuration)
|
||||||
|
}
|
||||||
|
|
||||||
fn run (&self, _: Box<ExtSender<Event>>, receiver: Receiver<Arc<Envelope>>) {
|
fn run (&self, _: Box<ExtSender<Event>>, receiver: Receiver<Arc<Envelope>>) {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user