Skip to content

Commit

Permalink
swarm/src/behaviour: Provide handler with Dial and DialAddr
Browse files Browse the repository at this point in the history
  • Loading branch information
mxinden committed Aug 12, 2021
1 parent 46904a6 commit d756be1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 29 deletions.
29 changes: 16 additions & 13 deletions swarm/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub trait NetworkBehaviour: Send + 'static {
/// This API mimics the API of the `Stream` trait. The method may register the current task in
/// order to wake it up at a later point in time.
fn poll(&mut self, cx: &mut Context<'_>, params: &mut impl PollParameters)
-> Poll<NetworkBehaviourAction<<<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent, Self::OutEvent>>;
-> Poll<NetworkBehaviourAction<<<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent, Self::OutEvent, Self::ProtocolsHandler>>;
}

/// Parameters passed to `poll()`, that the `NetworkBehaviour` has access to.
Expand Down Expand Up @@ -229,14 +229,16 @@ pub trait NetworkBehaviourEventProcess<TEvent> {
///
/// [`Swarm`]: super::Swarm
#[derive(Debug)]
pub enum NetworkBehaviourAction<TInEvent, TOutEvent> {
// TODO: Derive TInEvent from THandler.
pub enum NetworkBehaviourAction<TInEvent, TOutEvent, THandler> {
/// Instructs the `Swarm` to return an event when it is being polled.
GenerateEvent(TOutEvent),

/// Instructs the swarm to dial the given multiaddress optionally including a [`PeerId`].
DialAddress {
/// The address to dial.
address: Multiaddr,
handler: THandler,
},

/// Instructs the swarm to dial a known `PeerId`.
Expand All @@ -254,6 +256,7 @@ pub enum NetworkBehaviourAction<TInEvent, TOutEvent> {
peer_id: PeerId,
/// The condition for initiating a new dialing attempt.
condition: DialPeerCondition,
handler: THandler,
},

/// Instructs the `Swarm` to send an event to the handler dedicated to a
Expand Down Expand Up @@ -314,16 +317,16 @@ pub enum NetworkBehaviourAction<TInEvent, TOutEvent> {
},
}

impl<TInEvent, TOutEvent> NetworkBehaviourAction<TInEvent, TOutEvent> {
impl<TInEvent, TOutEvent, THandler> NetworkBehaviourAction<TInEvent, TOutEvent, THandler> {
/// Map the handler event.
pub fn map_in<E>(self, f: impl FnOnce(TInEvent) -> E) -> NetworkBehaviourAction<E, TOutEvent> {
pub fn map_in<E>(self, f: impl FnOnce(TInEvent) -> E) -> NetworkBehaviourAction<E, TOutEvent, THandler> {
match self {
NetworkBehaviourAction::GenerateEvent(e) => NetworkBehaviourAction::GenerateEvent(e),
NetworkBehaviourAction::DialAddress { address } => {
NetworkBehaviourAction::DialAddress { address }
NetworkBehaviourAction::DialAddress { address, handler } => {
NetworkBehaviourAction::DialAddress { address, handler }
}
NetworkBehaviourAction::DialPeer { peer_id, condition } => {
NetworkBehaviourAction::DialPeer { peer_id, condition }
NetworkBehaviourAction::DialPeer { peer_id, condition, handler } => {
NetworkBehaviourAction::DialPeer { peer_id, condition, handler }
}
NetworkBehaviourAction::NotifyHandler {
peer_id,
Expand All @@ -348,14 +351,14 @@ impl<TInEvent, TOutEvent> NetworkBehaviourAction<TInEvent, TOutEvent> {
}

/// Map the event the swarm will return.
pub fn map_out<E>(self, f: impl FnOnce(TOutEvent) -> E) -> NetworkBehaviourAction<TInEvent, E> {
pub fn map_out<E>(self, f: impl FnOnce(TOutEvent) -> E) -> NetworkBehaviourAction<TInEvent, E, THandler> {
match self {
NetworkBehaviourAction::GenerateEvent(e) => NetworkBehaviourAction::GenerateEvent(f(e)),
NetworkBehaviourAction::DialAddress { address } => {
NetworkBehaviourAction::DialAddress { address }
NetworkBehaviourAction::DialAddress { address, handler } => {
NetworkBehaviourAction::DialAddress { address, handler }
}
NetworkBehaviourAction::DialPeer { peer_id, condition } => {
NetworkBehaviourAction::DialPeer { peer_id, condition }
NetworkBehaviourAction::DialPeer { peer_id, condition, handler } => {
NetworkBehaviourAction::DialPeer { peer_id, condition, handler }
}
NetworkBehaviourAction::NotifyHandler {
peer_id,
Expand Down
45 changes: 31 additions & 14 deletions swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,16 +331,32 @@ where

/// Initiates a new dialing attempt to the given address.
pub fn dial_addr(&mut self, addr: Multiaddr) -> Result<(), DialError> {
let handler = self
.behaviour
.new_handler()
let handler = self.behaviour.new_handler();
self.dial_addr_with_handler(addr, handler)
}

fn dial_addr_with_handler(
&mut self,
addr: Multiaddr,
handler: <TBehaviour as NetworkBehaviour>::ProtocolsHandler,
) -> Result<(), DialError> {
let handler = handler
.into_node_handler_builder()
.with_substream_upgrade_protocol_override(self.substream_upgrade_protocol_override);
Ok(self.network.dial(&addr, handler).map(|_id| ())?)
}

/// Initiates a new dialing attempt to the given peer.
pub fn dial(&mut self, peer_id: &PeerId) -> Result<(), DialError> {
let handler = self.behaviour.new_handler();
self.dial_with_handler(peer_id, handler)
}

fn dial_with_handler(
&mut self,
peer_id: &PeerId,
handler: <TBehaviour as NetworkBehaviour>::ProtocolsHandler,
) -> Result<(), DialError> {
if self.banned_peers.contains(peer_id) {
// TODO: Needed?
// self.behaviour.inject_dial_failure(peer_id);
Expand All @@ -355,9 +371,7 @@ where
.filter(|a| !self_listening.contains(a));

let result = if let Some(first) = addrs.next() {
let handler = self
.behaviour
.new_handler()
let handler = handler
.into_node_handler_builder()
.with_substream_upgrade_protocol_override(self.substream_upgrade_protocol_override);
self.network
Expand Down Expand Up @@ -774,15 +788,16 @@ where
Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)) => {
return Poll::Ready(SwarmEvent::Behaviour(event))
}
Poll::Ready(NetworkBehaviourAction::DialAddress { address }) => {
let _ = Swarm::dial_addr(&mut *this, address);
Poll::Ready(NetworkBehaviourAction::DialAddress { address, handler }) => {
let _ = Swarm::dial_addr_with_handler(&mut *this, address, handler);
}
Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id, condition }) => {
Poll::Ready(NetworkBehaviourAction::DialPeer {
peer_id,
condition,
handler,
}) => {
if this.banned_peers.contains(&peer_id) {
this.behaviour.inject_dial_failure(
&peer_id,
todo!("Have DialPeer contain handler which can then be returned."),
);
this.behaviour.inject_dial_failure(&peer_id, handler);
} else {
let condition_matched = match condition {
DialPeerCondition::Disconnected => {
Expand All @@ -792,7 +807,7 @@ where
DialPeerCondition::Always => true,
};
if condition_matched {
if Swarm::dial(this, &peer_id).is_ok() {
if Swarm::dial_with_handler(this, &peer_id, handler).is_ok() {
return Poll::Ready(SwarmEvent::Dialing(peer_id));
}
} else {
Expand All @@ -814,6 +829,7 @@ where
}
}
}
// TODO: Return the handler to the behaviour.
}
}
}
Expand Down Expand Up @@ -1261,6 +1277,7 @@ impl NetworkBehaviour for DummyBehaviour {
NetworkBehaviourAction<
<Self::ProtocolsHandler as ProtocolsHandler>::InEvent,
Self::OutEvent,
Self::ProtocolsHandler,
>,
> {
Poll::Pending
Expand Down
5 changes: 3 additions & 2 deletions swarm/src/toggle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,11 @@ where
}

fn poll(&mut self, cx: &mut Context<'_>, params: &mut impl PollParameters)
-> Poll<NetworkBehaviourAction<<<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent, Self::OutEvent>>
-> Poll<NetworkBehaviourAction<<<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent, Self::OutEvent, Self::ProtocolsHandler>>
{
if let Some(inner) = self.inner.as_mut() {
inner.poll(cx, params)
// inner.poll(cx, params)
todo!("Wrap handler in Dial events with ToggleHandler.");
} else {
Poll::Pending
}
Expand Down

0 comments on commit d756be1

Please sign in to comment.