📄 serverchess.java
字号:
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.bluetooth.*;
import java.util.*;
class ServerChess implements Runnable
{
private int MaxErrorTime = 5;
private StreamConnectionNotifier scn=null;
private LocalDevice localDevice=null;
private UUID uuid=null;
private volatile boolean stop=false;
private Flash flash=null;
private int selectedSaveIndex;
private MainSelect mainSelect = null;
//调用来源有关变量
private boolean ifSelect = false;
private int[][] chess;
//标记服务器的种类
private int serverType = 0;
//接收棋子变化棋盘变化
private int[] accept = null;
private ChessCanvas chessCanvas=null;
private ViewerCanvas viewerCanvas = null;
//
private PublicCanvas publicCanvas = null;
//背景声音相关变量
private int musicIndex, soundIndex;
ServerChess(MainSelect ms,int saveIndex,boolean ifSelect,int[][] chess, int musicIndex, int soundIndex)
{
serverType = 0;
mainSelect = ms;
selectedSaveIndex=saveIndex;
this.musicIndex = musicIndex;
this.soundIndex = soundIndex;
this.ifSelect = ifSelect;
if(ifSelect)
this.chess = chess;
uuid = new UUID("0", false);
flash=new Flash();
flash.startFlash();
new Thread(this).start();
}
ServerChess(ChessCanvas cc)
{
serverType = 1;
chessCanvas = cc;
accept = new int[4];
uuid = new UUID("1", false);
new Thread(this).start();
}
ServerChess(ViewerCanvas vc)
{
serverType = 2;
viewerCanvas = vc;
accept = new int[4];
uuid = new UUID("2", false);
new Thread(this).start();
}
ServerChess(int[][] chess, ChessCanvas cc)
{
serverType = 3;
chessCanvas = cc;
this.chess = chess;
uuid = new UUID("3", false);
new Thread(this).start();
}
ServerChess(PublicCanvas pCanvas, MainSelect ms)
{
serverType = 4;
publicCanvas = pCanvas;
mainSelect = ms;
flash = new Flash(mainSelect);
flash.waiting();
flash.startFlash();
uuid = new UUID("4", false);
new Thread(this).start();
}
ServerChess(MainSelect ms, Flash flash)
{
serverType = 5;
mainSelect = ms;
this.flash = flash;
uuid = new UUID("5", false);
new Thread(this).start();
}
public void stop() throws IOException
{
stop = true;
scn.close();
}
public void run()
{
try
{
//System.err.println("获取本地设备管理器");
localDevice=LocalDevice.getLocalDevice();
localDevice.setDiscoverable(DiscoveryAgent.GIAC);
//System.err.println("获取本地设备管理器完毕");
}
catch (BluetoothStateException e)
{
//System.err.println("获取本地设备管理器异常");
//errorDeal.push("获取本地蓝牙设备管理器");
//提示错误,退出程序
//Alert alert=new Alert("Message","获取本地设备管理器异常",null,AlertType.WARNING);
//Chess.dis.setCurrent(alert);
Chess.quitApp();
//return;
}
try
{
//System.out.println("服务器开启服务");
scn=(StreamConnectionNotifier)Connector.open("btspp://localhost:"+uuid.toString()+";authorize=false");
//System.out.println("服务器开启服务完毕");
}
catch (IOException e)
{
//System.out.println("服务器开启服务异常");
//errorDeal.push("ServerChess#1", "开启蓝牙服务器失败");
//提示错误,退出程序
//Alert alert=new Alert("Message","服务器开启服务异常",null,AlertType.WARNING);
//Chess.dis.setCurrent(alert);
Chess.quitApp();
//return;
}
//System.out.println("开启服务成功,开始打开监听器");
if(serverType == 0)
flash.initOk();
//initialize completed
StreamConnection streamConnection=null;
int failure=0;//设置失败次数标志
while(!stop)
{//最多重复尝试五次,如果还不成功,启动报警
//System.out.println("失败次数为:"+failure);
try
{
//System.out.println("打开一个监听器,serverType是:"+serverType);
streamConnection=scn.acceptAndOpen();
//System.out.println("等到一个客户端的连接");
}
catch (IOException e)
{
failure = failure + 1;
if(failure == MaxErrorTime)
{
//发生了不可挽回的错误,提示用户后,重新建立看擂连接
//Alert alert=new Alert("Message","错误次数超过最大次数",null,AlertType.WARNING);
//Chess.dis.setCurrent(alert);
Chess.quitApp();
}
//System.out.println("打开监听器错误");
//errorDeal.push("ServerChess#2", "打开监听器错误,稍后重试");
try
{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
}
continue;
}
DataInputStream dis = null;
DataOutputStream dos = null;
failure = 0;
boolean ifFailure = false;
while(!ifFailure)
{
try
{
dis = streamConnection.openDataInputStream();
dos = streamConnection.openDataOutputStream();
//System.out.println("打开流成功serverType="+serverType+"dis="+dis);
break;
//打开流后,停止接收新的客户端
}
catch(IOException ie)
{
failure=failure+1;
if(failure==MaxErrorTime)
{
//Alert alert=new Alert("Message","错误次数超过最大次数",null,AlertType.WARNING);
//Chess.dis.setCurrent(alert);
Chess.quitApp();
//发生了不可挽回的错误,提示用户后,重新建立看擂连接
}
//System.out.println("打开流失败一次");
//errorDeal.push("ServerChess#2", "打开监听器错误,稍后重试");
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
}
continue;
}
}
if(ifFailure)
continue;
try
{
//对战时初始连接
if(serverType == 0)
{
//System.out.println("写入标记是否选择了来源boolean值:"+ifSelect);
dos.writeBoolean(ifSelect);
//如果选择了来源的话,开始写入初始棋盘
if(ifSelect)
{
StringBuffer sb=new StringBuffer(90);
char c=(char)0;
for(int i=0;i<10;i++)
{
for(int j=0;j<9;j++)
{
c=(char)chess[i][j];
sb.append(c);
//System.out.println("写入被选择的初始棋盘数组为:"+chess[i][j]);
}
}
dos.writeUTF(sb.toString());
//System.out.println("擂主写入被选择的初始棋盘数组完成");
}
dis.close();
dos.close();
streamConnection.close();
scn.close();
flash.connectOk();
//向用户发出Alert提示连接已经创建完毕
//System.out.println("是否保存记录的选择是"+selectedSaveIndex);
chessCanvas=new ChessCanvas(mainSelect,0,selectedSaveIndex,ifSelect,chess, musicIndex, soundIndex);
//System.out.println("客户端搭建完毕");
Chess.dis.setCurrent(chessCanvas);
flash.stopFlash();
}
//接收对方的棋盘变化数据
else if(serverType == 1)
{
boolean b = dis.readBoolean();
accept[0] = 9 - dis.readInt();
accept[1] = 8 - dis.readInt();
accept[2] = 9 - dis.readInt();
accept[3] = 8 - dis.readInt();
dis.close();
dos.close();
streamConnection.close();
scn.close();
chessCanvas.setChance(accept, b);
//System.out.println("将棋盘变化设置到chessCanvas");
}
//看擂者接收棋盘变化数据
else if(serverType == 2)
{
for(int i=0;i<4;i++)
{
accept[i] = dis.readInt();
}
//System.out.println("看擂者接收棋盘走步变化成功");
dis.close();
dos.close();
streamConnection.close();
scn.close();
viewerCanvas.setViewer(accept);
//continue;
}
//向看擂者发送棋盘初始数据
else if(serverType == 3)
{
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 9; j++)
dos.writeInt(chess[i][j]);
}
//System.out.println("向看擂者初始棋盘数组成功");
chessCanvas.haveViewer();
chessCanvas.setServerOrClient(true);
dis.close();
dos.close();
streamConnection.close();
scn.close();
}
else if(serverType == 4)
{
boolean isFirst = publicCanvas.isNew();
dos.writeBoolean(isFirst);
//System.out.println("观众:发送的boolean值是:"+isFirst);
if(isFirst)
{
int[][] tempChess = new int[10][9];
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 9; j++)
{
tempChess[i][j] = dis.readInt();
}
}
publicCanvas.setInitChess(tempChess);
flash.stopFlash();
}
else
{
int[] tempAccept = new int[4];
for(int i = 0; i < 4; i++)
tempAccept[i] = dis.readInt();
publicCanvas.setPublicer(tempAccept);
}
//System.out.println("观众:接收数据完毕");
dis.close();
dos.close();
streamConnection.close();
continue;
}
else if(serverType == 5)
{
boolean isPhase = dis.readBoolean();
int[][] tempChess = new int[10][9];
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 9; j++)
{
tempChess[i][j] = dis.readInt();
}
}
if(isPhase)
{
RestRecord rest = new RestRecord();
rest.addPhase(tempChess);
rest.closePhaseRecord();
}
else
{
ChessRecord chessRecord = new ChessRecord();
chessRecord.addIndex(tempChess);
int resetNumber = dis.readInt();
int[][] reset = new int[resetNumber][4];
for(int i = 0; i < resetNumber; i++)
{
for(int j = 0; j < 4; j++)
{
reset[i][j] = dis.readInt();
}
chessRecord.addContent(reset[i]);
}
chessRecord.closeChessRecord();
}
Chess.dis.setCurrent(mainSelect);
flash.stopFlash();
dis.close();
dos.close();
streamConnection.close();
scn.close();
}
}
catch(IOException e)
{
Alert alert=new Alert("Message","发送数据过程中出现异常",null,AlertType.WARNING);
Chess.dis.setCurrent(alert);
//Chess.quitApp();
}
//不管情况如何,到这一步应该退出循环
break;
}
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -