changed handling of async tasks

This commit is contained in:
ghe0 2024-08-15 22:06:43 +03:00
parent 719a0b5455
commit 32fcfcb385
5 changed files with 47 additions and 7 deletions

29
Cargo.lock generated

@ -518,6 +518,21 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "futures"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.30"
@ -525,6 +540,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
@ -533,6 +549,17 @@ version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
[[package]]
name = "futures-executor"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.30"
@ -568,6 +595,7 @@ version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
@ -642,6 +670,7 @@ name = "hacker-challenge"
version = "0.1.0"
dependencies = [
"ed25519-dalek",
"futures",
"hex",
"once_cell",
"prost",

@ -5,6 +5,7 @@ edition = "2021"
[dependencies]
ed25519-dalek = { version = "2.1.1", features = ["rand_core", "serde"] }
futures = "0.3.30"
hex = "0.4.3"
once_cell = "1.19.0"
prost = "0.13.1"

@ -54,16 +54,26 @@ impl KeyDistribution for MyKeyDistribution {
}
}
pub async fn start() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:31373".parse().unwrap();
let key_distribution = MyKeyDistribution::default();
async fn start_client() {
loop {
tokio::time::sleep(Duration::from_secs(10)).await;
}
}
async fn start_server() {
let addr = "0.0.0.0:31373".parse().unwrap();
let key_distribution = MyKeyDistribution::default();
Server::builder()
.add_service(KeyDistributionServer::new(key_distribution))
.serve(addr)
.await?;
.await
.unwrap();
}
Ok(())
pub async fn start() {
let start_client = tokio::task::spawn(start_server());
let start_server = tokio::task::spawn(start_server());
futures::future::select(start_client, start_server).await;
}
pub fn add_node(ip: String) {

@ -25,7 +25,7 @@ async fn sign(req: &mut Request) -> String {
}
pub async fn start() {
let acceptor = TcpListener::new("0.0.0.0:5800").bind().await;
let acceptor = TcpListener::new("0.0.0.0:31372").bind().await;
let router = Router::new()
.get(homepage)
.push(Router::with_path("sign").get(sign));