2025-11-08 10:30:21 -07:00
|
|
|
use std::{
|
2025-11-14 09:43:41 -07:00
|
|
|
io::{Read, Write},
|
2025-11-08 10:30:21 -07:00
|
|
|
net::{TcpListener, TcpStream},
|
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
|
thread::{self, JoinHandle},
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-25 14:27:06 -07:00
|
|
|
use crate::{config::RuntimeConfig, *};
|
2025-11-08 10:30:21 -07:00
|
|
|
|
|
|
|
|
pub struct ListenerRuntime {
|
|
|
|
|
thread_handle: JoinHandle<()>,
|
2025-11-25 14:27:06 -07:00
|
|
|
// join_signal: Arc<AtomicBool>,
|
2025-11-08 10:30:21 -07:00
|
|
|
// listener: TcpListener,
|
|
|
|
|
streams: Arc<Mutex<Vec<TcpStream>>>,
|
|
|
|
|
// reader: BufReader<TcpListener>,
|
|
|
|
|
// writer: BufWriter<TcpListener>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ListenerRuntime {
|
2025-11-25 14:27:06 -07:00
|
|
|
pub fn new(config: &'static RuntimeConfig) -> Result<Self, ModuleError> {
|
2025-11-13 11:52:01 -07:00
|
|
|
// info!("Starting listener runtime on {}",);
|
2025-11-25 14:27:06 -07:00
|
|
|
|
|
|
|
|
let host = match config.config.get("host") {
|
|
|
|
|
Some(host) => host,
|
|
|
|
|
None => {
|
|
|
|
|
return Err(ModuleError::Error(
|
|
|
|
|
"Could not find HOST in Server Runtime".into(),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let listener = TcpListener::bind(host).unwrap();
|
2025-11-08 10:30:21 -07:00
|
|
|
let streams = Arc::new(Mutex::new(Vec::new()));
|
|
|
|
|
|
|
|
|
|
let streams_clone = streams.clone();
|
|
|
|
|
|
|
|
|
|
let thread_handle = thread::spawn(move || {
|
|
|
|
|
let streams = streams_clone.clone();
|
|
|
|
|
for stream in listener.incoming() {
|
|
|
|
|
let stream = stream.unwrap();
|
2025-11-25 14:27:06 -07:00
|
|
|
debug!("New connection from {}", stream.peer_addr().unwrap());
|
2025-11-08 10:30:21 -07:00
|
|
|
streams.lock().unwrap().push(stream);
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-11-25 14:27:06 -07:00
|
|
|
Ok(Self {
|
2025-11-08 10:30:21 -07:00
|
|
|
thread_handle,
|
|
|
|
|
streams,
|
2025-11-25 14:27:06 -07:00
|
|
|
})
|
2025-11-08 10:30:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn send(&mut self, announcement: &Announcement) -> Result<(), std::io::Error> {
|
|
|
|
|
let bytes = announcement.encode();
|
|
|
|
|
|
|
|
|
|
let mut streams = self.streams.lock().unwrap();
|
|
|
|
|
|
|
|
|
|
for stream in streams.iter_mut() {
|
|
|
|
|
stream.write_all(&u32::to_be_bytes(bytes.len() as u32))?;
|
|
|
|
|
stream.write_all(&bytes)?;
|
|
|
|
|
stream.flush()?;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-25 14:27:06 -07:00
|
|
|
debug!("Announcement {:?} sent", announcement);
|
2025-11-08 10:30:21 -07:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2025-11-14 09:43:41 -07:00
|
|
|
|
|
|
|
|
pub fn recv(&mut self) -> Result<Announcement, ModuleError> {
|
|
|
|
|
let stream = &mut self.streams.lock().unwrap()[0];
|
|
|
|
|
|
|
|
|
|
let mut size_buf = [0u8; 4];
|
|
|
|
|
stream.read_exact(&mut size_buf).unwrap();
|
|
|
|
|
let size = u32::from_be_bytes(size_buf);
|
|
|
|
|
|
|
|
|
|
let mut buf = vec![0u8; size as usize];
|
|
|
|
|
|
|
|
|
|
stream.read_exact(&mut buf).unwrap();
|
|
|
|
|
|
|
|
|
|
if let Some(announcement) = Announcement::decode(&buf) {
|
|
|
|
|
Ok(announcement)
|
|
|
|
|
} else {
|
|
|
|
|
Err(ModuleError::Error("Failed to decode announcement".into()))
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-08 10:30:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ModuleRuntime for ListenerRuntime {
|
|
|
|
|
// fn init(&mut self) {}
|
|
|
|
|
|
|
|
|
|
fn is_running(&self) -> bool {
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn kill(self: Box<Self>) {
|
|
|
|
|
if !self.thread_handle.is_finished() {
|
2025-11-25 14:27:06 -07:00
|
|
|
// self.join_signal.store(true, Ordering::Relaxed);
|
2025-11-08 10:30:21 -07:00
|
|
|
let _ = self.thread_handle.join();
|
|
|
|
|
}
|
|
|
|
|
// drop(self);
|
|
|
|
|
}
|
|
|
|
|
}
|