Skip to content

Commit 49e878b

Browse files
committed
Used util_termination in examples.
1 parent 8521c70 commit 49e878b

File tree

3 files changed

+11
-26
lines changed
  • example_apps/example_util/src
  • server/swimos_server_app/src/server
  • swimos/src

3 files changed

+11
-26
lines changed

example_apps/example_util/src/lib.rs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,24 @@ use std::{
1818
net::SocketAddr,
1919
};
2020

21-
use swimos::server::ServerHandle;
21+
use swimos::server::{until_termination, ServerHandle};
2222
use tokio::{select, sync::oneshot};
2323
use tracing_subscriber::{filter::LevelFilter, EnvFilter};
2424

2525
pub async fn manage_handle(handle: ServerHandle) {
26-
manage_handle_report(handle, None).await
26+
until_termination(handle, None).await.expect("Failed to register interrupt handler.");
2727
}
2828
pub async fn manage_handle_report(
29-
mut handle: ServerHandle,
29+
handle: ServerHandle,
3030
bound: Option<oneshot::Sender<SocketAddr>>,
3131
) {
32-
let mut shutdown_hook = Box::pin(async {
33-
tokio::signal::ctrl_c()
34-
.await
35-
.expect("Failed to register interrupt handler.");
36-
});
37-
let print_addr = handle.bound_addr();
38-
39-
let maybe_addr = select! {
40-
_ = &mut shutdown_hook => None,
41-
maybe_addr = print_addr => maybe_addr,
42-
};
43-
44-
if let Some(addr) = maybe_addr {
45-
if let Some(tx) = bound {
32+
let f = bound.map(|tx| {
33+
let g: Box<dyn FnOnce(SocketAddr) + Send> = Box::new(move |addr| {
4634
let _ = tx.send(addr);
47-
}
48-
println!("Bound to: {}", addr);
49-
shutdown_hook.await;
50-
}
51-
52-
println!("Stopping server.");
53-
handle.stop();
35+
});
36+
g
37+
});
38+
until_termination(handle, f).await.expect("Failed to register interrupt handler.");
5439
}
5540

5641
struct FormatMap<'a, K, V>(&'a HashMap<K, V>);

server/swimos_server_app/src/server/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ pub mod wait {
147147
/// # Arguments
148148
/// * `server` - The server to run.
149149
/// * `bound` - If specified this will be called when the server has bound to a socket, with the address.
150-
pub async fn start_and_await(
150+
pub async fn until_termination(
151151
mut handle: ServerHandle,
152152
bound: Option<Box<dyn FnOnce(SocketAddr) + Send>>,
153153
) -> Result<(), RegistrationFailed> {

swimos/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub mod io {
9494
#[cfg(feature = "server")]
9595
pub mod server {
9696
pub use swimos_server_app::{
97-
start_and_await, BoxServer, DeflateConfig, IntrospectionConfig, RemoteConnectionsConfig,
97+
until_termination, BoxServer, DeflateConfig, IntrospectionConfig, RemoteConnectionsConfig,
9898
Server, ServerBuilder, ServerHandle, WindowBits,
9999
};
100100

0 commit comments

Comments
 (0)