mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-19 13:01:22 +00:00
refactor(examples): remove #[behaviour(to_swarm = "Event")]
Removes the usage of the `to_swarm` `libp2p-swarm-derive` attribute in favor of the automatically generated event through the `NetworkBehaviour` derive macro. Pull-Request: #4580.
This commit is contained in:
@ -110,7 +110,6 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
};
|
||||
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(to_swarm = "Event")]
|
||||
struct Behaviour {
|
||||
relay_client: relay::client::Behaviour,
|
||||
ping: ping::Behaviour,
|
||||
@ -118,39 +117,6 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
dcutr: dcutr::Behaviour,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
enum Event {
|
||||
Ping(ping::Event),
|
||||
Identify(identify::Event),
|
||||
Relay(relay::client::Event),
|
||||
Dcutr(dcutr::Event),
|
||||
}
|
||||
|
||||
impl From<ping::Event> for Event {
|
||||
fn from(e: ping::Event) -> Self {
|
||||
Event::Ping(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<identify::Event> for Event {
|
||||
fn from(e: identify::Event) -> Self {
|
||||
Event::Identify(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<relay::client::Event> for Event {
|
||||
fn from(e: relay::client::Event) -> Self {
|
||||
Event::Relay(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<dcutr::Event> for Event {
|
||||
fn from(e: dcutr::Event) -> Self {
|
||||
Event::Dcutr(e)
|
||||
}
|
||||
}
|
||||
|
||||
let behaviour = Behaviour {
|
||||
relay_client: client,
|
||||
ping: ping::Behaviour::new(ping::Config::new()),
|
||||
@ -207,12 +173,14 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
SwarmEvent::NewListenAddr { .. } => {}
|
||||
SwarmEvent::Dialing { .. } => {}
|
||||
SwarmEvent::ConnectionEstablished { .. } => {}
|
||||
SwarmEvent::Behaviour(Event::Ping(_)) => {}
|
||||
SwarmEvent::Behaviour(Event::Identify(identify::Event::Sent { .. })) => {
|
||||
SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {}
|
||||
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Sent {
|
||||
..
|
||||
})) => {
|
||||
info!("Told relay its public address.");
|
||||
told_relay_observed_addr = true;
|
||||
}
|
||||
SwarmEvent::Behaviour(Event::Identify(identify::Event::Received {
|
||||
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Received {
|
||||
info: identify::Info { observed_addr, .. },
|
||||
..
|
||||
})) => {
|
||||
@ -252,22 +220,22 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
SwarmEvent::NewListenAddr { address, .. } => {
|
||||
info!("Listening on {:?}", address);
|
||||
}
|
||||
SwarmEvent::Behaviour(Event::Relay(
|
||||
SwarmEvent::Behaviour(BehaviourEvent::RelayClient(
|
||||
relay::client::Event::ReservationReqAccepted { .. },
|
||||
)) => {
|
||||
assert!(opts.mode == Mode::Listen);
|
||||
info!("Relay accepted our reservation request.");
|
||||
}
|
||||
SwarmEvent::Behaviour(Event::Relay(event)) => {
|
||||
SwarmEvent::Behaviour(BehaviourEvent::RelayClient(event)) => {
|
||||
info!("{:?}", event)
|
||||
}
|
||||
SwarmEvent::Behaviour(Event::Dcutr(event)) => {
|
||||
SwarmEvent::Behaviour(BehaviourEvent::Dcutr(event)) => {
|
||||
info!("{:?}", event)
|
||||
}
|
||||
SwarmEvent::Behaviour(Event::Identify(event)) => {
|
||||
SwarmEvent::Behaviour(BehaviourEvent::Identify(event)) => {
|
||||
info!("{:?}", event)
|
||||
}
|
||||
SwarmEvent::Behaviour(Event::Ping(_)) => {}
|
||||
SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {}
|
||||
SwarmEvent::ConnectionEstablished {
|
||||
peer_id, endpoint, ..
|
||||
} => {
|
||||
|
@ -49,37 +49,18 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
// We create a custom network behaviour that combines Kademlia and mDNS.
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(to_swarm = "MyBehaviourEvent")]
|
||||
struct MyBehaviour {
|
||||
struct Behaviour {
|
||||
kademlia: kad::Behaviour<MemoryStore>,
|
||||
mdns: mdns::async_io::Behaviour,
|
||||
}
|
||||
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
enum MyBehaviourEvent {
|
||||
Kademlia(kad::Event),
|
||||
Mdns(mdns::Event),
|
||||
}
|
||||
|
||||
impl From<kad::Event> for MyBehaviourEvent {
|
||||
fn from(event: kad::Event) -> Self {
|
||||
MyBehaviourEvent::Kademlia(event)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<mdns::Event> for MyBehaviourEvent {
|
||||
fn from(event: mdns::Event) -> Self {
|
||||
MyBehaviourEvent::Mdns(event)
|
||||
}
|
||||
}
|
||||
|
||||
// Create a swarm to manage peers and events.
|
||||
let mut swarm = {
|
||||
// Create a Kademlia behaviour.
|
||||
let store = MemoryStore::new(local_peer_id);
|
||||
let kademlia = kad::Behaviour::new(local_peer_id, store);
|
||||
let mdns = mdns::async_io::Behaviour::new(mdns::Config::default(), local_peer_id)?;
|
||||
let behaviour = MyBehaviour { kademlia, mdns };
|
||||
let behaviour = Behaviour { kademlia, mdns };
|
||||
SwarmBuilder::with_async_std_executor(transport, behaviour, local_peer_id).build()
|
||||
};
|
||||
|
||||
@ -99,12 +80,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
SwarmEvent::NewListenAddr { address, .. } => {
|
||||
println!("Listening in {address:?}");
|
||||
},
|
||||
SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(mdns::Event::Discovered(list))) => {
|
||||
SwarmEvent::Behaviour(BehaviourEvent::Mdns(mdns::Event::Discovered(list))) => {
|
||||
for (peer_id, multiaddr) in list {
|
||||
swarm.behaviour_mut().kademlia.add_address(&peer_id, multiaddr);
|
||||
}
|
||||
}
|
||||
SwarmEvent::Behaviour(MyBehaviourEvent::Kademlia(kad::Event::OutboundQueryProgressed { result, ..})) => {
|
||||
SwarmEvent::Behaviour(BehaviourEvent::Kademlia(kad::Event::OutboundQueryProgressed { result, ..})) => {
|
||||
match result {
|
||||
kad::QueryResult::GetProviders(Ok(kad::GetProvidersOk::FoundProviders { key, providers, .. })) => {
|
||||
for peer in providers {
|
||||
|
@ -51,7 +51,7 @@ pub(crate) async fn new(
|
||||
// higher layer network behaviour logic.
|
||||
let mut swarm = SwarmBuilder::with_async_std_executor(
|
||||
transport,
|
||||
ComposedBehaviour {
|
||||
Behaviour {
|
||||
kademlia: kad::Behaviour::new(peer_id, kad::record::store::MemoryStore::new(peer_id)),
|
||||
request_response: request_response::cbor::Behaviour::new(
|
||||
[(
|
||||
@ -171,7 +171,7 @@ impl Client {
|
||||
}
|
||||
|
||||
pub(crate) struct EventLoop {
|
||||
swarm: Swarm<ComposedBehaviour>,
|
||||
swarm: Swarm<Behaviour>,
|
||||
command_receiver: mpsc::Receiver<Command>,
|
||||
event_sender: mpsc::Sender<Event>,
|
||||
pending_dial: HashMap<PeerId, oneshot::Sender<Result<(), Box<dyn Error + Send>>>>,
|
||||
@ -183,7 +183,7 @@ pub(crate) struct EventLoop {
|
||||
|
||||
impl EventLoop {
|
||||
fn new(
|
||||
swarm: Swarm<ComposedBehaviour>,
|
||||
swarm: Swarm<Behaviour>,
|
||||
command_receiver: mpsc::Receiver<Command>,
|
||||
event_sender: mpsc::Sender<Event>,
|
||||
) -> Self {
|
||||
@ -213,10 +213,10 @@ impl EventLoop {
|
||||
|
||||
async fn handle_event(
|
||||
&mut self,
|
||||
event: SwarmEvent<ComposedEvent, Either<void::Void, io::Error>>,
|
||||
event: SwarmEvent<BehaviourEvent, Either<void::Void, io::Error>>,
|
||||
) {
|
||||
match event {
|
||||
SwarmEvent::Behaviour(ComposedEvent::Kademlia(
|
||||
SwarmEvent::Behaviour(BehaviourEvent::Kademlia(
|
||||
kad::Event::OutboundQueryProgressed {
|
||||
id,
|
||||
result: kad::QueryResult::StartProviding(_),
|
||||
@ -229,7 +229,7 @@ impl EventLoop {
|
||||
.expect("Completed query to be previously pending.");
|
||||
let _ = sender.send(());
|
||||
}
|
||||
SwarmEvent::Behaviour(ComposedEvent::Kademlia(
|
||||
SwarmEvent::Behaviour(BehaviourEvent::Kademlia(
|
||||
kad::Event::OutboundQueryProgressed {
|
||||
id,
|
||||
result:
|
||||
@ -252,7 +252,7 @@ impl EventLoop {
|
||||
.finish();
|
||||
}
|
||||
}
|
||||
SwarmEvent::Behaviour(ComposedEvent::Kademlia(
|
||||
SwarmEvent::Behaviour(BehaviourEvent::Kademlia(
|
||||
kad::Event::OutboundQueryProgressed {
|
||||
result:
|
||||
kad::QueryResult::GetProviders(Ok(
|
||||
@ -261,8 +261,8 @@ impl EventLoop {
|
||||
..
|
||||
},
|
||||
)) => {}
|
||||
SwarmEvent::Behaviour(ComposedEvent::Kademlia(_)) => {}
|
||||
SwarmEvent::Behaviour(ComposedEvent::RequestResponse(
|
||||
SwarmEvent::Behaviour(BehaviourEvent::Kademlia(_)) => {}
|
||||
SwarmEvent::Behaviour(BehaviourEvent::RequestResponse(
|
||||
request_response::Event::Message { message, .. },
|
||||
)) => match message {
|
||||
request_response::Message::Request {
|
||||
@ -287,7 +287,7 @@ impl EventLoop {
|
||||
.send(Ok(response.0));
|
||||
}
|
||||
},
|
||||
SwarmEvent::Behaviour(ComposedEvent::RequestResponse(
|
||||
SwarmEvent::Behaviour(BehaviourEvent::RequestResponse(
|
||||
request_response::Event::OutboundFailure {
|
||||
request_id, error, ..
|
||||
},
|
||||
@ -298,7 +298,7 @@ impl EventLoop {
|
||||
.expect("Request to still be pending.")
|
||||
.send(Err(Box::new(error)));
|
||||
}
|
||||
SwarmEvent::Behaviour(ComposedEvent::RequestResponse(
|
||||
SwarmEvent::Behaviour(BehaviourEvent::RequestResponse(
|
||||
request_response::Event::ResponseSent { .. },
|
||||
)) => {}
|
||||
SwarmEvent::NewListenAddr { address, .. } => {
|
||||
@ -406,30 +406,11 @@ impl EventLoop {
|
||||
}
|
||||
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(to_swarm = "ComposedEvent")]
|
||||
struct ComposedBehaviour {
|
||||
struct Behaviour {
|
||||
request_response: request_response::cbor::Behaviour<FileRequest, FileResponse>,
|
||||
kademlia: kad::Behaviour<kad::record::store::MemoryStore>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum ComposedEvent {
|
||||
RequestResponse(request_response::Event<FileRequest, FileResponse>),
|
||||
Kademlia(kad::Event),
|
||||
}
|
||||
|
||||
impl From<request_response::Event<FileRequest, FileResponse>> for ComposedEvent {
|
||||
fn from(event: request_response::Event<FileRequest, FileResponse>) -> Self {
|
||||
ComposedEvent::RequestResponse(event)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<kad::Event> for ComposedEvent {
|
||||
fn from(event: kad::Event) -> Self {
|
||||
ComposedEvent::Kademlia(event)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Command {
|
||||
StartListening {
|
||||
|
@ -135,30 +135,12 @@ fn build_client() -> Swarm<Client> {
|
||||
}
|
||||
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(to_swarm = "ClientEvent", prelude = "libp2p_swarm::derive_prelude")]
|
||||
#[behaviour(prelude = "libp2p_swarm::derive_prelude")]
|
||||
struct Client {
|
||||
relay: relay::client::Behaviour,
|
||||
dcutr: dcutr::Behaviour,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum ClientEvent {
|
||||
Relay(relay::client::Event),
|
||||
Dcutr(dcutr::Event),
|
||||
}
|
||||
|
||||
impl From<relay::client::Event> for ClientEvent {
|
||||
fn from(event: relay::client::Event) -> Self {
|
||||
ClientEvent::Relay(event)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<dcutr::Event> for ClientEvent {
|
||||
fn from(event: dcutr::Event) -> Self {
|
||||
ClientEvent::Dcutr(event)
|
||||
}
|
||||
}
|
||||
|
||||
async fn wait_for_reservation(
|
||||
client: &mut Swarm<Client>,
|
||||
client_addr: Multiaddr,
|
||||
|
Reference in New Issue
Block a user