2024-06-06 19:32:29 +01:00
|
|
|
use async_trait::async_trait;
|
2024-06-10 14:25:05 +01:00
|
|
|
use bft_json_crdt::json_crdt::SignedOp;
|
2024-06-06 19:32:29 +01:00
|
|
|
use ezsockets::ClientConfig;
|
2024-06-10 14:25:05 +01:00
|
|
|
use tokio::sync::mpsc;
|
2024-06-06 19:32:29 +01:00
|
|
|
|
2024-06-18 11:19:36 +01:00
|
|
|
use crate::utils;
|
|
|
|
|
|
2024-06-20 17:21:41 +01:00
|
|
|
pub struct Client {
|
2024-06-10 14:25:05 +01:00
|
|
|
incoming_sender: mpsc::Sender<SignedOp>,
|
2024-06-20 17:21:41 +01:00
|
|
|
handle: ezsockets::Client<Client>,
|
2024-06-10 14:25:05 +01:00
|
|
|
}
|
2024-06-07 17:03:05 +01:00
|
|
|
|
2024-06-20 17:21:41 +01:00
|
|
|
impl Client {
|
2024-06-07 17:03:05 +01:00
|
|
|
/// Start the websocket client
|
2024-06-20 17:21:41 +01:00
|
|
|
pub async fn new(incoming_sender: mpsc::Sender<SignedOp>) -> ezsockets::Client<Client> {
|
2024-06-07 17:03:05 +01:00
|
|
|
let config = ClientConfig::new("ws://localhost:8080/websocket");
|
2024-06-11 18:13:51 +01:00
|
|
|
let (handle, future) = ezsockets::connect(
|
2024-06-20 17:21:41 +01:00
|
|
|
|client| Client {
|
2024-06-11 18:13:51 +01:00
|
|
|
incoming_sender,
|
|
|
|
|
handle: client,
|
|
|
|
|
},
|
|
|
|
|
config,
|
|
|
|
|
)
|
|
|
|
|
.await;
|
2024-06-07 17:03:05 +01:00
|
|
|
tokio::spawn(async move {
|
|
|
|
|
future.await.unwrap();
|
|
|
|
|
});
|
2024-06-11 16:50:21 +01:00
|
|
|
handle
|
2024-06-07 17:03:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-06-06 19:32:29 +01:00
|
|
|
|
|
|
|
|
#[async_trait]
|
2024-06-20 17:21:41 +01:00
|
|
|
impl ezsockets::ClientExt for Client {
|
2024-06-11 18:33:08 +01:00
|
|
|
// Right now we're only using the Call type for sending signed ops
|
|
|
|
|
// change this to an enum if we need to send other types of calls, and
|
|
|
|
|
// match on it.
|
2024-06-11 18:13:51 +01:00
|
|
|
type Call = String;
|
2024-06-06 19:32:29 +01:00
|
|
|
|
2024-06-25 13:50:02 +01:00
|
|
|
/// When we receive a text message, apply the bft-crdt operation contained in it to our
|
|
|
|
|
/// local bft-crdt.
|
2024-06-06 19:32:29 +01:00
|
|
|
async fn on_text(&mut self, text: String) -> Result<(), ezsockets::Error> {
|
2024-06-18 11:19:36 +01:00
|
|
|
let string_sha = utils::shassy(text.clone());
|
|
|
|
|
println!("received text, sha: {string_sha}");
|
2024-06-10 14:25:05 +01:00
|
|
|
let incoming: bft_json_crdt::json_crdt::SignedOp = serde_json::from_str(&text).unwrap();
|
2024-06-18 11:19:36 +01:00
|
|
|
let object_sha = utils::shappy(incoming.clone());
|
|
|
|
|
println!("deserialized: {}", object_sha);
|
|
|
|
|
if string_sha != object_sha {
|
2024-06-25 13:50:02 +01:00
|
|
|
panic!("sha mismatch: {string_sha} != {object_sha}, bft-bft-crdt has failed");
|
2024-06-18 11:19:36 +01:00
|
|
|
}
|
2024-06-11 18:29:02 +01:00
|
|
|
self.incoming_sender.send(incoming).await?;
|
2024-06-06 19:32:29 +01:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 18:42:13 +01:00
|
|
|
/// When we receive a binary message, log the bytes. Currently unused.
|
2024-06-06 19:32:29 +01:00
|
|
|
async fn on_binary(&mut self, bytes: Vec<u8>) -> Result<(), ezsockets::Error> {
|
|
|
|
|
tracing::info!("received bytes: {bytes:?}");
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 18:42:13 +01:00
|
|
|
/// Call this with the `Call` type to send application data to the websocket client
|
|
|
|
|
/// (and from there, to the server).
|
2024-06-06 19:32:29 +01:00
|
|
|
async fn on_call(&mut self, call: Self::Call) -> Result<(), ezsockets::Error> {
|
2024-06-11 18:33:08 +01:00
|
|
|
self.handle.text(call)?;
|
2024-06-06 19:32:29 +01:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|