-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
548 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_secs(1); | ||
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_secs(1); | ||
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.