package thread;
|
|
|
|
import java.io.IOException;
|
|
import java.net.Socket;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
import java.util.ResourceBundle;
|
|
|
|
import generic.Status;
|
|
import gui.GUI;
|
|
import network.Server;
|
|
|
|
/**
|
|
* Thread dedicated to create {@link ClientThread} upon client connection.
|
|
*/
|
|
public class ServerThread extends Thread{
|
|
private Server server;
|
|
private Boolean accept;
|
|
private GUI gui;
|
|
private List<ClientThread> clients;
|
|
|
|
public Server getServer() {
|
|
return server;
|
|
}
|
|
public void setServer(Server server) {
|
|
this.server = server;
|
|
}
|
|
public GUI getGui() {
|
|
return gui;
|
|
}
|
|
public void setGui(GUI gui) {
|
|
this.gui = gui;
|
|
}
|
|
public Boolean getAccept() {
|
|
return accept;
|
|
}
|
|
public void setAccept(Boolean accept) {
|
|
this.accept = accept;
|
|
}
|
|
|
|
public ServerThread(Server server){
|
|
super();
|
|
this.clients=new ArrayList<ClientThread>();
|
|
this.server=server;
|
|
this.accept=true;
|
|
this.gui=null;
|
|
}
|
|
public ServerThread(Server server,GUI gui){
|
|
super();
|
|
this.server=server;
|
|
this.accept=true;
|
|
this.gui=gui;
|
|
}
|
|
|
|
@Override
|
|
public void run(){
|
|
accept=true;
|
|
Status<Integer,IOException> serverStartStatus=server.start();
|
|
if(serverStartStatus.success) {
|
|
System.out.println(ResourceBundle.getBundle("bundle/Bundle",Locale.getDefault()).getString("serverStart"));
|
|
if(gui!=null) gui.displayServerStart();
|
|
while(accept) {
|
|
Status<Socket,IOException> connectionAttempt=server.accept();
|
|
if(connectionAttempt.success) {
|
|
Socket client=connectionAttempt.payload;
|
|
System.out.println(
|
|
ResourceBundle.getBundle("bundle/Bundle",Locale.getDefault()).getString("clientConnection")
|
|
+" "+client.getInetAddress()+":"+client.getPort()
|
|
+" ("
|
|
+ResourceBundle.getBundle("bundle/Bundle",Locale.getDefault()).getString("localPort")
|
|
+": "+client.getLocalPort()
|
|
+")"
|
|
);
|
|
if(gui!=null) gui.displayStartConnection(client.getInetAddress(),client.getPort(),client.getLocalPort());
|
|
ClientThread ct=new ClientThread(client);
|
|
clients.add(ct);
|
|
ct.start();
|
|
}else {
|
|
if(accept) {
|
|
System.out.println(ResourceBundle.getBundle("bundle/Bundle",Locale.getDefault()).getString("clientConnectionFail"));
|
|
}
|
|
}
|
|
}
|
|
}else {
|
|
System.out.println(ResourceBundle.getBundle("bundle/Bundle",Locale.getDefault()).getString("serverStartFail"));
|
|
System.out.println(serverStartStatus.error.toString());
|
|
return;
|
|
}
|
|
if(gui!=null) gui.displayServerStop();
|
|
System.out.println(ResourceBundle.getBundle("bundle/Bundle",Locale.getDefault()).getString("serverEnd"));
|
|
}
|
|
|
|
/**
|
|
* Stop the current server from accepting new connection.
|
|
* Old connection will remain active.
|
|
* @see #stopClientsSocket()
|
|
*/
|
|
public void stopServerSocket() {
|
|
server.stop();
|
|
accept=false;
|
|
}
|
|
|
|
/**
|
|
* Stop all the client connection.
|
|
* New connection are still possible.
|
|
* @see #stopServerSocket()
|
|
*/
|
|
public void stopClientsSocket() {
|
|
for(int i=0;i<clients.size();i++) {
|
|
clients.get(i).setRun(false);
|
|
}
|
|
}
|
|
}
|