impl stream for client
This commit is contained in:
parent
547902fe8f
commit
1e2672ba50
7 changed files with 254 additions and 70 deletions
|
|
@ -6,53 +6,85 @@ extern crate xml;
|
|||
|
||||
use std::str::FromStr;
|
||||
use tokio_core::reactor::Core;
|
||||
use futures::{Future, Stream, Sink};
|
||||
use jid::Jid;
|
||||
use tokio_xmpp::TcpClient;
|
||||
use futures::{Future, Stream, Sink, future};
|
||||
use tokio_xmpp::{Client, ClientEvent};
|
||||
use tokio_xmpp::xmpp_codec::Packet;
|
||||
|
||||
fn main() {
|
||||
let jid = Jid::from_str("astrobot@example.net").expect("JID");
|
||||
let password = "".to_owned();
|
||||
|
||||
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 client = TcpClient::connect(
|
||||
jid.clone(),
|
||||
&addr,
|
||||
&core.handle()
|
||||
).map_err(|e| format!("{}", e)
|
||||
).and_then(|stream| {
|
||||
if stream.can_starttls() {
|
||||
stream.starttls()
|
||||
} else {
|
||||
panic!("No STARTTLS")
|
||||
}
|
||||
}).and_then(|stream| {
|
||||
let username = jid.node.as_ref().unwrap().to_owned();
|
||||
stream.auth(username, password).expect("auth")
|
||||
}).and_then(|stream| {
|
||||
stream.bind()
|
||||
}).and_then(|stream| {
|
||||
println!("Bound to {}", stream.jid);
|
||||
let client = Client::new("astrobot@example.org", "", &core.handle()).unwrap();
|
||||
// let client = TcpClient::connect(
|
||||
// jid.clone(),
|
||||
// &addr,
|
||||
// &core.handle()
|
||||
// ).map_err(|e| format!("{}", e)
|
||||
// ).and_then(|stream| {
|
||||
// if stream.can_starttls() {
|
||||
// stream.starttls()
|
||||
// } else {
|
||||
// panic!("No STARTTLS")
|
||||
// }
|
||||
// }).and_then(|stream| {
|
||||
// let username = jid.node.as_ref().unwrap().to_owned();
|
||||
// stream.auth(username, password).expect("auth")
|
||||
// }).and_then(|stream| {
|
||||
// stream.bind()
|
||||
// }).and_then(|stream| {
|
||||
// println!("Bound to {}", stream.jid);
|
||||
|
||||
let presence = xml::Element::new("presence".to_owned(), None, vec![]);
|
||||
stream.send(Packet::Stanza(presence))
|
||||
.map_err(|e| format!("{}", e))
|
||||
}).and_then(|stream| {
|
||||
stream.for_each(|event| {
|
||||
match event {
|
||||
Packet::Stanza(el) => println!("<< {}", el),
|
||||
_ => println!("!! {:?}", event),
|
||||
}
|
||||
Ok(())
|
||||
}).map_err(|e| format!("{}", e))
|
||||
// let presence = xml::Element::new("presence".to_owned(), None, vec![]);
|
||||
// stream.send(Packet::Stanza(presence))
|
||||
// .map_err(|e| format!("{}", e))
|
||||
// }).and_then(|stream| {
|
||||
// let main_loop = |stream| {
|
||||
// stream.into_future()
|
||||
// .and_then(|(event, stream)| {
|
||||
// stream.send(Packet::Stanza(unreachable!()))
|
||||
// }).and_then(main_loop)
|
||||
// };
|
||||
// main_loop(stream)
|
||||
// }).and_then(|(event, stream)| {
|
||||
// let (mut sink, stream) = stream.split();
|
||||
// stream.for_each(move |event| {
|
||||
// match event {
|
||||
// Packet::Stanza(ref message)
|
||||
// if message.name == "message" => {
|
||||
// let ty = message.get_attribute("type", None);
|
||||
// let body = message.get_child("body", Some("jabber:client"))
|
||||
// .map(|body_el| body_el.content_str());
|
||||
// match ty {
|
||||
// None | Some("normal") | Some("chat")
|
||||
// if body.is_some() => {
|
||||
// let from = message.get_attribute("from", None).unwrap();
|
||||
// println!("Got message from {}: {:?}", from, body);
|
||||
// let reply = make_reply(from, body.unwrap());
|
||||
// sink.send(Packet::Stanza(reply))
|
||||
// .and_then(|_| Ok(()))
|
||||
// },
|
||||
// _ => future::ok(()),
|
||||
// }
|
||||
// },
|
||||
// _ => future::ok(()),
|
||||
// }
|
||||
// }).map_err(|e| format!("{}", e))
|
||||
// });
|
||||
|
||||
let done = client.for_each(|event| {
|
||||
match event {
|
||||
ClientEvent::Online => {
|
||||
println!("Online!");
|
||||
},
|
||||
ClientEvent::Stanza(stanza) => {
|
||||
},
|
||||
_ => {
|
||||
println!("Event: {:?}", event);
|
||||
},
|
||||
}
|
||||
|
||||
Ok(())
|
||||
});
|
||||
match core.run(client) {
|
||||
|
||||
match core.run(done) {
|
||||
Ok(_) => (),
|
||||
Err(e) => {
|
||||
println!("Fatal: {}", e);
|
||||
|
|
@ -60,3 +92,15 @@ fn main() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn make_reply(to: &str, body: String) -> xml::Element {
|
||||
let mut message = xml::Element::new(
|
||||
"message".to_owned(),
|
||||
None,
|
||||
vec![("type".to_owned(), None, "chat".to_owned()),
|
||||
("to".to_owned(), None, to.to_owned())]
|
||||
);
|
||||
message.tag(xml::Element::new("body".to_owned(), None, vec![]))
|
||||
.text(body);
|
||||
message
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue