routingtable.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:mgr.p2p.proxy 作者: tomusdrw 项目源码 文件源码
def addContact(self, contact):
        """ Add the given contact to the correct k-bucket; if it already
        exists, its status will be updated

        @param contact: The contact to add to this node's k-buckets
        @type contact: kademlia.contact.Contact
        """
        if contact.id == self._parentNodeID:
            return

        # Initialize/reset the "successively failed RPC" counter
        contact.failedRPCs = 0

        bucketIndex = self._kbucketIndex(contact.id)
        try:
            self._buckets[bucketIndex].addContact(contact)
        except kbucket.BucketFull:
            # The bucket is full; see if it can be split (by checking if its range includes the host node's id)
            if self._buckets[bucketIndex].keyInRange(self._parentNodeID):
                self._splitBucket(bucketIndex)
                # Retry the insertion attempt
                self.addContact(contact)
            else:
                # We can't split the k-bucket
                # NOTE: This implementation follows section 4.1 of the 13 page version
                # of the Kademlia paper (optimized contact accounting without PINGs
                #- results in much less network traffic, at the expense of some memory)

                # Put the new contact in our replacement cache for the corresponding k-bucket (or update it's position if it exists already)
                if not self._replacementCache.has_key(bucketIndex):
                    self._replacementCache[bucketIndex] = []
                if contact in self._replacementCache[bucketIndex]:
                    self._replacementCache[bucketIndex].remove(contact)
                #TODO: Using k to limit the size of the contact replacement cache - maybe define a seperate value for this in constants.py?
                elif len(self._replacementCache) >= constants.k:
                    self._replacementCache.pop(0)
                self._replacementCache[bucketIndex].append(contact)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号