client.rs

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