Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request-Response: Add method connected_peers #2354

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion protocols/request-response/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,11 @@ where
}
}

/// List of currently connected peers.
pub fn connected_peers(&self) -> Vec<&PeerId> {
self.connected.keys().collect()
}

/// Checks whether an outbound request to the peer with the provided
/// [`PeerId`] initiated by [`RequestResponse::send_request`] is still
/// pending, i.e. waiting for a response.
Expand Down Expand Up @@ -593,6 +598,29 @@ where
addresses
}

fn inject_address_change(
&mut self,
peer: &PeerId,
conn: &ConnectionId,
_old: &ConnectedPoint,
new: &ConnectedPoint,
) {
let new_address = match new {
ConnectedPoint::Dialer { address } => Some(address.clone()),
ConnectedPoint::Listener { .. } => None,
};
let connections = self
.connected
.get_mut(peer)
.expect("Address change can only happen on an established connection.");

let connection = connections
.iter_mut()
.find(|c| &c.id == conn)
.expect("Address change can only happen on an established connection.");
connection.address = new_address;
}

fn inject_connected(&mut self, peer: &PeerId) {
if let Some(pending) = self.pending_outbound_requests.remove(peer) {
for request in pending {
Expand Down Expand Up @@ -686,7 +714,7 @@ where
self.pending_events
.push_back(NetworkBehaviourAction::GenerateEvent(
RequestResponseEvent::OutboundFailure {
peer: peer,
peer,
request_id: request.request_id,
error: OutboundFailure::DialFailure,
},
Expand Down
10 changes: 10 additions & 0 deletions protocols/request-response/tests/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ fn ping_protocol() {
assert_eq!(&peer, &peer2_id);
}
SwarmEvent::Behaviour(e) => panic!("Peer1: Unexpected event: {:?}", e),
SwarmEvent::ConnectionEstablished { peer_id, .. } => {
assert_eq!(peer_id, peer2_id);
assert!(swarm1.behaviour().is_connected(&peer_id));
assert_eq!(swarm1.behaviour().connected_peers(), vec![&peer_id]);
}
_ => {}
}
}
Expand Down Expand Up @@ -157,6 +162,11 @@ fn ping_protocol() {
}
}
SwarmEvent::Behaviour(e) => panic!("Peer2: Unexpected event: {:?}", e),
SwarmEvent::ConnectionEstablished { peer_id, .. } => {
assert_eq!(peer_id, peer1_id);
assert!(swarm2.behaviour().is_connected(&peer_id));
assert_eq!(swarm2.behaviour().connected_peers(), vec![&peer_id]);
}
_ => {}
}
}
Expand Down