Skip to content

Commit aedf9c0

Browse files
authored
Add more methods on Topology (#826)
* Add more methods on Topology * Add a DisconnectReason
1 parent 2aa3c94 commit aedf9c0

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

core/src/swarm.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ use crate::{
5050
raw_swarm::{RawSwarm, RawSwarmEvent}
5151
},
5252
protocols_handler::{NodeHandlerWrapper, ProtocolsHandler},
53-
topology::Topology
53+
topology::Topology,
54+
topology::DisconnectReason,
5455
};
5556
use futures::prelude::*;
5657
use smallvec::SmallVec;
@@ -268,13 +269,20 @@ where TBehaviour: NetworkBehaviour<TTopology>,
268269
self.behaviour.inject_node_event(peer_id, event);
269270
},
270271
Async::Ready(RawSwarmEvent::Connected { peer_id, endpoint }) => {
272+
self.topology.set_connected(&peer_id, &endpoint);
271273
self.behaviour.inject_connected(peer_id, endpoint);
272274
},
273-
Async::Ready(RawSwarmEvent::NodeClosed { peer_id, endpoint }) |
275+
Async::Ready(RawSwarmEvent::NodeClosed { peer_id, endpoint }) => {
276+
self.topology.set_disconnected(&peer_id, &endpoint, DisconnectReason::Graceful);
277+
self.behaviour.inject_disconnected(&peer_id, endpoint);
278+
},
274279
Async::Ready(RawSwarmEvent::NodeError { peer_id, endpoint, .. }) => {
280+
self.topology.set_disconnected(&peer_id, &endpoint, DisconnectReason::Error);
275281
self.behaviour.inject_disconnected(&peer_id, endpoint);
276282
},
277283
Async::Ready(RawSwarmEvent::Replaced { peer_id, closed_endpoint, endpoint }) => {
284+
self.topology.set_disconnected(&peer_id, &closed_endpoint, DisconnectReason::Replaced);
285+
self.topology.set_connected(&peer_id, &endpoint);
278286
self.behaviour.inject_disconnected(&peer_id, closed_endpoint);
279287
self.behaviour.inject_connected(peer_id, endpoint);
280288
},
@@ -284,8 +292,12 @@ where TBehaviour: NetworkBehaviour<TTopology>,
284292
},
285293
Async::Ready(RawSwarmEvent::ListenerClosed { .. }) => {},
286294
Async::Ready(RawSwarmEvent::IncomingConnectionError { .. }) => {},
287-
Async::Ready(RawSwarmEvent::DialError { .. }) => {},
288-
Async::Ready(RawSwarmEvent::UnknownPeerDialError { .. }) => {},
295+
Async::Ready(RawSwarmEvent::DialError { multiaddr, .. }) => {
296+
self.topology.set_unreachable(&multiaddr);
297+
},
298+
Async::Ready(RawSwarmEvent::UnknownPeerDialError { multiaddr, .. }) => {
299+
self.topology.set_unreachable(&multiaddr);
300+
},
289301
}
290302

291303
let behaviour_poll = {

core/src/topology/mod.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
//! prototyping, it shouldn't be used in an actual high-performance production software.
3333
3434
use std::collections::HashMap;
35-
use crate::{Multiaddr, PeerId, PublicKey};
35+
use crate::{swarm::ConnectedPoint, Multiaddr, PeerId, PublicKey};
3636

3737
/// Storage for the network topology.
3838
///
@@ -44,18 +44,37 @@ pub trait Topology {
4444
/// > **Note**: Keep in mind that `peer` can be the local node.
4545
fn addresses_of_peer(&mut self, peer: &PeerId) -> Vec<Multiaddr>;
4646

47+
/// Returns the `PeerId` of the local node.
48+
fn local_peer_id(&self) -> &PeerId;
49+
50+
/// Returns the public key of the local node.
51+
fn local_public_key(&self) -> &PublicKey;
52+
4753
/// Adds an address that other nodes can use to connect to our local node.
4854
///
4955
/// > **Note**: Should later be returned when calling `addresses_of_peer()` with the `PeerId`
5056
/// > of the local node.
5157
fn add_local_external_addrs<TIter>(&mut self, addrs: TIter)
5258
where TIter: Iterator<Item = Multiaddr>;
5359

54-
/// Returns the `PeerId` of the local node.
55-
fn local_peer_id(&self) -> &PeerId;
60+
/// Indicates to the topology that we have successfully connected to the given address with the
61+
/// given `PeerId`.
62+
fn set_connected(&mut self, _peer_id: &PeerId, _addr: &ConnectedPoint) {}
5663

57-
/// Returns the public key of the local node.
58-
fn local_public_key(&self) -> &PublicKey;
64+
/// Indicates to the topology that we have been disconnected from the given address with the
65+
/// given `PeerId`.
66+
fn set_disconnected(&mut self, _peer_id: &PeerId, _addr: &ConnectedPoint, _reason: DisconnectReason) {}
67+
68+
/// Indicates to the topology that we have failed to reach the given address.
69+
fn set_unreachable(&mut self, _addr: &Multiaddr) {}
70+
}
71+
72+
/// Reason why the peer has been disconnected.
73+
#[derive(Debug, Copy, Clone)]
74+
pub enum DisconnectReason {
75+
Error,
76+
Graceful,
77+
Replaced,
5978
}
6079

6180
/// Topology of the network stored in memory.

0 commit comments

Comments
 (0)