Esempio n. 1
0
class BoardsController(object):

    def __init__(self):
        self.boards = {}
        self.message_handler = MessageHandler(self)
        self.message_parser = MessageParser(self.message_handler)

    def register(self, channel_id, handle):
        if channel_id in self.boards.keys():
            self.boards[channel_id].append(handle)
        else:
            self.boards[channel_id] = [handle]

    def unregister(self, handle, channel_id=None):
        if channel_id is None:
            for channel_id in self.boards:
                if handle in self.boards[channel_id]:
                    self.boards[channel_id].remove(handle)
                    self.message_handler.publish(Message("disconnect"),channel_id)
        else:
            self.boards[channel_id].remove(handle)
            self.message_handler.publish(Message("disconnect"),channel_id)

    def get_board_handles(self, channel_id):
        return self.boards.get(channel_id)


    def count_users(self, channel_id):
        if channel_id in self.boards.keys():
            return len(self.boards.get(channel_id))
        return 0

    def on_message(self, handle, message):
        self.message_parser.parse(handle, message)
class MessageHandlerTest(unittest.TestCase):

    def setUp(self):
        self.handle = MagicMock()
        self.message_handler = MagicMock()
        self.message_parser = MessageParser(self.message_handler)

    def test_parsing_register_message(self):
        self.message_parser.parse(self.handle, '{"type":"register", "args": {"channel_id":1}}')

        self.message_handler.handle.assert_called_with({"type":"register", "args": {"channel_id":1}},self.handle)

    def test_parsing_new_message(self):
        self.message_parser.parse(self.handle, '{"type":"new", "args": {}}')

        self.message_handler.handle.assert_called_with({"type":"new", "args": {}},self.handle)

    def test_parsing_postit_moved_message(self):
        self.message_parser.parse(self.handle, '{"type":"move", "args": {}}')
        self.message_handler.handle.assert_called_with({"type":"move", "args": {}},self.handle)

    def test_move_is_not_call_with_register_message(self):
        self.message_parser.parse(self.handle, '{"type":"register", "args": {"channel_id":1}}')

        assert self.message_handler.move.call_count == 0

    def test_register_message_with_arguments(self):
        self.message_parser.parse(self.handle, '{"type":"register", "args": {"channel_id":1}}')

        self.message_handler.register.assert_called_with(self.handle, 1)
Esempio n. 3
0
class BoardsController(object):

    # Generate a Boards instance
    def __init__(self):
        self.boards = {}
        self.message_handler = MessageHandler(self)
        self.message_parser = MessageParser(self.message_handler)

    def register(self, decoded_message, handle):
        channel_id = decoded_message['args']['channel_id']
        if channel_id in self.boards.keys():
            self.boards[channel_id][handle] = decoded_message['args']['user']
        else:
            self.boards[channel_id] = {}
            self.boards[channel_id][handle] = decoded_message['args']['user']

    # Remove a board to the Boards instances
    def unregister(self, handle, channel_id=None):
        if channel_id is None:
            for channel_id in self.boards:
                if handle in self.boards[channel_id]:
                    self.message_handler.publish(
                        Message("disconnect", self.boards[channel_id][handle]),
                        channel_id)
                    del self.boards[channel_id][handle]
        else:
            self.message_handler.publish(
                Message("disconnect", self.boards[channel_id][handle]),
                channel_id)
            del self.boards[channel_id][handle]

    # Get a specific board
    def get_board_handles(self, channel_id):
        return self.boards.get(channel_id)

    def get_board_users(self, channel_id):
        return self.boards.get(channel_id).values()

    # Get number of users that are in the board at the moment
    def count_users(self, channel_id):
        if channel_id in self.boards.keys():
            return len(self.boards.get(channel_id))
        return 0

    def on_message(self, handle, message):
        self.message_parser.parse(handle, message)
Esempio n. 4
0
class BoardsController(object):

    # Generate a Boards instance
    def __init__(self):
        self.boards = {}
        self.message_handler = MessageHandler(self)
        self.message_parser = MessageParser(self.message_handler)

    def register(self, decoded_message, handle):
        channel_id = decoded_message['args']['channel_id']
        if channel_id in self.boards.keys():
            self.boards[channel_id][handle] = decoded_message['args']['user']
        else:
            self.boards[channel_id] = {}
            self.boards[channel_id][handle] = decoded_message['args']['user']

    # Remove a board to the Boards instances
    def unregister(self, handle, channel_id=None):
        if channel_id is None:
            for channel_id in self.boards:
                if handle in self.boards[channel_id]:
                    self.message_handler.publish(Message("disconnect",self.boards[channel_id][handle]),channel_id)
                    del self.boards[channel_id][handle]
        else:
            self.message_handler.publish(Message("disconnect",self.boards[channel_id][handle]),channel_id)
            del self.boards[channel_id][handle]

    # Get a specific board
    def get_board_handles(self, channel_id):
        return self.boards.get(channel_id)

    def get_board_users(self, channel_id):
        return self.boards.get(channel_id).values()

    # Get number of users that are in the board at the moment
    def count_users(self, channel_id):
        if channel_id in self.boards.keys():
            return len(self.boards.get(channel_id))
        return 0

    def on_message(self, handle, message):
        self.message_parser.parse(handle, message)
Esempio n. 5
0
    def process_read_buffer(self):
        while self.recieved_data_buffer:

            msg_length = struct.unpack('>I', self.recieved_data_buffer[0:4])[0]
            packet_length = msg_length + 4

            # If the buffer doesn't contain the entirety of the packet then
            # break and wait until more data is read from socket
            if len(self.recieved_data_buffer) < packet_length:
                break

            message = MessageParser.parse(
                self.recieved_data_buffer[:packet_length],
                msg_length
            )

            self.recieved_data_buffer = self.recieved_data_buffer[packet_length:]
            if message.name != 'piece':
                print(message)
            else:
                print("We got a piece of our file!!!")
            # print('buffer is: ', self.recieved_data_buffer)

            # Msg ID 0
            if message.name == 'choke':
                self.is_choking = True

            # Msg ID 1
            if message.name == 'unchoke':
                self.is_choking = False

            # Msg ID 2
            if message.name == 'interested':
                # TODO: send unchoke message
                self.outbound_messages.append(
                    MessageParser.encode_msg('unchoke')
                )

            # Msg ID 3
            if message.name == 'not_interested':
                self.is_interested = False

            # Msg ID 4
            if message.name == 'have':
                have_piece = struct.unpack('>I', message.payload)[0]
                self.pieces[have_piece] = True

            # Msg ID 5
            if message.name == 'bitfield':
                self.pieces = BitArray(bytes=message.payload)
                self.outbound_messages.append(
                    MessageParser.encode_msg('interested')
                )

            # Msg ID 6
            if message.name == 'request':
                if not self.am_choking:
                    # Find piece from our downloaded torrent and send it back
                    pass

            # Msg ID 7
            if message.name == 'piece':
                self.inflight_requests -= 1
                index, begin = struct.unpack('>I I', message.payload[:8])
                print('Piece index: ', index)
                print(message.payload[:50])
Esempio n. 6
0
    def process_read_buffer(self):
        while self.recieved_data_buffer:

            msg_length = struct.unpack('>I', self.recieved_data_buffer[0:4])[0]
            packet_length = msg_length + 4

            # If the buffer doesn't contain the entirety of the packet then
            # break and wait until more data is read from socket
            if len(self.recieved_data_buffer) < packet_length:
                break

            message = MessageParser.parse(
                self.recieved_data_buffer[:packet_length], msg_length)

            self.recieved_data_buffer = self.recieved_data_buffer[
                packet_length:]
            if message.name != 'piece':
                print(message)
            else:
                print("We got a piece of our file!!!")
            # print('buffer is: ', self.recieved_data_buffer)

            # Msg ID 0
            if message.name == 'choke':
                self.is_choking = True

            # Msg ID 1
            if message.name == 'unchoke':
                self.is_choking = False

            # Msg ID 2
            if message.name == 'interested':
                # TODO: send unchoke message
                self.outbound_messages.append(
                    MessageParser.encode_msg('unchoke'))

            # Msg ID 3
            if message.name == 'not_interested':
                self.is_interested = False

            # Msg ID 4
            if message.name == 'have':
                have_piece = struct.unpack('>I', message.payload)[0]
                self.pieces[have_piece] = True

            # Msg ID 5
            if message.name == 'bitfield':
                self.pieces = BitArray(bytes=message.payload)
                self.outbound_messages.append(
                    MessageParser.encode_msg('interested'))

            # Msg ID 6
            if message.name == 'request':
                if not self.am_choking:
                    # Find piece from our downloaded torrent and send it back
                    pass

            # Msg ID 7
            if message.name == 'piece':
                self.inflight_requests -= 1
                index, begin = struct.unpack('>I I', message.payload[:8])
                print('Piece index: ', index)
                print(message.payload[:50])