improve echo_bot example
This commit is contained in:
parent
2654722fa3
commit
1d69b1a256
1 changed files with 27 additions and 22 deletions
|
@ -24,17 +24,32 @@ fn main() {
|
||||||
// Make the two interfaces for sending and receiving independent
|
// Make the two interfaces for sending and receiving independent
|
||||||
// of each other so we can move one into a closure.
|
// of each other so we can move one into a closure.
|
||||||
let (sink, stream) = client.split();
|
let (sink, stream) = client.split();
|
||||||
let mut sink_state = Some(sink);
|
|
||||||
// Main loop, processes events
|
|
||||||
let done = stream.for_each(move |event| {
|
|
||||||
let mut sink_future = None;
|
|
||||||
|
|
||||||
if event.is_online() {
|
// Create outgoing pipe
|
||||||
|
let (mut tx, rx) = futures::unsync::mpsc::unbounded();
|
||||||
|
rt.spawn(
|
||||||
|
rx.forward(
|
||||||
|
sink.sink_map_err(|_| panic!("Pipe"))
|
||||||
|
)
|
||||||
|
.map(|(rx, mut sink)| {
|
||||||
|
drop(rx);
|
||||||
|
let _ = sink.close();
|
||||||
|
})
|
||||||
|
.map_err(|e| {
|
||||||
|
panic!("Send error: {:?}", e);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
// Main loop, processes events
|
||||||
|
let mut wait_for_stream_end = false;
|
||||||
|
let done = stream.for_each(move |event| {
|
||||||
|
if wait_for_stream_end {
|
||||||
|
/* Do nothing */
|
||||||
|
} else if event.is_online() {
|
||||||
println!("Online!");
|
println!("Online!");
|
||||||
|
|
||||||
let presence = make_presence();
|
let presence = make_presence();
|
||||||
let sink = sink_state.take().unwrap();
|
tx.start_send(Packet::Stanza(presence)).unwrap();
|
||||||
sink_future = Some(Box::new(sink.send(Packet::Stanza(presence))));
|
|
||||||
} else if let Some(message) = event
|
} else if let Some(message) = event
|
||||||
.into_stanza()
|
.into_stanza()
|
||||||
.and_then(|stanza| Message::try_from(stanza).ok())
|
.and_then(|stanza| Message::try_from(stanza).ok())
|
||||||
|
@ -42,31 +57,21 @@ fn main() {
|
||||||
match (message.from, message.bodies.get("")) {
|
match (message.from, message.bodies.get("")) {
|
||||||
(Some(ref from), Some(ref body)) if body.0 == "die" => {
|
(Some(ref from), Some(ref body)) if body.0 == "die" => {
|
||||||
println!("Secret die command triggered by {}", from);
|
println!("Secret die command triggered by {}", from);
|
||||||
let sink = sink_state.take().unwrap();
|
wait_for_stream_end = true;
|
||||||
sink_future = Some(Box::new(sink.send(Packet::StreamEnd)));
|
tx.start_send(Packet::StreamEnd).unwrap();
|
||||||
}
|
}
|
||||||
(Some(ref from), Some(ref body)) => {
|
(Some(ref from), Some(ref body)) => {
|
||||||
if message.type_ != MessageType::Error {
|
if message.type_ != MessageType::Error {
|
||||||
// This is a message we'll echo
|
// This is a message we'll echo
|
||||||
let reply = make_reply(from.clone(), &body.0);
|
let reply = make_reply(from.clone(), &body.0);
|
||||||
let sink = sink_state.take().unwrap();
|
tx.start_send(Packet::Stanza(reply)).unwrap();
|
||||||
sink_future = Some(Box::new(sink.send(Packet::Stanza(reply))));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
sink_future
|
future::ok(())
|
||||||
.map(|future| {
|
|
||||||
let wait_send: Box<Future<Item = (), Error = tokio_xmpp::Error>> =
|
|
||||||
Box::new(future
|
|
||||||
.map(|sink| {
|
|
||||||
sink_state = Some(sink);
|
|
||||||
}));
|
|
||||||
wait_send
|
|
||||||
})
|
|
||||||
.unwrap_or_else(|| Box::new(future::ok(())))
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start polling `done`
|
// Start polling `done`
|
||||||
|
|
Loading…
Reference in a new issue