コード例 #1
0
ファイル: ConnectToMessage.cs プロジェクト: xujyan/brunet
 public ConnectToMessage(string contype, NodeInfo target, NodeInfo[] neighbors, string token)
 {
   _ct = contype;
   _target_ni = target;
   _neighbors = neighbors;
   _token = token;
 }
コード例 #2
0
ファイル: ConnectToMessage.cs プロジェクト: xujyan/brunet
 /**
  * @param t connection type
  * @param target the Address of the target node
  * @param token unique token used to associate all connection setup messages
  *              with each other
  */
 public ConnectToMessage(ConnectionType t, NodeInfo target, string token)
 {
   _ct = Connection.ConnectionTypeToString(t);
   _target_ni = target;
   _neighbors = new NodeInfo[0]; //Make sure this isn't null
   _token = token;
 }
コード例 #3
0
ファイル: ConnectToMessage.cs プロジェクト: xujyan/brunet
 public ConnectToMessage(string contype, NodeInfo target, string token)
 {
   _ct = contype;
   _target_ni = target;
   _neighbors = new NodeInfo[0]; //Make sure this isn't null
   _token = token;
 }
コード例 #4
0
ファイル: LinkMessage.cs プロジェクト: xujyan/brunet
 public LinkMessage(StringDictionary attributes, NodeInfo local, NodeInfo remote, string token)
 {
   _attributes = attributes;
   _local_ni = local;
   _remote_ni = remote;
   _token = token;
 }
コード例 #5
0
ファイル: LinkMessage.cs プロジェクト: xujyan/brunet
 public LinkMessage(string connection_type, NodeInfo local, NodeInfo remote, string token)
 {
   _attributes = new StringDictionary();
   _attributes["type"] = String.Intern( connection_type );
   _local_ni = local;
   _remote_ni = remote;
   _token = token;
 }
コード例 #6
0
ファイル: LinkMessage.cs プロジェクト: xujyan/brunet
 public LinkMessage(ConnectionType t,
                    NodeInfo local,
                    NodeInfo remote,
                    string token)
 {
   _attributes = new StringDictionary();
   _attributes["type"] = Connection.ConnectionTypeToString(t);
   _local_ni = local;
   _remote_ni = remote;
   _token = token;
 }
コード例 #7
0
ファイル: CtmRequestHandler.cs プロジェクト: xujyan/brunet
 protected ConnectToMessage GetCtmResponseTo(ConnectToMessage ctm_req) {
   NodeInfo target = ctm_req.Target;
   
   //Send the 4 neighbors closest to this node:
   ArrayList nearest = _n.ConnectionTable.GetNearestTo( (AHAddress)target.Address, 4);
   //Now get these the NodeInfo objects for these:
   ArrayList neighbors = new ArrayList();
   foreach(Connection cons in nearest) {
     //No need to send the TA, since only the address is used
     NodeInfo neigh = NodeInfo.CreateInstance(cons.Address);
     neighbors.Add( neigh );
   }
   //Put these into an NodeInfo[]
   NodeInfo[] neigh_array = new NodeInfo[ neighbors.Count ];
   for(int i = 0; i < neighbors.Count; i++) {
     neigh_array[i] = (NodeInfo)neighbors[i];
   }
   return new ConnectToMessage(ctm_req.ConnectionType, _n.GetNodeInfo(8), neigh_array, ctm_req.Token);
 }
コード例 #8
0
 /// We want to include our 4 nearest neighbors
 override protected ConnectToMessage GetConnectToMessage(string ConnectionType,
     string token)
 {
   ArrayList nearest = _node.ConnectionTable.GetNearestTo( (AHAddress)_node.Address, 4);
   NodeInfo[] near_ni = new NodeInfo[nearest.Count];
   int i = 0;
   foreach(Connection cons in nearest) {
     //We don't use the TAs, just the addresses
     near_ni[i] = NodeInfo.CreateInstance(cons.Address);
     i++;
   }
   return new ConnectToMessage(ConnectionType, _node.GetNodeInfo(12, TAAuth), near_ni, token);
 }
コード例 #9
0
ファイル: NodeInfo.cs プロジェクト: xujyan/brunet
 /**
  * Factory method to reduce memory allocations by caching
  * commonly used NodeInfo objects
  */
 public static NodeInfo CreateInstance(Address a) {
   //Read some of the least significant bytes out,
   //AHAddress all have last bit 0, so we skip the last byte which
   //will have less entropy
   MemBlock mb = a.ToMemBlock();
   ushort idx = (ushort)NumberSerializer.ReadShort(mb, Address.MemSize - 3);
   NodeInfo ni = _mb_cache[idx];
   if( ni != null ) {
     if (a.Equals(ni._address)) {
       return ni;
     }
   }
   ni = new NodeInfo(a);
   _mb_cache[idx] = ni;
   return ni;
 }
コード例 #10
0
ファイル: NodeInfo.cs プロジェクト: xujyan/brunet
 public void RoundTrip(NodeInfo ni) {
   NodeInfo ni_other = NodeInfo.CreateInstance(ni.Address, ni.Transports);
   Assert.AreEqual(ni, ni_other, "Hashtable roundtrip");
   Assert.AreEqual(ni.GetHashCode(), ni_other.GetHashCode(), "Hashtable GetHashCode roundtrip");
 }
コード例 #11
0
ファイル: NodeInfo.cs プロジェクト: xujyan/brunet
 public void RoundTripHT(NodeInfo ni) {
   NodeInfo ni_other = NodeInfo.CreateInstance( ni.ToDictionary() );
   Assert.AreEqual(ni, ni_other, "Hashtable roundtrip");
   Assert.AreEqual(ni.GetHashCode(), ni_other.GetHashCode(), "Hashtable GetHashCode roundtrip");
 }
コード例 #12
0
ファイル: NodeInfo.cs プロジェクト: xujyan/brunet
 public static NodeInfo CreateInstance(Address a, IList ta) {
   NodeInfo result = null;
   Cache ni_cache = Interlocked.Exchange<Cache>(ref _cache, null);
   if( ni_cache != null ) {
     try {
       //Set up the key:
       _cache_key._done_hash = false;
       _cache_key._address = a;
       _cache_key._tas = ta;
       
       result = (NodeInfo)ni_cache[_cache_key];
       if( result == null ) {
         //This may look weird, but we are using a NodeInfo as a key
         //to lookup NodeInfos, this will allow us to only keep one
         //identical NodeInfo in scope at a time.
         result = new NodeInfo(a, ta);
         ni_cache[result] = result;
       }
     }
     finally {
       Interlocked.Exchange<Cache>(ref _cache, ni_cache);
     }
   }
   else {
     result = new NodeInfo(a, ta);
   }
   return result;
 }
コード例 #13
0
ファイル: ConnectToMessage.cs プロジェクト: xujyan/brunet
 public ConnectToMessage(IDictionary ht) {
   _ct = (string)ht["type"];
   _target_ni = NodeInfo.CreateInstance((IDictionary)ht["target"]);
   _token = (string) ht["token"];
   IList neighht = ht["neighbors"] as IList;
   if( neighht != null ) {
     _neighbors = new NodeInfo[ neighht.Count ];
     for(int i = 0; i < neighht.Count; i++) {
       _neighbors[i] = NodeInfo.CreateInstance( (IDictionary)neighht[i] );
     }
   }
 }
コード例 #14
0
ファイル: ConnectToMessage.cs プロジェクト: xujyan/brunet
    public void CTMSerializationTest()
    {
      Address a = new DirectionalAddress(DirectionalAddress.Direction.Left);
      TransportAddress ta = TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:5000"); 
      NodeInfo ni = NodeInfo.CreateInstance(a, ta);

      RandomNumberGenerator rng = new RNGCryptoServiceProvider();      
      AHAddress tmp_add = new AHAddress(rng);
      ConnectToMessage ctm1 = new ConnectToMessage(ConnectionType.Unstructured, ni, tmp_add.ToString());
      
      HTRoundTrip(ctm1);

      //Test multiple tas:
      ArrayList tas = new ArrayList();
      tas.Add(ta);
      for(int i = 5001; i < 5010; i++)
        tas.Add(TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:" + i.ToString()));
      NodeInfo ni2 = NodeInfo.CreateInstance(a, tas);

      ConnectToMessage ctm2 = new ConnectToMessage(ConnectionType.Structured, ni2, tmp_add.ToString());
      HTRoundTrip(ctm2);
      //Here is a ConnectTo message with a neighbor list:
      NodeInfo[] neighs = new NodeInfo[5];
      for(int i = 0; i < 5; i++) {
	string ta_tmp = "brunet.tcp://127.0.0.1:" + (i+80).ToString();
        NodeInfo tmp =
		NodeInfo.CreateInstance(new DirectionalAddress(DirectionalAddress.Direction.Left),
	                     TransportAddressFactory.CreateInstance(ta_tmp)
			    );
	neighs[i] = tmp;
      }
      ConnectToMessage ctm3 = new ConnectToMessage("structured", ni, neighs, tmp_add.ToString());
      HTRoundTrip(ctm3);
#if false
      Console.Error.WriteLine( ctm3.ToString() );
      foreach(NodeInfo tni in ctm3a.Neighbors) {
        Console.Error.WriteLine(tni.ToString());
      }
#endif
    }
コード例 #15
0
    /**
     * Initiates connection setup. 
     * @param sender the ISender for the Connector to use
     * @param contype the type of connection we want to make
     * @param token the token used for connection messages
     * @param responses the maximum number of ctm response messages to listen
     */
    protected void ConnectTo(ISender sender, string contype, string token, int responses)
    {
      ConnectionType mt = Connection.StringToMainType(contype);
      /*
       * This is an anonymous delegate which is called before
       * the Connector starts.  If it returns true, the Connector
       * will finish immediately without sending an ConnectToMessage
       */
      Connector.AbortCheck abort = null;
      ForwardingSender fs = sender as ForwardingSender;
      if( fs != null ) {
        //In general, we only know the exact node we are trying
        //to reach when we are using a ForwardingSender
        Address target = fs.Destination;
        Linker l = new Linker(_node, target, null, contype, token);
        object linker_task = l.Task;  //This is what we check for
        abort = delegate(Connector c) {
          bool stop = _node.ConnectionTable.Contains( mt, target );
          if (!stop ) {
              /*
               * Make a linker to get the task.  We won't use
               * this linker.
               * No need in sending a ConnectToMessage if we
               * already have a linker going.
               */
            stop = _node.TaskQueue.HasTask( linker_task );
          }
          return stop;
        };
        if ( abort(null) ) {
          return;
        }
      }
      //Send the 4 neighbors closest to this node:
      ArrayList nearest = _node.ConnectionTable.GetNearestTo( (AHAddress)_node.Address, 4);
      NodeInfo[] near_ni = new NodeInfo[nearest.Count];
      int i = 0;
      foreach(Connection cons in nearest) {
        //We don't use the TAs, just the addresses
        near_ni[i] = NodeInfo.CreateInstance(cons.Address);
        i++;
      }
      ConnectToMessage ctm = new ConnectToMessage(contype, _node.GetNodeInfo(8), near_ni, token);

      Connector con = new Connector(_node, sender, ctm, this);
      con.AbortIf = abort;
      //Keep a reference to it does not go out of scope
      lock( _sync ) {
        _connectors[con] = responses;
      }
      con.FinishEvent += new EventHandler(this.ConnectorEndHandler);
      //Start up this Task:
      _node.TaskQueue.Enqueue(con);
    }