Example #1
0
  private synchronized void enterSimulationLog(int simulationID) {
    exitSimulationLog();

    if (rootFileHandler != null) {
      LogFormatter.separator(log, Level.FINE, "Entering log for simulation " + simulationID);
      try {
        Logger root = Logger.getLogger("");
        String name = logSimPrefix + "_SIM_" + simulationID + ".log";
        simLogHandler = new FileHandler(name, true);
        simLogHandler.setFormatter(formatter);
        simLogHandler.setLevel(rootFileHandler.getLevel());
        simLogName = name;
        root.addHandler(simLogHandler);
        root.removeHandler(rootFileHandler);
        LogFormatter.separator(
            log,
            Level.FINE,
            "Log for simulation "
                + simulationID
                + " at "
                + serverHost
                + ':'
                + serverPort
                + " started");
      } catch (Exception e) {
        log.log(Level.SEVERE, "could not open log file for simulation " + simulationID, e);
      }
    }
  }
Example #2
0
  public SimClient(
      ConfigManager config,
      String serverHost,
      int serverPort,
      String name,
      String password,
      String agentImpl)
      throws IOException {
    this.config = config;
    this.serverHost = serverHost;
    this.serverPort = serverPort;
    this.userName = name;
    this.userPassword = password;
    this.agentImpl = agentImpl;
    this.autoJoinCount = config.getPropertyAsInt("autojoin", 1);

    String logPrefix = config.getProperty("log.prefix", "aw");
    this.logFilePrefix = getLogDirectory("log.directory", logPrefix);
    this.logSimPrefix = getLogDirectory("log.sim.directory", logPrefix);

    // Set shorter names for the log
    formatter = new LogFormatter();
    formatter.setAliasLevel(2);
    LogFormatter.setFormatterForAllHandlers(formatter);

    setLogging();

    // TODO: create current context
    // currentContext = something

    if (!createAgentInstance()) {
      showWarning("Agent Setup Failed", "could not setup the agent");
      System.exit(1);
    }

    this.connection = new ServerConnection(this, 0L);
    this.connection.open();
    // Start the message thread
    new Thread(this, "SimClient").start();
  }
Example #3
0
  private synchronized void setLogging() throws IOException {
    int consoleLevel = config.getPropertyAsInt("log.consoleLevel", 0);
    int fileLevel = config.getPropertyAsInt("log.fileLevel", 0);
    Level consoleLogLevel = LogFormatter.getLogLevel(consoleLevel);
    Level fileLogLevel = LogFormatter.getLogLevel(fileLevel);
    Level logLevel =
        consoleLogLevel.intValue() < fileLogLevel.intValue() ? consoleLogLevel : fileLogLevel;
    boolean showThreads = config.getPropertyAsBoolean("log.threads", false);
    String[] packages = config.getPropertyAsArray("log.packages", "se.sics");
    if (packages != null && packages.length > 0) {
      for (int i = 0, n = packages.length; i < n; i++) {
        Logger.getLogger(packages[i]).setLevel(logLevel);
      }
    } else {
      Logger awRoot = Logger.getLogger("se.sics");
      awRoot.setLevel(logLevel);
    }

    formatter.setShowingThreads(showThreads);
    LogFormatter.setConsoleLevel(consoleLogLevel);
    // LogFormatter.setLevelForAllHandlers(logLevel);

    Logger root = Logger.getLogger("");
    if (fileLogLevel != Level.OFF) {
      if (rootFileHandler == null) {
        rootFileHandler = new FileHandler(logFilePrefix + "%g.log", 1000000, 10);
        rootFileHandler.setFormatter(formatter);
        root.addHandler(rootFileHandler);
      }
      rootFileHandler.setLevel(fileLogLevel);
      if (simLogHandler != null) {
        simLogHandler.setLevel(fileLogLevel);
      }
    } else if (rootFileHandler != null) {
      exitSimulationLog();
      root.removeHandler(rootFileHandler);
      rootFileHandler.close();
      rootFileHandler = null;
    }
  }
Example #4
0
  private synchronized void exitSimulationLog() {
    if (simLogHandler != null) {
      Logger root = Logger.getLogger("");
      LogFormatter.separator(log, Level.FINE, "Simulation log complete");

      root.addHandler(rootFileHandler);
      root.removeHandler(simLogHandler);
      simLogHandler.close();
      simLogHandler = null;
      // Try to remove the lock file since it is no longer needed
      if (simLogName != null) {
        new File(simLogName + ".lck").delete();
        simLogName = null;
      }
    }
  }
Example #5
0
  private void handleAdminContent(AdminContent admin) {
    ServerConnection connection = this.connection;
    int type = admin.getType();

    if (admin.isError()) {
      // Failed to do whatever we tried to do
      if (type == AdminContent.AUTH) {
        // Failed to login
        showWarning(
            "Authentication Failed",
            "could not login as " + userName + ": " + admin.getErrorReason());
        requestQuit();

      } else {
        showWarning(
            "Request Failed",
            "Failed to "
                + admin.getTypeAsString(type)
                + ": "
                + admin.getErrorAsString(admin.getError())
                + " ("
                + admin.getErrorReason()
                + ')');
        // What should be done here? FIX THIS!!!
      }
    } else if (connection == null) {
      // Connection has closed => ignore message

    } else {
      switch (type) {
        case AdminContent.AUTH:
          {
            String serverVersion = admin.getAttribute("server.version");
            connection.setAuthenticated(true);
            if (ConfigManager.compareVersion(serverVersion, "0.8.13") >= 0) {
              connection.setTransportSupported(BinaryTransportWriter.SUPPORT_TABLES);
              // connection
              // .setTransportSupported(BinaryTransportWriter.SUPPORT_CONSTANTS);
            }
            if (ConfigManager.compareVersion(serverVersion, "0.9") < 0) {
              // This is an older version of the server that does not
              // immediately send out this information by itself
              requestServerTime();
              autoJoinSimulation(false);
            }
            break;
          }

        case AdminContent.SERVER_TIME:
          {
            long serverTime = admin.getAttributeAsLong("time", 0L);
            if (serverTime > 0) {
              serverTimeDiff = System.currentTimeMillis() - serverTime;
              formatter.setLogTime(serverTime);
            }
            break;
          }

        case AdminContent.NEXT_SIMULATION:
        case AdminContent.JOIN_SIMULATION:
          {
            long currentTime = getServerTime();
            long startTime = admin.getAttributeAsLong("startTime", 0L);
            long nextTime = 0L;
            if (startTime > 0) {
              int simulationID = admin.getAttributeAsInt("simulation", -1);
              String simText = simulationID >= 0 ? " " + simulationID : "";
              // Simulation has been joined or already existed (displayed
              // to user by StatusInfo). Create the agent immediately to
              // ensure it has enough time to setup.
              if (this.agentService == null && !createAgentInstance()) {
                showWarning("Agent Setup Failed", "could not setup the agent");
                requestQuit();
              }
              if (startTime > currentTime) {
                log.info(
                    "next simulation"
                        + simText
                        + " starts in "
                        + ((startTime - currentTime) / 1000)
                        + " seconds");
              } else {
                log.info("next simulation" + simText + " has already started");
              }
              nextTime = startTime - 15000L;

            } else if (autoJoinCount > 0 && (type == AdminContent.NEXT_SIMULATION)) {
              autoJoinSimulation(false);

            } else {
              nextTime = admin.getAttributeAsLong("nextTime", 0L);
              if (nextTime < currentTime) {
                nextTime = currentTime + 60 * (long) (20000 + Math.random() * 5000);
              }
            }

            if (nextTime > currentTime) {
              // Next time to check for next simulation
              long delay = nextTime - currentTime;
              if (delay > MIN_MILLIS_BEFORE_DISCONNECT) {
                // We should wait a while => disconnect for now
                long maxSleep = 60 * (long) (56000 + Math.random() * 1000);
                // Do not wait too long before checking in at the server
                // again in case games have been rescheduled.
                if (delay > maxSleep) {
                  delay = maxSleep;
                }

                log.info(
                    "[will reconnect in "
                        + (delay / 60000)
                        + " minutes, "
                        + ((delay / 1000) % 60)
                        + " seconds]");

                isAutoJoinPending = true;
                this.connection = new ServerConnection(this, delay);
                this.connection.open();
                connection.close();
              }
            }
            break;
          }

        default:
          log.warning("unhandled admin content: " + admin);
          break;
      }
    }
  }