this kinda works
This commit is contained in:
parent
212d9e7e7e
commit
005013f37c
3 changed files with 90 additions and 70 deletions
|
@ -4,6 +4,6 @@ version = "0.1.0"
|
||||||
authors = ["Astro <astro@spaceboyz.net>"]
|
authors = ["Astro <astro@spaceboyz.net>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
futures = "0.1.6"
|
futures = "*"
|
||||||
tokio-core = "0.1.1"
|
tokio-core = "*"
|
||||||
RustyXML = "0.1.1"
|
RustyXML = "*"
|
||||||
|
|
149
src/lib.rs
149
src/lib.rs
|
@ -1,3 +1,4 @@
|
||||||
|
#[macro_use]
|
||||||
extern crate futures;
|
extern crate futures;
|
||||||
extern crate tokio_core;
|
extern crate tokio_core;
|
||||||
extern crate xml;
|
extern crate xml;
|
||||||
|
@ -6,12 +7,12 @@ use std::net::SocketAddr;
|
||||||
use std::net::ToSocketAddrs;
|
use std::net::ToSocketAddrs;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::io::ErrorKind;
|
use std::io::ErrorKind;
|
||||||
use futures::{Future, BoxFuture, Sink, Poll};
|
use futures::{Future, BoxFuture, Sink, Poll, Async};
|
||||||
use futures::stream::{Stream, iter};
|
use futures::stream::{Stream, iter};
|
||||||
use futures::future::result;
|
use futures::future::result;
|
||||||
use tokio_core::reactor::Handle;
|
use tokio_core::reactor::Handle;
|
||||||
use tokio_core::io::Io;
|
use tokio_core::io::Io;
|
||||||
use tokio_core::net::TcpStream;
|
use tokio_core::net::{TcpStream, TcpStreamNew};
|
||||||
|
|
||||||
mod xmpp_codec;
|
mod xmpp_codec;
|
||||||
use xmpp_codec::*;
|
use xmpp_codec::*;
|
||||||
|
@ -19,92 +20,108 @@ use xmpp_codec::*;
|
||||||
|
|
||||||
// type FullClient = sasl::Client<StartTLS<TCPConnection>>
|
// type FullClient = sasl::Client<StartTLS<TCPConnection>>
|
||||||
|
|
||||||
type Event = ();
|
#[derive(Debug)]
|
||||||
type Error = std::io::Error;
|
pub struct TcpClient {
|
||||||
|
state: TcpClientState,
|
||||||
struct TCPStream {
|
|
||||||
source: Box<Stream<Item=Event, Error=std::io::Error>>,
|
|
||||||
sink: Arc<Box<futures::stream::SplitSink<tokio_core::io::Framed<tokio_core::net::TcpStream, xmpp_codec::XMPPCodec>>>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TCPStream {
|
enum TcpClientState {
|
||||||
pub fn connect(addr: &SocketAddr, handle: &Handle) -> BoxFuture<Arc<TCPStream>, std::io::Error> {
|
Connecting(TcpStreamNew),
|
||||||
TcpStream::connect(addr, handle)
|
SendStart(futures::sink::Send<XMPPStream<TcpStream>>),
|
||||||
.and_then(|stream| {
|
RecvStart(Option<XMPPStream<TcpStream>>),
|
||||||
let (sink, source) = stream.framed(XMPPCodec::new())
|
Established,
|
||||||
// .framed(UTF8Codec::new())
|
Invalid,
|
||||||
.split();
|
}
|
||||||
|
|
||||||
sink.send(Packet::StreamStart)
|
|
||||||
.and_then(|sink| result(Ok((Arc::new(Box::new(sink)), source))))
|
|
||||||
})
|
|
||||||
.and_then(|(sink, source)| {
|
|
||||||
let sink1 = sink.clone();
|
|
||||||
let source = source
|
|
||||||
.map(|items| iter(items.into_iter().map(Ok)))
|
|
||||||
.flatten()
|
|
||||||
.filter_map(move |pkt| Self::process_packet(pkt, &sink1))
|
|
||||||
// .for_each(|ev| {
|
|
||||||
// match ev {
|
|
||||||
// Packet::Stanza
|
|
||||||
// _ => (),
|
|
||||||
// }
|
|
||||||
// Ok(println!("xmpp: {:?}", ev))
|
|
||||||
// })
|
|
||||||
// .boxed();
|
|
||||||
;
|
|
||||||
result(Ok(Arc::new(TCPStream {
|
|
||||||
source: Box::new(source),
|
|
||||||
sink: sink,
|
|
||||||
})))
|
|
||||||
}).boxed()
|
|
||||||
//.map_err(|e| std::io::Error::new(ErrorKind::Other, e));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn process_packet<S>(pkt: Packet, sink: &Arc<S>) -> Option<Event>
|
impl std::fmt::Debug for TcpClientState {
|
||||||
where S: Sink<SinkItem=Packet, SinkError=std::io::Error> {
|
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
|
||||||
|
let s = match *self {
|
||||||
println!("pkt: {:?}", pkt);
|
TcpClientState::Connecting(_) => "Connecting",
|
||||||
None
|
TcpClientState::SendStart(_) => "SendStart",
|
||||||
|
TcpClientState::RecvStart(_) => "RecvStart",
|
||||||
|
TcpClientState::Established => "Established",
|
||||||
|
TcpClientState::Invalid => "Invalid",
|
||||||
|
};
|
||||||
|
write!(fmt, "{}", s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ClientStream {
|
impl TcpClient {
|
||||||
inner: TCPStream,
|
pub fn connect(addr: &SocketAddr, handle: &Handle) -> Self {
|
||||||
|
let tcp_stream_new = TcpStream::connect(addr, handle);
|
||||||
|
TcpClient {
|
||||||
|
state: TcpClientState::Connecting(tcp_stream_new),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ClientStream {
|
impl Future for TcpClient {
|
||||||
pub fn connect(jid: &str, password: &str, handle: &Handle) -> Box<Future<Item=Self, Error=std::io::Error>> {
|
type Item = XMPPStream<TcpStream>;
|
||||||
let addr = "[2a01:4f8:a0:33d0::5]:5222"
|
type Error = std::io::Error;
|
||||||
.to_socket_addrs().unwrap()
|
|
||||||
.next().unwrap();
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
let stream =
|
let (new_state, result) = match self.state {
|
||||||
TCPStream::connect(&addr, handle)
|
TcpClientState::Connecting(ref mut tcp_stream_new) => {
|
||||||
.and_then(|stream| {
|
let tcp_stream = try_ready!(tcp_stream_new.poll());
|
||||||
Ok(ClientStream {
|
let xmpp_stream = tcp_stream.framed(XMPPCodec::new());
|
||||||
inner: stream
|
let send = xmpp_stream.send(Packet::StreamStart);
|
||||||
})
|
let new_state = TcpClientState::SendStart(send);
|
||||||
});
|
(new_state, Ok(Async::NotReady))
|
||||||
Box::new(stream)
|
},
|
||||||
|
TcpClientState::SendStart(ref mut send) => {
|
||||||
|
let xmpp_stream = try_ready!(send.poll());
|
||||||
|
let new_state = TcpClientState::RecvStart(Some(xmpp_stream));
|
||||||
|
(new_state, Ok(Async::NotReady))
|
||||||
|
},
|
||||||
|
TcpClientState::RecvStart(ref mut opt_xmpp_stream) => {
|
||||||
|
let mut xmpp_stream = opt_xmpp_stream.take().unwrap();
|
||||||
|
match xmpp_stream.poll() {
|
||||||
|
Ok(Async::Ready(Some(events))) => println!("Recv start: {:?}", events),
|
||||||
|
Ok(Async::Ready(_)) => return Err(std::io::Error::from(ErrorKind::InvalidData)),
|
||||||
|
Ok(Async::NotReady) => {
|
||||||
|
*opt_xmpp_stream = Some(xmpp_stream);
|
||||||
|
return Ok(Async::NotReady);
|
||||||
|
},
|
||||||
|
Err(e) => return Err(e)
|
||||||
|
};
|
||||||
|
let new_state = TcpClientState::Established;
|
||||||
|
(new_state, Ok(Async::Ready(xmpp_stream)))
|
||||||
|
},
|
||||||
|
TcpClientState::Established | TcpClientState::Invalid =>
|
||||||
|
unreachable!(),
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("Next state: {:?}", new_state);
|
||||||
|
self.state = new_state;
|
||||||
|
match result {
|
||||||
|
// by polling again, we register new future
|
||||||
|
Ok(Async::NotReady) => self.poll(),
|
||||||
|
result => result
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use tokio_core::reactor::Core;
|
use tokio_core::reactor::Core;
|
||||||
|
use futures::{Future, Stream};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_works() {
|
fn it_works() {
|
||||||
|
use std::net::ToSocketAddrs;
|
||||||
|
let addr = "[2a01:4f8:a0:33d0::5]:5222"
|
||||||
|
.to_socket_addrs().unwrap()
|
||||||
|
.next().unwrap();
|
||||||
|
|
||||||
let mut core = Core::new().unwrap();
|
let mut core = Core::new().unwrap();
|
||||||
let client = super::ClientStream::connect(
|
let client = super::TcpClient::connect(
|
||||||
"astro@spaceboyz.net",
|
&addr,
|
||||||
"...",
|
|
||||||
&core.handle()
|
&core.handle()
|
||||||
).and_then(|stream| {
|
).and_then(|stream| {
|
||||||
stream.inner.source.boxed().for_each(|item| {
|
stream.for_each(|item| {
|
||||||
Ok(println!("stream item: {:?}", item))
|
Ok(println!("stream item: {:?}", item))
|
||||||
})
|
})
|
||||||
}).boxed();
|
});
|
||||||
core.run(client).unwrap();
|
core.run(client).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ use std;
|
||||||
use std::str::from_utf8;
|
use std::str::from_utf8;
|
||||||
use std::io::{Error, ErrorKind};
|
use std::io::{Error, ErrorKind};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use tokio_core::io::{Codec, EasyBuf};
|
use tokio_core::io::{Codec, EasyBuf, Framed};
|
||||||
use xml;
|
use xml;
|
||||||
|
|
||||||
const NS_XMLNS: &'static str = "http://www.w3.org/2000/xmlns/";
|
const NS_XMLNS: &'static str = "http://www.w3.org/2000/xmlns/";
|
||||||
|
@ -51,6 +51,8 @@ pub enum Packet {
|
||||||
StreamEnd,
|
StreamEnd,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub type XMPPStream<T> = Framed<T, XMPPCodec>;
|
||||||
|
|
||||||
pub struct XMPPCodec {
|
pub struct XMPPCodec {
|
||||||
parser: xml::Parser,
|
parser: xml::Parser,
|
||||||
root: Option<XMPPRoot>,
|
root: Option<XMPPRoot>,
|
||||||
|
@ -70,6 +72,7 @@ impl Codec for XMPPCodec {
|
||||||
type Out = Packet;
|
type Out = Packet;
|
||||||
|
|
||||||
fn decode(&mut self, buf: &mut EasyBuf) -> Result<Option<Self::In>, Error> {
|
fn decode(&mut self, buf: &mut EasyBuf) -> Result<Option<Self::In>, Error> {
|
||||||
|
println!("XMPPCodec.decode {:?}", buf.len());
|
||||||
match from_utf8(buf.as_slice()) {
|
match from_utf8(buf.as_slice()) {
|
||||||
Ok(s) =>
|
Ok(s) =>
|
||||||
self.parser.feed_str(s),
|
self.parser.feed_str(s),
|
||||||
|
|
Loading…
Reference in a new issue