Exemplo n.º 1
0
 def join_game(self):
     PM = PokerMessage
     MT = MessageTypes
     PCT = PlayerControlTypes
     PCF = PlayerControlFields
     PCRF = PlayerControlResponseFields
     success = False
     sock = self.open_socket(self.launcher_port)
     message = {}
     message[PM.MESSAGE_TYPE] = MT.PLAYER_CONTROL
     player_control = {}
     player_control[PCF.CONTROL_TYPE] = PCT.JOIN
     player_control[PCF.CHIPS] = self.chips
     player_control[PCF.PLAYER_ID] = self.player_id
     player_control[PCF.BCC_ID] = "test"
     player_control[PCF.TABLE_ID] = "table_0"
     message[MT.PLAYER_CONTROL] = player_control
     NetMessage.send_message(sock, [message])
     response = NetMessage.get_message(sock)
     for message in response:
         if message[PM.MESSAGE_TYPE] == MT.PLAYER_CONTROL_RESPONSE:
             join_response = message[MT.PLAYER_CONTROL_RESPONSE]
             assert join_response[PCRF.CONTROL_TYPE] == PCT.JOIN
             if join_response[PCRF.SUCCESS] is True:
                 self.game_port = join_response[PCRF.PORT]
                 success = True
             else:
                 print(join_response[PCRF.ERROR])
                 print("failed to join game")
                 success = False
     sock.close()
     return success
Exemplo n.º 2
0
 def listen(self):
     """Listen for messages from users and pass them on to the controller"""
     ET = ErrorTypes
     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     s.settimeout(5)
     while self.port is None:
         try:
             host, port = self.port_manager.get_next_port()
             s.bind((host, port))
             self.port = port
         except:
             pass
     s.listen(20)
     while not self.done:
         try:
             client_socket, address = s.accept()
             messages = NetMessage.get_message(client_socket)
             for message in messages:
                 replies = []
                 try:
                     replies = self.controller.handle_message(message)
                 except:
                     text = "Uncaught exception"
                     error_type = ET.UNCAUGHT_EXCEPTION
                     error_message = GameError(error_type, text,
                                               traceback.format_exc())
                     replies = [error_message.make_message()]
                 NetMessage.send_message(client_socket, replies)
             client_socket.close()
         except socket.timeout:
             pass
     s.close()
Exemplo n.º 3
0
 def quit_table(self):
     PM = PokerMessage
     MT = MessageTypes
     PCT = PlayerControlTypes
     PCF = PlayerControlFields
     PCRF = PlayerControlResponseFields
     sock = self.open_socket(self.launcher_port)
     success = False
     message = {}
     message[PM.MESSAGE_TYPE] = MT.PLAYER_CONTROL
     player_control = {}
     player_control[PCF.CONTROL_TYPE] = PCT.QUIT
     player_control[PCF.PLAYER_ID] = self.player_id
     player_control[PCF.TABLE_ID] = "table_0"
     message[MT.PLAYER_CONTROL] = player_control
     NetMessage.send_message(sock, [message])
     responses = NetMessage.get_message(sock)
     for message in responses:
         assert message[PM.MESSAGE_TYPE] == MT.PLAYER_CONTROL_RESPONSE
         leave_response = message[MT.PLAYER_CONTROL_RESPONSE]
         assert leave_response[PCRF.CONTROL_TYPE] == PCT.LEAVE
         if leave_response[PCRF.SUCCESS] is True:
             success = True
         else:
             print(leave_response[PCRF.ERROR])
             print("failed to leave table")
             success = False
     sock.close()
     return success
Exemplo n.º 4
0
 def toggle_running(self):
     """Create or destroy the Table Manager on the host"""
     MM = ManagerMessage
     MT = MM.MessageTypes
     TMR = MM.ToggleManagerRunning
     TMRF = MM.ToggleManagerRunningFields
     host = self.host_handle.ip_address
     port = self.manager_port
     toggle_running_message = {}
     toggle_running_message[MM.MESSAGE_TYPE] = MT.TOGGLE_MANAGER_RUNNING
     toggle_running = {}
     status = self.status()
     if status == "Running" or status == "Stopping":
         toggle_running[TMR.ACTION] = TMRF.STOP
     elif status == "Created":
         toggle_running[TMR.ACTION] = TMRF.START
     elif status == "Not Created":
         error = "Connect start or stop manager. It has not been created"
         raise ValueError(error)
     toggle_running_message[MT.TOGGLE_MANAGER_RUNNING] = toggle_running
     sock = NetMessage.open_socket(host, port)
     NetMessage.send_message(sock, [toggle_running_message])
     response = NetMessage.get_message(sock)
     sock.close()
     for message in response:
         if message[MM.MESSAGE_TYPE] == MT.TOGGLE_MANAGER_RUNNING:
             toggle_response = message[MT.TOGGLE_MANAGER_RUNNING]
             if not toggle_response[TMR.SUCCESS]:
                 raise ValueError(toggle_response[TMR.ERROR])
Exemplo n.º 5
0
 def status(self):
     """Return whether the manager is responding"""
     MM = ManagerMessage
     MT = MM.MessageTypes
     MI = MM.ManagerInfo
     MIF = MM.ManagerInfoFields
     host = self.host_handle.ip_address
     port = self.manager_port
     status_request_message = {}
     status_request_message[MM.MESSAGE_TYPE] = MT.MANAGER_INFO
     status_request = {}
     status_request[MI.REQUEST] = MIF.STATUS_CHECK
     status_request_message[MT.MANAGER_INFO] = status_request
     sock = None
     try:
         sock = NetMessage.open_socket(host, port)
         NetMessage.send_message(sock, [status_request_message])
         response = NetMessage.get_message(sock)
     except:
         response = []
     finally:
         if sock is not None:
             sock.close()
     if response:
         for message in response:
             if message[MM.MESSAGE_TYPE] == MT.MANAGER_INFO:
                 status_response = message[MT.MANAGER_INFO]
                 status = status_response[MIF.STATUS]
                 if status == MIF.RUNNING:
                     return "Running"
                 elif status == MIF.STOPPING:
                     return "Stopping"
                 elif status == MIF.STOPPED:
                     return "Created"
     return "Not Created"
Exemplo n.º 6
0
    def send_action(self, action_type, action_amount=None):
        """Attempt to send a player's action

        Arguments:
        action_type -- the type of action to be sent
        action_amount -- the amount associated with the action [default: None]

        """
        PM = PokerMessage
        MT = MessageTypes
        AF = ActionFields
        message = {}
        message[PM.MESSAGE_TYPE] = MT.PLAYER_ACTION
        action = {}
        action[AF.BCC_ID] = self.bcc_id
        action[AF.ACTION_TYPE] = action_type
        if action_amount is not None:
            action[AF.ACTION_AMOUNT] = action_amount
        message[MT.PLAYER_ACTION] = action
        try:
            sock = NetMessage.open_socket(self.game_host, self.game_port)
            NetMessage.send_message(sock, [message])
            responses = NetMessage.get_message(sock)
            return responses[len(responses) - 1]
        except:
            logging.error('An error occurred due to an action request. %s',
                          traceback.format_exc())
            return {'errors':
                    'An error occurred making your action'}
        finally:
            sock.close()
Exemplo n.º 7
0
 def status(self):
     """Return whether the host is up"""
     MM = ManagerMessage
     MT = MM.MessageTypes
     HS = MM.HostStatus
     HSF = MM.HostStatusFields
     host = self.ip_address
     port = self.port
     status_request_message = {}
     status_request_message[MM.MESSAGE_TYPE] = MT.HOST_STATUS
     status_request = {}
     status_request[HS.ACTION] = HSF.CHECK_STATUS
     status_request_message[MT.HOST_STATUS] = status_request
     sock = None
     try:
         sock = NetMessage.open_socket(host, port)
         NetMessage.send_message(sock, [status_request_message])
         response = NetMessage.get_message(sock)
     except:
         response = []
     finally:
         if sock is not None:
             sock.close()
     if response:
         return "Alive"
     else:
         return "Dead"
Exemplo n.º 8
0
 def add_table(self, table_id, table_size, big_blind, little_blind):
     """Instruct the remote table manager to create a new table"""
     MM = ManagerMessage
     MT = MM.MessageTypes
     TTC = MM.ToggleTableCreated
     TTCF = MM.ToggleTableCreatedFields
     TF = MM.TableFields
     host = self.host_handle.ip_address
     port = self.manager_port
     toggle_created_message = {}
     toggle_created_message[MM.MESSAGE_TYPE] = MT.TOGGLE_TABLE_CREATED
     toggle_created = {}
     toggle_created[TTC.ACTION] = TTCF.CREATE
     table_info = {}
     table_info[TF.ID] = table_id
     table_info[TF.SIZE] = table_size
     table_info[TF.BIG_BLIND] = big_blind
     table_info[TF.LITTLE_BLIND] = little_blind
     toggle_created[TTC.TABLE_INFO] = table_info
     toggle_created_message[MT.TOGGLE_TABLE_CREATED] = toggle_created
     sock = NetMessage.open_socket(host, port)
     NetMessage.send_message(sock, [toggle_created_message])
     response = NetMessage.get_message(sock)
     sock.close()
     for message in response:
         if message[MM.MESSAGE_TYPE] == MT.TOGGLE_TABLE_CREATED:
             toggle_response = message[MT.TOGGLE_TABLE_CREATED]
             if not toggle_response[TTC.SUCCESS]:
                 raise ValueError(toggle_response[TTC.ERROR])
Exemplo n.º 9
0
 def shutdown(self):
     """Tell the host daemon to shut down"""
     MM = ManagerMessage
     MT = MM.MessageTypes
     host = self.ip_address
     port = self.port
     shutdown_request_message = {}
     shutdown_request_message[MM.MESSAGE_TYPE] = MT.SHUT_DOWN
     sock = NetMessage.open_socket(host, port)
     NetMessage.send_message(sock, [shutdown_request_message])
     NetMessage.get_message(sock)
     sock.close()
Exemplo n.º 10
0
    def quit_table(self):
        """Attempt to quit the table

        If successful, return True and None. Otherwise, return False and an
        error message

        """
        PM = PokerMessage
        MT = MessageTypes
        PCT = PlayerControlTypes
        PCF = PlayerControlFields
        PCRF = PlayerControlResponseFields
        success = False
        message = {}
        message[PM.MESSAGE_TYPE] = MT.PLAYER_CONTROL
        player_control = {}
        player_control[PCF.CONTROL_TYPE] = PCT.QUIT
#        player_control[PCF.PLAYER_ID] = self.player_id
        player_control[PCF.BCC_ID] = self.bcc_id
        player_control[PCF.TABLE_ID] = self.table_id
        message[MT.PLAYER_CONTROL] = player_control
        try:
            sock = NetMessage.open_socket(self.manager_host, self.manager_port)
            NetMessage.send_message(sock, [message])
            responses = NetMessage.get_message(sock)
            error_message = None
        except:
            success = False
            error_message = "Could not reach the table"
            responses = []
            logging.error('An error occurred communicating with the table ' +
                          'manager. Stacktrace: %s', traceback.format_exc())
        finally:
            sock.close()
        try:
            for message in responses:
                assert(message[PM.MESSAGE_TYPE] == MT.PLAYER_CONTROL_RESPONSE)
                leave_response = message[MT.PLAYER_CONTROL_RESPONSE]
                assert(leave_response[PCRF.CONTROL_TYPE] == PCT.QUIT)
                #if leave_response[PCRF.SUCCESS] is True:
                success = True
                #else:
                #    error_message = 'failed to leave table'
                #    success = False

        except:
            success = False
            error_message = "The table sent an expected response"
            logging.error('An error occurred processing the table manager\'s' +
                          'response to a quit request. Response: %s\n' +
                          'stacktrace: %s', str(responses),
                          traceback.format_exc())
        return success, error_message
Exemplo n.º 11
0
 def send_update_request(self):
     PM = PokerMessage
     MT = MessageTypes
     PURF = PlayerUpdateRequestFields
     sock = self.open_socket(self.game_port)
     message = {}
     message[PM.MESSAGE_TYPE] = MT.PLAYER_UPDATE_REQUEST
     update_request = {}
     update_request[PURF.PLAYER_ID] = self.player_id
     message[MT.PLAYER_UPDATE_REQUEST] = update_request
     NetMessage.send_message(sock, [message])
     responses = NetMessage.get_message(sock)
     return responses[0]
Exemplo n.º 12
0
def get_all_poker_tables(big_blinds):
    """Return a list of the available poker tables

    Arguments:
    big_blinds -- a list of integers representing the acceptable big blinds

    """
    PM = PokerMessage
    MT = MessageTypes
    TIF = TableInfoFields
    manager_host = hosts.TABLE_MANAGER_HOST
    manager_port = hosts.TABLE_MANAGER_PORT
    info_request_message = {}
    info_request_message[PM.MESSAGE_TYPE] = MT.TABLE_INFO_REQUEST
    info_request = {}
    info_request[TIF.BIG_BLINDS] = big_blinds
    # TODO: pagination
    info_request_message[MT.TABLE_INFO_REQUEST] = info_request
    sock = None
    try:
        sock = NetMessage.open_socket(manager_host, manager_port)
        NetMessage.send_message(sock, [info_request_message])
        response = NetMessage.get_message(sock)
    except:
        logging.error('An error occurred communicating with the table ' +
                      'manager. Stacktrace: %s', traceback.format_exc())
        response = []
    finally:
        if sock is not None:
            sock.close()
    tables = []
    try:
        for message in response:
            if (message[PM.MESSAGE_TYPE] == MT.TABLE_INFO_REQUEST):
                info_response = message[MT.TABLE_INFO_REQUEST]
                tables_info = info_response[TIF.TABLES_INFO]
                for table_info in tables_info:
                    table_id = table_info[TIF.TABLE_ID]
                    table_desc = table_info[TIF.DESCRIPTION]
                    suggested_chips = table_info[TIF.SUGGESTED_CHIPS]
                    #big_blind = table_info[TIF.BIG_BLIND]
                    num_users = table_info[TIF.NUM_USERS]
                    table = (table_id, table_desc, suggested_chips, num_users)
                    tables.append(table)
    except:
        logging.error('An error occurred processing the table manager\'s ' +
                      'response to a table info request. Response: %s\n' +
                      'stacktrack: %s', str(response), traceback.format_exc())
    return tables
Exemplo n.º 13
0
    def listen_for_user(self, s):
        """Listen for user requests

        Arguments:
        s -- the socket on which to listen for user requests

        """
        PM = PokerMessage
        MT = MessageTypes
        PCF = PlayerControlFields
        PCT = PlayerControlTypes
        ET = ErrorTypes
        conn, addr = s.accept()
        messages = NetMessage.get_message(conn)
        responses = []
        for message in messages:
            if message[PM.MESSAGE_TYPE] == MT.PLAYER_CONTROL:
                player_control = message[MT.PLAYER_CONTROL]
                if player_control[PCF.CONTROL_TYPE] == PCT.JOIN:
                    response = self.handle_join_request(player_control)
                    responses.append(response)
                elif player_control[PCF.CONTROL_TYPE] == PCT.QUIT:
                    response = self.handle_quit_request(player_control)
                    responses.append(response)
                elif player_control[PCF.CONTROL_TYPE] == PCT.STATUS:
                    response = self.handle_player_status_request(player_control)
                    responses.append(response)
                else:
                    bcc_id = player_control[PCF.BCC_ID]
                    text = "Got unexpected control type: "
                    text = text + player_control[PCF.CONTROL_TYPE]
                    error_type = ET.UNEXPECTED_CONTROL_TYPE
                    error = PlayerError(bcc_id=bcc_id, text=text, error_type=error_type)
                    response = error.make_message()
                    responses.append(response)
            elif message[PM.MESSAGE_TYPE] == MT.TABLE_INFO_REQUEST:
                info_request = message[MT.TABLE_INFO_REQUEST]
                response = self.handle_table_info_request(info_request)
                responses.append(response)
            else:
                text = "Got an unexpected message type: "
                text = text + message[PM.MESSAGE_TYPE]
                error_type = ET.UNEXPECTED_MESSAGE
                error = PlayerError(text=text, error_type=error_type)
                response = error.make_message()
                responses.append(response)
        NetMessage.send_message(conn, responses)
        conn.close()
Exemplo n.º 14
0
def check_for_valid_user_at_tables(bcc_id):
    """Find out if a user is already joined to a table in the backend

    Arguments:
    bcc_id -- the unique id of the player to search for

    """
    PM = PokerMessage
    MT = MessageTypes
    PCT = PlayerControlTypes
    PCF = PlayerControlFields
    PCRF = PlayerControlResponseFields
    table_id = None
    port = None
    manager_host = hosts.TABLE_MANAGER_HOST
    manager_port = hosts.TABLE_MANAGER_PORT
    check_message = {}
    check_message[PM.MESSAGE_TYPE] = MT.PLAYER_CONTROL
    status_check = {}
    status_check[PCF.CONTROL_TYPE] = PCT.STATUS
    status_check[PCF.BCC_ID] = bcc_id
    check_message[MT.PLAYER_CONTROL] = status_check
    sock = None
    try:
        sock = NetMessage.open_socket(manager_host, manager_port)
        NetMessage.send_message(sock, [check_message])
        response = NetMessage.get_message(sock)
    except:
        logging.error('An error occurred communicating with the table ' +
                      'manager. Stacktrace: %s', traceback.format_exc())
        response = []
    finally:
        if sock is not None:
            sock.close()
    try:
        for message in response:
            if (message[PM.MESSAGE_TYPE] == MT.PLAYER_CONTROL_RESPONSE):
                check_response = message[MT.PLAYER_CONTROL_RESPONSE]
                assert(check_response[PCRF.CONTROL_TYPE] == PCT.STATUS)
                if check_response[PCRF.VALID_USER] is True:
                    table_id = check_response[PCRF.TABLE_ID]
                    port = check_response[PCRF.PORT]
    except:
        logging.error('An error occurred processing the table manager\'s ' +
                      'response to a status check. Response: %s\n' +
                      'stacktrace: %s', str(response), traceback.format_exc())
    return table_id, port
Exemplo n.º 15
0
 def send_action(self, action_type, action_amount=None):
     PM = PokerMessage
     MT = MessageTypes
     AF = ActionFields
     sock = self.open_socket(self.game_port)
     message = {}
     message[PM.MESSAGE_TYPE] = MT.PLAYER_ACTION
     action = {}
     action[AF.PLAYER_ID] = self.player_id
     action[AF.ACTION_TYPE] = action_type
     if action_amount is not None:
         action[AF.ACTION_AMOUNT] = action_amount
     message[MT.PLAYER_ACTION] = action
     NetMessage.send_message(sock, [message])
     responses = NetMessage.get_message(sock)
     #        self.process_responses(responses)
     return responses[0]
Exemplo n.º 16
0
 def get_tables(self):
     """Return a list of the tables and their information"""
     MM = ManagerMessage
     MT = MM.MessageTypes
     MI = MM.ManagerInfo
     MIF = MM.ManagerInfoFields
     TF = MM.TableFields
     host = self.host_handle.ip_address
     port = self.manager_port
     info_request_message = {}
     info_request_message[MM.MESSAGE_TYPE] = MT.MANAGER_INFO
     info_request = {}
     info_request[MI.REQUEST] = MIF.GET_TABLES
     info_request_message[MT.MANAGER_INFO] = info_request
     tables = []
     sock = NetMessage.open_socket(host, port)
     NetMessage.send_message(sock, [info_request_message])
     response = NetMessage.get_message(sock)
     sock.close()
     for message in response:
         if message[MM.MESSAGE_TYPE] == MT.MANAGER_INFO:
             info_response = message[MT.MANAGER_INFO]
             for table_info in info_response[MIF.TABLES]:
                 table_id = table_info[TF.ID]
                 status = table_info[TF.STATUS]
                 big = table_info[TF.BIG_BLIND]
                 little = table_info[TF.LITTLE_BLIND]
                 size = table_info[TF.SIZE]
                 num_users = table_info[TF.NUM_USERS]
                 host = table_info[TF.HOST]
                 port = table_info[TF.PORT]
                 if status == TF.RUNNING or status == TF.WAITING:
                     toggle_action = "Stop"
                 elif status == TF.STOPPING:
                     toggle_action = "Cancel stop"
                 else:
                     toggle_action = "Start"
                 table = (table_id, status, big, little, size,
                          num_users, host, port, toggle_action)
                 tables.append(table)
     return tables
Exemplo n.º 17
0
 def toggle_table_running(self, table_id):
     """Make table manager start or stop the specified table"""
     MM = ManagerMessage
     MT = MM.MessageTypes
     TTR = MM.ToggleTableRunning
     host = self.host_handle.ip_address
     port = self.manager_port
     toggle_running_message = {}
     toggle_running_message[MM.MESSAGE_TYPE] = MT.TOGGLE_TABLE_RUNNING
     toggle_running = {}
     toggle_running[TTR.ID] = table_id
     toggle_running_message[MT.TOGGLE_TABLE_RUNNING] = toggle_running
     sock = NetMessage.open_socket(host, port)
     NetMessage.send_message(sock, [toggle_running_message])
     response = NetMessage.get_message(sock)
     sock.close()
     for message in response:
         if message[MM.MESSAGE_TYPE] == MT.TOGGLE_TABLE_RUNNING:
             toggle_response = message[MT.TOGGLE_TABLE_RUNNING]
             if not toggle_response[TTR.SUCCESS]:
                 raise ValueError(toggle_response[TTR.ERROR])
Exemplo n.º 18
0
 def num_tables(self):
     """Return the number of tables managed by the manager"""
     MM = ManagerMessage
     MT = MM.MessageTypes
     MI = MM.ManagerInfo
     MIF = MM.ManagerInfoFields
     host = self.host_handle.ip_address
     port = self.manager_port
     info_request_message = {}
     info_request_message[MM.MESSAGE_TYPE] = MT.MANAGER_INFO
     info_request = {}
     info_request[MI.REQUEST] = MIF.NUM_TABLES
     info_request_message[MT.MANAGER_INFO] = info_request
     sock = NetMessage.open_socket(host, port)
     NetMessage.send_message(sock, [info_request_message])
     response = NetMessage.get_message(sock)
     sock.close()
     for message in response:
         if message[MM.MESSAGE_TYPE] == MT.MANAGER_INFO:
             info_response = message[MT.MANAGER_INFO]
             return info_response[MIF.NUM_TABLES]
Exemplo n.º 19
0
    def listen_for_command(self, s):
        """Read messages from the socket and handle them

        Arguments:
        s -- the socket on which to read messages

        """
        MM = ManagerMessage
        MT = MM.MessageTypes
        conn, addr = s.accept()
        messages = NetMessage.get_message(conn)
        responses = []
        for message in messages:
            if message[MM.MESSAGE_TYPE] == MT.HOST_STATUS:
                responses = self.handle_status_request(message)
            elif message[MM.MESSAGE_TYPE] == MT.SHUT_DOWN:
                responses = self.handle_shutdown_request(message)
            elif message[MM.MESSAGE_TYPE] == MT.TOGGLE_MANAGER_CREATED:
                responses = self.handle_toggle_created_message(message)
        NetMessage.send_message(conn, responses)
        conn.close()
Exemplo n.º 20
0
 def send_update_request(self):
     """Send an update request to the game and return the response"""
     PM = PokerMessage
     MT = MessageTypes
     PURF = PlayerUpdateRequestFields
     message = {}
     message[PM.MESSAGE_TYPE] = MT.PLAYER_UPDATE_REQUEST
     update_request = {}
     update_request[PURF.BCC_ID] = self.bcc_id
     message[MT.PLAYER_UPDATE_REQUEST] = update_request
     try:
         sock = NetMessage.open_socket(self.game_host, self.game_port)
         NetMessage.send_message(sock, [message])
         responses = NetMessage.get_message(sock)
         return responses[len(responses) - 1]
     except:
         logging.error('An error occurred due to an update request. %s',
                       traceback.format_exc())
         return {'errors':
                 'An error occurred requesting an update of the table'}
     finally:
         sock.close()
Exemplo n.º 21
0
 def toggle_created(self):
     """Create or destroy the Table Manager on the host"""
     MM = ManagerMessage
     MT = MM.MessageTypes
     TMC = MM.ToggleManagerCreated
     TMCF = MM.ToggleManagerCreatedFields
     host = self.host_handle.ip_address
     toggle_created_message = {}
     toggle_created_message[MM.MESSAGE_TYPE] = MT.TOGGLE_MANAGER_CREATED
     toggle_created = {}
     toggle_created[TMC.USER_PORT] = self.user_port
     toggle_created[TMC.MANAGER_PORT] = self.manager_port
     toggle_created[TMC.TYPE] = self.table_type
     status = self.status()
     if status == "Running":
         raise ValueError('Cannot remove a running manager')
     elif status == "Created":
         toggle_created[TMC.ACTION] = TMCF.REMOVE
         port = self.manager_port
     elif status == "Not Created":
         toggle_created[TMC.ACTION] = TMCF.CREATE
         port = self.host_handle.port
     toggle_created_message[MT.TOGGLE_MANAGER_CREATED] = toggle_created
     sock = NetMessage.open_socket(host, port)
     NetMessage.send_message(sock, [toggle_created_message])
     response = NetMessage.get_message(sock)
     sock.close()
     if response is not None:
         for message in response:
             if message[MM.MESSAGE_TYPE] == MT.TOGGLE_MANAGER_CREATED:
                 toggle_response = message[MT.TOGGLE_MANAGER_CREATED]
                 success = toggle_response[TMC.SUCCESS]
                 if not success:
                     raise ValueError(toggle_response[TMC.ERROR])
     else:
         raise ValueError("Table could not be contacted")
Exemplo n.º 22
0
    def join_table(self):
        """Attempt to join the table

        If the join is successful, return True, None.
        Otherwise, return False and an error message

        """
        PM = PokerMessage
        MT = MessageTypes
        PCT = PlayerControlTypes
        PCF = PlayerControlFields
        PCRF = PlayerControlResponseFields
        success = False
        join_request = {}
        join_request[PM.MESSAGE_TYPE] = MT.PLAYER_CONTROL
        join_message = {}
        join_message[PCF.CONTROL_TYPE] = PCT.JOIN
        join_message[PCF.CHIPS] = self.chips
        join_message[PCF.PLAYER_ID] = self.player_id
        join_message[PCF.BCC_ID] = self.bcc_id
        join_message[PCF.TABLE_ID] = self.table_id
        join_request[MT.PLAYER_CONTROL] = join_message
        try:
            sock = NetMessage.open_socket(self.manager_host, self.manager_port)
            NetMessage.send_message(sock, [join_request])
            response = NetMessage.get_message(sock)
            error_message = None
        except:
            success = False
            error_message = "Could not reach the table"
            response = []
            logging.error('An error occurred communicating with the table ' +
                          'manager. Stacktrace: %s', traceback.format_exc())
        finally:
            sock.close()
        try:
            for message in response:
                if (message[PM.MESSAGE_TYPE] == MT.PLAYER_CONTROL_RESPONSE):
                    join_response = message[MT.PLAYER_CONTROL_RESPONSE]
                    assert(join_response[PCRF.CONTROL_TYPE] == PCT.JOIN)
                    if join_response[PCRF.SUCCESS] is True:
                        self.game_port = join_response[PCRF.PORT]
                        self.game_host = join_response[PCRF.HOST]
                        self.num_seats = join_response[PCRF.NUM_SEATS]
                        success = True
                    else:
                        success = False
                        error_message = join_response[PCRF.ERROR]
                else:
                    logging.error('The table manager sent back an ' +
                                  'inappropriate response. Message: %s',
                                  str(message))
                    success = False
                    error_message = "The table sent an unexpected response"
        except:
            success = False
            error_message = "The table sent a malformed response"
            logging.error('An error occurred processing the table manager\'s' +
                          ' response to a join request. Response: %s\n' +
                          'stacktrace: %s', str(response),
                          traceback.format_exc())
        return success, error_message
Exemplo n.º 23
0
 def listen(self):
     PM = PokerMessage
     MT = MessageTypes
     PCF = PlayerControlFields
     PCT = PlayerControlTypes
     PCRF = PlayerControlResponseFields
     ET = ErrorTypes
     self.controller1.start()
     HOST = ''
     PORT = 2423
     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     s.bind((HOST, PORT))
     s.listen(50)
     while len(self.users) < 5:
         conn, addr = s.accept()
         messages = NetMessage.get_message(conn)
         responses = []
         for message in messages:
             if message[PM.MESSAGE_TYPE] == MT.PLAYER_CONTROL:
                 player_control = message[MT.PLAYER_CONTROL]
                 if player_control[PCF.CONTROL_TYPE] == PCT.JOIN:
                     chips = player_control[PCF.CHIPS]
                     player_id = player_control[PCF.PLAYER_ID]
                     newUser = NetUser(player_id, chips)
                     self.users.append(newUser)
                     response = {}
                     response[PM.MESSAGE_TYPE] = MT.PLAYER_CONTROL_RESPONSE
                     join_success = {}
                     join_success[PCRF.CONTROL_TYPE] = PCT.JOIN
                     join_success[PCRF.PLAYER_ID] = player_id
                     join_success[PCRF.SUCCESS] = True
                     join_success[PCRF.PORT] = 2424
                     response[MT.PLAYER_CONTROL_RESPONSE] = join_success
                     responses.append(response)
                 else:
                     player_id = player_control[PCF.PLAYER_ID]
                     text = "Expected JOIN message, got "
                     text = text + player_control[PCF.CONTROL_TYPE]
                     error_type = ET.UNEXPECTED_CONTROL_TYPE
                     error = PlayerError(player_id=player_id,
                                         text=text,
                                         error_type=error_type)
                     response = error.make_message()
                     responses.append(response)
             else:
                 text = "Expected PLAYER_CONTROL message, got "
                 text = text + message[PM.MESSAGE_TYPE]
                 error_type = ET.UNEXPECTED_MESSAGE
                 error = PlayerError(text=text, error_type=error_type)
                 response = error.make_message()
                 responses.append(response)
         NetMessage.send_message(conn, responses)
         conn.close()
     players = []
     for user in self.users:
         players.append(Player(user, user.chips))
     self.round1.players = players
     self.roundthread = RoundThread(self.round1)
     self.roundthread.start()
     print('started the roundthread')
     self.roundthread.join()
     print('joined with roundthread')
     time.sleep(30)
     self.controller1.stop_listener()
     print('waiting for the listener thread to end')
     self.controller1.join()
     print('joined with the controller')
Exemplo n.º 24
0
    def listen_for_manager(self, s):
        """Listen for manager requests

        Arguments:
        s -- the socket on which to listen for manager requests

        """
        MM = ManagerMessage
        MT = MM.MessageTypes
        MI = MM.ManagerInfo
        MIF = MM.ManagerInfoFields
        TMC = MM.ToggleManagerCreated
        TMCF = MM.ToggleManagerCreatedFields
        TMR = MM.ToggleManagerRunning
        TMRF = MM.ToggleManagerRunningFields
        TTC = MM.ToggleTableCreated
        TTCF = MM.ToggleTableCreatedFields
        conn, addr = s.accept()
        messages = NetMessage.get_message(conn)
        responses = []
        for message in messages:
            if message[MM.MESSAGE_TYPE] == MT.MANAGER_INFO:
                info_request = message[MT.MANAGER_INFO]
                if info_request[MI.REQUEST] == MIF.STATUS_CHECK:
                    response = self.handle_manager_status_check(info_request)
                elif info_request[MI.REQUEST] == MIF.NUM_TABLES:
                    response = self.handle_num_tables_check(info_request)
                elif info_request[MI.REQUEST] == MIF.GET_TABLES:
                    response = self.handle_get_tables(info_request)
                else:
                    print("Got unrecognized MANAGER INFO message")
                    response = {}
            elif message[MM.MESSAGE_TYPE] == MT.TOGGLE_MANAGER_CREATED:
                toggle_request = message[MT.TOGGLE_MANAGER_CREATED]
                if toggle_request[TMC.ACTION] == TMCF.REMOVE:
                    response = self.close_manager(toggle_request)
                else:
                    print("Got unexpected TOGGLE_MANAGER_CREATED message")
                    response = {}
            elif message[MM.MESSAGE_TYPE] == MT.TOGGLE_MANAGER_RUNNING:
                toggle_request = message[MT.TOGGLE_MANAGER_RUNNING]
                if toggle_request[TMR.ACTION] == TMRF.STOP:
                    response = self.handle_stop_request(toggle_request)
                elif toggle_request[TMR.ACTION] == TMRF.START:
                    response = self.handle_start_request(toggle_request)
                else:
                    print("Got unrecognized TOGGLE_MANAGER_RUNNING message")
                    response = {}
            elif message[MM.MESSAGE_TYPE] == MT.TOGGLE_TABLE_CREATED:
                toggle_request = message[MT.TOGGLE_TABLE_CREATED]
                if toggle_request[TTC.ACTION] == TTCF.CREATE:
                    response = self.handle_create_table(toggle_request)
                elif toggle_request[TTC.ACTION] == TTCF.REMOVE:
                    response = self.handle_remove_table(toggle_request)
                else:
                    print("Got unrecognized TOGGLE_TABLE_CREATED message")
                    response = {}
            elif message[MM.MESSAGE_TYPE] == MT.TOGGLE_TABLE_RUNNING:
                toggle_request = message[MT.TOGGLE_TABLE_RUNNING]
                response = self.handle_toggle_table_running(toggle_request)
            else:
                print("Got unrecognized message type")
                response = {}
            responses.append(response)
        NetMessage.send_message(conn, responses)
        conn.close()