Example #1
0
 def initWithDict(self, dict):
     self._senderDict = dict
     self.id = dict['id']
     self.num = intify(self.id)
     self.port = dict['port']
     self.host = dict['host']
     return self
Example #2
0
    def __init__(self, id, host=None, port=None):
        """Initialize the node.
        
        @type id: C{string} or C{dictionary}
        @param id: the node's ID in the DHT, or a dictionary containing the
            node's id, host and port
        @type host: C{string}
        @param host: the IP address of the node
            (optional, but must be specified if id is not a dictionary)
        @type port: C{int}
        @param port: the port of the node
            (optional, but must be specified if id is not a dictionary)
        """
        self.fails = 0
        self.lastSeen = datetime(MINYEAR, 1, 1)

        # Alternate method, init Node from dictionary
        if isinstance(id, dict):
            host = id["host"]
            port = id["port"]
            id = id["id"]

        assert isinstance(id, str)
        assert isinstance(host, str)
        self.id = id
        self.num = khash.intify(id)
        self.host = host
        self.port = int(port)
        self.token = ""
        self.num_values = 0
        self._contactInfo = None
Example #3
0
 def init(self, id, host, port):
     self.id = id
     self.num = khash.intify(id)
     self.host = host
     self.port = port
     self._senderDict = {'id': self.id, 'port' : self.port, 'host' : self.host}
     return self
Example #4
0
 def init(self, id, host, port):
     self.id = id
     self.num = intify(id)
     self.host = host
     self.port = port
     self._senderDict = {'id': self.id, 'port' : self.port, 'host' : self.host}
     return self
Example #5
0
 def initWithDict(self, dict):
     self._senderDict = dict
     self.id = dict['id']
     self.num = khash.intify(self.id)
     self.port = dict['port']
     self.host = dict['host']
     return self
Example #6
0
 def findNodes(self, id):
     """
     Return K nodes in our own local table closest to the ID.
     """
     
     if isinstance(id, str):
         num = intify(id)
     elif isinstance(id, Node):
         num = id.num
     elif isinstance(id, int) or isinstance(id, long):
         num = id
     else:
         raise TypeError, "findNodes requires an int, string, or Node"
         
     nodes = []
     i = self._bucketIndexForInt(num)
     
     # Get the K closest nodes, even if we have the exact node we're looking for
     nodes += self.buckets[i].l
     if len(nodes) < K:
         # need more nodes
         min = i - 1
         max = i + 1
         while len(nodes) < K and (min >= 0 or max < len(self.buckets)):
             #ASw: note that this requires K be even
             if min >= 0:
                 nodes = nodes + self.buckets[min].l
             if max < len(self.buckets):
                 nodes = nodes + self.buckets[max].l
             min = min - 1
             max = max + 1
 
     nodes.sort(key = lambda x: num ^ x.num)
     return nodes[:K]
Example #7
0
    def getNode(self, id):
        """
        Return the node with that ID if it's in our own local table.
        """        
        if isinstance(id, str):
            num = intify(id)
        elif isinstance(id, Node):
            num = id.num
        elif isinstance(id, int) or isinstance(id, long):
            num = id
        else:
            raise TypeError, "getNode requires an int, string, or Node"

        i = self._bucketIndexForInt(num)

        # if this node is already in our table then return it
        try:
            index = self.buckets[i].l.index(num)
        except ValueError:
            pass
        else:
            return self.buckets[i].l[index]
            
        # don't have the node
        return None
Example #8
0
 def initWithDict(self, dict):
     self._senderDict = dict
     self.id = dict['id']
     self.num = khash.intify(self.id)
     self.port = dict['port']
     self.host = dict['host']
     self.age = dict.get('age', self.age)
     return self
Example #9
0
    def __init__(self,
                 caller,
                 target,
                 callback,
                 config,
                 stats,
                 action,
                 num_results=None):
        """Initialize the action.
        
        @type caller: L{khashmir.Khashmir}
        @param caller: the DHT instance that is performing the action
        @type target: C{string}
        @param target: the target of the action, usually a DHT key
        @type callback: C{method}
        @param callback: the method to call with the results
        @type config: C{dictionary}
        @param config: the configuration variables for the DHT
        @type stats: L{stats.StatsLogger}
        @param stats: the statistics gatherer
        @type action: C{string}
        @param action: the name of the action to call on remote nodes
        @type num_results: C{int}
        @param num_results: the minimum number of results that are needed before
            the action should stop (optional, defaults to getting all the results)
        
        """

        self.caller = caller
        self.target = target
        self.config = config
        self.action = action
        self.stats = stats
        self.stats.startedAction(action)
        self.num = intify(target)
        self.queried = {}
        self.answered = {}
        self.failed = {}
        self.found = {}
        self.sorted_nodes = []
        self.results = {}
        self.desired_results = num_results
        self.callback = callback
        self.outstanding = {}
        self.outstanding_results = 0
        self.finished = False
        self.started = datetime.now()

        def sort(a, b, num=self.num):
            """Sort nodes relative to the ID we are looking for."""
            x, y = num ^ a.num, num ^ b.num
            if x > y:
                return 1
            elif x < y:
                return -1
            return 0

        self.sort = sort
Example #10
0
 def __init__(self, table, target, callback, callLater):
     self.table = table
     self.target = target
     self.callLater = callLater
     self.num = intify(target)
     self.found = {}
     self.foundq = []
     self.queried = {}
     self.queriedip = {}
     self.answered = {}
     self.callback = callback
     self.outstanding = 0
     self.finished = 0
Example #11
0
 def __init__(self, table, target, callback, callLater):
     self.table = table
     self.target = target
     self.callLater = callLater
     self.num = intify(target)
     self.found = {}
     self.foundq = []
     self.queried = {}
     self.queriedip = {}
     self.answered = {}
     self.callback = callback
     self.outstanding = 0
     self.finished = 0
Example #12
0
 def __init__(self, caller, target, callback, config, stats, action, num_results = None):
     """Initialize the action.
     
     @type caller: L{khashmir.Khashmir}
     @param caller: the DHT instance that is performing the action
     @type target: C{string}
     @param target: the target of the action, usually a DHT key
     @type callback: C{method}
     @param callback: the method to call with the results
     @type config: C{dictionary}
     @param config: the configuration variables for the DHT
     @type stats: L{stats.StatsLogger}
     @param stats: the statistics gatherer
     @type action: C{string}
     @param action: the name of the action to call on remote nodes
     @type num_results: C{int}
     @param num_results: the minimum number of results that are needed before
         the action should stop (optional, defaults to getting all the results)
     
     """
     
     self.caller = caller
     self.target = target
     self.config = config
     self.action = action
     self.stats = stats
     self.stats.startedAction(action)
     self.num = intify(target)
     self.queried = {}
     self.answered = {}
     self.failed = {}
     self.found = {}
     self.sorted_nodes = []
     self.results = {}
     self.desired_results = num_results
     self.callback = callback
     self.outstanding = {}
     self.outstanding_results = 0
     self.finished = False
     self.started = datetime.now()
 
     def sort(a, b, num=self.num):
         """Sort nodes relative to the ID we are looking for."""
         x, y = num ^ a.num, num ^ b.num
         if x > y:
             return 1
         elif x < y:
             return -1
         return 0
     self.sort = sort
Example #13
0
File: ktable.py Project: hitzjd/DHT
    def findNodes(self, id, invalid=True):
        """
            return K nodes in our own local table closest to the ID.
        """

        # print 'id:',type(id)

        if isinstance(id, str):
            num = hash.intify(id)
        elif isinstance(id, Node):
            num = id.num
        elif isinstance(id, int) or isinstance(id, long):
            num = id
        else:
            raise TypeError, "findNodes requires an int, string, or Node"
            
        nodes = []
        i = self._bucketIndexForInt(num)
        
        # if this node is already in our table then return it
        try:
            node = self.buckets[i].getNodeWithInt(num)
        except ValueError:
            pass
        else:
            return [node]
            
        # don't have the node, get the K closest nodes
        nodes = nodes + self.buckets[i].l
        if not invalid:
            nodes = [a for a in nodes if not a.invalid]
        if len(nodes) < K:
            # need more nodes
            min = i - 1
            max = i + 1
            while len(nodes) < K and (min >= 0 or max < len(self.buckets)):
                #ASw: note that this requires K be even
                if min >= 0:
                    nodes = nodes + self.buckets[min].l
                if max < len(self.buckets):
                    nodes = nodes + self.buckets[max].l
                min = min - 1
                max = max + 1
                if not invalid:
                    nodes = [a for a in nodes if not a.invalid]

        nodes.sort(lambda a, b, num=num: cmp(num ^ a.num, num ^ b.num))
        return nodes[:K]
Example #14
0
    def findNodes(self, id, invalid=True):
        """
            return K nodes in our own local table closest to the ID.
        """
        
        if isinstance(id, str):
            num = hash.intify(id)
        elif isinstance(id, Node):
            num = id.num
        elif isinstance(id, int) or isinstance(id, long):
            num = id
        else:
            raise TypeError, "findNodes requires an int, string, or Node"
            
        nodes = []
        i = self._bucketIndexForInt(num)
        
        # if this node is already in our table then return it
        try:
            node = self.buckets[i].getNodeWithInt(num)
        except ValueError:
            pass
        else:
            return [node]
            
        # don't have the node, get the K closest nodes
        nodes = nodes + self.buckets[i].l
        if not invalid:
            nodes = [a for a in nodes if not a.invalid]
        if len(nodes) < K:
            # need more nodes
            min = i - 1
            max = i + 1
            while len(nodes) < K and (min >= 0 or max < len(self.buckets)):
                #ASw: note that this requires K be even
                if min >= 0:
                    nodes = nodes + self.buckets[min].l
                if max < len(self.buckets):
                    nodes = nodes + self.buckets[max].l
                min = min - 1
                max = max + 1
                if not invalid:
                    nodes = [a for a in nodes if not a.invalid]

        nodes.sort(lambda a, b, num=num: cmp(num ^ a.num, num ^ b.num))
        return nodes[:K]
Example #15
0
    def _nodeNum(self, id):
        """Takes different types of input and converts to the node ID number.

        @type id: C{string} or C{int} or L{node.Node}
        @param id: the ID to find nodes that are close to
        @raise TypeError: if id does not properly identify an ID
        """

        # Get the ID number from the input
        if isinstance(id, str):
            return khash.intify(id)
        elif isinstance(id, Node):
            return id.num
        elif isinstance(id, int) or isinstance(id, long):
            return id
        else:
            raise TypeError, "requires an int, string, or Node input"
Example #16
0
 def __init__(self, table, target, callback):
     self.table = table
     self.target = target
     self.num = intify(target)
     self.found = {}
     self.queried = {}
     self.answered = {}
     self.callback = callback
     self.outstanding = 0
     self.finished = 0
 
     def sort(a, b, num=self.num):
         """ this function is for sorting nodes relative to the ID we are looking for """
         x, y = num ^ a.num, num ^ b.num
         if x > y:
             return 1
         elif x < y:
             return -1
         return 0
     self.sort = sort