📄 server.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 + -