RustyLee: Adds organ lifecycle state tracking
Introduces a `last_reported_state` field to `OrganSocket` for better management of each organ's lifecycle status. Updates `OrganSocket` and `Heart` constructors to accept an initial `LifeState`, allowing for more flexible initialization. The `OrganFactory` is adjusted to pass this state during organ creation. This change provides a centralized mechanism for monitoring the state of all spawned organs.
This commit is contained in:
parent
175e01e361
commit
8195a3ccad
4 changed files with 13 additions and 14 deletions
|
|
@ -136,7 +136,7 @@ impl Brain {
|
|||
}
|
||||
|
||||
fn broadcast_to_organs(&self, command: OrganCommand) {
|
||||
for OrganSocket { tx, id } in &self.organ_sockets {
|
||||
for OrganSocket { tx, id,.. } in &self.organ_sockets {
|
||||
let envelope = OrganCommandEnvelope{
|
||||
command: command.clone(),
|
||||
issued_at: Time::time_stamp_millis(),
|
||||
|
|
|
|||
|
|
@ -15,14 +15,14 @@ pub struct Heart {
|
|||
}
|
||||
|
||||
impl Heart {
|
||||
pub(crate) fn new(id: u32, rx: Receiver<OrganCommandEnvelope>, tx: Sender<BrainMessage>) -> Self {
|
||||
pub(crate) fn new(id: u32, initial_life_state: LifeState, rx: Receiver<OrganCommandEnvelope>, tx: Sender<BrainMessage>) -> Self {
|
||||
Self {
|
||||
id,
|
||||
brain_command_rx: rx,
|
||||
feedback_to_brain_tx: tx,
|
||||
last_beat_time: 0,
|
||||
timestamp: 0,
|
||||
life_state: LifeState::Dead,
|
||||
life_state: initial_life_state,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use std::sync::mpsc::{self, Receiver, Sender};
|
||||
use std::thread;
|
||||
use crate::lifecycle::LifeState;
|
||||
use crate::organs::organ_socket::OrganSocket;
|
||||
use crate::organs::heart::Heart; // Assuming Heart is our first organ
|
||||
use crate::protocols::{OrganCommandEnvelope, BrainMessage};
|
||||
|
|
@ -22,14 +23,9 @@ impl OrganFactory {
|
|||
brain_tx: Sender<BrainMessage>
|
||||
) -> Vec<OrganSocket> {
|
||||
let mut sockets = Vec::new();
|
||||
let mut ids = 1..;
|
||||
|
||||
// Let's spawn a Heart as an example (ID: 1)
|
||||
// In a real factory, you might loop through a config list here
|
||||
let heart_socket = Self::spawn_heart(1, brain_tx.clone());
|
||||
sockets.push(heart_socket);
|
||||
|
||||
// Add more organs here...
|
||||
// let lung_socket = Self::spawn_lung(2, brain_tx.clone());
|
||||
sockets.push(Self::spawn_heart(ids.next().unwrap(), brain_tx.clone()));
|
||||
|
||||
tracing::info!(count = sockets.len(), "Organ collection built and threads spawned");
|
||||
|
||||
|
|
@ -37,13 +33,14 @@ impl OrganFactory {
|
|||
}
|
||||
|
||||
fn spawn_heart(id: u32, feedback_to_brain_tx: Sender<BrainMessage>) -> OrganSocket {
|
||||
let initial_life_state = LifeState::Dead;
|
||||
let (brain_command_to_organ_tx, brain_command_to_organ_rx) =
|
||||
Self::get_organ_channels();
|
||||
|
||||
let socket = OrganSocket::new(id, brain_command_to_organ_tx);
|
||||
let socket = OrganSocket::new(id, initial_life_state, brain_command_to_organ_tx);
|
||||
|
||||
thread::spawn(move || {
|
||||
let mut heart = Heart::new(id, brain_command_to_organ_rx, feedback_to_brain_tx);
|
||||
let mut heart = Heart::new(id, initial_life_state, brain_command_to_organ_rx, feedback_to_brain_tx);
|
||||
heart.start();
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,15 +1,17 @@
|
|||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use std::sync::mpsc::Sender;
|
||||
use crate::lifecycle::LifeState;
|
||||
use crate::protocols::{OrganCommand, OrganCommandEnvelope};
|
||||
|
||||
pub struct OrganSocket {
|
||||
pub id: u32,
|
||||
pub last_reported_state : LifeState,
|
||||
pub tx: Sender<OrganCommandEnvelope>,
|
||||
}
|
||||
|
||||
impl OrganSocket {
|
||||
pub fn new(id: u32, tx: Sender<OrganCommandEnvelope>) -> Self {
|
||||
Self { id, tx }
|
||||
pub fn new(id: u32, initial_state: LifeState, tx: Sender<OrganCommandEnvelope>) -> Self {
|
||||
Self { id, tx, last_reported_state: initial_state }
|
||||
}
|
||||
|
||||
pub fn send(&self, command: OrganCommand) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue