abstract some of the connection stuff away

This commit is contained in:
lumi 2017-02-20 17:41:09 +01:00
parent 83fdd9866c
commit 6e7ec9ddb4
3 changed files with 42 additions and 9 deletions

View file

@ -4,12 +4,12 @@ use error::Error;
use ns;
use plugin::{Plugin, PluginProxyBinding};
use event::AbstractEvent;
use connection::{Connection, C2S};
use base64;
use minidom::Element;
use xml::writer::XmlEvent as WriterEvent;
use xml::reader::XmlEvent as ReaderEvent;
use std::sync::mpsc::{Receiver, channel};
@ -42,10 +42,7 @@ impl ClientBuilder {
pub fn connect(self) -> Result<Client, Error> {
let host = &self.host.unwrap_or(self.jid.domain.clone());
let mut transport = SslTransport::connect(host, self.port)?;
transport.write_event(WriterEvent::start_element("stream:stream")
.attr("to", &self.jid.domain)
.default_ns(ns::CLIENT)
.ns("stream", ns::STREAM))?;
C2S::init(&mut transport, &self.jid.domain, "before_sasl")?;
let (sender_out, sender_in) = channel();
let (dispatcher_out, dispatcher_in) = channel();
Ok(Client {
@ -155,10 +152,7 @@ impl Client {
}
else if n.is("success", ns::SASL) {
self.transport.reset_stream();
self.transport.write_event(WriterEvent::start_element("stream:stream")
.attr("to", &self.jid.domain)
.default_ns(ns::CLIENT)
.ns("stream", ns::STREAM))?;
C2S::init(&mut self.transport, &self.jid.domain, "after_sasl")?;
loop {
let e = self.transport.read_event()?;
match e {

38
src/connection.rs Normal file
View file

@ -0,0 +1,38 @@
use transport::Transport;
use error::Error;
use ns;
use xml::writer::XmlEvent as WriterEvent;
pub trait Connection {
type InitError;
type CloseError;
fn namespace() -> &'static str;
fn init<T: Transport>(transport: &mut T, domain: &str, id: &str) -> Result<(), Self::InitError>;
fn close<T: Transport>(transport: &mut T) -> Result<(), Self::CloseError>;
}
pub struct C2S;
impl Connection for C2S {
type InitError = Error;
type CloseError = Error;
fn namespace() -> &'static str { ns::CLIENT }
fn init<T: Transport>(transport: &mut T, domain: &str, id: &str) -> Result<(), Error> {
transport.write_event(WriterEvent::start_element("stream:stream")
.attr("to", domain)
.attr("id", id)
.default_ns(ns::CLIENT)
.ns("stream", ns::STREAM))?;
Ok(())
}
fn close<T: Transport>(transport: &mut T) -> Result<(), Error> {
transport.write_event(WriterEvent::end_element())?;
Ok(())
}
}

View file

@ -11,5 +11,6 @@ pub mod client;
pub mod plugin;
pub mod event;
pub mod plugins;
pub mod connection;
mod locked_io;