Skip to content

Commit

Permalink
feat(swarm): rename NetworkBehaviourAction to ToSwarm
Browse files Browse the repository at this point in the history
Resolves libp2p#3123.

Pull-Request: libp2p#3658.
  • Loading branch information
thomaseizinger committed Mar 24, 2023
1 parent 27e2cb0 commit 03f9fb2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 37 deletions.
23 changes: 7 additions & 16 deletions src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use libp2p_swarm::{
ExpiredListenAddr, FromSwarm,
},
ConnectionDenied, ConnectionId, ExternalAddresses, ListenAddresses, NetworkBehaviour,
NetworkBehaviourAction, PollParameters, THandler, THandlerInEvent, THandlerOutEvent,
PollParameters, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm,
};
use std::{
collections::{HashMap, VecDeque},
Expand Down Expand Up @@ -208,9 +208,7 @@ pub struct Behaviour {

last_probe: Option<Instant>,

pending_actions: VecDeque<
NetworkBehaviourAction<<Self as NetworkBehaviour>::OutEvent, THandlerInEvent<Self>>,
>,
pending_actions: VecDeque<ToSwarm<<Self as NetworkBehaviour>::OutEvent, THandlerInEvent<Self>>>,

probe_id: ProbeId,

Expand Down Expand Up @@ -336,9 +334,7 @@ impl Behaviour {
} => {
if let Some(event) = self.as_server().on_outbound_connection(&peer, address) {
self.pending_actions
.push_back(NetworkBehaviourAction::GenerateEvent(Event::InboundProbe(
event,
)));
.push_back(ToSwarm::GenerateEvent(Event::InboundProbe(event)));
}
}
ConnectedPoint::Dialer {
Expand Down Expand Up @@ -399,9 +395,7 @@ impl Behaviour {
}));
if let Some(event) = self.as_server().on_outbound_dial_error(peer_id, error) {
self.pending_actions
.push_back(NetworkBehaviourAction::GenerateEvent(Event::InboundProbe(
event,
)));
.push_back(ToSwarm::GenerateEvent(Event::InboundProbe(event)));
}
}

Expand Down Expand Up @@ -441,7 +435,7 @@ impl NetworkBehaviour for Behaviour {
}

match self.inner.poll(cx, params) {
Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)) => {
Poll::Ready(ToSwarm::GenerateEvent(event)) => {
let actions = match event {
request_response::Event::Message {
message: request_response::Message::Response { .. },
Expand Down Expand Up @@ -474,9 +468,7 @@ impl NetworkBehaviour for Behaviour {
match self.as_client().poll_auto_probe(cx) {
Poll::Ready(event) => {
self.pending_actions
.push_back(NetworkBehaviourAction::GenerateEvent(Event::OutboundProbe(
event,
)));
.push_back(ToSwarm::GenerateEvent(Event::OutboundProbe(event)));
continue;
}
Poll::Pending => {}
Expand Down Expand Up @@ -601,8 +593,7 @@ impl NetworkBehaviour for Behaviour {
}
}

type Action =
NetworkBehaviourAction<<Behaviour as NetworkBehaviour>::OutEvent, THandlerInEvent<Behaviour>>;
type Action = ToSwarm<<Behaviour as NetworkBehaviour>::OutEvent, THandlerInEvent<Behaviour>>;

// Trait implemented for `AsClient` and `AsServer` to handle events from the inner [`request_response::Behaviour`] Protocol.
trait HandleInnerEvent {
Expand Down
21 changes: 8 additions & 13 deletions src/behaviour/as_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ use libp2p_core::Multiaddr;
use libp2p_identity::PeerId;
use libp2p_request_response::{self as request_response, OutboundFailure, RequestId};
use libp2p_swarm::{
AddressScore, ConnectionId, ExternalAddresses, ListenAddresses, NetworkBehaviourAction,
PollParameters,
AddressScore, ConnectionId, ExternalAddresses, ListenAddresses, PollParameters, ToSwarm,
};
use rand::{seq::SliceRandom, thread_rng};
use std::{
Expand Down Expand Up @@ -143,17 +142,13 @@ impl<'a> HandleInnerEvent for AsClient<'a> {

let mut actions = VecDeque::with_capacity(3);

actions.push_back(NetworkBehaviourAction::GenerateEvent(Event::OutboundProbe(
event,
)));
actions.push_back(ToSwarm::GenerateEvent(Event::OutboundProbe(event)));

if let Some(old) = self.handle_reported_status(response.result.clone().into()) {
actions.push_back(NetworkBehaviourAction::GenerateEvent(
Event::StatusChanged {
old,
new: self.nat_status.clone(),
},
));
actions.push_back(ToSwarm::GenerateEvent(Event::StatusChanged {
old,
new: self.nat_status.clone(),
}));
}

if let Ok(address) = response.result {
Expand All @@ -165,7 +160,7 @@ impl<'a> HandleInnerEvent for AsClient<'a> {
.find_map(|r| (r.addr == address).then_some(r.score))
.unwrap_or(AddressScore::Finite(0));
if let AddressScore::Finite(finite_score) = score {
actions.push_back(NetworkBehaviourAction::ReportObservedAddr {
actions.push_back(ToSwarm::ReportObservedAddr {
address,
score: AddressScore::Finite(finite_score + 1),
});
Expand All @@ -191,7 +186,7 @@ impl<'a> HandleInnerEvent for AsClient<'a> {

self.schedule_probe.reset(Duration::ZERO);

VecDeque::from([NetworkBehaviourAction::GenerateEvent(Event::OutboundProbe(
VecDeque::from([ToSwarm::GenerateEvent(Event::OutboundProbe(
OutboundProbeEvent::Error {
probe_id,
peer: Some(peer),
Expand Down
16 changes: 8 additions & 8 deletions src/behaviour/as_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use libp2p_request_response::{
};
use libp2p_swarm::{
dial_opts::{DialOpts, PeerCondition},
ConnectionId, DialError, NetworkBehaviourAction, PollParameters,
ConnectionId, DialError, PollParameters, ToSwarm,
};
use std::{
collections::{HashMap, HashSet, VecDeque},
Expand Down Expand Up @@ -124,14 +124,14 @@ impl<'a> HandleInnerEvent for AsServer<'a> {
self.throttled_clients.push((peer, Instant::now()));

VecDeque::from([
NetworkBehaviourAction::GenerateEvent(Event::InboundProbe(
ToSwarm::GenerateEvent(Event::InboundProbe(
InboundProbeEvent::Request {
probe_id,
peer,
addresses: addrs.clone(),
},
)),
NetworkBehaviourAction::Dial {
ToSwarm::Dial {
opts: DialOpts::peer_id(peer)
.condition(PeerCondition::Always)
.override_dial_concurrency_factor(
Expand All @@ -155,13 +155,13 @@ impl<'a> HandleInnerEvent for AsServer<'a> {
};
let _ = self.inner.send_response(channel, response);

VecDeque::from([NetworkBehaviourAction::GenerateEvent(
Event::InboundProbe(InboundProbeEvent::Error {
VecDeque::from([ToSwarm::GenerateEvent(Event::InboundProbe(
InboundProbeEvent::Error {
probe_id,
peer,
error: InboundProbeError::Response(error),
}),
)])
},
))])
}
}
}
Expand All @@ -183,7 +183,7 @@ impl<'a> HandleInnerEvent for AsServer<'a> {
_ => self.probe_id.next(),
};

VecDeque::from([NetworkBehaviourAction::GenerateEvent(Event::InboundProbe(
VecDeque::from([ToSwarm::GenerateEvent(Event::InboundProbe(
InboundProbeEvent::Error {
probe_id,
peer,
Expand Down

0 comments on commit 03f9fb2

Please sign in to comment.