Author: alanmc
Date: 2007-03-16 20:49:56 -0500 (Fri, 16 Mar 2007)
New Revision: 74509

Modified:
   trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs
   trunk/bitsharp/src/MonoTorrent.Client/Logger.cs
   trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs
   trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs
   trunk/bitsharp/src/MonoTorrent.Client/Piece.cs
   trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/StandardPicker.cs
   trunk/bitsharp/src/TestClient/main.cs
Log:
Reenabled FastPeers. Bugfixed handling for RejectRequest messages so that i 
accurately keep track of how many requests each peer has. EndGamePiecePicker 
has been disabled until it can be fully tested.

Modified: trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs       2007-03-17 
00:30:10 UTC (rev 74508)
+++ trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs       2007-03-17 
01:49:56 UTC (rev 74509)
@@ -50,7 +50,7 @@
     public class ClientEngine : IDisposable
     {
         #region Global Supports
-        internal static readonly bool SupportsFastPeer = false;
+        internal static readonly bool SupportsFastPeer = true;
         internal static readonly bool SupportsEncryption = true;
         #endregion
 

Modified: trunk/bitsharp/src/MonoTorrent.Client/Logger.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Logger.cs     2007-03-17 00:30:10 UTC 
(rev 74508)
+++ trunk/bitsharp/src/MonoTorrent.Client/Logger.cs     2007-03-17 01:49:56 UTC 
(rev 74509)
@@ -18,7 +18,6 @@
         public static void Log(PeerConnectionID id, string message)
         {
             Trace.WriteLine(id.ToString() + ": " + message);
-            return;
 
             if (!log.ContainsKey(id))
                 log.Add(id, new StringBuilder(512));
@@ -33,13 +32,7 @@
 
         public static void FlushToDisk()
         {
-            Random r = new Random();
-            int number = r.Next(0, 10000);
-            if (!Directory.Exists(@"C:\logs\" + number))
-                Directory.CreateDirectory(@"C:\logs\" + number);
 
-            foreach (KeyValuePair<PeerConnectionID, StringBuilder> keypair in 
log)
-                File.WriteAllText(@"C:\logs\" + number + "\\" + 
keypair.Key.Peer.Location.GetHashCode(), keypair.Value.ToString());
         }
     }
 }

Modified: trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs 
2007-03-17 00:30:10 UTC (rev 74508)
+++ trunk/bitsharp/src/MonoTorrent.Client/Managers/ConnectionManager.cs 
2007-03-17 01:49:56 UTC (rev 74509)
@@ -473,17 +473,31 @@
                         // Attempt to decode the message from the buffer.
                         message = 
PeerwireEncoder.Decode(id.Peer.Connection.recieveBuffer, 0, 
id.Peer.Connection.BytesToRecieve, id.TorrentManager);
 
-                        if (message is RejectRequestMessage || message is 
PieceMessage || message is RequestMessage || true)
+                        int count = 0;
+                        for (int i = 0; i < 
id.TorrentManager.Peers.ConnectedPeers.Count; i++)
+                            count += 
id.TorrentManager.Peers.ConnectedPeers[i].Peer.Connection.AmRequestingPiecesCount;
+                        if (count != 
id.TorrentManager.PieceManager.CurrentRequestCount())
                         {
-                            Logger.Log("Received message: " + 
message.ToString());
-                            Logger.Log("Outstanding AmRequests: " + 
id.Peer.Connection.AmRequestingPiecesCount);
-                            Logger.Log("Outstanding IsRequests: " + 
id.Peer.Connection.IsRequestingPiecesCount);
-                            Logger.Log("PieceManager Requests:  " + 
id.TorrentManager.PieceManager.CurrentRequestCount());
-                            Logger.Log("");
+                            Logger.Log(id, 
"***************************************************");
+                            Logger.Log(id, "        Received message: " + 
message.ToString());
+                            Logger.Log(id, "        Outstanding AmRequests: " 
+ id.Peer.Connection.AmRequestingPiecesCount);
+                            Logger.Log(id, "        Outstanding IsRequests: " 
+ id.Peer.Connection.IsRequestingPiecesCount);
+                            Logger.Log(id, "        PieceManager Requests:  " 
+ id.TorrentManager.PieceManager.CurrentRequestCount());
+                            Logger.Log(id, 
"***************************************************");
+                            Logger.FlushToDisk();
                         }
 
                         message.Handle(id);
 
+                        if (message is ChokeMessage || message is 
RejectRequestMessage || message is PieceMessage || message is RequestMessage || 
true)
+                        {
+                            Logger.Log(id,"        Received message: " + 
message.ToString());
+                            Logger.Log(id, "        Outstanding AmRequests: " 
+ id.Peer.Connection.AmRequestingPiecesCount);
+                            Logger.Log(id, "        Outstanding IsRequests: " 
+ id.Peer.Connection.IsRequestingPiecesCount);
+                            Logger.Log(id, "        PieceManager Requests:  " 
+ id.TorrentManager.PieceManager.CurrentRequestCount());
+                            Logger.Log(id, "");
+                        }
+
                         Logger.Log(id, "Recieved message: " + 
message.GetType().Name);
 
 
@@ -598,15 +612,28 @@
                         if (message is PieceMessage)
                             id.Peer.Connection.IsRequestingPiecesCount--;
 
-                        if (message is RejectRequestMessage || message is 
PieceMessage || message is RequestMessage || true)
+                        int count = 0;
+                        for (int i = 0; i < 
id.TorrentManager.Peers.ConnectedPeers.Count; i++)
+                            count += 
id.TorrentManager.Peers.ConnectedPeers[i].Peer.Connection.AmRequestingPiecesCount;
+                        if (count != 
id.TorrentManager.PieceManager.CurrentRequestCount())
                         {
-                            Logger.Log("Sending message: " + 
message.ToString());
-                            // Logger.Log("Outstanding AmRequests: " + 
id.Peer.Connection.AmRequestingPiecesCount);
-                            Logger.Log("Outstanding IsRequests: " + 
id.Peer.Connection.IsRequestingPiecesCount);
-                            Logger.Log("PieceManager Requests:  " + 
id.TorrentManager.PieceManager.CurrentRequestCount());
-                            Logger.Log("");
+                            
Logger.Log("***************************************************");
+                            Logger.Log(id, "        Sending message: " + 
message.ToString());
+                            Logger.Log(id, "        Outstanding AmRequests: " 
+ id.Peer.Connection.AmRequestingPiecesCount);
+                            Logger.Log(id, "        Outstanding IsRequests: " 
+ id.Peer.Connection.IsRequestingPiecesCount);
+                            Logger.Log(id, "        PieceManager Requests:  " 
+ id.TorrentManager.PieceManager.CurrentRequestCount());
+                            
Logger.Log("***************************************************");
                         }
 
+                        if (message is ChokeMessage || message is 
RejectRequestMessage || message is PieceMessage || message is RequestMessage || 
true)
+                        {
+                            Logger.Log(id, "        Sending message: " + 
message.ToString());
+                            Logger.Log(id, "        Outstanding AmRequests: " 
+ id.Peer.Connection.AmRequestingPiecesCount);
+                            Logger.Log(id, "        Outstanding IsRequests: " 
+ id.Peer.Connection.IsRequestingPiecesCount);
+                            Logger.Log(id, "        PieceManager Requests:  " 
+ id.TorrentManager.PieceManager.CurrentRequestCount());
+                            Logger.Log(id, "");
+                        }
+
                         id.Peer.Connection.BytesSent = 0;
                         id.Peer.Connection.BytesToSend = 
message.Encode(id.Peer.Connection.sendBuffer, 0);
 

Modified: trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs      
2007-03-17 00:30:10 UTC (rev 74508)
+++ trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs      
2007-03-17 01:49:56 UTC (rev 74509)
@@ -102,8 +102,8 @@
 
         internal IPeerMessageInternal PickPiece(PeerConnectionID id, 
List<PeerConnectionID> otherPeers)
         {
-            if ((this.MyBitField.Length - this.MyBitField.TrueCount < 15) && 
this.piecePicker is StandardPicker)
-                this.piecePicker = new EndGamePicker(this.MyBitField, 
id.TorrentManager.Torrent, ((StandardPicker)this.piecePicker).Requests);
+          //  if ((this.MyBitField.Length - this.MyBitField.TrueCount < 15) && 
this.piecePicker is StandardPicker)
+          //      this.piecePicker = new EndGamePicker(this.MyBitField, 
id.TorrentManager.Torrent, ((StandardPicker)this.piecePicker).Requests);
 
             return this.piecePicker.PickPiece(id, otherPeers);
         }

Modified: trunk/bitsharp/src/MonoTorrent.Client/Piece.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Piece.cs      2007-03-17 00:30:10 UTC 
(rev 74508)
+++ trunk/bitsharp/src/MonoTorrent.Client/Piece.cs      2007-03-17 01:49:56 UTC 
(rev 74509)
@@ -179,6 +179,19 @@
         }
 
 
+        public bool NoBlocksRequested
+        {
+            get
+            {
+                for (int i = 0; i < this.blocks.Length; i++)
+                    if (this.blocks[i].Requested)
+                        return false;
+
+                return true;
+            }
+        }
+
+
         /// <summary>
         /// 
         /// </summary>

Modified: trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/StandardPicker.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/StandardPicker.cs        
2007-03-17 00:30:10 UTC (rev 74508)
+++ trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/StandardPicker.cs        
2007-03-17 01:49:56 UTC (rev 74509)
@@ -74,7 +74,7 @@
                     for (int i = 0; i < keypair.Value.Count; i++)
                         for (int j = 0; j < keypair.Value[i].Blocks.Length; 
j++)
                             if (keypair.Value[i][j].Requested && 
!keypair.Value[i][j].Received)
-                                result += 1;
+                                result++;
             }
             return result;
         }
@@ -390,6 +390,44 @@
         }
 
 
+        private void RemoveRequests(PeerConnectionID id, RequestMessage 
message)
+        {
+            bool pieceEmpty = true;
+            lock (this.requests)
+            {
+                if (this.requests.ContainsKey(id))
+                {
+                    List<Piece> pieces = this.requests[id];
+                    for (int i = 0; i < pieces.Count; i++)
+                    {
+                        if (message.PieceIndex != pieces[i].Index)
+                            continue;
+
+                        for (int j = 0; j < pieces[i].Blocks.Length; j++)
+                        {
+                            if (pieces[i][j].StartOffset != 
message.StartOffset)
+                                continue;
+
+                            if (pieces[i][j].RequestLength != 
message.RequestLength)
+                                throw new TorrentException("Trying to remove a 
request that doesn't exist");
+
+                            if (!pieces[i][j].Requested)
+                                throw new TorrentException("The block was not 
requested");
+
+                            pieces[i][j].Requested = false;
+                            id.Peer.Connection.AmRequestingPiecesCount--;
+
+                            if (pieces[i].NoBlocksRequested)
+                                pieces.RemoveAt(i);
+
+                            return;
+                        }
+                    }
+                }
+            }
+        }
+
+
         /// <summary>
         /// 
         /// </summary>
@@ -477,7 +515,42 @@
             }
         }
 
+        public void ReceivedChokeMessage(PeerConnectionID id)
+        {
+            // If he does not support fast peer, all pending requests are 
implicitly rejected
+            if (!(id.Peer.Connection.SupportsFastPeer && 
ClientEngine.SupportsFastPeer))
+            {
+                this.RemoveRequests(id);
 
+                // Remove any pending request messages from the send queue as 
there's no point in sending them
+                IPeerMessageInternal message;
+                int length = id.Peer.Connection.QueueLength;
+                for (int i = 0; i < length; i++)
+                    if ((message = id.Peer.Connection.DeQueue()) is 
RequestMessage)
+                        continue;
+                    else
+                        id.Peer.Connection.EnQueue(message);
+
+                return;
+            }
+            // If fast peer is enabled, then we do not implicitly drop all 
requests. Instead i will just not request
+            // any new pieces
+            else
+            {
+                // Remove any pending request messages from the send queue as 
there's no point in sending them
+                IPeerMessageInternal message;
+                int length = id.Peer.Connection.QueueLength;
+                for (int i = 0; i < length; i++)
+                    if ((message = id.Peer.Connection.DeQueue()) is 
RequestMessage)
+                        RemoveRequests(id, (RequestMessage)message);
+                    else
+                        id.Peer.Connection.EnQueue(message);
+
+                return;
+            }
+        }
+
+
         /// <summary>
         /// 
         /// </summary>
@@ -518,6 +591,9 @@
                 if (block == null)
                     throw new MessageException("Received reject request for a 
piece i'm not requesting");
 
+                if (!block.Requested || block.Received)
+                    throw new MessageException("We didnt request this block or 
we already received it");
+
                 block.Requested = false;
                 id.Peer.Connection.AmRequestingPiecesCount--;
             }
@@ -540,30 +616,5 @@
             return pieces;
         }
         #endregion
-
-
-        public void ReceivedChokeMessage(PeerConnectionID id)
-        {
-            // If he does not support fast peer, all pending requests are 
implicitly rejected
-            if (!(id.Peer.Connection.SupportsFastPeer && 
ClientEngine.SupportsFastPeer))
-            {
-                this.RemoveRequests(id);
-
-                // Remove any pending request messages from the send queue as 
there's no point in sending them
-                IPeerMessageInternal message;
-                int length = id.Peer.Connection.QueueLength;
-                for (int i = 0; i < length; i++)
-                    if ((message = id.Peer.Connection.DeQueue()) is 
RequestMessage)
-                        continue;
-                    else
-                        id.Peer.Connection.EnQueue(message);
-
-                return;
-            }
-            else
-            {
-                throw new NotImplementedException();
-            }
-        }
     }
 }

Modified: trunk/bitsharp/src/TestClient/main.cs
===================================================================
--- trunk/bitsharp/src/TestClient/main.cs       2007-03-17 00:30:10 UTC (rev 
74508)
+++ trunk/bitsharp/src/TestClient/main.cs       2007-03-17 01:49:56 UTC (rev 
74509)
@@ -194,7 +194,6 @@
 
         private static void shutdown()
         {
-            Logger.FlushToDisk();
             WaitHandle[] handles = engine.Stop();
             for (int i = 0; i < handles.Length; i++)
                 if (handles[i] != null)

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

Reply via email to