Simplifying
This commit is contained in:
@@ -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::<SignedOp>(32);
|
||||
let crdt = BaseCrdt::<TransactionList>::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<websocket::WebSocketClient>,
|
||||
// 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();
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<SignedOp>,
|
||||
}
|
||||
|
||||
impl WebSocketClient {
|
||||
pub(crate) fn new(incoming_sender: mpsc::Sender<SignedOp>) -> Self {
|
||||
Self { incoming_sender }
|
||||
}
|
||||
|
||||
/// Start the websocket client
|
||||
pub(crate) async fn start(node: &mut SideNode, incoming_sender: mpsc::Sender<SignedOp>) {
|
||||
pub(crate) async fn start(
|
||||
incoming_sender: mpsc::Sender<SignedOp>,
|
||||
// stdin_sender: mpsc::Sender<String>,
|
||||
) -> tokio::task::JoinHandle<()> {
|
||||
tracing_subscriber::fmt::init();
|
||||
let config = ClientConfig::new("ws://localhost:8080/websocket");
|
||||
let (handle, future) =
|
||||
@@ -24,6 +23,7 @@ impl WebSocketClient {
|
||||
tokio::spawn(async move {
|
||||
future.await.unwrap();
|
||||
});
|
||||
tokio::spawn(async move {
|
||||
let stdin = std::io::stdin();
|
||||
let lines = stdin.lock().lines();
|
||||
for line in lines {
|
||||
@@ -31,17 +31,19 @@ impl WebSocketClient {
|
||||
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 if let "trace" = line.as_str() {
|
||||
// node.trace_crdt();
|
||||
// continue;
|
||||
} else {
|
||||
let fake = utils::fake_transaction("foo123".to_string());
|
||||
node.add_transaction_local(fake)
|
||||
// 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();
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user