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

📄 server.java

📁 JAVA实现的聊天工具,可以容纳最多10个用户 1.本系统需要JDK1.5 或更高版本的支持。 2.serverDatabase为服务器端的数据文件. 若使用现有数据,可用帐号:1, 密码
💻 JAVA
字号:
import java.io.IOException;

public class Server{	
	private ServerDatabase serverDatabase=null; //store all the info about clients
	
	private Thread threadPoolBuilder;
	private int threadPoolSize;	
	private Thread logInThread; //ServerLogInGUI
	private Thread controlThread;
	private boolean hasAdm;  //has adm or not
    
    private static Server aServer=null;
    
    public static void main(String[] args) throws IOException {
    	aServer=new Server();
    	aServer.loadDatabase();
    	if(aServer.serverDatabase.getAdmList().size()==0)
    		aServer.hasAdm=true;
    	else
    		aServer.hasAdm=false;
    	
    	if(!aServer.hasAdm){
    		//log in first
    		aServer.logInThread=new Thread(new ServerLogInGUI(),"ServerLogInGUIThread");
    		aServer.logInThread.start();
    	}
    	else //if database has no adm info,then no log-in
    		aServer.initiate(null);
    }
    
    private void loadDatabase(){
    	if(!ServerIO.checkFile() || ServerIO.readServerDatabase()==null){ //check if this is the initial start
        	serverDatabase=new ServerDatabase();
        	//System.out.println("test");
        }else{
        	serverDatabase=ServerIO.readServerDatabase();
        	//System.out.println("test");
        }
    	threadPoolSize=serverDatabase.getServerThreadNum();
    }
    
    void initiate(Administrator currentAdm){
    	if(!hasAdm)
    		logInThread.stop();
    	//System.out.println("state:"+logInThread.isAlive());
    	threadPoolBuilder=new Thread(new ServerThreadPoolBuilder(threadPoolSize,currentAdm),"serverThreadPoolBuilder");
    	threadPoolBuilder.start();
    	
        //System.out.println("test");
    }
	
	static ServerDatabase getServerDatabase(){
		return aServer.serverDatabase;
	}
	
	static Server getServer(){
		return aServer;
	}	
	
	//shutdown sequence:ServerControlGUI->ServerThread.exit()->ServerThreadPoolBuilder->Server.exit()
	static void exit(){  //called by control thread
		//aServer.controlThread.stop();
		/*ServerThread.exit(); //inform all client that server is shutdown and shut down thread-pool at last		
		ServerIO.writeServerDatabase(aServer.serverDatabase);*/
		System.exit(0);
	}
}

⌨️ 快捷键说明

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