Skip to content

Commit

Permalink
Add UDP multicast simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
manifest committed Mar 1, 2025
1 parent 7964685 commit b71e108
Show file tree
Hide file tree
Showing 9 changed files with 544 additions and 24 deletions.
11 changes: 11 additions & 0 deletions examples/udp_vpv4_multicast/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "udp_ipv4_multicast"
version = "0.1.0"
edition = "2024"
publish = false

[dependencies]
tokio = "1"
turmoil = { path = "../.." }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
56 changes: 56 additions & 0 deletions examples/udp_vpv4_multicast/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::{net::Ipv4Addr, time::Duration};
use tracing::info;
use turmoil::{IpVersion, net::UdpSocket};

const N_STEPS: usize = 3;

fn main() {
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::builder()
.with_default_directive(tracing::level_filters::LevelFilter::INFO.into())
.from_env_lossy(),
)
.init();

let tick = Duration::from_millis(100);
let mut sim = turmoil::Builder::new()
.tick_duration(tick)
.ip_version(IpVersion::V4)
.build();

let server_port = 9000;
let multicast_group = "239.0.0.1".parse().unwrap();

for server_index in 0..2 {
sim.client(format!("server-{server_index}"), async move {
let socket = UdpSocket::bind((Ipv4Addr::UNSPECIFIED, server_port)).await?;
socket.join_multicast_v4(multicast_group, Ipv4Addr::UNSPECIFIED)?;

let mut buf = [0; 1024];
for _ in 0..N_STEPS {
let (n, addr) = socket.recv_from(&mut buf).await?;
let data = &buf[0..n];

info!("UDP packet from {} has been received: {:?}", addr, data);
}
Ok(())
});
}

sim.client("client", async move {
let socket = UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0)).await?;
let dst = (multicast_group, server_port);

for _ in 0..N_STEPS {
let _ = socket.send_to(&[1, 2, 3], dst).await?;
info!("UDP packet has been sent");

tokio::time::sleep(tick).await;
}

Ok(())
});

sim.run().unwrap();
}
11 changes: 11 additions & 0 deletions examples/udp_vpv6_multicast/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "udp_ipv6_multicast"
version = "0.1.0"
edition = "2024"
publish = false

[dependencies]
tokio = "1"
turmoil = { path = "../.." }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
56 changes: 56 additions & 0 deletions examples/udp_vpv6_multicast/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::{net::Ipv6Addr, time::Duration};
use tracing::info;
use turmoil::{IpVersion, net::UdpSocket};

const N_STEPS: usize = 3;

fn main() {
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::builder()
.with_default_directive(tracing::level_filters::LevelFilter::INFO.into())
.from_env_lossy(),
)
.init();

let tick = Duration::from_millis(100);
let mut sim = turmoil::Builder::new()
.tick_duration(tick)
.ip_version(IpVersion::V6)
.build();

let server_port = 9000;
let multicast_group = "ff08::1".parse().unwrap();

for host_index in 0..2 {
sim.client(format!("server-{host_index}"), async move {
let socket = UdpSocket::bind((Ipv6Addr::UNSPECIFIED, server_port)).await?;
socket.join_multicast_v6(&multicast_group, 0)?;

let mut buf = [0; 1024];
for _ in 0..N_STEPS {
let (n, addr) = socket.recv_from(&mut buf).await?;
let data = &buf[0..n];

info!("UDP packet from {} has been received: {:?}", addr, data);
}
Ok(())
});
}

sim.client("client", async move {
let socket = UdpSocket::bind((Ipv6Addr::UNSPECIFIED, 0)).await?;
let dst = (multicast_group, server_port);

for _ in 0..N_STEPS {
let _ = socket.send_to(&[1, 2, 3], dst).await?;
info!("UDP packet has been sent");

tokio::time::sleep(tick).await;
}

Ok(())
});

sim.run().unwrap();
}
2 changes: 1 addition & 1 deletion src/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum Protocol {
}

/// UDP datagram.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Datagram(pub Bytes);

/// This is a simplification of real TCP.
Expand Down
2 changes: 1 addition & 1 deletion src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::net::SocketAddr;
pub mod tcp;
pub use tcp::{listener::TcpListener, stream::TcpStream};

mod udp;
pub(crate) mod udp;
pub use udp::UdpSocket;

#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
Expand Down
Loading

0 comments on commit b71e108

Please sign in to comment.