Move server component out of unshell-lib

This commit is contained in:
Michael Mikovsky
2025-12-20 12:25:35 -07:00
parent 0dcd841cc5
commit 514dd5c87b
11 changed files with 934 additions and 73 deletions
+1 -1
View File
@@ -21,7 +21,7 @@ pub struct PayloadConfig {
pub runtime_config: Vec<RuntimeConfig>,
}
#[derive(Debug, Clone, Encode, Decode, serde::Deserialize, serde::Serialize)]
#[derive(Debug, Clone, Encode, Decode)]
pub struct RuntimeConfig {
pub parent_component: String,
pub name: String,
-1
View File
@@ -4,7 +4,6 @@ pub mod config;
pub mod logger;
pub mod module;
pub mod network;
pub mod server;
mod announcement;
use std::{
-65
View File
@@ -1,65 +0,0 @@
// use std::sync::{
// Arc,
// atomic::{AtomicBool, Ordering},
// };
// use crate::{Announcement, ModuleError, network::Stream};
// use crossbeam_channel::{Receiver, Sender};
// pub struct Connection {
// tx: Sender<Announcement>,
// rx: Receiver<Announcement>,
// is_alive: Arc<AtomicBool>,
// }
// impl Connection {
// pub fn new() -> (Connection, Connection) {
// let (tx_mgr, rx) = crossbeam_channel::unbounded();
// let (tx, rx_mgr) = crossbeam_channel::unbounded();
// let alive = Arc::new(AtomicBool::new(false));
// (
// Self {
// tx: tx_mgr,
// rx: rx_mgr,
// is_alive: alive.clone(),
// },
// Self {
// tx,
// rx,
// is_alive: alive,
// },
// )
// }
// }
// impl Stream<Announcement> for Connection {
// fn is_alive(&self) -> bool {
// self.is_alive.load(Ordering::Relaxed)
// }
// fn len(&self) -> usize {
// self.rx.len()
// }
// fn read(&self) -> Vec<Announcement> {
// self.rx.try_iter().collect()
// }
// fn write(&mut self, data: Announcement) -> Result<(), crate::ModuleError> {
// self.tx
// .send(data)
// .map_err(|_| ModuleError::Error("Failed to send".into()))?;
// Ok(())
// }
// fn try_clone(&self) -> Result<Box<dyn Stream<Announcement> + Send + Sync>, crate::ModuleError> {
// Ok(Box::new(Self {
// tx: self.tx.clone(),
// rx: self.rx.clone(),
// is_alive: self.is_alive.clone(),
// }))
// }
// }
-26
View File
@@ -1,26 +0,0 @@
mod server_runtime;
pub use server_runtime::ListenerRuntime;
use crate::{
ModuleError, ModuleRuntime,
config::{InterfaceWrapper, NamedComponent, RuntimeConfig},
};
pub const COMPONENT_NAME: &'static str = "server";
fn get_interface() -> Option<&'static (dyn InterfaceWrapper + Sync)> {
None
}
fn start_runtime(config: &'static RuntimeConfig) -> Result<Box<dyn ModuleRuntime>, ModuleError> {
Ok(Box::new(ListenerRuntime::new(config)?))
}
pub const fn get_named_component() -> NamedComponent {
NamedComponent {
name: COMPONENT_NAME,
get_interface: &get_interface,
start_runtime: &start_runtime,
}
}
-108
View File
@@ -1,108 +0,0 @@
use std::{
net::TcpListener,
sync::{Arc, Mutex},
thread::{self, JoinHandle},
};
use crate::{config::RuntimeConfig, module::Manager, *};
pub struct ListenerRuntime {
config: &'static RuntimeConfig,
thread_handle: Option<JoinHandle<()>>,
// streams: Arc<Mutex<Vec<TcpStream>>>,
// manager: Option<Arc<Mutex<Manager>>>,
}
impl ListenerRuntime {
pub fn new(config: &'static RuntimeConfig) -> Result<Self, ModuleError> {
Ok(Self {
config,
thread_handle: None,
// streams: Arc::new(Mutex::new(Vec::new())),
// manager: None,
})
}
// 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()?;
// }
// debug!("Announcement {:?} sent", announcement);
// Ok(())
// }
// 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()))
// }
// }
}
impl ModuleRuntime for ListenerRuntime {
fn init(&mut self, manager: Arc<Mutex<Manager>>) -> Result<(), ModuleError> {
let host = match self.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();
// 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();
debug!("New connection from {}", stream.peer_addr().unwrap());
let stream = crate::network::TcpStream::new(stream);
manager.lock().unwrap().add_connection(Box::new(stream));
// streams.lock().unwrap().push(stream);
}
});
self.thread_handle = Some(thread_handle);
Ok(())
}
fn is_running(&self) -> bool {
true
}
fn kill(self: Box<Self>) {
// if let Some(thread)
// if !self.thread_handle.is_finished() {
// // self.join_signal.store(true, Ordering::Relaxed);
// let _ = self.thread_handle.join();
// }
// // drop(self);
}
}