Implemented Clone for IrcServer and updated multithreaded examples

accordingly.
This commit is contained in:
Aaron Weiss 2016-01-03 05:39:44 -05:00
parent a6cd761e51
commit ea0577bbaf
4 changed files with 10 additions and 6 deletions

View file

@ -1,7 +1,6 @@
extern crate irc;
use std::default::Default;
use std::sync::Arc;
use std::thread::spawn;
use irc::client::prelude::*;
@ -12,7 +11,7 @@ fn main() {
channels: Some(vec![format!("#vana")]),
.. Default::default()
};
let server = Arc::new(IrcServer::from_config(config).unwrap());
let server = IrcServer::from_config(config).unwrap();
// FIXME: if set_keepalive is stabilized, this can be readded.
// server.conn().set_keepalive(Some(5)).unwrap();
let server = server.clone();

View file

@ -1,7 +1,6 @@
extern crate irc;
use std::default::Default;
use std::sync::Arc;
use std::thread::spawn;
use irc::client::prelude::*;
@ -12,7 +11,7 @@ fn main() {
channels: Some(vec![format!("#vana")]),
.. Default::default()
};
let server = Arc::new(IrcServer::from_config(config).unwrap());
let server = IrcServer::from_config(config).unwrap();
server.identify().unwrap();
let server = server.clone();
let _ = spawn(move || {

View file

@ -1,7 +1,6 @@
extern crate irc;
use std::default::Default;
use std::sync::Arc;
use std::thread::{sleep, spawn};
use std::time::Duration;
use irc::client::prelude::*;
@ -13,7 +12,7 @@ fn main() {
channels: Some(vec![format!("#vana")]),
.. Default::default()
};
let server = Arc::new(IrcServer::from_config(config).unwrap());
let server = IrcServer::from_config(config).unwrap();
server.identify().unwrap();
let server2 = server.clone();
// Let's set up a loop that just prints the messages.

View file

@ -37,6 +37,7 @@ pub struct IrcServer<T: IrcRead, U: IrcWrite> {
state: Arc<ServerState<T, U>>,
}
/// Thread-safe internal state for an IRC server connection.
struct ServerState<T: IrcRead, U: IrcWrite> {
/// The thread-safe IRC connection.
conn: Connection<T, U>,
@ -81,6 +82,12 @@ impl IrcServer<BufReader<NetStream>, BufWriter<NetStream>> {
}
}
impl<'a, T: IrcRead, U: IrcWrite> Clone for IrcServer<T, U> {
fn clone(&self) -> IrcServer<T, U> {
IrcServer { state: self.state.clone() }
}
}
impl<'a, T: IrcRead, U: IrcWrite> Server<'a, T, U> for IrcServer<T, U> {
fn config(&self) -> &Config {
&self.state.config