mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-29 17:51:35 +00:00
swarm/src/behaviour: Deprecate NetworkBehaviourEventProcess (#2784)
In preparation for https://github.com/libp2p/rust-libp2p/pull/2751.
This commit is contained in:
@ -44,7 +44,7 @@ use libp2p::{
|
||||
mdns::{Mdns, MdnsEvent},
|
||||
mplex,
|
||||
noise,
|
||||
swarm::{dial_opts::DialOpts, NetworkBehaviourEventProcess, SwarmBuilder, SwarmEvent},
|
||||
swarm::{SwarmBuilder, SwarmEvent},
|
||||
// `TokioTcpTransport` is available through the `tcp-tokio` feature.
|
||||
tcp::TokioTcpTransport,
|
||||
Multiaddr,
|
||||
@ -82,47 +82,29 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
// Create a Floodsub topic
|
||||
let floodsub_topic = floodsub::Topic::new("chat");
|
||||
|
||||
// We create a custom network behaviour that combines floodsub and mDNS.
|
||||
// The derive generates a delegating `NetworkBehaviour` impl which in turn
|
||||
// requires the implementations of `NetworkBehaviourEventProcess` for
|
||||
// the events of each behaviour.
|
||||
// We create a custom behaviour that combines floodsub and mDNS.
|
||||
// The derive generates a delegating `NetworkBehaviour` impl.
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(event_process = true)]
|
||||
#[behaviour(out_event = "MyBehaviourEvent")]
|
||||
struct MyBehaviour {
|
||||
floodsub: Floodsub,
|
||||
mdns: Mdns,
|
||||
}
|
||||
|
||||
impl NetworkBehaviourEventProcess<FloodsubEvent> for MyBehaviour {
|
||||
// Called when `floodsub` produces an event.
|
||||
fn inject_event(&mut self, message: FloodsubEvent) {
|
||||
if let FloodsubEvent::Message(message) = message {
|
||||
println!(
|
||||
"Received: '{:?}' from {:?}",
|
||||
String::from_utf8_lossy(&message.data),
|
||||
message.source
|
||||
);
|
||||
}
|
||||
enum MyBehaviourEvent {
|
||||
Floodsub(FloodsubEvent),
|
||||
Mdns(MdnsEvent),
|
||||
}
|
||||
|
||||
impl From<FloodsubEvent> for MyBehaviourEvent {
|
||||
fn from(event: FloodsubEvent) -> Self {
|
||||
MyBehaviourEvent::Floodsub(event)
|
||||
}
|
||||
}
|
||||
|
||||
impl NetworkBehaviourEventProcess<MdnsEvent> for MyBehaviour {
|
||||
// Called when `mdns` produces an event.
|
||||
fn inject_event(&mut self, event: MdnsEvent) {
|
||||
match event {
|
||||
MdnsEvent::Discovered(list) => {
|
||||
for (peer, _) in list {
|
||||
self.floodsub.add_node_to_partial_view(peer);
|
||||
}
|
||||
}
|
||||
MdnsEvent::Expired(list) => {
|
||||
for (peer, _) in list {
|
||||
if !self.mdns.has_node(&peer) {
|
||||
self.floodsub.remove_node_from_partial_view(&peer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
impl From<MdnsEvent> for MyBehaviourEvent {
|
||||
fn from(event: MdnsEvent) -> Self {
|
||||
MyBehaviourEvent::Mdns(event)
|
||||
}
|
||||
}
|
||||
|
||||
@ -166,8 +148,36 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
swarm.behaviour_mut().floodsub.publish(floodsub_topic.clone(), line.as_bytes());
|
||||
}
|
||||
event = swarm.select_next_some() => {
|
||||
if let SwarmEvent::NewListenAddr { address, .. } = event {
|
||||
println!("Listening on {:?}", address);
|
||||
match event {
|
||||
SwarmEvent::NewListenAddr { address, .. } => {
|
||||
println!("Listening on {:?}", address);
|
||||
}
|
||||
SwarmEvent::Behaviour(MyBehaviourEvent::Floodsub(event)) => {
|
||||
if let FloodsubEvent::Message(message) = event {
|
||||
println!(
|
||||
"Received: '{:?}' from {:?}",
|
||||
String::from_utf8_lossy(&message.data),
|
||||
message.source
|
||||
);
|
||||
}
|
||||
}
|
||||
SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(event)) => {
|
||||
match event {
|
||||
MdnsEvent::Discovered(list) => {
|
||||
for (peer, _) in list {
|
||||
swarm.behaviour_mut().floodsub.add_node_to_partial_view(peer);
|
||||
}
|
||||
}
|
||||
MdnsEvent::Expired(list) => {
|
||||
for (peer, _) in list {
|
||||
if !swarm.behaviour().mdns.has_node(&peer) {
|
||||
swarm.behaviour_mut().floodsub.remove_node_from_partial_view(&peer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user