xmpp-rs-mirror/src/client.rs

58 lines
1.3 KiB
Rust
Raw Normal View History

2017-02-18 21:16:20 +00:00
use jid::Jid;
2017-02-19 14:25:18 +00:00
use transport::{Transport, SslTransport};
2017-02-18 23:01:39 +00:00
use error::Error;
2017-02-19 14:25:18 +00:00
use ns;
use xml::writer::XmlEvent as WriterEvent;
use xml::reader::XmlEvent as ReaderEvent;
2017-02-18 21:16:20 +00:00
2017-02-18 23:01:39 +00:00
pub struct ClientBuilder {
jid: Jid,
host: Option<String>,
port: u16,
2017-02-18 21:16:20 +00:00
}
2017-02-18 23:01:39 +00:00
impl ClientBuilder {
pub fn new(jid: Jid) -> ClientBuilder {
ClientBuilder {
jid: jid,
host: None,
port: 5222,
}
}
pub fn host(mut self, host: String) -> ClientBuilder {
self.host = Some(host);
self
}
pub fn port(mut self, port: u16) -> ClientBuilder {
self.port = port;
self
}
pub fn connect(self) -> Result<Client, Error> {
let host = &self.host.unwrap_or(self.jid.domain.clone());
2017-02-19 14:25:18 +00:00
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))?;
2017-02-18 23:01:39 +00:00
Ok(Client {
jid: self.jid,
transport: transport
})
2017-02-18 21:16:20 +00:00
}
}
2017-02-18 23:01:39 +00:00
pub struct Client {
jid: Jid,
transport: SslTransport,
}
impl Client {
2017-02-19 14:06:14 +00:00
pub fn jid(&self) -> &Jid {
&self.jid
}
2017-02-18 23:01:39 +00:00
}