Inheritance: StructuredAddress
Esempio n. 1
0
 public SubringTransportAddress(AHAddress target, string node_ns) :
   base(string.Format("brunet.{0}://{1}.{2}", TATypeToString(TAType.Subring),
         target.ToMemBlock().ToBase32String(), node_ns))
 {
   Target = target;
   Namespace = node_ns;
 }
Esempio n. 2
0
        /// <summary>Continues a broadcast to the overlay.</summary>
        public BroadcastSender(StructuredNode node, AHAddress source,
                               AHAddress from, AHAddress to, int forwarders, int hops)
        {
            Node             = node;
            Source           = source;
            From             = from;
            To               = to;
            _distance_sorter = new AbsoluteDistanceComparer(node.Address as AHAddress);
            _address_sorter  = new LeftDistanceComparer(node.Address as AHAddress);
            Forwarders       = forwarders;
            Hops             = hops;

            byte[] hops_data = new byte[4];
            NumberSerializer.WriteInt(hops, hops_data, 0);
            _hops = MemBlock.Reference(hops_data);

            if (forwarders == DEFAULT_FORWARDERS)
            {
                _forwarders = DEFAULT_FORWARDERS_MB;
            }
            else
            {
                byte[] def = new byte[4];
                NumberSerializer.WriteInt(forwarders, def, 0);
                _forwarders = MemBlock.Reference(def);
            }
        }
Esempio n. 3
0
            public int Compare(Connection x, Connection y)
            {
                AHAddress xaddr = x.Address as AHAddress;
                AHAddress yaddr = y.Address as AHAddress;

                if (xaddr == null || y == null)
                {
                    throw new Exception("Invalid comparison");
                }

                BigInteger xdist = _local.DistanceTo(xaddr).abs();
                BigInteger ydist = _local.DistanceTo(yaddr).abs();

                if (xdist < ydist)
                {
                    return(-1);
                }
                else if (ydist < xdist)
                {
                    return(1);
                }
                else
                {
                    return(0);
                }
            }
Esempio n. 4
0
        public void Test()
        {
            RandomNumberGenerator rng = new RNGCryptoServiceProvider();
            TargetSelector        ts  = new DefaultTargetSelector();

            //in this case we set the current to an address in our list
            for (int i = 0; i < _addr_list.Length; i++)
            {
                AHAddress tmp_addr = new AHAddress(rng);
                _addr_list[i] = tmp_addr;
            }
            _idx = 0;
            for (int i = 0; i < _addr_list.Length; i++)
            {
                ts.ComputeCandidates(new AHAddress(rng), 10, TargetSelectorCallback, _addr_list[i]);
            }

            //in this case we set the current address to null
            for (int i = 0; i < _addr_list.Length; i++)
            {
                AHAddress tmp_addr = new AHAddress(rng);
                _addr_list[i] = tmp_addr;
            }
            _idx = 0;
            for (int i = 0; i < _addr_list.Length; i++)
            {
                ts.ComputeCandidates(_addr_list[i], 10, TargetSelectorCallback, null);
            }
        }
Esempio n. 5
0
        /**
         * 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);
        }
 public SubringTransportAddress(AHAddress target, string node_ns) :
     base(string.Format("brunet.{0}://{1}.{2}", TATypeToString(TAType.Subring),
                        target.ToMemBlock().ToBase32String(), node_ns))
 {
     Target    = target;
     Namespace = node_ns;
 }
Esempio n. 7
0
        public StructuredNode(AHAddress add, string realm) : base(add, realm)
        {
            /**
             * Here are the ConnectionOverlords
             */
            _leafco = new LeafConnectionOverlord(this);
            AddConnectionOverlord(_leafco);
            _snco = new StructuredNearConnectionOverlord(this);
            AddConnectionOverlord(_snco);
            _ssco = new StructuredShortcutConnectionOverlord(this);
            AddConnectionOverlord(_ssco);
#if !BRUNET_SIMULATOR
            _iphandler = new IPHandler();
            _iphandler.Subscribe(this, null);
            AddTADiscovery(new LocalDiscovery(this, Realm, _rpc, _iphandler));
#endif

            /**
             * Turn on some protocol support :
             */
            /// Turn on Packet Forwarding Support :
            GetTypeSource(PType.Protocol.Forwarding).Subscribe(new PacketForwarder(this), null);
            //Handles AHRouting:
            GetTypeSource(PType.Protocol.AH).Subscribe(new AHHandler(this), this);
            GetTypeSource(PType.Protocol.Echo).Subscribe(new EchoHandler(), this);

            //Add the standard RPC handlers:
            _rpc.AddHandler("sys:ctm", new CtmRequestHandler(this));
            sys_link = new ConnectionPacketHandler(this);
            _rpc.AddHandler("sys:link", sys_link);
            _rpc.AddHandler("trace", new TraceRpcHandler(this));
            //Serve some public information about our ConnectionTable
            _rpc.AddHandler("ConnectionTable", new ConnectionTableRpc(ConnectionTable, _rpc));
            //Add a map-reduce handlers:
            _mr_handler = new MapReduceHandler(this);
            //Subscribe it with the RPC handler:
            _rpc.AddHandler("mapreduce", _mr_handler);

            //Subscribe map-reduce tasks
            _mr_handler.SubscribeTask(new MapReduceTrace(this));
            _mr_handler.SubscribeTask(new MapReduceRangeCounter(this));


            /*
             * Handle Node state changes.
             */
            StateChangeEvent += delegate(Node n, Node.ConnectionState s) {
                if (s == Node.ConnectionState.Leaving)
                {
                    //Start our StructuredNode specific leaving:
                    Leave();
                }
            };

            _connection_table.ConnectionEvent    += new EventHandler(this.EstimateSize);
            _connection_table.ConnectionEvent    += new EventHandler(this.UpdateNeighborStatus);
            _connection_table.DisconnectionEvent += new EventHandler(this.EstimateSize);
            _connection_table.DisconnectionEvent += new EventHandler(this.UpdateNeighborStatus);
        }
Esempio n. 8
0
 /**
  * @param zero the address to use as the zero in the space
  */
 public ConnectionRightComparer(Address zero)
 {
     byte[] binzero = new byte[Address.MemSize];
     zero.CopyTo(binzero);
     //Make sure the last bit is zero, so the address is class 0
     Address.SetClass(binzero, 0);
     _zero = new AHAddress(MemBlock.Reference(binzero, 0, Address.MemSize));
 }
Esempio n. 9
0
        public AnnealingRouting(AHAddress local, ConnectionList structured_cons)
        {
            //Fake connection to ourselves:
            _local_con = new Connection(null, local, "structured.self", null, null);
            int local_idx;

            _structs = ConnectionList.InsertInto(structured_cons, _local_con, out local_idx);
        }
Esempio n. 10
0
    public StructuredNode(AHAddress add, string realm):base(add,realm)
    {
      /**
       * Here are the ConnectionOverlords
       */ 
      _leafco = new LeafConnectionOverlord(this);
      AddConnectionOverlord(_leafco);
      _snco = new StructuredNearConnectionOverlord(this);
      AddConnectionOverlord(_snco);
      _ssco = new StructuredShortcutConnectionOverlord(this);
      AddConnectionOverlord(_ssco);
#if !BRUNET_SIMULATOR
      _iphandler = new IPHandler();
      _iphandler.Subscribe(this, null);
      AddTADiscovery(new LocalDiscovery(this, Realm, _rpc, _iphandler));
#endif

      /**
       * Turn on some protocol support : 
       */
      /// Turn on Packet Forwarding Support :
      GetTypeSource(PType.Protocol.Forwarding).Subscribe(new PacketForwarder(this), null);
      AHHandler = new AHHandler(this);
      GetTypeSource(PType.Protocol.AH).Subscribe(AHHandler, this);
      GetTypeSource(PType.Protocol.Echo).Subscribe(new EchoHandler(), this);
      
      //Add the standard RPC handlers:
      _rpc.AddHandler("sys:ctm", new CtmRequestHandler(this));
      sys_link = new ConnectionPacketHandler(this);
      _rpc.AddHandler("sys:link", sys_link);
      _rpc.AddHandler("trace", new TraceRpcHandler(this));
      //Serve some public information about our ConnectionTable
      _rpc.AddHandler("ConnectionTable", new ConnectionTableRpc(ConnectionTable, _rpc));
      //Add a map-reduce handlers:
      _mr_handler = new MapReduceHandler(this);
      //Subscribe it with the RPC handler:
      _rpc.AddHandler("mapreduce", _mr_handler);

      //Subscribe map-reduce tasks
      _mr_handler.SubscribeTask(new MapReduceTrace(this));
      _mr_handler.SubscribeTask(new MapReduceRangeCounter(this));

      
      /*
       * Handle Node state changes.
       */
      StateChangeEvent += delegate(Node n, Node.ConnectionState s) {
        if( s == Node.ConnectionState.Leaving ) {
          //Start our StructuredNode specific leaving:
          Leave();
        }
      };

      _connection_table.ConnectionEvent += new EventHandler(this.EstimateSize);
      _connection_table.ConnectionEvent += new EventHandler(this.UpdateNeighborStatus);
      _connection_table.DisconnectionEvent += new EventHandler(this.EstimateSize);
      _connection_table.DisconnectionEvent += new EventHandler(this.UpdateNeighborStatus);
    }
Esempio n. 11
0
    // adds a disconnected pair to the pool
    public void AddDisconnectedPair(out Address address1, out Address address2, bool nctunnel)
    {
      address1 = new AHAddress(new RNGCryptoServiceProvider());
      byte[] addrbuff = Address.ConvertToAddressBuffer(address1.ToBigInteger() + (Address.Full / 2));
      Address.SetClass(addrbuff, AHAddress._class);
      address2 = new AHAddress(addrbuff);

      AddDisconnectedPair(address1, address2, nctunnel);
    }
Esempio n. 12
0
        /**
         * This is a recursive function over the network
         * It helps to build an IList of IDictionary types that give the address
         * of each node in the path, and the connection to the next closest node if
         * there is one, otherwise no next.
         */
        protected void DoTraceRouteTo(AHAddress a, object req_state)
        {
            /*
             * First find the Connection pointing to the node closest to dest, if
             * there is one closer than us
             */

            ConnectionTable tab          = _node.ConnectionTable;
            ConnectionList  structs      = tab.GetConnections(ConnectionType.Structured);
            Connection      next_closest = structs.GetNearestTo((AHAddress)_node.Address, a);
            //Okay, we have the next closest:
            ListDictionary my_entry = new ListDictionary();

            my_entry["node"] = _node.Address.ToString();
            if (next_closest != null)
            {
                my_entry["next_con"] = next_closest.ToString();
                Channel result = new Channel();
                //We only want one result, so close the queue after we get the first
                result.CloseAfterEnqueue();
                result.CloseEvent += delegate(object o, EventArgs args) {
                    Channel q = (Channel)o;
                    if (q.Count > 0)
                    {
                        try {
                            RpcResult rres    = (RpcResult)q.Dequeue();
                            IList     l       = (IList)rres.Result;
                            ArrayList results = new ArrayList(l.Count + 1);
                            results.Add(my_entry);
                            results.AddRange(l);
                            _rpc.SendResult(req_state, results);
                        }
                        catch (Exception x) {
                            string    m  = String.Format("<node>{0}</node> trying <connection>{1}</connection> got <exception>{2}</exception>", _node.Address, next_closest, x);
                            Exception nx = new Exception(m);
                            _rpc.SendResult(req_state, nx);
                        }
                    }
                    else
                    {
                        //We got no results.
                        IList l = new ArrayList(1);
                        l.Add(my_entry);
                        _rpc.SendResult(req_state, l);
                    }
                };
                _rpc.Invoke(next_closest.State.Edge, result, "trace.GetRouteTo", a.ToString());
            }
            else
            {
                //We are the end of the line, send the result:
                ArrayList l = new ArrayList();
                l.Add(my_entry);
                _rpc.SendResult(req_state, l);
            }
        }
Esempio n. 13
0
            public AHState(AHAddress local, ConnectionList structs, ConnectionList leafs)
            {
                Leafs   = leafs;
                Structs = structs;
                Local   = local;

                _directional = new DirectionalRouting(local, structs);
                _greedy      = new GreedyRouting(local, structs);
                _annealing   = new AnnealingRouting(local, structs);
            }
Esempio n. 14
0
            protected AHState(AHState old_state, ConnectionList leafs)
            {
                Leafs   = leafs;
                Structs = old_state.Structs;
                Local   = old_state.Local;

                _directional = old_state._directional;
                _greedy      = old_state._greedy;
                _annealing   = old_state._annealing;
            }
Esempio n. 15
0
        public GreedyRouting(AHAddress local, ConnectionList structured_cons)
        {
            //Fake connection to ourselves:
            _local_con = new Connection(null, local, "structured.self", null, null);
            int local_idx;

            _structs = ConnectionList.InsertInto(structured_cons, _local_con, out local_idx);
            _NO_ONE  = new Pair <Connection, bool>(null, false);
            _LOCAL   = new Pair <Connection, bool>(null, true);
        }
        public SubringTransportAddress(string s) : base(s)
        {
            int addr_start = s.IndexOf(":") + 3;
            int addr_end   = s.IndexOf(".", addr_start);
            int ns_start   = addr_end + 1;
            int ns_end     = Math.Max(s.Length, s.IndexOf("/", ns_start));

            byte[] addr = Base32.Decode(s.Substring(addr_start, addr_end - addr_start));
            Target    = new AHAddress(MemBlock.Reference(addr));
            Namespace = s.Substring(ns_start, ns_end - ns_start);
        }
Esempio n. 17
0
        /// <summary>Calculate the Address immediately to the right.</summary>
        public static AHAddress GetRightNearTarget(AHAddress address)
        {
            BigInteger local_int_add = address.ToBigInteger();

            //must have even addresses so increment twice
            local_int_add += 2;
            //Make sure we don't overflow:
            BigInteger tbi = new BigInteger(local_int_add % Address.Full);

            return(new AHAddress(tbi));
        }
Esempio n. 18
0
    public SubringTransportAddress(string s) : base(s)
    {
      int addr_start = s.IndexOf(":") + 3;
      int addr_end = s.IndexOf(".", addr_start);
      int ns_start = addr_end + 1;
      int ns_end = Math.Max(s.Length, s.IndexOf("/", ns_start));

      byte[] addr = Base32.Decode(s.Substring(addr_start, addr_end - addr_start));
      Target = new AHAddress(MemBlock.Reference(addr));
      Namespace = s.Substring(ns_start, ns_end - ns_start);
    }
Esempio n. 19
0
    public GraphNode(AHAddress addr)
    {
      Shortcuts = 0;
      Address = addr;
      ConnectionTable = new ConnectionTable(addr);
      UpdateSystem();

      do {
        UniqueID = _rand.Next();
      } while(_unique_allocations.ContainsKey(UniqueID));
      _unique_allocations[UniqueID] = UniqueID;
    }
Esempio n. 20
0
 /// <summary>Stores data about this hop in a broadcast and provides a path
 /// back to the originating broadcaster.</summary>
 public BroadcastReceiver(BroadcastSender bs)
 {
   Node = bs.Node;
   From = bs.From;
   To = bs.To;
   Source = bs.Source;
   Hops = bs.Hops;
   _bs = bs;
   _sender = new AHSender(Node, Source,
       AHSender.DefaultTTLFor(Node.NetworkSize),
       AHHeader.Options.Exact);
 }
Esempio n. 21
0
 /// <summary>Stores data about this hop in a broadcast and provides a path
 /// back to the originating broadcaster.</summary>
 public BroadcastReceiver(BroadcastSender bs)
 {
     Node    = bs.Node;
     From    = bs.From;
     To      = bs.To;
     Source  = bs.Source;
     Hops    = bs.Hops;
     _bs     = bs;
     _sender = new AHSender(Node, Source,
                            AHSender.DefaultTTLFor(Node.NetworkSize),
                            AHHeader.Options.Exact);
 }
Esempio n. 22
0
  /**
   * This is a recursive function over the network
   * It helps to build an IList of IDictionary types that give the address
   * of each node in the path, and the connection to the next closest node if
   * there is one, otherwise no next.
   */
  protected void DoTraceRouteTo(AHAddress a, object req_state) {
    /*
     * First find the Connection pointing to the node closest to dest, if
     * there is one closer than us
     */

    ConnectionTable tab = _node.ConnectionTable;
    ConnectionList structs = tab.GetConnections(ConnectionType.Structured);
    Connection next_closest = structs.GetNearestTo((AHAddress) _node.Address, a);
    //Okay, we have the next closest:
    ListDictionary my_entry = new ListDictionary();
    my_entry["node"] = _node.Address.ToString();
    if( next_closest != null ) {
      my_entry["next_con"] = next_closest.ToString();
      Channel result = new Channel();
      //We only want one result, so close the queue after we get the first
      result.CloseAfterEnqueue();
      result.CloseEvent += delegate(object o, EventArgs args) {
        Channel q = (Channel)o;
        if( q.Count > 0 ) {
          try {
            RpcResult rres = (RpcResult)q.Dequeue();
            IList l = (IList) rres.Result;
            ArrayList results = new ArrayList( l.Count + 1);
            results.Add(my_entry);
            results.AddRange(l);
            _rpc.SendResult(req_state, results);
          }
          catch(Exception x) {
            string m = String.Format("<node>{0}</node> trying <connection>{1}</connection> got <exception>{2}</exception>", _node.Address, next_closest, x);
            Exception nx = new Exception(m);
            _rpc.SendResult(req_state, nx);
          }
        }
        else {
          //We got no results.
          IList l = new ArrayList(1);
          l.Add( my_entry );
          _rpc.SendResult(req_state, l);
        }
      };
      _rpc.Invoke(next_closest.State.Edge, result, "trace.GetRouteTo", a.ToString());
    }
    else {
      //We are the end of the line, send the result:
      ArrayList l = new ArrayList();
      l.Add(my_entry);
      _rpc.SendResult(req_state, l);  
    }
  }
        /// Determine if there are any unuseful STRUC_NEAR that we can trim
        protected void TrimConnections()
        {
            ConnectionTable tab             = _node.ConnectionTable;
            ConnectionList  cons            = tab.GetConnections(ConnectionType.Structured);
            ArrayList       trim_candidates = new ArrayList();

            foreach (Connection c in cons)
            {
                if (!c.ConType.Equals(STRUC_NEAR))
                {
                    continue;
                }

                int left_pos  = cons.LeftInclusiveCount(_node.Address, c.Address);
                int right_pos = cons.RightInclusiveCount(_node.Address, c.Address);

                if (right_pos >= 2 * DESIRED_NEIGHBORS && left_pos >= 2 * DESIRED_NEIGHBORS)
                {
                    //These are near neighbors that are not so near
                    trim_candidates.Add(c);
                }
            }

            if (trim_candidates.Count == 0)
            {
                return;
            }

            //Delete a farthest trim candidate:
            BigInteger biggest_distance = new BigInteger(0);
            BigInteger tmp_distance     = new BigInteger(0);
            Connection to_trim          = null;

            foreach (Connection tc in trim_candidates)
            {
                AHAddress t_ah_add = (AHAddress)tc.Address;
                tmp_distance = t_ah_add.DistanceTo((AHAddress)_node.Address).abs();
                if (tmp_distance > biggest_distance)
                {
                    biggest_distance = tmp_distance;
                    //Console.Error.WriteLine("...finding far distance for trim: {0}",biggest_distance.ToString() );
                    to_trim = tc;
                }
            }

#if POB_DEBUG
            Console.Error.WriteLine("Attempt to trim Near: {0}", to_trim);
#endif
            _node.GracefullyClose(to_trim.Edge, "SCO, near connection trim");
        }
Esempio n. 24
0
        /** 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);
        }
Esempio n. 25
0
        public void Send(ICopyable data)
        {
            ConnectionList cl = Node.ConnectionTable.GetConnections(ConnectionType.Structured);
            // We start with the node immediately after the starting index
            int start = cl.IndexOf(From);

            if (start < 0)
            {
                start = ~start;
            }

            // We end with the node immediately before the end index
            int end = cl.IndexOf(To);

            if (end < 0)
            {
                end = ~end;
            }

            // If start >= end, because From < To or because this is a terminal
            // node and there are no other entities to forward it to.  So the
            // second test ensures that the first entity is actually inside the
            // range to forward it to.
            AHAddress start_addr = cl[start].Address as AHAddress;

            if (start >= end && start_addr.IsBetweenFromLeft(From, To))
            {
                end += cl.Count;
            }

            List <Connection> cons = SortByDistance(cl, start, end, Forwarders);

            for (int i = 0; i < cons.Count; i++)
            {
                Connection con   = cons[i];
                int        next  = i + 1;
                AHAddress  nfrom = con.Address as AHAddress;
                Address    nto   = To;
                if (next < cons.Count)
                {
                    nto = GetLeftNearTarget(cons[next].Address as AHAddress);
                }
                con.Edge.Send(new CopyList(PType, Source.ToMemBlock(),
                                           nfrom.ToMemBlock(), nto.ToMemBlock(), _forwarders, _hops, data));
            }

            _sent_to = cons.Count;
        }
Esempio n. 26
0
        public void Test()
        {
            AHAddress  a1 = new AHAddress(Address.Full - 2);
            AHAddress  a2 = new AHAddress(Address.Half - 2);
            AHAddress  a3 = new AHAddress(Address.Half + 2);
            Connection c1 = new Connection(null, a1, "struectured", null, null);
            Connection c2 = new Connection(null, a2, "struectured", null, null);
            Connection c3 = new Connection(null, a3, "struectured", null, null);
            ConnectionRightComparer cmp = new ConnectionRightComparer();

            //The default zero is half, since a1 is half, it is zero,
            //the below should all be true:
            Assert.IsTrue(cmp.Compare(c1, c2) > 0, "Biggest is farther than half -2");
            Assert.IsTrue(cmp.Compare(c3, c2) > 0, "half +2 is farther than half -2");
            Assert.IsTrue(cmp.Compare(c3, c1) > 0, "half +2 is farther than Biggest");
        }
Esempio n. 27
0
        public int Compare(Connection x, Connection y)
        {
            //Equals is fast to check, lets do it before we
            //do more intense stuff :
            if (x.Equals(y))
            {
                return(0);
            }

            if ((x.Address is AHAddress) && (y.Address is AHAddress))
            {
                AHAddress add_x = (AHAddress)x.Address;
                AHAddress add_y = (AHAddress)y.Address;

                /**
                 * We compute the distances with the given zero
                 * dist_x : distance from zero to x
                 * dist_y : distance from zero to y
                 *
                 * The AHAddress.RightDistanceTo function gives
                 * the distance as measured from the node in count-clockwise.
                 *
                 * We can use this to set the "zero" we want :
                 */
                BigInteger dist_x = _zero.RightDistanceTo(add_x);
                BigInteger dist_y = _zero.RightDistanceTo(add_y);
                //Since we know they are not equal, either n_x is bigger
                //that n_y or vice-versa :
                if (dist_x > dist_y)
                {
                    //Then dist_x - dist_y > 0, and n_x is the bigger
                    return(1);
                }
                else
                {
                    return(-1);
                }
            }
            else
            {
                /**
                 * If addresses are not AHAddress, throw an exception
                 */
                throw new Exception(
                          String.Format("The addresses are not AHAddress. Only AHAddress can be compared."));
            }
        }
Esempio n. 28
0
        public void Test()
        {
            Address           a1  = new AHAddress(Address.Half);
            Address           a2  = new AHAddress(MemBlock.Reference(new byte[Address.MemSize], 0, Address.MemSize));
            Address           a3  = new AHAddress(Address.Full - 2);
            AHAddressComparer cmp = new AHAddressComparer();

            //The default zero is half, since a1 is half, it is zero,
            //the below should all be true:
            Assert.IsTrue(cmp.Compare(a1, a2) > 0, "Half is greater than 0");
            Assert.IsTrue(cmp.Compare(a3, a2) > 0, "Biggest is greater than 0");
            Assert.IsTrue(cmp.Compare(a3, a1) > 0, "Biggest is greater than half");
            Assert.IsTrue(a1.CompareTo(a2) == cmp.Compare(a1, a2),
                          "CompareTo and Compare 1");
            Assert.IsTrue(a1.CompareTo(a3) == cmp.Compare(a1, a3),
                          "CompareTo and Compare 2");
        }
Esempio n. 29
0
        /// <summary>Where data packets prepended with a prepended subring come.
        /// Here we receive data as well as create new SubringEdges.</summary>
        public void HandleData(MemBlock data, ISender return_path, object state)
        {
            AHSender from = return_path as AHSender;

            if (from == null)
            {
                return;
            }

            AHAddress target = (AHAddress)from.Destination;
            MemBlock  payload;
            int       local_id, remote_id;

            _it.Parse(data, out payload, out local_id, out remote_id);

            IIdentifierPair ip;
            SubringEdge     se;

            if (_it.TryGet(local_id, remote_id, out ip))
            {
                se = ip as SubringEdge;
            }
            else if (local_id == 0)
            {
                if (!from.Node.Realm.Equals(_shared_node.Realm))
                {
                    // We don't have matching realms
                    return;
                }
                var rem_sta = new SubringTransportAddress(target, from.Node.Realm);
                se = new SubringEdge(_local_ta, rem_sta, true, from, _ptype);
                _it.Add(se);
                se.RemoteID    = remote_id;
                se.CloseEvent += CloseHandler;
                SendEdgeEvent(se);
            }
            else
            {
                // Probably an edge closed earlier...
                return;
            }

            se.ReceivedPacketEvent(payload);
        }
Esempio n. 30
0
        /**
         * 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);
        }
Esempio n. 31
0
        /**
         * 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);
        }
Esempio n. 32
0
        /**
         * Sends a StatusMessage request (local node) to the nearest right and
         * left neighbors (in the local node's ConnectionTable) of the new Connection.
         */
        protected void UpdateNeighborStatus(object contab, EventArgs args)
        {
            ConnectionEventArgs cea = (ConnectionEventArgs)args;

            if (cea.ConnectionType != ConnectionType.Structured)
            {
                //We don't do anything,
                return;
            }

            //This is the list we had when things changed
            ConnectionList structs = cea.CList;

            //structs is constant
            if (structs.Count == 0)
            {
                //There is no one to talk to
                return;
            }

            /*
             * Get the data we need about this connection:
             */
            Connection con         = cea.Connection;
            AHAddress  new_address = (AHAddress)con.Address;

            /*
             * Update the left neighbor:
             */
            Connection lc = structs.GetLeftNeighborOf(new_address);

            EnqueueAction(new UpdateNeighborAction(this, con.ConType, lc));

            /*
             * Update the right neighbor:
             */
            Connection rc = structs.GetRightNeighborOf(new_address);

            if (lc != rc)
            {
                EnqueueAction(new UpdateNeighborAction(this, con.ConType, rc));
            }
        }
Esempio n. 33
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());
        }
Esempio n. 34
0
        public void Test()
        {
            TransportAddress ta =
                TransportAddressFactory.CreateInstance("brunet.tcp://169.0.5.1:5000");
            FakeEdge fe   = new FakeEdge(ta, ta);
            var      rng  = new System.Security.Cryptography.RNGCryptoServiceProvider();
            var      cons = new List <Connection>();

            for (int i = 0; i < 50; i++)
            {
                var addr = new AHAddress(rng);
                cons.Add(new Connection(fe, addr, "structured", null, null));
            }

            var start_addr = new AHAddress(rng);
            IComparer <Connection> distance_comparer =
                new BroadcastSender.AbsoluteDistanceComparer(start_addr);

            cons.Sort(distance_comparer);
            BigInteger current_distance = new BigInteger(0);

            foreach (Connection con in cons)
            {
                AHAddress  addr          = con.Address as AHAddress;
                BigInteger next_distance = start_addr.DistanceTo(addr).abs();
                Assert.IsTrue(current_distance < next_distance, "DistanceComparer");
                current_distance = next_distance;
            }

            IComparer <Connection> address_comparer =
                new BroadcastSender.LeftDistanceComparer(start_addr);

            cons.Sort(address_comparer);
            current_distance = new BigInteger(0);
            foreach (Connection con in cons)
            {
                AHAddress  addr          = con.Address as AHAddress;
                BigInteger next_distance = start_addr.LeftDistanceTo(addr).abs();
                Assert.IsTrue(current_distance < next_distance, "AddressComparer");
                current_distance = next_distance;
            }
        }
Esempio n. 35
0
        public DirectionalRouting(AHAddress local, ConnectionList structs)
        {
            _NULL_TRUE  = new Pair <Connection, bool>(null, true);
            _NULL_FALSE = new Pair <Connection, bool>(null, false);
            Connection left_c  = null;
            Connection right_c = null;

            if (structs.Count > 0)
            {
                int local_idx = ~structs.IndexOf(local);
                int left_idx  = local_idx;
                int right_idx = left_idx - 1;
                left_c  = structs[left_idx];
                right_c = structs[right_idx];
            }
            _LEFT_FALSE  = new Pair <Connection, bool>(left_c, false);
            _RIGHT_FALSE = new Pair <Connection, bool>(right_c, false);
            _LEFT_TRUE   = new Pair <Connection, bool>(left_c, true);
            _RIGHT_TRUE  = new Pair <Connection, bool>(right_c, true);
        }
Esempio n. 36
0
        /** Utility method to determine if this address is between start and end
         *  from the right, i.e. its satisfies the following constraints:
         *  1. Is to the right of start, and
         *  2. Is to the left of end
         *  @return 1 in case its within
         *  @return -1 in case it is not
         */
        public bool IsBetweenFromRight(AHAddress start, AHAddress end)
        {
            int se_comp = start.CompareTo(end);

            //simple case of no wrap around where "within" is lesser
            if (se_comp > 0)
            {
                return(start.CompareTo(this) > 0 && this.CompareTo(end) > 0);
            }
            else if (se_comp == 0)
            {
                //When start == end, nothing is between them
                return(false);
            }
            else
            {
                //in case there is a wrap around
                //"within" has become greater than "this"
                return(start.CompareTo(this) > 0 || this.CompareTo(end) > 0);
            }
        }
Esempio n. 37
0
        /// <summary>Creates a new BroadcastSender based upon a packet sent from
        /// one.  This makes the recursion step easier.</summary>
        public static BroadcastSender Parse(StructuredNode node, MemBlock packet,
                                            out MemBlock data)
        {
            int       pos    = 0;
            AHAddress source = new AHAddress(packet.Slice(pos, Address.MemSize));

            pos += Address.MemSize;
            AHAddress from = new AHAddress(packet.Slice(pos, Address.MemSize));

            pos += Address.MemSize;
            AHAddress to = new AHAddress(packet.Slice(pos, Address.MemSize));

            pos += Address.MemSize;
            int forwarders = NumberSerializer.ReadInt(packet, pos);

            pos += 4;
            int hops = NumberSerializer.ReadInt(packet, pos) + 1;

            pos += 4;
            data = packet.Slice(pos);
            return(new BroadcastSender(node, source, from, to, forwarders, hops));
        }
Esempio n. 38
0
 /**
 <summary>Reassign range info(Start and End) based on recalculated range.</summary>
 <param name = "rg_size">Current range size(round distance between start address and end address of this Entry).</param>
 <remarks>new_start = mid - rg_size/2, new_end = mid + rg_size/2 </remarks>
  */
 public Entry ReAssignRange(BigInteger rg_size) {
   // calculate middle address of range
   BigInteger start_int = Start.ToBigInteger();
   BigInteger end_int = End.ToBigInteger();
   BigInteger mid_int =  (start_int + end_int) / 2;  
   if (mid_int % 2 == 1) { mid_int = mid_int -1; }
   AHAddress mid_addr = new AHAddress(mid_int);
   /*
    * If we have a case where start -> end includes zero,
    * this is the wrap around.  So, we can imagine that
    * we have end' = end + Address.Full.  So,
    * mid' = (start + end')/2 = (start + end)/2 + Address.Full/2
           = (start + end)/ 2 + Address.Half
    */
   if (!mid_addr.IsBetweenFromLeft(Start, End)) {
     mid_int += Address.Half;
   }
   //addresses for new range
   BigInteger rg_half = rg_size / 2;
   if (rg_half % 2 == 1) { rg_half -= 1; }
   AHAddress n_a = new AHAddress(mid_int - rg_half);
   AHAddress n_b = new AHAddress(mid_int + rg_half);
   return new Entry(Content, Alpha, n_a, n_b);
 }
Esempio n. 39
0
    /**
     * Generates tree for bounded broadcast. Algorithm works as follows:
     * The goal is to broadcast to all nodes in range (start, end).
     * Given a range (a, b), determine all connections that belong to this range.
     * Let the left connections be l_1, l_2, ..... l_n.
     * Let the right connections be r_1, r_2, ... , r_n.
     * To left connection l_i assign the range [b_{i-1}, b_i).
     * To right connection r_i assign the range [r_i, r_{i-1}]
     * To the connection ln assign range [l_{n-1}, end)
     * To the connection rn assign range (start, r_{n-1}]
     */
    public override void GenerateTree(Channel q, MapReduceArgs mr_args)  
    {
      ArrayList gen_list = mr_args.GenArg as ArrayList;
      string start_range = gen_list[0] as string;
      AHAddress start_addr = (AHAddress) AddressParser.Parse(start_range);
      AHAddress end_addr;
      string end_range;
      /// If users do not specify an end range, this method understands 
      /// that users intend to broadcasting the whole range.
      /// Thus, the address of end range is set to (start_address - 2), 
      /// the farthest address from the start_addr.
      if (gen_list.Count < 2)  {
        BigInteger start_int = start_addr.ToBigInteger();
        BigInteger end_int = start_int -2;
        end_addr = new AHAddress(end_int);
        end_range = end_addr.ToString();
      }
      else {
        end_range = gen_list[1] as string;
        end_addr = (AHAddress) AddressParser.Parse(end_range);
      }
      Log("generating child tree, range start: {0}, range end: {1}.", start_range, end_range);
      //we are at the start node, here we go:
      ConnectionTable tab = _node.ConnectionTable;
      ConnectionList structs = tab.GetConnections(ConnectionType.Structured);
      List<MapReduceInfo> retval = new List<MapReduceInfo>();

      if (InRange(_this_addr, start_addr, end_addr)) {
        if (structs.Count > 0) {
          //make connection list in the range.
          //left connection list is a list of neighbors which are in the range (this node, end of range)
          //right connection list is a list of neighbors which are in the range (start of range, this node)
          Brunet.Collections.Pair<List<Connection>,List<Connection>> cons = GetConnectionInfo(_this_addr, start_addr, end_addr, structs);
          List<Connection> left_cons =  cons.First as List<Connection>;
          List<Connection> right_cons = cons.Second as List<Connection>;
          //PrintConnectionList(left_cons);
          //PrintConnectionList(right_cons);
          retval = GenerateTreeInRange(start_addr, end_addr, left_cons, true, mr_args);
          List<MapReduceInfo> ret_right = GenerateTreeInRange(start_addr, end_addr, right_cons, false, mr_args);
          retval.AddRange(ret_right);
        }
        else {  //this node is a leaf node.
          //MapReduceInfo mr_info = null;
          //retval.Add(mr_info);
          //Console.WriteLine("no connection in the range: return null info");
        }
      }
      else { // _node is out of range. Just pass it to the closest to the middle of range.
        retval = GenerateTreeOutRange(start_addr, end_addr, mr_args);
      }
      q.Enqueue( retval.ToArray());
    }
Esempio n. 40
0
 public MapReduceBoundedBroadcast(Node n):base(n) {
   _this_addr = _node.Address as AHAddress;
 }
Esempio n. 41
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());
    }
Esempio n. 42
0
 //returns true if addr is in a given range including boundary.
 /**
  * This returns true if addr is between start and end in a ring.
  * IsBetweenFrom*() excludes both start and end, but InRange() includes both.
  * @param addr, this node's address
  * @param start, the beginning address of range
  * @param end, the ending address of range
  */
 public bool InRange(AHAddress addr, AHAddress start, AHAddress end) {
   return addr.IsBetweenFromLeft(start, end) || addr.Equals(start) || addr.Equals(end);
 }
Esempio n. 43
0
 protected Connection NextGreedyClosest(AHAddress dest) {
 /*
  * First find the Connection pointing to the node closest 
  * from local to dest, 
  * if there is one closer than us
  */
   ConnectionTable tab = _node.ConnectionTable;
   ConnectionList structs = tab.GetConnections(ConnectionType.Structured);
   AHAddress local = (AHAddress)_node.Address;
   Connection next_closest = structs.GetNearestTo(local, dest);
   return next_closest;
 }
Esempio n. 44
0
 public AnnealingRouting(AHAddress local, ConnectionList structured_cons) {
   //Fake connection to ourselves:
   _local_con = new Connection(null, local, "structured.self", null, null);
   int local_idx;
   _structs = ConnectionList.InsertInto(structured_cons, _local_con, out local_idx);
 }
Esempio n. 45
0
 public GreedyRouting(AHAddress local, ConnectionList structured_cons) {
   //Fake connection to ourselves:
   _local_con = new Connection(null, local, "structured.self", null, null);
   int local_idx;
   _structs = ConnectionList.InsertInto(structured_cons, _local_con, out local_idx);
   _NO_ONE = new Pair<Connection, bool>(null, false);
   _LOCAL = new Pair<Connection, bool>(null, true);
 }
Esempio n. 46
0
    /// <summary>Always returns the oldest non-tunnel address.</summary>
    public virtual Address EvaluatePotentialOverlap(IDictionary msg)
    {
      Address oldest_addr = null;
      int oldest_age = -1;
      foreach(DictionaryEntry de in msg) {
        MemBlock key = de.Key as MemBlock;
        if(key == null) {
          key = MemBlock.Reference((byte[]) de.Key);
        }
        Address addr = new AHAddress(key);

        Hashtable values = de.Value as Hashtable;
        TransportAddress.TAType tatype =
          TransportAddressFactory.StringToType(values["ta"] as string);

        if(tatype.Equals(TransportAddress.TAType.Relay)) {
          continue;
        }

        int age = (int) values["ct"];
        if(age > oldest_age) {
          oldest_addr = addr;
        }
      }

      return oldest_addr;
    }
Esempio n. 47
0
    public void Test()
    {
      IRelayOverlap _ito = new SimpleRelayOverlap();
      Address addr_x = new AHAddress(new RNGCryptoServiceProvider());
      byte[] addrbuff = Address.ConvertToAddressBuffer(addr_x.ToBigInteger() + (Address.Full / 2));
      Address.SetClass(addrbuff, AHAddress._class);
      Address addr_y = new AHAddress(addrbuff);

      ArrayList addresses = new ArrayList();
      ConnectionTable ct_x = new ConnectionTable();
      ConnectionTable ct_y = new ConnectionTable();
      ConnectionTable ct_empty = new ConnectionTable();
      for(int i = 1; i <= 10; i++) {
        addrbuff = Address.ConvertToAddressBuffer(addr_x.ToBigInteger() + (i * Address.Full / 16));
        Address.SetClass(addrbuff, AHAddress._class);
        addresses.Add(new AHAddress(addrbuff));

        TransportAddress ta = TransportAddressFactory.CreateInstance("brunet.tcp://158.7.0.1:5000");
        Edge fe = new FakeEdge(ta, ta, TransportAddress.TAType.Tcp);
        ct_x.Add(new Connection(fe, addresses[i - 1] as AHAddress, "structured", null, null));
        if(i == 10) {
          ct_y.Add(new Connection(fe, addresses[i - 1] as AHAddress, "structured", null, null));
        }
      }


      ConnectionType con_type = ConnectionType.Structured;
      IDictionary id = _ito.GetSyncMessage(null, addr_x, ct_x.GetConnections(con_type));
      Assert.AreEqual(_ito.EvaluateOverlap(ct_y.GetConnections(con_type), id)[0].Address, addresses[9], "Have an overlap!");
      Assert.AreEqual(_ito.EvaluateOverlap(ct_empty.GetConnections(con_type), id).Count, 0, "No overlap!");
      Assert.AreEqual(addresses.Contains(_ito.EvaluatePotentialOverlap(id)), true, "EvaluatePotentialOverlap returns valid!");
    }
Esempio n. 48
0
    public void Test()
    {
      Address addr_x = new AHAddress(new RNGCryptoServiceProvider());
      byte[] addrbuff = Address.ConvertToAddressBuffer(addr_x.ToBigInteger() + (Address.Full / 2));
      Address.SetClass(addrbuff, AHAddress._class);
      Address addr_y = new AHAddress(addrbuff);

      List<Connection> connections = new List<Connection>();
      ConnectionTable ct_x = new ConnectionTable();
      ConnectionTable ct_y = new ConnectionTable();
      ConnectionTable ct_empty = new ConnectionTable();
      NCService ncservice = new NCService();

      Connection fast_con = null;
      for(int i = 1; i <= 11; i++) {
        addrbuff = Address.ConvertToAddressBuffer(addr_x.ToBigInteger() + (i * Address.Full / 16));
        Address.SetClass(addrbuff, AHAddress._class);
        Address addr = new AHAddress(addrbuff);
        Connection con = null;

        TransportAddress ta = TransportAddressFactory.CreateInstance("brunet.tcp://158.7.0.1:5000");
        Edge fe = new FakeEdge(ta, ta, TransportAddress.TAType.Tcp);
        if(i <= 10) {
          con = new Connection(fe, addr, "structured", null, null);
          ct_x.Add(con);
          if(i % 2 == 0) {
            ncservice.ProcessSample(DateTime.UtcNow, String.Empty, addr,
                new Point(new double[] {0, 0}, 0), 0, i*10);
          }
        } else {
          fast_con = new Connection(fe, addr, "structured", null, null);
          ncservice.ProcessSample(DateTime.UtcNow, String.Empty, addr,
              new Point(new double[] {0, 0}, 0), 0, 5);
        }

        if(i == 10) {
          ct_y.Add(con);
        }
        connections.Add(con);
      }

      ITunnelOverlap sto = new SimpleTunnelOverlap();
      ITunnelOverlap nto = new NCTunnelOverlap(ncservice);

      ConnectionType con_type = ConnectionType.Structured;
      List<Connection> pre_cons = new List<Connection>();
      pre_cons.Add(connections[9]);
      IDictionary id = nto.GetSyncMessage(pre_cons, addr_x, ct_x.GetConnections(con_type));

      // We do have some pre-existing overlap
      Assert.AreEqual(nto.EvaluateOverlap(ct_y.GetConnections(con_type), id)[0], connections[9], "NC: Have an overlap!");
      Assert.AreEqual(sto.EvaluateOverlap(ct_y.GetConnections(con_type), id)[0], connections[9], "Simple: Have an overlap!");

      // We have no overlap with an empty connection table
      Assert.AreEqual(nto.EvaluateOverlap(ct_empty.GetConnections(con_type), id).Count, 0, "No overlap!");
      Assert.AreEqual(sto.EvaluateOverlap(ct_empty.GetConnections(con_type), id).Count, 0, "No overlap!");

      // latency[0] == -1
      Assert.AreEqual(connections[1].Address.Equals(nto.EvaluatePotentialOverlap(id)), true,
          "NC: EvaluatePotentialOverlap returns expected!");
      Assert.AreEqual(ct_x.Contains(con_type, sto.EvaluatePotentialOverlap(id)), true,
          "Simple: EvaluatePotentialOverlap returns valid!");

      ct_y.Add(fast_con);
      ct_x.Add(fast_con);
      id = nto.GetSyncMessage(pre_cons, addr_x, ct_x.GetConnections(con_type));
      Assert.AreEqual(fast_con.Address.Equals(nto.EvaluatePotentialOverlap(id)), true,
          "NC: EvaluatePotentialOverlap returns expected!");
      Assert.AreEqual(nto.EvaluateOverlap(ct_y.GetConnections(con_type), id)[0], fast_con, "NC: Have better overlap!");
    }
Esempio n. 49
0
 /**
  * Generate tree within the range.
  * return list of MapReduceInfo
  */
 private List<MapReduceInfo> GenerateTreeInRange(AHAddress start, AHAddress end, List<Connection> cons, bool left, MapReduceArgs mr_args) {
   //Divide the range and trigger bounded broadcasting again in divided range starting with neighbor.
   //Deivided ranges are (start, n_1), (n_1, n_2), ... , (n_m, end)
   AHAddress this_minus2 = new AHAddress(_this_addr.ToBigInteger()-2);
   AHAddress this_plus2 = new AHAddress(_this_addr.ToBigInteger()+2);
   List<MapReduceInfo> retval = new List<MapReduceInfo>();
   if (cons.Count != 0) //make sure if connection list is not empty!
   {
     //con_list is sorted.
     AHAddress last;
     if (left) {
       last = end;
     }
     else {
       last = start;
     }
     string rg_start, rg_end;
     //the first element of cons is the nearest.
     //Let's start with the farthest neighbor first.
     for (int i = (cons.Count-1); i >= 0; i--) {
       ArrayList gen_arg = new ArrayList();
       Connection next_c = cons[i];
       AHAddress next_addr = (AHAddress)next_c.Address;
       ISender sender = next_c.State.Edge;
       if (i==0) {  // The last bit
         if (left) {
           // the left nearest neighbor 
           rg_start = this_plus2.ToString();
           rg_end = last.ToString();
         }
         else {
           // the right nearest neighbor
           rg_start = last.ToString();
           rg_end = this_minus2.ToString();
         }
       }
       else {
         if (left) { //left connections
           rg_start = next_addr.ToString();
           rg_end = last.ToString();
         }
         else {  //right connections
           rg_start = last.ToString();
           rg_end = next_addr.ToString();
         }
       }
       gen_arg.Add(rg_start);
       gen_arg.Add(rg_end);
       MapReduceInfo mr_info = new MapReduceInfo( sender,
        		                              new MapReduceArgs(this.TaskName,
       				               	             mr_args.MapArg,
       							     gen_arg,
       							     mr_args.ReduceArg));
       Log("{0}: {1}, adding address: {2} to sender list, range start: {3}, range end: {4}",
       			    this.TaskName, _node.Address, next_c.Address,
       			    gen_arg[0], gen_arg[1]);
       if (left) {
         last = new AHAddress(next_addr.ToBigInteger()-2);
       }
       else {
         last = new AHAddress(next_addr.ToBigInteger()+2);
       }
       retval.Add(mr_info);
     }
   }
   return retval;
 }    
Esempio n. 50
0
 public DirectionalRouting(AHAddress local, ConnectionList structs) {
   _NULL_TRUE = new Pair<Connection, bool>(null, true);
   _NULL_FALSE = new Pair<Connection, bool>(null, false);
   Connection left_c = null;
   Connection right_c = null;
   if( structs.Count > 0) {
     int local_idx = ~structs.IndexOf(local);
     int left_idx = local_idx;
     int right_idx = left_idx - 1;
     left_c = structs[left_idx];
     right_c = structs[right_idx];
   }
   _LEFT_FALSE = new Pair<Connection, bool>(left_c, false);
   _RIGHT_FALSE = new Pair<Connection, bool>(right_c, false);
   _LEFT_TRUE = new Pair<Connection, bool>(left_c, true);
   _RIGHT_TRUE = new Pair<Connection, bool>(right_c, true);
 }
Esempio n. 51
0
 /**
  * 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 List<MapReduceInfo> GenerateTreeOutRange(AHAddress start, AHAddress end, MapReduceArgs mr_args) {
   List<MapReduceInfo> retval = new List<MapReduceInfo>();
   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) ) {
   if (!InRange(mid_addr, 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));
     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;
 }
Esempio n. 52
0
    protected AHState(AHState old_state, ConnectionList leafs) {
      Leafs = leafs;
      Structs = old_state.Structs;
      Local = old_state.Local;

      _directional = old_state._directional;
      _greedy = old_state._greedy;
      _annealing = old_state._annealing;
    }
Esempio n. 53
0
 /**
  * Find neighbor connections within the range
  * return ArrayList of List<Connection> for left and right neighbors.
  */
 private Brunet.Collections.Pair<List<Connection>,List<Connection>> GetConnectionInfo(AHAddress t_addr, AHAddress start, AHAddress end, ConnectionList cl) {
    
   //this node is within the given range (start_addr, end_addr)
   List<Connection> left_con_list = new List<Connection>();
   List<Connection> right_con_list = new List<Connection>();
   foreach(Connection c in cl) {
     AHAddress adr = (AHAddress)c.Address;
     //if(adr.IsBetweenFromLeft(t_addr, end) ) {
     if (InRange(adr, start, t_addr) ) {
       left_con_list.Add(c);
     }
     //else if (adr.IsBetweenFromLeft(start, t_addr) ) {
     else if (InRange(adr, start, t_addr) ) {
       right_con_list.Add(c);
     }
     else {
       //Out of Range. Do nothing!
     }
   }
   //Make a compare and add it to ConnectionTable to sort by Address
   ConnectionLeftComparer left_cmp = new ConnectionLeftComparer(t_addr);
   left_con_list.Sort(left_cmp);
   ConnectionRightComparer right_cmp = new ConnectionRightComparer(t_addr);
   right_con_list.Sort(right_cmp);
   Brunet.Collections.Pair<List<Connection>,List<Connection>> ret = new Brunet.Collections.Pair<List<Connection>,List<Connection>>(left_con_list, right_con_list);
   return ret;
 }
Esempio n. 54
0
 public AHState(AHAddress local, ConnectionList structs, ConnectionList leafs) {
   Leafs = leafs;
   Structs = structs;
   Local = local;
   
   _directional = new DirectionalRouting(local, structs);
   _greedy = new GreedyRouting(local, structs);
   _annealing = new AnnealingRouting(local, structs);
 }
Esempio n. 55
0
 /// <summary>Generate a new unique address, there is potential for
 /// collissions when we make the address space small.</summary>
 protected AHAddress GenerateAddress()
 {
   byte[] addr = new byte[Address.MemSize];
   _rand.NextBytes(addr);
   Address.SetClass(addr, AHAddress._class);
   AHAddress ah_addr = new AHAddress(MemBlock.Reference(addr));
   if(Nodes.ContainsKey(ah_addr)) {
     ah_addr = GenerateAddress();
   }
   return ah_addr;
 }
Esempio n. 56
0
 /**
  * If you want to create a node in a realm other
  * than the default "global" realm, use this
  * @param add AHAddress of this node
  * @param realm Realm this node is to be a member of
  */
 public StructuredNode(AHAddress addr) : this(addr, "global")
 {
 
 }
Esempio n. 57
0
    /// <summary>Returns the four fastest in the overlap.</summary>
    public override List<Connection> EvaluateOverlap(ConnectionList cons, IDictionary msg)
    {
      List<Connection> overlap = new List<Connection>();

      foreach(DictionaryEntry de in msg) {
        MemBlock key = de.Key as MemBlock;
        if(key == null) {
          key = MemBlock.Reference((byte[]) de.Key);
        }
        Address addr = new AHAddress(key);

        int index = cons.IndexOf(addr);
        if(index < 0) {
          continue;
        }

        Connection con = cons[index];

        // Since there are no guarantees about routing over two tunnels, we do
        // not consider cases where we are connected to the overlapping tunnels
        // peers via tunnels
        if(con.Edge.TAType.Equals(TransportAddress.TAType.Tunnel)) {
          Hashtable values = de.Value as Hashtable;
          TransportAddress.TAType tatype =
            TransportAddressFactory.StringToType(values["ta"] as string);
          if(tatype.Equals(TransportAddress.TAType.Tunnel)) {
            continue;
          }
        }
        overlap.Add(con);
      }

      return GetClosest(overlap);
    }
Esempio n. 58
0
    protected virtual StructuredNode PrepareNode(int id, AHAddress address)
    {
      if(TakenIDs.ContainsKey(id)) {
        throw new Exception("ID already taken");
      }

      StructuredNode node = new StructuredNode(address, BrunetNamespace);

      NodeMapping nm = new NodeMapping();
      nm.ID = id;
      TakenIDs[id] = nm;
      nm.Node = node;
      Nodes.Add((Address) address, nm);

      EdgeListener el = CreateEdgeListener(nm.ID);

      if(_secure_edges || _secure_senders) {
        byte[] blob = _se_key.ExportCspBlob(true);
        RSACryptoServiceProvider rsa_copy = new RSACryptoServiceProvider();
        rsa_copy.ImportCspBlob(blob);

        string username = address.ToString().Replace('=', '0');
        CertificateMaker cm = new CertificateMaker("United States", "UFL", 
          "ACIS", username, "*****@*****.**", rsa_copy,
          address.ToString());
        Certificate cert = cm.Sign(_ca_cert, _se_key);

        CertificateHandler ch = null;
        if(_dtls) {
          ch = new OpenSslCertificateHandler();
        } else {
          ch = new CertificateHandler();
        }
        ch.AddCACertificate(_ca_cert.X509);
        ch.AddSignedCertificate(cert.X509);

        if(_dtls) {
          nm.SO = new DtlsOverlord(rsa_copy, ch, PeerSecOverlord.Security);
        } else {
          nm.Sso = new SymphonySecurityOverlord(node, rsa_copy, ch, node.Rrm);
          nm.SO = nm.Sso;
        }

        var brh = new BroadcastRevocationHandler(_ca_cert, nm.SO);
        node.GetTypeSource(BroadcastRevocationHandler.PType).Subscribe(brh, null);
        ch.AddCertificateVerification(brh);
        nm.SO.Subscribe(node, null);
        node.GetTypeSource(PeerSecOverlord.Security).Subscribe(nm.SO, null);
      }

      if(_pathing) {
        nm.PathEM = new PathELManager(el, nm.Node);
        nm.PathEM.Start();
        el = nm.PathEM.CreatePath();
        PType path_p = PType.Protocol.Pathing;
        nm.Node.DemuxHandler.GetTypeSource(path_p).Subscribe(nm.PathEM, path_p);
      }

      if(_secure_edges) {
        node.EdgeVerifyMethod = EdgeVerify.AddressInSubjectAltName;
        el = new SecureEdgeListener(el, nm.SO);
      }

      node.AddEdgeListener(el);

      if(!_start) {
        node.RemoteTAs = GetRemoteTAs();
      }

      IRelayOverlap ito = null;
      if(NCEnable) {
        nm.NCService = new NCService(node, new Point());
// My evaluations show that when this is enabled the system sucks
//        (node as StructuredNode).Sco.TargetSelector = new VivaldiTargetSelector(node, ncservice);
        ito = new NCRelayOverlap(nm.NCService);
      } else {
        ito = new SimpleRelayOverlap();
      }

      if(_broken != 0) {
        el = new Relay.RelayEdgeListener(node, ito);
        if(_secure_edges) {
          el = new SecureEdgeListener(el, nm.SO);
        }
        node.AddEdgeListener(el);
      }

      BroadcastHandler bhandler = new BroadcastHandler(node as StructuredNode);
      node.DemuxHandler.GetTypeSource(BroadcastSender.PType).Subscribe(bhandler, null);
      node.DemuxHandler.GetTypeSource(SimBroadcastPType).Subscribe(SimBroadcastHandler, null);

      // Enables Dht data store
      new TableServer(node);
      nm.Dht = new Dht(node, 3, 20);
      nm.DhtProxy = new RpcDhtProxy(nm.Dht, node);
      return node;
    }
Esempio n. 59
0
    /// <summary>Always returns the fastest non-tunnel, overlapping address.</summary>
    public override Address EvaluatePotentialOverlap(IDictionary msg)
    {
      Address best_addr = null;
      double best_latency = double.MaxValue;
      Address their_best_addr = null;
      double their_best_latency = double.MaxValue;

      foreach(DictionaryEntry de in msg) {
        MemBlock key = de.Key as MemBlock;
        if(key == null) {
          key = MemBlock.Reference((byte[]) de.Key);
        }
        Address addr = new AHAddress(key);

        IDictionary info = de.Value as IDictionary;
        TransportAddress.TAType tatype =
          TransportAddressFactory.StringToType(info["ta"] as string);

        if(tatype.Equals(TransportAddress.TAType.Tunnel)) {
          continue;
        }

        double latency = _ncservice.GetMeasuredLatency(addr);
        if(latency > 0 && latency < best_latency) {
          best_addr = addr;
          best_latency = latency;
        }

        if(!info.Contains("lat")) {
          continue;
        }

        latency = (double) info["lat"];
        if(latency > 0 && latency < their_best_latency) {
          their_best_addr = addr;
          their_best_latency = latency;
        }
      }

      best_addr = their_best_latency < best_latency ? their_best_addr : best_addr;
      return best_addr == null ? base.EvaluatePotentialOverlap(msg) : best_addr;
    }
Esempio n. 60
0
 ///<summary>Add a new specific node to the simulation.</summary>
 public virtual Node AddNode(int id, AHAddress address)
 {
   StructuredNode node = PrepareNode(id, address);
   if(!_start) {
     node.Connect();
   }
   CurrentNetworkSize++;
   return node;
 }