/** * Compute the distance from this to add such that * the magnitude is less than or equal to Address.Half */ public virtual BigInteger DistanceTo(AHAddress a) { BigInteger n_x = this.ToBigInteger(); BigInteger n_y = a.ToBigInteger(); BigInteger dist = n_y - n_x; if (n_y > n_x) { //(n_y > n_x ) == (dist > 0), //but the former is faster for BigInteger if (dist >= Address.Half) { dist = dist - AHAddress.Full; } } else { //we know dist <= 0: //If dist < -Address.Half //if (0 > (Address.Half + dist)) { //same as below, but below doesn't require BigInteger(0), //so it saves memory and CPU: if (n_x > (Address.Half + n_y)) { // dist = dist + AHAddress.Full; } } return(dist); }
// adds a disconnected pair to the pool public void AddDisconnectedPair(out Address address1, out Address address2, bool nctunnel) { address1 = new AHAddress(new RNGCryptoServiceProvider()); byte[] addrbuff = Address.ConvertToAddressBuffer(address1.ToBigInteger() + (Address.Full / 2)); Address.SetClass(addrbuff, AHAddress._class); address2 = new AHAddress(addrbuff); AddDisconnectedPair(address1, address2, nctunnel); }
/// <summary>Calculate the Address immediately to the right.</summary> public static AHAddress GetRightNearTarget(AHAddress address) { BigInteger local_int_add = address.ToBigInteger(); //must have even addresses so increment twice local_int_add += 2; //Make sure we don't overflow: BigInteger tbi = new BigInteger(local_int_add % Address.Full); return(new AHAddress(tbi)); }
/** * The Right (decreasing, counterclockwise) distance to * the given AHAddress * @param addr the AHAddress to compute the distance to * @return the distance */ public BigInteger RightDistanceTo(AHAddress addr) { BigInteger n_x = ToBigInteger(); BigInteger n_y = addr.ToBigInteger(); BigInteger dist; if (n_y < n_x) { //The given address is smaller than us, just subtract dist = n_x - n_y; } else { //We need to add AHAddress.Full to the result: dist = n_x - n_y + AHAddress.Full; } return(dist); }
/** * The Left (increasing, clockwise) distance to * the given AHAddress * @param addr the AHAddress to compute the distance to * @return the distance */ public BigInteger LeftDistanceTo(AHAddress addr) { BigInteger n_x = ToBigInteger(); BigInteger n_y = addr.ToBigInteger(); BigInteger dist; if (n_y > n_x) { //The given address is larger than us, just subtract dist = n_y - n_x; } else { //We need to add AHAddress.Full to the result: dist = n_y - n_x + AHAddress.Full; } return(dist); }
public void Test() { Address addr_x = new AHAddress(new RNGCryptoServiceProvider()); byte[] addrbuff = Address.ConvertToAddressBuffer(addr_x.ToBigInteger() + (Address.Full / 2)); Address.SetClass(addrbuff, AHAddress._class); Address addr_y = new AHAddress(addrbuff); List<Connection> connections = new List<Connection>(); ConnectionTable ct_x = new ConnectionTable(); ConnectionTable ct_y = new ConnectionTable(); ConnectionTable ct_empty = new ConnectionTable(); NCService ncservice = new NCService(); Connection fast_con = null; for(int i = 1; i <= 11; i++) { addrbuff = Address.ConvertToAddressBuffer(addr_x.ToBigInteger() + (i * Address.Full / 16)); Address.SetClass(addrbuff, AHAddress._class); Address addr = new AHAddress(addrbuff); Connection con = null; TransportAddress ta = TransportAddressFactory.CreateInstance("brunet.tcp://158.7.0.1:5000"); Edge fe = new FakeEdge(ta, ta, TransportAddress.TAType.Tcp); if(i <= 10) { con = new Connection(fe, addr, "structured", null, null); ct_x.Add(con); if(i % 2 == 0) { ncservice.ProcessSample(DateTime.UtcNow, String.Empty, addr, new Point(new double[] {0, 0}, 0), 0, i*10); } } else { fast_con = new Connection(fe, addr, "structured", null, null); ncservice.ProcessSample(DateTime.UtcNow, String.Empty, addr, new Point(new double[] {0, 0}, 0), 0, 5); } if(i == 10) { ct_y.Add(con); } connections.Add(con); } ITunnelOverlap sto = new SimpleTunnelOverlap(); ITunnelOverlap nto = new NCTunnelOverlap(ncservice); ConnectionType con_type = ConnectionType.Structured; List<Connection> pre_cons = new List<Connection>(); pre_cons.Add(connections[9]); IDictionary id = nto.GetSyncMessage(pre_cons, addr_x, ct_x.GetConnections(con_type)); // We do have some pre-existing overlap Assert.AreEqual(nto.EvaluateOverlap(ct_y.GetConnections(con_type), id)[0], connections[9], "NC: Have an overlap!"); Assert.AreEqual(sto.EvaluateOverlap(ct_y.GetConnections(con_type), id)[0], connections[9], "Simple: Have an overlap!"); // We have no overlap with an empty connection table Assert.AreEqual(nto.EvaluateOverlap(ct_empty.GetConnections(con_type), id).Count, 0, "No overlap!"); Assert.AreEqual(sto.EvaluateOverlap(ct_empty.GetConnections(con_type), id).Count, 0, "No overlap!"); // latency[0] == -1 Assert.AreEqual(connections[1].Address.Equals(nto.EvaluatePotentialOverlap(id)), true, "NC: EvaluatePotentialOverlap returns expected!"); Assert.AreEqual(ct_x.Contains(con_type, sto.EvaluatePotentialOverlap(id)), true, "Simple: EvaluatePotentialOverlap returns valid!"); ct_y.Add(fast_con); ct_x.Add(fast_con); id = nto.GetSyncMessage(pre_cons, addr_x, ct_x.GetConnections(con_type)); Assert.AreEqual(fast_con.Address.Equals(nto.EvaluatePotentialOverlap(id)), true, "NC: EvaluatePotentialOverlap returns expected!"); Assert.AreEqual(nto.EvaluateOverlap(ct_y.GetConnections(con_type), id)[0], fast_con, "NC: Have better overlap!"); }
/** * When a node is out of the range, this method is called. * This method tries to find the nearest node to the middle of range using greedty algorithm. * return list of MapReduceInfo */ private List<MapReduceInfo> GenerateTreeOutRange(AHAddress start, AHAddress end, MapReduceArgs mr_args) { List<MapReduceInfo> retval = new List<MapReduceInfo>(); BigInteger up = start.ToBigInteger(); BigInteger down = end.ToBigInteger(); BigInteger mid_range = (up + down) /2; if (mid_range % 2 == 1) {mid_range = mid_range -1; } AHAddress mid_addr = new AHAddress(mid_range); //if (!mid_addr.IsBetweenFromLeft(start, end) ) { if (!InRange(mid_addr, start, end) ) { mid_range += Address.Half; mid_addr = new AHAddress(mid_range); } ArrayList gen_arg = new ArrayList(); if (NextGreedyClosest(mid_addr) != null ) { AHGreedySender ags = new AHGreedySender(_node, mid_addr); string start_range = start.ToString(); string end_range = end.ToString(); gen_arg.Add(start_range); gen_arg.Add(end_range); MapReduceInfo mr_info = new MapReduceInfo( (ISender) ags, new MapReduceArgs(this.TaskName, mr_args.MapArg, gen_arg, mr_args.ReduceArg)); Log("{0}: {1}, out of range, moving to the closest node to mid_range: {2} to target node, range start: {3}, range end: {4}", this.TaskName, _node.Address, mid_addr, start, end); retval.Add(mr_info); } else { // cannot find a node in the range. } return retval; }
public void Test() { IRelayOverlap _ito = new SimpleRelayOverlap(); Address addr_x = new AHAddress(new RNGCryptoServiceProvider()); byte[] addrbuff = Address.ConvertToAddressBuffer(addr_x.ToBigInteger() + (Address.Full / 2)); Address.SetClass(addrbuff, AHAddress._class); Address addr_y = new AHAddress(addrbuff); ArrayList addresses = new ArrayList(); ConnectionTable ct_x = new ConnectionTable(); ConnectionTable ct_y = new ConnectionTable(); ConnectionTable ct_empty = new ConnectionTable(); for(int i = 1; i <= 10; i++) { addrbuff = Address.ConvertToAddressBuffer(addr_x.ToBigInteger() + (i * Address.Full / 16)); Address.SetClass(addrbuff, AHAddress._class); addresses.Add(new AHAddress(addrbuff)); TransportAddress ta = TransportAddressFactory.CreateInstance("brunet.tcp://158.7.0.1:5000"); Edge fe = new FakeEdge(ta, ta, TransportAddress.TAType.Tcp); ct_x.Add(new Connection(fe, addresses[i - 1] as AHAddress, "structured", null, null)); if(i == 10) { ct_y.Add(new Connection(fe, addresses[i - 1] as AHAddress, "structured", null, null)); } } ConnectionType con_type = ConnectionType.Structured; IDictionary id = _ito.GetSyncMessage(null, addr_x, ct_x.GetConnections(con_type)); Assert.AreEqual(_ito.EvaluateOverlap(ct_y.GetConnections(con_type), id)[0].Address, addresses[9], "Have an overlap!"); Assert.AreEqual(_ito.EvaluateOverlap(ct_empty.GetConnections(con_type), id).Count, 0, "No overlap!"); Assert.AreEqual(addresses.Contains(_ito.EvaluatePotentialOverlap(id)), true, "EvaluatePotentialOverlap returns valid!"); }
public AHAddress GetNearTarget(AHAddress address) { /** * try to get at least one neighbor using forwarding through the * leaf . The forwarded address is 2 larger than the address of * the new node that is getting connected. */ BigInteger local_int_add = address.ToBigInteger(); //must have even addresses so increment twice local_int_add += 2; //Make sure we don't overflow: BigInteger tbi = new BigInteger(local_int_add % Address.Full); return new AHAddress(tbi); }
/// <summary>Calculates a shortcut using a harmonic distribution as in a /// Symphony-lke shortcut.</summary> protected virtual AHAddress ComputeShortcutTarget(AHAddress addr) { int network_size = _addrs.Count; double logN = (double)(Brunet.Address.MemSize * 8); double logk = Math.Log( (double) network_size, 2.0 ); double p = _rand.NextDouble(); double ex = logN - (1.0 - p)*logk; int ex_i = (int)Math.Floor(ex); double ex_f = ex - Math.Floor(ex); //Make sure 2^(ex_long+1) will fit in a long: int ex_long = ex_i % 63; int ex_big = ex_i - ex_long; ulong dist_long = (ulong)Math.Pow(2.0, ex_long + ex_f); //This is 2^(ex_big): BigInteger big_one = 1; BigInteger dist_big = big_one << ex_big; BigInteger rand_dist = dist_big * dist_long; // Add or subtract random distance to the current address BigInteger t_add = addr.ToBigInteger(); // Random number that is 0 or 1 if( _rand.Next(2) == 0 ) { t_add += rand_dist; } else { t_add -= rand_dist; } BigInteger target_int = new BigInteger(t_add % Address.Full); if((target_int & 1) == 1) { target_int -= 1; } byte[]buf = Address.ConvertToAddressBuffer(target_int); Address.SetClass(buf, AHAddress.ClassValue); return new AHAddress(buf); }
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"); } }
/** * The Right (decreasing, counterclockwise) distance to * the given AHAddress * @param addr the AHAddress to compute the distance to * @return the distance */ public BigInteger RightDistanceTo(AHAddress addr) { BigInteger n_x = ToBigInteger(); BigInteger n_y = addr.ToBigInteger(); BigInteger dist; if (n_y < n_x) { //The given address is smaller than us, just subtract dist = n_x - n_y; } else { //We need to add AHAddress.Full to the result: dist = n_x - n_y + AHAddress.Full; } return dist; }
/** * The Left (increasing, clockwise) distance to * the given AHAddress * @param addr the AHAddress to compute the distance to * @return the distance */ public BigInteger LeftDistanceTo(AHAddress addr) { BigInteger n_x = ToBigInteger(); BigInteger n_y = addr.ToBigInteger(); BigInteger dist; if (n_y > n_x) { //The given address is larger than us, just subtract dist = n_y - n_x; } else { //We need to add AHAddress.Full to the result: dist = n_y - n_x + AHAddress.Full; } return dist; }
/** * Compute the distance from this to add such that * the magnitude is less than or equal to Address.Half */ public virtual BigInteger DistanceTo(AHAddress a) { BigInteger n_x = this.ToBigInteger(); BigInteger n_y = a.ToBigInteger(); BigInteger dist = n_y - n_x; if (n_y > n_x) { //(n_y > n_x ) == (dist > 0), //but the former is faster for BigInteger if (dist >= Address.Half) { dist = dist - AHAddress.Full; } } else { //we know dist <= 0: //If dist < -Address.Half //if (0 > (Address.Half + dist)) { //same as below, but below doesn't require BigInteger(0), //so it saves memory and CPU: if (n_x > (Address.Half + n_y)) { // dist = dist + AHAddress.Full; } } return dist; }
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"); } }
/// <summary>Calculate the Left AHAddress of the given address.</summary> public AHAddress GetLeftNearTarget(AHAddress address) { BigInteger local_int_add = address.ToBigInteger(); //must have even addresses so increment twice local_int_add -= 2; //Make sure we don't overflow: BigInteger tbi = new BigInteger(local_int_add % Address.Full); return new AHAddress(tbi); }