Exemple #1
0
 def __init__(self, processId):
     """
     it represents the constructor for the class,
     which initializes the object variables  
     @param processId: the system assigned process Id
     @type processId: integer 
     """
     self.processId = processId
     self.nodeId = computeIntHash(newID())
 def findNodes(self, value):
     """ 
     returns K nodes from its routing table 
     closest to the provided node id
         @param value: node id for which K nearest nodes to find 
         @type value: Kademlia.node.Node or integer, string representation 
         of 160-bit identifier computed through sha
         @return: list of K nodes nearest to the provided id
         @raise Exception: if value provided is not of suitable type 
     """  
     #check if nodeId has a valid type
     if isinstance(value, str):
         num = computeIntHash(value)
     elif isinstance(value, Node):
         num = value.nodeId
     elif isinstance(value, int):
         num = value
     else:
         raise Exception("findNodes expects integer, string, or Node argument")
         
     nodes = []
     bucketIndex = self.bucketIndexForInt(num)
     nodes = nodes + self.bucketList[bucketIndex].peerList
     
     if len(nodes) < K:
         # need more nodes
         minValue = bucketIndex - 1
         maxValue = bucketIndex + 1
         while len(nodes) < K and (minValue >= 0 or maxValue < len(self.bucketList)):
             if minValue >= 0:
                 nodes = nodes + self.bucketList[minValue].getPeers(K - len(nodes), self.node.nodeId, True)
                 minValue = minValue - 1
             if maxValue < len(self.bucketList):
                 nodes = nodes + self.bucketList[maxValue].getPeers(K - len(nodes), self.node.nodeId, False)
                 maxValue = maxValue + 1
     nodes.sort()           
     return nodes[0:K]