From be26529140300dfa8c981a3b20fd1960154afe15 Mon Sep 17 00:00:00 2001 From: burris Date: Sat, 21 Sep 2002 20:41:25 +0000 Subject: [PATCH] fixed some serious bugs in findNode also, change behaviour of insertNode based on if we have a confirmed contact or not --- ktable.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/ktable.py b/ktable.py index c34f363..7757829 100644 --- a/ktable.py +++ b/ktable.py @@ -65,21 +65,22 @@ class KTable: 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] @@ -108,10 +109,11 @@ class KTable: 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) # get the bucket for this node @@ -123,16 +125,21 @@ class KTable: ## 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 + 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(node) + 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 -- 2.39.2