]> git.mxchange.org Git - quix0rs-apt-p2p.git/blobdiff - ktable.py
automatically check responses to make sure it's from the node
[quix0rs-apt-p2p.git] / ktable.py
index c34f3630410c44941ee4f27329305001e977481b..5bbe8d9209944ca73186c8720d1671d10bd40b70 100644 (file)
--- a/ktable.py
+++ b/ktable.py
@@ -61,27 +61,25 @@ class KTable:
        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
@@ -108,12 +106,15 @@ 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)
+       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
@@ -123,23 +124,29 @@ 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
+               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]
        
@@ -189,7 +196,6 @@ class KBucket:
     def getNodeWithInt(self, int):
        try:
            return self.l[self.l.index(int)]
-           self.touch()
        except IndexError:
            raise ValueError