public void run() { try { while (((fromClientObject = in.readObject()) != null) && connection.isConnected()) { if (fromClientObject.getClass() == String.class) { synchronized (players) { String playerName = (String) fromClientObject; players.addPlayer(ID, playerName, 0); broadcast(players); } } if (fromClientObject.getClass() == Integer.class) { synchronized (players) { int tableSize = (int) fromClientObject; players.changeTableSize(ID, tableSize); broadcast(players); } } if (fromClientObject.getClass() == ClickEvent.class) { ClickEvent click = (ClickEvent) fromClientObject; ctrl.receiveClick(click); } checkToGameStart(); } } catch (Exception e) { ctrl.serverError("Client disconnected!"); } finally { try { System.out.println(ID); synchronized (receiverList) { Collection<ReceiverThread> found = Collections.synchronizedList(new ArrayList<ReceiverThread>()); for (ReceiverThread actualReceiver : receiverList) { if (actualReceiver.getID() == ID) { found.add(actualReceiver); } } receiverList.removeAll(found); } synchronized (players) { players.removePlayer(ID); } broadcast(players); connection.close(); } catch (IOException e) { ctrl.serverError("Error while closing connection!"); } } }
void broadcast(Scores scoreTable) { for (ReceiverThread actualThread : receiverList) { actualThread.send(scoreTable); } }
void broadcast(GameInfo gameInfo) { for (ReceiverThread actualThread : receiverList) { actualThread.send(gameInfo); } }
void broadcast(TimeStamp timeStamp) { for (ReceiverThread actualThread : receiverList) { actualThread.send(timeStamp); } }
void broadcast(PlayersList players) { for (ReceiverThread actualThread : receiverList) { actualThread.send(players); } }