diff options
author | David Li <li.davidm96@gmail.com> | 2016-01-27 21:56:53 -0500 |
---|---|---|
committer | David Li <li.davidm96@gmail.com> | 2016-01-27 21:56:53 -0500 |
commit | 9f36a68fba2d3c1e3e7052cc0a4e75f9c569adaa (patch) | |
tree | ac13e2cc18d84d632d87dda6990f4068fef1830d /src | |
parent | d5746e7cdb1db2b457688a98004620d42a49f6fa (diff) |
feat: add multithreading
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/src/main.rs b/src/main.rs index a230a7e..03f9e02 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,8 +5,10 @@ extern crate irc; extern crate serde; extern crate serde_json; +use std::sync::Arc; use std::collections::HashMap; use std::io::Read; +use std::thread; use hyper::Client; use hyper::header::Connection; @@ -74,11 +76,11 @@ fn extract_username(prefix: Option<String>) -> Option<String> { } struct IrcBot<T: IrcRead, U: IrcWrite> { - server: IrcServer<T, U>, + server: Arc<IrcServer<T, U>>, } impl<T: IrcRead, U: IrcWrite> IrcBot<T, U> { - fn new(server: IrcServer<T, U>) -> IrcBot<T, U> { + fn new(server: Arc<IrcServer<T, U>>) -> IrcBot<T, U> { IrcBot { server: server, } @@ -149,9 +151,19 @@ fn main() { .. Default::default() }; - let server = IrcServer::from_config(config).unwrap(); + let server = Arc::new(IrcServer::from_config(config).unwrap()); server.identify().unwrap(); - let bot = IrcBot::new(server); - bot.process_messages(); + let mut handles = Vec::new(); + for _ in 0..4 { + let s = server.clone(); + handles.push(thread::spawn(move || { + let bot = IrcBot::new(s.clone()); + bot.process_messages(); + })); + } + + for handle in handles.into_iter() { + handle.join(); + } } |