📄 remoteplayer.java
字号:
// Copyright (c) 2005 Sony Ericsson Mobile Communications AB
//
// This software is provided "AS IS," without a warranty of any kind.
// ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
// INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
//
// THIS SOFTWARE IS COMPLEMENTARY OF JAYWAY AB (www.jayway.se)
package bluegammon.logic;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import bluegammon.io.PlayerListenerProxy;
/**
* <p>
* Represents a player that is on the other side of a connection;
* a remote player does not have access to this device.
* </p><p>
* This <code>RemotePlayer</code> is also a <code>PlayerListener</code> and
* retreives events from a <code>LocalPlayer</code> remotely sent from a
* <code>PlayerListenerProxy</code> via streams. It then deparses remote data
* and calls appropriate action on the <code>BoardMediator</code>.
* </p>
* @author Peter Andersson
*/
public class RemotePlayer extends Player implements PlayerListener, Runnable
{
/** Data input stream */
protected DataInputStream m_in;
/**
* Creates a remote player.
* @param id The id of the remote player.
* @param name The name of the remote player.
* @param white The color of the remote player, true for white, false for black.
* @param in The stream the remote player is reading events from.
*/
public RemotePlayer(int id, char[] name, boolean white,
DataInputStream in)
{
init(id, name, white);
m_in = in;
new Thread(this,"RemotePlayer").start();
}
/**
* Called when a move made event is received.
*/
public void moveMade(int id, int moveIndex)
{
BoardMediator.makePlayerMove(moveIndex);
fireMoveMade(moveIndex);
}
/**
* Called when an undo performed event is received.
*/
public void undoPerformed(int id)
{
BoardMediator.undoLastMove();
fireUndoPerformed();
}
/**
* Called when a turn commit event is received.
*/
public void turnCommit(int id)
{
BoardMediator.commitTurn();
fireTurnCommit();
}
/**
* Called when a message event is received.
*/
public void messageSent(int id, char[] msg)
{
BoardMediator.showMessage(msg);
fireMessageSent(msg);
}
/**
* Called when a game exit event is received.
*/
public void gameExited(int id, int reason)
{
fireGameExited(reason);
BoardMediator.exitGame(reason);
}
/**
* Runnable implementation,
* reading data from the input stream and
* deserializes the events.
*/
public void run()
{
boolean running = true;
try
{
while (running)
{
// Read command byte
byte cmd = m_in.readByte();
// Read id
int id = m_in.readInt();
switch (cmd)
{
// Got a movement
case PlayerListenerProxy.MOVE:
int possibleMoveIndex = m_in.readInt();
moveMade(id, possibleMoveIndex);
break;
// Got an undo
case PlayerListenerProxy.UNDO:
undoPerformed(id);
break;
// Got new turn
case PlayerListenerProxy.TURN:
turnCommit(id);
break;
// Other player exited
case PlayerListenerProxy.EXIT:
int reason = m_in.readInt();
running = false;
gameExited(id, reason);
break;
// Got a message
case PlayerListenerProxy.MSG:
String msg = m_in.readUTF();
messageSent(id, msg.toCharArray());
break;
}
}
}
catch (EOFException eof)
{
System.err.println("RemotePlayer.run premature eof");
if (running) BoardMediator.lostRemoteConnection(eof);
}
catch (IOException ioe)
{
System.err.println("RemotePlayer.run IOException");
if (running) BoardMediator.lostRemoteConnection(ioe);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -