mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-05-15 12:21:20 +00:00
refactor(webrtc): remove example in favor of browser-webrtc
example
We've moved away from having individual examples in crates in #3111. This one in `transports/webrtc` seems to have slipped through. Pull-Request: #4531.
This commit is contained in:
parent
854a6ae868
commit
02d0ee0b3d
6
Cargo.lock
generated
6
Cargo.lock
generated
@ -3180,20 +3180,16 @@ dependencies = [
|
|||||||
name = "libp2p-webrtc"
|
name = "libp2p-webrtc"
|
||||||
version = "0.6.1-alpha"
|
version = "0.6.1-alpha"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"bytes",
|
"bytes",
|
||||||
"env_logger 0.10.0",
|
"env_logger 0.10.0",
|
||||||
"futures",
|
"futures",
|
||||||
"futures-timer",
|
"futures-timer",
|
||||||
"hex",
|
"hex",
|
||||||
"hex-literal",
|
|
||||||
"if-watch",
|
"if-watch",
|
||||||
"libp2p-core",
|
"libp2p-core",
|
||||||
"libp2p-identity",
|
"libp2p-identity",
|
||||||
"libp2p-noise",
|
"libp2p-noise",
|
||||||
"libp2p-ping",
|
|
||||||
"libp2p-swarm",
|
|
||||||
"libp2p-webrtc-utils",
|
"libp2p-webrtc-utils",
|
||||||
"log",
|
"log",
|
||||||
"multihash",
|
"multihash",
|
||||||
@ -3206,8 +3202,6 @@ dependencies = [
|
|||||||
"tinytemplate",
|
"tinytemplate",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-util",
|
"tokio-util",
|
||||||
"unsigned-varint",
|
|
||||||
"void",
|
|
||||||
"webrtc",
|
"webrtc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -38,20 +38,10 @@ tokio = ["dep:tokio", "dep:tokio-util", "dep:webrtc", "if-watch/tokio"]
|
|||||||
pem = ["webrtc?/pem"]
|
pem = ["webrtc?/pem"]
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
anyhow = "1.0"
|
|
||||||
env_logger = "0.10"
|
env_logger = "0.10"
|
||||||
hex-literal = "0.4"
|
|
||||||
libp2p-swarm = { workspace = true, features = ["macros", "tokio"] }
|
|
||||||
libp2p-ping = { workspace = true }
|
|
||||||
tokio = { version = "1.32", features = ["full"] }
|
tokio = { version = "1.32", features = ["full"] }
|
||||||
unsigned-varint = { version = "0.7", features = ["asynchronous_codec"] }
|
|
||||||
void = "1"
|
|
||||||
quickcheck = "1.0.3"
|
quickcheck = "1.0.3"
|
||||||
|
|
||||||
[[test]]
|
[[test]]
|
||||||
name = "smoke"
|
name = "smoke"
|
||||||
required-features = ["tokio"]
|
required-features = ["tokio"]
|
||||||
|
|
||||||
[[example]]
|
|
||||||
name = "listen_ping"
|
|
||||||
required-features = ["tokio"]
|
|
||||||
|
@ -1,61 +0,0 @@
|
|||||||
use anyhow::Result;
|
|
||||||
use futures::StreamExt;
|
|
||||||
use libp2p_core::muxing::StreamMuxerBox;
|
|
||||||
use libp2p_core::Transport;
|
|
||||||
use libp2p_identity as identity;
|
|
||||||
use libp2p_ping as ping;
|
|
||||||
use libp2p_swarm::{Swarm, SwarmBuilder};
|
|
||||||
use rand::thread_rng;
|
|
||||||
use std::time::Duration;
|
|
||||||
use void::Void;
|
|
||||||
|
|
||||||
/// An example WebRTC server that will accept connections and run the ping protocol on them.
|
|
||||||
#[tokio::main]
|
|
||||||
async fn main() -> Result<()> {
|
|
||||||
let mut swarm = create_swarm()?;
|
|
||||||
|
|
||||||
swarm.listen_on("/ip4/127.0.0.1/udp/0/webrtc-direct".parse()?)?;
|
|
||||||
|
|
||||||
loop {
|
|
||||||
let event = swarm.next().await.unwrap();
|
|
||||||
eprintln!("New event: {event:?}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_swarm() -> Result<Swarm<ping::Behaviour>> {
|
|
||||||
let id_keys = identity::Keypair::generate_ed25519();
|
|
||||||
let peer_id = id_keys.public().to_peer_id();
|
|
||||||
let transport = libp2p_webrtc::tokio::Transport::new(
|
|
||||||
id_keys,
|
|
||||||
libp2p_webrtc::tokio::Certificate::generate(&mut thread_rng())?,
|
|
||||||
);
|
|
||||||
|
|
||||||
let cfg = ping::Config::new().with_interval(Duration::from_millis(10));
|
|
||||||
let transport = transport
|
|
||||||
.map(|(peer_id, conn), _| (peer_id, StreamMuxerBox::new(conn)))
|
|
||||||
.boxed();
|
|
||||||
|
|
||||||
Ok(
|
|
||||||
SwarmBuilder::with_tokio_executor(transport, ping::Behaviour::new(cfg.clone()), peer_id)
|
|
||||||
.idle_connection_timeout(Duration::from_secs(5))
|
|
||||||
.build(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
#[allow(clippy::large_enum_variant)]
|
|
||||||
enum Event {
|
|
||||||
Ping(ping::Event),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<ping::Event> for Event {
|
|
||||||
fn from(e: ping::Event) -> Self {
|
|
||||||
Event::Ping(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Void> for Event {
|
|
||||||
fn from(event: Void) -> Self {
|
|
||||||
void::unreachable(event)
|
|
||||||
}
|
|
||||||
}
|
|
@ -97,24 +97,18 @@ enum Kind {
|
|||||||
InvalidPEM(#[from] webrtc::Error),
|
InvalidPEM(#[from] webrtc::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(all(test, feature = "pem"))]
|
||||||
mod test {
|
mod test {
|
||||||
#[cfg(feature = "pem")]
|
use super::*;
|
||||||
use anyhow::Result;
|
use rand::thread_rng;
|
||||||
|
|
||||||
#[cfg(feature = "pem")]
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_certificate_serialize_pem_and_from_pem() -> Result<()> {
|
fn test_certificate_serialize_pem_and_from_pem() {
|
||||||
use super::*;
|
|
||||||
use rand::thread_rng;
|
|
||||||
|
|
||||||
let cert = Certificate::generate(&mut thread_rng()).unwrap();
|
let cert = Certificate::generate(&mut thread_rng()).unwrap();
|
||||||
|
|
||||||
let pem = cert.serialize_pem();
|
let pem = cert.serialize_pem();
|
||||||
let loaded_cert = Certificate::from_pem(&pem)?;
|
let loaded_cert = Certificate::from_pem(&pem).unwrap();
|
||||||
|
|
||||||
assert_eq!(loaded_cert, cert);
|
assert_eq!(loaded_cert, cert)
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user