Added Server trait and implemented basic Server functionality for IrcServer.
This commit is contained in:
parent
4df7be1662
commit
2e2d411907
2 changed files with 49 additions and 3 deletions
|
@ -23,9 +23,9 @@ impl<T, U> Connection<T, U> where T: IrcWriter, U: IrcReader {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn send(&self, msg: Message) -> IoResult<()> {
|
||||
pub fn send(&self, message: Message) -> IoResult<()> {
|
||||
let mut send = self.writer.lock();
|
||||
try!(send.write_str(msg.into_string()[]));
|
||||
try!(send.write_str(message.into_string()[]));
|
||||
send.flush()
|
||||
}
|
||||
|
||||
|
|
|
@ -1,15 +1,61 @@
|
|||
use std::io::{BufferedReader, BufferedWriter, IoResult, TcpStream};
|
||||
use conn::Connection;
|
||||
use data::config::Config;
|
||||
use data::kinds::{IrcReader, IrcWriter};
|
||||
use data::message::Message;
|
||||
|
||||
pub trait Server<'a, T, U> {
|
||||
fn send(&self, _: Message) -> IoResult<()>;
|
||||
fn iter(&'a self) -> ServerIterator<'a, T, U>;
|
||||
}
|
||||
|
||||
pub struct IrcServer<'a, T, U> where T: IrcWriter, U: IrcReader {
|
||||
pub conn: Connection<T, U>
|
||||
pub conn: Connection<T, U>,
|
||||
pub config: Config
|
||||
}
|
||||
|
||||
impl<'a> IrcServer<'a, BufferedWriter<TcpStream>, BufferedReader<TcpStream>> {
|
||||
pub fn new(config: &str) -> IoResult<IrcServer<'a, BufferedWriter<TcpStream>, BufferedReader<TcpStream>>> {
|
||||
let config = try!(Config::load_utf8(config));
|
||||
let conn = try!(Connection::connect(config.server[], config.port));
|
||||
Ok(IrcServer {
|
||||
conn: conn,
|
||||
config: config
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, U> Server<'a, T, U> for IrcServer<'a, T, U> where T: IrcWriter, U: IrcReader{
|
||||
fn send(&self, message: Message) -> IoResult<()> {
|
||||
self.conn.send(message)
|
||||
}
|
||||
|
||||
fn iter(&'a self) -> ServerIterator<'a, T, U> {
|
||||
ServerIterator::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, U> IrcServer<'a, T, U> where T: IrcWriter, U: IrcReader {
|
||||
pub fn from_connection(config: &str, conn: Connection<T, U>) -> IoResult<IrcServer<'a, T, U>> {
|
||||
Ok(IrcServer {
|
||||
conn: conn,
|
||||
config: try!(Config::load_utf8(config))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ServerIterator<'a, T, U> where T: IrcWriter, U: IrcReader {
|
||||
pub server: &'a IrcServer<'a, T, U>
|
||||
}
|
||||
|
||||
impl<'a, T, U> ServerIterator<'a, T, U> where T: IrcWriter, U: IrcReader {
|
||||
pub fn new(server: &'a IrcServer<'a, T, U>) -> ServerIterator<'a, T, U> {
|
||||
ServerIterator {
|
||||
server: server
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, U> Iterator<Message> for ServerIterator<'a, T, U> where T: IrcWriter, U: IrcReader {
|
||||
fn next(&mut self) -> Option<Message> {
|
||||
let line = self.server.conn.recv();
|
||||
|
|
Loading…
Reference in a new issue