This commit is contained in:
Joe Ardent 2024-06-03 18:14:15 -07:00
parent f1864ad7b0
commit 30d7a487d6
1 changed files with 6 additions and 14 deletions

View File

@ -2,7 +2,7 @@ use std::{
collections::HashMap, collections::HashMap,
io::{BufRead, Write}, io::{BufRead, Write},
sync::{ sync::{
atomic::{AtomicU64, AtomicUsize, Ordering}, atomic::{AtomicU64, Ordering},
mpsc::{channel, Receiver, Sender}, mpsc::{channel, Receiver, Sender},
Arc, Mutex, OnceLock, Arc, Mutex, OnceLock,
}, },
@ -16,15 +16,11 @@ use serde_json::Value;
pub mod kv; pub mod kv;
pub type DynNode = Arc<Mutex<dyn Node>>; pub type NodeyNodeFace = Arc<Mutex<dyn Node>>;
pub type OnInit = Box<dyn Fn(&Runner)>; pub type OnInit = Box<dyn Fn(&Runner)>;
pub type Result<T> = std::result::Result<T, ErrorCode>; pub type Result<T> = std::result::Result<T, ErrorCode>;
pub type RpcPromise = Receiver<Message>; pub type RpcPromise = Receiver<Message>;
static MSG_ID: AtomicU64 = AtomicU64::new(0);
pub trait Node { pub trait Node {
fn handle(&mut self, runner: &Runner, msg: Message); fn handle(&mut self, runner: &Runner, msg: Message);
} }
@ -62,23 +58,21 @@ impl Network {
} }
pub struct Runner { pub struct Runner {
node: DynNode, node: NodeyNodeFace,
node_id: OnceLock<String>, node_id: OnceLock<String>,
nodes: OnceLock<Vec<String>>, nodes: OnceLock<Vec<String>>,
network: OnceLock<Network>, network: OnceLock<Network>,
backdoor: OnceLock<Sender<Message>>, backdoor: OnceLock<Sender<Message>>,
steps: Arc<AtomicUsize>,
} }
impl Runner { impl Runner {
pub fn new(node: DynNode) -> Self { pub fn new(node: NodeyNodeFace) -> Self {
Runner { Runner {
node, node,
nodes: OnceLock::new(), nodes: OnceLock::new(),
node_id: OnceLock::new(), node_id: OnceLock::new(),
network: OnceLock::new(), network: OnceLock::new(),
backdoor: OnceLock::new(), backdoor: OnceLock::new(),
steps: Arc::new(AtomicUsize::new(0)),
} }
} }
@ -175,10 +169,6 @@ impl Runner {
self.nodes.get().unwrap() self.nodes.get().unwrap()
} }
pub fn steps(&self) -> usize {
self.steps.load(Ordering::SeqCst)
}
pub fn init(&self, msg: &Message) { pub fn init(&self, msg: &Message) {
let node_id = msg let node_id = msg
.body .body
@ -245,3 +235,5 @@ pub fn mk_payload(payload: &[(&str, Value)]) -> Payload {
.map(|p| (p.0.to_string(), p.1.clone())) .map(|p| (p.0.to_string(), p.1.clone()))
.collect() .collect()
} }
static MSG_ID: AtomicU64 = AtomicU64::new(0);