Add server and port to connect_with_jid method on async_client

This commit is contained in:
Paul Fariello 2020-09-11 17:05:49 +02:00
commit c6376e1d28

View file

@ -31,6 +31,8 @@ pub struct Client {
jid: Jid, jid: Jid,
password: String, password: String,
reconnect: bool, reconnect: bool,
server: String,
port: u16,
// TODO: tls_required=true // TODO: tls_required=true
} }
@ -50,19 +52,27 @@ impl Client {
/// and yield events. /// and yield events.
pub fn new<P: Into<String>>(jid: &str, password: P) -> Result<Self, JidParseError> { pub fn new<P: Into<String>>(jid: &str, password: P) -> Result<Self, JidParseError> {
let jid = Jid::from_str(jid)?; let jid = Jid::from_str(jid)?;
let client = Self::new_with_jid(jid, password.into()); let server = jid.clone().domain();
let client = Self::new_with_jid(jid, password.into(), server, 5222);
Ok(client) Ok(client)
} }
/// Start a new client given that the JID is already parsed. /// Start a new client given that the JID is already parsed.
pub fn new_with_jid(jid: Jid, password: String) -> Self { pub fn new_with_jid(jid: Jid, password: String, server: String, port: u16) -> Self {
let local = LocalSet::new(); let local = LocalSet::new();
let connect = local.spawn_local(Self::connect(jid.clone(), password.clone())); let connect = local.spawn_local(Self::connect(
server.clone(),
port,
jid.clone(),
password.clone(),
));
let client = Client { let client = Client {
jid, jid,
password, password,
state: ClientState::Connecting(connect, local), state: ClientState::Connecting(connect, local),
reconnect: false, reconnect: false,
server: server,
port: port,
}; };
client client
} }
@ -74,13 +84,18 @@ impl Client {
self self
} }
async fn connect(jid: Jid, password: String) -> Result<XMPPStream, Error> { async fn connect(
server: String,
port: u16,
jid: Jid,
password: String,
) -> Result<XMPPStream, Error> {
let username = jid.clone().node().unwrap(); let username = jid.clone().node().unwrap();
let password = password; let password = password;
let domain = idna::domain_to_ascii(&jid.clone().domain()).map_err(|_| Error::Idna)?; let domain = idna::domain_to_ascii(&server).map_err(|_| Error::Idna)?;
// TCP connection // TCP connection
let tcp_stream = connect(&domain, Some("_xmpp-client._tcp"), 5222).await?; let tcp_stream = connect(&domain, Some("_xmpp-client._tcp"), port).await?;
// Unencryped XMPPStream // Unencryped XMPPStream
let xmpp_stream = let xmpp_stream =
@ -162,8 +177,12 @@ impl Stream for Client {
ClientState::Disconnected if self.reconnect => { ClientState::Disconnected if self.reconnect => {
// TODO: add timeout // TODO: add timeout
let mut local = LocalSet::new(); let mut local = LocalSet::new();
let connect = let connect = local.spawn_local(Self::connect(
local.spawn_local(Self::connect(self.jid.clone(), self.password.clone())); self.server.clone(),
self.port,
self.jid.clone(),
self.password.clone(),
));
let _ = Pin::new(&mut local).poll(cx); let _ = Pin::new(&mut local).poll(cx);
self.state = ClientState::Connecting(connect, local); self.state = ClientState::Connecting(connect, local);
self.poll_next(cx) self.poll_next(cx)