except ValueError:
pass
else:
- self.buckets[i].touch()
return [self.buckets[i].l[index]]
nodes = nodes + self.buckets[i].l
- if len(nodes) == K:
+ if len(nodes) >= K:
nodes.sort(sort)
- return nodes
+ return nodes[:K]
else:
# need more nodes
min = i - 1
max = i + 1
- while (len(nodes) < K and (min >= 0 and max < len(self.buckets))):
+ while (len(nodes) < K and (min >= 0 or max < len(self.buckets))):
if min >= 0:
nodes = nodes + self.buckets[min].l
- self.buckets[min].touch()
if max < len(self.buckets):
nodes = nodes + self.buckets[max].l
- self.buckets[max].touch()
-
+ min = min - 1
+ max = max + 1
nodes.sort(sort)
- return nodes[:K-1]
+ return nodes[:K]
def _splitBucket(self, a):
diff = (a.max - a.min) / 2
if new:
self.buckets[i].l.append(new)
- def insertNode(self, node):
+ def insertNode(self, node, contacted=1):
"""
this insert the node, returning None if successful, returns the oldest node in the bucket if it's full
the caller responsible for pinging the returned node and calling replaceStaleNode if it is found to be stale!!
+ contacted means that yes, we contacted THEM and we know the node is available
"""
assert(node.id != " "*20)
+ if node.id == self.node.id:
+ return
# get the bucket for this node
i = self. _bucketIndexForInt(node.int)
## check to see if node is in the bucket already
## no
pass
else:
- node.updateLastSeen()
- # move node to end of bucket
- del(self.buckets[i].l[it])
- self.buckets[i].l.append(node)
- self.buckets[i].touch()
+ if contacted:
+ node.updateLastSeen()
+ # move node to end of bucket
+ xnode = self.buckets[i].l[it]
+ del(self.buckets[i].l[it])
+ # note that we removed the original and replaced it with the new one
+ # utilizing this nodes new contact info
+ self.buckets[i].l.append(xnode)
+ self.buckets[i].touch()
return
# we don't have this node, check to see if the bucket is full
if len(self.buckets[i].l) < K:
# no, append this node and return
+ if contacted:
+ node.updateLastSeen()
self.buckets[i].l.append(node)
self.buckets[i].touch()
return
# bucket is full, check to see if self.node is in the bucket
try:
- me = self.buckets[i].l.index(self.node)
+ me = self.buckets[i].min <= self.node < self.buckets[i].max
except ValueError:
return self.buckets[i].l[0]
def getNodeWithInt(self, int):
try:
return self.l[self.l.index(int)]
- self.touch()
except IndexError:
raise ValueError