Example #1
0
        public Bucket Split(int cpl, DhtId target)
        {
            _rwlock.EnterWriteLock();
            try
            {
                var output  = new List <PeerId>();
                var newBuck = new Bucket(output);
                foreach (var e in _list)
                {
                    var peerId  = DhtId.ConvertPeerId(e);
                    var peerCpl = DhtId.CommonPrefixLength(peerId, target);

                    if (peerCpl > cpl)
                    {
                        output.Add(e);
                    }
                }

                _list.RemoveAll(output.Contains);

                return(newBuck);
            }
            finally
            {
                _rwlock.ExitWriteLock();
            }
        }
        public PeerId Find(PeerId id)
        {
            var srch = NearestPeers(DhtId.ConvertPeerId(id), 1);

            if (srch.Length == 0 || srch[0] != id)
            {
                return(null);
            }

            return(srch[0]);
        }
        public void Remove(PeerId p)
        {
            lock (_tabLock)
            {
                var peerId = DhtId.ConvertPeerId(p);
                var cpl    = DhtId.CommonPrefixLength(peerId, _local);

                var bucketId = cpl;
                if (bucketId >= Buckets.Length)
                {
                    bucketId = Buckets.Length - 1;
                }

                var bucket = Buckets[bucketId];
                bucket.Remove(p);
            }
        }
        public void Update(PeerId p)
        {
            var peerId = DhtId.ConvertPeerId(p);
            var cpl    = DhtId.CommonPrefixLength(peerId, _local);

            lock (_tabLock)
            {
                var bucketId = cpl;
                if (bucketId >= Buckets.Length)
                {
                    bucketId = Buckets.Length - 1;
                }

                var bucket = Buckets[bucketId];
                if (bucket.Has(p))
                {
                    bucket.MoveToFront(p);
                    return;
                }

                if (_metrics.LatencyEWMA(p) > _maxLatency)
                {
                    return;
                }

                bucket.PushFront(p);

                if (bucket.Length > _bucketSize)
                {
                    if (bucketId == Buckets.Length - 1)
                    {
                        NextBucket();
                    }
                    else
                    {
                        bucket.PopBack();
                    }
                }
            }
        }
Example #5
0
 public static IEnumerable <PeerDistance> CopyPeersFromList(DhtId target, IEnumerable <PeerId> peers)
 {
     return(from peer in peers let pid = DhtId.ConvertPeerId(peer) select new PeerDistance {
         P = peer, Distance = ((byte[])target).XOR(pid)
     });
 }