rust-irc/src/conn.rs

76 lines
2.4 KiB
Rust
Raw Normal View History

2014-11-03 00:52:15 -05:00
//! Thread-safe connections on any IrcWriters and IrcReaders
#![experimental]
use std::sync::{Mutex, MutexGuard};
2014-11-02 17:25:45 -05:00
use std::io::{BufferedReader, BufferedWriter, IoResult, TcpStream};
use data::kinds::{IrcWriter, IrcReader};
use data::message::Message;
2014-11-03 00:52:15 -05:00
/// A thread-safe connection
#[experimental]
2014-11-02 17:25:45 -05:00
pub struct Connection<T, U> where T: IrcWriter, U: IrcReader {
writer: Mutex<T>,
reader: Mutex<U>,
}
impl Connection<BufferedWriter<TcpStream>, BufferedReader<TcpStream>> {
2014-11-03 00:52:15 -05:00
/// Creates a thread-safe TCP connection to the specified server
#[experimental]
2014-11-02 17:25:45 -05:00
pub fn connect(host: &str, port: u16) -> IoResult<Connection<BufferedWriter<TcpStream>, BufferedReader<TcpStream>>> {
let socket = try!(TcpStream::connect(format!("{}:{}", host, port)[]));
2014-11-02 17:25:45 -05:00
Ok(Connection::new(BufferedWriter::new(socket.clone()), BufferedReader::new(socket)))
}
}
impl<T, U> Connection<T, U> where T: IrcWriter, U: IrcReader {
2014-11-03 00:52:15 -05:00
/// Creates a new connection from any arbitrary IrcWriter and IrcReader
#[experimental]
2014-11-02 17:25:45 -05:00
pub fn new(writer: T, reader: U) -> Connection<T, U> {
Connection {
writer: Mutex::new(writer),
reader: Mutex::new(reader),
}
}
2014-11-03 00:52:15 -05:00
/// Sends a Message over this connection
#[experimental]
pub fn send(&self, message: Message) -> IoResult<()> {
2014-11-02 17:25:45 -05:00
let mut send = self.writer.lock();
try!(send.write_str(message.into_string()[]));
2014-11-02 17:25:45 -05:00
send.flush()
}
2014-11-03 00:52:15 -05:00
/// Receives a single line from this connection
#[experimental]
2014-11-02 17:25:45 -05:00
pub fn recv(&self) -> IoResult<String> {
self.reader.lock().read_line()
}
/// Acquires the Writer lock
#[experimental]
pub fn writer<'a>(&'a self) -> MutexGuard<'a, T> {
self.writer.lock()
}
2014-11-02 17:25:45 -05:00
}
#[cfg(test)]
mod test {
use super::Connection;
use std::io::{MemReader, MemWriter};
use std::io::util::{NullReader, NullWriter};
use data::message::Message;
#[test]
fn send() {
let conn = Connection::new(MemWriter::new(), NullReader);
assert!(conn.send(Message::new(None, "PRIVMSG", Some(vec!["test"]), Some("Testing!"))).is_ok());
let data = String::from_utf8(conn.writer().get_ref().to_vec()).unwrap();
assert_eq!(data[], "PRIVMSG test :Testing!\r\n");
}
#[test]
fn recv() {
let conn = Connection::new(NullWriter, MemReader::new("PRIVMSG test :Testing!\r\n".as_bytes().to_vec()));
assert_eq!(conn.recv().unwrap()[], "PRIVMSG test :Testing!\r\n");
}
}