home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 October A / Pcwk10a98.iso / Inprise / TRIAL / JBUILDER / JSAMPLES.Z / ClientListener.java < prev    next >
Text File  |  1998-05-08  |  13KB  |  335 lines

  1. /*
  2.  * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
  3.  * 
  4.  * This SOURCE CODE FILE, which has been provided by Borland as part
  5.  * of a Borland product for use ONLY by licensed users of the product,
  6.  * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
  7.  *
  8.  * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
  9.  * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
  10.  * THE PRODUCT.
  11.  *
  12.  * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
  13.  * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
  14.  * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
  15.  * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
  16.  * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
  17.  * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
  18.  * CODE FILE.
  19.  */
  20. package borland.samples.apps.chess.client ;
  21.  
  22. import java.net.*;
  23. import java.io.*;
  24.  
  25. import java.util.*;
  26. /**Receives messages from the ChessServer
  27.  * This thread is created at logon and listens and processes messages from the server
  28. */
  29. public class ClientListener extends Thread {
  30.   ChessViewer app;
  31.   String msg;
  32.   Socket kkSocket;
  33.   DataInputStream is;
  34.   PlayerListListener listListener = null;
  35.   ResourceBundle res = ResourceBundle.getBundle("borland.samples.apps.chess.client.Res");
  36.  
  37.   public ClientListener(ChessViewer app,Socket kkSocket) {
  38.     super("ClientListener");
  39.     this.kkSocket = kkSocket;
  40.     this.app = app;
  41.   }
  42.  
  43.   public  ClientListener() {
  44.     super("ClientListener");
  45.   }
  46.  
  47.   public  void run() {
  48.     try{
  49.       String fromServer;
  50.       DataInputStream is = new DataInputStream(kkSocket.getInputStream());
  51.       Reader reader = new InputStreamReader(is);
  52.       BufferedReader bufferedReader = new BufferedReader(reader);
  53.       int blank = 32;
  54.       int offset = 0;
  55.       ServerMessage smsg = new ServerMessage(0,"","");
  56.       while (true) {
  57.         if ((fromServer = bufferedReader.readLine()) != null) {
  58.           System.out.println("CV Received: " + fromServer);
  59.           offset = fromServer.indexOf(blank);
  60.           if (offset > 0) {
  61.             smsg.msgid = fromServer.substring(0,offset);
  62.             smsg.msg = fromServer.substring(offset+1); //don't want the space char
  63.           }
  64.           else {
  65.             smsg.msgid = fromServer;
  66.                smsg.msg = null;
  67.           }
  68.           if (smsg.msgid.equals(ServerMessage.BYE)) {
  69.             app.logonEnable(false); //disable the world
  70.             break;
  71.           }
  72.           else
  73.           if (smsg.msgid.equals(ServerMessage.TOKEN)) {
  74.             app.moveToken = true;
  75.           }
  76.           else
  77.           if (smsg.msgid.equals(ServerMessage.REFUSED)) {
  78.             app.statusLine.setText(res.getString("OFFER_REFUSED"));
  79.           }
  80.           else
  81.           if (smsg.msgid.equals(ServerMessage.STATUS)) {
  82.             if (app.pDialog != null)
  83.               app.pDialog.badLogon();
  84.             app.statusLine.setText(smsg.msg);
  85.           }
  86.           else
  87.           if (smsg.msgid.equals(ServerMessage.CHAT)) {
  88.             app.infoText.append(smsg.msg + "\n");
  89.           }
  90.           else
  91.           if (smsg.msgid.equals(ServerMessage.WELCOME)) {
  92.             if (app.pDialog != null)
  93.               app.pDialog.goodLogon();
  94.             app.statusLine.setText(res.getString("WELCOME") + app.myName);
  95.             app.libraryList.removeAll();
  96.             app.libraryListContents.removeAllElements();
  97.             app.logonEnable(true);
  98.             synchronized (app.sender) {
  99.               app.sender.msgque.addElement(new ServerMessage(0,ServerMessage.LIST,""));
  100.               app.sender.notify();
  101.             }
  102.           }
  103.           else
  104.           if (smsg.msgid.equals(ServerMessage.LIST)) {
  105.             app.setListButtons(ChessViewer.PLAYERLIST) ;
  106.             app.fillList(smsg.msg);
  107.             //app.sbChoose.enable(false);
  108.           }
  109.           if (smsg.msgid.equals(ServerMessage.PLAYER_LIST)) {
  110.             if (listListener != null)
  111.               listListener.listMessage(smsg.msg);
  112.  
  113.           }
  114.           else
  115.           if (smsg.msgid.equals(ServerMessage.LIBRARY_LIST)) {
  116.             app.fillList(smsg.msg);
  117.             app.setListButtons(ChessViewer.LIBRARYLIST);
  118.           }
  119.           else
  120.           if (smsg.msgid.equals(ServerMessage.MOVE_LIST)) {
  121.             app.game.init();
  122.             app.game.parsePGNFile (new StringReader(smsg.msg));
  123.             app.playerTime[0] = app.game.getWhiteTimeRemaining();
  124.             app.playerTime[1] = app.game.getBlackTimeRemaining();
  125.             app.lMoveTime = app.game.getMoveTimeInterval();
  126.             app.setNames();
  127.             app.fillMoveList();
  128.             app.movecount = app.count;
  129.             app.setListButtons(ChessViewer.KIBITZ) ;
  130.             app.listPanel.setSelectedTab(app.res.getString("Moves"));
  131.             app.displayPos();
  132.             System.out.println("ML chessboard disabled");
  133.             app.theboard.setEnabled(false);
  134.           }
  135.           else
  136.           if (smsg.msgid.equals(ServerMessage.RESUME)) {
  137.             app.game.init();
  138.             app.game.parsePGNFile (new StringReader(smsg.msg));
  139.             app.color = app.game.getWhoMovedLast();
  140.             app.playerTime[0] = app.game.getWhiteTimeRemaining();
  141.             app.playerTime[1] = app.game.getBlackTimeRemaining();
  142.             app.lMoveTime = app.game.getMoveTimeInterval();
  143.             app.setNames();
  144.             if (app.game.getPlayerW().equals(app.myName)) {
  145.               app.yourColor = ChessRules.White;
  146.               app.opponentName = app.game.getPlayerB() ;
  147.             }
  148.             else {
  149.               app.yourColor = ChessRules.Black;
  150.               app.opponentName = app.game.getPlayerW() ;
  151.             }
  152.             app.fillMoveList();
  153.             app.movecount = app.count;
  154.             app.setListButtons(ChessViewer.MOVELIST) ;
  155.             app.setGameButtons();
  156.             app.hasListeners = true;
  157.             app.displayPos();
  158.             app.moveToken = true;
  159.             System.out.println("ML chessboard enabled");
  160.             String result = app.game.getResult();
  161.             if ( result != null){   //rare
  162.               app.gameOver();
  163.               app.statusLine.setText(result);
  164.             }
  165.             else {
  166.               app.theboard.setEnabled(true);
  167.               app.gameTime.setEnabled(false);
  168.               app.moveTime.setEnabled(false);
  169.               app.moveTime.setText(String.valueOf(app.lMoveTime/1000));
  170.               if (app.clock != null)
  171.                 app.clock.stop();
  172.               app.clock = new Thread(app);
  173.               app.clock.start();
  174.             }
  175.           }
  176.           else
  177.           if (smsg.msgid.equals(ServerMessage.LIBRARY_GAME)) {
  178.             getLibraryGame(smsg.msg);
  179.           }
  180.           else
  181.           if (smsg.msgid.equals(ServerMessage.RESIGNED)) {
  182.             //app.setListButtons(ChessViewer.KIBITZ);
  183.             app.game.setComment(app.movecount,app.color,smsg.msg);
  184.             app.statusLine.setText(smsg.msg);
  185.             app.gameOver();
  186.           }
  187.           else
  188.           if (smsg.msgid.equals(ServerMessage.MOVED)) {
  189.             app.addMove(smsg.msg);
  190.           }
  191.           if (smsg.msgid.equals(ServerMessage.TOKEN_MOVE)) {
  192.              app.tokenMove(smsg.msg);
  193.           }
  194.           else
  195.           if (smsg.msgid.equals(ServerMessage.ACCEPT_ABORT)) {
  196.             app.statusLine.setText(res.getString("GAME_ABORTED"));
  197.             app.game.setComment(app.movecount,app.color,"Aborted");
  198.             app.gameOver();
  199.             app.sbChoose.setEnabled(false);
  200.           }
  201.           else
  202.           if (fromServer.startsWith(ServerMessage.ACCEPT_DRAW)) {
  203.             app.gameOver();
  204.             app.game.setComment(app.movecount,app.color,"1/2-1/2");
  205.             app.displayPos();
  206.             app.statusLine.setText(res.getString("GAME_DRAWN"));
  207.           }
  208.           else
  209.           if (smsg.msgid.equals(ServerMessage.GAME_ACCEPTED)) {
  210.             String parm[] = PersistString.parse(smsg.msg);
  211.             app.opponentName = app.challenger;
  212.             if (parm[0].equals("black")) {
  213.               app.yourColor = ChessRules.White;
  214.               app.newGame(app.myName,app.opponentName);
  215.               app.game.setWhiteElo(parm[3]);
  216.               app.game.setBlackElo(parm[5]);
  217.             }
  218.             else
  219.             if (parm[0].equals("white")) {
  220.               app.yourColor = ChessRules.Black;
  221.               app.newGame(app.opponentName,app.myName);
  222.               app.game.setWhiteElo(parm[5]);
  223.               app.game.setBlackElo(parm[3]);
  224.             }
  225.             app.game.setSite(res.getString("BORLAND_CHESS_CLUB"));
  226.             app.game.setRound("-");
  227.             app.game.setEvent(res.getString("RATED_GAME"));
  228.             //Date today = new Date();
  229.             java.util.GregorianCalendar today = new GregorianCalendar();
  230.             app.game.setDate(String.valueOf(today.get(Calendar.YEAR))+ "." +
  231.                              String.valueOf(today.get(Calendar.MONTH)+1) + "." +
  232.                              String.valueOf(today.get(Calendar.DAY_OF_MONTH)));
  233.  
  234.           }
  235.           else
  236.           if (smsg.msgid.equals(ServerMessage.INFORMATION)) {
  237.             System.out.println("Information");
  238.  
  239.             String[]  info = PersistString.parse(smsg.msg);
  240.             if (listListener == null) {
  241.               InfoDlg dlg = new InfoDlg(app,info);
  242.               dlg.setBounds(0,0,400,400);   //430 was too wide
  243.               //pack();
  244.               dlg.setLocation(300,200);
  245.               dlg.setVisible(true);
  246.             }
  247.             else
  248.               listListener.displayData(info);
  249.           }
  250.           else
  251.           if (smsg.msgid.equals(ServerMessage.INFO)) {
  252.             System.out.println("Info");
  253.             String[]  info = PersistString.parse(smsg.msg);
  254.             app.infoText.append(info[1] + ": " + info[3] + "\r\n");
  255.             for (int i=4;i < 7 && i<info.length && info[i] != null ;i++)
  256.               if (!info[i].startsWith(" "))
  257.                 app.infoText.append(info[i] + "\r\n");
  258.           }
  259.           else
  260.           if (smsg.msgid.equals(ServerMessage.OFFER_DRAW)) {
  261.             synchronized (this){
  262.               DrawDlg dlg = new DrawDlg(app.f,smsg.msgid,app);
  263.               dlg.pack();
  264.               dlg.setBounds(200,200,300,170);
  265.               dlg.setVisible(true);
  266.             }
  267.           }
  268.           else
  269.           if (smsg.msgid.equals(ServerMessage.OFFER_ABORT)) {
  270.             synchronized (this){
  271.               DrawDlg dlg = new DrawDlg(app.f,smsg.msgid,app);
  272.               dlg.pack();
  273.               dlg.setBounds(200,200,300,170);
  274.               dlg.setVisible(true);
  275.             }
  276.           }
  277.           else
  278.           if (smsg.msgid.equals(ServerMessage.CHALLENGED)) {
  279.             String[] parm = PersistString.parse(smsg.msg);
  280.             synchronized (this){
  281.               int index = smsg.msg.indexOf(' ');
  282.               int index2 = smsg.msg.indexOf(' ',index+1);
  283.               try {
  284.                 //app.GameTime = Integer.parseInt(smsg.msg.substring(0,index));
  285.                 app.gameTimeValue = Integer.parseInt(parm[0]);
  286.                 app.playerTime[0] = app.gameTimeValue * 60000;
  287.                 app.playerTime[1] = app.playerTime[0];
  288.                 //app.lMoveTime = Integer.parseInt(smsg.msg.substring(index+1,index2)) * 1000;
  289.                 app.lMoveTime = Integer.parseInt(parm[1]);
  290.               }
  291.               catch (NumberFormatException e) {}
  292.               app.challenger = parm[2];
  293.               WannaPlay wannaplay  = new WannaPlay(app.f,app,parm);
  294.               wannaplay.pack();
  295.               wannaplay.setBounds(200,200,350,170);
  296.               wannaplay.setVisible(true);
  297.             }
  298.           }
  299.         }
  300.         else
  301.           break;    // end if readline != null
  302.       }       // end while(true)
  303.       //is.close();
  304.       app.sendMsg(ServerMessage.DEAD,"null line");
  305.       System.out.println("bclient shutting down, received null line");
  306.     }
  307.     catch (Exception e) {
  308.       if (app.sender != null) {
  309.         app.sendMsg(ServerMessage.DEAD,e.toString());
  310.         app.statusLine.setText(res.getString("LOST_CONNECTION_") + e)  ;
  311.         e.printStackTrace();
  312.         app.logonEnable(false);
  313.         System.err.println("CV Exception:  " + e);
  314.       }
  315.     }
  316.     try {
  317.       is.close();
  318.       kkSocket.close();
  319.     }
  320.     catch (Exception e) {}
  321.   } //end of method run
  322.  
  323.   void getLibraryGame(String msg){
  324.     int markerIndex = msg.indexOf('?'); //games can have question marks but not game filenames
  325.     String name = msg.substring(0,markerIndex);
  326.     String game = msg.substring(markerIndex+1);
  327.     app.libraryHash.put(name,game);
  328.     if (app.showLibraryGame)
  329.       app.getGame(name);
  330.     else
  331.       app.statusLine.setText(res.getString("RECEIVED_") + name);
  332.   }
  333. }   //end of class ClientListener
  334.  
  335.