Beispiel #1
0
    def __init__(self):
        self.own_host = Host(str(uuid.uuid1()), get_local_address(),
                             get_random_unicast_port(), HostType.SERVER)

        print(
            f'############################################ \n'
            f'# SERVER                                     \n'
            f'# Id: {self.own_host.id} #\n'
            f'# Address: {self.own_host.address}:{self.own_host.unicast_port}       #\n'
            f'############################################ \n')

        self.socket_service = SocketService(self.own_host, self.on_host_failed)
        self.socket_service.set_on_broadcast_delivered(
            self.on_broadcast_received)
        self.socket_service.set_on_multicast_delivered(
            self.on_server_multicast_received)
        self.socket_service.set_on_unicast_delivered(self.on_unicast_received)

        self.currently_initializing = []
        self.in_init = False
        self.ring = Ring(self.own_host)
        self.server_ring = Ring(self.own_host)

        self.all_host_group = KnownHostGroup(self.own_host, self.ring,
                                             self.server_ring,
                                             self.socket_service)
        self.all_host_group.add_participant(self.own_host)

        self.election_service = LCRElection(self.socket_service, self.own_host,
                                            self.server_ring,
                                            self.all_host_group)

        self.discovery_service = DiscoveryService(self.own_host,
                                                  self.socket_service,
                                                  self.discovery_counter)

        self.game_service = GameService(self.socket_service,
                                        self.all_host_group)

        self.heartbeat_service = HeartbeatService(self.own_host, self.ring,
                                                  self.socket_service)
        self.heartbeat_service.set_on_heartbeat_missing(self.on_host_missing)

        self.discovery_service.daemon = True
        self.socket_service.daemon = True
        self.heartbeat_service.daemon = True

        self.discovery_service.start_broadcasting()
        self.heartbeat_service.start_heartbeat()

        self.socket_service.start()
        self.discovery_service.start()

        self.socket_service.join()
        self.discovery_service.join()
Beispiel #2
0
    def __init__(self):
        self.own_host = Host(str(uuid.uuid1()), get_local_address(),
                             get_random_unicast_port(), HostType.CLIENT)

        self.current_game = None
        self.currently_initializing = []
        self.leader = None

        print(
            f'############################################ \n'
            f'# CLIENT                                     \n'
            f'# Id: {self.own_host.id} #\n'
            f'# Address: {self.own_host.address}:{self.own_host.unicast_port}       #\n'
            f'############################################ \n')

        self.ring = Ring(self.own_host)
        self.server_ring = Ring(self.own_host)

        self.socket_service = SocketService(self.own_host, self.on_host_failed)
        self.socket_service.sockets.remove(
            self.socket_service.broadcast_socket)
        self.all_host_group = KnownHostGroup(self.own_host, self.ring,
                                             self.server_ring,
                                             self.socket_service)
        self.all_host_group.add_participant(self.own_host)

        self.socket_service.set_on_broadcast_delivered(
            self.on_broadcast_received)
        self.socket_service.set_on_multicast_delivered(
            self.on_server_multicast_received)
        self.socket_service.set_on_unicast_delivered(self.on_unicast_received)

        self.discovery_service = DiscoveryService(self.own_host,
                                                  self.socket_service, None)

        self.heartbeat_service = HeartbeatService(self.own_host, self.ring,
                                                  self.socket_service)
        self.heartbeat_service.set_on_heartbeat_missing(self.on_host_missing)
        self.keybord = KeyboardThread(self.take_input)

        self.discovery_service.daemon = True
        self.keybord.daemon = True
        self.socket_service.daemon = True

        self.discovery_service.start_broadcasting()
        self.heartbeat_service.start_heartbeat()

        self.socket_service.start()
        self.discovery_service.start()
        self.keybord.start()

        self.keybord.join()
        self.socket_service.join()
        self.discovery_service.join()
Beispiel #3
0
    def test_ordering(self):
        own_node = Host("0000000", 0000)
        node_one = Host("1111111", 1111)
        node_two = Host("2222222", 2222)
        ring = Ring(own_node)

        ring.add_node(node_one)
        ring.add_node(node_two)

        assert ring.nodes[0].id == own_node.id
        assert ring.nodes[1].id == node_one.id
        assert ring.nodes[2].id == node_two.id
Beispiel #4
0
    def test_get_right_neighbour(self):
        own_node = Host("0000000", 0000)
        node_one = Host("1111111", 1111)
        node_two = Host("2222222", 2222)
        ring = Ring(own_node)

        ring.add_node(node_one)
        ring.remove_node(node_one)
        ring.add_node(node_two)

        neighbour = ring.get_right_neighbour()

        assert neighbour.id == node_two.id
Beispiel #5
0
    def test_ordering_delete(self):
        own_node = Host("0000000", 0000)
        node_one = Host("1111111", 1111)
        node_two = Host("2222222", 2222)
        ring = Ring(own_node)

        ring.add_node(node_one)
        ring.remove_node(node_one)
        ring.add_node(node_two)

        assert len(ring.nodes) == 2
        assert ring.nodes[0].id == own_node.id
        assert ring.nodes[1].id == node_two.id
Beispiel #6
0
    def test_multiple_add(self):
        own_node = Host("0000000", 0000)
        node_one = Host("1111111", 1111)
        node_two = Host("2222222", 2222)
        ring = Ring(own_node)

        ring.add_node(node_one)
        ring.add_node(node_two)
        ring.add_node(node_two)
        ring.add_node(node_two)
        ring.add_node(node_two)

        assert len(ring.nodes) == 3