示例#1
0
    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!");
        }
      }
    }
示例#2
0
 void broadcast(Scores scoreTable) {
   for (ReceiverThread actualThread : receiverList) {
     actualThread.send(scoreTable);
   }
 }
示例#3
0
 void broadcast(GameInfo gameInfo) {
   for (ReceiverThread actualThread : receiverList) {
     actualThread.send(gameInfo);
   }
 }
示例#4
0
 void broadcast(TimeStamp timeStamp) {
   for (ReceiverThread actualThread : receiverList) {
     actualThread.send(timeStamp);
   }
 }
示例#5
0
 void broadcast(PlayersList players) {
   for (ReceiverThread actualThread : receiverList) {
     actualThread.send(players);
   }
 }