Compare commits
2 commits
793c8eda8d
...
a353c48d21
| Author | SHA1 | Date | |
|---|---|---|---|
| a353c48d21 | |||
| 6ea909347b |
6 changed files with 49 additions and 29 deletions
|
|
@ -1 +1 @@
|
||||||
150
|
151
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
use tracing::{debug, info};
|
use tracing::{debug, info};
|
||||||
use std::sync::mpsc::{Receiver, Sender};
|
use std::sync::mpsc::{Receiver, Sender};
|
||||||
use std::thread::JoinHandle;
|
|
||||||
use crate::lifecycle::{LifeState, LifecycleCommand, LifecycleCommandResponse, LifecycleReceipt};
|
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_factory::OrganFactory;
|
||||||
use crate::organs::organ_socket::OrganSocket;
|
use crate::organs::organ_socket::OrganSocket;
|
||||||
use crate::protocols::{BrainMessage, OrganCommand, OrganCommandEnvelope};
|
use crate::protocols::{BrainMessage, OrganCommand, OrganCommandEnvelope};
|
||||||
|
|
@ -50,12 +49,16 @@ impl Brain {
|
||||||
self.request_heart_beat();
|
self.request_heart_beat();
|
||||||
}
|
}
|
||||||
|
|
||||||
Self::rest()
|
while let Ok(message) = self.organ_rx.try_recv() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
self.rest();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rest() {
|
fn rest(&self) {
|
||||||
debug!("Brain is resting.");
|
debug!("Brain is {} and resting.", self.state);
|
||||||
std::thread::sleep(std::time::Duration::from_millis(200))
|
std::thread::sleep(std::time::Duration::from_millis(200))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -86,12 +89,11 @@ impl Brain {
|
||||||
|
|
||||||
fn can_transition_lifecycle(&self, command: &LifecycleCommand) -> bool
|
fn can_transition_lifecycle(&self, command: &LifecycleCommand) -> bool
|
||||||
{
|
{
|
||||||
|
|
||||||
if command.required_state == Buried || command.required_state == Dying {
|
if command.required_state == Buried || command.required_state == Dying {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.state == LifeState::Dead && command.required_state == LifeState::Genisys {
|
if self.state == Dead && command.required_state == Genisys {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,40 @@
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum LifeState {
|
pub enum LifeState {
|
||||||
Dead,
|
Dead,
|
||||||
Genisys,
|
Genisys,
|
||||||
Sleeping,
|
// Sleeping,
|
||||||
Wakening,
|
// Wakening,
|
||||||
Awake,
|
// Awake,
|
||||||
DeepThought,
|
// DeepThought,
|
||||||
ActionFocused,
|
// ActionFocused,
|
||||||
Flight,
|
// Flight,
|
||||||
Panic,
|
// Panic,
|
||||||
Dying,
|
Dying,
|
||||||
Buried,
|
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)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum LifecycleCommandResponse {
|
pub enum LifecycleCommandResponse {
|
||||||
Ok,
|
Ok,
|
||||||
|
|
|
||||||
|
|
@ -59,14 +59,12 @@ fn wait_for_death(tx: Sender<LifecycleCommand>, rx: Receiver<LifecycleReceipt>)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn let_there_be_life(tx: &Sender<LifecycleCommand>, rx: &Receiver<LifecycleReceipt>) {
|
fn let_there_be_life(tx: &Sender<LifecycleCommand>, rx: &Receiver<LifecycleReceipt>) {
|
||||||
// 3. Command: Genisys
|
|
||||||
info!("God: Commanding Genisys...");
|
info!("God: Commanding Genisys...");
|
||||||
let _ = tx.send(LifecycleCommand {
|
let _ = tx.send(LifecycleCommand {
|
||||||
required_state: LifeState::Genisys,
|
required_state: LifeState::Genisys,
|
||||||
command_time: Instant::now(),
|
command_time: Instant::now(),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Wait for Brain's "OK"
|
|
||||||
if let Ok(receipt) = rx.recv() {
|
if let Ok(receipt) = rx.recv() {
|
||||||
info!("Brain: {:?}", receipt.response);
|
info!("Brain: {:?}", receipt.response);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ impl Heart {
|
||||||
|
|
||||||
match envelope.command {
|
match envelope.command {
|
||||||
OrganCommand::Beat(_) => {
|
OrganCommand::Beat(_) => {
|
||||||
if (self.ready_to_beat()) {
|
if self.ready_to_beat() {
|
||||||
self.beat();
|
self.beat();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,18 +2,18 @@ use crate::coordinates::Point3D;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum OrganCommand {
|
pub enum OrganCommand {
|
||||||
Sleep,
|
// Sleep,
|
||||||
Waken,
|
// Waken,
|
||||||
Pause,
|
// Pause,
|
||||||
Resume,
|
// Resume,
|
||||||
Beat (u32),
|
Beat (u32),
|
||||||
ChangeSpeed (u32),
|
// ChangeSpeed (u32),
|
||||||
GoFaster(u32),
|
// GoFaster(u32),
|
||||||
GoSlower(u32),
|
// GoSlower(u32),
|
||||||
MoveTo {
|
// MoveTo {
|
||||||
relative_to_center: Point3D,
|
// relative_to_center: Point3D,
|
||||||
speed: u32
|
// speed: u32
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue