// Processed by NMI's Java Code Viewer 4.8.2 © 1997-2000 B. Lemaire // Website: http://njcv.htmlplanet.com E-mail: info@njcv.htmlplanet.com // Copy registered to Evaluation Copy // Source File Name: ClientHandler.java import java.awt.Event; import java.io.*; import java.net.Socket; import java.util.Random; import java.util.Vector; class ClientHandler extends Thread { Socket socket; DataInputStream is; DataOutputStream os; MultiServer target; static boolean checkNick_available = true; public int myNumber; String inputLine; int intIn; char ch; boolean gameDataRecievedOk; public boolean needToClose; public boolean multiServerEndedGame; public int clientDisconnected; public int clientToBeKicked; ClientHandler(MultiServer multiserver, Socket socket1, DataOutputStream dataoutputstream, DataInputStream datainputstream, int i) { super("Client Handler"); gameDataRecievedOk = true; needToClose = false; multiServerEndedGame = false; clientDisconnected = -1; clientToBeKicked = -1; target = multiserver; myNumber = i; socket = socket1; os = dataoutputstream; is = datainputstream; } public void run() { try { os.writeInt(myNumber); } catch(IOException _ex) { closeClientHandler(114); } intIn = readInt(); if(intIn == 111) { if(checkNick()) { closeClientHandler(113); } else { ServerData.validPlayer[myNumber] = true; target.handleEvent(new Event(this, 0L, 1001, 155, 0, 0, 0, "")); } } else { try { os.writeInt(133); } catch(IOException _ex) { closeClientHandler(114); } } _L2: int i; for(i = 0; i == 0 && !ServerData.needToSendGameData[myNumber] && clientDisconnected == -1 && !needToClose && !ServerData.multiServerStartedGame && clientToBeKicked == -1;) { try { i = is.available(); } catch(IOException _ex) { closeClientHandler(114); } try { Thread.sleep(5L); } catch(InterruptedException _ex) { closeClientHandler(113); } } if(!ServerData.multiServerStartedGame) { if(needToClose) closeClientHandler(113); if(clientToBeKicked != -1) { try { os.writeInt(190); os.writeInt(clientToBeKicked); } catch(IOException _ex) { closeClientHandler(114); } clientToBeKicked = -1; } if(clientDisconnected != -1) { try { os.writeInt(114); os.writeBoolean(false); os.writeInt(clientDisconnected); ServerData.needToSendGameData[myNumber] = true; } catch(IOException _ex) { closeClientHandler(114); } clientDisconnected = -1; } if(ServerData.needToSendGameData[myNumber] && i == 0) { try { os.writeInt(116); os.writeInt(myNumber); os.writeBoolean(ServerData.isSpellerUsed); os.writeBoolean(ServerData.isTimeLimitUsed); os.writeInt(ServerData.timeLimit); os.writeInt(ServerData.clientsCurrentlyConnected); } catch(IOException _ex) { closeClientHandler(114); } sendLetterValues(); sendNicks(); try { gameDataRecievedOk = is.readBoolean(); } catch(IOException _ex) { closeClientHandler(114); } if(gameDataRecievedOk) ServerData.needToSendGameData[myNumber] = false; else gameDataRecievedOk = true; } if(i != 0) { intIn = readInt(); switch(intIn) { case 112: // 'p' inputLine = ""; inputLine = readLine(); intIn = readInt(); target.handleEvent(new Event(this, 0L, 1001, 112, myNumber, intIn, 0, inputLine)); break; case 114: // 'r' closeClientHandler(114); break; } if(false) ; } continue; /* Loop/switch isn't completed */ } try { os.writeInt(127); for(int j = 0; j < ServerData.clientsCurrentlyConnected; j++) os.writeInt(ServerData.playersOrder[j]); for(int l = 0; l < 15; l++) { for(int l2 = 0; l2 < 15; l2++) os.writeChar(ServerData.oldBoardLetters[l][l2]); } for(int i3 = 0; i3 < C.MAX_NUM_OF_PLAYERS; i3++) os.writeInt(ServerData.playerScore[i3]); for(int j4 = 0; j4 < 7; j4++) os.writeChar(ServerData.bankLetters[myNumber][j4]); } catch(IOException _ex) { closeClientHandler(114); } _L4: int k; label0: { for(k = 0; k == 0 && !ServerData.needToSendGameData[myNumber] && !ServerData.needToSendTurnData[myNumber] && !needToClose && clientDisconnected == -1 && !multiServerEndedGame && !ServerData.newTurn[myNumber];) { try { k = is.available(); } catch(IOException _ex) { closeClientHandler(114); } try { Thread.sleep(5L); } catch(InterruptedException _ex) { closeClientHandler(113); } } if(needToClose) closeClientHandler(113); if(ServerData.newTurn[myNumber]) { ServerData.newTurn[myNumber] = false; try { if(myNumber == ServerData.playersOrder[ServerData.whoseTurn]) os.writeInt(169); else os.writeInt(167); os.writeInt(ServerData.playersOrder[ServerData.whoseTurn]); } catch(IOException _ex) { closeClientHandler(114); } } if(!multiServerEndedGame) break label0; try { os.writeInt(172); os.writeInt(ServerData.winner); } catch(IOException _ex) { closeClientHandler(114); } multiServerEndedGame = false; } if(true) goto _L2; else goto _L1 _L1: if(clientDisconnected != -1) { try { os.writeInt(114); if(ServerData.endTurnStatus == 170) os.writeBoolean(true); else os.writeBoolean(false); os.writeInt(clientDisconnected); ServerData.needToSendGameData[myNumber] = true; } catch(IOException _ex) { closeClientHandler(114); } clientDisconnected = -1; } if(ServerData.needToSendGameData[myNumber] && k == 0) { try { os.writeInt(116); os.writeInt(ServerData.clientsCurrentlyConnected); for(int i1 = 0; i1 < ServerData.clientsCurrentlyConnected; i1++) os.writeInt(ServerData.playersOrder[i1]); } catch(IOException _ex) { closeClientHandler(114); } sendNicks(); try { gameDataRecievedOk = is.readBoolean(); } catch(IOException _ex) { closeClientHandler(114); } if(gameDataRecievedOk) ServerData.needToSendGameData[myNumber] = false; else gameDataRecievedOk = true; } if(ServerData.needToSendTurnData[myNumber]) { try { os.writeInt(168); os.writeInt(ServerData.endTurnStatus); switch(ServerData.endTurnStatus) { case 162: case 163: default: break; case 160: for(int j1 = 0; j1 < 15; j1++) { for(int j3 = 0; j3 < 15; j3++) os.writeChar(ServerData.oldBoardLetters[j1][j3]); } for(int k1 = 0; k1 < ServerData.words.size(); k1++) { os.writeInt(((String)ServerData.words.elementAt(k1)).length()); os.writeChars((String)ServerData.words.elementAt(k1)); } os.writeInt(126); os.writeInt(ServerData.turnScore); if(myNumber != ServerData.playersOrder[ServerData.whoseTurn]) break; for(int l1 = 0; l1 < 7; l1++) try { ServerData.bankLetters[myNumber][l1] = is.readChar(); } catch(IOException _ex) { closeClientHandler(114); } newBankLetters(); for(int k3 = 0; k3 < 7; k3++) try { os.writeChar(ServerData.bankLetters[myNumber][k3]); } catch(IOException _ex) { closeClientHandler(114); } break; case 161: ServerData.endTurnStatus = 166; break; } } catch(IOException _ex) { closeClientHandler(114); } ServerData.needToSendTurnData[myNumber] = false; } if(k == 0) goto _L4; else goto _L3 _L3: intIn = is.readInt(); intIn; JVM INSTR lookupswitch 5: default 729 // 112: 1560 // 114: 1623 // 162: 1632 // 163: 1701 // 164: 1731; goto _L5 _L6 _L7 _L8 _L9 _L10 _L5: if(true) goto _L4; else goto _L11 _L11: _L6: inputLine = ""; inputLine = readLine(); intIn = is.readInt(); target.handleEvent(new Event(this, 0L, 1001, 112, myNumber, intIn, 0, inputLine)); goto _L4 _L7: closeClientHandler(114); goto _L4 _L8: nullifyBankLetters(); newBankLetters(); for(int i2 = 0; i2 < 7; i2++) os.writeChar(ServerData.bankLetters[myNumber][i2]); target.handleEvent(new Event(this, 0L, 1001, 162, 0, 0, 0, null)); goto _L4 _L9: target.handleEvent(new Event(this, 0L, 1001, 163, 0, 0, 0, null)); goto _L4 _L10: for(int j2 = 0; j2 < 15; j2++) { for(int l3 = 0; l3 < 15; l3++) ServerData.newBoardLetters[j2][l3] = Character.toUpperCase(ServerData.oldBoardLetters[j2][l3]); } do { boolean flag = false; boolean flag1 = false; boolean flag2 = false; try { int k2 = readInt(); if(k2 == 126) break; int i4 = readInt(); char c = is.readChar(); ServerData.newLetterPresent[k2][i4] = true; ServerData.newBoardLetters[k2][i4] = Character.toUpperCase(c); } catch(IOException _ex) { closeClientHandler(114); } } while(true); target.handleEvent(new Event(this, 0L, 1001, 164, 0, 0, 0, null)); goto _L4 IOException ioexception; ioexception; ioexception.printStackTrace(); goto _L4 } public synchronized boolean checkNick() { while(!checkNick_available) try { wait(); } catch(InterruptedException _ex) { closeClientHandler(113); } checkNick_available = false; String s = ""; try { String s1 = readLine(); for(int i = 0; i < C.MAX_NUM_OF_PLAYERS; i++) if(ServerData.validPlayer[i] && ServerData.nicks[i].equals(s1)) { os.writeInt(131); ChatArea.addText(">>> Player tried to connect with existing nick : " + s1, 141); checkNick_available = true; notifyAll(); return true; } ServerData.nicks[myNumber] = s1; os.writeInt(132); checkNick_available = true; notifyAll(); return false; } catch(IOException ioexception) { ioexception.printStackTrace(); } return true; } public void sendNicks() { for(int i = 0; i < C.MAX_NUM_OF_PLAYERS; i++) if(ServerData.validPlayer[i]) try { os.writeInt(i); String s = ServerData.nicks[i]; os.writeInt(s.length()); os.writeChars(ServerData.nicks[i]); } catch(IOException ioexception) { ioexception.printStackTrace(); gameDataRecievedOk = false; } try { os.writeInt(126); return; } catch(IOException ioexception1) { ioexception1.printStackTrace(); } gameDataRecievedOk = false; } public void sendLetterValues() { for(int i = 0; i < 26; i++) try { os.writeInt(ServerData.letterValues[i]); } catch(IOException ioexception) { ioexception.printStackTrace(); gameDataRecievedOk = false; } } public void sendMessage(String s) { try { os.writeInt(112); os.writeInt(s.length()); os.writeChars(s); return; } catch(IOException ioexception) { ioexception.printStackTrace(); } } public int readInt() { int i = 0; try { i = is.readInt(); } catch(IOException ioexception) { ChatArea.addText("Error reading connectionStatus : " + ioexception.getMessage(), 141); closeClientHandler(114); } return i; } private String readLine() { boolean flag = false; StringBuffer stringbuffer = new StringBuffer(""); try { int i = is.readInt(); for(int j = 0; j < i; j++) stringbuffer.append(is.readChar()); } catch(IOException ioexception) { ChatArea.addText("Error in readLine : " + ioexception.getMessage(), 141); closeClientHandler(114); } return stringbuffer.toString(); } protected synchronized void newBankLetters() { Random random = new Random(); float f = 0.0F; boolean flag = false; for(int j = 0; j < 7; j++) if(ServerData.bankLetters[myNumber][j] == 0) { float f1 = random.nextFloat() * 4923F * (float)(0x3 ^ myNumber + 34324 * (int)random.nextFloat()); int i = (int)f1 % 27; if(i == 26) ServerData.bankLetters[myNumber][j] = '*'; else ServerData.bankLetters[myNumber][j] = (char)(65 + i); } } public void nullifyBankLetters() { for(int i = 0; i < 7; i++) ServerData.bankLetters[myNumber][i] = '\0'; } public synchronized void closeClientHandler(int i) { if(os != null && socket != null) try { if(i == 113) os.writeInt(113); os.close(); is.close(); socket.close(); } catch(IOException ioexception) { ioexception.printStackTrace(); } ServerData.needToSendGameData[myNumber] = false; ServerData.nicks[myNumber] = null; ServerData.connected[myNumber] = false; ServerData.validPlayer[myNumber] = false; target.handleEvent(new Event(this, 0L, 1001, 153, 0, 0, 0, Integer.toString(myNumber))); stop(); } }