2018-08-02 17:58:19 +00:00
|
|
|
//! `XMPPStream` is the common container for all XMPP network connections
|
|
|
|
|
2018-08-01 22:19:06 +00:00
|
|
|
use futures::sink::Send;
|
2018-12-18 18:04:31 +00:00
|
|
|
use futures::{Poll, Sink, StartSend, Stream};
|
2017-06-13 23:55:56 +00:00
|
|
|
use jid::Jid;
|
2018-12-18 18:04:31 +00:00
|
|
|
use minidom::Element;
|
|
|
|
use tokio_codec::Framed;
|
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
2017-06-05 00:50:22 +00:00
|
|
|
|
2018-12-18 17:29:31 +00:00
|
|
|
use crate::stream_start::StreamStart;
|
2018-12-18 18:04:31 +00:00
|
|
|
use crate::xmpp_codec::{Packet, XMPPCodec};
|
2017-06-05 00:50:22 +00:00
|
|
|
|
2018-08-02 17:58:19 +00:00
|
|
|
/// <stream:stream> namespace
|
2017-06-05 00:50:22 +00:00
|
|
|
pub const NS_XMPP_STREAM: &str = "http://etherx.jabber.org/streams";
|
|
|
|
|
2018-08-02 17:58:19 +00:00
|
|
|
/// Wraps a `stream`
|
2017-06-05 00:50:22 +00:00
|
|
|
pub struct XMPPStream<S> {
|
2018-08-02 17:58:19 +00:00
|
|
|
/// The local Jabber-Id
|
2017-06-13 23:55:56 +00:00
|
|
|
pub jid: Jid,
|
2018-08-02 17:58:19 +00:00
|
|
|
/// Codec instance
|
2017-06-05 00:50:22 +00:00
|
|
|
pub stream: Framed<S, XMPPCodec>,
|
2018-08-02 17:58:19 +00:00
|
|
|
/// `<stream:features/>` for XMPP version 1.0
|
2017-07-17 18:53:00 +00:00
|
|
|
pub stream_features: Element,
|
2018-08-02 17:58:19 +00:00
|
|
|
/// Root namespace
|
|
|
|
///
|
|
|
|
/// This is different for either c2s, s2s, or component
|
|
|
|
/// connections.
|
2017-07-18 23:02:45 +00:00
|
|
|
pub ns: String,
|
2017-06-05 00:50:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: AsyncRead + AsyncWrite> XMPPStream<S> {
|
2018-08-02 17:58:19 +00:00
|
|
|
/// Constructor
|
2018-12-18 18:04:31 +00:00
|
|
|
pub fn new(
|
|
|
|
jid: Jid,
|
|
|
|
stream: Framed<S, XMPPCodec>,
|
|
|
|
ns: String,
|
|
|
|
stream_features: Element,
|
|
|
|
) -> Self {
|
|
|
|
XMPPStream {
|
|
|
|
jid,
|
|
|
|
stream,
|
|
|
|
stream_features,
|
|
|
|
ns,
|
|
|
|
}
|
2017-06-13 23:55:56 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 17:58:19 +00:00
|
|
|
/// Send a `<stream:stream>` start tag
|
2017-07-18 23:02:45 +00:00
|
|
|
pub fn start(stream: S, jid: Jid, ns: String) -> StreamStart<S> {
|
2018-07-31 21:34:04 +00:00
|
|
|
let xmpp_stream = Framed::new(stream, XMPPCodec::new());
|
2017-07-18 23:02:45 +00:00
|
|
|
StreamStart::from_stream(xmpp_stream, jid, ns)
|
2017-06-05 00:50:22 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 17:58:19 +00:00
|
|
|
/// Unwraps the inner stream
|
2017-06-05 00:50:22 +00:00
|
|
|
pub fn into_inner(self) -> S {
|
|
|
|
self.stream.into_inner()
|
|
|
|
}
|
|
|
|
|
2018-08-02 17:58:19 +00:00
|
|
|
/// Re-run `start()`
|
2017-06-05 23:38:48 +00:00
|
|
|
pub fn restart(self) -> StreamStart<S> {
|
2017-07-18 23:02:45 +00:00
|
|
|
Self::start(self.stream.into_inner(), self.jid, self.ns)
|
2017-06-05 23:38:48 +00:00
|
|
|
}
|
2017-06-05 00:50:22 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 22:19:06 +00:00
|
|
|
impl<S: AsyncWrite> XMPPStream<S> {
|
|
|
|
/// Convenience method
|
|
|
|
pub fn send_stanza<E: Into<Element>>(self, e: E) -> Send<Self> {
|
|
|
|
self.send(Packet::Stanza(e.into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-05 23:29:20 +00:00
|
|
|
/// Proxy to self.stream
|
2017-06-05 00:50:22 +00:00
|
|
|
impl<S: AsyncWrite> Sink for XMPPStream<S> {
|
|
|
|
type SinkItem = <Framed<S, XMPPCodec> as Sink>::SinkItem;
|
|
|
|
type SinkError = <Framed<S, XMPPCodec> as Sink>::SinkError;
|
|
|
|
|
|
|
|
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
|
|
|
|
self.stream.start_send(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
|
|
|
|
self.stream.poll_complete()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-05 23:29:20 +00:00
|
|
|
/// Proxy to self.stream
|
2017-06-05 00:50:22 +00:00
|
|
|
impl<S: AsyncRead> Stream for XMPPStream<S> {
|
|
|
|
type Item = <Framed<S, XMPPCodec> as Stream>::Item;
|
|
|
|
type Error = <Framed<S, XMPPCodec> as Stream>::Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
|
|
|
self.stream.poll()
|
|
|
|
}
|
|
|
|
}
|