예제 #1
0
파일: AddtoDns.cs 프로젝트: xujyan/brunet
    ///This tester simply establishes the Brunet network and log the edges made
 
    static void Main(string[] args)
    {

      String config_file = args[0];
      NetworkConfiguration network_configuration = NetworkConfiguration.Deserialize(config_file);

      for(int i = 0; i < network_configuration.Nodes.Count; i++){

        NodeConfiguration this_node_configuration = (NodeConfiguration)network_configuration.Nodes[i];
        TransportAddressConfiguration local_ta_configuration = (TransportAddressConfiguration)this_node_configuration.TransportAddresses[0];
        short port = local_ta_configuration.Port;
        SHA1 sha = new SHA1CryptoServiceProvider();
        String local_ta = local_ta_configuration.GetTransportAddressURI();
        //We take the local transport address plus the port number to be hashed to obtain a random AHAddress
        byte[] hashedbytes = sha.ComputeHash(Encoding.UTF8.GetBytes(local_ta + port));
        //inforce type 0
        hashedbytes[Address.MemSize - 1] &= 0xFE;
        AHAddress _local_ahaddress = new AHAddress(hashedbytes);
        Console.WriteLine(_local_ahaddress.ToBigInteger().ToString() + ' ' + local_ta_configuration.Address + ' ' + (port%25000));
      }
    }
예제 #2
0
파일: AHAddress.cs 프로젝트: xujyan/brunet
    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 < 100; 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");
        }
        Assert.IsFalse(a5.IsBetweenFromLeft(a6, a7) ==
                            a5.IsBetweenFromRight(a6, a7),
                            "can't be between left and between right");
      }
    }
예제 #3
0
파일: AHAddress.cs 프로젝트: xujyan/brunet
    /**
     * 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;
    }
예제 #4
0
파일: AHAddress.cs 프로젝트: xujyan/brunet
    /**
     * 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;
    }
예제 #5
0
파일: AHAddress.cs 프로젝트: xujyan/brunet
    /**
     * 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;
    }
    /**
     * 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 ArrayList GenerateTreeOutRange(AHAddress start, AHAddress end, MapReduceArgs mr_args, int timeout) {
      ArrayList retval = new ArrayList();
      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) ) {
          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,
								  timeout));
	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;
    }
예제 #7
0
 /**
 <summary>This method returns matching querying address from caching address.<\summary>
 */
 protected AHAddress AddressTranspose(AHAddress a) {
   BigInteger one = new BigInteger(1);
   BigInteger s_BIN = one << 80;
   BigInteger addr = a.ToBigInteger();
   BigInteger addr_j = addr % s_BIN;
   BigInteger addr_i = (addr - addr_j) / s_BIN;
   BigInteger q = addr_j * s_BIN + addr_i;
   //AHAddress q_addr = new AHAddress(q);
   byte[] target = Address.ConvertToAddressBuffer(q);
   //Address.SetClass(target, _c_node.Address.Class);
   Address.SetClass(target, a.Class);
   AHAddress q_addr = new AHAddress(target);
   return q_addr;
 }