client.rs

 1use jid::Jid;
 2use transport::{Transport, SslTransport};
 3use error::Error;
 4use ns;
 5
 6use xml::writer::XmlEvent;
 7
 8pub struct ClientBuilder {
 9    jid: Jid,
10    host: Option<String>,
11    port: u16,
12}
13
14impl ClientBuilder {
15    pub fn new(jid: Jid) -> ClientBuilder {
16        ClientBuilder {
17            jid: jid,
18            host: None,
19            port: 5222,
20        }
21    }
22
23    pub fn host(mut self, host: String) -> ClientBuilder {
24        self.host = Some(host);
25        self
26    }
27
28    pub fn port(mut self, port: u16) -> ClientBuilder {
29        self.port = port;
30        self
31    }
32
33    pub fn connect(self) -> Result<Client, Error> {
34        let host = &self.host.unwrap_or(self.jid.domain.clone());
35        let mut transport = SslTransport::connect(host, self.port)?;
36        transport.write_event(XmlEvent::start_element("stream:stream")
37                                       .attr("to", &self.jid.domain)
38                                       .default_ns(ns::CLIENT)
39                                       .ns("stream", ns::STREAM))?;
40        Ok(Client {
41            jid: self.jid,
42            transport: transport
43        })
44    }
45}
46
47pub struct Client {
48    jid: Jid,
49    transport: SslTransport,
50}
51
52impl Client {
53    pub fn jid(&self) -> &Jid {
54        &self.jid
55    }
56}