use std::sync::Arc;
use asyn_rs::trace::TraceManager;
use crate::plugin::runtime::PluginRuntimeHandle;
use crate::plugin::wiring::WiringRegistry;
use super::DriverContext;
pub struct PluginManager {
driver: parking_lot::Mutex<Option<Arc<dyn DriverContext>>>,
plugin_handles: parking_lot::Mutex<Vec<PluginRuntimeHandle>>,
port_runtimes: parking_lot::Mutex<Vec<asyn_rs::runtime::port::PortRuntimeHandle>>,
trace: Arc<TraceManager>,
wiring: Arc<WiringRegistry>,
}
impl PluginManager {
pub fn new(trace: Arc<TraceManager>) -> Arc<Self> {
Arc::new(Self {
driver: parking_lot::Mutex::new(None),
plugin_handles: parking_lot::Mutex::new(Vec::new()),
port_runtimes: parking_lot::Mutex::new(Vec::new()),
trace,
wiring: Arc::new(WiringRegistry::new()),
})
}
pub fn wiring(&self) -> &Arc<WiringRegistry> {
&self.wiring
}
pub fn set_driver(&self, driver: Arc<dyn DriverContext>) {
*self.driver.lock() = Some(driver);
}
pub fn driver(&self) -> Result<Arc<dyn DriverContext>, String> {
self.driver
.lock()
.clone()
.ok_or_else(|| "driver must be configured first".into())
}
pub fn trace(&self) -> &Arc<TraceManager> {
&self.trace
}
pub fn add_plugin(&self, _dtyp: &str, handle: &PluginRuntimeHandle) {
let port_handle = handle.port_runtime().port_handle().clone();
let port_name = port_handle.port_name().to_string();
self.wiring
.register_output(&port_name, handle.array_output().clone());
self.plugin_handles.lock().push(handle.clone());
asyn_rs::asyn_record::register_port(&port_name, port_handle, self.trace.clone());
}
pub fn add_port(&self, _dtyp: &str, runtime: asyn_rs::runtime::port::PortRuntimeHandle) {
let port_handle = runtime.port_handle().clone();
let port_name = port_handle.port_name().to_string();
self.port_runtimes.lock().push(runtime);
asyn_rs::asyn_record::register_port(&port_name, port_handle, self.trace.clone());
}
pub fn report(&self) {
let handles = self.plugin_handles.lock();
println!(" Plugins: {}", handles.len());
for h in handles.iter() {
println!(" - {}", h.port_runtime().port_handle().port_name());
}
}
}