mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-12 09:31:20 +00:00
swarm/: Enable advanced dialing requests (#2317)
Enable advanced dialing requests both on `Swarm` and via `NetworkBehaviourAction`. Users can now trigger a dial with a specific set of addresses, optionally extended via `NetworkBehaviour::addresses_of_peer`. In addition the whole process is now modelled in a type safe way via the builder pattern. Example of a `NetworkBehaviour` requesting a dial to a specific peer with a set of addresses additionally extended through `NetworkBehaviour::addresses_of_peer`: ```rust NetworkBehaviourAction::Dial { opts: DialOpts::peer_id(peer_id) .condition(PeerCondition::Always) .addresses(addresses) .extend_addresses_through_behaviour() .build(), handler, } ``` Example of a user requesting a dial to an unknown peer with a single address via `Swarm`: ```rust swarm1.dial( DialOpts::unknown_peer_id() .address(addr2.clone()) .build() ) ```
This commit is contained in:
@ -29,8 +29,9 @@ use libp2p_core::connection::{ConnectedPoint, ConnectionId, ListenerId};
|
||||
use libp2p_core::multiaddr::Multiaddr;
|
||||
use libp2p_core::PeerId;
|
||||
use libp2p_swarm::{
|
||||
DialError, DialPeerCondition, IntoProtocolsHandler, NetworkBehaviour, NetworkBehaviourAction,
|
||||
NotifyHandler, PollParameters,
|
||||
dial_opts::{self, DialOpts},
|
||||
DialError, IntoProtocolsHandler, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
|
||||
PollParameters,
|
||||
};
|
||||
use std::collections::{hash_map::Entry, HashMap, HashSet, VecDeque};
|
||||
use std::task::{Context, Poll};
|
||||
@ -310,7 +311,7 @@ impl NetworkBehaviour for Relay {
|
||||
error: &DialError,
|
||||
) {
|
||||
if let DialError::DialPeerConditionFalse(
|
||||
DialPeerCondition::Disconnected | DialPeerCondition::NotDialing,
|
||||
dial_opts::PeerCondition::Disconnected | dial_opts::PeerCondition::NotDialing,
|
||||
) = error
|
||||
{
|
||||
// Return early. The dial, that this dial was canceled for, might still succeed.
|
||||
@ -483,9 +484,10 @@ impl NetworkBehaviour for Relay {
|
||||
);
|
||||
let handler = self.new_handler();
|
||||
self.outbox_to_swarm
|
||||
.push_back(NetworkBehaviourAction::DialPeer {
|
||||
peer_id: dest_id,
|
||||
condition: DialPeerCondition::NotDialing,
|
||||
.push_back(NetworkBehaviourAction::Dial {
|
||||
opts: DialOpts::peer_id(dest_id)
|
||||
.condition(dial_opts::PeerCondition::NotDialing)
|
||||
.build(),
|
||||
handler,
|
||||
});
|
||||
} else {
|
||||
@ -676,9 +678,10 @@ impl NetworkBehaviour for Relay {
|
||||
dst_peer_id,
|
||||
send_back,
|
||||
});
|
||||
return Poll::Ready(NetworkBehaviourAction::DialPeer {
|
||||
peer_id: relay_peer_id,
|
||||
condition: DialPeerCondition::Disconnected,
|
||||
return Poll::Ready(NetworkBehaviourAction::Dial {
|
||||
opts: DialOpts::peer_id(relay_peer_id)
|
||||
.condition(dial_opts::PeerCondition::Disconnected)
|
||||
.build(),
|
||||
handler: self.new_handler(),
|
||||
});
|
||||
}
|
||||
@ -743,9 +746,10 @@ impl NetworkBehaviour for Relay {
|
||||
to_listener,
|
||||
},
|
||||
);
|
||||
return Poll::Ready(NetworkBehaviourAction::DialPeer {
|
||||
peer_id: relay_peer_id,
|
||||
condition: DialPeerCondition::Disconnected,
|
||||
return Poll::Ready(NetworkBehaviourAction::Dial {
|
||||
opts: DialOpts::peer_id(relay_peer_id)
|
||||
.condition(dial_opts::PeerCondition::Disconnected)
|
||||
.build(),
|
||||
handler: self.new_handler(),
|
||||
});
|
||||
}
|
||||
|
@ -26,7 +26,7 @@
|
||||
//! ```rust
|
||||
//! # use libp2p_core::transport::memory::MemoryTransport;
|
||||
//! # use libp2p_relay::{RelayConfig, new_transport_and_behaviour};
|
||||
//! # use libp2p_swarm::Swarm;
|
||||
//! # use libp2p_swarm::{Swarm, dial_opts::DialOpts};
|
||||
//! # use libp2p_core::{identity, Multiaddr, multiaddr::Protocol, PeerId, upgrade, Transport};
|
||||
//! # use libp2p_yamux::YamuxConfig;
|
||||
//! # use libp2p_plaintext::PlainText2Config;
|
||||
@ -62,7 +62,7 @@
|
||||
//! swarm.listen_on(relay_addr).unwrap();
|
||||
//!
|
||||
//! // Dial node (5678) via relay node (1234).
|
||||
//! swarm.dial_addr(dst_addr).unwrap();
|
||||
//! swarm.dial(dst_addr).unwrap();
|
||||
//! ```
|
||||
//!
|
||||
//! ## Terminology
|
||||
|
Reference in New Issue
Block a user