Beispiel #1
0
 /**
  * This method is used to display error messages. Invoking it terminates the program.
  *
  * @param msg the error message to display
  */
 public static void fail(String msg) {
   System.out.printf("[server:fatal] %s\n", msg);
   System.exit(-1);
 }
Beispiel #2
0
  /**
   * Runs a match; configures the controller and list of proxies, and starts running the game in a
   * separate thread.
   */
  private void runMatch(final Match match, final ProxyWriter proxyWriter) throws Exception {
    if (Mode.HEADLESS.equals(mode) || Mode.SCRIMMAGE.equals(mode) || Mode.TOURNAMENT.equals(mode)) {
      this.state = State.RUNNING;
      this.runUntil = Integer.MAX_VALUE;
    }

    // Poll for RUNNING, if mode == Mode.LOCAL
    while (!State.RUNNING.equals(state)) {
      try {
        Thread.sleep(250);
      } catch (InterruptedException e) {
      }
    }

    long startTime = System.currentTimeMillis();

    say("-------------------- Match Starting --------------------");
    say(match.toString());

    // Compute the header and send it to all listeners.
    MatchHeader header = match.getHeader();
    proxyWriter.enqueue(header);
    ExtensibleMetadata exHeader = match.getHeaderMetadata();
    proxyWriter.enqueue(exHeader);

    this.state = State.RUNNING;

    int count = 0;

    final String throttle = options.get("bc.server.throttle");
    final int throttleCount = options.getInt("bc.server.throttle-count");
    final boolean doYield = "yield".equals(throttle);
    final boolean doSleep = "sleep".equals(throttle);

    // If there are more rounds to be run, run them and
    // and send the round (and optionally stats) bytes to
    // recipients.
    while (match.hasMoreRounds()) {

      // If not paused/stopped:
      switch (this.state) {
        case RUNNING:
          if (match.getRoundNumber() == runUntil) {
            Thread.sleep(25);
            break;
          }

          final RoundDelta round = match.getRound();
          if (round == null) break;

          if (GameState.BREAKPOINT.equals(match.getGameState())) {
            this.state = State.PAUSED;
            proxyWriter.enqueue(new PauseEvent());
          } else if (GameState.DONE.equals(match.getGameState())) {
            this.state = State.FINISHED;
          }

          proxyWriter.enqueue(round);

          if (count++ == throttleCount) {
            if (doYield) Thread.yield();
            else if (doSleep) Thread.sleep(1);
            count = 0;
          }

          break;

        case PAUSED:
          Thread.sleep(250);
          break;
      }
    }

    // Compute footer data.
    GameStats gameStats = match.getGameStats();
    MatchFooter footer = match.getFooter();

    proxyWriter.enqueue(gameStats);
    proxyWriter.enqueue(footer);

    say(match.getWinnerString());
    say("-------------------- Match Finished --------------------");

    double timeDiff = (System.currentTimeMillis() - startTime) / 1000.0;
    debug(String.format("match completed in %.4g seconds", timeDiff));

    this.state = State.FINISHED;
  }