Author: alanmc
Date: 2006-09-10 16:24:03 -0400 (Sun, 10 Sep 2006)
New Revision: 65210

Added:
   branches/BitSharp/bitsharp/src/MonoTorrent.Client/AllowedFastAlgorithm.cs
   
branches/BitSharp/bitsharp/src/MonoTorrent.Client/Tests/AllowedFastAlgorithmTest.cs
Log:
Wrote an unoptimised AllowedFastAlgorithm which calculates which pieces a peer 
can "fast request". Also wrote an NUnit test to validate the algorithm.

Added: branches/BitSharp/bitsharp/src/MonoTorrent.Client/AllowedFastAlgorithm.cs
===================================================================
--- branches/BitSharp/bitsharp/src/MonoTorrent.Client/AllowedFastAlgorithm.cs   
2006-09-10 20:23:32 UTC (rev 65209)
+++ branches/BitSharp/bitsharp/src/MonoTorrent.Client/AllowedFastAlgorithm.cs   
2006-09-10 20:24:03 UTC (rev 65210)
@@ -0,0 +1,56 @@
+
+using System;
+using System.Collections.Generic;
+using MonoTorrent.Client.Encryption;
+using System.Net;
+using System.Security.Cryptography;
+
+namespace MonoTorrent.Client
+{
+       /// <summary>
+       /// Description of AllowedFastAlgorithm.
+       /// </summary>
+       public class AllowedFastAlgorithm
+    {
+        public const int AllowedFastPieceCount = 10;
+        private static SHA1Managed hasher = new SHA1Managed();
+
+               public static UInt32[] Calculate(byte[] addressBytes, byte[] 
infohash, int numberOfResults)
+               {
+            byte[] hashBuffer = new byte[24];   // The hash buffer to be used 
in hashing
+            uint[] results = new uint[numberOfResults];   // The results array 
which will be returned
+
+            // 1) Convert the bytes into an int32 and make them Network order
+            int ip = 
IPAddress.HostToNetworkOrder(BitConverter.ToInt32(addressBytes, 0));
+            // 2) binary AND this value with 0xFFFFFF00 to select the three 
most sigificant bytes
+            int ipMostSignificant = (int)(0xFFFFFF00 & ip);
+
+            // 3) Make ipMostSignificant into NetworkOrder
+            UInt32 ip2 = 
(UInt32)IPAddress.HostToNetworkOrder(ipMostSignificant);
+
+            // 4) Copy ip2 into the hashBuffer
+            Buffer.BlockCopy(BitConverter.GetBytes(ip2), 0, hashBuffer, 0, 4);
+
+            // 5) Copy the infohash into the hashbuffer
+            Buffer.BlockCopy(infohash, 0, hashBuffer, 4, 20);
+
+            // 6) Keep hashing and cycling until we have AllowedFastPieceCount 
number of results
+            // Then return that result
+            int j = 0;
+            while (true)
+            {
+                lock(hasher)
+                    hashBuffer = hasher.ComputeHash(hashBuffer);
+
+                for (int i = 0; i < 20; i += 4)
+                {
+                    UInt32 ttt = 
(UInt32)IPAddress.HostToNetworkOrder(BitConverter.ToInt32(hashBuffer, i));
+                    results[j++] = ttt % 1313;
+                    if (j == results.Length)
+                        return results;
+                }
+            }
+
+               }
+       }
+}


Property changes on: 
branches/BitSharp/bitsharp/src/MonoTorrent.Client/AllowedFastAlgorithm.cs
___________________________________________________________________
Name: svn:eol-style
   + native

Added: 
branches/BitSharp/bitsharp/src/MonoTorrent.Client/Tests/AllowedFastAlgorithmTest.cs
===================================================================
--- 
branches/BitSharp/bitsharp/src/MonoTorrent.Client/Tests/AllowedFastAlgorithmTest.cs
 2006-09-10 20:23:32 UTC (rev 65209)
+++ 
branches/BitSharp/bitsharp/src/MonoTorrent.Client/Tests/AllowedFastAlgorithmTest.cs
 2006-09-10 20:24:03 UTC (rev 65210)
@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using NUnit.Framework;
+using System.Net;
+
+
+namespace MonoTorrent.Client.Tests
+{
+    [TestFixture]
+    public class AllowedFastAlgorithmTest
+    {
+        [Test]
+        public void CalculateTest()
+        {
+            byte[] infohash = new byte[20];
+            IPEndPoint endpoint = new 
IPEndPoint(IPAddress.Parse("80.4.4.200"), 24142);
+
+            for (int i = 0; i < infohash.Length; i++)
+                infohash[i] = (byte)170;
+
+            UInt32[] results = 
AllowedFastAlgorithm.Calculate(endpoint.Address.GetAddressBytes(), infohash, 9);
+            Assert.AreEqual(1059, results[0]);
+            Assert.AreEqual(431, results[1]);
+            Assert.AreEqual(808, results[2]);
+            Assert.AreEqual(1217, results[3]);
+            Assert.AreEqual(287, results[4]);
+            Assert.AreEqual(376, results[5]);
+            Assert.AreEqual(1188, results[6]);
+            Assert.AreEqual(353, results[7]);
+            Assert.AreEqual(508, results[8]);
+        }
+    }
+}


Property changes on: 
branches/BitSharp/bitsharp/src/MonoTorrent.Client/Tests/AllowedFastAlgorithmTest.cs
___________________________________________________________________
Name: svn:eol-style
   + native

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

Reply via email to