diff options
author | Till Hoeppner | 2015-06-26 16:58:52 +0200 |
---|---|---|
committer | Till Hoeppner | 2015-06-26 16:58:52 +0200 |
commit | b7bb9b131056c5ffc0552e6c2c16e52781558df7 (patch) | |
tree | 856baf8c4e1af8e5ea43782c39fac59298b27633 /src | |
parent | e0e1329dbc7c88cd72d94bd0c70fdebc08ed0997 (diff) | |
download | irsc-b7bb9b131056c5ffc0552e6c2c16e52781558df7.tar.gz irsc-b7bb9b131056c5ffc0552e6c2c16e52781558df7.tar.xz irsc-b7bb9b131056c5ffc0552e6c2c16e52781558df7.zip |
Re-add carboxyl, split into owned and shared client
Diffstat (limited to 'src')
-rw-r--r-- | src/client.rs | 88 | ||||
-rw-r--r-- | src/lib.rs | 1 |
2 files changed, 64 insertions, 25 deletions
diff --git a/src/client.rs b/src/client.rs index 55ec101..e548a83 100644 --- a/src/client.rs +++ b/src/client.rs @@ -8,8 +8,9 @@ use std::io::{ use std::net::TcpStream; use std::borrow::Cow::{ self, Borrowed, Owned }; +use std::sync::{ Arc, Mutex }; -//use carboxyl::{ Stream, Sink }; +use carboxyl::{ Stream, Sink }; use message::Message; use command::Command; @@ -56,16 +57,37 @@ impl Read for StreamKind { } } -pub struct Client { +pub trait Client { + fn send_message(&mut self, msg: Message) -> Result<()>; + fn join(&mut self, channel: &str, password: Option<&str>) -> Result<()> { + self.send_message(JOIN(vec![channel.into()], password.iter().map(|&p| p.into()).collect()).to_message()) + } + + fn msg(&mut self, to: &str, message: &str) -> Result<()> { + self.send_message(PRIVMSG(to.into(), message.into()).to_message()) + } + + fn register(&mut self, nick: &str, user: &str, desc: &str, pass: Option<&str>) -> Result<()> { + Result(if let Some(pass) = pass { + self.send_message(PASS(pass.into()).to_message()).inner() + } else { Ok(()) } + .and_then(|_| self.send_message(NICK(nick.into()).to_message()).inner()) + .and_then(|_| self.send_message(USER(user.into(), Borrowed("0"), Borrowed("*"), desc.into()).to_message()).inner()) + ) + } + +} + +pub struct OwnedClient { stream: Option<StreamKind>, -// sink: Sink<Message> + sink: Sink<Message> } -impl Client { - pub fn new() -> Client { - Client { +impl OwnedClient { + pub fn new() -> OwnedClient { + OwnedClient { stream: None, -// sink: Sink::new() + sink: Sink::new() } } @@ -120,9 +142,6 @@ impl Client { .map_err(IrscError::Io))) } - pub fn send_message(&mut self, msg: Message) -> Result<()> { - self.send_raw(&msg.to_string()) - } pub fn send(&mut self, cmd: Command) -> Result<()> { self.send_message(cmd.to_message()) @@ -157,7 +176,7 @@ impl Client { on_event(self, &msg, event); } - //self.sink.send(msg) + self.sink.send(msg) } } Result(Ok(())) @@ -172,10 +191,35 @@ impl Client { self.intern_listen(Some(events)) } -// pub fn messages(&self) -> Stream<Message> { self.sink.stream() } + pub fn into_shared(self) -> SharedClient { + SharedClient { + client: Arc::new(Mutex::new(self)), + } + } + + pub fn messages(&self) -> Stream<Message> { self.sink.stream() } +} + +impl Client for OwnedClient { + fn send_message(&mut self, msg: Message) -> Result<()> { + self.send_raw(&msg.to_string()) + } +} + +#[derive(Clone)] +pub struct SharedClient { + client: Arc<Mutex<OwnedClient>>, +} + +impl SharedClient { + pub fn messages(&self) -> Stream<(Arc<Mutex<OwnedClient>>, Message)> { + let cl = self.client.clone(); + self.client.lock().unwrap().messages() + .map(move |m| (cl.clone(), m)) + } /*pub fn events(&self) -> Stream<(Message, Event)> { - self.messages().filter_map(|msg| match Command::from_message(&msg) { + self.client.lock().unwrap().messages().filter_map(|msg| match Command::from_message(&msg) { Some(m) => Some((msg, Event::Command(m.clone()))), None => match Reply::from_message(&msg) { Some(r) => Some((msg, Event::Reply(r))), @@ -187,18 +231,12 @@ impl Client { // pub fn commands(&self) -> Stream<Command> { // self.messages().filter_map(|msg| Command::from_message(&msg).map(|c| c.to_static())) // } +} - pub fn join(&mut self, channel: &str, password: Option<&str>) -> Result<()> { - self.send_message(JOIN(vec![channel.into()], password.iter().map(|&p| p.into()).collect()).to_message()) - } - - pub fn msg(&mut self, to: &str, message: &str) -> Result<()> { - self.send_message(PRIVMSG(to.into(), message.into()).to_message()) - } - - pub fn register(&mut self, nick: &str, user: &str, desc: &str) -> Result<()> { - Result(self.send_message(NICK(nick.into()).to_message()).inner() - .and_then(|_| self.send_message(USER(user.into(), Borrowed("0"), Borrowed("*"), desc.into()).to_message()).inner())) - +impl Client for SharedClient { + fn send_message(&mut self, msg: Message) -> Result<()> { + if let Ok(mut guard) = self.client.lock() { + guard.send_raw(&msg.to_string()) + } else { Result(Ok(())) } } } @@ -10,6 +10,7 @@ extern crate regex; extern crate log; #[cfg(feature = "ssl")] extern crate openssl; +extern crate carboxyl; pub mod client; pub mod color; |