removed redundant notify and instead awaiting the task futures

This commit is contained in:
Jędrzej Stuczyński
2023-08-17 12:21:15 +01:00
parent 15ea11d5fd
commit a9df5da39c
@@ -4,8 +4,6 @@
use futures::StreamExt;
use nym_sdk::mixnet;
use nym_sdk::mixnet::MixnetMessageSender;
use std::sync::Arc;
use tokio::sync::Notify;
#[tokio::main]
async fn main() {
@@ -18,36 +16,27 @@ async fn main() {
let our_address = *client.nym_address();
println!("Our client nym address is: {our_address}");
let receive_notify = Arc::new(Notify::new());
let receive_notify2 = receive_notify.clone();
let send_notify = Arc::new(Notify::new());
let send_notify2 = send_notify.clone();
let sender = client.split_sender();
// receiving task
tokio::spawn(async move {
let receiving_task_handle = tokio::spawn(async move {
if let Some(received) = client.next().await {
println!("Received: {}", String::from_utf8_lossy(&received.message));
}
client.disconnect().await;
// notify that we're done and leave the task!
receive_notify.notify_waiters();
});
// sending task
tokio::spawn(async move {
let sending_task_handle = tokio::spawn(async move {
sender
.send_plain_message(our_address, "hello from a different task!")
.await
.unwrap();
send_notify.notify_waiters();
});
// wait for both tasks to be done
println!("waiting for shutdown");
send_notify2.notified().await;
receive_notify2.notified().await;
sending_task_handle.await.unwrap();
receiving_task_handle.await.unwrap();
}