コード例 #1
0
ファイル: Simulator.cs プロジェクト: xujyan/brunet
    /**
     * @return the number of Structured Connections in the interval
     * (a1, a2) (not including the end points) when we move to the right.
     */
    public int RightInclusiveCount(AHAddress a1, AHAddress a2) {
      if( a1.Equals(a2) ) { return 0; }
      int dist;
      
      int count = _con_list.Count;
      int a2_idx = _con_list.BinarySearch(a2);
      int a1_idx = _con_list.BinarySearch(a1);
      
        /*
         * There are four cases, we deal with each separately:
         * 0) neither a1 nor a2 are in the table
         * 1) a1 is not, but a2 is
         * 2) a1 is, but a2 is not
         * 3) a1 and a2 are.
         */

      bool a2_present = true;
      bool a1_present = true;
      if( a2_idx < 0 ) {
        a2_present = false;
        a2_idx = ~a2_idx;
      }
      if( a1_idx < 0 ) {
        a1_present = false;
        a1_idx = ~a1_idx;
      }
      if( a1_idx == a2_idx ) {
        //This is an easy case:
        int max_dist = count;
        if( a2_present ) {
          max_dist--;
        }
        if( a1_present ) {
          max_dist--;
        }
        if( a2.CompareTo( a1 ) < 0 ) {
          dist = 0;  
        }
        else {
          dist = max_dist;
        }
      }
      else {
        //These two indices are different:
        dist = a1_idx - a2_idx;
        if( dist < 0 ) {
          //Wrap around.
          dist += count;
        }
        if( a2_present ) {
          /*
           * In thie case, our calculations are too much by one, in both
           * cases (dist > 0, dist < 0), so we decrease by one:
           */
          dist = dist - 1;
        }
      }
      return dist;
    }
コード例 #2
0
ファイル: AHAddress.cs プロジェクト: twchoi/tmp-brunet-deetoo
    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");
      }
    }
コード例 #3
0
    public static void Main(string []args) {
      if (args.Length < 1) {
	Console.WriteLine("please specify the number edge protocol."); 
        Environment.Exit(0);
      }
      if (args.Length < 2) {
        Console.WriteLine("please specify the number of p2p nodes."); 
        Environment.Exit(0);
      }
      if (args.Length < 3) {
        Console.WriteLine("please specify the number of missing edges."); 
        Environment.Exit(0);
      }
      string proto = "function";
      try {
	proto = args[0].Trim();
      } catch(Exception) {}

      bool tunnel = false;
      int base_port = 54000;
      int network_size = Int32.Parse(args[1]);
      int missing_count = Int32.Parse(args[2]);
      try {
	tunnel = args[3].Trim().Equals("tunnel");
      } catch (Exception) {}

      Console.WriteLine("use tunnel edges: {0}", tunnel);

      Random rand = new Random();

      ArrayList missing_edges = new ArrayList();
      for (int i = 0; i < missing_count; i++) {
	int idx = -1;
	int left, right;
	do {
	  idx = rand.Next(0, network_size);
	  left = (idx + 1)%network_size;
	  if (idx == 0) {
	    right = network_size - 1;
	  } else {
	    right = idx - 1;
	  }
	} while (missing_edges.Contains(idx));// ||
	//missing_edges.Contains(left) ||
	//missing_edges.Contains(right));
	
	Console.WriteLine("Will drop a left edge on idx {0}: ", idx);
	missing_edges.Add(idx);
      }
      
      //
      // Sort missing edges.
      //
      missing_edges.Sort();
      SortedList dist = new SortedList();
      //
      // Compute the average distance between missing edges. 
      //
      if (missing_count > 1) {
	for (int i = 0; i < missing_count; i++) {
	  int idx = (int) missing_edges[i];
	  int idx_next;
	  int d;
	  if (i == missing_count - 1) {
	    idx_next = (int) missing_edges[0];
	    d = (network_size - 1) - idx + idx_next;
	  } else {
	    idx_next = (int) missing_edges[i+1];
	    d = idx_next - idx - 1;
	  }
	  if (!dist.Contains(d)) {
	    dist[d] = 0;
	  } else {
	    int c = (int) dist[d];
	    dist[d] = c + 1;
	  }
	}
      }
      double sum = 0.0;
      int num = 0;
      Console.WriteLine("distribution of missing edges separation");
      foreach(DictionaryEntry de in dist) {
	int k = (int) de.Key;
	int c = (int) de.Value;
	Console.WriteLine("{0} {1}", k, c);
	sum = sum + k*c;
	num = num + c;
      }

      Console.WriteLine("average separation: {0}", (double) sum/num);
      string brunet_namespace = "testing";
      Console.WriteLine("Initializing...");

      ArrayList RemoteTA = new ArrayList();
      for(int i = 0; i < network_size; i++) {
	if (proto.Equals("udp")) {
	  RemoteTA.Add(TransportAddressFactory.CreateInstance("brunet.udp://localhost:" + (base_port + i)));
	} else if (proto.Equals("function")) { 
	  RemoteTA.Add(TransportAddressFactory.CreateInstance("brunet.function://localhost:" + (base_port + i)));
	}
      }

      for(int i = 0; i < network_size; i++) {
        AHAddress address = new AHAddress(new RNGCryptoServiceProvider());
        Node node = new StructuredNode(address, brunet_namespace);
        _sorted_node_list.Add((Address) address, node);
	_node_list.Add(node);
	RouteTestHandler test_handler = new RouteTestHandler();
	node.GetTypeSource(new PType(routing_test)).Subscribe(test_handler, address.ToMemBlock());
	RpcManager rpc_man = RpcManager.GetInstance(node);
	rpc_man.AddHandler("rpc_routing_test", new  RpcRoutingTestHandler(node));
      }

      for (int i = 0; i < network_size; i++) {
	Node node = (Node) _sorted_node_list.GetByIndex(i);
	Console.WriteLine("Configuring node: {0} ", node.Address);
	TAAuthorizer ta_auth = null;
	if (missing_edges.Contains(i)) {
	  int remote_port;
	  if (i == network_size - 1) {
	    remote_port = base_port;
	  } else {
	    remote_port = base_port + i + 1;
	  }

	  PortTAAuthorizer port_auth = new PortTAAuthorizer(remote_port);
	  Console.WriteLine("Adding a port TA authorizer at: {0} for remote port: {1}", base_port + i, remote_port);
	  ArrayList arr_tas = new ArrayList();
	  arr_tas.Add(port_auth);
	  arr_tas.Add(new ConstantAuthorizer(TAAuthorizer.Decision.Allow));
	  ta_auth = new SeriesTAAuthorizer(arr_tas);
	}
	
	if (proto.Equals("udp")) { 
	  node.AddEdgeListener(new UdpEdgeListener(base_port + i, null, ta_auth));
	} else if(proto.Equals("function")) {
	  node.AddEdgeListener(new FunctionEdgeListener(base_port + i, -1.00, ta_auth));
	}
	
	if (tunnel) {
	  Console.WriteLine("Adding a tunnel edge listener");
	  node.AddEdgeListener(new Tunnel.TunnelEdgeListener(node));
	}
	_node_to_port[node] = base_port + i;
        node.RemoteTAs = RemoteTA;	
      }

      //start nodes one by one.
      for (int i  = 0; i < network_size; i++) {
	Node node = (Node) _node_list[i];
	Console.WriteLine("Starting node: {0}, {1}", i, node.Address);
        node.Connect();
	Console.WriteLine("Going to sleep for 2 seconds.");
        System.Threading.Thread.Sleep(2000);
      }

      //wait for 300000 more seconds
      Console.WriteLine("Going to sleep for 300000 seconds.");
      System.Threading.Thread.Sleep(300000);
      bool complete = CheckStatus();

      int count = 0;
      //
      // Send a large number of packets as exact packets to random destinations
      // and make sure exact routing is perfect.
      //
      
      for (int i = 0; i < network_size; i++) {
	for (int j = 0; j < network_size; j++) {
	  
	  int src_idx = i;
	  int dest_idx = j;
	  Node src_node = (Node) _sorted_node_list.GetByIndex(src_idx);
	  Node dest_node = (Node) _sorted_node_list.GetByIndex(dest_idx);
	  //Console.WriteLine("{0} -> {1}", src_idx, dest_idx);
	  Address dest_address = (Address) dest_node.Address;
	  ISender s = new AHExactSender(src_node, dest_address);
	  MemBlock p = dest_address.ToMemBlock();
	  s.Send(new CopyList(new PType(routing_test), p));
	  _sent++;
	  //System.Threading.Thread.Sleep(10);
	  s.Send(new CopyList(new PType(routing_test), p));
	  _sent++;
	  //System.Threading.Thread.Sleep(10);
	}
      }
      //wait for 10 more seconds
      Console.WriteLine("Going to sleep for 10 seconds.");
      System.Threading.Thread.Sleep(10000);      
      Console.WriteLine("Final statistics");
      lock(_class_lock) {
	Console.WriteLine("Sent: {0}, Received: {1}, Wrongly routed: {2}", 
			  _sent, _received, _wrongly_routed);
      }

      int missing_rpcs = 0;
      int correct_rpcs = 0;
      int incorrect_rpcs = 0;
      Hashtable queue_to_address = new Hashtable();
      for (int i = 0; i < network_size; i++) {
	for (int j = 0; j < network_size; j++) {
	  
	  int src_idx = i;
	  int dest_idx = j;
	  Node src_node = (Node) _sorted_node_list.GetByIndex(src_idx);
	  Node dest_node = (Node) _sorted_node_list.GetByIndex(dest_idx);
	  //Console.WriteLine("{0} -> {1}", src_idx, dest_idx);
	  Address dest_address = (Address) dest_node.Address;
	  ISender s = new AHExactSender(src_node, dest_address);
	  RpcManager rpc_man = RpcManager.GetInstance(src_node);
	  Channel q = new Channel();
	  lock (_class_lock) {
	    queue_to_address[q] = dest_address;
	  }
	  q.CloseAfterEnqueue();
	  q.CloseEvent += delegate(object o, EventArgs cargs) {
	    lock(_class_lock) {
	      Channel qu = (Channel) o;
	      if (qu.Count == 0) {
		missing_rpcs++;
	      }
	      queue_to_address.Remove(qu);
	    }
	  };
	  q.EnqueueEvent += delegate(object o, EventArgs cargs) {
	    lock(_class_lock) {
	      Channel qu = (Channel) o;
	      RpcResult rpc_reply = (RpcResult) qu.Peek();
	      byte []result = (byte[]) rpc_reply.Result;
	      Address target = new AHAddress(result);
	      if (target.Equals(queue_to_address[qu])) {
		correct_rpcs++;
	      } else {
		incorrect_rpcs++;
	      }
	    }
	  };
	  rpc_man.Invoke(s, q, "rpc_routing_test.GetIdentification", new object[]{});
	}
      }
      
      //wait for 10 more seconds
      while (true) {
	int c = -1;
	lock(_class_lock) {
	  c = incorrect_rpcs + missing_rpcs + correct_rpcs;
	}
	if (c < network_size*network_size) {
	  Console.WriteLine("Going to sleep for 10 seconds.");
	  System.Threading.Thread.Sleep(10000);
	} else {
	  break;
	}
      }
      
      Console.WriteLine("Final statistics");
      Console.WriteLine("correct rpcs: {0}, incorrect rpcs: {1}, missing rpcs: {2}", 
			correct_rpcs, incorrect_rpcs, missing_rpcs);
      
      System.Environment.Exit(1);
    }
コード例 #4
0
ファイル: AHAddress.cs プロジェクト: twchoi/tmp-brunet-deetoo
 /** check which address is closed to this one
  * @return true if we are closer to the first than second
  */
 public bool IsCloserToFirst(AHAddress first, AHAddress sec) {
   uint pre0 = _prefix;
   uint pref = first._prefix;
   uint pres = sec._prefix;
   if( pref == pres ) {
     //They could be the same:
     if( first.Equals( sec ) ) { return false; }
     return DistanceTo(first).abs() < DistanceTo(sec).abs();
   }
   //See if the upper and lower bounds can avoid doing big-int stuff
   uint udf = pre0 > pref ? pre0 - pref : pref - pre0;
   uint uds = pre0 > pres ? pre0 - pres : pres - pre0;
   if( udf > Int32.MaxValue ) {
     //Wrap it around:
     udf = UInt32.MaxValue - udf;
   }
   if( uds > Int32.MaxValue ) {
     uds = UInt32.MaxValue - uds;
   }
   uint upperbound_f = udf + 1;
   uint lowerbound_s = uds > 0 ? uds - 1 : 0;
   if( upperbound_f <= lowerbound_s ) {
     //There is no way the exact value could make df bigger than ds:
     return true;
   }
   uint upperbound_s = uds + 1;
   uint lowerbound_f = udf > 0 ? udf - 1 : 0;
   if( upperbound_s <= lowerbound_f ) {
     //There is no way the exact value could make ds bigger than df:
     return false;
   }
   //Else just do it the simple, but costly way
   BigInteger df = DistanceTo(first).abs();
   BigInteger ds = DistanceTo(sec).abs();
   return (df < ds);
 }