(Broken) Work on new dynamic runtime config system

This commit is contained in:
Michael Mikovsky
2025-11-12 17:39:11 -07:00
parent 258599c9c7
commit 920a0f78e9
14 changed files with 319 additions and 93 deletions
+4 -8
View File
@@ -1,7 +1,5 @@
mod client_runtime;
use std::sync::{Arc, Mutex};
pub const MODULE_NAME: &'static str = "client";
// pub use unshell_modules::setup_logger;
@@ -10,9 +8,7 @@ pub const MODULE_NAME: &'static str = "client";
use crate::{
Component,
ModuleRuntime,
client::client_runtime::RuntimeTest,
module::{Interface, Manager},
module::Interface,
module_interface,
warn,
// module_interface,
@@ -60,9 +56,9 @@ impl Component for ClientComponent {
MODULE_NAME
}
fn start_runtime(&self, manager: Arc<Mutex<Manager>>) -> Option<Box<dyn ModuleRuntime>> {
Some(Box::new(RuntimeTest::new(manager)))
}
// fn start_runtime(&self, manager: Arc<Mutex<Manager>>) -> Option<Box<dyn ModuleRuntime>> {
// Some(Box::new(RuntimeTest::new(manager)))
// }
fn clone_box(&self) -> Box<dyn Component> {
Box::new(self.clone())
+22
View File
@@ -0,0 +1,22 @@
use unshell_obfuscate::obfuscated_symbol;
use crate::{Component, config::NamedComponent};
use std::collections::HashMap;
#[obfuscated_symbol]
pub fn get_components() -> Vec<NamedComponent> {
// let mut components: HashMap<&'static str, Box<dyn Component>> = HashMap::new();
return vec![
NamedComponent {name:,crate::client::MODULE_NAME, get_interface: crate::client::get_interface, start_runtime: todo!() },
];
// components
// vec![
// Feature::Client,
// #[cfg(feature = "server")]
// Feature::Server,
// ]
}
+45
View File
@@ -0,0 +1,45 @@
use std::collections::HashMap;
// use bincode::{Decode, Encode};
// use serde::{Deserialize, Serialize};
use crate::{ModuleError, ModuleRuntime};
// /// Payload config that is instantiated
// #[derive(Serialize, Deserialize)]
// pub struct Config {
// pub id: String,
// pub key: String,
// pub components: Vec<String>,
// }
pub struct PayloadConfig {
pub id: &'static str,
pub components: Vec<NamedComponent>,
pub runtime_config: Vec<RuntimeConfig>,
}
#[derive(Debug, Clone)]
pub struct RuntimeConfig {
pub parent_component: &'static str,
pub name: &'static str,
pub config: HashMap<String, String>,
}
pub struct NamedComponent {
pub name: &'static str,
// + Sync + Sync + Sync + Sync + Sync + Sync + Sync + Sync
pub get_interface: &'static (dyn Fn() -> Option<&'static (dyn InterfaceWrapper + Sync)> + Sync),
pub start_runtime: &'static (
dyn Fn(&'static RuntimeConfig) -> Result<&'static dyn ModuleRuntime, ModuleError>
+ Sync
),
}
/// Trait that wraps the get_interface<T>() function inside of components
pub trait InterfaceWrapper: Send + Sync {
fn get_interface<T>() -> Option<T>
where
Self: Sized;
}
+7 -2
View File
@@ -1,10 +1,14 @@
#![no_main]
pub mod client;
pub mod config;
pub mod logger;
pub mod module;
pub mod server;
mod components;
pub use components::get_components;
mod announcement;
use std::{
fmt,
@@ -46,7 +50,7 @@ impl fmt::Display for ModuleError {
}
/// Trait for defining modules that have a runtime.
pub trait ModuleRuntime: Send {
pub trait ModuleRuntime: Send + Sync {
/// Returns true if the module is running.
/// After returning false, the module will be dropped.
fn is_running(&self) -> bool;
@@ -56,7 +60,8 @@ pub trait ModuleRuntime: Send {
pub trait Component {
fn name(&self) -> &'static str;
fn start_runtime(&self, manager: Arc<Mutex<Manager>>) -> Option<Box<dyn ModuleRuntime>>;
// fn start_runtime(&self, manager: Arc<Mutex<Manager>>) -> Option<Box<dyn ModuleRuntime>>;
fn get_interface(&self) -> Box<dyn Interface>;
fn clone_box(&self) -> Box<dyn Component>;
}
+107 -46
View File
@@ -7,81 +7,142 @@ use std::{
use unshell_obfuscate::symbol;
use crate::*;
use crate::{
config::{NamedComponent, PayloadConfig, RuntimeConfig},
*,
};
use module::Module;
// #[derive(Debug)]
pub struct Manager {
pub struct Manager<'a> {
id: &'static str,
modules: Vec<Module>,
components: HashMap<&'static str, Box<dyn Component>>,
active_runtimes: Vec<&'a dyn ModuleRuntime>,
// runtime_config: Vec<RuntimeConfig>,
components: HashMap<String, &'a NamedComponent>,
}
// static mut MANAGER_RUNTIME: Option<Arc<Mutex<Manager>>> = None;
impl Manager {
impl<'a> Manager<'a> {
fn new(id: &'static str, config: &'a Vec<NamedComponent>, modules: Vec<Module>) -> Self {
Self {
id,
// config,
modules,
components: config.iter().map(|c| (c.name.to_string(), c)).collect(),
active_runtimes: Vec::new(),
}
}
/// Create Manager, and run initilization for each Module
#[allow(static_mut_refs)]
pub fn run<'a>(modules: Vec<Module>) {
let this: Self = Self::load_modules(modules);
let components = this.components.clone();
pub fn run(config: &'static PayloadConfig, modules: Vec<Module>) {
// Construct self
let this = Self::new(&config.id, &config.components, modules);
// Load each of the pre-prepared modules
// this.load_components();
let this = Arc::new(Mutex::new(this));
let mut runtimes: Vec<Box<dyn ModuleRuntime>> = Vec::new();
Self::start_runtimes(this.clone(), &config.runtime_config);
for (_name, component) in components {
let module_runtime = component.start_runtime(this.clone());
if let Some(module_runtime) = module_runtime {
runtimes.push(module_runtime);
}
}
// let components = this.components.clone();
Self::join(&mut runtimes);
// let mut runtimes: Vec<Box<dyn ModuleRuntime>> = Vec::new();
// for (_name, component) in components {
// let module_runtime = component.start_runtime(this.clone());
// if let Some(module_runtime) = module_runtime {
// runtimes.push(module_runtime);
// }
// }
Self::join(this);
}
pub fn load_modules<'a>(modules: Vec<Module>) -> Self {
let module_count = modules.len();
let mut this = Self {
modules,
components: HashMap::new(),
};
// fn load_components(&mut self) {
// for i in 0..self.modules.len() {
// debug!("Importing module {}", i);
// // let this_lock = .unwrap();
// let component_func = if let Ok(component_func) = self.modules[i]
// .get_symbol::<fn() -> HashMap<&'static str, Box<dyn Component>>>(
// symbol!("get_components").as_bytes(),
// ) {
// component_func
// } else {
// warn!("get_components function not found");
// continue;
// };
for i in 0..module_count {
debug!("Importing module {}", i);
// let this_lock = .unwrap();
let component_func = if let Ok(component_func) = this.modules[i]
.get_symbol::<fn() -> HashMap<&'static str, Box<dyn Component>>>(
symbol!("get_components").as_bytes(),
) {
component_func
} else {
warn!("get_components function not found");
continue;
// let components = component_func();
// let len = components.len();
// debug!("[{}] Loaded {} components", i, len);
// self.components.extend(components);
// }
// }
/// Start each runtime
fn start_runtimes(this: Arc<Mutex<Self>>, runtimes: &'static Vec<RuntimeConfig>) {
for runtime in runtimes {
let mut this_lock = this.lock().unwrap();
let component = match this_lock.components.get(runtime.parent_component) {
Some(component) => component,
None => {
warn!(
"Could not find component {} which is referenced by runtime {}",
runtime.parent_component, runtime.name
);
continue;
}
};
let components = component_func();
let runtime = match (*component.start_runtime)(runtime) {
Ok(runtime) => runtime,
Err(e) => {
warn!("Failed to start runtime: {:?}", e);
continue;
}
};
let len = components.len();
debug!("[{}] Loaded {} components", i, len);
this.components.extend(components);
this_lock.active_runtimes.push(runtime);
}
this
}
/// Iterateratively loop through all runtimes, until all are finished executing
pub fn join(runtimes: &mut Vec<Box<dyn ModuleRuntime>>) {
// let mut len = runtimes.len().clone();
while runtimes.len() > 0 {
runtimes.retain(|runtime| runtime.is_running());
fn join(this: Arc<Mutex<Self>>) {
loop {
let mut this_lock = this.lock().unwrap();
thread::sleep(Duration::from_micros(100));
if this_lock.active_runtimes.len() <= 0 {
break;
}
this_lock
.active_runtimes
.retain(|runtime| runtime.is_running());
drop(this_lock);
thread::sleep(Duration::from_millis(500));
}
}
pub fn get_component(&self) -> HashMap<&'static str, Box<dyn Component>> {
self.components.clone()
// pub fn get_component(&self) -> HashMap<&'static str, Box<dyn Component>> {
// self.components.clone()
// }
pub fn get_name(&self) -> &str {
self.id
}
// pub extern "C" fn test1234(&self, float: f32) {