protected String getConfigProperty(String prop, String defaultValue) { ConfigManager config = parent.getConfig(); String value = config.getProperty(roleName + '.' + getName() + '.' + prop); if (value == null) { value = config.getProperty(roleName + '.' + prop); } return value == null ? defaultValue : value; }
public ViewerPanel(String userName, String serverName) { this.serverName = serverName; this.userName = userName; // TODO: use the tasim_viewer.conf file // NOTE: this should actually be the tac09aa_viewer.conf file String configFile = "tasim_viewer.conf"; // String configFile = "tac13adx_viewer.conf"; URL configURL = ViewerPanel.class.getResource("/config/" + configFile); config = new ConfigManager(); try { if (configURL != null) { config.loadConfiguration(configURL); } else if (!config.loadConfiguration("config" + File.separatorChar + configFile)) { // Failed to load the configuration. log.severe("could not find config " + configFile); } } catch (Exception e) { log.severe("could not find config " + configFile); } // Should not be hardcoded but setup depending on simulation type. FIX // THIS!!! 0 \TODO viewer = null; try { String simulationViewerClass = config.getProperty("simulationViewer"); viewer = (SimulationViewer) Class.forName(simulationViewerClass).newInstance(); } catch (InstantiationException e) { log.severe("Could not instantiate the simulation viewer"); } catch (IllegalAccessException e) { log.severe("Could not instantiate the simulation viewer"); } catch (ClassNotFoundException e) { log.severe("Could not find the simulation viewer class"); } viewer.init(this); viewerPanel = viewer.getComponent(); mainPanel = new JPanel(new BorderLayout()); mainPanel.setForeground(foregroundColor); mainPanel.setBackground(backgroundColor); mainPanel.add(viewerPanel, BorderLayout.CENTER); statusLabel = new JLabel("Status:"); statusLabel.setOpaque(true); statusLabel.setForeground(foregroundColor); statusLabel.setBackground(backgroundColor); chatPanel = new ChatPanel(this); // Hack to avoid using another panel chatPanel.setStatusLabel(statusLabel); mainPanel.add(chatPanel, BorderLayout.SOUTH); statusPanel = new StatusPanel(this, foregroundColor, backgroundColor); mainPanel.add(statusPanel, BorderLayout.NORTH); }
public String getProperty(String name, String defaultValue) { String value = properties.getProperty(name); if (value == null || value.length() == 0) { value = parent != null ? parent.getProperty(name, defaultValue) : defaultValue; } return value; }
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(); }
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; } }
private String getLogDirectory(String property, String name) throws IOException { String logDirectory = config.getProperty(property); if (logDirectory != null) { // Create directories for logs File fp = new File(logDirectory); if ((!fp.exists() && !fp.mkdirs()) || !fp.isDirectory()) { throw new IOException("could not create directory '" + logDirectory + '\''); } return fp.getAbsolutePath() + File.separatorChar + name; } else { return name; } }
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; } } }