⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serverconfig.java

📁 是一款国外的网络游戏平台的源码*不是类似浩方那种虚拟局域网技术
💻 JAVA
字号:
/*
 * Created on May 30, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.GTADS.server;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.*;
import java.net.Socket;
import java.util.Enumeration;
import java.util.Properties;

import org.GTADS.messenger.MessageAdapter;
import org.GTADS.protocol.MetaData;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ServerConfig {
	public Properties SystemConfiguration; 
	private static boolean configVerbose;
	private static int configDebugLevel;
	private static int configStatusReport;
	private String Motd;
	public static ServerConfig instance;
	public static String configFileName;
	
	public static ServerConfig getInstance(String configFile){
		if (configFile == null) {
			configFile = "gtadsconfig";
		}
		
		configFileName = configFile;
		if (instance == null){
			instance = new ServerConfig(configFileName);
		}
		return instance;
	}
	
	public static ServerConfig getInstance(){
		return ServerConfig.getInstance(configFileName);
	}
	
	public static void clearInstance(){
		if (instance != null){
			instance = null;
			configFileName = "gtadsconfig";
		}
	}
	
	public ServerConfig(String configFile) {
		// Until we use an actual config file
		// hardcode the values
		// TODO: implement a config file
		
		SystemConfiguration = new Properties();
		DSChatServer.verbose = true;
		DSChatServer.debugLevel = 5;
		DSChatServer.statusReport = 30;
		
		readInConfigFile(configFile);
		//Motd = "Welcome to GTADS Server\r\n";
		//Motd += "This is a temporary MOTD for our Beta server\r\n";
		//Motd += "as so this software is BETA and is not guaranteed to work\r\n";
		//Motd += "Please visit http://sourceforge.net/projects/gtads/\r\n";
		boolean isMotd = SystemConfiguration.getProperty("MOTD", "no").equalsIgnoreCase("yes");
		
		if (isMotd)
			readinMotd();
	}
	
	public String getPasswdFileName() {
		return SystemConfiguration.getProperty("ACCOUNTS FILE");
	}
	
	public String getMotd(){
		return Motd;
	}
	
	public String getMotdFile(){
		return SystemConfiguration.getProperty("MOTD FILE", "");
	}
	
	public void readinMotd(){
		Motd = new String();
		String buffer = new String();
		try {
			boolean endOfFile = false;
			FileReader authFile = new FileReader(getMotdFile());
			BufferedReader inputBuffer = new BufferedReader(authFile);
			
			while (!endOfFile) {
				buffer = inputBuffer.readLine();
				if (buffer == null)
					endOfFile = true;
				else if (buffer.equals(""))
					Motd += " \r\n";
				else
					Motd += buffer  + "\r\n";
			}
		} catch (FileNotFoundException FnE) {
			return;
		} catch (IOException ioe){
			return;
		}
	}
	
	public boolean readInConfigFile(String configFileName) {
		boolean success = true;
		boolean endOfFile = false;
		String buffer = new String();
		
		try {
			FileReader propertiesFile = new FileReader(configFileName);
			BufferedReader inputBuffer = new BufferedReader(propertiesFile);
			
			while (!endOfFile) {
				buffer = inputBuffer.readLine();
				if (buffer != null) {
				handlePropertyValue(buffer);
				}
				else {
					endOfFile = true;
				}
			}
		} catch (FileNotFoundException FnE) {
			return !success;
		}
		catch (IOException ioe) {
			return !success;
		}
		return success;
	}
	
	private void handlePropertyValue(String propertyLine) {
		String propertyKey;
		String propertyValue;
		String splitString[] = new String[2];
		
		if (propertyLine != null) {
			if (propertyLine.startsWith(";")){
				return;
			}
			else if (propertyLine.split("\\=", 2).length > 1){
				splitString = propertyLine.split("\\=", 2);
				propertyKey = splitString[0];
				propertyValue = splitString[1];
				SystemConfiguration.setProperty(propertyKey.toUpperCase(),propertyValue);
			}
		}
	}
	
	public int getServerPort(){
		int defaultPort = 7000;
		Integer serverPortInt = new Integer(7000);
		String serverPortString = SystemConfiguration.getProperty("SERVERPORT", defaultPort + "");
		
		try {
			if (serverPortString != null) {
				serverPortInt = Integer.valueOf(serverPortString);
			}
		} catch (NumberFormatException nfe) {
			return serverPortInt.intValue();
		}
		return serverPortInt.intValue();
	}
	
	public boolean isFriendsListEnabled(){
		return SystemConfiguration.getProperty("FRIENDSLIST", "no").equalsIgnoreCase("yes");
	}
	public String getFriendsListDirectory(){
		if (isFriendsListEnabled()){
			return SystemConfiguration.getProperty("FRIENDS LIST DIRECTORY", null);
		}
		return null;
	}
	
	public boolean isConsoleOutAllowed(){
		return SystemConfiguration.getProperty("CONSOLE OUTPUT", "no").equalsIgnoreCase("yes");
	}
	
	public int consoleDebugLevel(){
		String debugLevelString = SystemConfiguration.getProperty("DEBUG LEVEL", "3");
		int debugLevelInteger = 3;
		try {
			debugLevelInteger = Integer.valueOf(debugLevelString).intValue();
		} catch (NumberFormatException nfe) {
			return debugLevelInteger;
		}
		return debugLevelInteger;
	}
	
	public int statusReportInterval(){
		String statusInterval = SystemConfiguration.getProperty("STATUS REPORT INTERVAL", "600");
		int statusInt = 30;
		try {
			statusInt = Integer.valueOf(statusInterval).intValue();
		} catch (NumberFormatException nfe) {
			return statusInt;
		}
		return statusInt;
	}
	
	public int userPingInterval(){
		String pingInterval = SystemConfiguration.getProperty("USER PING INTERVAL", "30");
		int pingInt = 30;
		try {
			pingInt = Integer.valueOf(pingInterval).intValue();
		} catch (NumberFormatException nfe) {
			return pingInt;
		}
		return pingInt;
	}
	
	public int accountRefreshCacheInterval(){
		String cacheInterval = SystemConfiguration.getProperty("ACCOUNT REFRESH CACHE", "120");
		int cacheInt = 120;
		try {
			cacheInt = Integer.valueOf(cacheInterval).intValue();
		} catch (NumberFormatException nfe) {
			return cacheInt;
		}
		return cacheInt;
	}
	
	public boolean isAccountRefreshCacheOn() {
		return accountRefreshCacheInterval() != 0;
	}
	
	public int getGSIPPort() {
		int gsipPort = 8000;
		String gsipPortStr = SystemConfiguration.getProperty("GSIP PORT", gsipPort + "");
		
		try {
			gsipPort = Integer.valueOf(gsipPortStr).intValue();
		} catch (NumberFormatException nfe) {
			return gsipPort;
		}
		return gsipPort;
	}
	
	public String getServerName(){
		String serverName = SystemConfiguration.getProperty("SERVERNAME", "");
		return serverName;
	}
	
	public int getMaxClients(){
		int maxClients = 0;
		String maxClientsStr = SystemConfiguration.getProperty("MAX CLIENTS", maxClients + "");
		
		try {
			maxClients = Integer.valueOf(maxClientsStr).intValue();
		} catch (NumberFormatException nfe) {
			return maxClients;
		}
		return maxClients;
	}
	
	public String getAdminAccount(){
		return SystemConfiguration.getProperty("ADMIN","admin");
	}
	
	public boolean getGSIPSOn(){
		return SystemConfiguration.getProperty("GSIP SERVER", "yes").equalsIgnoreCase("yes");
	}
	
	public boolean clientSignupEnabled(){
		return SystemConfiguration.getProperty("CLIENT SIGNUP", "no").equalsIgnoreCase("yes");
	}
	
	public int getMaxChatrooms(){
		int maxChatrooms = 0;
		String maxChatroomStr = SystemConfiguration.getProperty("MAX CHATROOMS", maxChatrooms + "");
		
		try {
			maxChatrooms = Integer.valueOf(maxChatroomStr).intValue();
		} catch (NumberFormatException nfe) {
			return maxChatrooms;
		}
		return maxChatrooms;
	}
	
	public int getMaxInChatrooms(){
		int maxInChatrooms = 0;
		String maxInChatroomStr = SystemConfiguration.getProperty("MAX IN CHATROOM", maxInChatrooms + "");
		
		try {
			maxInChatrooms = Integer.valueOf(maxInChatroomStr).intValue();
		} catch (NumberFormatException nfe) {
			return maxInChatrooms;
		}
		return maxInChatrooms;
	}
	
	public boolean getIsLogging(){
		return SystemConfiguration.getProperty("LOG", "no").equalsIgnoreCase("yes");
	}
	
	public String getLogFileName(){
		return SystemConfiguration.getProperty("LOGFILE", "gtadsLog.log");
	}
	
	public String getBannedFileName(){
		return SystemConfiguration.getProperty("BAN FILE");
	}
	
	public int getBanFileRefreshCache(){
		int interval = 0;
		String intervalStr = SystemConfiguration.getProperty("BAN FILE REFRESH CACHE", interval + "");
		
		try {
			interval = Integer.valueOf(intervalStr).intValue();
		} catch (NumberFormatException nfe) {
			return interval;
		}
		return interval;
	}
	
	public boolean getIsPublish(){
		return SystemConfiguration.getProperty("PUBLISH", "no").equalsIgnoreCase("yes");
	}
	
	public String getPublishURL(){
		return SystemConfiguration.getProperty("PUBLISH URL", "http://localhost/null.pl");
	}
	
	public int getPublishInterval(){
		//publish refresh interval
		int publishInterval = 0;
		String maxInChatroomStr = SystemConfiguration.getProperty("PUBLISH REFRESH INTERVAL", publishInterval + "");
		
		try {
			publishInterval = Integer.valueOf(maxInChatroomStr).intValue();
		} catch (NumberFormatException nfe) {
			return publishInterval;
		}
		return publishInterval;
	}
	
	public String getHostName(){
		return SystemConfiguration.getProperty("HOSTNAME", "I DON'T SET MY CONFIG FILES");
	}
	
	public void setGenericValues(String key, String value, Socket adminSocket) throws IOException{
		if (key != null && value != null){
			SystemConfiguration.setProperty(key.toUpperCase(),value);
			
			MetaData sendOkMessageHeader = new MetaData(MetaData.FROM_SERVER, MetaData.CHATROOM, MetaData.NO_SESSION_NAME);
			MessageAdapter.sendData(null,adminSocket,sendOkMessageHeader,"Property " + key + " value set to: " + value);
		}
	}
	
	public String[] getGenericValues(String keyObject){
		String[] keyValues = new String[2];
		
		if (keyObject != null){
			keyValues = keyObject.split(",");
			if (keyValues.length <= 1)
				keyValues = null;
		}
		return keyValues;
	}
	
	public void displayConfigValues(Socket adminSocket) throws IOException{
		MetaData configHeader = new MetaData(MetaData.FROM_SERVER, MetaData.CHATROOM, MetaData.NO_SESSION_NAME);
		Enumeration configEnum = SystemConfiguration.keys();
		
		while (configEnum.hasMoreElements()){
			String configKey = (String)configEnum.nextElement();
			String configValue = SystemConfiguration.getProperty(configKey, "default or blank");
			MessageAdapter.sendData(null,adminSocket,configHeader,configKey +" = "+ configValue);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -