mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-13 01:51:23 +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:
@ -28,8 +28,8 @@ use cuckoofilter::{CuckooError, CuckooFilter};
|
||||
use fnv::FnvHashSet;
|
||||
use libp2p_core::{connection::ConnectionId, PeerId};
|
||||
use libp2p_swarm::{
|
||||
DialPeerCondition, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, OneShotHandler,
|
||||
PollParameters,
|
||||
dial_opts::{self, DialOpts},
|
||||
NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, OneShotHandler, PollParameters,
|
||||
};
|
||||
use log::warn;
|
||||
use smallvec::SmallVec;
|
||||
@ -107,9 +107,10 @@ impl Floodsub {
|
||||
|
||||
if self.target_peers.insert(peer_id) {
|
||||
let handler = self.new_handler();
|
||||
self.events.push_back(NetworkBehaviourAction::DialPeer {
|
||||
peer_id,
|
||||
condition: DialPeerCondition::Disconnected,
|
||||
self.events.push_back(NetworkBehaviourAction::Dial {
|
||||
opts: DialOpts::peer_id(peer_id)
|
||||
.condition(dial_opts::PeerCondition::Disconnected)
|
||||
.build(),
|
||||
handler,
|
||||
});
|
||||
}
|
||||
@ -310,9 +311,10 @@ impl NetworkBehaviour for Floodsub {
|
||||
// try to reconnect.
|
||||
if self.target_peers.contains(id) {
|
||||
let handler = self.new_handler();
|
||||
self.events.push_back(NetworkBehaviourAction::DialPeer {
|
||||
peer_id: *id,
|
||||
condition: DialPeerCondition::Disconnected,
|
||||
self.events.push_back(NetworkBehaviourAction::Dial {
|
||||
opts: DialOpts::peer_id(*id)
|
||||
.condition(dial_opts::PeerCondition::Disconnected)
|
||||
.build(),
|
||||
handler,
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user