def on_multicast_received(self, host, method, message): if method == "MAIN_GROUP/HOSTS": hosts = json.loads(message) list_of_hosts = [Host.from_json(h) for h in hosts] client_ids = [ client.id for client in list_of_hosts if client.host_type == HostType.CLIENT ] for game in self.games: removed = False t_players = [] for player in game.players: if player.id in client_ids: p = game.get_player(player.id) t_players.append(p) else: posx, posy = game.get_player_position(player) game.grid.cells[posy][posx].player = None removed = True game.players = t_players if removed: self.send_game(game) if method == "GS/OV": game = Game.from_pickle(message) if not game: return if game not in self.games: print("FOUND NEW GAME!") self.games.append(game) for i, _game in enumerate(self.games): if _game.id == game.id and _game.seq + 1 == game.seq: self.games[i] = game
def on_unicast_received(self, host: Host, method: str, message: str): if method == "SERVER/WELCOME": print("Got other host, stop broadcasting") self.discovery_service.stop_broadcasting() data = json.loads(message) host = Host.from_json(data.get("host")) hosts = json.loads(data.get("hosts")) list_of_hosts = [Host.from_json(h) for h in hosts] for new_host in list_of_hosts: self.currently_initializing.append(new_host.id) self.socket_service.send_unicast(new_host, "SERVER/JOIN", "") if method == "SERVER/JOIN": self.all_host_group.add_participant(host) print("ANSWERED HOST!") current_leader = False if self.election_service.current_leader: current_leader = self.election_service.current_leader == self.own_host self.socket_service.send_unicast(host, "SERVER/JOINED", str(current_leader)) if method == "SERVER/JOINED": print("Got answer from host!") self.all_host_group.add_participant(host) self.currently_initializing.remove(host.id) if message == "True": print("Got leader") self.election_service.set_leader(host.id) if len(self.currently_initializing) == 0: self.socket_service.add_group(self.all_host_group) self.all_host_group.announce_participants() self.socket_service.send_unicast( self.election_service.current_leader, "GS/SYNC_GAMES", '') time.sleep(3) if not self.election_service.leader_elected(): self.election_service.start_election() elif method == "HB": self.heartbeat_service.on_heartbeat_received(host, message) self.election_service.on_message_received(host, method, message) self.game_service.on_unicast_received(host, method, message)
def on_multicast_received(self, host, method: str, message, header): if method == "MAIN_GROUP/HOSTS": if 'resent' not in header: hosts = json.loads(message) list_of_hosts = [Host.from_json(h) for h in hosts] self.set_host_list(list_of_hosts) if method == "MAIN_GROUP/GET_HOSTS": self.socket_service.send_group_multicast( self, f"MAIN_GROUP/HOSTS", json.dumps([ob.to_json() for ob in self.participants]), )
def on_server_multicast_received(self, host: Host, method: str, message: str, header): self.all_host_group.on_multicast_received(host, method, message, header) self.election_service.on_message_received(host, method, message) if method == "MAIN_GROUP/HOSTS": hosts = json.loads(message) list_of_hosts_id = [Host.from_json(h).id for h in hosts] if self.election_service.current_leader and self.election_service.current_leader.id not in list_of_hosts_id: self.election_service.start_election() elif not self.election_service.current_leader: self.election_service.start_election() self.game_service.on_multicast_received(host, method, message)
def on_unicast_received(self, host: Host, method: str, message: str): if method == "SERVER/WELCOME": print("Got other host, stop broadcasting") self.discovery_service.stop_broadcasting() data = json.loads(message) hosts = json.loads(data.get("hosts")) list_of_hosts = [Host.from_json(h) for h in hosts] for new_host in list_of_hosts: self.currently_initializing.append(new_host.id) self.socket_service.send_unicast(new_host, "SERVER/JOIN", "") elif method == "SERVER/JOINED": print("Got answer from host!") self.all_host_group.add_participant(host) self.currently_initializing.remove(host.id) if message == "True": print("Got leader") self.leader = host if len(self.currently_initializing) == 0: print("Announce new group") self.socket_service.add_group(self.all_host_group) self.all_host_group.announce_participants() self.run_game() if method == "SERVER/JOIN": self.all_host_group.add_participant(host) print("ANSWERED HOST!") current_leader = False self.socket_service.send_unicast(host, "SERVER/JOINED", str(current_leader)) elif method == "GS/MS": print(message) elif method == "GS/ERROR": self.run_game() print(message) elif method == "GS/LS": self.run_game() print(message) elif method == "HB": self.heartbeat_service.on_heartbeat_received(host, message)
while True: (read, write, exception) = select.select([multicast_socket], [], []) # Iterate through the tagged read descriptors for receiver in read: try: data, address = receiver.recvfrom(BUFFER_SIZE) encoded_data = data.decode('utf-8') hostname = address[0] port = address[1] chunks = encoded_data.split(HEADER_DELIMITER) header = json.loads(chunks[0]) method = header.get('m') message = '' if len(chunks) > 1: message = chunks[1] message_chunks = message.split(PAYLOAD_DELIMITER) if method == "LCR/LEADER_CHANGED": leader_id = message_chunks[0] render() elif method == "MAIN_GROUP/HOSTS": print("GOT HOST UPDATE") if 'resent' not in header: hosts = json.loads(message) hosts = [Host.from_json(h) for h in hosts] render() except Exception as e: print("Exception", e) traceback.print_exc() continue