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
-123
View File
@@ -1,123 +0,0 @@
use std::{
net::TcpStream,
sync::{
Arc,
atomic::{AtomicBool, Ordering},
},
thread::{self, JoinHandle},
time::Duration,
};
use crate::{config::RuntimeConfig, network::Stream, *};
// use unshell_modules::{Manager, ModuleRuntime};
use crate::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 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 = crate::network::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(())
}
}
-69
View File
@@ -1,69 +0,0 @@
mod client_runtime;
pub const MODULE_NAME: &'static str = "client";
// pub use unshell_modules::setup_logger;
// use unshell_modules::{Manager, ModuleRuntime, module_interface};
use std::any::TypeId;
use crate::{
ModuleError,
ModuleRuntime,
client::client_runtime::ClientRuntime,
config::{InterfaceWrapper, NamedComponent, RuntimeConfig},
module_interface,
warn, // module_interface,
};
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,
}
}
-14
View File
@@ -1,14 +0,0 @@
use unshell_obfuscate::obfuscated_symbol;
use crate::config::NamedComponent;
/// Publicly facing accessor function for the payload to load inside the breakout modules.
#[obfuscated_symbol]
pub fn get_components() -> Vec<NamedComponent> {
return vec![
#[cfg(feature = "client")]
crate::client::get_named_component(),
#[cfg(feature = "server")]
crate::server::get_named_component(),
];
}
-4
View File
@@ -1,15 +1,11 @@
#![no_main]
pub mod client;
pub mod config;
pub mod logger;
pub mod module;
pub mod network;
pub mod server;
mod components;
pub use components::get_components;
mod announcement;
use std::{
fmt::{self, Debug},