From 8fc0a36f42c9cf863a35b79cb013b5d0348b9fce Mon Sep 17 00:00:00 2001 From: Cameron Dale Date: Wed, 26 Mar 2008 17:09:04 -0700 Subject: [PATCH 1/1] 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. --- apt_p2p_Khashmir/khashmir.py | 3 +++ apt_p2p_Khashmir/node.py | 11 +++++++++++ 2 files changed, 14 insertions(+) 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: -- 2.30.2