ezsockets integrated with cli startup

This commit is contained in:
Dave Hrycyszyn
2024-06-06 19:32:29 +01:00
parent ff9fbd49ec
commit 404a769259
3 changed files with 53 additions and 51 deletions

View File

@@ -10,7 +10,6 @@ use bft_json_crdt::{
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use websockets::WebSocket;
use crate::keys;
@@ -39,9 +38,9 @@ pub(crate) fn new(side_dir: PathBuf) -> (BaseCrdt<CrdtList>, Ed25519KeyPair) {
pub(crate) async fn send(
count: u32,
bft_crdt: &mut BaseCrdt<CrdtList>,
ws: &mut WebSocket,
// ws: &mut WebSocket,
keys: &Ed25519KeyPair,
) -> Result<(), websockets::WebSocketError> {
) {
// generate a placeholder transaction
let transaction = generate_transaction(count, keys.public().to_string());
@@ -53,9 +52,9 @@ pub(crate) async fn send(
.insert_idx(next - 1, transaction.clone())
.sign(&keys);
Ok(ws
.send_text(serde_json::to_string(&signed_op).unwrap())
.await?)
// Ok(ws
// .send_text(serde_json::to_string(&signed_op).unwrap())
// .await?)
}
fn generate_transaction(count: u32, pubkey: String) -> Value {

View File

@@ -1,4 +1,4 @@
/** use cli::{parse_args, Commands};
use cli::{parse_args, Commands};
pub(crate) mod cli;
pub(crate) mod init;
@@ -22,7 +22,7 @@ async fn main() {
Some(Commands::Run { name }) => {
let side_dir = home(name);
let (mut bft_crdt, keys) = list_transaction_crdt::new(side_dir);
websocket::start(keys, &mut bft_crdt).await.unwrap();
websocket::start(keys, &mut bft_crdt).await;
}
None => println!("No command provided. Exiting. See --help for more information."),
}
@@ -34,46 +34,3 @@ 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

@@ -21,3 +21,49 @@ loop {
}
*/
use async_trait::async_trait;
use bft_json_crdt::json_crdt::BaseCrdt;
use ezsockets::ClientConfig;
use fastcrypto::ed25519::Ed25519KeyPair;
use std::io::BufRead;
use crate::list_transaction_crdt::CrdtList;
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(())
}
}
pub(crate) async fn start(keys: Ed25519KeyPair, bft_crdt: &mut BaseCrdt<CrdtList>) {
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();
}
}