Exemplo n.º 1
0
        public void Initialize()
        {
            var privateKey = new PrivateKey(TestPrivateKeyHex);

            _publicKey = privateKey.PublicKey;
            var logManager = NullLogManager.Instance;
            //var config = new NetworkConfigurationProvider(new NetworkHelper(logger)) { PongTimeout = 100 };
            var config        = new JsonConfigProvider();
            var networkConfig = config.GetConfig <INetworkConfig>();

            networkConfig.PongTimeout = 100;

            var statsConfig = config.GetConfig <IStatsConfig>();

            _messageSender = Substitute.For <IMessageSender>();
            _nodeFactory   = new NodeFactory();
            var calculator = new NodeDistanceCalculator(config);

            _nodeTable = new NodeTable(_nodeFactory, new FileKeyStore(config, new JsonSerializer(logManager), new AesEncrypter(config, logManager), new CryptoRandom(), logManager), calculator, config, logManager);
            _nodeTable.Initialize();

            var evictionManager  = new EvictionManager(_nodeTable, logManager);
            var lifecycleFactory = new NodeLifecycleManagerFactory(_nodeFactory, _nodeTable, new DiscoveryMessageFactory(config), evictionManager, new NodeStatsProvider(statsConfig, _nodeFactory, logManager), config, logManager);

            _nodes = new[] { _nodeFactory.CreateNode("192.168.1.18", 1), _nodeFactory.CreateNode("192.168.1.19", 2) };

            _discoveryManager = new DiscoveryManager(lifecycleFactory, _nodeFactory, _nodeTable, new NetworkStorage("test", networkConfig, logManager, new PerfService(logManager)), config, logManager);
            _discoveryManager.MessageSender = _messageSender;
        }
Exemplo n.º 2
0
        private void AddPersistedNodes()
        {
            if (!_configurationProvider.IsDiscoveryNodesPersistenceOn)
            {
                return;
            }

            var nodes = _discoveryStorage.GetPersistedNodes();

            foreach (var networkNode in nodes)
            {
                var node    = _nodeFactory.CreateNode(networkNode.NodeId, networkNode.Host, networkNode.Port);
                var manager = _discoveryManager.GetNodeLifecycleManager(node, true);
                if (manager == null)
                {
                    if (_logger.IsDebug)
                    {
                        _logger.Debug($"Skiping persisted node {networkNode.NodeId}@{networkNode.Host}:{networkNode.Port}, manager couldnt be created");
                    }
                    continue;;
                }
                manager.NodeStats.CurrentPersistedNodeReputation = networkNode.Reputation;
                if (_logger.IsTrace)
                {
                    _logger.Trace($"Adding persisted node {networkNode.NodeId}@{networkNode.Host}:{networkNode.Port}");
                }
            }

            if (_logger.IsInfo)
            {
                _logger.Info($"Added persisted discovery nodes: {nodes.Length}");
            }
        }
        public void Initialize()
        {
            NetworkNodeDecoder.Init();
            var privateKey = new PrivateKey(TestPrivateKeyHex);

            _publicKey = privateKey.PublicKey;
            var logManager = NullLogManager.Instance;

            IKeyStoreConfig keyStoreConfig = new KeyStoreConfig();
            INetworkConfig  networkConfig  = new NetworkConfig();

            networkConfig.PongTimeout = 100;
            IStatsConfig statsConfig = new StatsConfig();

            _messageSender = Substitute.For <IMessageSender>();
            _nodeFactory   = new NodeFactory(LimboLogs.Instance);
            var calculator = new NodeDistanceCalculator(networkConfig);


            _nodeTable = new NodeTable(_nodeFactory, new FileKeyStore(keyStoreConfig, new EthereumJsonSerializer(), new AesEncrypter(keyStoreConfig, logManager), new CryptoRandom(), logManager), calculator, networkConfig, logManager);
            _nodeTable.Initialize();

            _timestamp = new Timestamp();

            var evictionManager  = new EvictionManager(_nodeTable, logManager);
            var lifecycleFactory = new NodeLifecycleManagerFactory(_nodeFactory, _nodeTable, new DiscoveryMessageFactory(networkConfig, _timestamp), evictionManager, new NodeStatsProvider(statsConfig, _nodeFactory, logManager, true), networkConfig, logManager);

            _nodes = new[] { _nodeFactory.CreateNode("192.168.1.18", 1), _nodeFactory.CreateNode("192.168.1.19", 2) };

            _discoveryManager = new DiscoveryManager(lifecycleFactory, _nodeFactory, _nodeTable, new NetworkStorage("test", networkConfig, logManager, new PerfService(logManager)), networkConfig, logManager);
            _discoveryManager.MessageSender = _messageSender;
        }
Exemplo n.º 4
0
        private void AddTrustedPeers()
        {
            var trustedPeers = _configurationProvider.TrustedPeers;

            if (trustedPeers == null || !trustedPeers.Any())
            {
                return;
            }

            if (_logger.IsInfoEnabled)
            {
                _logger.Info("Initializing trusted peers.");
            }

            foreach (var trustedPeer in trustedPeers)
            {
                var node = _nodeFactory.CreateNode(new NodeId(new PublicKey(new Hex(trustedPeer.NodeId))), trustedPeer.Host, trustedPeer.Port);
                node.Description = trustedPeer.Description;

                var nodeStats = _nodeStatsProvider.GetNodeStats(node.Id);
                nodeStats.IsTrustedPeer = true;

                var peer = new Peer(node, nodeStats);
                if (!_candidatePeers.TryAdd(node.Id, peer))
                {
                    continue;
                }

                if (_logger.IsInfoEnabled)
                {
                    _logger.Info($"Adding trusted peer to New collection {node.Id}@{node.Host}:{node.Port}");
                }
            }
        }
Exemplo n.º 5
0
        public void Can_store_discovery_nodes()
        {
            var persistedNodes = _storage.GetPersistedNodes();

            Assert.AreEqual(0, persistedNodes.Length);

            var nodes = new[]
            {
                _nodeFactory.CreateNode("192.1.1.1", 3441),
                _nodeFactory.CreateNode("192.1.1.2", 3442),
                _nodeFactory.CreateNode("192.1.1.3", 3443),
                _nodeFactory.CreateNode("192.1.1.4", 3444),
                _nodeFactory.CreateNode("192.1.1.5", 3445)
            };

            nodes[0].Description = "Test desc";
            nodes[4].Description = "Test desc 2";

            var managers     = nodes.Select(CreateLifecycleManager).ToArray();
            var networkNodes = managers.Select(x => new NetworkNode(x.ManagedNode.Id.PublicKey, x.ManagedNode.Host, x.ManagedNode.Port, x.ManagedNode.Description, x.NodeStats.NewPersistedNodeReputation)).ToArray();


            _storage.StartBatch();
            _storage.UpdateNodes(networkNodes);
            _storage.Commit();

            persistedNodes = _storage.GetPersistedNodes();
            foreach (INodeLifecycleManager manager in managers)
            {
                NetworkNode persistedNode = persistedNodes.FirstOrDefault(x => x.NodeId.Equals(manager.ManagedNode.Id));
                Assert.IsNotNull(persistedNode);
                Assert.AreEqual(manager.ManagedNode.Port, persistedNode.Port);
                Assert.AreEqual(manager.ManagedNode.Host, persistedNode.Host);
                Assert.AreEqual(manager.ManagedNode.Description, persistedNode.Description);
                Assert.AreEqual(manager.NodeStats.CurrentNodeReputation, persistedNode.Reputation);
            }

            _storage.StartBatch();
            _storage.RemoveNodes(new[] { networkNodes.First() });
            _storage.Commit();

            persistedNodes = _storage.GetPersistedNodes();
            foreach (INodeLifecycleManager manager in managers.Take(1))
            {
                NetworkNode persistedNode = persistedNodes.FirstOrDefault(x => x.NodeId.Equals(manager.ManagedNode.Id));
                Assert.IsNull(persistedNode);
            }

            foreach (INodeLifecycleManager manager in managers.Skip(1))
            {
                NetworkNode persistedNode = persistedNodes.FirstOrDefault(x => x.NodeId.Equals(manager.ManagedNode.Id));
                Assert.IsNotNull(persistedNode);
                Assert.AreEqual(manager.ManagedNode.Port, persistedNode.Port);
                Assert.AreEqual(manager.ManagedNode.Host, persistedNode.Host);
                Assert.AreEqual(manager.ManagedNode.Description, persistedNode.Description);
                Assert.AreEqual(manager.NodeStats.CurrentNodeReputation, persistedNode.Reputation);
            }
        }
Exemplo n.º 6
0
        public void PeersReadWriteTest()
        {
            var persistedPeers = _peerStorage.GetPersistedPeers();

            Assert.AreEqual(0, persistedPeers.Length);

            var nodes = new[]
            {
                _nodeFactory.CreateNode("192.1.1.1", 3441),
                _nodeFactory.CreateNode("192.1.1.2", 3442),
                _nodeFactory.CreateNode("192.1.1.3", 3443),
                _nodeFactory.CreateNode("192.1.1.4", 3444),
                _nodeFactory.CreateNode("192.1.1.5", 3445),
            };

            nodes[0].Description = "Test desc";
            nodes[4].Description = "Test desc 2";

            var peers = nodes.Select(x => new Peer(x, new NodeStats(_configurationProvider))).ToArray();

            _peerStorage.StartBatch();
            _peerStorage.UpdatePeers(peers);
            _peerStorage.Commit();

            persistedPeers = _peerStorage.GetPersistedPeers();
            foreach (var peer in peers)
            {
                var persistedNode = persistedPeers.FirstOrDefault(x => x.Node.Id.Equals(peer.Node.Id));
                Assert.IsNotNull(persistedNode);
                Assert.AreEqual(peer.Node.Port, persistedNode.Node.Port);
                Assert.AreEqual(peer.Node.Host, persistedNode.Node.Host);
                Assert.AreEqual(peer.Node.Description, persistedNode.Node.Description);
                Assert.AreEqual(peer.NodeStats.CurrentNodeReputation, persistedNode.PersistedReputation);
            }

            _peerStorage.StartBatch();
            _peerStorage.RemovePeers(peers.Take(1).ToArray());
            _peerStorage.Commit();

            persistedPeers = _peerStorage.GetPersistedPeers();
            foreach (var peer in peers.Take(1))
            {
                var persistedNode = persistedPeers.FirstOrDefault(x => x.Node.Id.Equals(peer.Node.Id));
                Assert.IsNull(persistedNode.Node);
            }

            foreach (var peer in peers.Skip(1))
            {
                var persistedNode = persistedPeers.FirstOrDefault(x => x.Node.Id.Equals(peer.Node.Id));
                Assert.IsNotNull(persistedNode);
                Assert.AreEqual(peer.Node.Port, persistedNode.Node.Port);
                Assert.AreEqual(peer.Node.Host, persistedNode.Node.Host);
                Assert.AreEqual(peer.Node.Description, persistedNode.Node.Description);
                Assert.AreEqual(peer.NodeStats.CurrentNodeReputation, persistedNode.PersistedReputation);
            }
        }
Exemplo n.º 7
0
        public void InPeerBecomesActiveAndDisconnectTest()
        {
            var node = _nodeFactory.CreateNode("192.1.1.1", 3333);

            //trigger connection initialized
            var p2pSession = new TestP2PSession();

            p2pSession.RemoteNodeId        = node.Id;
            p2pSession.RemoteHost          = node.Host;
            p2pSession.RemotePort          = node.Port;
            p2pSession.ConnectionDirection = ConnectionDirection.In;
            _localPeer.TriggerSessionCreated(p2pSession);
            p2pSession.TriggerHandshakeComplete();

            //trigger p2p initialization
            var p2pProtocol = new P2PProtocolHandler(p2pSession, new MessageSerializationService(), p2pSession.RemoteNodeId, p2pSession.RemotePort ?? 0, _logManager, new PerfService(_logManager));
            var p2pArgs     = new P2PProtocolInitializedEventArgs(p2pProtocol)
            {
                P2PVersion   = 4,
                Capabilities = new[] { new Capability(Protocol.Eth, 62), new Capability(Protocol.Eth, 63) }.ToList()
            };

            p2pSession.TriggerProtocolInitialized(p2pArgs);
            AssertTrue(() => _peerManager.ActivePeers.First().NodeStats.DidEventHappen(NodeStatsEventType.P2PInitialized), 5000);
            //Assert.IsTrue(_peerManager.ActivePeers.First().NodeStats.DidEventHappen(NodeStatsEventType.P2PInitialized));

            //trigger eth62 initialization
            var eth62 = new Eth62ProtocolHandler(p2pSession, new MessageSerializationService(), _synchronizationManager, _logManager, new PerfService(_logManager), _blockTree, _transactionPool, _timestamp);
            var args  = new EthProtocolInitializedEventArgs(eth62)
            {
                ChainId = _synchronizationManager.ChainId
            };

            p2pSession.TriggerProtocolInitialized(args);
            AssertTrue(() => _peerManager.ActivePeers.First().NodeStats.DidEventHappen(NodeStatsEventType.Eth62Initialized), 5000);

            Assert.AreEqual(1, _peerManager.CandidatePeers.Count);
            Assert.AreEqual(1, _peerManager.ActivePeers.Count);
            //Assert.IsTrue(_peerManager.ActivePeers.First().NodeStats.DidEventHappen(NodeStatsEventType.Eth62Initialized));
            AssertTrue(() => _peerManager.ActivePeers.First().SynchronizationPeer != null, 5000);
            //Assert.NotNull(_peerManager.ActivePeers.First().SynchronizationPeer);

            //verify active peer was added to synch manager
            _synchronizationManager.Received(1).AddPeer(Arg.Any <ISynchronizationPeer>());

            //trigger disconnect
            p2pSession.TriggerPeerDisconnected();
            AssertTrue(() => _peerManager.ActivePeers.Count == 0, 5000);
            Assert.AreEqual(1, _peerManager.CandidatePeers.Count);
            //Assert.AreEqual(0, _peerManager.ActivePeers.Count);

            //verify active peer was removed from synch manager
            _synchronizationManager.Received(1).RemovePeer(Arg.Any <ISynchronizationPeer>());
        }
        public MatrixMap(string path, Directions directions, INodeFactory nodeFactory, INeighbourFuncFactory neighbourFuncFactory)
        {
            var lines = File.ReadAllLines(path);
            var N     = lines.Count();

            Nodes = new Node[N, N];
            SizeX = N;
            SizeY = N;
            CreateNodes(nodeFactory, lines, N);

            GetNeighboursOf = neighbourFuncFactory.CreateFunc(directions, SizeX, SizeY);

            void CreateNodes(INodeFactory nodeFactory, string[] lines, int N)
            {
                for (var i = 0; i < N; i++)
                {
                    var costs = lines[i].Split(',');
                    for (var j = 0; j < N; j++)
                    {
                        var cost = Convert.ToInt32(costs[j]);
                        Nodes[i, j] = nodeFactory.CreateNode(cost);
                    }
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Creates tree from hierarchy hasthtable
        /// </summary>
        /// <param name="root">Root object</param>
        /// <param name="factory">Factory</param>
        /// <param name="hierarchy">Hierarchy hashtable</param>
        /// <param name="links">Links between nodes and objects</param>
        /// <returns>Root node</returns>
        public static INode CreateTree(object root, INodeFactory factory, Hashtable hierarchy, Hashtable links)
        {
            INode node = factory.CreateNode(root);

            ++commonCount;
            if (hierarchy.ContainsKey(root))
            {
                ArrayList list = hierarchy[root] as ArrayList;
                foreach (object o in list)
                {
                    INode n = CreateTree(o, factory, hierarchy, links);
                    n.Parent = node;
                }
            }
            if (links != null)
            {
                ArrayList l = null;
                if (links.ContainsKey(root))
                {
                    l = links[root] as ArrayList;
                }
                else
                {
                    l           = new ArrayList();
                    links[root] = l;
                }
                l.Add(node);
            }
            return(node);
        }
Exemplo n.º 10
0
	private INode Insert(INode h, K key, V val)
	{
		if (h == null)
			return nodeFactory.CreateNode(key, val, Color.Red);
		
		// split 4-nodes on the way down
		if (IsRed(h.Left) && IsRed(h.Left.Left))
			h = SplitFourNode(h);
		
		// standard BST insert code
		int cmp = key.CompareTo(h.Key);
		
		if (cmp == 0)
			AddToValues(h, val);
		else if (cmp < 0)
			h.Left = Insert(h.Left, key, val);
		else
			h.Right = Insert(h.Right, key, val);
		
		// fix right-leaning reds on the way up
		if (IsRed(h.Right))
			h = LeanLeft(h);
		
		return h;
	}
Exemplo n.º 11
0
        private TestP2PSession InitializeNode()
        {
            var node = _nodeFactory.CreateNode("192.1.1.1", 3333);

            _discoveryManager.GetNodeLifecycleManager(node);

            //verify new peer is added
            Assert.AreEqual(1, _peerManager.NewPeers.Count);
            Assert.AreEqual(node.Id, _peerManager.NewPeers.First().Node.Id);

            //trigger connection start
            var task = _peerManager.RunPeerUpdate();

            task.Wait();
            Assert.AreEqual(1, _localPeer.ConnectionAsyncCallsCounter);
            Assert.AreEqual(0, _peerManager.NewPeers.Count);
            Assert.AreEqual(1, _peerManager.ActivePeers.Count);

            //trigger connection initialized
            var p2pSession = new TestP2PSession();

            p2pSession.RemoteNodeId = node.Id;
            _localPeer.TriggerConnectionInitialized(p2pSession);

            var peer = _peerManager.ActivePeers.First();

            Assert.IsNotNull(peer.Session);

            return(p2pSession);
        }
Exemplo n.º 12
0
        private NodeBunch BuildCustomItem(Element item)
        {
            var links = item.GetItemLinks();

            var headNode = _nodeFactory.CreateNode(item.AsPseudoList());

            if (headNode == null)
            {
                throw new BuildingException("Node factory returned null.");
            }

            var tree = headNode.FetchTree();

            if (tree.Count == 1)
            {
                var nodeBox = new NodeBox(headNode, links);
                return(new NodeBunch(nodeBox, nodeBox));
            }
            else
            {
                var headNodeBox = new NodeBox(headNode);

                try
                {
                    var tailNode    = tree.Single(x => x.EstablishedLinks.Count == 0);
                    var tailNodeBox = new NodeBox(tailNode, links);

                    return(new NodeBunch(headNodeBox, tailNodeBox));
                }
                catch (Exception ex)
                {
                    throw new BuildingException("More than one tail node (the node which doesn't have links).", ex);
                }
            }
        }
Exemplo n.º 13
0
        public void Initialize(NodeId masterNodeKey = null)
        {
            Buckets = new NodeBucket[_configurationProvider.BucketsCount];
            var pass    = new SecureString();
            var rawPass = _configurationProvider.KeyPass;

            for (var i = 0; i < rawPass.Length; i++)
            {
                pass.AppendChar(rawPass[i]);
            }
            pass.MakeReadOnly();

            if (masterNodeKey == null)
            {
                var key = _keyStore.GenerateKey(pass);
                if (key.Item2.ResultType == ResultType.Failure)
                {
                    var msg = $"Cannot create key, error: {key.Item2.Error}";
                    _logger.Error(msg);
                    throw new Exception(msg);
                }

                masterNodeKey = new NodeId(key.PrivateKey.PublicKey);
            }

            MasterNode = _nodeFactory.CreateNode(masterNodeKey, _configurationProvider.MasterHost, _configurationProvider.MasterPort);
            _logger.Info($"Created MasterNode: {MasterNode}");

            for (var i = 0; i < Buckets.Length; i++)
            {
                Buckets[i] = new NodeBucket(i, _configurationProvider.BucketSize);
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Create root node from collection of nodes
        /// </summary>
        /// <param name="objects">Prototypes</param>
        /// <param name="factory">Factory</param>
        /// <returns>Root node</returns>
        public static INode CreateRootNode(ICollection objects, INodeFactory factory)
        {
            INode     root  = null;
            Hashtable hash  = new Hashtable();
            ArrayList nodes = new ArrayList();

            foreach (object o in objects)
            {
                try
                {
                    INode node = factory.CreateNode(o);
                    nodes.Add(node);
                    hash[node.Id] = node;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            foreach (INode node in nodes)
            {
                object p = node.ParentId;
                if (p == null)
                {
                    root = node;
                }
                else if (p.Equals(node.Id))
                {
                    root = node;
                    continue;
                }
                INode parent = hash[p] as INode;
                if (parent != null)
                {
                    node.Parent = parent;
                }
            }
            if (root != null)
            {
                return(root);
            }
            if (root == null)
            {
                foreach (INode node in hash.Values)
                {
                    if (node.Parent == null)
                    {
                        root = node;
                        break;
                    }
                }
            }
            return(root);
        }
Exemplo n.º 15
0
        public INodeLifecycleManager CreateNodeLifecycleManager(NodeId id, string host, int port)
        {
            if (DiscoveryManager == null)
            {
                throw new Exception($"{nameof(DiscoveryManager)} has to be set");
            }

            var node = _nodeFactory.CreateNode(id, host, port);

            return(new NodeLifecycleManager(node, DiscoveryManager, _nodeTable, _logger, _configurationProvider, _discoveryMessageFactory, _evictionManager, _nodeStatsProvider.GetOrAddNodeStats(node)));
        }
Exemplo n.º 16
0
        /// <summary>
        /// Creates peer tree
        /// </summary>
        /// <param name="prototypeRoot">Root of prototype node</param>
        /// <param name="factory">Node factory</param>
        /// <returns>Root of prototype tree</returns>
        public static INode CreatePeerTree(INode prototypeRoot, INodeFactory factory)
        {
            INode root = factory.CreateNode(prototypeRoot);

            for (int i = 0; i < prototypeRoot.Count; i++)
            {
                INode n    = prototypeRoot[i];
                INode node = CreatePeerTree(n, factory);
                node.Parent = root;
            }
            return(root);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Creates subtree
        /// </summary>
        /// <param name="node">Root</param>
        /// <param name="list">List of nodes</param>
        /// <param name="factory">Factory</param>
        /// <returns>Tree root</returns>
        public static INode CreateSubTree(INode node, ArrayList list, INodeFactory factory)
        {
            INode root = factory.CreateNode(node);

            for (int i = 0; i < node.Count; i++)
            {
                if (list.Contains(node[i]))
                {
                    INode nod = CreateSubTree(node[i], list, factory);
                    nod.Parent = root;
                }
            }
            return(root);
        }
Exemplo n.º 18
0
    private void GenerateDialogueNodes(GraphItem graphItem)
    {
        foreach (var node in graphItem.NodeData)
        {
            var tempNode = nodeFactory.CreateNode(node.nodeType, graphView);
            if (tempNode == null)
            {
                continue;
            }

            tempNode.nodeGuid = node.nodeGuid;
            tempNode.SetPosition(new Rect(node.position, graphView.DefaultNodeSize));
            graphView.AddElement(tempNode);
            //tempNode.PopulateAdditionalData(node.additionalDataJSON);
        }
    }
Exemplo n.º 19
0
        /// <summary>
        /// Navigates to the given node.
        /// </summary>
        /// <param name="nodeFactory">Node factory class to create the node for the</param>
        /// <param name="nodeContext"></param>
        public void Navigate(INodeFactory nodeFactory, INodeContext nodeContext)
        {
            if (nodeFactory == null)
            {
                ExceptionManager.Throw(new ArgumentNullException("nodeFactory"));
            }
            if (nodeContext == null)
            {
                ExceptionManager.Throw(new ArgumentNullException("nodeContext"));
            }

            _currentNode = _navigationManager.GetCurrentNode();

            INode currentNode = nodeFactory.CreateNode(nodeContext);

            NavigateToNode(currentNode);
        }
Exemplo n.º 20
0
        /// <summary>
        /// Create roots' nodes from collection of nodes
        /// </summary>
        /// <param name="objects">Prototypes</param>
        /// <param name="factory">Factory</param>
        /// <returns>Roots' nodes</returns>
        public static INode[] CreateRoots(ICollection objects, INodeFactory factory)
        {
            //INode root = null;
            Hashtable hash  = new Hashtable();
            ArrayList nodes = new ArrayList();
            ArrayList roots = new ArrayList();

            foreach (object o in objects)
            {
                try
                {
                    INode node = factory.CreateNode(o);
                    nodes.Add(node);
                    hash[node.Id] = node;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            foreach (INode node in nodes)
            {
                object p = node.ParentId;
                if (p == null)
                {
                    roots.Add(node);
                }
                else if (p.Equals(node.Id))
                {
                    roots.Add(node);
                    continue;
                }
                INode parent = hash[p] as INode;
                if (parent != null)
                {
                    node.Parent = parent;
                }
            }
            INode[] r = new INode[roots.Count];
            for (int i = 0; i < r.Length; i++)
            {
                r[i] = roots[i] as INode;
            }
            return(r);
        }
        public void ActiveStateTest()
        {
            var node    = _nodeFactory.CreateNode(_host, _port);
            var manager = _discoveryManager.GetNodeLifecycleManager(node);

            Assert.AreEqual(NodeLifecycleState.New, manager.State);

            manager.ProcessPongMessage(new PongMessage {
                FarAddress = new IPEndPoint(IPAddress.Parse(_host), _port), FarPublicKey = _nodeIds[0].PublicKey
            });

            Assert.AreEqual(NodeLifecycleState.Active, manager.State);
        }
        /// <summary>
        /// Words to train the auto-complete engine on.
        /// </summary>
        /// <param name="passage">text that is processed to enhance predictions.</param>
        /// <param name="removePunctuation">removes punctuation before processing</param>
        /// <param name="caseSensitive">predicts words with case sensitivity</param>
        public void Train(string passage, bool removePunctuation = true, bool caseSensitive = false)
        {
            if (removePunctuation)
            {
                passage = _RemovePunctuation(passage);
            }

            var words = passage.Split(null /* whitespace */)
                        .Where(word => !string.IsNullOrWhiteSpace(word))
                        .Select(word =>
            {
                var modified = word;
                modified     = word.Trim();
                if (!caseSensitive)
                {
                    modified = modified.ToLowerInvariant();
                }
                return(_NodeFactory.CreateNode(modified, modified));
            });

            _Trie.AddRange(words);
        }
Exemplo n.º 23
0
        public INodeStats GetOrAddNodeStats(NodeId nodeId, string host, int port)
        {
            var node = _nodeFactory.CreateNode(nodeId, host, port);

            return(GetOrAddNodeStats(node));
        }
Exemplo n.º 24
0
        private async Task <bool> InitializeBootnodes()
        {
            var bootNodes = _configurationProvider.BootNodes;

            if (bootNodes == null || !bootNodes.Any())
            {
                if (_logger.IsWarnEnabled)
                {
                    _logger.Warn("No bootnodes specified in configuration");
                }
                return(false);
            }
            var managers = new INodeLifecycleManager[bootNodes.Length];

            for (var i = 0; i < bootNodes.Length; i++)
            {
                var bootnode = bootNodes[i];
                var node     = bootnode.NodeId == null
                    ? _nodeFactory.CreateNode(bootnode.Host, bootnode.Port)
                    : _nodeFactory.CreateNode(new NodeId(new PublicKey(new Hex(bootnode.NodeId))), bootnode.Host, bootnode.Port, true);

                var manager = _discoveryManager.GetNodeLifecycleManager(node);
                managers[i] = manager;
            }

            //Wait for pong message to come back from Boot nodes
            var maxWaitTime = _configurationProvider.BootNodePongTimeout;
            var itemTime    = maxWaitTime / 100;

            for (var i = 0; i < 100; i++)
            {
                if (managers.Any(x => x.State == NodeLifecycleState.Active))
                {
                    break;
                }

                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug($"Waiting {itemTime} ms for bootnodes to respond");
                }

                await Task.Delay(1000);
            }

            var reachedNodeCounter = 0;

            for (var i = 0; i < managers.Length; i++)
            {
                var manager = managers[i];
                if (manager.State != NodeLifecycleState.Active)
                {
                    if (_logger.IsWarnEnabled)
                    {
                        _logger.Warn($"Cannot reach bootnode: {manager.ManagedNode.Host}:{manager.ManagedNode.Port}");
                    }
                }
                else
                {
                    reachedNodeCounter++;
                }
            }

            return(reachedNodeCounter > 0);
        }
Exemplo n.º 25
0
        /// <summary>
        /// Navigates to the given node.
        /// </summary>
        /// <param name="nodeFactory">Node factory class to create the node for the</param>
        /// <param name="nodeContext"></param>
        public void Navigate(INodeFactory nodeFactory, INodeContext nodeContext)
        {
            if (nodeFactory == null)
            {
                ExceptionManager.Throw(new ArgumentNullException("nodeFactory"));
            }
            if (nodeContext == null)
            {
                ExceptionManager.Throw(new ArgumentNullException("nodeContext"));
            }

            _currentNode = _navigationManager.GetCurrentNode();

            INode currentNode = nodeFactory.CreateNode(nodeContext);

            NavigateToNode(currentNode);
        }