From 6ea909347bf5b564d1010880c554bfaa6f2e4009 Mon Sep 17 00:00:00 2001 From: Russell Gilbert Date: Sat, 14 Mar 2026 06:15:23 +0000 Subject: [PATCH] RustyLee: Enhances Brain's Dead state behaviour and logging The Brain now actively rests when in the `Dead` lifecycle state, ensuring consistent idle behavior and continuous logging of its status. Adds the `Display` trait implementation for `LifeState`, providing human-readable representations of lifecycle states for improved debugging output. --- src/rustylee/build_number.txt | 2 +- src/rustylee/src/brain.rs | 15 ++++++++++----- src/rustylee/src/lifecycle.rs | 20 ++++++++++++++++++++ src/rustylee/src/main.rs | 2 -- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/rustylee/build_number.txt b/src/rustylee/build_number.txt index 4701cc7..c663e4d 100644 --- a/src/rustylee/build_number.txt +++ b/src/rustylee/build_number.txt @@ -1 +1 @@ -150 \ No newline at end of file +151 \ No newline at end of file diff --git a/src/rustylee/src/brain.rs b/src/rustylee/src/brain.rs index 89408f4..0234280 100644 --- a/src/rustylee/src/brain.rs +++ b/src/rustylee/src/brain.rs @@ -2,7 +2,7 @@ use tracing::{debug, info}; use std::sync::mpsc::{Receiver, Sender}; use std::thread::JoinHandle; use crate::lifecycle::{LifeState, LifecycleCommand, LifecycleCommandResponse, LifecycleReceipt}; -use crate::lifecycle::LifeState::{Dying, Buried, Genisys}; +use crate::lifecycle::LifeState::{Dying, Buried, Genisys, Dead}; use crate::organs::organ_factory::OrganFactory; use crate::organs::organ_socket::OrganSocket; use crate::protocols::{BrainMessage, OrganCommand, OrganCommandEnvelope}; @@ -46,16 +46,21 @@ impl Brain { break; } + if self.state == Dead { + self.rest(); + continue; + } + if self.is_ready() { self.request_heart_beat(); } - Self::rest() + self.rest() } } - fn rest() { - debug!("Brain is resting."); + fn rest(&self) { + debug!("Brain is {} and resting.", self.state); std::thread::sleep(std::time::Duration::from_millis(200)) } @@ -91,7 +96,7 @@ impl Brain { return true } - if self.state == LifeState::Dead && command.required_state == LifeState::Genisys { + if self.state == Dead && command.required_state == Genisys { return true } diff --git a/src/rustylee/src/lifecycle.rs b/src/rustylee/src/lifecycle.rs index 626ca11..87a9e05 100644 --- a/src/rustylee/src/lifecycle.rs +++ b/src/rustylee/src/lifecycle.rs @@ -1,4 +1,5 @@ use std::time::Instant; +use std::fmt; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum LifeState { @@ -15,6 +16,25 @@ pub enum LifeState { Buried, } +impl fmt::Display for LifeState { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let state_str = match self { + LifeState::Dead => "Dead", + LifeState::Genisys => "In Genesis", + LifeState::Sleeping => "Sleeping", + LifeState::Wakening => "Wakening", + LifeState::Awake => "Fully Awake", + LifeState::DeepThought => "In Deep Thought", + LifeState::ActionFocused => "Action Focused", + LifeState::Flight => "In Flight", + LifeState::Panic => "PANIC!", + LifeState::Dying => "Dying", + LifeState::Buried => "Buried", + }; + write!(f, "{}", state_str) + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum LifecycleCommandResponse { Ok, diff --git a/src/rustylee/src/main.rs b/src/rustylee/src/main.rs index 93471a8..09e5789 100644 --- a/src/rustylee/src/main.rs +++ b/src/rustylee/src/main.rs @@ -59,14 +59,12 @@ fn wait_for_death(tx: Sender, rx: Receiver) } fn let_there_be_life(tx: &Sender, rx: &Receiver) { - // 3. Command: Genisys info!("God: Commanding Genisys..."); let _ = tx.send(LifecycleCommand { required_state: LifeState::Genisys, command_time: Instant::now(), }); - // Wait for Brain's "OK" if let Ok(receipt) = rx.recv() { info!("Brain: {:?}", receipt.response); }