xmpp-rs-mirror/tokio-xmpp/src/component/mod.rs

164 lines
5.5 KiB
Rust
Raw Normal View History

2018-08-02 17:58:19 +00:00
//! Components in XMPP are services/gateways that are logged into an
//! XMPP server under a JID consisting of just a domain name. They are
//! allowed to use any user and resource identifiers in their stanzas.
2018-12-18 18:04:31 +00:00
use futures::{done, Async, AsyncSink, Future, Poll, Sink, StartSend, Stream};
2017-07-22 00:59:51 +00:00
use std::mem::replace;
use std::str::FromStr;
2018-09-01 19:59:02 +00:00
use tokio::net::TcpStream;
2017-07-22 00:59:51 +00:00
use tokio_io::{AsyncRead, AsyncWrite};
use xmpp_parsers::{Element, Jid, JidParseError};
2017-07-22 00:59:51 +00:00
2018-12-18 18:04:31 +00:00
use super::event::Event;
use super::happy_eyeballs::Connecter;
2017-07-22 00:59:51 +00:00
use super::xmpp_codec::Packet;
use super::xmpp_stream;
2018-09-06 15:46:06 +00:00
use super::Error;
2017-07-22 00:59:51 +00:00
mod auth;
use self::auth::ComponentAuth;
2018-08-02 17:58:19 +00:00
/// Component connection to an XMPP server
2017-07-22 00:59:51 +00:00
pub struct Component {
2018-08-02 17:58:19 +00:00
/// The component's Jabber-Id
2017-07-22 00:59:51 +00:00
pub jid: Jid,
state: ComponentState,
}
type XMPPStream = xmpp_stream::XMPPStream<TcpStream>;
const NS_JABBER_COMPONENT_ACCEPT: &str = "jabber:component:accept";
enum ComponentState {
Invalid,
Disconnected,
Connecting(Box<dyn Future<Item = XMPPStream, Error = Error>>),
2017-07-22 00:59:51 +00:00
Connected(XMPPStream),
}
impl Component {
2018-08-02 17:58:19 +00:00
/// Start a new XMPP component
///
/// Start polling the returned instance so that it will connect
/// and yield events.
2018-09-01 19:59:02 +00:00
pub fn new(jid: &str, password: &str, server: &str, port: u16) -> Result<Self, JidParseError> {
2018-08-02 18:10:26 +00:00
let jid = Jid::from_str(jid)?;
2017-07-22 00:59:51 +00:00
let password = password.to_owned();
2018-09-01 19:59:02 +00:00
let connect = Self::make_connect(jid.clone(), password, server, port);
2017-07-22 00:59:51 +00:00
Ok(Component {
jid,
2018-09-06 15:46:06 +00:00
state: ComponentState::Connecting(Box::new(connect)),
2017-07-22 00:59:51 +00:00
})
}
2018-12-18 18:04:31 +00:00
fn make_connect(
jid: Jid,
password: String,
server: &str,
port: u16,
) -> impl Future<Item = XMPPStream, Error = Error> {
2017-07-22 00:59:51 +00:00
let jid1 = jid.clone();
let password = password;
2018-09-06 22:12:00 +00:00
done(Connecter::from_lookup(server, None, port))
.flatten()
2018-09-06 16:20:05 +00:00
.and_then(move |tcp_stream| {
2018-12-18 18:04:31 +00:00
xmpp_stream::XMPPStream::start(
tcp_stream,
jid1,
NS_JABBER_COMPONENT_ACCEPT.to_owned(),
)
2018-09-06 15:46:06 +00:00
})
2018-12-18 18:04:31 +00:00
.and_then(move |xmpp_stream| Self::auth(xmpp_stream, password).expect("auth"))
2017-07-22 00:59:51 +00:00
}
2018-12-18 18:04:31 +00:00
fn auth<S: AsyncRead + AsyncWrite>(
stream: xmpp_stream::XMPPStream<S>,
password: String,
) -> Result<ComponentAuth<S>, Error> {
2017-07-22 00:59:51 +00:00
ComponentAuth::new(stream, password)
}
}
impl Stream for Component {
type Item = Event;
2018-09-06 15:46:06 +00:00
type Error = Error;
2017-07-22 00:59:51 +00:00
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
let state = replace(&mut self.state, ComponentState::Invalid);
match state {
2018-12-18 18:04:31 +00:00
ComponentState::Invalid => Err(Error::InvalidState),
ComponentState::Disconnected => Ok(Async::Ready(None)),
ComponentState::Connecting(mut connect) => match connect.poll() {
Ok(Async::Ready(stream)) => {
self.state = ComponentState::Connected(stream);
Ok(Async::Ready(Some(Event::Online(self.jid.clone()))))
2018-12-18 18:04:31 +00:00
}
Ok(Async::NotReady) => {
self.state = ComponentState::Connecting(connect);
Ok(Async::NotReady)
2017-07-22 00:59:51 +00:00
}
2018-12-18 18:04:31 +00:00
Err(e) => Err(e),
2017-07-22 00:59:51 +00:00
},
ComponentState::Connected(mut stream) => {
// Poll sink
match stream.poll_complete() {
Ok(Async::NotReady) => (),
Ok(Async::Ready(())) => (),
2018-12-18 18:04:31 +00:00
Err(e) => return Err(e)?,
};
// Poll stream
2017-07-22 00:59:51 +00:00
match stream.poll() {
Ok(Async::NotReady) => {
self.state = ComponentState::Connected(stream);
Ok(Async::NotReady)
2018-12-18 18:04:31 +00:00
}
2017-07-22 00:59:51 +00:00
Ok(Async::Ready(None)) => {
// EOF
self.state = ComponentState::Disconnected;
Ok(Async::Ready(Some(Event::Disconnected)))
2018-12-18 18:04:31 +00:00
}
2017-07-22 00:59:51 +00:00
Ok(Async::Ready(Some(Packet::Stanza(stanza)))) => {
self.state = ComponentState::Connected(stream);
Ok(Async::Ready(Some(Event::Stanza(stanza))))
2018-12-18 18:04:31 +00:00
}
2017-07-22 00:59:51 +00:00
Ok(Async::Ready(_)) => {
self.state = ComponentState::Connected(stream);
Ok(Async::NotReady)
2018-12-18 18:04:31 +00:00
}
Err(e) => Err(e)?,
2017-07-22 00:59:51 +00:00
}
2018-12-18 18:04:31 +00:00
}
2017-07-22 00:59:51 +00:00
}
}
}
impl Sink for Component {
type SinkItem = Element;
2018-09-06 16:20:05 +00:00
type SinkError = Error;
2017-07-22 00:59:51 +00:00
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
match self.state {
2018-12-18 18:04:31 +00:00
ComponentState::Connected(ref mut stream) => match stream
.start_send(Packet::Stanza(item))
{
Ok(AsyncSink::NotReady(Packet::Stanza(stanza))) => Ok(AsyncSink::NotReady(stanza)),
Ok(AsyncSink::NotReady(_)) => {
panic!("Component.start_send with stanza but got something else back")
}
Ok(AsyncSink::Ready) => Ok(AsyncSink::Ready),
Err(e) => Err(e)?,
},
_ => Ok(AsyncSink::NotReady(item)),
2017-07-22 00:59:51 +00:00
}
}
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
match &mut self.state {
2018-12-18 18:04:31 +00:00
&mut ComponentState::Connected(ref mut stream) => {
stream.poll_complete().map_err(|e| e.into())
}
_ => Ok(Async::Ready(())),
2017-07-22 00:59:51 +00:00
}
}
}