Ho un server che comunica con + client e se entro certo tempo non avviene una nuova connessione inizia la partita con i client connessi.
Io avevo pensato di fare una sleep al server, però poi non ho idea di come risvegliarlo se si connette nuovo client.
Avete delle idee??
codice:
public class Server {
private Giocatore giocatore;
private List<ServerThread> chatClientHandlers = new CopyOnWriteArrayList<ServerThread>();
int num=0;
private Scanner in;
private PrintWriter out;
public void startServer(int port) throws IOException, InterruptedException {
ExecutorService executor = Executors.newCachedThreadPool();
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server avviato normalmente.");
try {
while (true) {
String colorePartita="BIANCO";
num++;
giocatore=getGiocatore(num);
/*Thread.sleep(1000);*/
Socket socket = serverSocket.accept();
ServerThread clientHandler = new ServerThread(socket, chatClientHandlers, serverSocket,giocatore);
chatClientHandlers.add(clientHandler);
executor.submit(clientHandler);
//si connette secondo giocatore parte timeout.
if(num==maxGiocatori){
for (ServerThread c:chatClientHandlers) {
c.partitaIniziata(colorePartita);
}}
}
} catch (IOException e) {
serverSocket.close();
executor.shutdown();
for (ServerThread c:chatClientHandlers) {
c.getSocket().close();
}
chatClientHandlers.clear();
}
}
public static void main(String[] args) throws InterruptedException {
Server server = new Server();
try {
server.startServer(4747);
System.out.println("Server terminato normalmente.");
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}