Exemple #1
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);
            }
        }
Exemple #2
0
        public Node[] GetClosestNodes()
        {
            List <NodeBucketItem> nodes = new List <NodeBucketItem>();
            int bucketSize = _discoveryConfig.BucketSize;

            for (int i = 0; i < Buckets.Length; i++)
            {
                NodeBucket nodeBucket = Buckets[i];
                IReadOnlyCollection <NodeBucketItem> bucketItems = nodeBucket.Items;
                if (!bucketItems.Any())
                {
                    continue;
                }

                int availableCount = bucketSize - nodes.Count;
                if (bucketItems.Count >= availableCount)
                {
                    nodes.AddRange(bucketItems.Take(availableCount).ToArray());
                    break;
                }

                nodes.AddRange(bucketItems.ToArray());
            }

            return(nodes.Select(x => x.Node).ToArray());
        }
Exemple #3
0
        public void RefreshNode(Node node)
        {
            int        distanceFromMaster = _nodeDistanceCalculator.CalculateDistance(MasterNode.IdHash.Bytes, node.IdHash.Bytes);
            NodeBucket bucket             = Buckets[distanceFromMaster > 0 ? distanceFromMaster - 1 : 0];

            bucket.RefreshNode(node);
        }
Exemple #4
0
        public void ReplaceNode(Node nodeToRemove, Node nodeToAdd)
        {
            int        distanceFromMaster = _nodeDistanceCalculator.CalculateDistance(MasterNode.IdHash.Bytes, nodeToAdd.IdHash.Bytes);
            NodeBucket bucket             = Buckets[distanceFromMaster > 0 ? distanceFromMaster - 1 : 0];

            _nodes.AddOrUpdate(nodeToAdd.IdHash, nodeToAdd, (x, y) => nodeToAdd);
            _nodes.TryRemove(nodeToRemove.IdHash, out _);
            bucket.ReplaceNode(nodeToRemove, nodeToAdd);
        }
Exemple #5
0
        public void ReplaceNode(Node nodeToRemove, Node nodeToAdd)
        {
            CheckInitialization();

            int        distanceFromMaster = _nodeDistanceCalculator.CalculateDistance(MasterNode.IdHash.Bytes, nodeToAdd.IdHash.Bytes);
            NodeBucket bucket             = Buckets[distanceFromMaster > 0 ? distanceFromMaster - 1 : 0];

            bucket.ReplaceNode(nodeToRemove, nodeToAdd);
        }
Exemple #6
0
        public NodeAddResult AddNode(Node node)
        {
            if (_logger.IsTrace)
            {
                _logger.Trace($"Adding node to NodeTable: {node}");
            }
            int        distanceFromMaster = _nodeDistanceCalculator.CalculateDistance(MasterNode.IdHash.Bytes, node.IdHash.Bytes);
            NodeBucket bucket             = Buckets[distanceFromMaster > 0 ? distanceFromMaster - 1 : 0];

            _nodes.AddOrUpdate(node.IdHash, node, (x, y) => node);
            return(bucket.AddNode(node));
        }
Exemple #7
0
        public NodeTable(INodeDistanceCalculator nodeDistanceCalculator, IDiscoveryConfig discoveryConfig, INetworkConfig networkConfig, ILogManager logManager)
        {
            _logger                 = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));
            _networkConfig          = networkConfig ?? throw new ArgumentNullException(nameof(networkConfig));
            _discoveryConfig        = discoveryConfig ?? throw new ArgumentNullException(nameof(discoveryConfig));
            _nodeDistanceCalculator = nodeDistanceCalculator ?? throw new ArgumentNullException(nameof(nodeDistanceCalculator));

            Buckets = new NodeBucket[_discoveryConfig.BucketsCount];
            for (int i = 0; i < Buckets.Length; i++)
            {
                Buckets[i] = new NodeBucket(i, _discoveryConfig.BucketSize);
            }
        }
Exemple #8
0
        public NodeAddResult AddNode(Node node)
        {
            CheckInitialization();

            if (_logger.IsTrace)
            {
                _logger.Trace($"Adding node to NodeTable: {node}");
            }
            int        distanceFromMaster = _nodeDistanceCalculator.CalculateDistance(MasterNode.IdHash.Bytes, node.IdHash.Bytes);
            NodeBucket bucket             = Buckets[distanceFromMaster > 0 ? distanceFromMaster - 1 : 0];

            return(bucket.AddNode(node));
        }
Exemple #9
0
        public void Initialize(PublicKey masterNodeKey)
        {
            Buckets    = new NodeBucket[_discoveryConfig.BucketsCount];
            MasterNode = new Node(masterNodeKey, _discoveryConfig.MasterHost, _discoveryConfig.MasterPort);
            if (_logger.IsTrace)
            {
                _logger.Trace($"Created MasterNode: {MasterNode}");
            }

            for (var i = 0; i < Buckets.Length; i++)
            {
                Buckets[i] = new NodeBucket(i, _discoveryConfig.BucketSize);
            }
        }
Exemple #10
0
        public void Initialize(PublicKey masterNodeKey)
        {
            Buckets    = new NodeBucket[_discoveryConfig.BucketsCount];
            MasterNode = new Node(masterNodeKey, _networkConfig.ExternalIp, _networkConfig.DiscoveryPort);
            if (_logger.IsTrace)
            {
                _logger.Trace($"Created MasterNode: {MasterNode}");
            }

            for (int i = 0; i < Buckets.Length; i++)
            {
                Buckets[i] = new NodeBucket(i, _discoveryConfig.BucketSize);
            }
        }
Exemple #11
0
        public void Initialize(PublicKey masterNodeKey)
        {
            Buckets = new NodeBucket[_networkConfig.BucketsCount];
            var pass    = new SecureString();
            var rawPass = _networkConfig.KeyPass;

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

            pass.MakeReadOnly();

            MasterNode = new Node(masterNodeKey, _networkConfig.MasterHost, _networkConfig.MasterPort);
            if (_logger.IsTrace)
            {
                _logger.Trace($"Created MasterNode: {MasterNode}");
            }

            for (var i = 0; i < Buckets.Length; i++)
            {
                Buckets[i] = new NodeBucket(i, _networkConfig.BucketSize);
            }
        }