Files
unshell/unshell-lib/src/client/client_runtime.rs
T

96 lines
2.8 KiB
Rust
Raw Normal View History

use std::{
io::Read,
net::TcpStream,
sync::{
2025-11-13 11:52:01 -07:00
Arc,
atomic::{AtomicBool, Ordering},
},
thread::{self, JoinHandle},
};
2025-11-13 11:52:01 -07:00
use crate::{config::RuntimeConfig, *};
// use unshell_modules::{Manager, ModuleRuntime};
2025-11-13 11:52:01 -07:00
use crate::{Announcement, ModuleRuntime};
2025-11-13 11:52:01 -07:00
pub struct ClientRuntime {
thread_handle: JoinHandle<()>,
join_signal: Arc<AtomicBool>,
}
2025-11-13 11:52:01 -07:00
impl ClientRuntime {
pub fn new(config: &'static RuntimeConfig) -> Result<ClientRuntime, ModuleError> {
let join_signal = Arc::new(AtomicBool::new(false));
let join_clone = join_signal.clone();
2025-11-13 11:52:01 -07:00
let host = match config.config.get("host") {
Some(host) => host,
None => {
return Err(ModuleError::Error(
"Could not find HOST in Client Runtime".into(),
));
}
};
Ok(Self {
thread_handle: thread::spawn(move || {
2025-11-11 11:00:28 -07:00
debug!("Connecting to server...");
2025-11-13 11:52:01 -07:00
let mut stream = match TcpStream::connect(host) {
2025-11-11 11:00:28 -07:00
Ok(stream) => stream,
Err(e) => {
error!("Failed to connect to server: {}", e);
return;
}
};
info!("Connected");
// let reader = BufReader::new(stream.try_clone().unwrap());
// let mut writer = BufWriter::new(stream.try_clone().unwrap());
// let (a, b) = crossbeam_channel::unbounded();
// a.
// if join_receiver.len() == 0 {
// join_receiver.recv().unwrap();
// }
while !join_clone.load(Ordering::Relaxed) {
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();
let a = Announcement::decode(&buf).unwrap();
match a {
Announcement::TestAnnouncement(s) => {
2025-11-09 12:34:52 -07:00
println!("Received test announcement: {}", s)
}
}
}
}),
join_signal,
2025-11-13 11:52:01 -07:00
})
}
}
2025-11-13 11:52:01 -07:00
impl ModuleRuntime for ClientRuntime {
// fn init(&mut self) {}
fn is_running(&self) -> bool {
// println!("Checking if running");
!self.thread_handle.is_finished()
}
fn kill(self: Box<Self>) {
if !self.thread_handle.is_finished() {
self.join_signal.store(true, Ordering::Relaxed);
let _ = self.thread_handle.join();
}
// drop(self);
}
}