def checkIsPortFree(self):
        """This might be flaky unless this test is run with a portserver."""
        # The port should be free initially.
        port = portpicker.pick_unused_port()
        self.assertTrue(portpicker.is_port_free(port))

        cases = [
            (socket.AF_INET,  socket.SOCK_STREAM, None),
            (socket.AF_INET6, socket.SOCK_STREAM, 0),
            (socket.AF_INET6, socket.SOCK_STREAM, 1),
            (socket.AF_INET,  socket.SOCK_DGRAM,  None),
            (socket.AF_INET6, socket.SOCK_DGRAM,  0),
            (socket.AF_INET6, socket.SOCK_DGRAM,  1),
        ]
        for (sock_family, sock_type, v6only) in cases:
            # Occupy the port on a subset of possible protocols.
            try:
                sock = socket.socket(sock_family, sock_type, 0)
            except socket.error:
                print('Kernel does not support sock_family=%d' % sock_family,
                      file=sys.stderr)
                # Skip this case, since we cannot occupy a port.
                continue

            if not hasattr(socket, 'IPPROTO_IPV6'):
                v6only = None

            if v6only is not None:
                try:
                    sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY,
                                    v6only)
                except socket.error:
                    print('Kernel does not support IPV6_V6ONLY=%d' % v6only,
                          file=sys.stderr)
                    # Don't care; just proceed with the default.

            # Socket may have been taken in the mean time, so catch the
            # socket.error with errno set to EADDRINUSE and skip this
            # attempt.
            try:
                sock.bind(('', port))
            except socket.error as e:
                if e.errno == errno.EADDRINUSE:
                    raise portpicker.NoFreePortFoundError
                raise

            # The port should be busy.
            self.assertFalse(portpicker.is_port_free(port))
            sock.close()

            # Now it's free again.
            self.assertTrue(portpicker.is_port_free(port))
Exemplo n.º 2
0
def DetermineLocalPort(port_arg=0):
    if not port_arg:
        port_arg = portpicker.pick_unused_port()
    if not portpicker.is_port_free(port_arg):
        raise LocalPortUnavailableError('Local port [%d] is not available.' %
                                        port_arg)
    return port_arg
def startStopSim():
    global PORT, client, cameras

    if client is None:
        # No client, then Start

        PORT = int(request.args.get('port'))

        print("waiting")
        while portpicker.is_port_free(PORT):
            sleep(5 * againIn)
            print(".")

        client = sim.simxStart('127.0.0.1', PORT, True, True, 5000, 5)
        verboseResp(client, "simxStart")

        return str(client)
    else:
        # There is a client, then Stop
        stop()
        sleep(.5)
        response = sim.simxSetIntegerSignal(client, 'doClose', 1,
                                            sim.simx_opmode_oneshot_wait)
        verboseResp(response, "simxSetIntegerSignal doClose")
        try:
            finish()
        except:
            verboseResp("already finished?", "finish")
        reset()
        if response == -1:
            return "-1"
        else:
            return "0"
Exemplo n.º 4
0
 def _GetLocalHostPort(self, args):
     local_port = (int(args.local_host_port.port)
                   if args.local_host_port.port else 0)
     if not local_port:
         local_port = portpicker.pick_unused_port()
         log.Print('Picking local unused port [%d].' % local_port)
     if not portpicker.is_port_free(local_port):
         raise LocalHostPortUnavailableError(
             'Local port [%d] is not available.' % local_port)
     return args.local_host_port.host, local_port
Exemplo n.º 5
0
    def testIsPortFree(self):
        """This might be flaky unless this test is run with a portserver."""
        # The port should be free initially.
        port = portpicker.pick_unused_port()
        self.assertTrue(portpicker.is_port_free(port))

        cases = [
            (socket.AF_INET,  socket.SOCK_STREAM, None),
            (socket.AF_INET6, socket.SOCK_STREAM, 0),
            (socket.AF_INET6, socket.SOCK_STREAM, 1),
            (socket.AF_INET,  socket.SOCK_DGRAM,  None),
            (socket.AF_INET6, socket.SOCK_DGRAM,  0),
            (socket.AF_INET6, socket.SOCK_DGRAM,  1),
        ]
        for (sock_family, sock_type, v6only) in cases:
            # Occupy the port on a subset of possible protocols.
            try:
                sock = socket.socket(sock_family, sock_type, 0)
            except socket.error:
                print('Kernel does not support sock_family=%d' % sock_family,
                      file=sys.stderr)
                # Skip this case, since we cannot occupy a port.
                continue
            if v6only is not None:
                try:
                    sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY,
                                    v6only)
                except socket.error:
                    print('Kernel does not support IPV6_V6ONLY=%d' % v6only,
                          file=sys.stderr)
                    # Don't care; just proceed with the default.
            sock.bind(('', port))

            # The port should be busy.
            self.assertFalse(portpicker.is_port_free(port))
            sock.close()

            # Now it's free again.
            self.assertTrue(portpicker.is_port_free(port))
Exemplo n.º 6
0
def getUnusedPort():
    """Returns an unused TCP port on the local host inside the defined port range.
  To be used when starting peer SAS webserver or other database webserver.
  """
    config = _GetSharedTestConfig()
    if config.min_port < 0:
        return portpicker.pick_unused_port()
    global _ports
    # Find the first available port in the defined range.
    for p in xrange(config.min_port, config.max_port):
        if p not in _ports and portpicker.is_port_free(p):
            _ports.add(p)
            return p
    raise AssertionError('No available new ports')
Exemplo n.º 7
0
 def contiguous_ports(cls, guests=1, attempts=40):
     """Returns a Portconfig with adjacent ports"""
     for _ in range(attempts):
         start = portpicker.pick_unused_port()
         others = [start + j for j in range(1, 2 + guests * 2)]
         if all(portpicker.is_port_free(p) for p in others):
             server_ports = [start, others.pop(0)]
             player_ports = []
             while others:
                 player_ports.append([others.pop(0), others.pop(0)])
             pc = cls(server_ports=server_ports, player_ports=player_ports)
             pc._picked_ports.append(start)
             return pc
     raise portpicker.NoFreePortFoundError()
Exemplo n.º 8
0
def DefaultPortIfAvailable():
  """Returns default port if available.

  Raises:
    EmulatorArgumentsError: if port is not available.

  Returns:
    int, default port
  """
  if portpicker.is_port_free(_DEFAULT_PORT):
    return _DEFAULT_PORT
  else:
    raise EmulatorArgumentsError(
        'Default emulator port [{}] is already in use'.format(_DEFAULT_PORT))
Exemplo n.º 9
0
def pick_contiguous_unused_ports(num_ports,
                                 retry_interval_secs=3,
                                 retry_attempts=5):
    """Reserves and returns a list of `num_ports` contiguous unused ports."""
    for _ in range(retry_attempts):
        start_port = portpicker.pick_unused_port()
        if start_port is not None:
            ports = [start_port + p for p in range(num_ports)]
            if all(portpicker.is_port_free(p) for p in ports):
                return ports
            else:
                return_ports(ports)

        time.sleep(retry_interval_secs)

    raise RuntimeError("Unable to obtain %d contiguous unused ports." %
                       num_ports)
Exemplo n.º 10
0
def pick_contiguous_unused_ports(num_ports,
                                 retry_interval_secs=1,
                                 retry_attempts=5):
    """Reserves and returns a list of `num_ports` contiguous unused ports."""
    if num_ports <= 0:
        raise ValueError("Number of ports, must be >= 1, got: %s" % num_ports)
    for _ in range(retry_attempts):
        start_port = portpicker.pick_unused_port()
        if start_port is not None:
            ports = [start_port + p for p in range(num_ports)]
            if all(portpicker.is_port_free(p) for p in ports):
                _contiguous_ports.update(ports[1:])
                return ports
            else:
                portpicker.return_port(start_port)

        time.sleep(retry_interval_secs)

    raise RuntimeError("Unable to obtain %d contiguous unused ports." %
                       num_ports)
Exemplo n.º 11
0
def is_port_open(port):
    if not is_port_free(port):
        return True
    else:
        return False
Exemplo n.º 12
0
def get_free_port(start, end):
    return [port for port in range(start, end) if is_port_free(port)]
Exemplo n.º 13
0
def human():
    """Run a host which expects one player to connect remotely."""
    run_config = run_configs.get()

    map_inst = maps.get(FLAGS.map)

    if not FLAGS.rgb_screen_size or not FLAGS.rgb_minimap_size:
        logging.info(
            "Use --rgb_screen_size and --rgb_minimap_size if you want rgb "
            "observations.")

    ports = [FLAGS.config_port + p for p in range(5)]  # tcp + 2 * num_players
    if not all(portpicker.is_port_free(p) for p in ports):
        sys.exit("Need 5 free ports after the config port.")

    proc = None
    ssh_proc = None
    tcp_conn = None
    udp_sock = None
    try:
        proc = run_config.start(extra_ports=ports[1:],
                                timeout_seconds=300,
                                host=FLAGS.host,
                                window_loc=(50, 50))

        tcp_port = ports[0]
        settings = {
            "remote": FLAGS.remote,
            "game_version": proc.version.game_version,
            "realtime": FLAGS.realtime,
            "map_name": map_inst.name,
            "map_path": map_inst.path,
            "map_data": map_inst.data(run_config),
            "ports": {
                "server": {
                    "game": ports[1],
                    "base": ports[2]
                },
                "client": {
                    "game": ports[3],
                    "base": ports[4]
                },
            }
        }

        create = sc_pb.RequestCreateGame(
            realtime=settings["realtime"],
            local_map=sc_pb.LocalMap(map_path=settings["map_path"]))
        create.player_setup.add(type=sc_pb.Participant)
        create.player_setup.add(type=sc_pb.Participant)

        controller = proc.controller
        controller.save_map(settings["map_path"], settings["map_data"])
        controller.create_game(create)

        if FLAGS.remote:
            ssh_proc = lan_sc2_env.forward_ports(
                FLAGS.remote, proc.host, [settings["ports"]["client"]["base"]],
                [tcp_port, settings["ports"]["server"]["base"]])

        print("-" * 80)
        print("Join: play_vs_agent --host %s --config_port %s" %
              (proc.host, tcp_port))
        print("-" * 80)

        tcp_conn = lan_sc2_env.tcp_server(
            lan_sc2_env.Addr(proc.host, tcp_port), settings)

        if FLAGS.remote:
            udp_sock = lan_sc2_env.udp_server(
                lan_sc2_env.Addr(proc.host,
                                 settings["ports"]["client"]["game"]))

            lan_sc2_env.daemon_thread(
                lan_sc2_env.tcp_to_udp,
                (tcp_conn, udp_sock,
                 lan_sc2_env.Addr(proc.host,
                                  settings["ports"]["server"]["game"])))

            lan_sc2_env.daemon_thread(lan_sc2_env.udp_to_tcp,
                                      (udp_sock, tcp_conn))

        join = sc_pb.RequestJoinGame()
        join.shared_port = 0  # unused
        join.server_ports.game_port = settings["ports"]["server"]["game"]
        join.server_ports.base_port = settings["ports"]["server"]["base"]
        join.client_ports.add(game_port=settings["ports"]["client"]["game"],
                              base_port=settings["ports"]["client"]["base"])

        join.race = sc2_env.Race[FLAGS.user_race]
        join.player_name = FLAGS.user_name
        if FLAGS.render:
            join.options.raw = True
            join.options.score = True
            if FLAGS.feature_screen_size and FLAGS.feature_minimap_size:
                fl = join.options.feature_layer
                fl.width = 24
                FLAGS.feature_screen_size.assign_to(fl.resolution)
                FLAGS.feature_minimap_size.assign_to(fl.minimap_resolution)
            if FLAGS.rgb_screen_size and FLAGS.rgb_minimap_size:
                FLAGS.rgb_screen_size.assign_to(join.options.render.resolution)
                FLAGS.rgb_minimap_size.assign_to(
                    join.options.render.minimap_resolution)
        controller.join_game(join)

        if FLAGS.render:
            renderer = renderer_human.RendererHuman(fps=FLAGS.fps,
                                                    render_feature_grid=False)
            renderer.run(run_configs.get(), controller, max_episodes=1)
        else:  # Still step forward so the Mac/Windows renderer works.
            while True:
                frame_start_time = time.time()
                if not FLAGS.realtime:
                    controller.step()
                obs = controller.observe()

                if obs.player_result:
                    break
                time.sleep(
                    max(0, frame_start_time - time.time() + 1 / FLAGS.fps))
    except KeyboardInterrupt:
        pass
    finally:
        if tcp_conn:
            tcp_conn.close()
        if proc:
            proc.close()
        if udp_sock:
            udp_sock.close()
        if ssh_proc:
            ssh_proc.terminate()
            for _ in range(5):
                if ssh_proc.poll() is not None:
                    break
                time.sleep(1)
            if ssh_proc.poll() is None:
                ssh_proc.kill()
                ssh_proc.wait()
Exemplo n.º 14
0
def human():
  """Run a host which expects one player to connect remotely."""
  run_config = run_configs.get()

  map_inst = maps.get(FLAGS.map)

  if not FLAGS.rgb_screen_size or not FLAGS.rgb_minimap_size:
    logging.info("Use --rgb_screen_size and --rgb_minimap_size if you want rgb "
                 "observations.")

  while True:
    start_port = portpicker.pick_unused_port()
    ports = [start_port + p for p in range(4)]  # 2 * num_players
    if all(portpicker.is_port_free(p) for p in ports):
      break

  host_proc = run_config.start(extra_ports=ports, host=FLAGS.host,
                               timeout_seconds=300, window_loc=(50, 50))
  client_proc = run_config.start(extra_ports=ports, host=FLAGS.host,
                                 connect=False, window_loc=(700, 50))

  create = sc_pb.RequestCreateGame(
      realtime=FLAGS.realtime, local_map=sc_pb.LocalMap(map_path=map_inst.path))
  create.player_setup.add(type=sc_pb.Participant)
  create.player_setup.add(type=sc_pb.Participant)

  controller = host_proc.controller
  controller.save_map(map_inst.path, map_inst.data(run_config))
  controller.create_game(create)

  print("-" * 80)
  print("Join host: play_vs_agent --map %s --host %s --host_port %s "
        "--lan_port %s" % (FLAGS.map, FLAGS.host, client_proc.port, start_port))
  print("-" * 80)

  join = sc_pb.RequestJoinGame()
  join.shared_port = 0  # unused
  join.server_ports.game_port = ports.pop(0)
  join.server_ports.base_port = ports.pop(0)
  join.client_ports.add(game_port=ports.pop(0), base_port=ports.pop(0))

  join.race = sc2_env.Race[FLAGS.user_race]
  if FLAGS.render:
    join.options.raw = True
    join.options.score = True
    if FLAGS.feature_screen_size and FLAGS.feature_minimap_size:
      fl = join.options.feature_layer
      fl.width = 24
      fl.resolution.x = FLAGS.feature_screen_size
      fl.resolution.y = FLAGS.feature_screen_size
      fl.minimap_resolution.x = FLAGS.feature_minimap_size
      fl.minimap_resolution.y = FLAGS.feature_minimap_size
    if FLAGS.rgb_screen_size and FLAGS.rgb_minimap_size:
      join.options.render.resolution.x = FLAGS.rgb_screen_size
      join.options.render.resolution.y = FLAGS.rgb_screen_size
      join.options.render.minimap_resolution.x = FLAGS.rgb_minimap_size
      join.options.render.minimap_resolution.y = FLAGS.rgb_minimap_size
  controller.join_game(join)

  if FLAGS.render:
    renderer = renderer_human.RendererHuman(
        fps=FLAGS.fps, render_feature_grid=False)
    renderer.run(run_configs.get(), controller, max_episodes=1)
  else:  # Still step forward so the Mac/Windows renderer works.
    try:
      while True:
        frame_start_time = time.time()
        if not FLAGS.realtime:
          controller.step()
        obs = controller.observe()

        if obs.player_result:
          break
        time.sleep(max(0, frame_start_time - time.time() + 1 / FLAGS.fps))
    except KeyboardInterrupt:
      pass

  for p in [host_proc, client_proc]:
    p.close()
Exemplo n.º 15
0
def server():
    """Run a host which expects one player to connect remotely."""
    run_config = run_configs.get()

    map_inst = maps.get(FLAGS.map)

    if not FLAGS.rgb_screen_size or not FLAGS.rgb_minimap_size:
        logging.info(
            "Use --rgb_screen_size and --rgb_minimap_size if you want rgb "
            "observations.")

    ports = [FLAGS.port0, FLAGS.port1, FLAGS.port2, FLAGS.port3, FLAGS.port4]
    if not all(portpicker.is_port_free(p) for p in ports):
        sys.exit("Need 5 free ports after the config port.")

    proc = None
    tcp_conn = None

    try:
        proc = run_config.start(extra_ports=ports[1:],
                                timeout_seconds=300,
                                host=FLAGS.host,
                                window_loc=(50, 50))

        tcp_port = ports[0]
        settings = {
            "remote": False,
            "game_version": proc.version.game_version,
            "realtime": FLAGS.realtime,
            "map_name": map_inst.name,
            "map_path": map_inst.path,
            "map_data": map_inst.data(run_config),
            "ports": {
                "server": {
                    "game": ports[1],
                    "base": ports[2]
                },
                "client": {
                    "game": ports[3],
                    "base": ports[4]
                },
            }
        }

        create = sc_pb.RequestCreateGame(
            realtime=settings["realtime"],
            local_map=sc_pb.LocalMap(map_path=settings["map_path"]),
            disable_fog=FLAGS.disable_fog)
        create.player_setup.add(type=sc_pb.Participant)
        create.player_setup.add(type=sc_pb.Participant)

        controller = proc.controller
        controller.save_map(settings["map_path"], settings["map_data"])
        controller.create_game(create)

        print("-" * 80)
        print("Join: agent_vs_agent --host %s --config_port %s" %
              (proc.host, tcp_port))
        print("-" * 80)

        tcp_conn = lan_sc2_env.tcp_server(
            lan_sc2_env.Addr(proc.host, tcp_port), settings)

        join = sc_pb.RequestJoinGame()
        join.shared_port = 0  # unused
        join.server_ports.game_port = settings["ports"]["server"]["game"]
        join.server_ports.base_port = settings["ports"]["server"]["base"]
        join.client_ports.add(game_port=settings["ports"]["client"]["game"],
                              base_port=settings["ports"]["client"]["base"])

        join.race = sc2_env.Race[FLAGS.agent_race]
        join.options.raw = True
        join.options.score = True
        if FLAGS.feature_screen_size and FLAGS.feature_minimap_size:
            fl = join.options.feature_layer
            fl.width = 24
            FLAGS.feature_screen_size.assign_to(fl.resolution)
            FLAGS.feature_minimap_size.assign_to(fl.minimap_resolution)

        if FLAGS.rgb_screen_size and FLAGS.rgb_minimap_size:
            FLAGS.rgb_screen_size.assign_to(join.options.render.resolution)
            FLAGS.rgb_minimap_size.assign_to(
                join.options.render.minimap_resolution)

        controller.join_game(join)

        with lan_server_sc2_env.LanServerSC2Env(
                race=sc2_env.Race[FLAGS.agent_race],
                step_mul=FLAGS.step_mul,
                agent_interface_format=sc2_env.parse_agent_interface_format(
                    feature_screen=FLAGS.feature_screen_size,
                    feature_minimap=FLAGS.feature_minimap_size,
                    rgb_screen=FLAGS.rgb_screen_size,
                    rgb_minimap=FLAGS.rgb_minimap_size,
                    action_space=FLAGS.action_space,
                    use_feature_units=FLAGS.use_feature_units),
                visualize=False,
                controller=controller,
                map_name=FLAGS.map) as env:
            agent_module, agent_name = FLAGS.agent.rsplit(".", 1)
            agent_cls = getattr(importlib.import_module(agent_module),
                                agent_name)
            agent_kwargs = {}
            if FLAGS.agent_config:
                agent_kwargs['config_path'] = FLAGS.agent_config
            agents = [agent_cls(**agent_kwargs)]

            try:
                run_loop(agents, env, FLAGS.max_steps)
            except lan_server_sc2_env.RestartException:
                pass

            if FLAGS.save_replay:
                env.save_replay(agent_cls.__name__)
    finally:
        if tcp_conn:
            tcp_conn.close()
        if proc:
            proc.close()
Exemplo n.º 16
0
import portpicker

if __name__ == '__main__':
    test_port = portpicker.is_port_free(55555)
    print(test_port)
Exemplo n.º 17
0
  def Run(self, args):
    if 'all' in args.emulators:
      if len(args.emulators) > 1:
        raise util.EmulatorArgumentsError(
            "Cannot specify 'all' with other emulators")
      if args.route_to_public:
        raise util.EmulatorArgumentsError(
            'Cannot specify --route-to-public and --emulators=all')
    else:
      unknown_emulators = [x for x in args.emulators
                           if x not in config.EMULATORS]
      if unknown_emulators:
        raise util.EmulatorArgumentsError('Specified unrecognized emulators: '
                                          ','.join(unknown_emulators))

    proxy_port = args.proxy_port
    if args.proxy_port is None:
      proxy_port = util.DefaultPortIfAvailable()

    if not portpicker.is_port_free(proxy_port):
      raise util.EmulatorArgumentsError(
          'Specified proxy port [{}] is not available'.format(proxy_port))

    util.EnsureComponentIsInstalled('emulator-reverse-proxy',
                                    'gcloud emulators start')
    for flag, emulator in six.iteritems(config.EMULATORS):
      title = emulator.emulator_title
      component = emulator.emulator_component
      if (args.emulators is not None and
          (flag in args.emulators or 'all' in args.emulators)):
        java.RequireJavaInstalled(title)
        util.EnsureComponentIsInstalled(component, title)

    with contextlib.ExitStack() as stack:

      local_emulator_ports = {}
      for emulator in args.emulators:
        port = portpicker.pick_unused_port()
        local_emulator_ports[emulator] = port
        stack.enter_context(config.EMULATORS[emulator].Start(port))

      _, routes_config_file = tempfile.mkstemp()
      config.WriteRoutesConfig(config.EMULATORS, routes_config_file)
      log.status.Print(
          'routes configuration written to file: {}'.format(routes_config_file))

      proxy_config = config.ProxyConfiguration(local_emulator_ports,
                                               args.route_to_public,
                                               proxy_port)

      _, proxy_config_file = tempfile.mkstemp()
      proxy_config.WriteJsonToFile(proxy_config_file)
      log.status.Print(
          'proxy configuration written to file: {}'.format(proxy_config_file))

      # TODO(b/35872500) for some reason, in this case, this will block. Maybe
      #   we need to flush something, maybe not. Regardless, this is fine for
      #   now, but would be nice for it to not block like everything else
      with proxy_util.StartEmulatorProxy(
          args=[routes_config_file, proxy_config_file]) as proxy_process:
        # This will block the console
        util.PrefixOutput(proxy_process, 'emulator-reverse-proxy')
Exemplo n.º 18
0
 def testIsPortFreeException(self):
     port = portpicker.pick_unused_port()
     with mock.patch.object(socket, 'socket') as mock_sock:
         mock_sock.side_effect = socket.error('fake socket error', 0)
         self.assertFalse(portpicker.is_port_free(port))