Exemplo n.º 1
0
Arquivo: tests.py Projeto: jchmura/PiE
    def test_message_size(self):
        sock = TestSocketWrapper.SocketMock()
        wrapper = SocketWrapper(sock)
        data = 'some data'
        wrapper.send(data)

        self.assertEqual(MSG_SIZE, len(sock.payload))
Exemplo n.º 2
0
Arquivo: tests.py Projeto: jchmura/PiE
 def test_pass_through(self):
     sock = TestSocketWrapper.SocketMock()
     wrapper = SocketWrapper(sock)
     data = 'some data'
     wrapper.send(data)
     received = wrapper.receive()
     self.assertEqual(data, received)
Exemplo n.º 3
0
Arquivo: tests.py Projeto: jchmura/PiE
    def test_not_sent_all_logged(self):
        with patch.object(logging.Logger, 'warn', return_value=None) as log:
            sock = TestSocketWrapper.SocketMock()
            sock.send = Mock(return_value=MSG_SIZE - 1)
            wrapper = SocketWrapper(sock)
            wrapper.send('data')

            self.assertTrue(log.called)
Exemplo n.º 4
0
 def __init__(self, sign, sock, server):
     super().__init__(name='client {}'.format(sign))
     self._sign = sign
     self._sock = SocketWrapper(sock)
     self._server = server
     self._finished = False
     self._board_to_send = None
     self._logger = logging.getLogger(__name__ + str(sign))
Exemplo n.º 5
0
Arquivo: tests.py Projeto: jchmura/PiE
 def test_property(self):
     sock = object()
     wrapper = SocketWrapper(sock)
     self.assertIs(sock, wrapper.socket)
Exemplo n.º 6
0
 def _accept_user(self):
     client_socket, address = self._sock.accept()
     logger.info('Client connected from {}'.format(address))
     return SocketWrapper(client_socket)
Exemplo n.º 7
0
 def test_tic_tac_toe_server_game_sent(self):
     sock = SocketWrapper(SocketMock())
     tic_tac_toe = TicTacToe(sock)
     self.assertEqual(tic_tac_toe.server_game, sock.receive().payload)
Exemplo n.º 8
0
 def test_more_or_less_server_game_sent(self):
     sock = SocketWrapper(SocketMock())
     more_less = MoreOrLess(sock)
     self.assertEqual(more_less.server_game, sock.receive().payload)
Exemplo n.º 9
0
 def connect(self):
     self._sock = SocketWrapper(socket.socket())
     self._sock.socket.connect(
         (self._config.server_ip, self._config.server_port))
Exemplo n.º 10
0
class Client:
    def __init__(self):
        self._config = Configuration()
        self._sock = None
        self._board = Board()
        self._display = None
        self._logger = logging.getLogger(__name__)

    def connect(self):
        self._sock = SocketWrapper(socket.socket())
        self._sock.socket.connect(
            (self._config.server_ip, self._config.server_port))

    def run(self):
        sign = self.receive().payload()
        self._display = Display(sign, self._board, self)
        self._display.display_empty_grid()
        self._display.display_title('You are {}'.format(sign))
        self._logger = logging.getLogger(__name__ + str(sign))
        self._logger.debug('Connected to server')

        ready = self.receive().payload()
        if not ready:
            self._display.display_message('Waiting for opponent...')
            self._logger.debug('Waiting for opponent')

        ready = self.receive().payload()
        if not ready:
            self._logger.warn(
                'Now the server should be ready, something bad happened')
            return
        self._display.clear_message()
        self._logger.info('Everyone is connected, the game can start')

        while True:
            message = self.receive()
            if isinstance(message, BoardMessage):
                board = message.payload()
                self._logger.debug('Received new board')
                self._board.board = board
                self._display.fill_grid()
                self._logger.debug('New board displayed on the screen')
            elif isinstance(message, RoundMessage):
                round = message.payload()
                if round == sign:
                    self._logger.debug('It is my round')
                    self._display.display_message('This is your round')
                    self._display.input()
                    self._logger.info(
                        'Move is made, sending new board to the server\n{}'.
                        format(self._board))
                    self.send(BoardMessage(self._board.board))
                else:
                    self._logger.debug("It is my opponent's round")
                    self._display.display_message(
                        "This is your opponent's round")
            elif isinstance(message, EndMessage):
                self._logger.info('The game has ended')
                tie = message.payload()
                if tie:
                    self._display.display_message("Is's a tie")
                    self._logger.debug("It's a tie")
                else:
                    winner = self._board.won(sign)
                    if winner:
                        self._display.display_message('You won!')
                    else:
                        self._display.display_message('You lost')
                    self._logger.debug('Have I won? {}'.format(winner))
                self._display.quit()
                break
            else:
                self._logger.warn('Unknown message: {}'.format(message))

    def receive(self):
        return self._sock.receive()

    def send(self, message):
        self._sock.send(message)

    def disconnect(self):
        if self._sock is not None:
            self._sock.socket.close()
            self._sock = None
            self._logger.info('Socket closed')
        else:
            self._logger.debug('Socket is not open, no need to close it')

        if self._display is not None:
            self._display.exit()
Exemplo n.º 11
0
class ClientConnection(Thread):
    def __init__(self, sign, sock, server):
        super().__init__(name='client {}'.format(sign))
        self._sign = sign
        self._sock = SocketWrapper(sock)
        self._server = server
        self._finished = False
        self._board_to_send = None
        self._logger = logging.getLogger(__name__ + str(sign))

    def finish(self):
        self._finished = True

    def send_board(self, board):
        self._board_to_send = board

    def _wait_for_board(self):
        while self._board_to_send is None:
            if self._finished:
                raise GameFinished
            time.sleep(.1)
        board = self._board_to_send
        self._board_to_send = None
        return board

    def run(self):
        self._sock.send(message.SignMessage(self._sign))
        self._sock.send(message.ReadyMessage(self._server.full))
        while not self._server.full:
            time.sleep(.1)
        self._sock.send(message.ReadyMessage(True))
        self._logger.debug('The game is ready, player have been informed')

        self._logger.info('Waiting for the first board')
        board = self._wait_for_board()
        self._logger.debug('Sending the first board to the player')
        self._sock.send(message.BoardMessage(board))

        try:
            while True:
                self._round()
        except GameFinished:
            self._logger.info('The game have finished, sending last board')
            self._sock.send(message.BoardMessage(self._server.board.board))
            self._logger.info('Sending end message')
            self._sock.send(message.EndMessage(self._server.board.tie()))
            self._sock.socket.close()

    def _round(self):
        round_sign = self._server.round_sign
        self._sock.send(message.RoundMessage(round_sign))
        if round_sign == self._sign:
            self._logger.debug(
                "It is my player's round, waiting for the new board")
            board = self._sock.receive().payload()
            self._logger.debug('Board received')
            self._server.board = board

        self._logger.info('Waiting for the board for this round')
        board = self._wait_for_board()
        self._logger.debug('Sending the new board to the player')
        self._sock.send(message.BoardMessage(board))
Exemplo n.º 12
0
 def connect(self):
     self._sock = SocketWrapper(socket.socket())
     self._sock.socket.connect((self._config.server_ip, self._config.server_port))
Exemplo n.º 13
0
class Client:
    def __init__(self):
        self._config = Configuration()
        self._sock = None
        self._board = Board()
        self._display = None
        self._logger = logging.getLogger(__name__)

    def connect(self):
        self._sock = SocketWrapper(socket.socket())
        self._sock.socket.connect((self._config.server_ip, self._config.server_port))

    def run(self):
        sign = self.receive().payload()
        self._display = Display(sign, self._board, self)
        self._display.display_empty_grid()
        self._display.display_title('You are {}'.format(sign))
        self._logger = logging.getLogger(__name__ + str(sign))
        self._logger.debug('Connected to server')

        ready = self.receive().payload()
        if not ready:
            self._display.display_message('Waiting for opponent...')
            self._logger.debug('Waiting for opponent')

        ready = self.receive().payload()
        if not ready:
            self._logger.warn('Now the server should be ready, something bad happened')
            return
        self._display.clear_message()
        self._logger.info('Everyone is connected, the game can start')

        while True:
            message = self.receive()
            if isinstance(message, BoardMessage):
                board = message.payload()
                self._logger.debug('Received new board')
                self._board.board = board
                self._display.fill_grid()
                self._logger.debug('New board displayed on the screen')
            elif isinstance(message, RoundMessage):
                round = message.payload()
                if round == sign:
                    self._logger.debug('It is my round')
                    self._display.display_message('This is your round')
                    self._display.input()
                    self._logger.info('Move is made, sending new board to the server\n{}'.format(self._board))
                    self.send(BoardMessage(self._board.board))
                else:
                    self._logger.debug("It is my opponent's round")
                    self._display.display_message("This is your opponent's round")
            elif isinstance(message, EndMessage):
                self._logger.info('The game has ended')
                tie = message.payload()
                if tie:
                    self._display.display_message("Is's a tie")
                    self._logger.debug("It's a tie")
                else:
                    winner = self._board.won(sign)
                    if winner:
                        self._display.display_message('You won!')
                    else:
                        self._display.display_message('You lost')
                    self._logger.debug('Have I won? {}'.format(winner))
                self._display.quit()
                break
            else:
                self._logger.warn('Unknown message: {}'.format(message))

    def receive(self):
        return self._sock.receive()

    def send(self, message):
        self._sock.send(message)

    def disconnect(self):
        if self._sock is not None:
            self._sock.socket.close()
            self._sock = None
            self._logger.info('Socket closed')
        else:
            self._logger.debug('Socket is not open, no need to close it')

        if self._display is not None:
            self._display.exit()
Exemplo n.º 14
0
 def __init__(self, server: SocketWrapper):
     self._server = server
     server.send(GameChooser(self.server_game))