Skip to content

Commit

Permalink
feat(Swarm): IsRunning property
Browse files Browse the repository at this point in the history
Determines if the swarm has been started.
  • Loading branch information
richardschneider committed Apr 25, 2019
1 parent 42865c2 commit 4537bee
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/AutoDialer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public void Dispose()
/// <value>
/// Defaults to <see cref="DefaultMinConnections"/>.
/// </value>
/// <remarks>
/// Setting this to zero will basically disable the auto dial features.
/// </remarks>
public int MinConnections { get; set; } = DefaultMinConnections;

/// <summary>
Expand All @@ -85,7 +88,7 @@ public void Dispose()
/// </remarks>
void OnPeerDiscovered(object sender, Peer peer)
{
if (swarm.Manager.Connections.Count() < MinConnections)
if (swarm.IsRunning && swarm.Manager.Connections.Count() < MinConnections)
{
Task.Run(async () =>
{
Expand Down
21 changes: 15 additions & 6 deletions src/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@
using System.IO;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Reflection;
using PeerTalk.Protocols;
using PeerTalk.Cryptography;
using PeerTalk.Discovery;
using PeerTalk.Routing;
using Ipfs.CoreApi;

namespace PeerTalk
Expand Down Expand Up @@ -119,6 +116,16 @@ public Peer LocalPeer
/// </summary>
public INetworkProtector NetworkProtector { get; set; }

/// <summary>
/// Determines if the swarm has been started.
/// </summary>
/// <value>
/// <b>true</b> if the swarm has started; otherwise, <b>false</b>.
/// </value>
/// <seealso cref="StartAsync"/>
/// <seealso cref="StopAsync"/>
public bool IsRunning { get; private set; }

/// <summary>
/// Cancellation tokens for the listeners.
/// </summary>
Expand Down Expand Up @@ -322,15 +329,17 @@ public Task StartAsync()
log.Warn("Peer key is missing, using unencrypted connections.");
}

log.Debug("Starting");
IsRunning = true;
log.Debug("Started");

return Task.CompletedTask;
}

/// <inheritdoc />
public async Task StopAsync()
{
log.Debug($"Stoping {LocalPeer}");
IsRunning = false;
log.Debug($"Stopping {LocalPeer}");

// Stop the listeners.
while (listeners.Count > 0)
Expand All @@ -346,7 +355,7 @@ public async Task StopAsync()
BlackList = new BlackList<MultiAddress>();
WhiteList = new WhiteList<MultiAddress>();

log.Debug($"Stoped {LocalPeer}");
log.Debug($"Stopped {LocalPeer}");
}


Expand Down
12 changes: 12 additions & 0 deletions test/SwarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,18 @@ public async Task PeerDiscovered()
Assert.AreEqual(3, peerCount);
}

[TestMethod]
public async Task IsRunning()
{
var swarm = new Swarm { LocalPeer = self };
Assert.IsFalse(swarm.IsRunning);

await swarm.StartAsync();
Assert.IsTrue(swarm.IsRunning);

await swarm.StopAsync();
Assert.IsFalse(swarm.IsRunning);
}
[TestMethod]
public async Task Connect_PrivateNetwork()
{
Expand Down

0 comments on commit 4537bee

Please sign in to comment.