📄 playerlistenerproxy.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.io;
import java.io.DataOutputStream;
import java.io.IOException;
import bluegammon.logic.BoardMediator;
import bluegammon.logic.PlayerListener;
/**
* <p>
* This class serializes all events from sent to a <code>PlayerListener</code> and
* sends them to specified output stream.
* In this game there is a <code>RemotePlayer</code>
* on the other side of the connection. The <code>RemotePlayer</code> is also a
* <code>PlayerListener</code> and will get same listener events as this proxy via
* streams.</p><p>
* Relies on that specified outstream delivers and handles all fuzziness with
* I/O.
* </p>
*
* @author Peter Andersson
*/
public class PlayerListenerProxy implements PlayerListener
{
/**
* Identification byte for reporting a piece movement.
* Following is an ID integer and an move index integer.
*/
public static final byte MOVE = (byte) 0;
/**
* Identification byte for reporting an undo.
* Following is an ID integer.
*/
public static final byte UNDO = (byte) 1;
/**
* Identification byte for reporting a committed turn.
* Following is an ID integer.
*/
public static final byte TURN = (byte) 2;
/**
* Identification byte for sending a message to remote player.
* Following is an ID integer and a UTF-8-string.
*/
public static final byte MSG = (byte) 3;
/**
* Identification byte for reporting a player's shutdown.
* Following is an ID integer and a reason integer.
*/
public static final byte EXIT = (byte) 4;
/** The data output stream */
protected DataOutputStream m_out;
/**
* Creates a new <code>PlayerListenerProxy</code> that
* sends received <code>PlayerListener<code> events via
* specified output stream.
* @param out The stream to send events to.
*/
public PlayerListenerProxy(DataOutputStream out)
{
m_out = out;
}
// See interface javadoc
public void moveMade(int id, int moveIndex)
{
try
{
m_out.writeByte(MOVE);
m_out.writeInt(id);
m_out.writeInt(moveIndex);
m_out.flush();
}
catch (IOException e)
{
BoardMediator.lostRemoteConnection(e);
}
}
// See interface javadoc
public void undoPerformed(int id)
{
try
{
m_out.writeByte(UNDO);
m_out.writeInt(id);
m_out.flush();
}
catch (IOException e)
{
BoardMediator.lostRemoteConnection(e);
}
}
// See interface javadoc
public void turnCommit(int id)
{
try
{
m_out.writeByte(TURN);
m_out.writeInt(id);
m_out.flush();
}
catch (IOException e)
{
BoardMediator.lostRemoteConnection(e);
}
}
// See interface javadoc
public void messageSent(int id, char[] msg)
{
try
{
m_out.writeByte(MSG);
m_out.writeInt(id);
m_out.writeUTF(new String(msg));
m_out.flush();
}
catch (IOException e)
{
BoardMediator.lostRemoteConnection(e);
}
}
// See interface javadoc
public void gameExited(int id, int reason)
{
try
{
// Sending the reason to other device,
// flip local/remote if giving up/quitting
if (reason == LOCAL_GIVE_UP)
{
reason = REMOTE_GIVE_UP;
}
else if (reason == REMOTE_GIVE_UP)
{
reason = LOCAL_GIVE_UP;
}
else if (reason == REMOTE_QUIT)
{
reason = LOCAL_QUIT;
}
else if (reason == LOCAL_QUIT)
{
reason = REMOTE_QUIT;
}
m_out.writeByte(EXIT);
m_out.writeInt(id);
m_out.writeInt(reason);
m_out.flush();
}
catch (IOException e)
{
BoardMediator.lostRemoteConnection(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -