Exemplo n.º 1
0
        override public bool Equals(object o)
        {
            ForwardingSender fs = o as ForwardingSender;
            bool             eq = false;

            if (fs != null)
            {
                eq = fs.Destination.Equals(_dest);
            }
            return(eq);
        }
        /**
         * This method is called when a new Connection is added
         * to the ConnectionTable
         */
        protected void ConnectHandler(object contab, EventArgs eargs)
        {
            lock ( _sync ) {
                _last_connection_time   = DateTime.UtcNow;
                _current_retry_interval = _DEFAULT_RETRY_INTERVAL;
                _need_left  = -1;
                _need_right = -1;
            }

            if (IsActive == false)
            {
                return;
            }

            ConnectionEventArgs args    = (ConnectionEventArgs)eargs;
            Connection          new_con = args.Connection;

            new_con.StateChangeEvent += this.StatusChangedHandler;

            ConnectionList structs = null;

            if (new_con.MainType == ConnectionType.Structured)
            {
                structs = args.CList;
            }
            else
            {
                if (new_con.MainType == ConnectionType.Leaf)
                {
                    /*
                     * We just got a leaf.  Try to use it to get a shortcut.near
                     * This leaf could be connecting a new part of the network
                     * to us.  We try to connect to ourselves to make sure
                     * the network is connected:
                     */
                    Address target = GetSelfTarget();
                    //This is a near neighbor connection
                    ISender send = new ForwardingSender(_node, new_con.Address, target);
                    //Try to connect to the two nearest to us:
                    ConnectTo(send, target, STRUC_NEAR, 2);
                }
                structs = _node.ConnectionTable.GetConnections(ConnectionType.Structured);
            }
            ConnectToNearer(structs, new_con.Address, new_con.State.StatusMessage.Neighbors);
        }
        /**
         * Similar to the above except the forwarder is the same for all targets
         * @param cl ConnectionList of structs
         * @param forwarder the Node to forward through
         * @param ni an IEnumerable of NodeInfo objects representing neighbors
         * forwarder
         */
        protected void ConnectToNearer(ConnectionList cl, Address forwarder, IEnumerable ni)
        {
            Address nltarget;
            Address nrtarget;

            CheckForNearerNeighbors(cl, ni, out nltarget, out nrtarget);

            if (nrtarget != null)
            {
                ISender send = new ForwardingSender(_node, forwarder, nrtarget);
                ConnectTo(send, nrtarget, STRUC_NEAR, 1);
            }

            if (nltarget != null && !nltarget.Equals(nrtarget))
            {
                ISender send = new ForwardingSender(_node, forwarder, nltarget);
                ConnectTo(send, nltarget, STRUC_NEAR, 1);
            }
        }
Exemplo n.º 4
0
        public void Test()
        {
            RandomNumberGenerator rng     = new RNGCryptoServiceProvider();
            AHAddress             tmp_add = new AHAddress(rng);
            Node             n            = new StructuredNode(tmp_add, "unittest");
            AHSender         ah           = new AHSender(n, AddressParser.Parse("brunet:node:JOJZG7VO6RFOEZJ6CJJ2WOIJWTXRVRP4"));
            ForwardingSender fs           = new ForwardingSender(n,
                                                                 AddressParser.Parse("brunet:node:JOJZG7VO6RFOEZJ6CJJ2WOIJWTXRVRP4"),
                                                                 AddressParser.Parse("brunet:node:5FMQW3KKJWOOGVDO6QAQP65AWVZQ4VUQ"));

            string  uri = "sender:ah?dest=JOJZG7VO6RFOEZJ6CJJ2WOIJWTXRVRP4&mode=exact";
            ISender s   = SenderFactory.CreateInstance(n, uri);

            Assert.IsTrue(s is AHSender);
            Assert.AreEqual(uri, s.ToUri());
            uri = "sender:ah?dest=JOJZG7VO6RFOEZJ6CJJ2WOIJWTXRVRP4&mode=greedy";

            //Create the above programatically
            IDictionary <string, string> param_args = new Dictionary <string, string>();

            param_args["dest"] = "JOJZG7VO6RFOEZJ6CJJ2WOIJWTXRVRP4";
            param_args["mode"] = "greedy";
            string uri0 = SenderFactory.EncodeUri("ah", param_args);

            Assert.AreEqual(uri, uri0, "EncodeUri works");
            //Check decode:
            string scheme;

            param_args = SenderFactory.DecodeUri(uri, out scheme);
            Assert.AreEqual(scheme, "ah", "Scheme decoded");
            Assert.AreEqual(param_args.Count, 2, "2 parameters in uri");
            Assert.AreEqual(param_args["dest"], "JOJZG7VO6RFOEZJ6CJJ2WOIJWTXRVRP4", "Extracted address");
            Assert.AreEqual(param_args["mode"], "greedy", "got mode");

            s = SenderFactory.CreateInstance(n, uri);
            Assert.IsTrue(s is AHSender);
            Assert.AreEqual(uri, s.ToUri());
            string furi = "sender:fw?relay=JOJZG7VO6RFOEZJ6CJJ2WOIJWTXRVRP4&init_mode=greedy&dest=5FMQW3KKJWOOGVDO6QAQP65AWVZQ4VUQ&ttl=3&mode=path";

            s = SenderFactory.CreateInstance(n, furi);
            Assert.IsTrue(s is ForwardingSender);
            Assert.AreEqual(furi, s.ToUri());
        }
        /**
         * @param structs the ConnectionList to work with
         * @param target_to_for a mapping of Address -> Address, if we want to
         * connect to the key, the value should be the forwarder
         * @param neighs an IEnumerable of NodeInfo objects.
         */
        protected void ConnectToNearer(ConnectionList structs,
                                       IDictionary target_to_for, IEnumerable neighs)
        {
            Address nltarget;
            Address nrtarget;

            CheckForNearerNeighbors(structs, neighs, out nltarget, out nrtarget);

            if (nrtarget != null)
            {
                Address forwarder = (Address)target_to_for[nrtarget];
                ISender send      = new ForwardingSender(_node, forwarder, nrtarget);
                ConnectTo(send, nrtarget, STRUC_NEAR, 1);
            }

            if (nltarget != null && !nltarget.Equals(nrtarget))
            {
                Address forwarder = (Address)target_to_for[nltarget];
                ISender send      = new ForwardingSender(_node, forwarder, nltarget);
                ConnectTo(send, nltarget, STRUC_NEAR, 1);
            }
        }
Exemplo n.º 6
0
    public void Test() {
      RandomNumberGenerator rng = new RNGCryptoServiceProvider();      
      AHAddress tmp_add = new AHAddress(rng);
      Node n = new StructuredNode(tmp_add, "unittest");
      AHSender ah = new AHSender(n, AddressParser.Parse("brunet:node:JOJZG7VO6RFOEZJ6CJJ2WOIJWTXRVRP4"));
      ForwardingSender fs = new ForwardingSender(n, 
                                                 AddressParser.Parse("brunet:node:JOJZG7VO6RFOEZJ6CJJ2WOIJWTXRVRP4"),
                                                 AddressParser.Parse("brunet:node:5FMQW3KKJWOOGVDO6QAQP65AWVZQ4VUQ"));
      
      string uri = "sender:ah?dest=JOJZG7VO6RFOEZJ6CJJ2WOIJWTXRVRP4&mode=exact";
      ISender s = SenderFactory.CreateInstance(n, uri);
      Assert.IsTrue(s is AHSender);
      Assert.AreEqual(uri, s.ToUri());
      uri = "sender:ah?dest=JOJZG7VO6RFOEZJ6CJJ2WOIJWTXRVRP4&mode=greedy";
      
      //Create the above programatically
      IDictionary<string, string> param_args = new Dictionary<string,string>();
      param_args["dest"] = "JOJZG7VO6RFOEZJ6CJJ2WOIJWTXRVRP4";
      param_args["mode"] = "greedy";
      string uri0 = SenderFactory.EncodeUri("ah", param_args); 
      Assert.AreEqual(uri, uri0, "EncodeUri works");
      //Check decode:
      string scheme;
      param_args = SenderFactory.DecodeUri(uri, out scheme);
      Assert.AreEqual(scheme, "ah", "Scheme decoded");
      Assert.AreEqual(param_args.Count, 2, "2 parameters in uri");
      Assert.AreEqual(param_args["dest"], "JOJZG7VO6RFOEZJ6CJJ2WOIJWTXRVRP4", "Extracted address");
      Assert.AreEqual(param_args["mode"], "greedy", "got mode");

      s = SenderFactory.CreateInstance(n, uri);
      Assert.IsTrue(s is AHSender);
      Assert.AreEqual(uri, s.ToUri());      
      string furi = "sender:fw?relay=JOJZG7VO6RFOEZJ6CJJ2WOIJWTXRVRP4&init_mode=greedy&dest=5FMQW3KKJWOOGVDO6QAQP65AWVZQ4VUQ&ttl=3&mode=path";
      s = SenderFactory.CreateInstance(n, furi);
      Assert.IsTrue(s is ForwardingSender);
      Assert.AreEqual(furi, s.ToUri());
    }
Exemplo n.º 7
0
        /**
         * This handles the packet forwarding protocol
         */
        public void HandleData(MemBlock b, ISender ret_path, object state)
        {
            /*
             * Check it
             */
            AHSender ahs = ret_path as AHSender;

            if (ahs != null)
            {
                //This was an AHSender:

                /*
                 * This goes A -> B -> C
                 */
                if (b[0] == 0)
                {
                    int offset = 1;
                    //This is the first leg, going from A->B
                    Address add_c = AddressParser.Parse(b.Slice(offset, Address.MemSize));
                    offset += Address.MemSize;
                    //Since ahs a sender to return, we would be the source:
                    Address add_a = ahs.Destination;
                    short   ttl   = NumberSerializer.ReadShort(b, offset);          //2 bytes
                    offset += 2;
                    ushort options = (ushort)NumberSerializer.ReadShort(b, offset); //2 bytes
                    offset += 2;
                    MemBlock payload  = b.Slice(offset);
                    MemBlock f_header = MemBlock.Reference(new byte[] { 1 });

                    /*
                     * switch the packet from [A B f0 C] to [B C f 1 A]
                     */
                    ICopyable new_payload = new CopyList(PType.Protocol.Forwarding,
                                                         f_header, add_a, payload);

                    /*
                     * ttl and options are present in the forwarding header.
                     */
                    AHSender next = new AHSender(_n, ahs.ReceivedFrom, add_c,
                                                 ttl,
                                                 options);
                    next.Send(new_payload);
                }
                else if (b[0] == 1)
                {
                    /*
                     * This is the second leg: B->C
                     * Make a Forwarding Sender, and unwrap the inside packet
                     */
                    Address  add_a           = AddressParser.Parse(b.Slice(1, Address.MemSize));
                    Address  add_b           = ahs.Destination;
                    MemBlock rest_of_payload = b.Slice(1 + Address.MemSize);
                    //Here's the return path:
                    ISender new_ret_path = new ForwardingSender(_n, add_b, add_a);
                    _n.HandleData(rest_of_payload, new_ret_path, this);
                }
            }
            else
            {
                //This is not (currently) supported.
                Console.Error.WriteLine("Got a forwarding request from: {0}", ret_path);
            }
        }
    /**
     * Similar to the above except the forwarder is the same for all targets
     * @param cl ConnectionList of structs
     * @param forwarder the Node to forward through
     * @param ni an IEnumerable of NodeInfo objects representing neighbors
     * forwarder
     */
    protected void ConnectToNearer(ConnectionList cl, Address forwarder, IEnumerable ni)
    {
      Address nltarget;
      Address nrtarget;
      CheckForNearerNeighbors(cl, ni, out nltarget, out nrtarget);
      
      if( nrtarget != null ) {
        ISender send = new ForwardingSender(_node, forwarder, nrtarget);
        ConnectTo(send, nrtarget, STRUC_NEAR, 1);
      }

      if( nltarget != null && !nltarget.Equals(nrtarget) ) {
        ISender send = new ForwardingSender(_node, forwarder, nltarget);
        ConnectTo(send, nltarget, STRUC_NEAR, 1);
      }
    }
    /**
     * @param structs the ConnectionList to work with
     * @param target_to_for a mapping of Address -> Address, if we want to
     * connect to the key, the value should be the forwarder
     * @param neighs an IEnumerable of NodeInfo objects.
     */
    protected void ConnectToNearer(ConnectionList structs, 
                                   IDictionary target_to_for, IEnumerable neighs) {
      Address nltarget;
      Address nrtarget;
      CheckForNearerNeighbors(structs, neighs, out nltarget, out nrtarget);
      
      if( nrtarget != null ) {
        Address forwarder = (Address)target_to_for[nrtarget];
        ISender send = new ForwardingSender(_node, forwarder, nrtarget);
        ConnectTo(send, nrtarget, STRUC_NEAR, 1);
      }

      if( nltarget != null && !nltarget.Equals(nrtarget) ) {
        Address forwarder = (Address)target_to_for[nltarget];
        ISender send = new ForwardingSender(_node, forwarder, nltarget);
        ConnectTo(send, nltarget, STRUC_NEAR, 1);
      }
    }
    /**
     * This method is called when a new Connection is added
     * to the ConnectionTable
     */
    protected void ConnectHandler(object contab, EventArgs eargs)
    {
      lock( _sync ) {
        _last_connection_time = DateTime.UtcNow;
        _current_retry_interval = _DEFAULT_RETRY_INTERVAL;
        _need_left = -1;
        _need_right = -1;
      }

      if( IsActive == false ) {
        return;
      }

      ConnectionEventArgs args = (ConnectionEventArgs)eargs;
      Connection new_con = args.Connection;
      ConnectionList structs = null;

      if( new_con.MainType == ConnectionType.Structured ) {
        structs = args.CList;
      } else {
        if( new_con.MainType == ConnectionType.Leaf ) {
          /*
           * We just got a leaf.  Try to use it to get a shortcut.near
           * This leaf could be connecting a new part of the network
           * to us.  We try to connect to ourselves to make sure
           * the network is connected:
           */
          Address target = GetSelfTarget();
          //This is a near neighbor connection
          ISender send = new ForwardingSender(_node, new_con.Address, target);
          //Try to connect to the two nearest to us:
          ConnectTo(send, target, STRUC_NEAR, 2);
        }
        structs = _node.ConnectionTable.GetConnections(ConnectionType.Structured);
      }
      ConnectToNearer(structs, new_con.Address, new_con.Status.Neighbors);
    }
    ///////////////// 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);
            }
        }
Exemplo n.º 13
0
 /**
  * This handles the packet forwarding protocol
  */
 public void HandleData(MemBlock b, ISender ret_path, object state)
 {
   /*
    * Check it
    */
   AHSender ahs = ret_path as AHSender;
   if( ahs != null ) {
     //This was an AHSender:
     /*
      * This goes A -> B -> C
      */
     if( b[0] == 0 ) {
       int offset = 1;
       //This is the first leg, going from A->B
       Address add_c = AddressParser.Parse(b.Slice(offset, Address.MemSize));
       offset += Address.MemSize;
       //Since ahs a sender to return, we would be the source:
       Address add_a = ahs.Destination;
       short ttl = NumberSerializer.ReadShort(b, offset);//2 bytes
       offset += 2;
       ushort options = (ushort) NumberSerializer.ReadShort(b, offset);//2 bytes
       offset += 2;
       MemBlock payload = b.Slice(offset);
       MemBlock f_header = MemBlock.Reference( new byte[]{1} );
       /*
        * switch the packet from [A B f0 C] to [B C f 1 A]
        */
       ICopyable new_payload = new CopyList(PType.Protocol.Forwarding,
                                        f_header, add_a, payload);
       /*
        * ttl and options are present in the forwarding header.
        */
       AHSender next = new AHSender(_n, ahs.ReceivedFrom, add_c,
                                    ttl,
                                    options); 
       next.Send(new_payload);
     }
     else if ( b[0] == 1 ) {
       /*
        * This is the second leg: B->C
        * Make a Forwarding Sender, and unwrap the inside packet
        */
       Address add_a = AddressParser.Parse(b.Slice(1, Address.MemSize));
       Address add_b = ahs.Destination;
       MemBlock rest_of_payload = b.Slice(1 + Address.MemSize);
       //Here's the return path:
       ISender new_ret_path = new ForwardingSender(_n, add_b, add_a);
       _n.HandleData(rest_of_payload, new_ret_path, this);
     }
   }
   else {
     //This is not (currently) supported.
     Console.Error.WriteLine("Got a forwarding request from: {0}", ret_path);
   }
 }
Exemplo n.º 14
0
 public object[] forwardingproxy(string relay, int init_option, string dest, int ttl, int ahOptions, int maxResultsToWait, string method, params object[] args) {
   Address forwarder = AddressParser.Parse(relay);
   Address target = AddressParser.Parse(dest);
   ForwardingSender s = new ForwardingSender(_node, forwarder, (ushort) init_option, target, (short) ttl, (ushort)ahOptions);
   return this.Proxy(s, maxResultsToWait, method, args);
 }