diff --git a/side-node/src/main.rs b/side-node/src/main.rs index 6b6252b..3e07c35 100644 --- a/side-node/src/main.rs +++ b/side-node/src/main.rs @@ -39,8 +39,29 @@ async fn setup(name: &String) -> SideNode { let keys = keys::load_from_file(side_dir); let (incoming_sender, incoming_receiver) = mpsc::channel::(32); let crdt = BaseCrdt::::new(&keys); - let mut node = SideNode::new(crdt, keys, incoming_receiver); + let node = SideNode::new(crdt, keys, incoming_receiver); + WebSocketClient::start(incoming_sender).await; println!("Node setup complete."); - WebSocketClient::start(&mut node, incoming_sender).await; node } + +// async fn maybe_autosend( +// autosend: bool, +// handle: ezsockets::Client, +// mut node: SideNode, +// ) { +// if autosend { +// let forever = task::spawn(async { +// let mut interval = time::interval(Duration::from_millis(10)); + +// loop { +// interval.tick().await; +// let fake = utils::fake_transaction("foo123".to_string()); +// let signed_op = node.add_transaction_local(fake); +// let json = serde_json::to_string(&signed_op).unwrap(); +// handle.text(json).unwrap(); +// } +// }); +// forever.await.unwrap(); +// } +// } diff --git a/side-node/src/node.rs b/side-node/src/node.rs index 008cbfb..b4f7247 100644 --- a/side-node/src/node.rs +++ b/side-node/src/node.rs @@ -38,7 +38,7 @@ impl SideNode { self.crdt.apply(incoming.clone()); } - pub(crate) fn add_transaction_local( + pub(crate) fn _add_transaction_local( &mut self, transaction: serde_json::Value, ) -> bft_json_crdt::json_crdt::SignedOp { @@ -59,7 +59,7 @@ impl SideNode { } /// Print the current state of the CRDT, can be used to debug - pub(crate) fn trace_crdt(&self) { + pub(crate) fn _trace_crdt(&self) { println!("{:?}", self.crdt.doc.list); } } diff --git a/side-node/src/websocket.rs b/side-node/src/websocket.rs index f5594a9..56709c3 100644 --- a/side-node/src/websocket.rs +++ b/side-node/src/websocket.rs @@ -4,19 +4,18 @@ use ezsockets::ClientConfig; use std::io::BufRead; use tokio::sync::mpsc; -use crate::{node::SideNode, utils}; +use crate::utils; pub(crate) struct WebSocketClient { incoming_sender: mpsc::Sender, } impl WebSocketClient { - pub(crate) fn new(incoming_sender: mpsc::Sender) -> Self { - Self { incoming_sender } - } - /// Start the websocket client - pub(crate) async fn start(node: &mut SideNode, incoming_sender: mpsc::Sender) { + pub(crate) async fn start( + incoming_sender: mpsc::Sender, + // stdin_sender: mpsc::Sender, + ) -> tokio::task::JoinHandle<()> { tracing_subscriber::fmt::init(); let config = ClientConfig::new("ws://localhost:8080/websocket"); let (handle, future) = @@ -24,24 +23,27 @@ impl WebSocketClient { tokio::spawn(async move { future.await.unwrap(); }); - let stdin = std::io::stdin(); - let lines = stdin.lock().lines(); - for line in lines { - println!("We don't get here until we send a message"); - let line = line.unwrap(); - let signed_op = if let "exit" = line.as_str() { - break; - } else if let "trace" = line.as_str() { - node.trace_crdt(); - continue; - } else { - let fake = utils::fake_transaction("foo123".to_string()); - node.add_transaction_local(fake) - }; - tracing::info!("sending {:?}", signed_op); - let json = serde_json::to_string(&signed_op).unwrap(); - handle.text(json).unwrap(); - } + tokio::spawn(async move { + let stdin = std::io::stdin(); + let lines = stdin.lock().lines(); + for line in lines { + println!("We don't get here until we send a message"); + let line = line.unwrap(); + let signed_op = if let "exit" = line.as_str() { + break; + // } else if let "trace" = line.as_str() { + // node.trace_crdt(); + // continue; + } else { + let fake = utils::fake_transaction("foo123".to_string()); + // stdin_sender.send(fake).await.unwrap(); + fake + }; + tracing::info!("sending {:?}", signed_op); + let json = serde_json::to_string(&signed_op).unwrap(); + handle.text(json).unwrap(); + } + }) } }