Skip to content

Commit

Permalink
feat(Swarm): deregister a peer
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Aug 10, 2019
1 parent 9e40a37 commit 5b6491d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ public class Swarm : IService, IPolicy<MultiAddress>
/// </summary>
public event EventHandler<Peer> PeerDisconnected;

/// <summary>
/// Raised when a peer should no longer be used.
/// </summary>
/// <remarks>
/// This event indicates that the peer has been removed
/// from the <see cref="KnownPeers"/> and should no longer
/// be used.
/// </remarks>
public event EventHandler<Peer> PeerRemoved;

/// <summary>
/// The local peer.
/// </summary>
Expand Down Expand Up @@ -307,6 +317,30 @@ public Peer RegisterPeer(Peer peer)
return p;
}

/// <summary>
/// Deregister a peer.
/// </summary>
/// <param name="peer">
/// The peer to remove..
/// </param>
/// <remarks>
/// Remove all knowledge of the peer. The <see cref="PeerRemoved"/> event
/// is raised.
/// </remarks>
public void DeregisterPeer(Peer peer)
{
if (peer.Id == null)
{
throw new ArgumentNullException("peer.ID");
}

if (otherPeers.TryRemove(peer.Id.ToBase58(), out Peer found))
{
peer = found;
}
PeerRemoved?.Invoke(this, peer);
}

/// <summary>
/// Determines if a connection is being made to the peer.
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions test/SwarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,7 @@ public async Task IsRunning()
await swarm.StopAsync();
Assert.IsFalse(swarm.IsRunning);
}

[TestMethod]
public async Task Connect_PrivateNetwork()
{
Expand Down Expand Up @@ -1087,6 +1088,20 @@ public async Task Connect_PrivateNetwork()
await swarmB.StopAsync();
}
}

[TestMethod]
public void DeregisterPeer()
{
var swarm = new Swarm { LocalPeer = self };
swarm.RegisterPeer(other);
Assert.IsTrue(swarm.KnownPeers.Contains(other));

Peer removedPeer = null;
swarm.PeerRemoved += (s, e) => removedPeer = e;
swarm.DeregisterPeer(other);
Assert.IsFalse(swarm.KnownPeers.Contains(other));
Assert.AreEqual(other, removedPeer);
}
}

/// <summary>
Expand Down

0 comments on commit 5b6491d

Please sign in to comment.