DistanceTo() public method

public DistanceTo ( AHAddress a ) : BigInteger
a AHAddress
return Brunet.Util.BigInteger
示例#1
0
            public int Compare(Connection x, Connection y)
            {
                AHAddress xaddr = x.Address as AHAddress;
                AHAddress yaddr = y.Address as AHAddress;

                if (xaddr == null || y == null)
                {
                    throw new Exception("Invalid comparison");
                }

                BigInteger xdist = _local.DistanceTo(xaddr).abs();
                BigInteger ydist = _local.DistanceTo(yaddr).abs();

                if (xdist < ydist)
                {
                    return(-1);
                }
                else if (ydist < xdist)
                {
                    return(1);
                }
                else
                {
                    return(0);
                }
            }
        /// Determine if there are any unuseful STRUC_NEAR that we can trim
        protected void TrimConnections()
        {
            ConnectionTable tab             = _node.ConnectionTable;
            ConnectionList  cons            = tab.GetConnections(ConnectionType.Structured);
            ArrayList       trim_candidates = new ArrayList();

            foreach (Connection c in cons)
            {
                if (!c.ConType.Equals(STRUC_NEAR))
                {
                    continue;
                }

                int left_pos  = cons.LeftInclusiveCount(_node.Address, c.Address);
                int right_pos = cons.RightInclusiveCount(_node.Address, c.Address);

                if (right_pos >= 2 * DESIRED_NEIGHBORS && left_pos >= 2 * DESIRED_NEIGHBORS)
                {
                    //These are near neighbors that are not so near
                    trim_candidates.Add(c);
                }
            }

            if (trim_candidates.Count == 0)
            {
                return;
            }

            //Delete a farthest trim candidate:
            BigInteger biggest_distance = new BigInteger(0);
            BigInteger tmp_distance     = new BigInteger(0);
            Connection to_trim          = null;

            foreach (Connection tc in trim_candidates)
            {
                AHAddress t_ah_add = (AHAddress)tc.Address;
                tmp_distance = t_ah_add.DistanceTo((AHAddress)_node.Address).abs();
                if (tmp_distance > biggest_distance)
                {
                    biggest_distance = tmp_distance;
                    //Console.Error.WriteLine("...finding far distance for trim: {0}",biggest_distance.ToString() );
                    to_trim = tc;
                }
            }

#if POB_DEBUG
            Console.Error.WriteLine("Attempt to trim Near: {0}", to_trim);
#endif
            _node.GracefullyClose(to_trim.Edge, "SCO, near connection trim");
        }
示例#3
0
        public void Test()
        {
            TransportAddress ta =
                TransportAddressFactory.CreateInstance("brunet.tcp://169.0.5.1:5000");
            FakeEdge fe   = new FakeEdge(ta, ta);
            var      rng  = new System.Security.Cryptography.RNGCryptoServiceProvider();
            var      cons = new List <Connection>();

            for (int i = 0; i < 50; i++)
            {
                var addr = new AHAddress(rng);
                cons.Add(new Connection(fe, addr, "structured", null, null));
            }

            var start_addr = new AHAddress(rng);
            IComparer <Connection> distance_comparer =
                new BroadcastSender.AbsoluteDistanceComparer(start_addr);

            cons.Sort(distance_comparer);
            BigInteger current_distance = new BigInteger(0);

            foreach (Connection con in cons)
            {
                AHAddress  addr          = con.Address as AHAddress;
                BigInteger next_distance = start_addr.DistanceTo(addr).abs();
                Assert.IsTrue(current_distance < next_distance, "DistanceComparer");
                current_distance = next_distance;
            }

            IComparer <Connection> address_comparer =
                new BroadcastSender.LeftDistanceComparer(start_addr);

            cons.Sort(address_comparer);
            current_distance = new BigInteger(0);
            foreach (Connection con in cons)
            {
                AHAddress  addr          = con.Address as AHAddress;
                BigInteger next_distance = start_addr.LeftDistanceTo(addr).abs();
                Assert.IsTrue(current_distance < next_distance, "AddressComparer");
                current_distance = next_distance;
            }
        }
示例#4
0
    public void Test() {
      byte[]  buf1 = new byte[20];
      for (int i = 0; i <= 18; i++)
      {
        buf1[i] = 0x00;
      }
      buf1[19] = 0x0A;
      AHAddress test_address_1 = new AHAddress( MemBlock.Reference(buf1, 0, buf1.Length) );

      byte[] buf2 = new byte[20];
      for (int i = 0; i <= 18; i++) {
        buf2[i] = 0xFF;
      }
      buf2[19] = 0xFE;
      AHAddress test_address_2 = new AHAddress( MemBlock.Reference(buf2, 0, buf2.Length) );
      //test_address_1 is to the left of test_address_2
      //because it only a few steps in the clockwise direction:
      Assert.IsTrue( test_address_1.IsLeftOf( test_address_2 ), "IsLeftOf");
      Assert.IsTrue( test_address_2.IsRightOf( test_address_1 ), "IsRightOf");
      //This distance is twelve:
      Assert.AreEqual( test_address_2.DistanceTo( test_address_1),
                       new BigInteger(12), "DistanceTo");
      Assert.IsTrue( test_address_1.CompareTo(test_address_2) < 0, "CompareTo");
      Assert.IsTrue( test_address_2.CompareTo(test_address_1) > 0, "CompareTo");
      byte[] buf3 = new byte[Address.MemSize];
      test_address_2.CopyTo(buf3);
      AHAddress test3 = new AHAddress(MemBlock.Reference(buf3,0,buf3.Length)); 
      Assert.IsTrue( test3.CompareTo( test_address_2 ) == 0 , "CompareTo");
      Assert.IsTrue( test3.CompareTo( test3 ) == 0, "CompareTo");
      //As long as the address does not wrap around, adding should increase it:
      AHAddress a4 = new AHAddress( test_address_1.ToBigInteger() + 100 );
      Assert.IsTrue( a4.CompareTo( test_address_1 ) > 0, "adding increases");
      Assert.IsTrue( a4.CompareTo( test_address_2 ) < 0, "smaller than biggest");
      //Here are some consistency tests:
      for( int i = 0; i < 1000; i++) {
        System.Random r = new Random();
        byte[] b1 = new byte[Address.MemSize];
        r.NextBytes(b1);
        //Make sure it is class 0:
        Address.SetClass(b1, 0);
        byte[] b2 = new byte[Address.MemSize];
        r.NextBytes(b2);
        //Make sure it is class 0:
        Address.SetClass(b2, 0);
        byte[] b3 = new byte[Address.MemSize];
        r.NextBytes(b3);
        //Make sure it is class 0:
        Address.SetClass(b3, 0);
        AHAddress a5 = new AHAddress( MemBlock.Reference(b1,0,b1.Length) );
        AHAddress a6 = new AHAddress( MemBlock.Reference(b2,0,b2.Length) );
        AHAddress a7 = new AHAddress( MemBlock.Reference(b3,0,b3.Length) );
        Assert.IsTrue( a5.CompareTo(a6) == -1 * a6.CompareTo(a5), "consistency");
        //Nothing is between the same address:
        Assert.IsFalse( a5.IsBetweenFromLeft(a6, a6), "Empty Between Left");
        Assert.IsFalse( a5.IsBetweenFromRight(a7, a7), "Empty Between Right");
        //Endpoints are not between:
        Assert.IsFalse( a6.IsBetweenFromLeft(a6, a7), "End point Between Left");
        Assert.IsFalse( a6.IsBetweenFromRight(a6, a7), "End point Between Right");
        Assert.IsFalse( a7.IsBetweenFromLeft(a6, a7), "End point Between Left");
        Assert.IsFalse( a7.IsBetweenFromRight(a6, a7), "End point Between Right");

        if ( a5.IsBetweenFromLeft(a6, a7) ) {
          //Then the following must be true:
          Assert.IsTrue( a6.LeftDistanceTo(a5) < a6.LeftDistanceTo(a7),
                         "BetweenLeft true");
        }
        else {
          //Then the following must be false:
          Assert.IsFalse( a6.LeftDistanceTo(a5) < a6.LeftDistanceTo(a7),
                          "BetweenLeft false");
        }
        if ( a5.IsBetweenFromRight(a6, a7) ) {
          //Then the following must be true:
          Assert.IsTrue( a6.RightDistanceTo(a5) < a6.RightDistanceTo(a7),
                         "BetweenRight true");
        }
        else {
          //Then the following must be false:
          Assert.IsFalse( a6.RightDistanceTo(a5) < a6.RightDistanceTo(a7),
                          "BetweenRight false");
        }
        if( a5.IsCloserToFirst(a6, a7) ) {
          Assert.IsTrue( a5.DistanceTo(a6).abs() < a5.DistanceTo(a7).abs(), "IsCloser 1");
        }
        else {
          Assert.IsFalse( a5.DistanceTo(a6).abs() < a5.DistanceTo(a7).abs(), "IsCloser 2");
        }
        Assert.IsFalse(a5.IsCloserToFirst(a6, a7) && a5.IsCloserToFirst(a7,a6), "can only be closer to one!");
        if( false == a5.Equals(a6) ) {
          Assert.IsTrue(a5.IsCloserToFirst(a5, a6), "Always closer to self");
        }
        Assert.IsFalse(a5.IsBetweenFromLeft(a6, a7) ==
                            a5.IsBetweenFromRight(a6, a7),
                            "can't be between left and between right");
      }
    }
示例#5
0
        /**
         * When the connectiontable changes, we re-estimate
         * the size of the network:
         */
        protected void EstimateSize(object contab, System.EventArgs args)
        {
            try {
                //Estimate the new size:
                int            net_size      = -1;
                BigInteger     least_dist    = null;
                BigInteger     greatest_dist = null;
                int            shorts        = 0;
                ConnectionList structs       = ((ConnectionEventArgs)args).CList;

                if (structs.MainType == ConnectionType.Structured)
                {
                    /*
                     * We know we are in the network, so the network
                     * has size at least 1.  And all our connections
                     * plus us is certainly a lower bound.
                     */

                    if (structs.Count + 1 > net_size)
                    {
                        net_size = structs.Count + 1;
                    }

                    /*
                     * We estimate the density of nodes in the address space,
                     * and since we know the size of the whole address space,
                     * we can use the density to estimate the number of nodes.
                     */
                    AHAddress local = (AHAddress)_local_add;
                    foreach (Connection c in structs)
                    {
                        if (c.ConType == "structured.near")
                        {
                            BigInteger dist = local.DistanceTo((AHAddress)c.Address);
                            if (shorts == 0)
                            {
                                //This is the first one
                                least_dist    = dist;
                                greatest_dist = dist;
                            }
                            else
                            {
                                if (dist > greatest_dist)
                                {
                                    greatest_dist = dist;
                                }
                                if (dist < least_dist)
                                {
                                    least_dist = dist;
                                }
                            }
                            shorts++;
                        }
                    }

                    /*
                     * Now we have the distance between the range of our neighbors
                     */
                    if (shorts > 0)
                    {
                        if (greatest_dist > least_dist)
                        {
                            BigInteger width = greatest_dist - least_dist;
                            //Here is our estimate of the inverse density:
                            BigInteger inv_density = width / (shorts);
                            //The density times the full address space is the number
                            BigInteger total     = Address.Full / inv_density;
                            int        total_int = total.IntValue();
                            if (total_int > net_size)
                            {
                                net_size = total_int;
                            }
                        }
                    }
                    //Now we have our estimate:
                    lock ( _sync ) {
                        _netsize = net_size;
                    }
                }

                if (ProtocolLog.NodeLog.Enabled)
                {
                    ProtocolLog.Write(ProtocolLog.NodeLog, String.Format(
                                          "Network size: {0} at {1}", _netsize,
                                          DateTime.UtcNow.ToString()));
                }
            }
            catch (Exception x) {
                if (ProtocolLog.Exceptions.Enabled)
                {
                    ProtocolLog.Write(ProtocolLog.Exceptions, x.ToString());
                }
            }
        }
示例#6
0
 /**
  * Use the DistanceTo function.  If this node is
  * positive distance from add, it is left of.
  */
 public bool IsLeftOf(AHAddress add)
 {
   return (add.DistanceTo(this) > 0);
 }
示例#7
0
 /**
  * Use the DistanceTo function.  If this node is
  * positive distance from add it is right of.
  */
 public bool IsRightOf(AHAddress add)
 {
   return (add.DistanceTo(this) < 0);
 }
示例#8
0
        public void Test()
        {
            byte[] buf1 = new byte[20];
            for (int i = 0; i <= 18; i++)
            {
                buf1[i] = 0x00;
            }
            buf1[19] = 0x0A;
            AHAddress test_address_1 = new AHAddress(MemBlock.Reference(buf1, 0, buf1.Length));

            byte[] buf2 = new byte[20];
            for (int i = 0; i <= 18; i++)
            {
                buf2[i] = 0xFF;
            }
            buf2[19] = 0xFE;
            AHAddress test_address_2 = new AHAddress(MemBlock.Reference(buf2, 0, buf2.Length));

            //test_address_1 is to the left of test_address_2
            //because it only a few steps in the clockwise direction:
            Assert.IsTrue(test_address_1.IsLeftOf(test_address_2), "IsLeftOf");
            Assert.IsTrue(test_address_2.IsRightOf(test_address_1), "IsRightOf");
            //This distance is twelve:
            Assert.AreEqual(test_address_2.DistanceTo(test_address_1),
                            new BigInteger(12), "DistanceTo");
            Assert.IsTrue(test_address_1.CompareTo(test_address_2) < 0, "CompareTo");
            Assert.IsTrue(test_address_2.CompareTo(test_address_1) > 0, "CompareTo");
            byte[] buf3 = new byte[Address.MemSize];
            test_address_2.CopyTo(buf3);
            AHAddress test3 = new AHAddress(MemBlock.Reference(buf3, 0, buf3.Length));

            Assert.IsTrue(test3.CompareTo(test_address_2) == 0, "CompareTo");
            Assert.IsTrue(test3.CompareTo(test3) == 0, "CompareTo");
            //As long as the address does not wrap around, adding should increase it:
            AHAddress a4 = new AHAddress(test_address_1.ToBigInteger() + 100);

            Assert.IsTrue(a4.CompareTo(test_address_1) > 0, "adding increases");
            Assert.IsTrue(a4.CompareTo(test_address_2) < 0, "smaller than biggest");
            //Here are some consistency tests:
            for (int i = 0; i < 1000; i++)
            {
                System.Random r  = new Random();
                byte[]        b1 = new byte[Address.MemSize];
                r.NextBytes(b1);
                //Make sure it is class 0:
                Address.SetClass(b1, 0);
                byte[] b2 = new byte[Address.MemSize];
                r.NextBytes(b2);
                //Make sure it is class 0:
                Address.SetClass(b2, 0);
                byte[] b3 = new byte[Address.MemSize];
                r.NextBytes(b3);
                //Make sure it is class 0:
                Address.SetClass(b3, 0);
                AHAddress a5 = new AHAddress(MemBlock.Reference(b1, 0, b1.Length));
                AHAddress a6 = new AHAddress(MemBlock.Reference(b2, 0, b2.Length));
                AHAddress a7 = new AHAddress(MemBlock.Reference(b3, 0, b3.Length));
                Assert.IsTrue(a5.CompareTo(a6) == -1 * a6.CompareTo(a5), "consistency");
                //Nothing is between the same address:
                Assert.IsFalse(a5.IsBetweenFromLeft(a6, a6), "Empty Between Left");
                Assert.IsFalse(a5.IsBetweenFromRight(a7, a7), "Empty Between Right");
                //Endpoints are not between:
                Assert.IsFalse(a6.IsBetweenFromLeft(a6, a7), "End point Between Left");
                Assert.IsFalse(a6.IsBetweenFromRight(a6, a7), "End point Between Right");
                Assert.IsFalse(a7.IsBetweenFromLeft(a6, a7), "End point Between Left");
                Assert.IsFalse(a7.IsBetweenFromRight(a6, a7), "End point Between Right");

                if (a5.IsBetweenFromLeft(a6, a7))
                {
                    //Then the following must be true:
                    Assert.IsTrue(a6.LeftDistanceTo(a5) < a6.LeftDistanceTo(a7),
                                  "BetweenLeft true");
                }
                else
                {
                    //Then the following must be false:
                    Assert.IsFalse(a6.LeftDistanceTo(a5) < a6.LeftDistanceTo(a7),
                                   "BetweenLeft false");
                }
                if (a5.IsBetweenFromRight(a6, a7))
                {
                    //Then the following must be true:
                    Assert.IsTrue(a6.RightDistanceTo(a5) < a6.RightDistanceTo(a7),
                                  "BetweenRight true");
                }
                else
                {
                    //Then the following must be false:
                    Assert.IsFalse(a6.RightDistanceTo(a5) < a6.RightDistanceTo(a7),
                                   "BetweenRight false");
                }
                if (a5.IsCloserToFirst(a6, a7))
                {
                    Assert.IsTrue(a5.DistanceTo(a6).abs() < a5.DistanceTo(a7).abs(), "IsCloser 1");
                }
                else
                {
                    Assert.IsFalse(a5.DistanceTo(a6).abs() < a5.DistanceTo(a7).abs(), "IsCloser 2");
                }
                Assert.IsFalse(a5.IsCloserToFirst(a6, a7) && a5.IsCloserToFirst(a7, a6), "can only be closer to one!");
                if (false == a5.Equals(a6))
                {
                    Assert.IsTrue(a5.IsCloserToFirst(a5, a6), "Always closer to self");
                }
                Assert.IsFalse(a5.IsBetweenFromLeft(a6, a7) ==
                               a5.IsBetweenFromRight(a6, a7),
                               "can't be between left and between right");
            }
        }
示例#9
0
 /**
  * Use the DistanceTo function.  If this node is
  * positive distance from add, it is left of.
  */
 public bool IsLeftOf(AHAddress add)
 {
     return(add.DistanceTo(this) > 0);
 }
示例#10
0
 /**
  * Use the DistanceTo function.  If this node is
  * positive distance from add it is right of.
  */
 public bool IsRightOf(AHAddress add)
 {
     return(add.DistanceTo(this) < 0);
 }