tag senders/receivers with module names so we can eventually associate individual events to module names

This commit is contained in:
Adrian Malacoda
2017-02-19 17:31:37 -06:00
parent 0f945ec604
commit ce35368676

View File

@@ -55,18 +55,26 @@ impl Tenquestionmarks {
// Module event consumer threads. // Module event consumer threads.
// tenquestionmarks propagates all events to each Module through these // tenquestionmarks propagates all events to each Module through these
// channels. // channels.
let senders: Vec<Sender<Arc<Event>>> = self.modules.values().map(|module| { let senders: BTreeMap<&str, Sender<Arc<Event>>> = self.modules.iter().map(|(key, module)| {
let (sender, receiver) = mpsc::channel(); let (sender, receiver) = mpsc::channel();
scope.spawn(move || module.consume_events(receiver)); info!("Spawning consumer thread for \"{}\"", key);
sender scope.spawn(move || {
module.consume_events(receiver);
info!("Consumer thread for \"{}\" is exiting", key);
});
(&key[..], sender)
}).collect(); }).collect();
// Module event producer threads. // Module event producer threads.
// Each Module will produce events which tenquestionmarks will push // Each Module will produce events which tenquestionmarks will push
// into all other Modules. // into all other Modules.
for module in self.modules.values() { for (key, module) in self.modules.iter() {
let module_sender = sender.clone(); let module_sender = sender.clone();
scope.spawn(move || module.produce_events(module_sender)); info!("Spawning producer thread for \"{}\"", key);
scope.spawn(move || {
module.produce_events(module_sender);
info!("Producer thread for \"{}\" is exiting", key);
});
} }
// tenquestionmarks main event loop. // tenquestionmarks main event loop.
@@ -75,9 +83,9 @@ impl Tenquestionmarks {
loop { loop {
match receiver.recv() { match receiver.recv() {
Ok(event) => { Ok(event) => {
for sender in &senders { for (key, sender) in &senders {
match sender.send(event.clone()) { match sender.send(event.clone()) {
Err(err) => debug!("Failed to dispatch event to module: {:?}", err), Err(err) => debug!("Failed to dispatch event to module \"{}\": {:?}", key, err),
Ok(_) => {} Ok(_) => {}
} }
} }