fixed some serious bugs in findNode
authorburris <burris>
Sat, 21 Sep 2002 20:41:25 +0000 (20:41 +0000)
committerburris <burris>
Sat, 21 Sep 2002 20:41:25 +0000 (20:41 +0000)
also, change behaviour of insertNode based on if we have a confirmed contact or not

ktable.py

index c34f3630410c44941ee4f27329305001e977481b..7757829b1c77d0da0306b78eba30193545bbb303 100644 (file)
--- 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