Almost working, I've got a blocking I/O problem with stdin now :)

This commit is contained in:
Dave Hrycyszyn
2024-06-10 14:25:05 +01:00
parent c3f5b2890b
commit 4717ffa7e8
3 changed files with 70 additions and 40 deletions

View File

@@ -1,35 +1,47 @@
use async_trait::async_trait;
use bft_json_crdt::json_crdt::SignedOp;
use ezsockets::ClientConfig;
use std::io::BufRead;
use tokio::fs::File;
use tokio::io;
use tokio::sync::mpsc;
pub(crate) struct WebSocketClient {}
use crate::{node::SideNode, utils};
pub(crate) struct WebSocketClient {
incoming_sender: mpsc::Sender<SignedOp>,
}
impl WebSocketClient {
pub(crate) fn new() -> Self {
Self {}
pub(crate) fn new(incoming_sender: mpsc::Sender<SignedOp>) -> Self {
Self { incoming_sender }
}
/// Start the websocket client
pub(crate) async fn start(&mut self) {
pub(crate) async fn start(node: &mut SideNode, incoming_sender: mpsc::Sender<SignedOp>) {
tracing_subscriber::fmt::init();
let config = ClientConfig::new("ws://localhost:8080/websocket");
let (handle, future) = ezsockets::connect(|_client| WebSocketClient {}, config).await;
let (handle, future) =
ezsockets::connect(|_client| WebSocketClient { incoming_sender }, config).await;
tokio::spawn(async move {
future.await.unwrap();
});
let stdin = std::io::stdin();
let lines = stdin.lock().lines();
for line in lines {
let line = line.unwrap();
let signed_op = if let "exit" = line.as_str() {
break;
} else {
// list_transaction_crdt::create(bft_crdt, &keys)
};
tracing::info!("sending {:?}", signed_op);
let json = serde_json::to_string(&signed_op).unwrap();
handle.text(json).unwrap();
}
let stdin = tokio::io::stdin();
// let lines = stdin.lock().lines();
//
let mut reader = FramedRead::new(stdin, LinesCodec::new());
let line = reader.next().await.transpose()?.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();
}
}
@@ -39,9 +51,9 @@ impl ezsockets::ClientExt for WebSocketClient {
async fn on_text(&mut self, text: String) -> Result<(), ezsockets::Error> {
tracing::info!("received message: {text}");
let _incoming: bft_json_crdt::json_crdt::SignedOp = serde_json::from_str(&text).unwrap();
// TODO: make this sucker work
// self.bft_crdt.apply(incoming.clone());
let incoming: bft_json_crdt::json_crdt::SignedOp = serde_json::from_str(&text).unwrap();
tracing::info!("received signed op: {incoming:?}");
self.incoming_sender.send(incoming).await.unwrap();
Ok(())
}