Move modules to individual projects. Change API syntax.

This commit is contained in:
Michael Mikovsky
2025-12-17 13:17:58 -07:00
parent 41c47048be
commit 6e73cb8a8e
22 changed files with 546 additions and 164 deletions
+128
View File
@@ -0,0 +1,128 @@
use std::{
net,
sync::{
Arc, Mutex,
atomic::{AtomicBool, Ordering},
},
thread::{self, JoinHandle},
time::Duration,
};
use unshell_lib::{
config::RuntimeConfig,
module::Manager,
network::{Stream, TcpStream},
*,
};
// use unshell_modules::{Manager, ModuleRuntime};
use unshell_lib::ModuleRuntime;
pub struct ClientRuntime {
config: &'static RuntimeConfig,
thread_handle: Option<JoinHandle<()>>,
join_signal: Arc<AtomicBool>,
}
impl ClientRuntime {
pub fn new(config: &'static RuntimeConfig) -> Result<ClientRuntime, ModuleError> {
let join_signal = Arc::new(AtomicBool::new(false));
Ok(Self {
config,
thread_handle: None,
join_signal,
})
}
// pub fn send(&mut self, announcement: &Announcement) -> Result<(), ModuleError> {
// let bytes = announcement.encode();
// let mut streams = self.stream.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()?;
// }
// println!("Announcement {:?} sent", announcement);
// Ok(())
// }
}
impl ModuleRuntime for ClientRuntime {
fn is_running(&self) -> bool {
self.thread_handle.as_ref().is_none_or(|h| h.is_finished())
}
fn kill(self: Box<Self>) {
if !self.is_running() {
self.join_signal.store(true, Ordering::Relaxed);
if let Some(handle) = self.thread_handle {
let _ = handle.join();
}
}
}
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 Client Runtime".into(),
));
}
};
let retry = match self.config.config.get("retry") {
Some(retry) => Duration::from_millis(retry.parse::<u64>().unwrap()),
None => {
return Err(ModuleError::Error(
"Could not find RETRY in Client Runtime".into(),
));
}
};
// let join_clone = self.join_signal.clone();
thread::spawn(move || {
debug!("Connecting to server...");
loop {
let stream = match net::TcpStream::connect(host) {
Ok(stream) => stream,
Err(e) => {
error!("Failed to connect to server: {}", e);
thread::sleep(retry);
continue;
}
};
info!("Connected to {}", host);
thread::sleep(Duration::from_millis(100));
// Duration::from_millis(100);
let stream = TcpStream::new(stream);
let stream_clone = stream.try_clone().unwrap();
manager.lock().unwrap().add_connection(stream_clone);
// while !join_clone.load(Ordering::Relaxed) {
// }
while stream.is_alive() {
thread::sleep(Duration::from_millis(100));
}
debug!("Disconnected from 1234 {}", host);
thread::sleep(retry);
}
});
Ok(())
}
}
+67
View File
@@ -0,0 +1,67 @@
mod client_runtime;
pub const MODULE_NAME: &'static str = "client";
use std::any::TypeId;
use unshell_lib::{
ModuleError,
ModuleRuntime,
// client::client_runtime::ClientRuntime,
config::{InterfaceWrapper, NamedComponent, RuntimeConfig},
module_interface,
warn, // module_interface,
};
use crate::client_runtime::ClientRuntime;
pub extern "C" fn test1() {
warn!("Test1 called xxxxxxxxxxx");
}
pub extern "C" fn test2() {
warn!("Test2 called");
}
pub extern "C" fn test3() {
warn!("Test3 called");
}
module_interface! {
ClientInterface {
fn test1();
fn test2();
fn test3();
}
}
pub struct ClientInterfaceWrapper;
impl InterfaceWrapper for ClientInterfaceWrapper {
fn get_interface<T: 'static>(&self) -> Option<T>
where
Self: Sized,
{
if TypeId::of::<T>() == TypeId::of::<ClientInterface>() {
let my_struct = ClientInterface::from_raw(test1, test2, test3);
unsafe { Some(std::mem::transmute_copy(&my_struct)) }
} else {
None
}
}
}
fn get_interface() -> Option<&'static (dyn InterfaceWrapper + Sync)> {
Some(&ClientInterfaceWrapper)
}
fn start_runtime(config: &'static RuntimeConfig) -> Result<Box<dyn ModuleRuntime>, ModuleError> {
Ok(Box::new(ClientRuntime::new(config)?))
}
pub const fn get_named_component() -> NamedComponent {
NamedComponent {
name: MODULE_NAME,
get_interface: &get_interface,
start_runtime: &start_runtime,
}
}