Author: alanmc
Date: 2007-06-24 00:40:32 -0400 (Sun, 24 Jun 2007)
New Revision: 80609

Modified:
   trunk/bitsharp/src/MonoTorrent/Client/Managers/TorrentManager.cs
   trunk/bitsharp/src/MonoTorrent/Client/PeerConnections/PeerConnectionBase.cs
   trunk/bitsharp/src/MonoTorrent/Client/PeerMessages/BitfieldMessage.cs
   trunk/bitsharp/src/MonoTorrent/Client/PeerMessages/HaveMessage.cs
Log:
Simplified the logic controlling whether a peer is interesting or not

Modified: trunk/bitsharp/src/MonoTorrent/Client/Managers/TorrentManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Client/Managers/TorrentManager.cs    
2007-06-24 00:36:54 UTC (rev 80608)
+++ trunk/bitsharp/src/MonoTorrent/Client/Managers/TorrentManager.cs    
2007-06-24 04:40:32 UTC (rev 80609)
@@ -617,10 +617,7 @@
                         //if (counter % 500 == 0)
                         //    DumpStats(id, counter);
 
-                        // If the peer is interesting to me and i havent sent 
an Interested message
-                        SetAmInterestedStatus(id);
 
-
                         // If he is not interested and i am not choking him
                         if (!id.Peer.Connection.IsInterested && 
!id.Peer.Connection.AmChoking)
                             SetChokeStatus(id, true);
@@ -634,7 +631,7 @@
                             SetChokeStatus(id, true);
 
                         // If the peer is interesting, try to queue up some 
piece requests off him
-                        if(id.Peer.Connection.IsInterestingToMe)
+                        if(id.Peer.Connection.AmInterested)
                             while (this.pieceManager.AddPieceRequest(id)) { }
 
                         if (nintySecondsAgo > 
id.Peer.Connection.LastMessageSent)
@@ -763,24 +760,9 @@
             DownloadLogic(counter);
         }
 
-
         /// <summary>
         /// 
         /// </summary>
-        /// <param name="id"></param>
-        internal void SetAmInterestedStatus(PeerIdInternal id)
-        {
-            if (id.Peer.Connection.IsInterestingToMe && 
(!id.Peer.Connection.AmInterested))
-                SetAmInterestedStatus(id, true);
-
-            else if (!id.Peer.Connection.IsInterestingToMe && 
id.Peer.Connection.AmInterested)
-                SetAmInterestedStatus(id, false);
-        }
-
-
-        /// <summary>
-        /// 
-        /// </summary>
         /// <param name="counter"></param>
         internal void SuperSeedingLogic(int counter)
         {
@@ -926,8 +908,8 @@
                             bool hasPiece = 
this.peers.ConnectedPeers[i].Peer.Connection.BitField[pieceIndex];
                             if (hasPiece)
                             {
-                                
this.peers.ConnectedPeers[i].Peer.Connection.IsInterestingToMe = 
this.pieceManager.IsInteresting(this.peers.ConnectedPeers[i]);
-                                
SetAmInterestedStatus(this.peers.ConnectedPeers[i]);
+                                bool isInteresting = 
this.pieceManager.IsInteresting(this.peers.ConnectedPeers[i]);
+                                
SetAmInterestedStatus(this.peers.ConnectedPeers[i], isInteresting);
                             }
 
                             // Check to see if have supression is enabled and 
send the have message accordingly
@@ -937,22 +919,7 @@
         }
 
 
-        /// <summary>
-        /// Changes the peers "Interesting" status to the new value
-        /// </summary>
-        /// <param name="id">The peer to change the status of</param>
-        /// <param name="amInterested">True if we are interested in the peer, 
false otherwise</param>
-        private void SetAmInterestedStatus(PeerIdInternal id, bool 
amInterested)
-        {
-            // If we used to be not interested but now we are, send a message.
-            // If we used to be interested but now we're not, send a message
-            id.Peer.Connection.AmInterested = amInterested;
 
-            if (amInterested)
-                id.Peer.Connection.Enqueue(new InterestedMessage());
-            else
-                id.Peer.Connection.Enqueue(new NotInterestedMessage());
-        }
 
 
         /// <summary>
@@ -1000,5 +967,31 @@
         }
 
         #endregion Private Methods
+
+        internal void SetAmInterestedStatus(PeerIdInternal id, bool 
interesting)
+        {
+            bool enqueued = false;
+            if (interesting && !id.Peer.Connection.AmInterested)
+            {
+                id.Peer.Connection.AmInterested = true;
+                id.Peer.Connection.Enqueue(new InterestedMessage());
+
+                // He's interesting, so attempt to queue up any FastPieces (if 
that's possible)
+                while (id.TorrentManager.pieceManager.AddPieceRequest(id)) { }
+                enqueued = true;
+            }
+            else if (!interesting && id.Peer.Connection.AmInterested)
+            {
+                id.Peer.Connection.AmInterested = false;
+                id.Peer.Connection.Enqueue(new NotInterestedMessage());
+                enqueued = true;
+            }
+
+            if (enqueued && !id.Peer.Connection.ProcessingQueue)
+            {
+                id.Peer.Connection.ProcessingQueue = true;
+                id.ConnectionManager.MessageHandler.EnqueueSend(id);
+            }
+        }
     }
 }

Modified: 
trunk/bitsharp/src/MonoTorrent/Client/PeerConnections/PeerConnectionBase.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Client/PeerConnections/PeerConnectionBase.cs 
2007-06-24 00:36:54 UTC (rev 80608)
+++ trunk/bitsharp/src/MonoTorrent/Client/PeerConnections/PeerConnectionBase.cs 
2007-06-24 04:40:32 UTC (rev 80609)
@@ -236,17 +236,6 @@
 
 
         /// <summary>
-        /// True if the peer has pieces that i might like to request. If he is 
interesting to me
-        /// i need to send an InterestedMessage.
-        /// </summary>
-        internal bool IsInterestingToMe
-        {
-            get { return this.isinterestingtoMe; }
-            set { this.isinterestingtoMe = value; }
-        }
-
-
-        /// <summary>
         /// The number of pieces the peer has requested off me
         /// </summary>
         public int IsRequestingPiecesCount

Modified: trunk/bitsharp/src/MonoTorrent/Client/PeerMessages/BitfieldMessage.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Client/PeerMessages/BitfieldMessage.cs       
2007-06-24 00:36:54 UTC (rev 80608)
+++ trunk/bitsharp/src/MonoTorrent/Client/PeerMessages/BitfieldMessage.cs       
2007-06-24 04:40:32 UTC (rev 80609)
@@ -114,8 +114,7 @@
             id.Peer.Connection.BitField = this.bitField;
             id.Peer.IsSeeder = (id.Peer.Connection.BitField.AllTrue);
 
-            id.Peer.Connection.IsInterestingToMe = 
id.TorrentManager.PieceManager.IsInteresting(id);
-            id.TorrentManager.SetAmInterestedStatus(id);
+            id.TorrentManager.SetAmInterestedStatus(id, 
id.TorrentManager.PieceManager.IsInteresting(id));
         }
 
 

Modified: trunk/bitsharp/src/MonoTorrent/Client/PeerMessages/HaveMessage.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Client/PeerMessages/HaveMessage.cs   
2007-06-24 00:36:54 UTC (rev 80608)
+++ trunk/bitsharp/src/MonoTorrent/Client/PeerMessages/HaveMessage.cs   
2007-06-24 04:40:32 UTC (rev 80609)
@@ -118,10 +118,7 @@
             // We can do a fast check to see if the peer is interesting or not 
when we receive a Have Message.
             // If the peer just received a piece we don't have, he's 
interesting. Otherwise his state is unchanged
             if (!id.TorrentManager.Bitfield[this.pieceIndex])
-            {
-                id.Peer.Connection.IsInterestingToMe = true;
-                id.TorrentManager.SetAmInterestedStatus(id);
-            }
+                id.TorrentManager.SetAmInterestedStatus(id, true);
 
             // Fastcheck to see if a peer is a seeder or not
             id.Peer.IsSeeder = (id.Peer.Connection.BitField.AllTrue);

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to