2025-11-08 14:56:03 -07:00
|
|
|
use std::{
|
|
|
|
|
collections::HashMap,
|
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
|
thread,
|
|
|
|
|
time::Duration,
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-12 17:39:11 -07:00
|
|
|
use crate::{
|
|
|
|
|
config::{NamedComponent, PayloadConfig, RuntimeConfig},
|
|
|
|
|
*,
|
|
|
|
|
};
|
2025-11-09 12:34:52 -07:00
|
|
|
use module::Module;
|
2025-11-13 11:52:01 -07:00
|
|
|
use unshell_obfuscate::symbol;
|
2025-11-08 14:56:03 -07:00
|
|
|
|
2025-11-08 17:58:40 -07:00
|
|
|
// #[derive(Debug)]
|
2025-11-13 11:52:01 -07:00
|
|
|
pub struct Manager {
|
2025-11-12 17:39:11 -07:00
|
|
|
id: &'static str,
|
|
|
|
|
|
2025-11-13 11:52:01 -07:00
|
|
|
pub modules: Vec<Module>,
|
2025-11-12 17:39:11 -07:00
|
|
|
|
2025-11-13 11:52:01 -07:00
|
|
|
active_runtimes: Vec<Box<dyn ModuleRuntime>>,
|
|
|
|
|
components: HashMap<String, NamedComponent>,
|
2025-11-08 14:56:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// static mut MANAGER_RUNTIME: Option<Arc<Mutex<Manager>>> = None;
|
|
|
|
|
|
2025-11-13 11:52:01 -07:00
|
|
|
impl Manager {
|
2025-11-14 09:43:41 -07:00
|
|
|
fn new(id: &'static str, components: Vec<NamedComponent>, modules: Vec<Module>) -> Self {
|
2025-11-12 17:39:11 -07:00
|
|
|
Self {
|
|
|
|
|
id,
|
|
|
|
|
modules,
|
2025-11-14 09:43:41 -07:00
|
|
|
components: components
|
2025-11-13 11:52:01 -07:00
|
|
|
.into_iter()
|
|
|
|
|
.map(|c| (c.name.to_string(), c))
|
|
|
|
|
.collect(),
|
2025-11-12 17:39:11 -07:00
|
|
|
active_runtimes: Vec::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-08 14:56:03 -07:00
|
|
|
/// Create Manager, and run initilization for each Module
|
|
|
|
|
#[allow(static_mut_refs)]
|
2025-11-12 17:39:11 -07:00
|
|
|
pub fn run(config: &'static PayloadConfig, modules: Vec<Module>) {
|
|
|
|
|
// Construct self
|
2025-11-13 11:52:01 -07:00
|
|
|
let mut this = Self::new(&config.id, config.components.clone(), modules);
|
2025-11-12 17:39:11 -07:00
|
|
|
|
2025-11-14 09:43:41 -07:00
|
|
|
debug!("Imported {} base components", this.components.len());
|
|
|
|
|
debug!("Imported {} base runtimes", &config.runtime_config.len());
|
|
|
|
|
|
2025-11-12 17:39:11 -07:00
|
|
|
// Load each of the pre-prepared modules
|
2025-11-13 11:52:01 -07:00
|
|
|
this.load_components();
|
2025-11-08 14:56:03 -07:00
|
|
|
|
|
|
|
|
let this = Arc::new(Mutex::new(this));
|
|
|
|
|
|
2025-11-12 17:39:11 -07:00
|
|
|
Self::start_runtimes(this.clone(), &config.runtime_config);
|
2025-11-08 14:56:03 -07:00
|
|
|
|
2025-11-13 11:52:01 -07:00
|
|
|
// drop(config);
|
2025-11-12 17:39:11 -07:00
|
|
|
|
|
|
|
|
Self::join(this);
|
2025-11-08 14:56:03 -07:00
|
|
|
}
|
|
|
|
|
|
2025-11-13 11:52:01 -07:00
|
|
|
fn load_components(&mut self) {
|
|
|
|
|
for module in &self.modules {
|
|
|
|
|
// Load get_components function from shared object library
|
|
|
|
|
let component_func = match module
|
2025-11-14 09:43:41 -07:00
|
|
|
.get_symbol::<fn() -> Vec<NamedComponent>>(symbol!("get_components").as_bytes())
|
2025-11-13 11:52:01 -07:00
|
|
|
{
|
|
|
|
|
Ok(func) => func,
|
|
|
|
|
Err(_) => {
|
|
|
|
|
warn!("get_components function not found");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let components = component_func();
|
2025-11-14 09:43:41 -07:00
|
|
|
let component_name = "TODO"; //TODO: Make this actually load component name
|
2025-11-13 11:52:01 -07:00
|
|
|
|
|
|
|
|
debug!("{} - Retrieved payload metadata", component_name);
|
|
|
|
|
|
|
|
|
|
// Add each component into self
|
|
|
|
|
for c in components {
|
|
|
|
|
debug!("{} - Found component '{}'", "TODO", c.name);
|
|
|
|
|
self.components.insert(c.name.to_owned(), c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-08 14:56:03 -07:00
|
|
|
|
2025-11-12 17:39:11 -07:00
|
|
|
/// Start each runtime
|
|
|
|
|
fn start_runtimes(this: Arc<Mutex<Self>>, runtimes: &'static Vec<RuntimeConfig>) {
|
2025-11-13 11:52:01 -07:00
|
|
|
debug!("Starting runtimes...");
|
2025-11-12 17:39:11 -07:00
|
|
|
for runtime in runtimes {
|
|
|
|
|
let mut this_lock = this.lock().unwrap();
|
|
|
|
|
|
2025-11-14 09:43:41 -07:00
|
|
|
let component = match this_lock.components.get(&runtime.parent_component) {
|
2025-11-12 17:39:11 -07:00
|
|
|
Some(component) => component,
|
|
|
|
|
None => {
|
|
|
|
|
warn!(
|
2025-11-14 09:43:41 -07:00
|
|
|
"Could not find component '{}' which is referenced by runtime: {}",
|
2025-11-12 17:39:11 -07:00
|
|
|
runtime.parent_component, runtime.name
|
|
|
|
|
);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-11-08 14:56:03 -07:00
|
|
|
|
2025-11-13 11:52:01 -07:00
|
|
|
debug!("Starting runtime: {}", runtime.name);
|
|
|
|
|
|
2025-11-12 17:39:11 -07:00
|
|
|
let runtime = match (*component.start_runtime)(runtime) {
|
|
|
|
|
Ok(runtime) => runtime,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
warn!("Failed to start runtime: {:?}", e);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-11-08 14:56:03 -07:00
|
|
|
|
2025-11-12 17:39:11 -07:00
|
|
|
this_lock.active_runtimes.push(runtime);
|
2025-11-08 14:56:03 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Iterateratively loop through all runtimes, until all are finished executing
|
2025-11-12 17:39:11 -07:00
|
|
|
fn join(this: Arc<Mutex<Self>>) {
|
|
|
|
|
loop {
|
|
|
|
|
let mut this_lock = this.lock().unwrap();
|
2025-11-08 14:56:03 -07:00
|
|
|
|
2025-11-12 17:39:11 -07:00
|
|
|
if this_lock.active_runtimes.len() <= 0 {
|
2025-11-14 09:43:41 -07:00
|
|
|
debug!("There are no more runtimes! Exiting...");
|
2025-11-12 17:39:11 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-14 09:43:41 -07:00
|
|
|
this_lock.active_runtimes.retain(|runtime| {
|
|
|
|
|
if runtime.is_running() {
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
debug!("Runtime exited!"); //TODO: Make this better
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-11-12 17:39:11 -07:00
|
|
|
|
|
|
|
|
drop(this_lock);
|
|
|
|
|
|
|
|
|
|
thread::sleep(Duration::from_millis(500));
|
2025-11-08 14:56:03 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-12 17:39:11 -07:00
|
|
|
// pub fn get_component(&self) -> HashMap<&'static str, Box<dyn Component>> {
|
|
|
|
|
// self.components.clone()
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
pub fn get_name(&self) -> &str {
|
|
|
|
|
self.id
|
2025-11-08 17:58:40 -07:00
|
|
|
}
|
|
|
|
|
|
2025-11-08 14:56:03 -07:00
|
|
|
// pub extern "C" fn test1234(&self, float: f32) {
|
|
|
|
|
// info!("Manager Test Sucsessfull! {}", float.powf(2.));
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// #[allow(static_mut_refs, improper_ctypes_definitions)]
|
|
|
|
|
// pub extern "C" fn get_manager() -> Arc<Mutex<Manager>> {
|
|
|
|
|
// unsafe { MANAGER_RUNTIME.clone().unwrap() }
|
|
|
|
|
// }
|
|
|
|
|
}
|