2017-06-05 00:50:22 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use futures::*;
|
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
|
|
|
use tokio_io::codec::Framed;
|
|
|
|
use xml;
|
2017-06-13 23:55:56 +00:00
|
|
|
use jid::Jid;
|
2017-06-05 00:50:22 +00:00
|
|
|
|
|
|
|
use xmpp_codec::*;
|
|
|
|
use stream_start::*;
|
|
|
|
|
|
|
|
pub const NS_XMPP_STREAM: &str = "http://etherx.jabber.org/streams";
|
|
|
|
|
|
|
|
pub struct XMPPStream<S> {
|
2017-06-13 23:55:56 +00:00
|
|
|
pub jid: Jid,
|
2017-06-05 00:50:22 +00:00
|
|
|
pub stream: Framed<S, XMPPCodec>,
|
|
|
|
pub stream_attrs: HashMap<String, String>,
|
|
|
|
pub stream_features: xml::Element,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: AsyncRead + AsyncWrite> XMPPStream<S> {
|
2017-06-13 23:55:56 +00:00
|
|
|
pub fn new(jid: Jid,
|
|
|
|
stream: Framed<S, XMPPCodec>,
|
|
|
|
stream_attrs: HashMap<String, String>,
|
|
|
|
stream_features: xml::Element) -> Self {
|
|
|
|
XMPPStream { jid, stream, stream_attrs, stream_features }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_stream(stream: S, jid: Jid) -> StreamStart<S> {
|
2017-06-05 00:50:22 +00:00
|
|
|
let xmpp_stream = AsyncRead::framed(stream, XMPPCodec::new());
|
2017-06-13 23:55:56 +00:00
|
|
|
StreamStart::from_stream(xmpp_stream, jid)
|
2017-06-05 00:50:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn into_inner(self) -> S {
|
|
|
|
self.stream.into_inner()
|
|
|
|
}
|
|
|
|
|
2017-06-05 23:38:48 +00:00
|
|
|
pub fn restart(self) -> StreamStart<S> {
|
2017-06-13 23:55:56 +00:00
|
|
|
Self::from_stream(self.stream.into_inner(), self.jid)
|
2017-06-05 23:38:48 +00:00
|
|
|
}
|
2017-06-05 00:50:22 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|