Beispiel #1
0
    def find_successor(self,
                       key: int,
                       start_key: int,
                       count: int = 0,
                       use_fingers=True) -> ('Node', str):
        msg = ""

        # Check if the key is between us an our successor.
        # If that is the case we are done and can return the
        # successor.
        if in_interval(self.key, self.successor.key, key):
            return self.successor, "Found using self successor", count
        # Also check the predecessor
        if self.predecessor is not None:
            if in_interval(self.predecessor.key, self.key, key):
                return Node(self.ip,
                            self.port), "Found using self predecessor", count

        # Trying finger tables first if enabled
        if use_fingers:
            finger_result, msg, finger_count = self._use_fingertable(
                key, start_key, count)
            if finger_result is not None:
                return finger_result, msg, finger_count

        # Finger table did not return a result or is not enabled
        # Trying slow method to move forward
        slow_result, msg_slow, slow_count = self._slow_successor(
            key, start_key, count)
        if slow_result is not None:
            return slow_result, msg_slow + " : " + msg, slow_count

        return None, "Everything failed (returned None): " + msg_slow + " : " + msg, count
Beispiel #2
0
    def give_photons(self, key: int):
        result = [
            x.photon_id for x in self.photons
            if in_interval(self.key, key, x.key)
        ]
        self.photons = [
            x for x in self.photons if not in_interval(self.key, key, x.key)
        ]

        print(self.port, "Photons left: ", self.photons)
        print(self.port, "Giving photons to: ", key, result)

        return result
Beispiel #3
0
 def closest_preceding_finger(self, key: int):
     for i in range(self.max_i, -1, -1):
         if in_interval(self.key, key, self.keys[i]):
             if not self.fingers[i]:
                 continue
             return self.fingers[i]
     return None
Beispiel #4
0
    def stabilize(self):
        # clear last request key
        self.last_request_key = None
        self.last_request_owner = []

        if self.key != self.successor.key:
            x = None

            # Request successor for it's predecessor
            url = 'http://{0}:{1}/predecessor'.format(self.successor.ip,
                                                      self.successor.port)
            try:
                data = json.loads(requests.get(url).text)
            except:
                self.set_new_successor()
                data = None

            if data is not None:
                if data['predecessor']:
                    x = Node(data['ip'], data['port'])

            if x and in_interval(self.key, self.successor.key, x.key):
                self.successor = x

            url = 'http://{0}:{1}/notify'.format(self.successor.ip,
                                                 self.successor.port)
            requests.post(url, data={
                'ip': self.ip,
                'port': self.port
            })  # TODO: perhaps check for answer?
        else:
            if self.predecessor and in_interval(self.key, self.successor.key,
                                                self.predecessor.key):
                self.successor = self.predecessor
            self.notify(self)
        self.finger_table.update_finger(0, self.successor)
Beispiel #5
0
 def notify(self, node: 'Node'):
     if not self.predecessor or in_interval(self.predecessor.key, self.key,
                                            node.key):
         self.predecessor = node