diff --git a/src/AutoDialer.cs b/src/AutoDialer.cs index 579e9d0..ce433b7 100644 --- a/src/AutoDialer.cs +++ b/src/AutoDialer.cs @@ -68,6 +68,9 @@ public void Dispose() /// /// Defaults to . /// + /// + /// Setting this to zero will basically disable the auto dial features. + /// public int MinConnections { get; set; } = DefaultMinConnections; /// @@ -85,7 +88,7 @@ public void Dispose() /// void OnPeerDiscovered(object sender, Peer peer) { - if (swarm.Manager.Connections.Count() < MinConnections) + if (swarm.IsRunning && swarm.Manager.Connections.Count() < MinConnections) { Task.Run(async () => { diff --git a/src/Swarm.cs b/src/Swarm.cs index 9127037..07bb328 100644 --- a/src/Swarm.cs +++ b/src/Swarm.cs @@ -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 @@ -119,6 +116,16 @@ public Peer LocalPeer /// public INetworkProtector NetworkProtector { get; set; } + /// + /// Determines if the swarm has been started. + /// + /// + /// true if the swarm has started; otherwise, false. + /// + /// + /// + public bool IsRunning { get; private set; } + /// /// Cancellation tokens for the listeners. /// @@ -322,7 +329,8 @@ public Task StartAsync() log.Warn("Peer key is missing, using unencrypted connections."); } - log.Debug("Starting"); + IsRunning = true; + log.Debug("Started"); return Task.CompletedTask; } @@ -330,7 +338,8 @@ public Task StartAsync() /// public async Task StopAsync() { - log.Debug($"Stoping {LocalPeer}"); + IsRunning = false; + log.Debug($"Stopping {LocalPeer}"); // Stop the listeners. while (listeners.Count > 0) @@ -346,7 +355,7 @@ public async Task StopAsync() BlackList = new BlackList(); WhiteList = new WhiteList(); - log.Debug($"Stoped {LocalPeer}"); + log.Debug($"Stopped {LocalPeer}"); } diff --git a/test/SwarmTest.cs b/test/SwarmTest.cs index 7dc43fb..13f88cd 100644 --- a/test/SwarmTest.cs +++ b/test/SwarmTest.cs @@ -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() {