Use a Config struct for async_client
This commit is contained in:
parent
c6376e1d28
commit
fb1b4cdbbc
1 changed files with 24 additions and 17 deletions
|
@ -27,13 +27,18 @@ use crate::{Error, ProtocolError};
|
||||||
/// This implements the `futures` crate's [`Stream`](#impl-Stream) and
|
/// This implements the `futures` crate's [`Stream`](#impl-Stream) and
|
||||||
/// [`Sink`](#impl-Sink<Packet>) traits.
|
/// [`Sink`](#impl-Sink<Packet>) traits.
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
|
config: Config,
|
||||||
state: ClientState,
|
state: ClientState,
|
||||||
|
reconnect: bool,
|
||||||
|
// TODO: tls_required=true
|
||||||
|
}
|
||||||
|
|
||||||
|
/// XMMPP client configuration
|
||||||
|
pub struct Config {
|
||||||
jid: Jid,
|
jid: Jid,
|
||||||
password: String,
|
password: String,
|
||||||
reconnect: bool,
|
|
||||||
server: String,
|
server: String,
|
||||||
port: u16,
|
port: u16,
|
||||||
// TODO: tls_required=true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type XMPPStream = xmpp_stream::XMPPStream<TlsStream<TcpStream>>;
|
type XMPPStream = xmpp_stream::XMPPStream<TlsStream<TcpStream>>;
|
||||||
|
@ -52,27 +57,29 @@ 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 server = jid.clone().domain();
|
let config = Config {
|
||||||
let client = Self::new_with_jid(jid, password.into(), server, 5222);
|
jid: jid.clone(),
|
||||||
|
password: password.into(),
|
||||||
|
server: jid.clone().domain(),
|
||||||
|
port: 5222,
|
||||||
|
};
|
||||||
|
let client = Self::new_with_config(config);
|
||||||
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, server: String, port: u16) -> Self {
|
pub fn new_with_config(config: Config) -> Self {
|
||||||
let local = LocalSet::new();
|
let local = LocalSet::new();
|
||||||
let connect = local.spawn_local(Self::connect(
|
let connect = local.spawn_local(Self::connect(
|
||||||
server.clone(),
|
config.server.clone(),
|
||||||
port,
|
config.port,
|
||||||
jid.clone(),
|
config.jid.clone(),
|
||||||
password.clone(),
|
config.password.clone(),
|
||||||
));
|
));
|
||||||
let client = Client {
|
let client = Client {
|
||||||
jid,
|
config: config,
|
||||||
password,
|
|
||||||
state: ClientState::Connecting(connect, local),
|
state: ClientState::Connecting(connect, local),
|
||||||
reconnect: false,
|
reconnect: false,
|
||||||
server: server,
|
|
||||||
port: port,
|
|
||||||
};
|
};
|
||||||
client
|
client
|
||||||
}
|
}
|
||||||
|
@ -178,10 +185,10 @@ impl Stream for Client {
|
||||||
// TODO: add timeout
|
// TODO: add timeout
|
||||||
let mut local = LocalSet::new();
|
let mut local = LocalSet::new();
|
||||||
let connect = local.spawn_local(Self::connect(
|
let connect = local.spawn_local(Self::connect(
|
||||||
self.server.clone(),
|
self.config.server.clone(),
|
||||||
self.port,
|
self.config.port,
|
||||||
self.jid.clone(),
|
self.config.jid.clone(),
|
||||||
self.password.clone(),
|
self.config.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);
|
||||||
|
|
Loading…
Reference in a new issue