From: Cameron Dale Date: Thu, 27 Mar 2008 00:09:04 +0000 (-0700) Subject: Use copies of the routing table nodes in actions. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=8fc0a36f42c9cf863a35b79cb013b5d0348b9fce;p=quix0rs-apt-p2p.git Use copies of the routing table nodes in actions. The recursive actions need to use copies of the nodes in the routing table, otherwise the token and num_values could be preset or set twice by multiple actions. --- diff --git a/apt_p2p_Khashmir/khashmir.py b/apt_p2p_Khashmir/khashmir.py index f8c718f..aa1a181 100644 --- a/apt_p2p_Khashmir/khashmir.py +++ b/apt_p2p_Khashmir/khashmir.py @@ -9,6 +9,7 @@ warnings.simplefilter("ignore", DeprecationWarning) from datetime import datetime, timedelta from random import randrange, shuffle from sha import sha +from copy import copy import os from twisted.internet.defer import Deferred @@ -175,6 +176,7 @@ class KhashmirBase(protocol.Factory): """ # Get K nodes out of local table/cache nodes = self.table.findNodes(id) + nodes = [copy(node) for node in nodes] d = Deferred() if errback: d.addCallbacks(callback, errback) @@ -374,6 +376,7 @@ class KhashmirRead(KhashmirBase): """ # Get K nodes out of local table/cache nodes = self.table.findNodes(key) + nodes = [copy(node) for node in nodes] d = Deferred() if errback: d.addCallbacks(callback, errback) diff --git a/apt_p2p_Khashmir/node.py b/apt_p2p_Khashmir/node.py index 49b8fe7..c1300c7 100644 --- a/apt_p2p_Khashmir/node.py +++ b/apt_p2p_Khashmir/node.py @@ -105,6 +105,17 @@ class Node: def __repr__(self): return `(self.id, self.host, self.port)` + def __copy__(self): + """Create a shallow copy of the node, resetting some values.""" + cp = self.__class__(self.id, self.host, self.port) + cp.fails = self.fails + cp.lastSeen = self.lastSeen + if getattr(self, 'table', None) is not None: + cp.table = self.table + if getattr(self, 'conn', None) is not None: + cp.conn = self.conn + return cp + #{ Comparators to bisect/index a list of nodes with either a node or a long def __lt__(self, a): if type(a) == InstanceType: