home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / ClientListener.java < prev    next >
Text File  |  1997-07-03  |  12KB  |  317 lines

  1. package borland.samples.apps.chess.client ;
  2.  
  3. import java.net.*;
  4. import java.io.*;
  5.  
  6. import java.util.*;
  7. /**Receives messages from the ChessServer
  8.  * This thread is created at logon and listens and processes messages from the server
  9. */
  10. public class ClientListener extends Thread {
  11.   ChessViewer app;
  12.   String msg;
  13.   Socket kkSocket;
  14.   DataInputStream is;
  15.   PlayerListListener listListener = null;
  16.  
  17.   public ClientListener(ChessViewer app,Socket kkSocket) {
  18.     super("ClientListener");
  19.     this.kkSocket = kkSocket;
  20.     this.app = app;
  21.   }
  22.  
  23.   public  ClientListener() {
  24.     super("ClientListener");
  25.   }
  26.  
  27.   public  void run() {
  28.     try{
  29.       String fromServer;
  30.       DataInputStream is = new DataInputStream(kkSocket.getInputStream());
  31.       Reader reader = new InputStreamReader(is);
  32.       BufferedReader bufferedReader = new BufferedReader(reader);
  33.       int blank = 32;
  34.       int offset = 0;
  35.       ServerMessage smsg = new ServerMessage(0,"","");
  36.       while (true) {
  37.         if ((fromServer = bufferedReader.readLine()) != null) {
  38.           System.out.println("CV Received: " + fromServer);
  39.           offset = fromServer.indexOf(blank);
  40.           if (offset > 0) {
  41.             smsg.msgid = fromServer.substring(0,offset);
  42.             smsg.msg = fromServer.substring(offset+1); //don't want the space char
  43.           }
  44.           else {
  45.             smsg.msgid = fromServer;
  46.                smsg.msg = null;
  47.           }
  48.           if (smsg.msgid.equals("Bye")) {
  49.             app.logonEnable(false); //disable the world
  50.             break;
  51.           }
  52.           else
  53.           if (smsg.msgid.equals("Token")) {
  54.             app.moveToken = true;
  55.           }
  56.           else
  57.           if (smsg.msgid.equals("Refused")) {
  58.             app.statusLine.setText(CVS.OFFER_REFUSED);
  59.           }
  60.           else
  61.           if (smsg.msgid.equals("Status")) {
  62.             if (app.pDialog != null)
  63.               app.pDialog.badLogon();
  64.             app.statusLine.setText(smsg.msg);
  65.           }
  66.           else
  67.           if (smsg.msgid.equals("Chat")) {
  68.             app.infoText.append(smsg.msg + "\n");
  69.           }
  70.           else
  71.           if (smsg.msgid.equals("Welcome")) {
  72.             if (app.pDialog != null)
  73.               app.pDialog.goodLogon();
  74.             app.statusLine.setText(CVS.WELCOME + app.myName);
  75.             app.libraryList.removeAll();
  76.             app.libraryListContents.removeAllElements();
  77.             app.logonEnable(true);
  78.             synchronized (app.sender) {
  79.               app.sender.msgque.addElement(new ServerMessage(0,"List",""));
  80.               app.sender.notify();
  81.             }
  82.           }
  83.           else
  84.           if (smsg.msgid.equals("List")) {
  85.             app.setListButtons(ChessViewer.PLAYERLIST) ;
  86.             app.fillList(smsg.msg);
  87.             //app.sbChoose.enable(false);
  88.           }
  89.           if (smsg.msgid.equals("PlayerList")) {
  90.             if (listListener != null)
  91.               listListener.listMessage(smsg.msg);
  92.          
  93.           }
  94.           else
  95.           if (smsg.msgid.equals("LibraryList")) {
  96.             app.fillList(smsg.msg);
  97.             app.setListButtons(ChessViewer.LIBRARYLIST);
  98.           }
  99.           else
  100.           if (smsg.msgid.equals("MoveList")) {
  101.             app.game.init();
  102.             app.game.parsePGNFile (new StringReader(smsg.msg));
  103.             app.playerTime[0] = app.game.getWhiteTimeRemaining();
  104.             app.playerTime[1] = app.game.getBlackTimeRemaining();
  105.             app.lMoveTime = app.game.getMoveTimeInterval();
  106.             app.setNames();
  107.             app.fillMoveList();
  108.             app.movecount = app.count;
  109.             app.setListButtons(ChessViewer.KIBITZ) ;
  110.             app.listPanel.setSelectedTab(ChessViewer.MOVELIST_LABEL);
  111.             app.displayPos();
  112.             System.out.println("ML chessboard disabled");
  113.             app.theboard.setEnabled(false);
  114.           }
  115.           else
  116.           if (smsg.msgid.equals("Resume")) {
  117.             app.game.init();
  118.             app.game.parsePGNFile (new StringReader(smsg.msg));
  119.             app.color = app.game.getWhoMovedLast();
  120.             app.playerTime[0] = app.game.getWhiteTimeRemaining();
  121.             app.playerTime[1] = app.game.getBlackTimeRemaining();
  122.             app.lMoveTime = app.game.getMoveTimeInterval();
  123.             app.setNames();
  124.             if (app.game.getPlayerW().equals(app.myName)) {
  125.               app.yourColor = ChessRules.White;
  126.               app.opponentName = app.game.getPlayerB() ;
  127.             }
  128.             else {
  129.               app.yourColor = ChessRules.Black;
  130.               app.opponentName = app.game.getPlayerW() ;
  131.             }
  132.             app.fillMoveList();
  133.             app.movecount = app.count;
  134.             app.setListButtons(ChessViewer.MOVELIST) ;
  135.             app.setGameButtons();
  136.             app.hasListeners = true;
  137.             app.displayPos();
  138.             app.moveToken = true;
  139.             System.out.println("ML chessboard enabled");
  140.             String result = app.game.getResult();
  141.             if ( result != null){   //rare
  142.               app.gameOver();
  143.               app.statusLine.setText(result);
  144.             }
  145.             else {
  146.               app.theboard.setEnabled(true);
  147.               app.gameTime.setEnabled(false);
  148.               app.moveTime.setEnabled(false);
  149.               app.moveTime.setText(String.valueOf(app.lMoveTime/1000));
  150.               if (app.clock != null)
  151.                 app.clock.stop();
  152.               app.clock = new Thread(app);
  153.               app.clock.start();
  154.             }
  155.           }
  156.           else
  157.           if (smsg.msgid.equals("LibraryGame")) {
  158.             getLibraryGame(smsg.msg);
  159.           }
  160.           else
  161.           if (smsg.msgid.equals("Resigned")) {
  162.             //app.setListButtons(ChessViewer.KIBITZ);
  163.             app.game.setComment(app.movecount,app.color,smsg.msg);
  164.             app.statusLine.setText(smsg.msg);
  165.             app.gameOver();
  166.           }
  167.           else
  168.           if (smsg.msgid.equals("Moved")) {
  169.             app.addMove(smsg.msg);
  170.           }
  171.           if (smsg.msgid.equals("TokenMove")) {
  172.              app.tokenMove(smsg.msg);
  173.           }
  174.           else
  175.           if (smsg.msgid.equals("AcceptAbort")) {
  176.             app.statusLine.setText(CVS.GAME_ABORTED);
  177.             app.game.setComment(app.movecount,app.color,"Aborted");
  178.             app.gameOver();
  179.             app.sbChoose.setEnabled(false);
  180.           }
  181.           else
  182.           if (fromServer.startsWith("AcceptDraw")) {
  183.             app.gameOver();
  184.             app.game.setComment(app.movecount,app.color,"1/2-1/2");
  185.             app.displayPos();
  186.             app.statusLine.setText(CVS.GAME_DRAWN);
  187.           }
  188.           else
  189.           if (smsg.msgid.equals("GameAccepted")) {
  190.             String parm[] = PersistString.parse(smsg.msg);
  191.             app.opponentName = app.challenger;
  192.             if (parm[0].equals("black")) {
  193.               app.yourColor = ChessRules.White;
  194.               app.newGame(app.myName,app.opponentName);
  195.               app.game.setWhiteElo(parm[3]);
  196.               app.game.setBlackElo(parm[5]);
  197.             }
  198.             else
  199.             if (parm[0].equals("white")) {
  200.               app.yourColor = ChessRules.Black;
  201.               app.newGame(app.opponentName,app.myName);
  202.               app.game.setWhiteElo(parm[5]);
  203.               app.game.setBlackElo(parm[3]);
  204.             }
  205.             app.game.setSite(CVS.BORLAND_CHESS_CLUB);
  206.             app.game.setRound("-");
  207.             app.game.setEvent(CVS.RATED_GAME);
  208.             //Date today = new Date();
  209.             java.util.GregorianCalendar today = new GregorianCalendar();
  210.             app.game.setDate(String.valueOf(today.get(Calendar.YEAR))+ "." +
  211.                              String.valueOf(today.get(Calendar.MONTH)+1) + "." +
  212.                              String.valueOf(today.get(Calendar.DAY_OF_MONTH)));
  213.  
  214.           }
  215.           else
  216.           if (smsg.msgid.equals("Information")) {
  217.             System.out.println("Information");
  218.  
  219.             String[]  info = PersistString.parse(smsg.msg);
  220.             if (listListener == null) {
  221.               InfoDlg dlg = new InfoDlg(app,info);
  222.               dlg.setBounds(0,0,400,400);   //430 was too wide
  223.               //pack();
  224.               dlg.setLocation(300,200);
  225.               dlg.show();
  226.             }
  227.             else
  228.               listListener.displayData(info);
  229.           }
  230.           else
  231.           if (smsg.msgid.equals("Info")) {
  232.             System.out.println("Info");
  233.             String[]  info = PersistString.parse(smsg.msg);
  234.             app.infoText.append(info[1] + ": " + info[3] + "\r\n");
  235.             for (int i=4;i < 7 && i<info.length && info[i] != null ;i++)
  236.               if (!info[i].startsWith(" "))
  237.                 app.infoText.append(info[i] + "\r\n");
  238.           }
  239.           else
  240.           if (smsg.msgid.equals("OfferDraw")) {
  241.             synchronized (this){
  242.               DrawDlg dlg = new DrawDlg(app.f,smsg.msgid,app);
  243.               dlg.pack();
  244.               dlg.setBounds(200,200,300,170);
  245.               dlg.show();
  246.             }
  247.           }
  248.           else
  249.           if (smsg.msgid.equals("OfferAbort")) {
  250.             synchronized (this){
  251.               DrawDlg dlg = new DrawDlg(app.f,smsg.msgid,app);
  252.               dlg.pack();
  253.               dlg.setBounds(200,200,300,170);
  254.               dlg.show();
  255.             }
  256.           }
  257.           else
  258.           if (smsg.msgid.equals("Challenged")) {
  259.             String[] parm = PersistString.parse(smsg.msg);
  260.             synchronized (this){
  261.               int index = smsg.msg.indexOf(' ');
  262.               int index2 = smsg.msg.indexOf(' ',index+1);
  263.               try {
  264.                 //app.GameTime = Integer.parseInt(smsg.msg.substring(0,index));
  265.                 app.gameTimeValue = Integer.parseInt(parm[0]);
  266.                 app.playerTime[0] = app.gameTimeValue * 60000;
  267.                 app.playerTime[1] = app.playerTime[0];
  268.                 //app.lMoveTime = Integer.parseInt(smsg.msg.substring(index+1,index2)) * 1000;
  269.                 app.lMoveTime = Integer.parseInt(parm[1]);
  270.               }
  271.               catch (NumberFormatException e) {}
  272.              // String timeControl = CVS.GAME_IN + parm[0] + CVS.MINUTES_PLUS +
  273.              //                       parm[1] +  CVS.SECONDS_PER_MOVE;
  274.  
  275.               //app.challenger = smsg.msg.substring(index2+1);
  276.               app.challenger = parm[2];
  277.               WannaPlay wannaplay  = new WannaPlay(app.f,app,parm);
  278.               wannaplay.pack();
  279.               wannaplay.setBounds(200,200,350,170);
  280.               wannaplay.show();
  281.             }
  282.           }
  283.         }     // end if readline != null
  284.       }       // end while(true)
  285.       //is.close();
  286.       app.sendMsg("Dead","null line");
  287.       System.out.println("bclient shutting down, received null line");
  288.     }
  289.     catch (Exception e) {
  290.       if (app.sender != null) {
  291.         app.sendMsg("Dead",e.toString());
  292.         app.statusLine.setText(CVS.LOST_CONNECTION_ + e)  ;
  293.         e.printStackTrace();
  294.         app.logonEnable(false);
  295.         System.err.println("CV Exception:  " + e);
  296.       }
  297.     }
  298.     try {
  299.       is.close();
  300.       kkSocket.close();
  301.     }
  302.     catch (Exception e) {}
  303.   } //end of method run
  304.  
  305.   void getLibraryGame(String msg){
  306.     int markerIndex = msg.indexOf('?'); //games can have question marks but not game filenames
  307.     String name = msg.substring(0,markerIndex);
  308.     String game = msg.substring(markerIndex+1);
  309.     app.libraryHash.put(name,game);
  310.     if (app.showLibraryGame)
  311.       app.getGame(name);
  312.     else
  313.       app.statusLine.setText(CVS.RECEIVED_ + name);
  314.   }
  315. }   //end of class ClientListener
  316.  
  317.