PHPCAQ6ZOH64J22YO3LAHZDZ6IWRKDT4CRRC4Y2I5ADERWPBRWIQC
pub fn xmpp_process<F>(signal: F) -> impl future::Future<Item = (), Error = tokio::io::Error>
impl<F> XmppState<F> {
fn new(cmd_recv: Receiver<XmppCommand>, signal: F) -> XmppState<F> {
XmppState { cmd_recv, signal }
}
}
pub fn xmpp_process<F>(
signal: F,
cmd_recv: Receiver<XmppCommand>,
) -> impl future::Future<Item = (), Error = tokio::io::Error>
signal.map_err(|_| tokio::io::Error::new(tokio::io::ErrorKind::Other, "Wrong shutdown signal"))
let signal = signal
.map_err(|_| tokio::io::Error::new(tokio::io::ErrorKind::Other, "Wrong shutdown signal"));
future::loop_fn(XmppState::new(cmd_recv, signal), |s| {
s.signal.select2(s.cmd_recv.into_future()).then(|r| {
match r {
Ok(Either::A((_x, _b))) => {
// got signal, breaks
future::ok(future::Loop::Break(()))
}
Ok(Either::B((x, a))) => {
// got cmd, continue
future::ok(future::Loop::Continue(XmppState::new(x.1, a)))
}
Err(Either::A((e, _b))) => {
// got signal error, breaks
error!("Signal error: {}", e);
future::ok(future::Loop::Break(()))
}
Err(Either::B((_e, _a))) => {
// got cmd error, its bad
future::err(tokio::io::Error::new(
tokio::io::ErrorKind::Other,
"Cmd error",
))
}
}
})
})
.serve(|| service_fn_ok(hello_world))
.with_graceful_shutdown(ctrl_c.clone().map(|_| ()))
.serve(move || {
let cmd_send = cmd_send.clone();
service_fn(move |_req: Request<Body>| {
info!("Got request");
cmd_send.clone().send(XmppCommand {}).then(|r| match r {
Ok(_) => tokio::prelude::future::ok(Response::new(Body::from("Accepted"))),
Err(e) => {
error!("Command sent error: {}", e);
tokio::prelude::future::result(
Response::builder()
.status(hyper::StatusCode::BAD_REQUEST)
.body(Body::from(format!("Command sent error: {}", e))),
)
}
})
})
}).with_graceful_shutdown(ctrl_c.clone().map(|_| ()))
hyper::rt::run(http_server);
let xmpp_join = std::thread::spawn(move || -> Result<(), tokio::io::Error> {
// Launch single-threaded runtime
let mut ctrt = current_thread::Runtime::new()?;
ctrt.block_on(xmpp_process(ctrl_c.clone().map(|_| ()), cmd_recv))
});
info!("Server started");
rt.shutdown_on_idle().wait().expect("Shutdown error");
info!("Server stopper");
xmpp_join
.join()
.expect("Join xmpp thread")
.expect("Result xmpp thread");