Skip to content

Commit

Permalink
swarm/: Format with rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
mxinden committed Aug 9, 2021
1 parent cd247a5 commit ada983a
Show file tree
Hide file tree
Showing 13 changed files with 1,245 additions and 866 deletions.
134 changes: 79 additions & 55 deletions swarm/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use crate::{AddressScore, AddressRecord};
use crate::protocols_handler::{IntoProtocolsHandler, ProtocolsHandler};
use libp2p_core::{ConnectedPoint, Multiaddr, PeerId, connection::{ConnectionId, ListenerId}};
use crate::{AddressRecord, AddressScore};
use libp2p_core::{
connection::{ConnectionId, ListenerId},
ConnectedPoint, Multiaddr, PeerId,
};
use std::{error, task::Context, task::Poll};

/// A behaviour for the network. Allows customizing the swarm.
Expand Down Expand Up @@ -90,7 +93,7 @@ pub trait NetworkBehaviour: Send + 'static {
///
/// This method is only called when the first connection to the peer is established, preceded by
/// [`inject_connection_established`](NetworkBehaviour::inject_connection_established).
fn inject_connected(&mut self, _: &PeerId) { }
fn inject_connected(&mut self, _: &PeerId) {}

/// Indicates to the behaviour that we disconnected from the node with the given peer id.
///
Expand All @@ -99,28 +102,27 @@ pub trait NetworkBehaviour: Send + 'static {
///
/// This method is only called when the last established connection to the peer is closed,
/// preceded by [`inject_connection_closed`](NetworkBehaviour::inject_connection_closed).
fn inject_disconnected(&mut self, _: &PeerId) { }
fn inject_disconnected(&mut self, _: &PeerId) {}

/// Informs the behaviour about a newly established connection to a peer.
fn inject_connection_established(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint)
{}
fn inject_connection_established(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint) {}

/// Informs the behaviour about a closed connection to a peer.
///
/// A call to this method is always paired with an earlier call to
/// `inject_connection_established` with the same peer ID, connection ID and
/// endpoint.
fn inject_connection_closed(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint)
{}
fn inject_connection_closed(&mut self, _: &PeerId, _: &ConnectionId, _: &ConnectedPoint) {}

/// Informs the behaviour that the [`ConnectedPoint`] of an existing connection has changed.
fn inject_address_change(
&mut self,
_: &PeerId,
_: &ConnectionId,
_old: &ConnectedPoint,
_new: &ConnectedPoint
) {}
_new: &ConnectedPoint,
) {
}

/// Informs the behaviour about an event generated by the handler dedicated to the peer identified by `peer_id`.
/// for the behaviour.
Expand All @@ -131,52 +133,50 @@ pub trait NetworkBehaviour: Send + 'static {
&mut self,
peer_id: PeerId,
connection: ConnectionId,
event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent
event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent,
);

/// Indicates to the behaviour that we tried to reach an address, but failed.
///
/// If we were trying to reach a specific node, its ID is passed as parameter. If this is the
/// last address to attempt for the given node, then `inject_dial_failure` is called afterwards.
fn inject_addr_reach_failure(&mut self, _peer_id: Option<&PeerId>, _addr: &Multiaddr, _error: &dyn error::Error) {
fn inject_addr_reach_failure(
&mut self,
_peer_id: Option<&PeerId>,
_addr: &Multiaddr,
_error: &dyn error::Error,
) {
}

/// Indicates to the behaviour that we tried to dial all the addresses known for a node, but
/// failed.
///
/// The `peer_id` is guaranteed to be in a disconnected state. In other words,
/// `inject_connected` has not been called, or `inject_disconnected` has been called since then.
fn inject_dial_failure(&mut self, _peer_id: &PeerId) {
}
fn inject_dial_failure(&mut self, _peer_id: &PeerId) {}

/// Indicates to the behaviour that a new listener was created.
fn inject_new_listener(&mut self, _id: ListenerId) {
}
fn inject_new_listener(&mut self, _id: ListenerId) {}

/// Indicates to the behaviour that we have started listening on a new multiaddr.
fn inject_new_listen_addr(&mut self, _id: ListenerId, _addr: &Multiaddr) {
}
fn inject_new_listen_addr(&mut self, _id: ListenerId, _addr: &Multiaddr) {}

/// Indicates to the behaviour that a multiaddr we were listening on has expired,
/// which means that we are no longer listening in it.
fn inject_expired_listen_addr(&mut self, _id: ListenerId, _addr: &Multiaddr) {
}
fn inject_expired_listen_addr(&mut self, _id: ListenerId, _addr: &Multiaddr) {}

/// A listener experienced an error.
fn inject_listener_error(&mut self, _id: ListenerId, _err: &(dyn std::error::Error + 'static)) {
}

/// A listener closed.
fn inject_listener_closed(&mut self, _id: ListenerId, _reason: Result<(), &std::io::Error>) {
}
fn inject_listener_closed(&mut self, _id: ListenerId, _reason: Result<(), &std::io::Error>) {}

/// Indicates to the behaviour that we have discovered a new external address for us.
fn inject_new_external_addr(&mut self, _addr: &Multiaddr) {
}
fn inject_new_external_addr(&mut self, _addr: &Multiaddr) {}

/// Indicates to the behaviour that an external address was removed.
fn inject_expired_external_addr(&mut self, _addr: &Multiaddr) {
}
fn inject_expired_external_addr(&mut self, _addr: &Multiaddr) {}

/// Polls for things that swarm should do.
///
Expand Down Expand Up @@ -311,47 +311,71 @@ pub enum NetworkBehaviourAction<TInEvent, TOutEvent> {
peer_id: PeerId,
/// Whether to close a specific or all connections to the given peer.
connection: CloseConnection,
}
},
}

impl<TInEvent, TOutEvent> NetworkBehaviourAction<TInEvent, TOutEvent> {
/// Map the handler event.
pub fn map_in<E>(self, f: impl FnOnce(TInEvent) -> E) -> NetworkBehaviourAction<E, TOutEvent> {
match self {
NetworkBehaviourAction::GenerateEvent(e) =>
NetworkBehaviourAction::GenerateEvent(e),
NetworkBehaviourAction::DialAddress { address } =>
NetworkBehaviourAction::DialAddress { address },
NetworkBehaviourAction::DialPeer { peer_id, condition } =>
NetworkBehaviourAction::DialPeer { peer_id, condition },
NetworkBehaviourAction::NotifyHandler { peer_id, handler, event } =>
NetworkBehaviourAction::NotifyHandler {
peer_id,
handler,
event: f(event)
},
NetworkBehaviourAction::ReportObservedAddr { address, score } =>
NetworkBehaviourAction::ReportObservedAddr { address, score },
NetworkBehaviourAction::CloseConnection { peer_id, connection } =>
NetworkBehaviourAction::CloseConnection { peer_id, connection }
NetworkBehaviourAction::GenerateEvent(e) => NetworkBehaviourAction::GenerateEvent(e),
NetworkBehaviourAction::DialAddress { address } => {
NetworkBehaviourAction::DialAddress { address }
}
NetworkBehaviourAction::DialPeer { peer_id, condition } => {
NetworkBehaviourAction::DialPeer { peer_id, condition }
}
NetworkBehaviourAction::NotifyHandler {
peer_id,
handler,
event,
} => NetworkBehaviourAction::NotifyHandler {
peer_id,
handler,
event: f(event),
},
NetworkBehaviourAction::ReportObservedAddr { address, score } => {
NetworkBehaviourAction::ReportObservedAddr { address, score }
}
NetworkBehaviourAction::CloseConnection {
peer_id,
connection,
} => NetworkBehaviourAction::CloseConnection {
peer_id,
connection,
},
}
}

/// Map the event the swarm will return.
pub fn map_out<E>(self, f: impl FnOnce(TOutEvent) -> E) -> NetworkBehaviourAction<TInEvent, E> {
match self {
NetworkBehaviourAction::GenerateEvent(e) =>
NetworkBehaviourAction::GenerateEvent(f(e)),
NetworkBehaviourAction::DialAddress { address } =>
NetworkBehaviourAction::DialAddress { address },
NetworkBehaviourAction::DialPeer { peer_id, condition } =>
NetworkBehaviourAction::DialPeer { peer_id, condition },
NetworkBehaviourAction::NotifyHandler { peer_id, handler, event } =>
NetworkBehaviourAction::NotifyHandler { peer_id, handler, event },
NetworkBehaviourAction::ReportObservedAddr { address, score } =>
NetworkBehaviourAction::ReportObservedAddr { address, score },
NetworkBehaviourAction::CloseConnection { peer_id, connection } =>
NetworkBehaviourAction::CloseConnection { peer_id, connection }
NetworkBehaviourAction::GenerateEvent(e) => NetworkBehaviourAction::GenerateEvent(f(e)),
NetworkBehaviourAction::DialAddress { address } => {
NetworkBehaviourAction::DialAddress { address }
}
NetworkBehaviourAction::DialPeer { peer_id, condition } => {
NetworkBehaviourAction::DialPeer { peer_id, condition }
}
NetworkBehaviourAction::NotifyHandler {
peer_id,
handler,
event,
} => NetworkBehaviourAction::NotifyHandler {
peer_id,
handler,
event,
},
NetworkBehaviourAction::ReportObservedAddr { address, score } => {
NetworkBehaviourAction::ReportObservedAddr { address, score }
}
NetworkBehaviourAction::CloseConnection {
peer_id,
connection,
} => NetworkBehaviourAction::CloseConnection {
peer_id,
connection,
},
}
}
}
Expand Down
Loading

0 comments on commit ada983a

Please sign in to comment.