mirror of
https://github.com/Astatin3/unshell.git
synced 2026-06-09 06:47:59 -06:00
Make streams system
This commit is contained in:
@@ -7,7 +7,7 @@ use std::{
|
||||
|
||||
use crate::{
|
||||
config::{NamedComponent, PayloadConfig, RuntimeConfig},
|
||||
network::Connection,
|
||||
network::Stream,
|
||||
*,
|
||||
};
|
||||
use module::Module;
|
||||
@@ -24,7 +24,7 @@ pub struct Manager {
|
||||
components: HashMap<String, NamedComponent>,
|
||||
active_runtimes: Vec<Box<dyn ModuleRuntime>>,
|
||||
|
||||
pub connections: Vec<Connection>,
|
||||
pub connections: Vec<Box<dyn Stream<Announcement>>>,
|
||||
}
|
||||
|
||||
// static mut MANAGER_RUNTIME: Option<Arc<Mutex<Manager>>> = None;
|
||||
@@ -60,9 +60,16 @@ impl Manager {
|
||||
|
||||
let this = Arc::new(Mutex::new(this));
|
||||
|
||||
debug!("Starting runtimes...");
|
||||
debug!("Creating runtimes...");
|
||||
for runtime in &config.runtime_config {
|
||||
Self::start_runtime(this.clone(), runtime);
|
||||
Self::create_runtime(this.clone(), runtime);
|
||||
}
|
||||
|
||||
debug!("Starting runtimes...");
|
||||
for runtime in &mut this.lock().unwrap().active_runtimes {
|
||||
if let Err(e) = runtime.init(this.clone()) {
|
||||
warn!("Failed to start runtime: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
this.lock().unwrap().handle = Some(Self::start_thread(this.clone()));
|
||||
@@ -141,7 +148,7 @@ impl Manager {
|
||||
}
|
||||
|
||||
/// Start a runtime
|
||||
pub fn start_runtime<'a>(this: Arc<Mutex<Self>>, runtime: &'static RuntimeConfig) {
|
||||
fn create_runtime<'a>(this: Arc<Mutex<Self>>, runtime: &'static RuntimeConfig) {
|
||||
let mut this_lock = this.lock().unwrap();
|
||||
|
||||
let component = match this_lock.components.get(&runtime.parent_component) {
|
||||
@@ -168,6 +175,21 @@ impl Manager {
|
||||
this_lock.active_runtimes.push(runtime);
|
||||
}
|
||||
|
||||
pub fn add_runtime(
|
||||
this: Arc<Mutex<Self>>,
|
||||
runtime: &'static RuntimeConfig,
|
||||
) -> Result<(), ModuleError> {
|
||||
Self::create_runtime(this.clone(), runtime);
|
||||
|
||||
this.lock()
|
||||
.unwrap()
|
||||
.active_runtimes
|
||||
.iter_mut()
|
||||
.last()
|
||||
.unwrap()
|
||||
.init(this.clone())
|
||||
}
|
||||
|
||||
pub fn get_name(&self) -> &str {
|
||||
self.id
|
||||
}
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
use crate::{
|
||||
Announcement,
|
||||
module::Manager,
|
||||
network::{Connection, Stream},
|
||||
};
|
||||
use crate::{Announcement, ModuleError, module::Manager, network::Stream};
|
||||
|
||||
impl Manager {
|
||||
pub fn add_connection(&mut self, connection: Connection) {
|
||||
pub fn add_connection(&mut self, connection: Box<dyn Stream<Announcement>>) {
|
||||
self.connections.push(connection);
|
||||
}
|
||||
|
||||
@@ -17,8 +13,8 @@ impl Manager {
|
||||
// Collect all incoming announcements
|
||||
let announcements = self
|
||||
.connections
|
||||
.iter()
|
||||
.map(|c| c.read())
|
||||
.iter_mut()
|
||||
.map(|c| c.try_read())
|
||||
.flat_map(|array| array)
|
||||
.collect::<Vec<Announcement>>();
|
||||
|
||||
@@ -26,4 +22,11 @@ impl Manager {
|
||||
self.recv_announcement(&announcement)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn broadcast(&mut self, announcement: Announcement) -> Result<(), ModuleError> {
|
||||
for connection in &mut self.connections {
|
||||
connection.write(announcement.clone())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user