Inheritance: StructuredAddress
示例#1
0
 public DirectionalAddress(DirectionalAddress.Direction bearing) : base()
 {
   byte[] buffer = new byte[ MemSize ];
   NumberSerializer.WriteInt((int)bearing, buffer, 0);
   SetClass(buffer, this.Class);
   _dir = bearing;
   _buffer = MemBlock.Reference(buffer, 0, MemSize);
 }
        /**
         * This method is called when there is a Disconnection from
         * the ConnectionTable
         */
        protected void DisconnectHandler(object connectiontable, EventArgs args)
        {
            ConnectionEventArgs ceargs = (ConnectionEventArgs)args;
            Connection          c      = ceargs.Connection;


            lock ( _sync ) {
                _last_connection_time   = DateTime.UtcNow;
                _need_left              = -1;
                _need_right             = -1;
                _current_retry_interval = _DEFAULT_RETRY_INTERVAL;
            }

            if (!IsActive)
            {
                return;
            }


            if (c.MainType != ConnectionType.Structured)
            {
                //Just activate and see what happens:
                Activate();
                return;
            }

            ConnectionList cl        = ceargs.CList;
            int            right_pos = cl.RightInclusiveCount(_node.Address, c.Address);

            if (right_pos < DESIRED_NEIGHBORS)
            {
                //We lost a close friend.
                Address target  = new DirectionalAddress(DirectionalAddress.Direction.Right);
                short   ttl     = (short)DESIRED_NEIGHBORS;
                string  contype = STRUC_NEAR;
                ISender send    = new AHSender(_node, target, ttl, AHHeader.Options.Last);
                ConnectTo(send, target, contype, 1);
            }

            int left_pos = cl.LeftInclusiveCount(_node.Address, c.Address);

            if (left_pos < DESIRED_NEIGHBORS)
            {
                //We lost a close friend.
                Address target  = new DirectionalAddress(DirectionalAddress.Direction.Left);
                short   ttl     = (short)DESIRED_NEIGHBORS;
                string  contype = STRUC_NEAR;
                ISender send    = new AHSender(_node, target, ttl, AHHeader.Options.Last);
                ConnectTo(send, target, contype, 1);
            }
        }
示例#3
0
文件: AHSender.cs 项目: pcbing/brunet
        public override Pair <Connection, bool> NextConnection(Edge from, AHHeader h)
        {
            DirectionalAddress dest = (DirectionalAddress)h.Destination;

            if (h.Ttl <= h.Hops)
            {
                //Deliver it to us but stop it here:
                return(_NULL_TRUE);
            }
            if (dest.Bearing == DirectionalAddress.Direction.Left)
            {
                if (h.Opts == AHHeader.Options.Path)
                {
                    return(_LEFT_TRUE);
                }
                else
                {
                    return(_LEFT_FALSE);
                }
            }
            else if (dest.Bearing == DirectionalAddress.Direction.Right)
            {
                if (h.Opts == AHHeader.Options.Path)
                {
                    return(_RIGHT_TRUE);
                }
                else
                {
                    return(_RIGHT_FALSE);
                }
            }
            else
            {
                throw new System.Exception(
                          System.String.Format("Unrecognized direction: {0}", dest.Bearing));
            }
        }
    /**
     * This method is called when there is a Disconnection from
     * the ConnectionTable
     */
    protected void DisconnectHandler(object connectiontable, EventArgs args)
    { 
      ConnectionEventArgs ceargs = (ConnectionEventArgs)args;
      Connection c = ceargs.Connection;


      lock( _sync ) {
        _last_connection_time = DateTime.UtcNow;
        _need_left = -1;
        _need_right = -1;
        _current_retry_interval = _DEFAULT_RETRY_INTERVAL;
      }

      if( !IsActive ) {
        return;
      }


      if( c.MainType != ConnectionType.Structured ) {
        //Just activate and see what happens:
        Activate();
        return;
      }

      ConnectionList cl = ceargs.CList;
      int right_pos = cl.RightInclusiveCount(_node.Address, c.Address);
      if( right_pos < DESIRED_NEIGHBORS ) {
        //We lost a close friend.
        Address target = new DirectionalAddress(DirectionalAddress.Direction.Right);
        short ttl = (short)DESIRED_NEIGHBORS;
        string contype = STRUC_NEAR;
        ISender send = new AHSender(_node, target, ttl, AHHeader.Options.Last);
        ConnectTo(send, target, contype, 1);
      }

      int left_pos = cl.LeftInclusiveCount(_node.Address, c.Address);
      if( left_pos < DESIRED_NEIGHBORS ) {
        //We lost a close friend.
        Address target = new DirectionalAddress(DirectionalAddress.Direction.Left);
        short ttl = (short)DESIRED_NEIGHBORS;
        string contype = STRUC_NEAR;
        ISender send = new AHSender(_node, target, ttl, AHHeader.Options.Last);
        ConnectTo(send, target, contype, 1);
      }
    }
    ///////////////// Methods //////////////////////
    
    /**
     * Starts the Overlord if we are active
     *
     * This method is called by the CheckState method
     * IF we have not seen any connections in a while
     * AND we still need some connections
     *
     */
    public override void Activate()
    {
#if POB_DEBUG
      Console.Error.WriteLine("In Activate: {0}", _node.Address);
#endif
      if( IsActive == false ) {
        return;
      }

      DateTime now = DateTime.UtcNow;
      lock( _sync ) {
        if( now - _last_retry_time < _current_retry_interval ) {
          //Not time yet...
          return;
        }
        _last_retry_time = now;
        //Double the length of time we wait (resets to default on connections)
        _current_retry_interval += _current_retry_interval;
        _current_retry_interval = (_MAX_RETRY_INTERVAL < _current_retry_interval) ?
            _MAX_RETRY_INTERVAL : _current_retry_interval;
      }

      ConnectionTable tab = _node.ConnectionTable;
      //If we are going to connect to someone, this is how we
      //know who to use
      Address target = null;
      string contype = String.Empty;
      ISender sender = null;
      int desired_ctms = 1;
      
      ConnectionList structs = tab.GetConnections(ConnectionType.Structured);
      if( structs.Count < 2 ) {
        ConnectionList leafs = tab.GetConnections(ConnectionType.Leaf);
        if( leafs.Count == 0 )
        {
          /*
           * We first need to get a Leaf connection
           */
          return;
        }
        //We don't have enough connections to guarantee a connected
        //graph.  Use a leaf connection to get another connection
        Connection leaf = null;
        //Make sure the following loop can't go on forever
        int attempts = 2 * leafs.Count;
        do {
          leaf = leafs[ _rand.Next( leafs.Count ) ];
          attempts--;
        }
        while( leafs.Count > 1 && structs.Contains( leaf.Address ) &&
               attempts > 0 );
        //Now we have a random leaf that is not a
        //structured neighbor to try to get a new neighbor with:
        if( leaf != null ) {
          target = GetSelfTarget();
          /*
           * This is the case of trying to find the nodes nearest
           * to ourselves, use the Annealing routing to get connected
           * more quickly
           */
          sender = new ForwardingSender(_node, leaf.Address, target);
          //We are trying to connect to the two nearest nodes in one
          //one attempt, so wait for two distinct responses:
          desired_ctms = 2;
          //This is a near neighbor connection
          contype = STRUC_NEAR;
        }
      }
      
      if( structs.Count > 0 && sender == null ) {
        /**
         * We need left or right neighbors we send
         * a ConnectToMessage in the directons we
         * need.
         */
        if( NeedLeftNeighbor ) {
#if POB_DEBUG
          Console.Error.WriteLine("NeedLeftNeighbor: {0}", _node.Address);
#endif
          target = new DirectionalAddress(DirectionalAddress.Direction.Left);
          short ttl = (short)DESIRED_NEIGHBORS;
          sender = new AHSender(_node, target, ttl, AHHeader.Options.Last);
          contype = STRUC_NEAR;
        } else if( NeedRightNeighbor ) {
#if POB_DEBUG
          Console.Error.WriteLine("NeedRightNeighbor: {0}", _node.Address);
#endif
          target = new DirectionalAddress(DirectionalAddress.Direction.Right);
          short ttl = (short)DESIRED_NEIGHBORS;
          sender = new AHSender(_node, target, ttl, AHHeader.Options.Last);
          contype = STRUC_NEAR;
        }
      }

      if( sender != null ) {
        ConnectTo(sender, target, contype, desired_ctms);
      }
    }
        ///////////////// Methods //////////////////////

        /**
         * Starts the Overlord if we are active
         *
         * This method is called by the CheckState method
         * IF we have not seen any connections in a while
         * AND we still need some connections
         *
         */
        public override void Activate()
        {
#if POB_DEBUG
            Console.Error.WriteLine("In Activate: {0}", _node.Address);
#endif
            if (IsActive == false)
            {
                return;
            }

            DateTime now = DateTime.UtcNow;
            lock ( _sync ) {
                if (now - _last_retry_time < _current_retry_interval)
                {
                    //Not time yet...
                    return;
                }
                _last_retry_time = now;
                //Double the length of time we wait (resets to default on connections)
                _current_retry_interval += _current_retry_interval;
                _current_retry_interval  = (_MAX_RETRY_INTERVAL < _current_retry_interval) ?
                                           _MAX_RETRY_INTERVAL : _current_retry_interval;
            }

            ConnectionTable tab = _node.ConnectionTable;
            //If we are going to connect to someone, this is how we
            //know who to use
            Address target       = null;
            string  contype      = String.Empty;
            ISender sender       = null;
            int     desired_ctms = 1;

            ConnectionList structs = tab.GetConnections(ConnectionType.Structured);
            if (structs.Count < 2)
            {
                ConnectionList leafs = tab.GetConnections(ConnectionType.Leaf);
                if (leafs.Count == 0)
                {
                    /*
                     * We first need to get a Leaf connection
                     */
                    return;
                }
                //We don't have enough connections to guarantee a connected
                //graph.  Use a leaf connection to get another connection
                Connection leaf = null;
                //Make sure the following loop can't go on forever
                int attempts = 2 * leafs.Count;
                do
                {
                    leaf = leafs[_rand.Next(leafs.Count)];
                    attempts--;
                }while(leafs.Count > 1 && structs.Contains(leaf.Address) &&
                       attempts > 0);
                //Now we have a random leaf that is not a
                //structured neighbor to try to get a new neighbor with:
                if (leaf != null)
                {
                    target = GetSelfTarget();

                    /*
                     * This is the case of trying to find the nodes nearest
                     * to ourselves, use the Annealing routing to get connected
                     * more quickly
                     */
                    sender = new ForwardingSender(_node, leaf.Address, target);
                    //We are trying to connect to the two nearest nodes in one
                    //one attempt, so wait for two distinct responses:
                    desired_ctms = 2;
                    //This is a near neighbor connection
                    contype = STRUC_NEAR;
                }
            }

            if (structs.Count > 0 && sender == null)
            {
                /**
                 * We need left or right neighbors we send
                 * a ConnectToMessage in the directons we
                 * need.
                 */
                if (NeedLeftNeighbor)
                {
#if POB_DEBUG
                    Console.Error.WriteLine("NeedLeftNeighbor: {0}", _node.Address);
#endif
                    target = new DirectionalAddress(DirectionalAddress.Direction.Left);
                    short ttl = (short)DESIRED_NEIGHBORS;
                    sender  = new AHSender(_node, target, ttl, AHHeader.Options.Last);
                    contype = STRUC_NEAR;
                }
                else if (NeedRightNeighbor)
                {
#if POB_DEBUG
                    Console.Error.WriteLine("NeedRightNeighbor: {0}", _node.Address);
#endif
                    target = new DirectionalAddress(DirectionalAddress.Direction.Right);
                    short ttl = (short)DESIRED_NEIGHBORS;
                    sender  = new AHSender(_node, target, ttl, AHHeader.Options.Last);
                    contype = STRUC_NEAR;
                }
            }

            if (sender != null)
            {
                ConnectTo(sender, target, contype, desired_ctms);
            }
        }
示例#7
0
    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
    }
示例#8
0
    public void SMTest()
    {
      Address a = new DirectionalAddress(DirectionalAddress.Direction.Left);
      TransportAddress ta = TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:5000");
      NodeInfo ni = NodeInfo.CreateInstance(a, ta);
      
      //Test with one neighbor:
      ArrayList neighbors = new ArrayList();
      neighbors.Add(ni);
      StatusMessage sm1 = new StatusMessage(ConnectionType.Structured, neighbors);
      RoundTripHT(sm1);
      //Console.Error.WriteLine("\n{0}\n", sm1);
      //Test with many neighbors:
        
      for(int i = 5001; i < 5010; i++) {
        neighbors.Add(NodeInfo.CreateInstance(a,
				  TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:"
					  + i.ToString())));
      }
      StatusMessage sm2 = new StatusMessage(ConnectionType.Unstructured, neighbors);
      RoundTripHT(sm2);
      //Console.Error.WriteLine("\n{0}\n", sm2);
     
      //Here is a StatusMessage with no neighbors (that has to be a possibility)
      StatusMessage sm3 = new StatusMessage("structured", new ArrayList());
      RoundTripHT(sm3);
      //Console.Error.WriteLine("\n{0}\n", sm3);

    }