www.pudn.com > Bluegammon蓝牙的应用编程.rar > 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; 
 
/** 
 * 

* This class serializes all events from sent to a PlayerListener and * sends them to specified output stream. * In this game there is a RemotePlayer * on the other side of the connection. The RemotePlayer is also a * PlayerListener and will get same listener events as this proxy via * streams.

* Relies on that specified outstream delivers and handles all fuzziness with * I/O. *

* * @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 PlayerListenerProxy that * sends received PlayerListener 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); } } }