Starting a move towards ezsockets

This commit is contained in:
Dave Hrycyszyn
2024-06-06 19:25:54 +01:00
parent bc2307f085
commit ff9fbd49ec
6 changed files with 494 additions and 381 deletions

View File

@@ -1,4 +1,4 @@
use cli::{parse_args, Commands};
/** use cli::{parse_args, Commands};
pub(crate) mod cli;
pub(crate) mod init;
@@ -34,3 +34,46 @@ fn home(name: &String) -> std::path::PathBuf {
path.push(name);
path
}
**/
use async_trait::async_trait;
use ezsockets::ClientConfig;
use std::io::BufRead;
struct Client {}
#[async_trait]
impl ezsockets::ClientExt for Client {
type Call = ();
async fn on_text(&mut self, text: String) -> Result<(), ezsockets::Error> {
tracing::info!("received message: {text}");
Ok(())
}
async fn on_binary(&mut self, bytes: Vec<u8>) -> Result<(), ezsockets::Error> {
tracing::info!("received bytes: {bytes:?}");
Ok(())
}
async fn on_call(&mut self, call: Self::Call) -> Result<(), ezsockets::Error> {
let () = call;
Ok(())
}
}
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
let config = ClientConfig::new("ws://localhost:8080/websocket");
let (handle, future) = ezsockets::connect(|_client| Client {}, 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();
tracing::info!("sending {line}");
handle.text(line).unwrap();
}
}

View File

@@ -1,41 +1,23 @@
use crate::list_transaction_crdt::{self, CrdtList};
use base64::{engine::general_purpose, Engine as _};
use bft_json_crdt::json_crdt::BaseCrdt;
use bft_json_crdt::json_crdt::SignedOp;
use bft_json_crdt::keypair::Ed25519KeyPair;
use tokio::time;
use websockets::WebSocket;
/// Starts a websocket and periodically sends a BFT-CRDT message to the websocket server
pub(crate) async fn start(
keys: Ed25519KeyPair,
bft_crdt: &mut BaseCrdt<CrdtList>,
) -> Result<(), websockets::WebSocketError> {
println!("connecting to websocket at ws://127.0.0.1:8080/");
let mut ws = WebSocket::connect("ws://127.0.0.1:8080/").await?;
/*
let mut interval = every_ten_seconds();
let mut count = 0;
loop {
let _ = list_transaction_crdt::send(count, bft_crdt, &mut ws, &keys).await;
loop {
let _ = list_transaction_crdt::send(count, bft_crdt, &mut ws, &keys).await;
let msg = ws.receive().await?;
let msg = ws.receive().await?;
// deserialize the received websocket Frame into a string
let msg = msg.into_text().unwrap().0;
// deserialize the received websocket Frame into a string
let msg = msg.into_text().unwrap().0;
// deserialize the message into a Transaction struct
let incoming_operation: SignedOp = serde_json::from_str(&msg).unwrap();
// deserialize the message into a Transaction struct
let incoming_operation: SignedOp = serde_json::from_str(&msg).unwrap();
let author = general_purpose::STANDARD.encode(&incoming_operation.author());
println!("Received from {:?}", author);
let author = general_purpose::STANDARD.encode(&incoming_operation.author());
println!("Received from {:?}", author);
bft_crdt.apply(incoming_operation.clone());
bft_crdt.apply(incoming_operation.clone());
count = count + 1;
interval.tick().await;
}
count = count + 1;
interval.tick().await;
}
fn every_ten_seconds() -> time::Interval {
time::interval(time::Duration::from_secs(10))
}
*/