CreateInstance() публичный статический Метод

public static CreateInstance ( Address a ) : NodeInfo
a Address
Результат NodeInfo
Пример #1
0
        protected LinkMessage MakeLM()
        {
            NodeInfo my_info     = NodeInfo.CreateInstance(_node.Address, _e.LocalTA);
            NodeInfo remote_info = NodeInfo.CreateInstance(_linker.Target, _e.RemoteTA);

            return(new LinkMessage(_contype, my_info, remote_info, _node.Realm, _linker.Token));
        }
Пример #2
0
        public void SMTest()
        {
            Address          a  = new DirectionalAddress(DirectionalAddress.Direction.Left);
            TransportAddress ta = TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:5000");
            NodeInfo         ni = NodeInfo.CreateInstance(a, ta);

            //Test with one neighbor:
            ArrayList neighbors = new ArrayList();

            neighbors.Add(ni);
            StatusMessage sm1 = new StatusMessage(ConnectionType.Structured, neighbors);

            RoundTripHT(sm1);
            //Console.Error.WriteLine("\n{0}\n", sm1);
            //Test with many neighbors:

            for (int i = 5001; i < 5010; i++)
            {
                neighbors.Add(NodeInfo.CreateInstance(a,
                                                      TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:"
                                                                                             + i.ToString())));
            }
            StatusMessage sm2 = new StatusMessage(ConnectionType.Unstructured, neighbors);

            RoundTripHT(sm2);
            //Console.Error.WriteLine("\n{0}\n", sm2);

            //Here is a StatusMessage with no neighbors (that has to be a possibility)
            StatusMessage sm3 = new StatusMessage("structured", new ArrayList());

            RoundTripHT(sm3);
            //Console.Error.WriteLine("\n{0}\n", sm3);
        }
Пример #3
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);
        }
Пример #4
0
        public void RoundTrip(NodeInfo ni)
        {
            NodeInfo ni_other = NodeInfo.CreateInstance(ni.Address, ni.Transports);

            Assert.AreEqual(ni, ni_other, "Hashtable roundtrip");
            Assert.AreEqual(ni.GetHashCode(), ni_other.GetHashCode(), "Hashtable GetHashCode roundtrip");
        }
Пример #5
0
        public LinkMessage(IDictionary ht)
        {
            IDictionaryEnumerator en = ht.GetEnumerator();

            _attributes = new StringDictionary();
            while (en.MoveNext())
            {
                if (en.Key.Equals("local"))
                {
                    IDictionary lht = en.Value as IDictionary;
                    if (lht != null)
                    {
                        _local_ni = NodeInfo.CreateInstance(lht);
                    }
                }
                else if (en.Key.Equals("remote"))
                {
                    IDictionary rht = en.Value as IDictionary;
                    if (rht != null)
                    {
                        _remote_ni = NodeInfo.CreateInstance(rht);
                    }
                }
                else if (en.Key.Equals("token"))
                {
                    _token = (string)ht["token"];
                }
                else
                {
                    _attributes[String.Intern(en.Key.ToString())] = String.Intern(en.Value.ToString());
                }
            }
        }
Пример #6
0
        public void RoundTripHT(NodeInfo ni)
        {
            NodeInfo ni_other = NodeInfo.CreateInstance(ni.ToDictionary());

            Assert.AreEqual(ni, ni_other, "Hashtable roundtrip");
            Assert.AreEqual(ni.GetHashCode(), ni_other.GetHashCode(), "Hashtable GetHashCode roundtrip");
        }
Пример #7
0
        protected LinkMessage MakeLM()
        {
            NodeInfo my_info     = NodeInfo.CreateInstance(_node.Address, _e.LocalTA);
            NodeInfo remote_info = NodeInfo.CreateInstance(_linker.Target, _e.RemoteTA);

            System.Collections.Specialized.StringDictionary attrs
                           = new System.Collections.Specialized.StringDictionary();
            attrs["type"]  = String.Intern(_contype);
            attrs["realm"] = String.Intern(_node.Realm);
            return(new LinkMessage(attrs, my_info, remote_info, _linker.Token));
        }
Пример #8
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);
        }
Пример #9
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");
        }
Пример #10
0
        public ConnectToMessage(IDictionary ht)
        {
            _ct        = (string)ht["type"];
            _target_ni = NodeInfo.CreateInstance((IDictionary)ht["target"]);
            _token     = (string)ht["token"];
            IList neighht = ht["neighbors"] as IList;

            if (neighht != null)
            {
                _neighbors = new NodeInfo[neighht.Count];
                for (int i = 0; i < neighht.Count; i++)
                {
                    _neighbors[i] = NodeInfo.CreateInstance((IDictionary)neighht[i]);
                }
            }
        }
Пример #11
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
        }
Пример #12
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);
        }
Пример #13
0
        /**
         * Initialize from a Hashtable containing all the information
         */
        public StatusMessage(IDictionary ht)
        {
            IDictionary neighborinfo = ht["neighbors"] as IDictionary;

            if (neighborinfo != null)
            {
                _neigh_ct = String.Intern(neighborinfo["type"] as String);
                IList nodes = neighborinfo["nodes"] as IList;
                if (nodes != null)
                {
                    _neighbors = new ArrayList(nodes.Count);
                    foreach (IDictionary nih in nodes)
                    {
                        _neighbors.Add(NodeInfo.CreateInstance(nih));
                    }
                }
                else
                {
                    _neighbors = new ArrayList(1);
                }
            }
        }
Пример #14
0
        protected ConnectToMessage GetCtmResponseTo(ConnectToMessage ctm_req)
        {
            NodeInfo target = ctm_req.Target;

            //Send the 4 neighbors closest to this node:
            ArrayList nearest = _n.ConnectionTable.GetNearestTo((AHAddress)target.Address, 4);
            //Now get these the NodeInfo objects for these:
            ArrayList neighbors = new ArrayList();

            foreach (Connection cons in nearest)
            {
                //No need to send the TA, since only the address is used
                NodeInfo neigh = NodeInfo.CreateInstance(cons.Address);
                neighbors.Add(neigh);
            }
            //Put these into an NodeInfo[]
            NodeInfo[] neigh_array = new NodeInfo[neighbors.Count];
            for (int i = 0; i < neighbors.Count; i++)
            {
                neigh_array[i] = (NodeInfo)neighbors[i];
            }
            return(new ConnectToMessage(ctm_req.ConnectionType, _n.GetNodeInfo(12), neigh_array, ctm_req.Token));
        }
Пример #15
0
        public LinkMessage(IDictionary ht)
        {
            IDictionaryEnumerator en = ht.GetEnumerator();

            while (en.MoveNext())
            {
                if (en.Key.Equals("local"))
                {
                    IDictionary lht = en.Value as IDictionary;
                    if (lht != null)
                    {
                        Local = NodeInfo.CreateInstance(lht);
                    }
                }
                else if (en.Key.Equals("remote"))
                {
                    IDictionary rht = en.Value as IDictionary;
                    if (rht != null)
                    {
                        Remote = NodeInfo.CreateInstance(rht);
                    }
                }
                else if (en.Key.Equals("token"))
                {
                    Token = String.Intern((string)en.Value);
                }
                else if (en.Key.Equals("realm"))
                {
                    Realm = String.Intern((string)en.Value);
                }
                else if (en.Key.Equals("type"))
                {
                    ConTypeString = String.Intern((string)en.Value);
                }
            }
        }
Пример #16
0
        /**
         * This starts a linking operation on the given edge
         */
        public IDictionary Start(IDictionary link_message, ISender edge)
        {
            if (ProtocolLog.LinkDebug.Enabled)
            {
                ProtocolLog.Write(ProtocolLog.LinkDebug, String.Format(
                                      "{0} -start- sys:link.Start", _node.Address));
            }

            Edge        from = GetEdge(edge);
            LinkMessage lm   = new LinkMessage(link_message);

            if (ProtocolLog.LinkDebug.Enabled)
            {
                ProtocolLog.Write(ProtocolLog.LinkDebug, String.Format(
                                      "{0} -args- sys:link.Start({1},{2})", _node.Address, lm, from));
            }

            CphState cph = new CphState(from, lm);

            lock ( _sync ) {
                if (!_edge_to_cphstate.ContainsKey(from))
                {
                    _edge_to_cphstate[from] = cph;
                }
                else
                {
                    throw new AdrException((int)ErrorMessage.ErrorCode.InProgress,
                                           "Already have a link in progress on this edge");
                }
            }
            ErrorMessage err = null;

            if (CanConnect(cph, out err))
            {
                try {
                    //If the CloseEvent was already called, this throws an exception
                    from.CloseEvent += this.CloseHandler;
                }
                catch {
                    CloseHandler(from, null);
                    throw new AdrException((int)ErrorMessage.ErrorCode.EdgeClosed,
                                           "Edge Closed after receiving message");
                }
            }
            else
            {
                lock ( _sync ) {
                    _edge_to_cphstate.Remove(from);
                }
            }
            //Now we prepare our response
            LinkMessage lm_resp = null;

            if (err == null)
            {
                //We send a response:
                NodeInfo n_info      = NodeInfo.CreateInstance(_node.Address, from.LocalTA);
                NodeInfo remote_info = NodeInfo.CreateInstance(null, from.RemoteTA);
                System.Collections.Specialized.StringDictionary attrs =
                    new System.Collections.Specialized.StringDictionary();
                attrs["type"]  = String.Intern(lm.ConTypeString);
                attrs["realm"] = String.Intern(_node.Realm);
                lm_resp        = new LinkMessage(attrs, n_info, remote_info, lm.Token);
            }
            else
            {
                if (err.Ec == ErrorMessage.ErrorCode.AlreadyConnected)
                {
                    /**
                     * When we send the ErrorCode.AlreadyConnected,
                     * we could have a stale connection, lets try pinging
                     * the other node, if they are there, but have lost
                     * the Edge, this may trigger the edge to close, causing
                     * us to remove the Connection.
                     * @todo consider putting this address on a "fast track"
                     * to removal if we don't hear from it soon
                     */
                    ConnectionTable tab = _node.ConnectionTable;
                    Connection      c   = tab.GetConnection(lm.ConnectionType,
                                                            lm.Local.Address);
                    if (c != null)
                    {
                        RpcManager rpc = _node.Rpc;
                        rpc.Invoke(c.Edge, null, "sys:link.Ping", String.Empty);
                    }
                }
            }
            if (err != null)
            {
                throw new AdrException((int)err.Ec, err.Message);
            }
            if (ProtocolLog.LinkDebug.Enabled)
            {
                ProtocolLog.Write(ProtocolLog.LinkDebug, String.Format(
                                      "{0} -end- sys:link.Start()->{1}", _node.Address, lm_resp));
            }
            return(lm_resp.ToDictionary());
        }
Пример #17
0
        /**
         * This starts a linking operation on the given edge
         */
        public IDictionary Start(IDictionary link_message, ISender edge)
        {
            if (ProtocolLog.LinkDebug.Enabled)
            {
                ProtocolLog.Write(ProtocolLog.LinkDebug, String.Format(
                                      "{0} -start- sys:link.Start", _node.Address));
            }

            Edge        from = GetEdge(edge);
            LinkMessage lm   = new LinkMessage(link_message);

            if (ProtocolLog.LinkDebug.Enabled)
            {
                ProtocolLog.Write(ProtocolLog.LinkDebug, String.Format(
                                      "{0} -args- sys:link.Start({1},{2})", _node.Address, lm, from));
            }

            CphState cph = new CphState(from, lm);

            lock ( _sync ) {
                if (!_edge_to_cphstate.ContainsKey(from))
                {
                    _edge_to_cphstate[from] = cph;
                }
                else
                {
                    throw new AdrException((int)ErrorMessage.ErrorCode.InProgress,
                                           "Already have a link in progress on this edge");
                }
            }
            ErrorMessage err = null;

            if (CanConnect(cph, out err))
            {
                try {
                    //If the CloseEvent was already called, this throws an exception
                    from.CloseEvent += this.CloseHandler;
                }
                catch {
                    CloseHandler(from, null);
                    throw new AdrException((int)ErrorMessage.ErrorCode.EdgeClosed,
                                           "Edge Closed after receiving message");
                }
            }
            else
            {
                lock ( _sync ) {
                    _edge_to_cphstate.Remove(from);
                }
            }
            //Now we prepare our response
            LinkMessage lm_resp = null;

            if (err == null)
            {
                //We send a response:
                NodeInfo n_info      = NodeInfo.CreateInstance(_node.Address, from.LocalTA);
                NodeInfo remote_info = NodeInfo.CreateInstance(null, from.RemoteTA);
                lm_resp = new LinkMessage(lm.ConTypeString, n_info, remote_info, _node.Realm, lm.Token);
            }
            else
            {
                throw new AdrException((int)err.Ec, err.Message);
            }
            if (ProtocolLog.LinkDebug.Enabled)
            {
                ProtocolLog.Write(ProtocolLog.LinkDebug, String.Format(
                                      "{0} -end- sys:link.Start()->{1}", _node.Address, lm_resp));
            }
            return(lm_resp.ToDictionary());
        }