Separate server bootstrap sequence between listen() and run()

Initially it uses a channel that given through the `run()` parameter to notify when a server becomes ready,
but that doesn't work because it never run the procedure until `await` called.
This means if it calls `await`, it blocks the procedure so it cannot consume a channel simultaneously.
Thus, it separates bootstrap sequence between `listen()` and `run()`.
`listen()`: Start UDP listening. After this function call is finished, the RADIUS server is ready.
`run()`: Start a loop to handle the RADIUS requests.
This commit is contained in:
moznion
2021-01-03 11:19:28 +09:00
parent 5c74cf92c7
commit 88e01fc828
3 changed files with 120 additions and 57 deletions
+17 -5
View File
@@ -1,8 +1,8 @@
#[macro_use]
extern crate log;
use std::io;
use std::net::SocketAddr;
use std::{io, process};
use async_trait::async_trait;
use tokio::net::UdpSocket;
@@ -17,18 +17,30 @@ use radius::server::{RequestHandler, SecretProvider, SecretProviderError, Server
async fn main() {
env_logger::init();
let server_future = Server::run(
// start UDP listening
let mut server = Server::listen(
"0.0.0.0",
1812,
1500,
true,
false,
MyRequestHandler {},
MySecretProvider {},
signal::ctrl_c(),
)
.await
.unwrap();
// once it has reached here, a RADIUS server is now ready
info!(
"serve is now ready: {}",
server.get_listen_address().unwrap()
);
let result = server_future.await;
// start the loop to handle the RADIUS requests
let result = server.run(signal::ctrl_c()).await;
info!("{:?}", result);
if result.is_err() {
process::exit(1);
}
}
struct MyRequestHandler {}