2014-10-09 02:57:23 +02:00
|
|
|
use std::cell::{RefCell, RefMut};
|
|
|
|
use std::io::{BufferedReader, BufferedWriter, IoResult, TcpStream, Writer};
|
2014-10-08 19:10:55 +02:00
|
|
|
use data::{IrcReader, IrcWriter, Message};
|
2014-09-24 02:11:13 +02:00
|
|
|
|
2014-10-09 02:57:23 +02:00
|
|
|
pub struct Connection<T, U> where T: IrcWriter, U: IrcReader {
|
|
|
|
writer: RefCell<T>,
|
|
|
|
reader: RefCell<U>,
|
2014-10-07 20:30:38 +02:00
|
|
|
}
|
2014-09-24 02:11:13 +02:00
|
|
|
|
2014-10-09 02:57:23 +02:00
|
|
|
impl Connection<BufferedWriter<TcpStream>, BufferedReader<TcpStream>> {
|
|
|
|
pub fn connect(host: &str, port: u16) -> IoResult<Connection<BufferedWriter<TcpStream>, BufferedReader<TcpStream>>> {
|
2014-10-08 18:57:36 +02:00
|
|
|
let socket = try!(TcpStream::connect(host, port));
|
2014-10-09 02:57:23 +02:00
|
|
|
Connection::new(BufferedWriter::new(socket.clone()), BufferedReader::new(socket.clone()))
|
2014-10-08 18:57:36 +02:00
|
|
|
}
|
2014-09-24 02:11:13 +02:00
|
|
|
}
|
|
|
|
|
2014-10-08 19:10:55 +02:00
|
|
|
impl<T, U> Connection<T, U> where T: IrcWriter, U: IrcReader {
|
2014-10-09 04:09:35 +02:00
|
|
|
pub fn new(writer: T, reader: U) -> IoResult<Connection<T, U>> {
|
2014-10-09 02:57:23 +02:00
|
|
|
Ok(Connection {
|
|
|
|
writer: RefCell::new(writer),
|
|
|
|
reader: RefCell::new(reader),
|
|
|
|
})
|
2014-10-07 20:30:38 +02:00
|
|
|
}
|
2014-09-24 02:11:13 +02:00
|
|
|
|
2014-10-09 02:57:23 +02:00
|
|
|
fn send_internal(&self, msg: &str) -> IoResult<()> {
|
|
|
|
let mut send = self.writer.borrow_mut();
|
|
|
|
try!(send.write_str(msg));
|
|
|
|
send.flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn send(&self, msg: Message) -> IoResult<()> {
|
2014-10-08 18:57:36 +02:00
|
|
|
let mut send = msg.command.to_string();
|
2014-10-09 04:09:35 +02:00
|
|
|
if msg.args.init().len() > 0 {
|
|
|
|
send.push_str(" ");
|
|
|
|
send.push_str(msg.args.init().connect(" ").as_slice());
|
|
|
|
}
|
2014-10-08 18:57:36 +02:00
|
|
|
send.push_str(" :");
|
|
|
|
send.push_str(*msg.args.last().unwrap());
|
|
|
|
send.push_str("\r\n");
|
2014-10-09 02:57:23 +02:00
|
|
|
self.send_internal(send.as_slice())
|
|
|
|
}
|
|
|
|
|
2014-10-09 04:09:35 +02:00
|
|
|
pub fn writer<'a>(&'a self) -> RefMut<'a, T> {
|
|
|
|
self.writer.borrow_mut()
|
|
|
|
}
|
|
|
|
|
2014-10-09 02:57:23 +02:00
|
|
|
pub fn reader<'a>(&'a self) -> RefMut<'a, U> {
|
|
|
|
self.reader.borrow_mut()
|
2014-10-08 18:57:36 +02:00
|
|
|
}
|
2014-09-24 02:11:13 +02:00
|
|
|
}
|
2014-10-09 03:33:20 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2014-10-09 04:09:35 +02:00
|
|
|
use super::Connection;
|
2014-10-09 03:33:20 +02:00
|
|
|
use std::io::MemWriter;
|
|
|
|
use std::io::util::NullReader;
|
|
|
|
use data::Message;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn new_connection() {
|
|
|
|
let w = MemWriter::new();
|
|
|
|
assert!(Connection::new(w, NullReader).is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn send_internal() {
|
|
|
|
let w = MemWriter::new();
|
|
|
|
let c = Connection::new(w, NullReader).unwrap();
|
|
|
|
c.send_internal("string of text").unwrap();
|
2014-10-09 04:09:35 +02:00
|
|
|
assert_eq!(c.writer().deref_mut().get_ref(), "string of text".as_bytes());
|
2014-10-09 03:33:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn send() {
|
|
|
|
let w = MemWriter::new();
|
|
|
|
let c = Connection::new(w, NullReader).unwrap();
|
|
|
|
let args = ["flare.to.ca.fyrechat.net"];
|
|
|
|
c.send(Message::new(None, "PING", args)).unwrap();
|
2014-10-09 04:09:35 +02:00
|
|
|
assert_eq!(c.writer().deref_mut().get_ref(), "PING :flare.to.ca.fyrechat.net\r\n".as_bytes());
|
2014-10-09 03:33:20 +02:00
|
|
|
}
|
|
|
|
}
|