home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-03 | 11.6 KB | 317 lines |
- package borland.samples.apps.chess.client ;
-
- import java.net.*;
- import java.io.*;
-
- import java.util.*;
- /**Receives messages from the ChessServer
- * This thread is created at logon and listens and processes messages from the server
- */
- public class ClientListener extends Thread {
- ChessViewer app;
- String msg;
- Socket kkSocket;
- DataInputStream is;
- PlayerListListener listListener = null;
-
- public ClientListener(ChessViewer app,Socket kkSocket) {
- super("ClientListener");
- this.kkSocket = kkSocket;
- this.app = app;
- }
-
- public ClientListener() {
- super("ClientListener");
- }
-
- public void run() {
- try{
- String fromServer;
- DataInputStream is = new DataInputStream(kkSocket.getInputStream());
- Reader reader = new InputStreamReader(is);
- BufferedReader bufferedReader = new BufferedReader(reader);
- int blank = 32;
- int offset = 0;
- ServerMessage smsg = new ServerMessage(0,"","");
- while (true) {
- if ((fromServer = bufferedReader.readLine()) != null) {
- System.out.println("CV Received: " + fromServer);
- offset = fromServer.indexOf(blank);
- if (offset > 0) {
- smsg.msgid = fromServer.substring(0,offset);
- smsg.msg = fromServer.substring(offset+1); //don't want the space char
- }
- else {
- smsg.msgid = fromServer;
- smsg.msg = null;
- }
- if (smsg.msgid.equals("Bye")) {
- app.logonEnable(false); //disable the world
- break;
- }
- else
- if (smsg.msgid.equals("Token")) {
- app.moveToken = true;
- }
- else
- if (smsg.msgid.equals("Refused")) {
- app.statusLine.setText(CVS.OFFER_REFUSED);
- }
- else
- if (smsg.msgid.equals("Status")) {
- if (app.pDialog != null)
- app.pDialog.badLogon();
- app.statusLine.setText(smsg.msg);
- }
- else
- if (smsg.msgid.equals("Chat")) {
- app.infoText.append(smsg.msg + "\n");
- }
- else
- if (smsg.msgid.equals("Welcome")) {
- if (app.pDialog != null)
- app.pDialog.goodLogon();
- app.statusLine.setText(CVS.WELCOME + app.myName);
- app.libraryList.removeAll();
- app.libraryListContents.removeAllElements();
- app.logonEnable(true);
- synchronized (app.sender) {
- app.sender.msgque.addElement(new ServerMessage(0,"List",""));
- app.sender.notify();
- }
- }
- else
- if (smsg.msgid.equals("List")) {
- app.setListButtons(ChessViewer.PLAYERLIST) ;
- app.fillList(smsg.msg);
- //app.sbChoose.enable(false);
- }
- if (smsg.msgid.equals("PlayerList")) {
- if (listListener != null)
- listListener.listMessage(smsg.msg);
-
- }
- else
- if (smsg.msgid.equals("LibraryList")) {
- app.fillList(smsg.msg);
- app.setListButtons(ChessViewer.LIBRARYLIST);
- }
- else
- if (smsg.msgid.equals("MoveList")) {
- app.game.init();
- app.game.parsePGNFile (new StringReader(smsg.msg));
- app.playerTime[0] = app.game.getWhiteTimeRemaining();
- app.playerTime[1] = app.game.getBlackTimeRemaining();
- app.lMoveTime = app.game.getMoveTimeInterval();
- app.setNames();
- app.fillMoveList();
- app.movecount = app.count;
- app.setListButtons(ChessViewer.KIBITZ) ;
- app.listPanel.setSelectedTab(ChessViewer.MOVELIST_LABEL);
- app.displayPos();
- System.out.println("ML chessboard disabled");
- app.theboard.setEnabled(false);
- }
- else
- if (smsg.msgid.equals("Resume")) {
- app.game.init();
- app.game.parsePGNFile (new StringReader(smsg.msg));
- app.color = app.game.getWhoMovedLast();
- app.playerTime[0] = app.game.getWhiteTimeRemaining();
- app.playerTime[1] = app.game.getBlackTimeRemaining();
- app.lMoveTime = app.game.getMoveTimeInterval();
- app.setNames();
- if (app.game.getPlayerW().equals(app.myName)) {
- app.yourColor = ChessRules.White;
- app.opponentName = app.game.getPlayerB() ;
- }
- else {
- app.yourColor = ChessRules.Black;
- app.opponentName = app.game.getPlayerW() ;
- }
- app.fillMoveList();
- app.movecount = app.count;
- app.setListButtons(ChessViewer.MOVELIST) ;
- app.setGameButtons();
- app.hasListeners = true;
- app.displayPos();
- app.moveToken = true;
- System.out.println("ML chessboard enabled");
- String result = app.game.getResult();
- if ( result != null){ //rare
- app.gameOver();
- app.statusLine.setText(result);
- }
- else {
- app.theboard.setEnabled(true);
- app.gameTime.setEnabled(false);
- app.moveTime.setEnabled(false);
- app.moveTime.setText(String.valueOf(app.lMoveTime/1000));
- if (app.clock != null)
- app.clock.stop();
- app.clock = new Thread(app);
- app.clock.start();
- }
- }
- else
- if (smsg.msgid.equals("LibraryGame")) {
- getLibraryGame(smsg.msg);
- }
- else
- if (smsg.msgid.equals("Resigned")) {
- //app.setListButtons(ChessViewer.KIBITZ);
- app.game.setComment(app.movecount,app.color,smsg.msg);
- app.statusLine.setText(smsg.msg);
- app.gameOver();
- }
- else
- if (smsg.msgid.equals("Moved")) {
- app.addMove(smsg.msg);
- }
- if (smsg.msgid.equals("TokenMove")) {
- app.tokenMove(smsg.msg);
- }
- else
- if (smsg.msgid.equals("AcceptAbort")) {
- app.statusLine.setText(CVS.GAME_ABORTED);
- app.game.setComment(app.movecount,app.color,"Aborted");
- app.gameOver();
- app.sbChoose.setEnabled(false);
- }
- else
- if (fromServer.startsWith("AcceptDraw")) {
- app.gameOver();
- app.game.setComment(app.movecount,app.color,"1/2-1/2");
- app.displayPos();
- app.statusLine.setText(CVS.GAME_DRAWN);
- }
- else
- if (smsg.msgid.equals("GameAccepted")) {
- String parm[] = PersistString.parse(smsg.msg);
- app.opponentName = app.challenger;
- if (parm[0].equals("black")) {
- app.yourColor = ChessRules.White;
- app.newGame(app.myName,app.opponentName);
- app.game.setWhiteElo(parm[3]);
- app.game.setBlackElo(parm[5]);
- }
- else
- if (parm[0].equals("white")) {
- app.yourColor = ChessRules.Black;
- app.newGame(app.opponentName,app.myName);
- app.game.setWhiteElo(parm[5]);
- app.game.setBlackElo(parm[3]);
- }
- app.game.setSite(CVS.BORLAND_CHESS_CLUB);
- app.game.setRound("-");
- app.game.setEvent(CVS.RATED_GAME);
- //Date today = new Date();
- java.util.GregorianCalendar today = new GregorianCalendar();
- app.game.setDate(String.valueOf(today.get(Calendar.YEAR))+ "." +
- String.valueOf(today.get(Calendar.MONTH)+1) + "." +
- String.valueOf(today.get(Calendar.DAY_OF_MONTH)));
-
- }
- else
- if (smsg.msgid.equals("Information")) {
- System.out.println("Information");
-
- String[] info = PersistString.parse(smsg.msg);
- if (listListener == null) {
- InfoDlg dlg = new InfoDlg(app,info);
- dlg.setBounds(0,0,400,400); //430 was too wide
- //pack();
- dlg.setLocation(300,200);
- dlg.show();
- }
- else
- listListener.displayData(info);
- }
- else
- if (smsg.msgid.equals("Info")) {
- System.out.println("Info");
- String[] info = PersistString.parse(smsg.msg);
- app.infoText.append(info[1] + ": " + info[3] + "\r\n");
- for (int i=4;i < 7 && i<info.length && info[i] != null ;i++)
- if (!info[i].startsWith(" "))
- app.infoText.append(info[i] + "\r\n");
- }
- else
- if (smsg.msgid.equals("OfferDraw")) {
- synchronized (this){
- DrawDlg dlg = new DrawDlg(app.f,smsg.msgid,app);
- dlg.pack();
- dlg.setBounds(200,200,300,170);
- dlg.show();
- }
- }
- else
- if (smsg.msgid.equals("OfferAbort")) {
- synchronized (this){
- DrawDlg dlg = new DrawDlg(app.f,smsg.msgid,app);
- dlg.pack();
- dlg.setBounds(200,200,300,170);
- dlg.show();
- }
- }
- else
- if (smsg.msgid.equals("Challenged")) {
- String[] parm = PersistString.parse(smsg.msg);
- synchronized (this){
- int index = smsg.msg.indexOf(' ');
- int index2 = smsg.msg.indexOf(' ',index+1);
- try {
- //app.GameTime = Integer.parseInt(smsg.msg.substring(0,index));
- app.gameTimeValue = Integer.parseInt(parm[0]);
- app.playerTime[0] = app.gameTimeValue * 60000;
- app.playerTime[1] = app.playerTime[0];
- //app.lMoveTime = Integer.parseInt(smsg.msg.substring(index+1,index2)) * 1000;
- app.lMoveTime = Integer.parseInt(parm[1]);
- }
- catch (NumberFormatException e) {}
- // String timeControl = CVS.GAME_IN + parm[0] + CVS.MINUTES_PLUS +
- // parm[1] + CVS.SECONDS_PER_MOVE;
-
- //app.challenger = smsg.msg.substring(index2+1);
- app.challenger = parm[2];
- WannaPlay wannaplay = new WannaPlay(app.f,app,parm);
- wannaplay.pack();
- wannaplay.setBounds(200,200,350,170);
- wannaplay.show();
- }
- }
- } // end if readline != null
- } // end while(true)
- //is.close();
- app.sendMsg("Dead","null line");
- System.out.println("bclient shutting down, received null line");
- }
- catch (Exception e) {
- if (app.sender != null) {
- app.sendMsg("Dead",e.toString());
- app.statusLine.setText(CVS.LOST_CONNECTION_ + e) ;
- e.printStackTrace();
- app.logonEnable(false);
- System.err.println("CV Exception: " + e);
- }
- }
- try {
- is.close();
- kkSocket.close();
- }
- catch (Exception e) {}
- } //end of method run
-
- void getLibraryGame(String msg){
- int markerIndex = msg.indexOf('?'); //games can have question marks but not game filenames
- String name = msg.substring(0,markerIndex);
- String game = msg.substring(markerIndex+1);
- app.libraryHash.put(name,game);
- if (app.showLibraryGame)
- app.getGame(name);
- else
- app.statusLine.setText(CVS.RECEIVED_ + name);
- }
- } //end of class ClientListener
-
-