This commit is contained in:
Evan
2026-03-04 17:25:44 +00:00
parent 530c125462
commit b59bb9dba6

View File

@@ -1,7 +1,6 @@
use futures_lite::StreamExt;
use futures_lite::StreamExt as _;
use libp2p::identity;
use networking::swarm;
use networking::swarm::{FromSwarm, ToSwarm};
use networking::{FromSwarm, ToSwarm};
use tokio::sync::{mpsc, oneshot};
use tokio::{io, io::AsyncBufReadExt as _};
use tracing_subscriber::EnvFilter;
@@ -9,23 +8,23 @@ use tracing_subscriber::filter::LevelFilter;
#[tokio::main]
async fn main() {
let _ = tracing_subscriber::fmt()
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env().add_directive(LevelFilter::INFO.into()))
.try_init();
.init();
let (to_swarm, from_client) = mpsc::channel(20);
// Configure swarm
let mut swarm = swarm::create_swarm(identity::Keypair::generate_ed25519(), from_client)
let mut swarm = networking::create_swarm(identity::Keypair::generate_ed25519(), from_client)
.expect("Swarm creation failed")
.into_stream();
// Create a Gossipsub topic & subscribe
let (tx, rx) = oneshot::channel();
_ = to_swarm
let (sub_tx, sub_rx) = oneshot::channel();
to_swarm
.send(ToSwarm::Subscribe {
topic: "test-net".to_string(),
result_sender: tx,
topic: "test-net".to_owned(),
result_sender: sub_tx,
})
.await
.expect("should send");
@@ -35,15 +34,16 @@ async fn main() {
println!("Enter messages via STDIN and they will be sent to connected peers using Gossipsub");
tokio::task::spawn(async move {
rx.await
sub_rx
.await
.expect("tx not dropped")
.expect("subscribe shouldn't fail");
loop {
if let Ok(Some(line)) = stdin.next_line().await {
let (tx, rx) = oneshot::channel();
if let Err(e) = to_swarm
.send(swarm::ToSwarm::Publish {
topic: "test-net".to_string(),
.send(ToSwarm::Publish {
topic: "test-net".to_owned(),
data: line.as_bytes().to_vec(),
result_sender: tx,
})
@@ -51,7 +51,7 @@ async fn main() {
{
println!("Send error: {e:?}");
return;
};
}
match rx.await {
Ok(Err(e)) => println!("Publish error: {e:?}"),
Err(e) => println!("Publish error: {e:?}"),