Exemplo n.º 1
0
    def test_join_one_room_two_clients_leave(self):
        delay = self.delay
        server = self._server

        c0_name = "c0_name"
        c0_room = "c0_room"

        c1_name = "c1_name"
        c1_room = c0_room

        d0 = Delegate()
        c0 = Client()
        c0.join_room(c0_room)
        c0.set_client_attributes({common.ClientAttributes.USERNAME: c0_name})

        d1 = Delegate()
        c1 = Client()
        c1.join_room(c1_room)
        c1.set_client_attributes({common.ClientAttributes.USERNAME: c1_name})

        c1.leave_room(c1_room)

        delay()
        network_consumer(c0, self._delegate)
        network_consumer(c1, self._delegate)
        expected = [(c0_name, c0_room)]
        self.assertEqual(server.client_count(), (1, 1))
        self.assertEqual(len(d0.name_room), 1)
        self.assertCountEqual(d0.name_room, expected)
        self.assertListEqual(d0.name_room, d1.name_room)
Exemplo n.º 2
0
def upload_room(host: str, port: int, room_name: str, room_attributes: dict,
                commands: List[Command]):
    """
    Upload a room to the server.
    Warning: This function is blocking, so when run from Blender the client dedicated to the user will be blocked and will accumulate lot of
    room updates that will be processed later.
    Todo: Write a non blocking version of this function to be used inside Blender, some kind of UploadClient that can exist side by side with BlenderClient.
    """
    with Client(host, port) as client:
        client.join_room(room_name)
        client.set_room_attributes(room_name, room_attributes)
        client.set_room_keep_open(room_name, True)

        for idx, c in enumerate(commands):
            logger.debug("Sending command %s (%d / %d)", c.type, idx,
                         len(commands))
            client.send_command(c)

            # The server will send back room update messages since the room is joined.
            # Consume them to avoid a client/server deadlock on broadcaster full send socket
            read_all_messages(client.socket, timeout=0.0)

        client.send_command(Command(MessageType.CONTENT))

        client.leave_room(room_name)
        if not client.wait(MessageType.LEAVE_ROOM):
            raise ClientDisconnectedException(
                "Client disconnected before the end of upload room")
Exemplo n.º 3
0
    def grab(self, host, port, room_name: str):
        with Client(host, port) as client:
            client.join_room(room_name)

            attempts_max = 20
            attempts = 0
            try:
                while attempts < attempts_max:
                    received_commands = client.fetch_incoming_commands()

                    attempts += 1
                    time.sleep(0.01)

                    for command in received_commands:
                        attempts = 0
                        if command.type <= MessageType.COMMAND:
                            continue
                        # Ignore command serial Id, that may not match
                        command.id = 0
                        self.streams.data[command.type].append(command.data)
            except ClientDisconnectedException:
                print("Grabber: disconnected before received command stream.",
                      file=sys.stderr)

            client.send_command(
                Command(MessageType.SET_ROOM_KEEP_OPEN,
                        encode_string(room_name) + encode_bool(False)))
            client.send_command(
                Command(MessageType.LEAVE_ROOM, room_name.encode("utf8")))

            if not client.wait(MessageType.LEAVE_ROOM):
                print("Grabber: disconnected before receiving LEAVE_ROOM.",
                      file=sys.stderr)
Exemplo n.º 4
0
    def test_connect(self):
        delay = self.delay
        server = self._server

        client1 = Client()
        delay()
        self.assertTrue(client1.is_connected())
        self.assertEqual(server.client_count(), (0, 1))

        client1.disconnect()
        delay()
        self.assertFalse(client1.is_connected())
        self.assertEqual(server.client_count(), (0, 0))

        #
        client2 = Client()
        delay()
        self.assertTrue(client2.is_connected())
        self.assertEqual(server.client_count(), (0, 1))

        client3 = Client()
        delay()
        self.assertTrue(client3.is_connected())
        self.assertEqual(server.client_count(), (0, 2))

        client2.disconnect()
        delay()
        self.assertFalse(client2.is_connected())
        self.assertTrue(client3.is_connected())
        self.assertEqual(server.client_count(), (0, 1))

        client2.disconnect()
        delay()
        self.assertFalse(client2.is_connected())
        self.assertTrue(client3.is_connected())
        self.assertEqual(server.client_count(), (0, 1))

        client3.disconnect()
        delay()
        self.assertFalse(client3.is_connected())
        self.assertEqual(server.client_count(), (0, 0))
Exemplo n.º 5
0
    def test_client_is_disconnected_when_server_process_is_killed(self):
        server_process = ServerProcess()
        server_process.start()

        with Client(server_process.host, server_process.port) as client:
            self.assertTrue(client.is_connected())
            client.fetch_commands()

            server_process.kill()

            self.assertRaises(common.ClientDisconnectedException,
                              client.fetch_commands)

            self.assertTrue(not client.is_connected())
Exemplo n.º 6
0
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
Exemplo n.º 7
0
    def grab(self, host, port, room_name: str):
        with Client(host, port) as client:
            client.join_room(room_name, "ignored", "ignored", True, True)

            attempts_max = 20
            attempts = 0
            try:
                while attempts < attempts_max:
                    received_commands = client.fetch_incoming_commands()

                    attempts += 1
                    time.sleep(0.01)

                    for command in received_commands:
                        attempts = 0
                        if command.type == MessageType.SEND_ERROR:
                            message = decode_string(command.data, 0)
                            raise RuntimeError(
                                f"Received error message {message}")
                        if command.type <= MessageType.COMMAND:
                            continue
                        # Ignore command serial Id, that may not match
                        command.id = 0
                        self.streams.commands[command.type].append(command)

            except ClientDisconnectedException:
                raise RuntimeError(
                    "Grabber: disconnected before received command stream.")

            client.send_command(
                Command(MessageType.SET_ROOM_KEEP_OPEN,
                        encode_string(room_name) + encode_bool(False)))
            client.send_command(
                Command(MessageType.LEAVE_ROOM, room_name.encode("utf8")))

            if not client.wait(MessageType.LEAVE_ROOM):
                raise RuntimeError(
                    "Grabber: disconnected before receiving LEAVE_ROOM.")

            count = sum(
                [len(commands) for commands in self.streams.commands.values()])
            assert count > 0, "No message grabbed"
Exemplo n.º 8
0
    def test_join_one_room_one_client(self):
        delay = self.delay
        server = self._server

        c0_name = "c0_name"
        c0_room = "c0_room"

        d0 = Delegate()
        c0 = Client()
        delay()
        self.assertEqual(server.client_count(), (0, 1))

        c0.set_client_attributes({common.ClientAttributes.USERNAME: c0_name})
        c0.join_room(c0_room)
        delay()
        network_consumer(c0, self._delegate)
        expected = (c0_name, c0_room)
        self.assertEqual(server.client_count(), (1, 0))
        self.assertEqual(len(d0.name_room), 1)
        self.assertIn(expected, d0.name_room)