def network_consumer(client, delegate): received_commands = client.fetch_commands() if received_commands is None: return for command in received_commands: if command.type == common.MessageType.LIST_ROOMS: delegate.update_rooms_attributes(command.data) elif command.type == common.MessageType.LIST_CLIENTS: clients_attributes, _ = common.decode_json(command.data, 0) delegate.update_clients_attributes(clients_attributes)
def download_room(host: str, port: int, room_name: str) -> Tuple[Dict[str, Any], List[Command]]: from mixer.broadcaster.common import decode_json, RoomAttributes logger.info("Downloading room %s", room_name) commands = [] with Client(host, port) as client: client.join_room(room_name) room_attributes = None try: while room_attributes is None or len(commands) < room_attributes[ RoomAttributes.COMMAND_COUNT]: received_commands = client.fetch_incoming_commands() for command in received_commands: if room_attributes is None and command.type == MessageType.LIST_ROOMS: rooms_attributes, _ = decode_json(command.data, 0) if room_name not in rooms_attributes: logger.error("Room %s does not exist on server", room_name) return {}, [] room_attributes = rooms_attributes[room_name] logger.info( "Meta data received, number of commands in the room: %d", room_attributes[RoomAttributes.COMMAND_COUNT], ) elif command.type <= MessageType.COMMAND: continue # don't store server protocol commands commands.append(command) if room_attributes is not None: logger.debug( "Command %d / %d received", len(commands), room_attributes[RoomAttributes.COMMAND_COUNT]) except ClientDisconnectedException: logger.error( f"Disconnected while downloading room {room_name} from {host}:{port}" ) return {}, [] assert room_attributes is not None client.leave_room(room_name) return room_attributes, commands
def _set_room_custom_attributes(command: common.Command): room_name, offset = common.decode_string(command.data, 0) custom_attributes, _ = common.decode_json(command.data, offset) self._server.set_room_custom_attributes(room_name, custom_attributes)
def _set_client_custom_attributes(command: common.Command): _set_custom_attributes(common.decode_json(command.data, 0)[0])
def _handle_client_update(self, command: common.Command): clients_attributes_update, _ = common.decode_json(command.data, 0) update_named_attributes(self.clients_attributes, clients_attributes_update)
def _handle_room_update(self, command: common.Command): rooms_attributes_update, _ = common.decode_json(command.data, 0) update_named_attributes(self.rooms_attributes, rooms_attributes_update)