change logic how to work with sockets in server.rs
use std::sync::{Arc, atomic::{AtomicBool, Ordering}};
use tokio::net::TcpListener;
use tokio::sync::Notify;
use tokio::task;
use tokio::time::{sleep, Duration};
#[tokio::main]
async fn main() -> tokio::io::Result<()> {
let addr = "127.0.0.1:8080";
let listener = TcpListener::bind(&addr).await?;
let is_running = Arc::new(AtomicBool::new(true));
let is_running_clone = is_running.clone();
let notify = Arc::new(Notify::new());
let notify_clone = notify.clone();
// Launch an asynchronous task to listen for connections
let listener_task = task::spawn(async move {
while is_running_clone.load(Ordering::SeqCst) {
tokio::select! {
// Waiting for incoming connections
Ok((stream, _)) = listener.accept() => {
println!("Connection accepted: {:?}", stream);
// Here you can handle the connection
},
// Waiting for the termination signal
_ = notify_clone.notified() => {
println!("Termination signal received");
break;
},
}
}
println!("TcpListener stopped.");
});
// Simulate some working time
sleep(Duration::from_secs(10)).await;
// Set the termination flag and notify the task
is_running.store(false, Ordering::SeqCst);
notify.notify_one();
// Wait for the task to complete
listener_task.await?;
println!("Program completed.");
Ok(())
}
send signals for stop listerning for removed ports in Cbltfile (process_workers function need to modify)