diff --git a/src/client.rs b/src/client.rs index 2b8f4398..2a35ce74 100644 --- a/src/client.rs +++ b/src/client.rs @@ -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 { 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 { diff --git a/src/connection.rs b/src/connection.rs new file mode 100644 index 00000000..76bef64e --- /dev/null +++ b/src/connection.rs @@ -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(transport: &mut T, domain: &str, id: &str) -> Result<(), Self::InitError>; + fn close(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(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(transport: &mut T) -> Result<(), Error> { + transport.write_event(WriterEvent::end_element())?; + Ok(()) + } +} diff --git a/src/lib.rs b/src/lib.rs index 811c308a..2d941ffd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,5 +11,6 @@ pub mod client; pub mod plugin; pub mod event; pub mod plugins; +pub mod connection; mod locked_io;