Ejemplo n.º 1
0
def start_server(sock_file: str,
                 game: Game,
                 dockers,
                 use_docker=True) -> socketserver.BaseServer:
    '''
    Start a socket server for the players to connect to
    Args:
        sock_file: This is a string name of the file that will be used for
                    as UnixStream

        game: The game information that is being run

        use_docker bool: whether to use docker or not

    Return:
        server_thread: The connection so it can be closed by parent functions at
                        the appropriate time
    '''

    # Create handler for mangaing each connections to server
    receive_handler = create_receive_handler(game, dockers, use_docker, True)

    if isinstance(sock_file, tuple):
        # tcp port
        server = socketserver.ThreadingTCPServer(sock_file, receive_handler)
    else:
        print('starting server', sock_file)
        server = socketserver.ThreadingUnixStreamServer(
            sock_file, receive_handler)

    server_thread = threading.Thread(target=server.serve_forever, daemon=True)
    logging.info("Server Started at %s", sock_file)
    server_thread.start()

    return server
Ejemplo n.º 2
0
 def __init__(self,socket,handlerfactory):
   self.socket = socket
   try:
     os.unlink(socket)
   except: pass
   self.server = socketserver.ThreadingUnixStreamServer(socket,handlerfactory)
   self.server.daemon = self
Ejemplo n.º 3
0
def start_server(sock_file: str,
                 game: Game,
                 dockers,
                 use_docker=True) -> socketserver.BaseServer:
    '''
    Start a socket server for the players to connect to
    Args:
        sock_file: This is a string name of the file that will be used for
                    as UnixStream

        game: The game information that is being run

        use_docker bool: whether to use docker or not

    Return:
        server_thread: The connection so it can be closed by parent functions at
                        the appropriate time
    '''

    # Create handler for mangaing each connections to server
    receive_handler = create_receive_handler(game, dockers, use_docker, True)

    if isinstance(sock_file, tuple):
        # tcp port
        server = socketserver.ThreadingTCPServer(sock_file, receive_handler)
    else:
        server = socketserver.ThreadingUnixStreamServer(
            sock_file, receive_handler)

    def wait_for_connections():
        time.sleep(BUILD_TIMEOUT)
        for player in game.players:
            if not player['built_successfully']:
                print('Player failed to connect to manager after',
                      BUILD_TIMEOUT, 'seconds:', player['player'])
                if bc.Team.Red == player['player'].team:
                    game.winner = 'player2'
                else:
                    game.winner = 'player1'
                game.disconnected = True
                game.game_over = True

    server_thread = threading.Thread(target=server.serve_forever, daemon=True)
    logging.info("Server Started at %s", sock_file)
    server_thread.start()
    waiter_thread = threading.Thread(target=wait_for_connections, daemon=True)
    waiter_thread.start()

    return server
Ejemplo n.º 4
0
def server_generator(socket_type, addr, handler):
    """
    This function start a server daemon that will listen on specific address
    Parameter:
        socket_type     Possible values are either 'AF_UNIX' or 'AF_INET'
        address         Depends on socket_type, it can be a path in the file
                            system (AF_UNIX case) or a (HOST, PORT) tuple
                            (AF_INET case)
        handler         A class derived from socketserver.BaseRequestHandler
                            that would be initialize and execute on each
                            request
    """
    if socket_type == 'AF_UNIX':
        print('Using AF_UNIX socket')
        return socketserver.ThreadingUnixStreamServer(addr, handler)
    elif socket_type == 'AF_INET':
        print('Using AF_INET socket')
        return socketserver.ThreadingTCPServer(addr, handler)
    else:
        raise ValueError(
            'Invalid socket_type {0} provided'.format(socket_type))
Ejemplo n.º 5
0
 def run(self):
     self.unixserver = socketserver.ThreadingUnixStreamServer(
         self.filename, handler)
     self.unixserver.serve_forever()