bluegammon.io
Class Handshake

java.lang.Object
  extended bybluegammon.io.Handshake

public class Handshake
extends java.lang.Object

A Handshake consists of sending and receiving data as specified below, where one player has server role and the other has client role.

Stores needed data for further setup, and initializes static aspects of the game (rules and random seed).

 SERVER                              CLIENT
                                     SEND random seed (int)
                                     SEND client id (int)
                                     SEND client name (UTF)
 SEND has saved game (boolean)
 IF saved game: SEND saved data (byte[])
 SEND server id (int)
 SEND server name (UTF)
 SEND client color (boolean)
 IF no saved game: SEND rules(int)
                                     IF no remote saved game: SEND has saved game (boolean)
                                     IF no remote saved game AND has saved game: SEND saved data (byte[])
 

Author:
Peter Andersson

Constructor Summary
Handshake()
           
 
Method Summary
 void clientHandshake(java.io.DataInputStream dis, java.io.DataOutputStream dos, java.lang.String localName)
          Performs a handshake as a client.
 int getRemoteId()
          Returns the id of the other device after a successful handshake.
 char[] getRemoteName()
          Returns the name of the other device after a successful handshake.
 byte[] getSavedGame()
          Returns the data of a shared saved game.
 boolean isRemoteResume()
          Returns whether the saved game data was collected from the remote device or from this device.
 boolean isWhite()
          Returns the color of this device as decided by the handshake.
 void serverHandshake(java.io.DataInputStream dis, java.io.DataOutputStream dos, java.lang.String localName)
          Performs a handshake as a server.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Handshake

public Handshake()
Method Detail

serverHandshake

public void serverHandshake(java.io.DataInputStream dis,
                            java.io.DataOutputStream dos,
                            java.lang.String localName)
                     throws java.io.IOException
Performs a handshake as a server. This will exchange data and any shared game with the other device.

Parameters:
dis - The input stream from the other device.
dos - The output stream to the other device.
localName - The name of this device.
Throws:
java.io.IOException - if the handshake fails.

clientHandshake

public void clientHandshake(java.io.DataInputStream dis,
                            java.io.DataOutputStream dos,
                            java.lang.String localName)
                     throws java.io.IOException
Performs a handshake as a client. This will exchange data and any shared game with the other device.

Parameters:
dis - The input stream from the other device.
dos - The output stream to the other device.
localName - The name of this device.
Throws:
java.io.IOException - if the handshake fails.

getRemoteId

public int getRemoteId()
Returns the id of the other device after a successful handshake.

Returns:
The id of the remote device.

getRemoteName

public char[] getRemoteName()
Returns the name of the other device after a successful handshake.

Returns:
The name of the remote device.

getSavedGame

public byte[] getSavedGame()
Returns the data of a shared saved game. If there was no shared saved game betweed these devices, null is returned.

Returns:
The shared saved game data or null.

isWhite

public boolean isWhite()
Returns the color of this device as decided by the handshake. If a saved game exists, this value is arbitrary.

Returns:
The local device's color, true for white, false for black.

isRemoteResume

public boolean isRemoteResume()
Returns whether the saved game data was collected from the remote device or from this device. If there is no saved game data, this method always returns false.

Returns:
true if saved game data was collected from remote device, false if it was collected from local device.