135 lines
3.7 KiB
Rust
135 lines
3.7 KiB
Rust
extern crate toml;
|
|
extern crate crossbeam;
|
|
extern crate discord;
|
|
extern crate rand;
|
|
extern crate pvn;
|
|
extern crate echobox;
|
|
|
|
use std::collections::BTreeMap;
|
|
use toml::Table;
|
|
|
|
mod modules;
|
|
use modules::Module;
|
|
use modules::loader::{ModuleLoader, ModuleLoaderError};
|
|
|
|
mod event;
|
|
use event::Envelope;
|
|
|
|
use std::sync::Arc;
|
|
use std::sync::mpsc;
|
|
use std::sync::mpsc::Sender;
|
|
|
|
mod helpers;
|
|
|
|
#[macro_use]
|
|
extern crate log;
|
|
|
|
pub struct Tenquestionmarks {
|
|
modules: BTreeMap<String, Box<Module>>
|
|
}
|
|
|
|
impl Tenquestionmarks {
|
|
pub fn with_modules (modules: BTreeMap<String, Box<Module>>) -> Tenquestionmarks {
|
|
let tqm = Tenquestionmarks {
|
|
modules: modules
|
|
};
|
|
|
|
for (_, module) in &tqm.modules {
|
|
module.register(&tqm);
|
|
}
|
|
|
|
tqm
|
|
}
|
|
|
|
pub fn from_configuration (configuration: Table) -> Result<Tenquestionmarks, ModuleLoaderError> {
|
|
let loader = ModuleLoader::new();
|
|
let modules = loader.load_from_configuration(configuration)?;
|
|
Result::Ok(Tenquestionmarks::with_modules(modules))
|
|
}
|
|
|
|
pub fn run (&self) {
|
|
crossbeam::scope(|scope| {
|
|
// Our main event channel.
|
|
// Modules push events to tenquestionmarks using this channel.
|
|
let (ref main_sender, ref main_receiver) = mpsc::channel();
|
|
|
|
// Module threads.
|
|
// Each Module will produce events which tenquestionmarks will push
|
|
// into all other Modules.
|
|
// tenquestionmarks propagates all events to each Module through these
|
|
// channels.
|
|
let module_senders: BTreeMap<&str, Sender<Arc<Envelope>>> = self.modules.iter().map(|(key, module)| {
|
|
let main_sender_clone = main_sender.clone();
|
|
let (module_sender, module_receiver) = mpsc::channel();
|
|
info!("Spawning thread for \"{}\"", key);
|
|
scope.spawn(move || {
|
|
module.run(main_sender_clone, module_receiver);
|
|
info!("Thread for \"{}\" is exiting", key);
|
|
});
|
|
(&key[..], module_sender)
|
|
}).collect();
|
|
|
|
// tenquestionmarks main event loop.
|
|
// tenquestionmarks receives events produced by Modules and pushes them
|
|
// into all other Modules
|
|
loop {
|
|
match main_receiver.recv() {
|
|
Ok(envelope) => {
|
|
let arc_envelope = Arc::new(envelope);
|
|
for (key, sender) in &module_senders {
|
|
match sender.send(arc_envelope.clone()) {
|
|
Err(err) => debug!("Failed to dispatch event to module \"{}\": {:?}", key, err),
|
|
Ok(_) => {}
|
|
}
|
|
}
|
|
},
|
|
Err(err) => { error!("Failed to receive event in main event loop: {:?}", err); }
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
pub struct Message {
|
|
content: String,
|
|
author: User,
|
|
channel: Option<Channel>
|
|
}
|
|
|
|
impl Message {
|
|
fn reply (&self, message: &str) {
|
|
match self.channel {
|
|
Some(ref channel) => channel.send(message),
|
|
None => self.author.send(message)
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct Channel {
|
|
name: String,
|
|
description: String,
|
|
topic: String,
|
|
sender: Box<MessageSender>
|
|
}
|
|
|
|
impl Channel {
|
|
pub fn send (&self, message: &str) {
|
|
self.sender.send_message(message);
|
|
}
|
|
}
|
|
|
|
pub struct User {
|
|
name: String,
|
|
sender: Box<MessageSender>
|
|
}
|
|
|
|
impl User {
|
|
pub fn send (&self, message: &str) {
|
|
self.sender.send_message(message);
|
|
}
|
|
}
|
|
|
|
pub trait MessageSender : Sync + Send {
|
|
fn send_message (&self, _: &str) {}
|
|
}
|