Exemplo n.º 1
0
        public NodeContact[] GetKClosestContacts(BinaryID networkID)
        {
            NodeContact[] contacts      = null;
            KBucket       closestBucket = FindClosestBucket(networkID);

            if (closestBucket._totalContacts >= DhtClient.KADEMLIA_K)
            {
                contacts = closestBucket.GetAllContacts(false);

                if (contacts.Length > DhtClient.KADEMLIA_K)
                {
                    return(GetClosestContacts(contacts, networkID, DhtClient.KADEMLIA_K));
                }
                else if (contacts.Length == DhtClient.KADEMLIA_K)
                {
                    return(contacts);
                }
                else if (closestBucket._parentBucket == null)
                {
                    return(contacts);
                }
                else
                {
                    contacts = null;
                }
            }

            while (closestBucket._parentBucket != null)
            {
                KBucket parentBucket = closestBucket._parentBucket;

                if (parentBucket._totalContacts >= DhtClient.KADEMLIA_K)
                {
                    contacts = parentBucket.GetAllContacts(false);

                    if (contacts.Length > DhtClient.KADEMLIA_K)
                    {
                        return(GetClosestContacts(contacts, networkID, DhtClient.KADEMLIA_K));
                    }
                    else if (contacts.Length == DhtClient.KADEMLIA_K)
                    {
                        return(contacts);
                    }
                }

                closestBucket = parentBucket;
            }

            if (contacts == null)
            {
                return(closestBucket.GetAllContacts(false));
            }
            else
            {
                return(contacts);
            }
        }
Exemplo n.º 2
0
        public NodeContact[] GetAllContacts(bool includeReplacementCache)
        {
            _lock.TryEnterReadLock(LOCK_TIMEOUT);
            try
            {
                NodeContact[] contacts;

                if (_contacts == null)
                {
                    NodeContact[] leftContacts  = _leftBucket.GetAllContacts(includeReplacementCache);
                    NodeContact[] rightContacts = _rightBucket.GetAllContacts(includeReplacementCache);

                    contacts = new NodeContact[leftContacts.Length + rightContacts.Length];

                    Array.Copy(leftContacts, contacts, leftContacts.Length);
                    Array.Copy(rightContacts, 0, contacts, leftContacts.Length, rightContacts.Length);
                }
                else
                {
                    int cacheCount;

                    if (includeReplacementCache)
                    {
                        cacheCount = _totalReplacementContacts;
                    }
                    else
                    {
                        cacheCount = 0;
                    }

                    List <NodeContact> contactsList = new List <NodeContact>(_totalContacts + cacheCount);

                    foreach (NodeContact contact in _contacts.Values)
                    {
                        if (!contact.IsStale() && !contact.IsCurrentNode)
                        {
                            contactsList.Add(contact);
                        }
                    }

                    if (includeReplacementCache)
                    {
                        foreach (NodeContact contact in _replacementContacts.Values)
                        {
                            if (!contact.IsStale())
                            {
                                contactsList.Add(contact);
                            }
                        }
                    }

                    contacts = contactsList.ToArray();
                }

                return(contacts);
            }
            finally
            {
                _lock.ExitReadLock();
            }
        }
Exemplo n.º 3
0
        public NodeContact[] GetKClosestContacts(BinaryNumber nodeID)
        {
            KBucket currentBucket = this;

            while (true)
            {
                NodeContact[] contacts    = currentBucket._contacts;
                KBucket       leftBucket  = currentBucket._leftBucket;
                KBucket       rightBucket = currentBucket._rightBucket;

                if ((contacts != null) || (leftBucket == null) || (rightBucket == null))
                {
                    #region find closest contacts from this bucket

                    KBucket       closestBucket   = currentBucket;
                    NodeContact[] closestContacts = null;

                    if (closestBucket._contactCount >= DhtNode.KADEMLIA_K)
                    {
                        closestContacts = closestBucket.GetAllContacts(false);

                        if (closestContacts.Length > DhtNode.KADEMLIA_K)
                        {
                            return(GetClosestContacts(closestContacts, nodeID, DhtNode.KADEMLIA_K));
                        }

                        if (closestContacts.Length == DhtNode.KADEMLIA_K)
                        {
                            return(closestContacts);
                        }

                        if (closestBucket._parentBucket == null)
                        {
                            return(closestContacts);
                        }
                    }

                    while (closestBucket._parentBucket != null)
                    {
                        KBucket parentBucket = closestBucket._parentBucket;

                        closestContacts = parentBucket.GetAllContacts(false);

                        if (closestContacts.Length > DhtNode.KADEMLIA_K)
                        {
                            return(GetClosestContacts(closestContacts, nodeID, DhtNode.KADEMLIA_K));
                        }

                        if (closestContacts.Length == DhtNode.KADEMLIA_K)
                        {
                            return(closestContacts);
                        }

                        closestBucket = parentBucket;
                    }

                    if (closestContacts == null)
                    {
                        closestContacts = closestBucket.GetAllContacts(false);
                    }

                    return(closestContacts);

                    #endregion
                }

                if ((leftBucket._bucketID & nodeID) == leftBucket._bucketID)
                {
                    currentBucket = leftBucket;
                }
                else
                {
                    currentBucket = rightBucket;
                }
            }
        }