Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f9ef6b9
initial impl zombienet backchannel
pepoviola Nov 25, 2021
ce50d2f
clean code
pepoviola Nov 25, 2021
46c25b3
changes from feedback
pepoviola Nov 28, 2021
6a3b7c6
fmt and typo
pepoviola Nov 28, 2021
e6f73a1
remove default comment
pepoviola Nov 28, 2021
b3be116
refactor to use ws tx/rx
pepoviola Dec 7, 2021
b3eed29
fmt
pepoviola Dec 7, 2021
91b6975
Merge branch 'master' into javier-zombienet-backchannel
pepoviola Dec 7, 2021
5953051
derive Clone for ZombienetBackchannel
pepoviola Dec 7, 2021
e340d32
Revert "derive Clone for ZombienetBackchannel"
pepoviola Dec 7, 2021
0ccb705
change tracing prefix value
pepoviola Dec 9, 2021
39dae39
Merge branch 'master' into javier-zombienet-backchannel
pepoviola Dec 14, 2021
f3014e7
refactor backchannel
pepoviola Dec 14, 2021
1558d42
Update node/zombienet-backchannel/Cargo.toml
pepoviola Dec 14, 2021
bd729ae
add docs to broadcaster methods
pepoviola Dec 14, 2021
486794f
add more docs
pepoviola Dec 14, 2021
ed47fee
Merge branch 'master' into javier-zombienet-backchannel
pepoviola Dec 23, 2021
16a7b99
fmt
pepoviola Dec 23, 2021
c83269d
Merge branch 'master' into javier-zombienet-backchannel
pepoviola Jan 6, 2022
1e001cd
fix spellcheck
pepoviola Jan 6, 2022
d1e4223
update lock file
pepoviola Jan 6, 2022
1a38285
remove unused
pepoviola Jan 6, 2022
e4d9dbc
fmt
pepoviola Jan 6, 2022
c5a0791
Merge branch 'master' into javier-zombienet-backchannel
pepoviola Jan 7, 2022
9cda227
changes from feedback
pepoviola Jan 7, 2022
d14b7c8
fmt
pepoviola Jan 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fmt
  • Loading branch information
pepoviola committed Dec 23, 2021
commit 16a7b99b8ab996b9eeae34d90f207bb816cde737
26 changes: 12 additions & 14 deletions node/zombienet-backchannel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ use futures_util::{stream::SplitSink, SinkExt, StreamExt};
use lazy_static::lazy_static;
use parity_scale_codec as codec;
use serde::{Deserialize, Serialize};
use std::env;
use std::sync::Mutex;
use std::{env, sync::Mutex};
use tokio::{net::TcpStream, sync::broadcast};
use tokio_tungstenite::{
connect_async, tungstenite::protocol::Message, MaybeTlsStream, WebSocketStream,
Expand Down Expand Up @@ -54,8 +53,8 @@ pub struct Broadcaster;
pub const ZOMBIENET: &str = "🧟ZOMBIENET🧟";

impl Broadcaster {
/// Return a subscriber that will receive all message broadcasted by the zombienet backchannel
/// websocket server.
/// Return a subscriber that will receive all message broadcasted by the zombienet backchannel
/// websocket server.
pub fn subscribe(&self) -> Result<broadcast::Receiver<BackchannelItem>, BackchannelError> {
let mut zombienet_bkc = ZOMBIENET_BACKCHANNEL.lock().unwrap();
let sender = zombienet_bkc.as_mut().unwrap().broadcast_tx.clone();
Expand All @@ -66,15 +65,15 @@ impl Broadcaster {
}
}

/// Provides a simple api to send a key/value to the zombienet websocket server.
/// Provides a simple api to send a key/value to the zombienet websocket server.
pub async fn send(
&mut self,
key: &'static str,
val: impl codec::Encode,
) -> Result<(), BackchannelError> {
let mut zombienet_bkc = ZOMBIENET_BACKCHANNEL.lock().unwrap();
if zombienet_bkc.is_none() {
return Err(BackchannelError::Uninitialized);
return Err(BackchannelError::Uninitialized)
}
let encoded = val.encode();
let backchannel_item = BackchannelItem {
Expand Down Expand Up @@ -114,15 +113,14 @@ impl ZombienetBackchannel {
tokio::spawn(async move {
while let Some(Ok(Message::Text(text))) = read.next().await {
match serde_json::from_str::<BackchannelItem>(&text) {
Ok(backchannel_item) => {
Ok(backchannel_item) =>
if tx1.send(backchannel_item).is_err() {
tracing::error!("Error sending through the channel");
return;
}
}
return
},
Err(_) => {
tracing::error!("Invalid payload received");
}
},
}
}
});
Expand All @@ -141,14 +139,14 @@ impl ZombienetBackchannel {
});

*zombienet_bkc = Some(ZombienetBackchannel { broadcast_tx: tx, ws_tx: tx_relay });
return Ok(());
return Ok(())
}

Err(BackchannelError::AlreadyInitialized)
}

/// Ensure that the backchannel is initialized and return a broadcaster instance
/// allowing to subscribe or send new items.
/// Ensure that the backchannel is initialized and return a broadcaster instance
/// allowing to subscribe or send new items.
pub fn broadcaster() -> Result<Broadcaster, BackchannelError> {
if ZOMBIENET_BACKCHANNEL.lock().unwrap().is_some() {
Ok(Broadcaster {})
Expand Down