Skip to content

Commit b721476

Browse files
Ashleytomaka
authored andcommitted
Improve the code readability of the chat example (#1253)
1 parent 73e7878 commit b721476

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

examples/chat.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ use libp2p::{
5555
Swarm,
5656
NetworkBehaviour,
5757
identity,
58-
tokio_codec::{FramedRead, LinesCodec}
58+
tokio_codec::{FramedRead, LinesCodec},
59+
tokio_io::{AsyncRead, AsyncWrite},
60+
floodsub::{self, Floodsub, FloodsubEvent},
61+
mdns::{Mdns, MdnsEvent},
62+
swarm::NetworkBehaviourEventProcess
5963
};
6064

6165
fn main() {
@@ -70,25 +74,25 @@ fn main() {
7074
let transport = libp2p::build_development_transport(local_key);
7175

7276
// Create a Floodsub topic
73-
let floodsub_topic = libp2p::floodsub::TopicBuilder::new("chat").build();
77+
let floodsub_topic = floodsub::TopicBuilder::new("chat").build();
7478

7579
// We create a custom network behaviour that combines floodsub and mDNS.
7680
// In the future, we want to improve libp2p to make this easier to do.
7781
#[derive(NetworkBehaviour)]
78-
struct MyBehaviour<TSubstream: libp2p::tokio_io::AsyncRead + libp2p::tokio_io::AsyncWrite> {
79-
floodsub: libp2p::floodsub::Floodsub<TSubstream>,
80-
mdns: libp2p::mdns::Mdns<TSubstream>,
82+
struct MyBehaviour<TSubstream: AsyncRead + AsyncWrite> {
83+
floodsub: Floodsub<TSubstream>,
84+
mdns: Mdns<TSubstream>,
8185
}
8286

83-
impl<TSubstream: libp2p::tokio_io::AsyncRead + libp2p::tokio_io::AsyncWrite> libp2p::swarm::NetworkBehaviourEventProcess<libp2p::mdns::MdnsEvent> for MyBehaviour<TSubstream> {
84-
fn inject_event(&mut self, event: libp2p::mdns::MdnsEvent) {
87+
impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<MdnsEvent> for MyBehaviour<TSubstream> {
88+
fn inject_event(&mut self, event: MdnsEvent) {
8589
match event {
86-
libp2p::mdns::MdnsEvent::Discovered(list) => {
90+
MdnsEvent::Discovered(list) => {
8791
for (peer, _) in list {
8892
self.floodsub.add_node_to_partial_view(peer);
8993
}
9094
},
91-
libp2p::mdns::MdnsEvent::Expired(list) => {
95+
MdnsEvent::Expired(list) => {
9296
for (peer, _) in list {
9397
if !self.mdns.has_node(&peer) {
9498
self.floodsub.remove_node_from_partial_view(&peer);
@@ -99,10 +103,10 @@ fn main() {
99103
}
100104
}
101105

102-
impl<TSubstream: libp2p::tokio_io::AsyncRead + libp2p::tokio_io::AsyncWrite> libp2p::swarm::NetworkBehaviourEventProcess<libp2p::floodsub::FloodsubEvent> for MyBehaviour<TSubstream> {
106+
impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<FloodsubEvent> for MyBehaviour<TSubstream> {
103107
// Called when `floodsub` produces an event.
104-
fn inject_event(&mut self, message: libp2p::floodsub::FloodsubEvent) {
105-
if let libp2p::floodsub::FloodsubEvent::Message(message) = message {
108+
fn inject_event(&mut self, message: FloodsubEvent) {
109+
if let FloodsubEvent::Message(message) = message {
106110
println!("Received: '{:?}' from {:?}", String::from_utf8_lossy(&message.data), message.source);
107111
}
108112
}
@@ -111,12 +115,12 @@ fn main() {
111115
// Create a Swarm to manage peers and events
112116
let mut swarm = {
113117
let mut behaviour = MyBehaviour {
114-
floodsub: libp2p::floodsub::Floodsub::new(local_peer_id.clone()),
115-
mdns: libp2p::mdns::Mdns::new().expect("Failed to create mDNS service"),
118+
floodsub: Floodsub::new(local_peer_id.clone()),
119+
mdns: Mdns::new().expect("Failed to create mDNS service"),
116120
};
117121

118122
behaviour.floodsub.subscribe(floodsub_topic.clone());
119-
libp2p::Swarm::new(transport, behaviour, local_peer_id)
123+
Swarm::new(transport, behaviour, local_peer_id)
120124
};
121125

122126
// Reach out to another node if specified
@@ -138,7 +142,7 @@ fn main() {
138142
let mut framed_stdin = FramedRead::new(stdin, LinesCodec::new());
139143

140144
// Listen on all interfaces and whatever port the OS assigns
141-
libp2p::Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse().unwrap()).unwrap();
145+
Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse().unwrap()).unwrap();
142146

143147
// Kick it off
144148
let mut listening = false;

0 commit comments

Comments
 (0)