From 32fcfcb38570f48fa9c568a48a5c139555a6f79f Mon Sep 17 00:00:00 2001 From: ghe0 Date: Thu, 15 Aug 2024 22:06:43 +0300 Subject: [PATCH] changed handling of async tasks --- Cargo.lock | 29 +++++++++++++++++++++++++++++ Cargo.toml | 1 + src/database.rs | 2 +- src/grpc.rs | 20 +++++++++++++++----- src/http_server.rs | 2 +- 5 files changed, 47 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 48927b4..957ae1c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/Cargo.toml b/Cargo.toml index 6d12eb9..6b638c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/database.rs b/src/database.rs index d57fa73..3610f56 100644 --- a/src/database.rs +++ b/src/database.rs @@ -73,7 +73,7 @@ pub fn sign_message_with_key(pubkey: &str, message: &str) -> Result k, diff --git a/src/grpc.rs b/src/grpc.rs index 5e94f4e..857b750 100644 --- a/src/grpc.rs +++ b/src/grpc.rs @@ -54,16 +54,26 @@ impl KeyDistribution for MyKeyDistribution { } } -pub async fn start() -> Result<(), Box> { - 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) { diff --git a/src/http_server.rs b/src/http_server.rs index 317751a..62cafe0 100644 --- a/src/http_server.rs +++ b/src/http_server.rs @@ -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));