2024-06-05 19:52:37 +01:00
|
|
|
use crate::list_transaction_crdt::{self, CrdtList};
|
2024-06-05 16:50:28 +01:00
|
|
|
use bft_json_crdt::json_crdt::SignedOp;
|
2024-06-05 19:42:41 +01:00
|
|
|
use bft_json_crdt::json_crdt::{BaseCrdt, CrdtNode};
|
|
|
|
|
use bft_json_crdt::keypair::Ed25519KeyPair;
|
2024-06-05 18:22:11 +01:00
|
|
|
use tokio::time;
|
2024-05-29 16:35:00 +01:00
|
|
|
use websockets::WebSocket;
|
|
|
|
|
|
2024-05-29 17:29:20 +01:00
|
|
|
/// Starts a websocket and periodically sends a BFT-CRDT message to the websocket server
|
2024-06-05 19:42:41 +01:00
|
|
|
pub(crate) async fn start(
|
|
|
|
|
keys: Ed25519KeyPair,
|
2024-06-05 19:52:37 +01:00
|
|
|
bft_crdt: &mut BaseCrdt<CrdtList>,
|
2024-06-05 19:42:41 +01:00
|
|
|
) -> Result<(), websockets::WebSocketError> {
|
2024-06-05 16:18:47 +01:00
|
|
|
println!("connecting to websocket at ws://127.0.0.1:8080/");
|
|
|
|
|
let mut ws = WebSocket::connect("ws://127.0.0.1:8080/").await?;
|
2024-05-29 16:35:00 +01:00
|
|
|
|
2024-06-05 18:22:11 +01:00
|
|
|
let mut interval = every_two_seconds();
|
|
|
|
|
let mut count = 0;
|
2024-06-05 18:07:59 +01:00
|
|
|
loop {
|
2024-06-05 19:49:13 +01:00
|
|
|
let _ = list_transaction_crdt::send(count, bft_crdt, &mut ws, &keys).await;
|
2024-05-29 17:29:20 +01:00
|
|
|
|
2024-05-29 18:17:34 +01:00
|
|
|
let msg = ws.receive().await?;
|
2024-05-29 16:35:00 +01:00
|
|
|
println!("Received: {:?}", msg);
|
2024-06-05 16:50:28 +01:00
|
|
|
|
2024-06-05 19:42:41 +01:00
|
|
|
// deserialize the received websocket Frame into a string
|
2024-06-05 16:50:28 +01:00
|
|
|
let msg = msg.into_text().unwrap().0;
|
|
|
|
|
|
|
|
|
|
// deserialize the message into a Transaction struct
|
2024-06-05 18:22:11 +01:00
|
|
|
let incoming_operation: SignedOp = serde_json::from_str(&msg).unwrap();
|
|
|
|
|
println!("Received a new network operation: {:?}", incoming_operation);
|
2024-06-05 16:50:28 +01:00
|
|
|
|
2024-06-05 18:22:11 +01:00
|
|
|
bft_crdt.apply(incoming_operation);
|
2024-06-05 18:07:59 +01:00
|
|
|
|
2024-06-05 18:22:11 +01:00
|
|
|
println!("New crdt state is: {}", bft_crdt.doc.view());
|
|
|
|
|
count = count + 1;
|
|
|
|
|
interval.tick().await;
|
2024-05-29 16:35:00 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-05-29 18:17:34 +01:00
|
|
|
|
2024-06-05 18:22:11 +01:00
|
|
|
fn every_two_seconds() -> time::Interval {
|
|
|
|
|
time::interval(time::Duration::from_secs(2))
|
|
|
|
|
}
|