Exemple #1
0
        /**
         * <summary>Returns a list of keys stored at this node that exist between the
         * two addresses.  Such keys returned are the storest path between the two
         * addresses.</summary>
         * <param name="add1">One of the address end points.</param>
         * <param name="add2">Another of the address end points.</param>
         * <returns>A LinkedList of key entries between add1 and add2</returns>
         */
        public LinkedList <MemBlock> GetKeysBetween(AHAddress add1, AHAddress add2)
        {
            LinkedList <MemBlock> keys = new LinkedList <MemBlock>();

            if (add1.IsRightOf(add2))
            {
                foreach (MemBlock key in list_of_keys)
                {
                    AHAddress key_addr = new AHAddress(key);
                    if (key_addr.IsBetweenFromLeft(add1, add2))
                    {
                        keys.AddLast(key);
                    }
                }
            }
            else
            {
                foreach (MemBlock key in list_of_keys)
                {
                    AHAddress key_addr = new AHAddress(key);
                    if (key_addr.IsBetweenFromRight(add1, add2))
                    {
                        keys.AddLast(key);
                    }
                }
            }
            return(keys);
        }
Exemple #2
0
        /// <summary>Sends a packet from A to B returning the delay and hop count.</summary>
        public List <SendPacketResult> SendPacket(AHAddress from, AHAddress to, ushort option)
        {
            AHHeader ah = new AHHeader(0, 100, from, to, option);

            GraphNode cnode              = _addr_to_node[from];
            Edge      cedge              = null;
            AHAddress last_addr          = from;
            Pair <Connection, bool> next = new Pair <Connection, bool>(null, false);

            int delay   = 0;
            int hops    = 0;
            var results = new List <SendPacketResult>();

            while (true)
            {
                next = cnode.NextConnection(cedge, ah);
                if (next.Second)
                {
                    results.Add(new SendPacketResult(last_addr, delay, hops));
                }
                if (next.First == null)
                {
                    break;
                }
                AHAddress caddress = next.First.Address as AHAddress;
                cnode     = _addr_to_node[caddress];
                cedge     = cnode.ConnectionTable.GetConnection(next.First.MainType, last_addr).Edge;
                last_addr = caddress;
                delay    += (cedge as GraphEdge).Delay;
                hops++;
            }

            return(results);
        }
Exemple #3
0
        /**
         * Return an IDictionary with entries:
         * self -> my Address
         * left -> Address of left neighbor
         * right -> Address of right neighbor
         * left2 -> Second left neighbor
         * right2 -> Second right neighbor
         *
         * If the node has any shortcuts:
         * shortcut -> Random shortcut connection
         */
        public IDictionary GetNeighbors()
        {
            AHAddress   self   = (AHAddress)_node.Address;
            IDictionary result = new ListDictionary();

            //Put it in:
            result["self"] = self.ToString();

            // if we have no connections, this throws an exception
            try {
                Connection left = _node.ConnectionTable.GetLeftStructuredNeighborOf(self);
                if (left != null)
                {
                    AHAddress la = (AHAddress)left.Address;
                    result["left"] = la.ToString();
                    Connection left2 = _node.ConnectionTable.GetLeftStructuredNeighborOf(la);
                    if (left2 != null)
                    {
                        result["left2"] = left2.Address.ToString();
                    }
                }
            } catch {}
            // If there are no connections, this throws an exception
            try {
                Connection right = _node.ConnectionTable.GetRightStructuredNeighborOf(self);
                if (right != null)
                {
                    AHAddress ra = (AHAddress)right.Address;
                    result["right"] = ra.ToString();
                    Connection right2 = _node.ConnectionTable.GetRightStructuredNeighborOf(ra);
                    if (right2 != null)
                    {
                        result["right2"] = right2.Address.ToString();
                    }
                }
            }
            catch {}
            //Get a random shortcut:
            ArrayList shortcuts = new ArrayList();

            // If there are no connections, this throws an exception
            try {
                foreach (Connection c in _node.ConnectionTable.GetConnections("structured.shortcut"))
                {
                    shortcuts.Add(c);
                }
            }
            catch {}
            if (shortcuts.Count > 0)
            {
#if BRUNET_SIMULATOR
                Random r = Node.SimulatorRandom;
#else
                Random r = new Random();
#endif
                Connection sc = (Connection)shortcuts[r.Next(shortcuts.Count)];
                result["shortcut"] = sc.Address.ToString();
            }
            return(result);
        }
Exemple #4
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.Tunnel))
                {
                    continue;
                }

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

            return(oldest_addr);
        }
Exemple #5
0
        /// <summary>This is the generic Put that is used by both the regular Put
        /// and Create methods.  The use of the unique variable differentiates the
        /// two.  This is asynchronous.  Results are stored in the Channel returns.
        /// Creates and Puts return true if successful or exception if there are
        /// network errors in adding the entry, creates also fail if a previous
        /// entry exists.  The work of determining success is handled in
        /// PutEnqueueHandler and PutCloseHandler.</summary>
        /// <param name="key">The index to store the value at.</param>
        /// <param name="value">The value to store.</param>
        /// <param name="ttl">The dht lease time for the key:value pair.</param>
        /// <param name="returns">The Channel where the result will be placed.</param>
        /// <param name="unique">True to do a create, false otherwise.</param>
        public void AsyncPut(MemBlock key, MemBlock value, int ttl, Channel returns, bool unique)
        {
            if (!_online)
            {
                throw new DhtException("The Node is (going) offline, DHT is offline.");
            }
            AsDhtPutState adps = new AsDhtPutState(returns);

            MemBlock[] brunet_address_for_key = MapToRing(key);
            Channel[]  q = new Channel[DEGREE];
            lock (_adps_table.SyncRoot) {
                for (int k = 0; k < DEGREE; k++)
                {
                    Channel queue = new Channel(1);
                    queue.CloseEvent  += this.PutCloseHandler;
                    _adps_table[queue] = adps;
                    q[k] = queue;
                }
            }

            for (int k = 0; k < DEGREE; k++)
            {
                Address  target = new AHAddress(brunet_address_for_key[k]);
                AHSender s      = new AHGreedySender(Node, target);
                _rpc.Invoke(s, q[k], "dht.Put", brunet_address_for_key[k], value, ttl, unique);
            }
        }
Exemple #6
0
        public void TestWriteAndParse()
        {
            RandomNumberGenerator rng = new RNGCryptoServiceProvider();
            Address          a        = new AHAddress(rng);
            TransportAddress ta       = TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:5000");
            NodeInfo         ni       = NodeInfo.CreateInstance(a, ta);

            RoundTripHT(ni);
            RoundTrip(ni);

            //Test multiple tas:
            ArrayList tas = new ArrayList();

            tas.Add(ta);
            for (int i = 5001; i < 5010; i++)
            {
                tas.Add(TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:" + i.ToString()));
            }
            NodeInfo ni3 = NodeInfo.CreateInstance(a, tas);

            RoundTripHT(ni3);
            RoundTrip(ni3);

            //Test null address:
            NodeInfo ni4 = NodeInfo.CreateInstance(null, ta);

            RoundTripHT(ni4);
            RoundTrip(ni4);

            //No TAs:
            NodeInfo ni5 = NodeInfo.CreateInstance(a);

            RoundTripHT(ni5);
            RoundTrip(ni5);
        }
Exemple #7
0
        /// <summary>Called by HandleIPOut if the current packet has a Multicast
        /// address in its destination field.  This sends the multicast packet
        /// to all members of the multicast group stored in the dht.</summary>
        /// <param name="ipp">The IP Packet destined for multicast.</param>
        /// <returns>This returns true since this is implemented.</returns>
        protected override bool HandleMulticast(IPPacket ipp)
        {
            if (!_ipop_config.EnableMulticast)
            {
                return(true);
            }

            WaitCallback wcb = delegate(object o) {
                Hashtable[] results = null;
                try {
                    results = AppNode.Dht.Get(Encoding.UTF8.GetBytes(_ipop_config.IpopNamespace + ".multicast.ipop"));
                } catch {
                    return;
                }
                foreach (Hashtable result in results)
                {
                    try {
                        AHAddress target = (AHAddress)AddressParser.Parse(Encoding.UTF8.GetString((byte[])result["value"]));
                        if (IpopLog.PacketLog.Enabled)
                        {
                            ProtocolLog.Write(IpopLog.PacketLog, String.Format(
                                                  "Brunet destination ID: {0}", target));
                        }
                        SendIP(target, ipp.Packet);
                    }
                    catch {}
                }
            };

            ThreadPool.QueueUserWorkItem(wcb, ipp);
            return(true);
        }
Exemple #8
0
        protected AHAddress FindNodeNearestToAddress(AHAddress addr)
        {
            int       index  = _addrs.BinarySearch(addr);
            AHAddress to_use = addr;

            if (index < 0)
            {
                index = ~index;
                if (index == _addrs.Count)
                {
                    index = 0;
                }
                AHAddress right = _addrs[index];
                if (index == 0)
                {
                    index = _addrs.Count - 1;
                }
                AHAddress left = _addrs[index - 1];
                if (right.DistanceTo(addr) < left.DistanceTo(addr))
                {
                    to_use = right;
                }
                else
                {
                    to_use = left;
                }
            }

            return(to_use);
        }
    /// <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.Tunnel)) {
          continue;
        }

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

      return oldest_addr;
    }
Exemple #10
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);
        }
Exemple #11
0
        /** Greedy routing.  gen_arg is the Address of the destination
         */
        public override void GenerateTree(Channel q, MapReduceArgs mr_args)
        {
            object gen_arg = mr_args.GenArg;

            Log("{0}: {1}, greedy generator called, arg: {2}.",
                this.TaskName, _node.Address, gen_arg);
            string          address      = gen_arg as string;
            AHAddress       a            = (AHAddress)AddressParser.Parse(address);
            ArrayList       retval       = new ArrayList();
            ConnectionTable tab          = _node.ConnectionTable;
            ConnectionList  structs      = tab.GetConnections(ConnectionType.Structured);
            Connection      next_closest = structs.GetNearestTo((AHAddress)_node.Address, a);

            if (next_closest != null)
            {
                //arguments do not change at all
                MapReduceInfo mr_info = new MapReduceInfo(next_closest.Edge, mr_args);
                retval.Add(mr_info);
            }

            Log("{0}: {1}, greedy generator returning: {2} senders.",
                this.TaskName, _node.Address, retval.Count);
            //Send the result:
            q.Enqueue(retval.ToArray(typeof(MapReduceInfo)));
        }
Exemple #12
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());
        }
Exemple #13
0
        public virtual Node AddNode(int id, AHAddress address)
        {
            StructuredNode node = PrepareNode(id, address);

            node.Connect();
            CurrentNetworkSize++;
            return(node);
        }
Exemple #14
0
        /**
         * <summary>Generates a new AHAddress as a byte array.</summary>
         * <returns>An unique AHAddress as type byte[].</returns>
         */
        public static byte [] GenerateAddress()
        {
            AHAddress temp = GenerateAHAddress();

            byte [] tempb = new byte[20];
            temp.CopyTo(tempb);
            return(tempb);
        }
Exemple #15
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);
        }
Exemple #16
0
        /**
         * Recursive function to compute the latency of an Rpc call
         * by accumulating measured latencies of individual hops.
         * @param target address of the target
         */
        public void ComputePathLatencyTo(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_latency"] = GetMeasuredLatency(next_closest.Address);
                my_entry["next_contype"] = next_closest.ConType;
                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.Edge, result, "ncserver.ComputePathLatencyTo", 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);
            }
        }
Exemple #17
0
        /**
         * Read the address out of the buffer  This makes a copy
         * and calls Parse on the copy.  This is a "convienience" method.
         * @throw ParseException if the buffer is not a valid address
         */
        static public Address Parse(MemBlock mb)
        {
            //Read some of the least significant bytes out,
            //AHAddress all have last bit 0, so we skip the last byte which
            //will have less entropy
            ushort  idx = (ushort)NumberSerializer.ReadShort(mb, Address.MemSize - 3);
            Address a   = _mb_cache[idx];

            if (a != null)
            {
                if (a.ToMemBlock().Equals(mb))
                {
                    return(a);
                }
            }
            //Else we need to read the address and put it in the cache
            try {
                if (2 * mb.Length < mb.ReferencedBufferLength)
                {
                    /*
                     * This MemBlock is much smaller than the array
                     * we are referencing, don't keep the big one
                     * in scope, instead make a copy
                     */
                    mb = MemBlock.Copy((ICopyable)mb);
                }
                int add_class = Address.ClassOf(mb);
                switch (add_class)
                {
                case AHAddress.ClassValue:
                    a = new AHAddress(mb);
                    break;

                case DirectionalAddress.ClassValue:
                    a = new DirectionalAddress(mb);
                    break;

                default:
                    a = null;
                    throw new ParseException("Unknown Address Class: " +
                                             add_class + ", buffer:" +
                                             mb.ToString());
                }
                //Cache this result:
                _mb_cache[idx] = a;
                return(a);
            }
            catch (ArgumentOutOfRangeException ex) {
                throw new ParseException("Address too short: " +
                                         mb.ToString(), ex);
            }
            catch (ArgumentException ex) {
                throw new ParseException("Could not parse: " +
                                         mb.ToString(), ex);
            }
        }
Exemple #18
0
 /*
  * This is to see if connection list elements are sorted or not.
  */
 private void PrintConnectionList(List <Connection> l)
 {
     for (int i = 0; i < l.Count; i++)
     {
         Connection c        = (Connection)l[i];
         AHAddress  next_add = (AHAddress)c.Address;
         BigInteger dist     = _this_addr.LeftDistanceTo(next_add);
         Console.WriteLine("add: {0}, dis: {1}", next_add, dist);
     }
 }
Exemple #19
0
        /// <summary>Calculate the Left AHAddress of the given address.</summary>
        public AHAddress GetLeftNearTarget(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));
        }
Exemple #20
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;
    }
Exemple #21
0
        public void LMSerializationTest()
        {
            NodeInfo n1 = NodeInfo.CreateInstance(null, TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:45"));
            RandomNumberGenerator rng     = new RNGCryptoServiceProvider();
            AHAddress             tmp_add = new AHAddress(rng);
            LinkMessage           l1      = new LinkMessage(ConnectionType.Structured, n1,
                                                            NodeInfo.CreateInstance(new DirectionalAddress(DirectionalAddress.Direction.Left),
                                                                                    TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:837")), string.Empty,
                                                            tmp_add.ToString());

            RoundTripHT(l1);
        }
Exemple #22
0
        public void CacheTest()
        {
            RandomNumberGenerator rng = new RNGCryptoServiceProvider();
            Address          a        = new AHAddress(rng);
            Address          a2       = new AHAddress(a.ToMemBlock());
            TransportAddress ta       = TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:5000");
            TransportAddress ta2      = TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:5000");
            NodeInfo         ni       = NodeInfo.CreateInstance(a, ta);
            NodeInfo         ni2      = NodeInfo.CreateInstance(a2, ta2);

            Assert.AreSame(ni, ni2, "Reference equality of NodeInfo objects");
        }
Exemple #23
0
        override protected void SeekTAs(DateTime now)
        {
            if (Interlocked.Exchange(ref _ongoing, 1) == 1)
            {
                return;
            }

            Channel chan = new Channel();

            EventHandler handler = delegate(object o, EventArgs ea) {
                if (!chan.Closed && chan.Count < 8)
                {
                    return;
                }

                List <TransportAddress> tas = new List <TransportAddress>();
                while (chan.Count > 0)
                {
                    AHAddress addr = null;
                    try {
                        IDictionary dict  = (IDictionary)chan.Dequeue();
                        byte[]      baddr = (byte[])dict["value"];
                        addr = new AHAddress(MemBlock.Reference(baddr));
                    } catch {
                        continue;
                    }
                    tas.Add(new SubringTransportAddress(addr, _shared_namespace));
                }

                if (tas.Count > 0)
                {
                    CheckAndUpdateRemoteTAs(tas);
                }

                if (chan.Closed)
                {
                    Interlocked.Exchange(ref _ongoing, 0);
                }
            };

            if (_steady_state == 0)
            {
                chan.EnqueueEvent += handler;
            }
            chan.CloseEvent += handler;

            try {
                _dht.AsyncGet(_private_dht_key, chan);
            } catch (DhtException) {
                chan.Close();
            }
        }
Exemple #24
0
        protected AHAddress GenerateAddress()
        {
            AHAddress addr = null;

            do
            {
                byte[] baddr = new byte[Address.MemSize];
                _rand.NextBytes(baddr);
                Address.SetClass(baddr, AHAddress.ClassValue);
                addr = new AHAddress(MemBlock.Reference(baddr));
            } while(_addr_to_node.ContainsKey(addr));
            return(addr);
        }
Exemple #25
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;
        }
Exemple #26
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);
        }
Exemple #27
0
        public void WrapperEdgeRegressionTest()
        {
            AHAddress        addr  = new AHAddress(new System.Security.Cryptography.RNGCryptoServiceProvider());
            TransportAddress ta    = TransportAddressFactory.CreateInstance("brunet.tcp://169.0.5.1:5000");
            FakeEdge         fe    = new FakeEdge(ta, ta);
            WrapperEdge      we_fe = new WrapperEdge(fe);
            Connection       fcon  = new Connection(we_fe, addr, "structured", null, null);

            List <Connection> overlap = new List <Connection>();

            overlap.Add(fcon);
            RelayTransportAddress tta    = new RelayTransportAddress(addr, overlap);
            RelayEdge             te1    = new RelayEdge(null, tta, tta, new SimpleForwarderSelector(), overlap);
            WrapperEdge           we_te1 = new WrapperEdge(te1);
            Connection            t1con  = new Connection(we_te1, addr, "structured", null, null);

            overlap = new List <Connection>();
            overlap.Add(t1con);
            RelayEdge   te2    = new RelayEdge(null, tta, tta, new SimpleForwarderSelector(), overlap);
            WrapperEdge we_te2 = new WrapperEdge(te2);
            Connection  t2con  = new Connection(we_te2, addr, "structured", null, null);

            overlap = new List <Connection>();
            overlap.Add(t2con);
            RelayEdge   te3    = new RelayEdge(null, tta, tta, new SimpleForwarderSelector(), overlap);
            WrapperEdge we_te3 = new WrapperEdge(te3);
            Connection  t3con  = new Connection(we_te3, addr, "structured", null, null);

            overlap = new List <Connection>();
            overlap.Add(t3con);
            RelayEdge   te4    = new RelayEdge(null, tta, tta, new SimpleForwarderSelector(), overlap);
            WrapperEdge we_te4 = new WrapperEdge(te4);
            Connection  t4con  = new Connection(we_te4, addr, "structured", null, null);

            overlap = new List <Connection>();
            overlap.Add(t4con);
            RelayEdge   te5    = new RelayEdge(null, tta, tta, new SimpleForwarderSelector(), overlap);
            WrapperEdge we_te5 = new WrapperEdge(te5);
            Connection  t5con  = new Connection(we_te5, addr, "structured", null, null);

            Assert.AreEqual(te5.ShouldClose(), false, "Shouldn't close yet...");
            te1.DisconnectionHandler(fcon);
            Assert.AreEqual(te5.ShouldClose(), true, "Should close...");

            overlap.Add(t5con);
            overlap.Add(t3con);
            overlap.Add(t1con);
            te2.UpdateNeighborIntersection(overlap);
            Assert.AreEqual(te5.ShouldClose(), true, "Should close... 2");
        }
Exemple #28
0
        /// <summary>Performs a broadcast to the entire overlay.</summary>
        public int Broadcast(AHAddress from)
        {
            GraphNode      node  = _addr_to_node[from];
            ConnectionList cl    = node.ConnectionTable.GetConnections(ConnectionType.Structured);
            int            index = cl.IndexOf(from);

            if (index < 0)
            {
                index = ~index;
            }
            AHAddress start = cl[index].Address as AHAddress;

            return(BoundedBroadcast(from, start, GetLeftNearTarget(from)));
        }
Exemple #29
0
        /// <summary>Restores any of the Dht results that don't return all their
        /// values.  We only get here at the end of a Dht return operation.</summary>
        /// <remarks>This analyzes the holes and fills them in individually.  This only
        /// fills holes where there was a positive result (MAJORITY of results
        /// received).</remarks>
        /// <param name="adgs">The AsDhtGetState to analyze for follow up.</param>
        protected void GetFollowUp(AsDhtGetState adgs)
        {
            foreach (DictionaryEntry de in adgs.results)
            {
                if (de.Value == null || de.Key == null)
                {
                    continue;
                }

                Hashtable res = (Hashtable)de.Value;
                if (res.Count < MAJORITY || res.Count == DEGREE)
                {
                    if (res.Count < MAJORITY)
                    {
                        if (Dht.DhtLog.Enabled)
                        {
                            ProtocolLog.Write(Dht.DhtLog, String.Format(
                                                  "Failed get count:total = {0}:{1}", res.Count, DEGREE));
                        }
                    }
                    res.Clear();
                    continue;
                }
                MemBlock value = (MemBlock)de.Key;

                int ttl = (int)adgs.ttls[value] / res.Count;
                if (Dht.DhtLog.Enabled)
                {
                    ProtocolLog.Write(Dht.DhtLog, String.Format(
                                          "Doing follow up put count:total = {0}:{1}", res.Count, DEGREE));
                }
                for (int i = 0; i < DEGREE; i++)
                {
                    if (!res.Contains(i))
                    {
                        MemBlock key    = adgs.brunet_address_for_key[i];
                        Channel  queue  = new Channel();
                        Address  target = new AHAddress(key);
                        AHSender s      = new AHGreedySender(Node, target);
                        try {
                            _rpc.Invoke(s, queue, "dht.Put", key, value, ttl, false);
                        }
                        catch (Exception) {}
                    }
                }
                res.Clear();
            }
            adgs.ttls.Clear();
            adgs.results.Clear();
        }
Exemple #30
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);
        }
Exemple #31
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.Relay))
                {
                    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);
        }
Exemple #32
0
        ///<summary>Add a new specific node to the simulation.</summary>
        public virtual Node AddNode(int id, AHAddress address)
        {
            if (TakenIDs.ContainsKey(id))
            {
                throw new Exception("ID already taken");
            }

            StructuredNode node = PrepareNode(id, address);

            if (!_start)
            {
                node.Connect();
            }
            CurrentNetworkSize++;
            return(node);
        }
Exemple #33
0
    ///This tester simply establishes the Brunet network and log the edges made
 
    static void Main(string[] args)
    {

      String config_file = args[0];
      NetworkConfiguration network_configuration = NetworkConfiguration.Deserialize(config_file);

      for(int i = 0; i < network_configuration.Nodes.Count; i++){

        NodeConfiguration this_node_configuration = (NodeConfiguration)network_configuration.Nodes[i];
        TransportAddressConfiguration local_ta_configuration = (TransportAddressConfiguration)this_node_configuration.TransportAddresses[0];
        short port = local_ta_configuration.Port;
        SHA1 sha = new SHA1CryptoServiceProvider();
        String local_ta = local_ta_configuration.GetTransportAddressURI();
        //We take the local transport address plus the port number to be hashed to obtain a random AHAddress
        byte[] hashedbytes = sha.ComputeHash(Encoding.UTF8.GetBytes(local_ta + port));
        //inforce type 0
        hashedbytes[Address.MemSize - 1] &= 0xFE;
        AHAddress _local_ahaddress = new AHAddress(hashedbytes);
        Console.WriteLine(_local_ahaddress.ToBigInteger().ToString() + ' ' + local_ta_configuration.Address + ' ' + (port%25000));
      }
    }
Exemple #34
0
  static void Main(string[] args)  
  {
   
    RandomNumberGenerator rng = new RNGCryptoServiceProvider();
    
    //Initialize hosts
    Console.WriteLine("\n\n---------------------------------------\n\n");
    int port = 20287;
    int net_size = 3;
    string net_type = "function";
    if( args.Length > 0 ) {
      net_size = Int32.Parse(args[0]);
    }
    if( args.Length > 1 ) {
      net_type = args[1];
    }
    int ms_sleep = 0;
    if( args.Length > 2 ) {
      ms_sleep = Int32.Parse(args[2]);
    }
    bool wait_after_connect = true;
    if( args.Length > 3 ) {
      ///@todo we really need better option parsing here
      wait_after_connect = false;
    }
    ArrayList node_list = new ArrayList();
    Hashtable add_to_node = new Hashtable();
    PathELManager pem = null;
    for (int loop=0;loop<net_size;loop++)
    {
      //create and initialize new host
      //create one new node for each host
      AHAddress tmp_add = new AHAddress(rng);
      Node tmp_node = new StructuredNode(tmp_add, "bstland");
      //Node tmp_node = new HybridNode(tmp_add, "bstland");
      
      node_list.Add(tmp_node);
      add_to_node[tmp_add] = tmp_node;
      
      //long small_add = 2*(loop+1);
      //Node tmp_node = new StructuredNode(new AHAddress( new BigInteger(small_add)) );
      PType path_p = PType.Protocol.Pathing;
      switch(net_type) {
        case "tcp":
          tmp_node.AddEdgeListener(new TcpEdgeListener(port+loop));
	        break;
        case "udp":
          tmp_node.AddEdgeListener(new UdpEdgeListener(port+loop));
          break;
        case "function":
          tmp_node.AddEdgeListener(new FunctionEdgeListener(port+loop));
          break;
        case "path":
          if( pem == null ) {
            EdgeListener el = new UdpEdgeListener(port);
            pem = new PathELManager(el);  
            pem.Start();
          }
          //Pass path messages to the pem:
          tmp_node.DemuxHandler.GetTypeSource(path_p).Subscribe(pem, path_p);
          tmp_node.AddEdgeListener(pem.CreatePath( (port+loop).ToString() ));
          Console.WriteLine(port+loop);
          break;
        case "single_path":
          EdgeListener myel = new UdpEdgeListener(port+loop);
          //Test "default" path edge listener:
          pem = new PathELManager(myel);
          pem.Start();
          tmp_node.DemuxHandler.GetTypeSource(path_p).Subscribe(pem, path_p);
          //Make the default path:
          tmp_node.AddEdgeListener(pem.CreateRootPath());
          break;
        default:
          throw new Exception("Unknown net type: " + net_type);
      }
      //tmp_node.AddEdgeListener(new FunctionEdgeListener(port+loop));
      for (int loop2=0;loop2<net_size;loop2++) {
        if (loop == loop2) {
          continue;
        }
        int other_port = port+loop2;
        string ta_str = null;
        switch(net_type) {
          case "tcp":
            ta_str = "brunet.tcp://127.0.0.1:";
            break;
          case "udp":
            ta_str = "brunet.udp://127.0.0.1:";
            break;
          case "function":
            ta_str = "brunet.function://localhost:";
            break;
          case "path":
            ta_str = String.Format("brunet.udp://127.0.0.1:{0}/",port);
           break;
          case "single_path":
            ta_str = "brunet.udp://127.0.0.1:";
            break;
          default:
            throw new Exception("Unknown net type: " + net_type);
        }
        ta_str = ta_str + other_port.ToString();
        TransportAddress this_ta = TransportAddressFactory.CreateInstance(ta_str);
        tmp_node.RemoteTAs.Add(this_ta);
      }
    }
    
    //This logs the changes in connection table
    BootStrapTester bst = new BootStrapTester(node_list);
    if( bst != null ) {
    //This is just here to prevent a warning for
    //not using bst, which is just an observer
    }
    //Get Connected:
    int total_started = 0;
    ArrayList rnd_list = (ArrayList)node_list.Clone();
    Random rnd = new Random();
    for(int j = 0; j < rnd_list.Count; j++) {
          //Swap the j^th position with this position:
          int i = rnd.Next(j, rnd_list.Count);
          if( i != j ) {
            object o = rnd_list[i];
            rnd_list[i] = rnd_list[j];
            rnd_list[j] = o;
          }
    }
    ArrayList c_threads = new ArrayList(); 
    //var xrms = new Brunet.Rpc.XmlRpcManagerServer(20000);
    int cnt = 0;
    foreach( Node item in rnd_list)
    {
      Thread t = new Thread( item.Connect );
      c_threads.Add(t);
      t.Start();
      //xrms.Add(item, "xm" + cnt++ + ".rem");
      Console.WriteLine(item.Address.ToString()
		      + " RemoteTAs count: " + item.RemoteTAs.Count);
      total_started++;
      Console.WriteLine("Started: " + total_started.ToString());
      //Thread.Sleep(10000);
      Thread.Sleep(ms_sleep);
      //Console.ReadLine();
      //foreach (TransportAddress item2 in item.RemoteTAs)
      //  Console.WriteLine(item2);
    
    }

    System.Console.Out.WriteLine("Finished with BootStrapTester.Main");
    string[] this_command = new string[] { "Q" };
    if( wait_after_connect) {
      Console.WriteLine("Enter Q to stop");
      this_command = Console.ReadLine().Split(' ');
    }
    while( this_command[0] != "Q" ) {
      if( this_command[0] == "D" ) { 
        //Disconnect a node:
        int node = -1;
        try {
          node = Int32.Parse(this_command[1]);
          Node to_disconnect = (Node)node_list[node];
          Console.WriteLine("About to Disconnect: {0}", to_disconnect.Address);
	        to_disconnect.Disconnect();
          bst.Remove(to_disconnect);
        }
        catch(Exception) {
 
        }
      }
      if( this_command[0] == "abort" ) { 
        //Disconnect a node:
        int node = -1;
        try {
          node = Int32.Parse(this_command[1]);
          Node to_abort = (Node)node_list[node];
          Console.WriteLine("About to Abort: {0}", to_abort.Address);
	        to_abort.Abort();
          bst.Remove(to_abort);
        }
        catch(Exception) {
 
        }
      }
      if( this_command[0] == "P" ) {
        //Pick a random pair of nodes to ping:
	Ping(node_list);
      }
      if( this_command[0] == "BP" ) {
        try {
          int reps = Int32.Parse(this_command[1]);
          bst.BenchmarkPing(reps);
        }
        catch(Exception x) {
          Console.WriteLine(x);
        }
      }
      if( this_command[0] == "BH" ) {
        try {
          int reps = Int32.Parse(this_command[1]);
          bst.BenchmarkHops(reps);
        }
        catch(Exception x) {
          Console.WriteLine(x);
        }
      }
      if( this_command[0] == "T" ) {
        //Pick a random pair of nodes to ping:
	TraceRoute(node_list);
      }
      if( wait_after_connect ) {
        this_command = Console.ReadLine().Split(' ');
      }
    }
    
    foreach(Node n in node_list)
    {
      n.Disconnect();
    }
    if( pem != null ) {
      pem.Stop();
    }
    //Block until all Connect threads finish.
    //foreach(Thread t in c_threads) {
    //  t.Join();
    //}

  }
Exemple #35
0
    static void Main(string[] args)
    {
      //first, remove the log file
      if(File.Exists("time_stamp.log")){
	File.Delete("time_stamp.log");
      }

      String config_file = args[0];
      NetworkConfiguration network_configuration = NetworkConfiguration.Deserialize(config_file);

      int port_selection = Convert.ToInt32(args[1]); //There will be 10 different ports available for use: 0, 1, 2..
      //for example, node 0 on a machine will use port_selection # 0, node 1 on a machine will use port_selection # 1

      ///There will be multiple BruNet nodes on the same machine. The following is a list of possible ports used
      int list_size = 10;
      int [] port_list = new int[list_size];
      for(int i = 0; i < list_size; i++){
	port_list[i] = 25010 + i*10;
      }
	
      ///The line below is used when there is only one node per machine
      //int local_host_index = network_configuration.GetLocalHostIndex();                                                                
	
      int desired_port = port_list[port_selection];
      int local_host_index = network_configuration.GetLocalHostIndex(desired_port); 

      NodeConfiguration this_node_configuration = (NodeConfiguration)network_configuration.Nodes[local_host_index];
      TransportAddressConfiguration local_ta_configuration = (TransportAddressConfiguration)this_node_configuration.TransportAddresses[0];
      short port = local_ta_configuration.Port;

      SHA1 sha = new SHA1CryptoServiceProvider();
      String local_ta = local_ta_configuration.GetTransportAddressURI();
      //We take the local transport address plus the port number to be hashed to obtain a random AHAddress
      byte[] hashedbytes = sha.ComputeHash(Encoding.UTF8.GetBytes(local_ta + port));
      //inforce type 0
      hashedbytes[Address.MemSize - 1] &= 0xFE;
      AHAddress _local_ahaddress = new AHAddress(hashedbytes);
      Node this_node = new StructuredNode( _local_ahaddress );
      ///Node this_node = new HybridNode( new AHAddress( new BigInteger( 2*(local_host_index+1) ) ) );      

      String file_string = "brunetadd" + Convert.ToString(desired_port) + ".log";
      StreamWriter sw = new StreamWriter(file_string, false);
      sw.WriteLine( "local_address " + this_node.Address.ToBigInteger().ToString() + " " + Dns.GetHostName()); 
      sw.Close();      

      if ( local_ta_configuration.Protocol == "tcp" ) {
        this_node.AddEdgeListener( new TcpEdgeListener(port) );
      } 
      else if( local_ta_configuration.Protocol == "udp" ) {
        this_node.AddEdgeListener( new UdpEdgeListener(port) );        
      }

      int remote_node_index = local_host_index-1;
      int num_remote_ta = 20; //20 nodes on the list to try to bootstrap to

      if (local_host_index!=0) {
        NodeConfiguration remote_node_configuration = (NodeConfiguration)network_configuration.Nodes[0];
        TransportAddressConfiguration remote_ta_configuration = (TransportAddressConfiguration)remote_node_configuration.TransportAddresses[0];

        String remote_ta = remote_ta_configuration.GetTransportAddressURI(); 
        this_node.RemoteTAs.Add( TransportAddressFactory.CreateInstance( remote_ta  ) );
      }
      
      while ( (remote_node_index>=0) && (num_remote_ta>=0) ) { 
        NodeConfiguration remote_node_configuration = (NodeConfiguration)network_configuration.Nodes[remote_node_index];
        TransportAddressConfiguration remote_ta_configuration = (TransportAddressConfiguration)remote_node_configuration.TransportAddresses[0];

        String remote_ta = remote_ta_configuration.GetTransportAddressURI(); 
        this_node.RemoteTAs.Add( TransportAddressFactory.CreateInstance( remote_ta  ) );

        System.Console.WriteLine("Adding {0}", remote_ta);

          remote_node_index--;
          num_remote_ta--;
        }

      //EchoTester echo_printer = new EchoTester();
      //this_node.Subscribe(AHPacket.Protocol.Echo, echo_printer);
      StreamWriter stamp_sw = new StreamWriter("time_stamp.log", true);
      stamp_sw.WriteLine("Local_node: {0} start_time_GMT: {1}:{2}", Dns.GetHostName(), DateTime.Now.ToUniversalTime().ToString(),
			DateTime.Now.ToUniversalTime().Millisecond );
      stamp_sw.Close();

      this_node.Connect();

    }
Exemple #36
0
    public void LMSerializationTest()
    {
      NodeInfo n1 = NodeInfo.CreateInstance(null, TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:45"));
      RandomNumberGenerator rng = new RNGCryptoServiceProvider();      
      AHAddress tmp_add = new AHAddress(rng);
      LinkMessage l1 = new LinkMessage(ConnectionType.Structured, n1,
				       NodeInfo.CreateInstance(new DirectionalAddress(DirectionalAddress.Direction.Left),
				       TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:837")), tmp_add.ToString() );
      RoundTripHT(l1);
      StringDictionary attrs = new StringDictionary();
      attrs["realm"] = "test_realm";
      attrs["type"] = "structured.near";
      LinkMessage l3 = new LinkMessage(attrs, n1, n1, tmp_add.ToString());
      RoundTripHT(l3);
    }
Exemple #37
0
 private string GetRandomNodeAddr() {
   AHAddress addr = new AHAddress(new RNGCryptoServiceProvider());
   return addr.ToString();
 }
Exemple #38
0
    public static void Main(string []args) {
      if (args.Length < 1) {
	Console.WriteLine("please specify the number edge protocol."); 
        Environment.Exit(0);
      }
      if (args.Length < 2) {
        Console.WriteLine("please specify the number of p2p nodes."); 
        Environment.Exit(0);
      }
      if (args.Length < 3) {
        Console.WriteLine("please specify the number of missing edges."); 
        Environment.Exit(0);
      }
      string proto = "function";
      try {
	proto = args[0].Trim();
      } catch(Exception) {}

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

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

      Random rand = new Random();

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

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

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

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

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

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

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

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

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

      int missing_rpcs = 0;
      int correct_rpcs = 0;
      int incorrect_rpcs = 0;
      Hashtable queue_to_address = new Hashtable();
      for (int i = 0; i < network_size; i++) {
	for (int j = 0; j < network_size; j++) {
	  
	  int src_idx = i;
	  int dest_idx = j;
	  Node src_node = (Node) _sorted_node_list.GetByIndex(src_idx);
	  Node dest_node = (Node) _sorted_node_list.GetByIndex(dest_idx);
	  //Console.WriteLine("{0} -> {1}", src_idx, dest_idx);
	  Address dest_address = (Address) dest_node.Address;
	  ISender s = new AHExactSender(src_node, dest_address);
	  RpcManager rpc_man = src_node.Rpc;
	  Channel q = new Channel();
	  lock (_class_lock) {
	    queue_to_address[q] = dest_address;
	  }
	  q.CloseAfterEnqueue();
	  q.CloseEvent += delegate(object o, EventArgs cargs) {
	    lock(_class_lock) {
	      Channel qu = (Channel) o;
	      if (qu.Count == 0) {
		missing_rpcs++;
	      }
	      queue_to_address.Remove(qu);
	    }
	  };
	  q.EnqueueEvent += delegate(object o, EventArgs cargs) {
	    lock(_class_lock) {
	      Channel qu = (Channel) o;
	      RpcResult rpc_reply = (RpcResult) qu.Peek();
	      byte []result = (byte[]) rpc_reply.Result;
	      Address target = new AHAddress(result);
	      if (target.Equals(queue_to_address[qu])) {
		correct_rpcs++;
	      } else {
		incorrect_rpcs++;
	      }
	    }
	  };
	  rpc_man.Invoke(s, q, "rpc_routing_test.GetIdentification", new object[]{});
	}
      }
      
      //wait for 10 more seconds
      while (true) {
	int c = -1;
	lock(_class_lock) {
	  c = incorrect_rpcs + missing_rpcs + correct_rpcs;
	}
	if (c < network_size*network_size) {
	  Console.WriteLine("Going to sleep for 10 seconds.");
	  System.Threading.Thread.Sleep(10000);
	} else {
	  break;
	}
      }
      
      Console.WriteLine("Final statistics");
      Console.WriteLine("correct rpcs: {0}, incorrect rpcs: {1}, missing rpcs: {2}", 
			correct_rpcs, incorrect_rpcs, missing_rpcs);
      
      System.Environment.Exit(1);
    }
Exemple #39
0
      public Address TransAddressCheck(Address a) {
        BigInteger one = new BigInteger(1);
  	BigInteger s_Bin = one << 80;
	BigInteger addr = a.ToBigInteger();
	BigInteger addr_j = addr % s_Bin;
	BigInteger addr_i = (addr - addr_j) / s_Bin;
	BigInteger q = addr_j * s_Bin + addr_i;
	byte [] target = Address.ConvertToAddressBuffer(q);
	Address.SetClass(target, a.Class);
	Address q_addr = new AHAddress(target);
    	return q_addr;
      }
Exemple #40
0
 /**
 <summary>This method returns matching querying address from caching address.<\summary>
 */
 
 protected AHAddress AddressTranspose(AHAddress a) {
   BigInteger one = new BigInteger(1);
   BigInteger s_BIN = one << 80;
   BigInteger addr = a.ToBigInteger();
   BigInteger addr_j = addr % s_BIN;
   BigInteger addr_i = (addr - addr_j) / s_BIN;
   BigInteger q = addr_j * s_BIN + addr_i;
   byte[] target = Address.ConvertToAddressBuffer(q);
   Address.SetClass(target, a.Class);
   AHAddress q_addr = new AHAddress(target);
   return q_addr;
 }
    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!");
    }
Exemple #42
0
 /// <summary>Sends a packet from A to B returning the delay and hop count
 /// using the Greedy Routing Algorithm.</summary>
 public List<SendPacketResult> SendPacket(AHAddress from, AHAddress to)
 {
   return SendPacket(from, to, AHHeader.Options.Greedy);
 }
Exemple #43
0
 public AHAddress GetNearTarget(AHAddress address)
 {
   /**
    * try to get at least one neighbor using forwarding through the 
    * leaf .  The forwarded address is 2 larger than the address of
    * the new node that is getting connected.
    */
   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);
 }
Exemple #44
0
    protected AHAddress FindNodeNearestToAddress(AHAddress addr)
    {
      int index = _addrs.BinarySearch(addr);
      AHAddress to_use = addr;

      if(index < 0) { 
        index = ~index;
        if(index == _addrs.Count) {
          index = 0;
        }
        AHAddress right = _addrs[index];
        if(index == 0) {
          index = _addrs.Count - 1;
        }
        AHAddress left = _addrs[index - 1];
        if(right.DistanceTo(addr) < left.DistanceTo(addr)) {
          to_use = right;
        } else {
          to_use = left;
        }
      }

      return to_use;
    }
Exemple #45
0
    /// <summary>Calculates a shortcut using a harmonic distribution as in a
    /// Symphony-lke shortcut.</summary>
    protected virtual AHAddress ComputeShortcutTarget(AHAddress addr)
    {
      int network_size = _addrs.Count;
      double logN = (double)(Brunet.Address.MemSize * 8);
      double logk = Math.Log( (double) network_size, 2.0 );
      double p = _rand.NextDouble();
      double ex = logN - (1.0 - p)*logk;
      int ex_i = (int)Math.Floor(ex);
      double ex_f = ex - Math.Floor(ex);
      //Make sure 2^(ex_long+1)  will fit in a long:
      int ex_long = ex_i % 63;
      int ex_big = ex_i - ex_long;
      ulong dist_long = (ulong)Math.Pow(2.0, ex_long + ex_f);
      //This is 2^(ex_big):
      BigInteger big_one = 1;
      BigInteger dist_big = big_one << ex_big;
      BigInteger rand_dist = dist_big * dist_long;

      // Add or subtract random distance to the current address
      BigInteger t_add = addr.ToBigInteger();

      // Random number that is 0 or 1
      if( _rand.Next(2) == 0 ) {
        t_add += rand_dist;
      }
      else {
        t_add -= rand_dist;
      }

      BigInteger target_int = new BigInteger(t_add % Address.Full);
      if((target_int & 1) == 1) {
        target_int -= 1;
      }

      byte[]buf = Address.ConvertToAddressBuffer(target_int);

      Address.SetClass(buf, AHAddress.ClassValue);
      return new AHAddress(buf);
    }
Exemple #46
0
 protected AHAddress GenerateAddress()
 {
   AHAddress addr = null;
   do {
     byte[] baddr = new byte[Address.MemSize];
     _rand.NextBytes(baddr);
     Address.SetClass(baddr, AHAddress.ClassValue);
     addr = new AHAddress(MemBlock.Reference(baddr));
   } while(_addr_to_node.ContainsKey(addr));
   return addr;
 }
Exemple #47
0
 /**
  * Recursive function to compute the latency of an Rpc call
  * by accumulating measured latencies of individual hops.
  * @param target address of the target
  */
 public void ComputePathLatencyTo(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_latency"] = GetMeasuredLatency(next_closest.Address);
     my_entry["next_contype"] = next_closest.ConType;
     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.Edge, result, "ncserver.ComputePathLatencyTo", 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);  
   }
 }
Exemple #48
0
    /**
     * Read the address out of the buffer  This makes a copy
     * and calls Parse on the copy.  This is a "convienience" method.
     * @throw ParseException if the buffer is not a valid address
     */
    static public Address Parse(MemBlock mb)
    {
#if BRUNET_SIMULATOR
      Address a = null;
#else
      //Read some of the least significant bytes out,
      //AHAddress all have last bit 0, so we skip the last byte which
      //will have less entropy
      ushort idx = (ushort)NumberSerializer.ReadShort(mb, Address.MemSize - 3);
      Address a = _mb_cache[idx];
      if( a != null ) {
        if( a.ToMemBlock().Equals(mb) ) {
          return a;
        }
      }
#endif
      //Else we need to read the address and put it in the cache
      try {
        if( 2 * mb.Length < mb.ReferencedBufferLength ) {
            /*
             * This MemBlock is much smaller than the array
             * we are referencing, don't keep the big one
             * in scope, instead make a copy
             */
          mb = MemBlock.Copy((ICopyable)mb);
        }
        int add_class = Address.ClassOf(mb);
        switch (add_class) {
        case AHAddress.ClassValue:
          a = new AHAddress(mb);
          break;
        case DirectionalAddress.ClassValue:
          a = new DirectionalAddress(mb);
          break;
        default:
          a = null;
          throw new ParseException("Unknown Address Class: " +
                                   add_class + ", buffer:" +
                                   mb.ToString());
        }
#if BRUNET_SIMULATOR
        return SimulatorCache(a);
#else
        //Cache this result:
        _mb_cache[ idx ] = a;
        return a;
#endif
      }
      catch(ArgumentOutOfRangeException ex) {
        throw new ParseException("Address too short: " +
                                 mb.ToString(), ex);
      }
      catch(ArgumentException ex) {
        throw new ParseException("Could not parse: " +
                                 mb.ToString(), ex);
      }
    }
Exemple #49
0
    public void TestService() {
      NCService nc_service = new NCService();
      DateTime now = DateTime.UtcNow;
      Address addr_remote = new AHAddress(new RNGCryptoServiceProvider());
      Address addr_remote1 = new AHAddress(new RNGCryptoServiceProvider());
      Address addr_remote2 = new AHAddress(new RNGCryptoServiceProvider());

      nc_service.ProcessSample(now + new TimeSpan(0, 0, 5), "local-test", addr_remote, 
             new Point(new double[] {(double) 3.0, (double) 4.0}, 0),
             (double) 0.9, (double)10.0); 
      NCService.VivaldiState state = nc_service.State;
      

      nc_service.ProcessSample(now + new TimeSpan(0, 0, 6), "local-test",addr_remote1, 
             new Point(new double[] {(double) 10.0, (double) 2.0}, 0),
             (double) 0.9, (double)10.0); 

      nc_service.ProcessSample(now + new TimeSpan(0, 0, 6), "local-test",addr_remote2, 
             new Point(new double[] {(double) 5.0, (double) 6.0}, 0),
             (double) 0.9, (double)10.0); 


      nc_service.ProcessSample(now + new TimeSpan(0, 0, 7), "local-test",addr_remote, 
             new Point(new double[] {(double) 3.0, (double) 4.0}, 0),
             (double) 0.8, (double)12.0); 

      nc_service.ProcessSample(now + new TimeSpan(0, 0, 8), "local-test",addr_remote1, 
             new Point(new double[] {(double) 10.0, (double) 2.0}, 0),
             (double) 0.8, (double)12.0); 


      nc_service.ProcessSample(now + new TimeSpan(0, 0, 9), "local-test",addr_remote, 
             new Point(new double[] {(double) 3.0, (double) 4.0}, 0),
             (double)0.7, (double)13.0); 

      nc_service.ProcessSample(now + new TimeSpan(0, 0, 11), "local-test",addr_remote1, 
             new Point(new double[] {(double) 10.0, (double) 2.0}, 0),
             (double)0.7, (double)13.0); 


      nc_service.ProcessSample(now + new TimeSpan(0, 0, 12), "local-test",addr_remote, 
             new Point(new double[] {(double) 3.0, (double) 4.0}, 0),
             (double)0.6, (double)10.0); 

      nc_service.ProcessSample(now + new TimeSpan(0, 0, 13), "local-test",addr_remote1, 
             new Point(new double[] {(double) 10.0, (double) 2.0}, 0),
             (double)0.6, (double)10.0);       

      state = nc_service.State;
      Console.Error.WriteLine("position: {0}, error: {1}", state.Position, state.WeightedError);
    }
    /// <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);
    }
Exemple #51
0
    /**
    <summary>Called by a Dht client to store data here, this supports both Puts
    and Creates by using the unique parameter.</summary>
    <remarks>Puts will store the value no matter what, Creates will only store
    the value if they are the first ones to store data on that key.  This is
    the first part of a Put operation.  This calls PutHandler on itself and
    the neighbor nearest to the key, which actually places the data into the
    store.  The result is returned to the client upon completion of the call
    to the neighbor, if that fails the data is removed locally and an exception
    is sent to the client indicating failure.</remarks>
    <param name="key">The index to store the data at.</param>
    <param name="value">Data to store at the key.</param>
    <param name="ttl">Dht lease time in seconds</param>
    <param name="unique">True if this should perform a create, false otherwise.
    </param>
    <param name="rs">The return state sent back to the RpcManager so that it
    knows who to return the result to.</param>
    <returns>True on success, thrown exception on failure</returns>
    <exception cref="Exception">Data is too large, unresolved remote issues,
    or the create is no successful</exception>
    */

    public bool Put(MemBlock key, MemBlock value, int ttl, bool unique, object rs) {
      if(value.Length > MAX_BYTES) {
        throw new Exception(String.Format(
          "Dht only supports storing data smaller than {0} bytes.", MAX_BYTES));
      }
      PutHandler(key, value, ttl, unique);
      Channel remote_put = new Channel();
      remote_put.CloseAfterEnqueue();
      remote_put.CloseEvent += delegate(Object o, EventArgs eargs) {
        object result = false;
        try {
          result = remote_put.Dequeue();
          RpcResult rpcResult = (RpcResult) result;
          result = rpcResult.Result;
          if(result.GetType() != typeof(bool)) {
            throw new Exception("Incompatible return value.");
          }
          else if(!(bool) result) {
            throw new Exception("Unknown error!");
          }
        }
        catch (Exception e) {
          lock(_sync) {
            _data.RemoveEntry(key, value);
          }
          result = new AdrException(-32602, e);
        }
        _rpc.SendResult(rs, result);
      };

      try {
        Address key_address = new AHAddress(key);
        ISender s = null;
        var structs =
        _node.ConnectionTable.GetConnections(ConnectionType.Structured);
        // We need to forward this to the appropriate node!
        if(((AHAddress)_node.Address).IsLeftOf((AHAddress) key_address)) {
          var con = structs.GetRightNeighborOf(_node.Address);
          s = con.Edge;
        }
        else {
          var con = structs.GetLeftNeighborOf(_node.Address);
          s = con.Edge;
        }
        _rpc.Invoke(s, remote_put, "dht.PutHandler", key, value, ttl, unique);
      }
      catch (Exception) {
        lock(_sync) {
          _data.RemoveEntry(key, value);
        }
        throw;
      }
      return true;
    }
Exemple #52
0
    static void Main(string[] args)
    {
    //log.Debug( "Starting the Brunet Echo Tester" );

      String config_file = args[0];
      NetworkConfiguration network_configuration = NetworkConfiguration.Deserialize(config_file);

      int port_selection = Convert.ToInt32(args[1]); //There will be 10 different ports available for use: 0, 1, 2..
      //for example, node 0 on a machine will use port_selection # 0, node 1 on a machine will use port_selection # 1

      ///There will be multiple BruNet nodes on the same machine. The following is a list of possible ports used
      int list_size = 150;
      int [] port_list = new int[list_size];
      for(int i = 0; i < list_size; i++){
	port_list[i] = 25000 + i;
      }
	
      ///The line below is used when there is only one node per machine
      //int local_host_index = network_configuration.GetLocalHostIndex();                                                                
	
      int desired_port = port_list[port_selection];
      int local_host_index = network_configuration.GetLocalHostIndex(desired_port); 

      NodeConfiguration this_node_configuration = (NodeConfiguration)network_configuration.Nodes[local_host_index];
      TransportAddressConfiguration local_ta_configuration = (TransportAddressConfiguration)this_node_configuration.TransportAddresses[0];
      short port = local_ta_configuration.Port;

      SHA1 sha = new SHA1CryptoServiceProvider();
      String local_ta = local_ta_configuration.GetTransportAddressURI();
      //We take the local transport address plus the port number to be hashed to obtain a random AHAddress
      byte[] hashedbytes = sha.ComputeHash(Encoding.UTF8.GetBytes(local_ta + port));
      //inforce type 0
      hashedbytes[Address.MemSize - 1] &= 0xFE;
      AHAddress _local_ahaddress = new AHAddress(hashedbytes);
      Node this_node = new StructuredNode( _local_ahaddress );
      ///Node this_node = new HybridNode( new AHAddress( new BigInteger( 2*(local_host_index+1) ) ) );      

      String file_string = "./data/brunetadd" + Convert.ToString(desired_port) + ".log";
      StreamWriter sw = new StreamWriter(file_string, false);
      sw.WriteLine( "local_address " + this_node.Address.ToBigInteger().ToString() + " " + Dns.GetHostName()
		      + ":" + port); 
      sw.Close();

      if ( local_ta_configuration.Protocol == "tcp" ) {
        this_node.AddEdgeListener( new TcpEdgeListener(port) );
      } 
      else if( local_ta_configuration.Protocol == "udp" ) {
        this_node.AddEdgeListener( new UdpEdgeListener(port) );        
      }

      int remote_node_index = local_host_index-1;
      int num_remote_ta = 20; //20 nodes on the list to try to bootstrap to

      if (local_host_index!=0) {
        NodeConfiguration remote_node_configuration = (NodeConfiguration)network_configuration.Nodes[0];
        TransportAddressConfiguration remote_ta_configuration = (TransportAddressConfiguration)remote_node_configuration.TransportAddresses[0];

        String remote_ta = remote_ta_configuration.GetTransportAddressURI(); 
        this_node.RemoteTAs.Add( TransportAddressFactory.CreateInstance( remote_ta  ) );
      }
      
      while ( (remote_node_index>=0) && (num_remote_ta>=0) ) { 
        NodeConfiguration remote_node_configuration = (NodeConfiguration)network_configuration.Nodes[remote_node_index];
        TransportAddressConfiguration remote_ta_configuration = (TransportAddressConfiguration)remote_node_configuration.TransportAddresses[0];

        String remote_ta = remote_ta_configuration.GetTransportAddressURI(); 
        this_node.RemoteTAs.Add( TransportAddressFactory.CreateInstance( remote_ta  ) );

        System.Console.WriteLine("Adding {0}", remote_ta);

          remote_node_index--;
          num_remote_ta--;
        }

     /* NodeConfiguration remote_node_configuration = (NodeConfiguration)network_configuration.Nodes[remote_node_index];
      TransportAddressConfiguration remote_ta_configuration = (TransportAddressConfiguration)remote_node_configuration.TransportAddresses[0];

      String remote_ta = remote_ta_configuration.GetTransportAddressURI(); 
      this_node.RemoteTAs.Add( TransportAddressFactory.CreateInstance( remote_ta  ) );*/
 
      EchoTester echo_printer = new EchoTester();
      this_node.GetTypeSource(PType.Protocol.Echo).Subscribe(echo_printer, this_node);

      this_node.Connect();

       //Send a "hello message" to a random neighbor

      ASCIIEncoding ascii = new ASCIIEncoding();

      //Make the target addresses      
      AHAddress target  = new AHAddress( new BigInteger( 2*(remote_node_index+1) ) );

      string hello_msg = "hello, brunet";
      int byteCount = ascii.GetByteCount(hello_msg);
      byte[] bytes = new byte[byteCount + 1];
      ascii.GetBytes(hello_msg, 0, hello_msg.Length, bytes, 1);

      // update the payload
      // This is a request, so the first byte is greater than zero
      bytes[0] = (byte) 1;
      ICopyable p = new CopyList(PType.Protocol.AH,
                                 new AHHeader(0, 30, this_node.Address,
                                              target, AHHeader.Options.Greedy),
                                 PType.Protocol.Echo, MemBlock.Reference(bytes));

      ///RDP Experiment: sending the echo packet periodically
/*      int seq = 0;
      while(true){
	int start_time = System.DateTime.Now.Millisecond;
	this_node.Send(p);
	Console.WriteLine("Seq = {0}, Start Time = {1}", seq, start_time);
        System.Threading.Thread.Sleep(10000);
	seq++;
      }*/


///The following is a while-loop for the local node to Brunet-ping all other nodes in the network
      System.Threading.Thread.Sleep(60000);  ///IMPORTANT: change this parameter so we wait longer for larger network
      Random uid_generator = new Random( DateTime.Now.Millisecond + local_ta.GetHashCode() + port);
      bytes = new byte[5];
      int target_index = 0, num_pings = 10, wait_time = 10000; //the wait_time is in ms
      double ping_time;
      PingWrapper pw = new PingWrapper();    
      
      while( target_index < network_configuration.Nodes.Count ){
 
 	  if(target_index != local_host_index){///we do not ping the local machine
	      NodeConfiguration target_node_configuration = (NodeConfiguration)network_configuration.Nodes[target_index];
	      TransportAddressConfiguration target_ta_configuration = (TransportAddressConfiguration)target_node_configuration.TransportAddresses[0];
	      short target_port = target_ta_configuration.Port;
	      double ping1 = pw.Ping(target_ta_configuration.Address, 10000);
	      double ping2 = pw.Ping(target_ta_configuration.Address, 10000);
	      if(ping1 >= 0 || ping2 >= 0){ //we gather the data only when the node is ping-able
		  sha = new SHA1CryptoServiceProvider();
		  String target_ta = target_ta_configuration.GetTransportAddressURI();
		  //We take the transport address plus the port number to be hashed to obtain a random AHAddress
		  hashedbytes = sha.ComputeHash(Encoding.UTF8.GetBytes(target_ta + target_port));
		  //inforce type 0
		  hashedbytes[Address.MemSize - 1] &= 0xFE;
		  AHAddress _target_ahaddress = new AHAddress(hashedbytes);	      
		  for(int i = 0; i < num_pings; i++){
		    //ping and Brunet-ping the target node for a number of times
		    int uid = uid_generator.Next(); //this is the unique id of the packet
		    // update the payload
		    // This is a request, so the first byte is greater than zero
		    bytes[0] = (byte) 1;
		    NumberSerializer.WriteInt(uid, bytes, 1);
                    p = new CopyList(PType.Protocol.AH,
                                 new AHHeader(0, 30, this_node.Address,
                                              _target_ahaddress, AHHeader.Options.Greedy),
                                 PType.Protocol.Echo, MemBlock.Reference(bytes));

		    this_node.Send(p);
		    ping_time = pw.Ping(target_ta_configuration.Address, wait_time); //wait wait_time number of ms
		    System.Console.WriteLine("Ping time: {0}",ping_time);
		    System.Threading.Thread.Sleep(wait_time); 
		  }//end of for-loop 
		}                  

          }//end of if-loop    

    
    	  target_index++;
       }//end of while-loop

     }
    /**
    <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 CacheEntry).</param>
    <remarks>new_start = mid - rg_size/2, new_end = mid + rg_size/2 </remarks>
     */
    public void ReAssignRange(BigInteger rg_size) {
      AHAddress start_addr = (AHAddress)this.Start;
      AHAddress end_addr = (AHAddress)this.End;
      // calculate middle address of range
      BigInteger start_int = start_addr.ToBigInteger();
      BigInteger end_int = end_addr.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 (!mid_addr.IsBetweenFromLeft(start_addr, end_addr)) {
        mid_int += Address.Half;
	mid_addr = new AHAddress(mid_int);
      }
      //addresses for new range
      BigInteger rg_half = rg_size / 2;
      if (rg_half % 2 == 1) { rg_half -= 1; }
      BigInteger n_st = mid_int - rg_half;
      /*
      if (n_st < 0) { //underflow
	n_st += AHAddress.Full; 
      }
      */
      BigInteger n_ed = n_st + rg_size;
      /*
      if (n_ed > AHAddress.Full) { //overflow
	n_ed -= AHAddress.Full; 
      }
      */
      /// underflow and overflow are handled by AHAddress class.
      AHAddress n_a = new AHAddress(n_st);
      AHAddress n_b = new AHAddress(n_ed);
      //Console.WriteLine("old range BigInt: ({0},{1}), new range Big Int ({2},{3})", up, down, n_st, n_ed);
      //Console.WriteLine("old range: ({0},{1}), new range({2},{3}), mid_addr: {4}", a,b,n_a,n_b, mid_addr);
      this.Start = n_a;
      this.End = n_b;
    }
Exemple #54
0
  public static int Main(string[] args) {

    /**
     * Get the arguments
     */
    if( args.Length < 2 ) {
      Console.Error.WriteLine("usage: SNodeExample.exe [tcp|udp] port remota_ta0 remote_ta1 ...");
      return 0;
    }

    /**
     * Make the edge listener:
     */
    EdgeListener el = null;
    int port = Int32.Parse( args[1] );
    if( args[0].ToLower() == "tcp" ) {
      el = new TcpEdgeListener(port);
    }
    else if( args[0].ToLower() == "udp" ) {
      el = new UdpEdgeListener(port);
    }
    /**
     * Create a random address for our node.
     * Some other application might want to select the address
     * a particular way, or reuse a previously selected random
     * address.  If the addresses are not random (or the output
     * of secure hashes) the network might not behave correctly.
     */
    RandomNumberGenerator rng = new RNGCryptoServiceProvider();
    AHAddress tmp_add = new AHAddress(rng);
    Console.WriteLine("Address: {0}", tmp_add);
    /**
     * Make the node that lives in a particular
using Brunet.Messaging;
     * namespace (or realm) called "testspace"
     */
    Node tmp_node = new StructuredNode(tmp_add, "testspace");
    ReqrepManager rrman = tmp_node.Rrm;
    ReqrepExample irh = new ReqrepExample();
    tmp_node.GetTypeSource(PType.Protocol.Chat).Subscribe(irh, tmp_node);
    /**
     * Add the EdgeListener
     */
    tmp_node.AddEdgeListener( el );
    /**
     * Tell the node who it can connect to:
     */
    for(int i = 2; i < args.Length; i++) {
      tmp_node.RemoteTAs.Add( TransportAddressFactory.CreateInstance( args[i] ) );
    }
    /**
     * Now we connect
     */
    tmp_node.Connect();
    Console.WriteLine("Connected");
    /**
     * In a real application, we would create some IAHPacketHandler
     * objects and do:
     * tmp_node.Subscribe( )
     * finally, we could send packets using tmp_node.Send( ) or
     * tmp_node.SendTo( )
     */
    string msg = "";
    System.Text.Encoding coder = new System.Text.ASCIIEncoding();
    while( true ) {
     Console.Write("To: ");
     msg = Console.ReadLine();
     if ( msg == "q" ) { break; }
     Address dest = AddressParser.Parse(msg);
     while( msg != "." ) {
      msg = Console.ReadLine();
      int length = coder.GetByteCount(msg);
      byte[] payload = new byte[length];
      coder.GetBytes(msg, 0, msg.Length, payload, 0);
      ISender sender = new AHSender(tmp_node, dest);
      rrman.SendRequest(sender, ReqrepManager.ReqrepType.Request,
                        new CopyList(PType.Protocol.Chat, MemBlock.Reference(payload)),
			irh , null);
     }
    }
	 
    return 1;
  }
Exemple #55
0
    /// <summary>Sends a packet from A to B returning the delay and hop count.</summary>
    public List<SendPacketResult> SendPacket(AHAddress from, AHAddress to, ushort option)
    {
      AHHeader ah = new AHHeader(0, 100, from, to, option);

      GraphNode cnode = _addr_to_node[from];
      Edge cedge = null;
      AHAddress last_addr = from;
      Pair<Connection, bool> next = new Pair<Connection, bool>(null, false);

      int delay = 0;
      int hops = 0;
      var results = new List<SendPacketResult>();

      while(true) {
        next = cnode.NextConnection(cedge, ah);
        if(next.Second) {
          results.Add(new SendPacketResult(last_addr, delay, hops));
        }
        if(next.First == null) {
          break;
        }
        AHAddress caddress = next.First.Address as AHAddress;
        cnode = _addr_to_node[caddress];
        cedge = cnode.ConnectionTable.GetConnection(next.First.MainType, last_addr).Edge;
        last_addr = caddress;
        delay += (cedge as GraphEdge).Delay;
        hops++;
      }

      return results;
    }
Exemple #56
0
    public void CTMSerializationTest()
    {
      Address a = new DirectionalAddress(DirectionalAddress.Direction.Left);
      TransportAddress ta = TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:5000"); 
      NodeInfo ni = NodeInfo.CreateInstance(a, ta);

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

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

      ConnectToMessage ctm2 = new ConnectToMessage(ConnectionType.Structured, ni2, tmp_add.ToString());
      HTRoundTrip(ctm2);
      //Here is a ConnectTo message with a neighbor list:
      NodeInfo[] neighs = new NodeInfo[5];
      for(int i = 0; i < 5; i++) {
	string ta_tmp = "brunet.tcp://127.0.0.1:" + (i+80).ToString();
        NodeInfo tmp =
		NodeInfo.CreateInstance(new DirectionalAddress(DirectionalAddress.Direction.Left),
	                     TransportAddressFactory.CreateInstance(ta_tmp)
			    );
	neighs[i] = tmp;
      }
      ConnectToMessage ctm3 = new ConnectToMessage("structured", ni, neighs, tmp_add.ToString());
      HTRoundTrip(ctm3);
#if false
      Console.Error.WriteLine( ctm3.ToString() );
      foreach(NodeInfo tni in ctm3a.Neighbors) {
        Console.Error.WriteLine(tni.ToString());
      }
#endif
    }
Exemple #57
0
 public int DhtQuery(AHAddress from, byte[] key)
 {
   // Done with getting connected to nearest neighbors
   // On to querying the Dht
   MemBlock[] endpoints = MapToRing(key);
   int slowest = 0;
   foreach(MemBlock endpoint in endpoints) {
     AHAddress to = new AHAddress(endpoint);
     var request = SendPacket(from, to, AHHeader.Options.Greedy);
     var response = SendPacket(request[0].Destination, from, AHHeader.Options.Greedy);
     int delay = request[0].Delay + response[0].Delay;
     if(delay > slowest) {
       slowest = delay;
     }
   }
   return slowest;
 }
Exemple #58
0
    /// <summary>Crawls the network using the given starting address.</summary>
    public void Crawl(AHAddress start)
    {
      long total_delay = 0;
      List<int> delays = new List<int>(_addrs.Count - 1);
      long total_hops = 0;
      List<int> hops = new List<int>(_addrs.Count - 1);

      int network_size = _addrs.Count;
      int start_pos = _addr_to_index[start];
      int pos = (start_pos + 1) % network_size;

      while(pos != start_pos) {
        AHAddress current = _addrs[pos];
        var results = SendPacket(start, current);
        if(results.Count == 0) {
          throw new Exception("SendPacket failed!");
        }

        total_delay += results[0].Delay;
        delays.Add(results[0].Delay);
        total_hops += results[0].Hops;
        hops.Add(results[0].Hops);

        pos++;
        if(pos >= network_size) {
          pos -= network_size;
        }
      }

      Console.WriteLine("Crawl results:");
      double average = Average(hops);
      Console.WriteLine("\tHops: Total: {0}, Average: {1}, Stdev: {2}",
          total_hops, average, StandardDeviation(hops, average));
      average = Average(delays);
      Console.WriteLine("\tDelay: Total: {0}, Average: {1}, Stdev: {2}",
          total_delay, average, StandardDeviation(delays, average));
    }
    /// <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;
    }
Exemple #60
0
    public BrunetTester(int p, NetworkConfiguration nc, StreamWriter fs)
    { 
      int desired_port = p;
      _port = (short)p;
      _sw = fs;
      int local_host_index = nc.GetLocalHostIndex(desired_port); 
      NodeConfiguration this_node_configuration = (NodeConfiguration)nc.Nodes[local_host_index];

      TransportAddressConfiguration local_ta_configuration = 
        (TransportAddressConfiguration)this_node_configuration.TransportAddresses[0];
      short this_port = local_ta_configuration.Port;

      SHA1 sha = new SHA1CryptoServiceProvider();
      String local_ta = local_ta_configuration.GetTransportAddressURI();
      //We take the local transport address plus the port number to be hashed to obtain a random AHAddress
      byte[] hashedbytes = sha.ComputeHash(Encoding.UTF8.GetBytes(local_ta + desired_port));
      //inforce type 0
      hashedbytes[Address.MemSize - 1] &= 0xFE;
      AHAddress _local_ahaddress = new AHAddress(hashedbytes);
      //Node this_node = new HybridNode( _local_ahaddress );
      Node this_node = new StructuredNode( _local_ahaddress );

      node = this_node;
      if ( local_ta_configuration.Protocol == "tcp" ) {
        node.AddEdgeListener( new TcpEdgeListener(this_port) );
      } 
      else if( local_ta_configuration.Protocol == "udp" ) {
        node.AddEdgeListener( new UdpEdgeListener(this_port) );        
      }

      int remote_node_index = local_host_index-1;
      int num_remote_ta = 150; //20 nodes on the list to try to bootstrap to

      if (local_host_index!=0) {
        NodeConfiguration remote_node_configuration = (NodeConfiguration)nc.Nodes[0];
        TransportAddressConfiguration remote_ta_configuration = 
          (TransportAddressConfiguration)remote_node_configuration.TransportAddresses[0];

        String remote_ta = remote_ta_configuration.GetTransportAddressURI(); 
        node.RemoteTAs.Add( TransportAddressFactory.CreateInstance( remote_ta  ) );
      }

      while ( (remote_node_index>=0) && (num_remote_ta>=0) ) { 
        NodeConfiguration remote_node_configuration = (NodeConfiguration)nc.Nodes[remote_node_index];
        TransportAddressConfiguration remote_ta_configuration = 
          (TransportAddressConfiguration)remote_node_configuration.TransportAddresses[0];

        String remote_ta = remote_ta_configuration.GetTransportAddressURI(); 
        node.RemoteTAs.Add(
	             TransportAddressFactory.CreateInstance( remote_ta  ) );

        //System.Console.WriteLine("Adding {0}", remote_ta);

        remote_node_index--;
        num_remote_ta--;
      }

      //String file_string = "./data/brunetadd" + Convert.ToString(desired_port) + ".log";
      fs.WriteLine( "local_address " + node.Address.ToBigInteger().ToString() 
          + " " + Dns.GetHostName() + ":" + desired_port);
      fs.Write( DateTime.Now.ToUniversalTime().ToString("MM'/'dd'/'yyyy' 'HH':'mm':'ss") + 
          ":" + DateTime.Now.ToUniversalTime().Millisecond +
          "  Start  Start  " + node.Address.ToBigInteger().ToString() + '\n'); 
      fs.Flush(); 
      //bool log_rdp = false;	  

#if PACKET_LOG
      String file_packet = "./data/packet" + Convert.ToString(desired_port) + ".log";
      StreamWriter packet_sw = new StreamWriter(file_packet, false);
      packet_sw.WriteLine("Local_node: {0}:{1} start_time_GMT: {2}:{3} local_address {4}", Dns.GetHostName(), 
          desired_port, DateTime.Now.ToUniversalTime().ToString("MM'/'dd'/'yyyy' 'HH':'mm':'ss"), 
          DateTime.Now.ToUniversalTime().Millisecond, node.Address.ToBigInteger().ToString() ); 
      packet_sw.Close(); 
#endif

    }