home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1998 October A
/
Pcwk10a98.iso
/
Inprise
/
TRIAL
/
JBUILDER
/
JSAMPLES.Z
/
ClientListener.java
< prev
next >
Wrap
Text File
|
1998-05-08
|
13KB
|
335 lines
/*
* Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
*
* This SOURCE CODE FILE, which has been provided by Borland as part
* of a Borland product for use ONLY by licensed users of the product,
* includes CONFIDENTIAL and PROPRIETARY information of Borland.
*
* USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS
* OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
* THE PRODUCT.
*
* IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
* COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
* OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
* OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
* OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
* OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
* CODE FILE.
*/
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;
ResourceBundle res = ResourceBundle.getBundle("borland.samples.apps.chess.client.Res");
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(ServerMessage.BYE)) {
app.logonEnable(false); //disable the world
break;
}
else
if (smsg.msgid.equals(ServerMessage.TOKEN)) {
app.moveToken = true;
}
else
if (smsg.msgid.equals(ServerMessage.REFUSED)) {
app.statusLine.setText(res.getString("OFFER_REFUSED"));
}
else
if (smsg.msgid.equals(ServerMessage.STATUS)) {
if (app.pDialog != null)
app.pDialog.badLogon();
app.statusLine.setText(smsg.msg);
}
else
if (smsg.msgid.equals(ServerMessage.CHAT)) {
app.infoText.append(smsg.msg + "\n");
}
else
if (smsg.msgid.equals(ServerMessage.WELCOME)) {
if (app.pDialog != null)
app.pDialog.goodLogon();
app.statusLine.setText(res.getString("WELCOME") + app.myName);
app.libraryList.removeAll();
app.libraryListContents.removeAllElements();
app.logonEnable(true);
synchronized (app.sender) {
app.sender.msgque.addElement(new ServerMessage(0,ServerMessage.LIST,""));
app.sender.notify();
}
}
else
if (smsg.msgid.equals(ServerMessage.LIST)) {
app.setListButtons(ChessViewer.PLAYERLIST) ;
app.fillList(smsg.msg);
//app.sbChoose.enable(false);
}
if (smsg.msgid.equals(ServerMessage.PLAYER_LIST)) {
if (listListener != null)
listListener.listMessage(smsg.msg);
}
else
if (smsg.msgid.equals(ServerMessage.LIBRARY_LIST)) {
app.fillList(smsg.msg);
app.setListButtons(ChessViewer.LIBRARYLIST);
}
else
if (smsg.msgid.equals(ServerMessage.MOVE_LIST)) {
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(app.res.getString("Moves"));
app.displayPos();
System.out.println("ML chessboard disabled");
app.theboard.setEnabled(false);
}
else
if (smsg.msgid.equals(ServerMessage.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(ServerMessage.LIBRARY_GAME)) {
getLibraryGame(smsg.msg);
}
else
if (smsg.msgid.equals(ServerMessage.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(ServerMessage.MOVED)) {
app.addMove(smsg.msg);
}
if (smsg.msgid.equals(ServerMessage.TOKEN_MOVE)) {
app.tokenMove(smsg.msg);
}
else
if (smsg.msgid.equals(ServerMessage.ACCEPT_ABORT)) {
app.statusLine.setText(res.getString("GAME_ABORTED"));
app.game.setComment(app.movecount,app.color,"Aborted");
app.gameOver();
app.sbChoose.setEnabled(false);
}
else
if (fromServer.startsWith(ServerMessage.ACCEPT_DRAW)) {
app.gameOver();
app.game.setComment(app.movecount,app.color,"1/2-1/2");
app.displayPos();
app.statusLine.setText(res.getString("GAME_DRAWN"));
}
else
if (smsg.msgid.equals(ServerMessage.GAME_ACCEPTED)) {
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(res.getString("BORLAND_CHESS_CLUB"));
app.game.setRound("-");
app.game.setEvent(res.getString("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(ServerMessage.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.setVisible(true);
}
else
listListener.displayData(info);
}
else
if (smsg.msgid.equals(ServerMessage.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(ServerMessage.OFFER_DRAW)) {
synchronized (this){
DrawDlg dlg = new DrawDlg(app.f,smsg.msgid,app);
dlg.pack();
dlg.setBounds(200,200,300,170);
dlg.setVisible(true);
}
}
else
if (smsg.msgid.equals(ServerMessage.OFFER_ABORT)) {
synchronized (this){
DrawDlg dlg = new DrawDlg(app.f,smsg.msgid,app);
dlg.pack();
dlg.setBounds(200,200,300,170);
dlg.setVisible(true);
}
}
else
if (smsg.msgid.equals(ServerMessage.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) {}
app.challenger = parm[2];
WannaPlay wannaplay = new WannaPlay(app.f,app,parm);
wannaplay.pack();
wannaplay.setBounds(200,200,350,170);
wannaplay.setVisible(true);
}
}
}
else
break; // end if readline != null
} // end while(true)
//is.close();
app.sendMsg(ServerMessage.DEAD,"null line");
System.out.println("bclient shutting down, received null line");
}
catch (Exception e) {
if (app.sender != null) {
app.sendMsg(ServerMessage.DEAD,e.toString());
app.statusLine.setText(res.getString("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(res.getString("RECEIVED_") + name);
}
} //end of class ClientListener