Author: alanmc
Date: 2008-02-16 19:31:09 -0500 (Sat, 16 Feb 2008)
New Revision: 95941

Modified:
   trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/SimpleTorrentManager.cs
   trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/Tracker.cs
Log:
Implemented proper recording of Complete and Incomplete peers and implemented 
removal of zombie peers.

Modified: 
trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/SimpleTorrentManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/SimpleTorrentManager.cs  
2008-02-17 00:14:05 UTC (rev 95940)
+++ trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/SimpleTorrentManager.cs  
2008-02-17 00:31:09 UTC (rev 95941)
@@ -163,8 +163,7 @@
             Debug.WriteLine(string.Format("Adding: {0}", peer.ClientAddress));
             peers.Add(peer.ClientAddress.Address, peer);
 
-            if (peer.HasCompleted)
-                System.Threading.Interlocked.Increment(ref complete.number);
+            UpdateCounts();
         }
 
         /// <summary>
@@ -214,7 +213,16 @@
                 response.Add(Tracker.peers, nonCompactResponse);
         }
 
+        public void ClearZombiePeers(DateTime cutoff)
+        {
+            IPAddress[] keys = new IPAddress[peers.Keys.Count];
+            peers.Keys.CopyTo(keys, 0);
+            foreach (IPAddress ip in keys)
+                if (peers[ip].LastAnnounceTime < cutoff)
+                    peers.Remove(ip);
+        }
 
+
         /// <summary>
         /// Removes the peer from the tracker
         /// </summary>
@@ -227,10 +235,26 @@
             Debug.WriteLine(string.Format("Removing: {0}", 
peer.ClientAddress));
             peers.Remove(peer.ClientAddress.Address);
 
-            if (peer.HasCompleted)
-                System.Threading.Interlocked.Decrement(ref complete.number);
+            UpdateCounts();
         }
 
+        private void UpdateCounts()
+        {
+            int complete = 0;
+            int incomplete = 0;
+
+            foreach (Peer p in peers.Values)
+            {
+                if (p.HasCompleted)
+                    complete++;
+                else
+                    incomplete++;
+            }
+
+            this.complete.number = complete;
+            this.incomplete.number = incomplete;
+        }
+
         /// <summary>
         /// Updates the peer in the tracker database based on the announce 
parameters
         /// </summary>
@@ -245,25 +269,15 @@
             }
 
             Debug.WriteLine(string.Format("Updating: {0}", 
peer.ClientAddress));
-            switch (par.Event)
-            {
-                case TorrentEvent.Completed:
-                    System.Threading.Interlocked.Increment(ref 
complete.number);
-                    System.Threading.Interlocked.Increment(ref 
downloaded.number);
-                    break;
+            peer.Update(par);
 
-                case TorrentEvent.Stopped:
-                    Remove(peer);
-                    break;
+            if (par.Event == TorrentEvent.Completed)
+                System.Threading.Interlocked.Increment(ref downloaded.number);
 
-                case TorrentEvent.None:
-                case TorrentEvent.Started:
-                    // Do nothing
-                    break;
+            else if (par.Event == TorrentEvent.Stopped)
+                Remove(peer);
 
-            }
-
-            peer.Update(par);
+            UpdateCounts();
         }
 
         #endregion Methods

Modified: trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/Tracker.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/Tracker.cs       
2008-02-17 00:14:05 UTC (rev 95940)
+++ trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/Tracker.cs       
2008-02-17 00:31:09 UTC (rev 95941)
@@ -180,6 +180,9 @@
                 // updates it's information or removes it depending on the 
context
                 manager.Update(e);
 
+                // Clear any peers who haven't announced within the allowed 
timespan and may be inactive
+                
manager.ClearZombiePeers(DateTime.Now.AddSeconds(-this.IntervalAlgorithm.PeerTimeout));
+
                 // Fulfill the announce request
                 manager.GetPeers(e.Response, e.NumberWanted, 
e.HasRequestedCompact, e.ClientAddress);
             }

_______________________________________________
Mono-patches maillist  -  Mono-patches@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to