thegame.shared
Class GameConnection

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--thegame.shared.GameConnection
All Implemented Interfaces:
GameConnectionConstants, java.lang.Runnable
Direct Known Subclasses:
GameClientConnection, GameServerConnection, GameServerKiller

public abstract class GameConnection
extends java.lang.Thread
implements GameConnectionConstants

Defines methods and data members used by GameClientConnection and GameServerConnection.

Version:
0.99 (2000/08/28)
Author:
Erki Suurjaak

Field Summary
protected  java.io.BufferedReader in
          Used for reading from the socket.
protected  boolean isRunning
          Is true if the thread is running; is false otherwise.
protected  java.io.PrintWriter out
          Used for writing to the socket.
protected  java.net.Socket socket
          The socket used.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Fields inherited from interface thegame.shared.GameConnectionConstants
DEFAULT_SERVER_PORT, DELIMITER, P2P_MOVE_MADE_MESSAGE, P2P_NEW_GAME_ACCEPTED_MESSAGE, P2P_NEW_GAME_REFUSED_MESSAGE, P2P_NEW_GAME_REQUESTED_MESSAGE, P2P_PLAYER_SURRENDERED_MESSAGE, P2P_TEXT_MESSAGE, P2P_WINNING_MOVE_MADE_MESSAGE, P2S_CREATE_GAME_MESSAGE, P2S_JOIN_GAME_MESSAGE, P2S_PLAYER_NAME_MESSAGE, P2S_PLAYER_QUIT_MESSAGE, P2S_PLAYER_RETURNED_TO_START_SCREEN_MESSAGE, P2S_PLAYER_WAITED_FOR_NEW_PLAYER_MESSAGE, P2S_READY_FOR_PLAYER_LIST_MESSAGE, PLAYER_TO_SYSTEM_MESSAGES, S2P_JOIN_GAME_FAILED_MESSAGE, S2P_JOIN_GAME_SUCCEEDED_MESSAGE, S2P_PLAYER_DROPPED_MESSAGE, S2P_PLAYER_JOINED_MESSAGE, S2P_PLAYER_LIST_MESSAGE, S2P_PLAYER_NAME_NOT_OK_MESSAGE, S2P_PLAYER_NAME_OK_MESSAGE, S2P_PLAYER_QUIT_MESSAGE, S2P_PLAYER_RETURNED_TO_START_SCREEN_MESSAGE, S2P_SERVER_SHUTDOWN_MESSAGE, SHUTDOWN_MESSAGE
 
Constructor Summary
GameConnection(java.net.Socket socket)
          Constructs a GameConnection on the specified socket.
 
Method Summary
 void close()
          Closes the socket.
static java.lang.String getMessageBody(java.lang.String message)
          Returns the body of the message, or null if the message has no body.
static int[] getMessageContentsAs1DIntArray(java.lang.String message)
          Returns the contents of the message as int[2], or null if the message has no body.
static java.lang.String[] getMessageContentsAs1DStringArray(java.lang.String message)
          Returns the contents of the message as a String array, or null if the message has no body.
static int[][] getMessageContentsAs2DIntArray(java.lang.String message)
          Returns the contents of the message in the form of int[x][2] (where x is undefined), or null if the message has no body.
static boolean getMessageContentsAsBoolean(java.lang.String message)
          Returns the contents of the message as a boolean value.
static int getMessageID(java.lang.String message)
          Returns the type identifier of the message.
static java.lang.String getReadyToSendForm(int id, java.lang.String[] body)
          Returns the method arguments in a form that is ready to be sent as a message.
 void send(int id)
          Sends a message with id as the type identifier of the message.
 void send(int id, boolean body)
          Sends a message with id as the message type identifier and body as the body of the message.
 void send(int id, int[] body)
          Sends a message with id as the type identifier of the message and body as the body of the message.
 void send(int id, int[][] body)
          Sends a message with id as the type identifier of the message and body as the body of the message.
 void send(int id, java.lang.String body)
          Sends a message with id as the message type identifier and body as the body of the message.
 void send(java.lang.String str)
          Sends a string.
 void setRunning(boolean running)
          Sets the isRunning property.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, run, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

in

protected java.io.BufferedReader in
Used for reading from the socket.

isRunning

protected boolean isRunning
Is true if the thread is running; is false otherwise. The inner loop in run() exits when this becomes false.

out

protected java.io.PrintWriter out
Used for writing to the socket.

socket

protected java.net.Socket socket
The socket used.
Constructor Detail

GameConnection

public GameConnection(java.net.Socket socket)
               throws java.io.IOException
Constructs a GameConnection on the specified socket.
Parameters:
socket - the socket used; must be already created
Throws:
java.io.IOException - if an I/O error occurs when creating the I/O streams
Method Detail

close

public void close()
Closes the socket.

getMessageBody

public static java.lang.String getMessageBody(java.lang.String message)
Returns the body of the message, or null if the message has no body.
Parameters:
message - the message to be parsed
Returns:
the body of the message or null if the message has no body

getMessageContentsAs1DIntArray

public static int[] getMessageContentsAs1DIntArray(java.lang.String message)
Returns the contents of the message as int[2], or null if the message has no body.
Parameters:
message - the message to be parsed
Returns:
the contents of the message as int[2] or null if the message has no body

getMessageContentsAs1DStringArray

public static java.lang.String[] getMessageContentsAs1DStringArray(java.lang.String message)
Returns the contents of the message as a String array, or null if the message has no body.
Parameters:
message - the message to be parsed
Returns:
the contents of the message or null if the message has no body

getMessageContentsAs2DIntArray

public static int[][] getMessageContentsAs2DIntArray(java.lang.String message)
Returns the contents of the message in the form of int[x][2] (where x is undefined), or null if the message has no body.
Parameters:
message - the message to be parsed
Returns:
the contents of the message or null if the message has no body

getMessageContentsAsBoolean

public static boolean getMessageContentsAsBoolean(java.lang.String message)
Returns the contents of the message as a boolean value.
Parameters:
message - the message to be parsed
Returns:
the contents of the message as a boolean value

getMessageID

public static int getMessageID(java.lang.String message)
Returns the type identifier of the message.
Parameters:
message - the message to be parsed
Returns:
the type identifier of the message

getReadyToSendForm

public static java.lang.String getReadyToSendForm(int id,
                                                  java.lang.String[] body)
Returns the method arguments in a form that is ready to be sent as a message.
Parameters:
id - the type identifier of the message
body - the body of the message
Returns:
the method arguments in a form that is ready to be sent as a message

send

public void send(int id)
          throws java.io.IOException
Sends a message with id as the type identifier of the message.
Parameters:
id - the type identifier of the message
Throws:
java.io.IOException - if an I/O error occurs

send

public void send(int id,
                 boolean body)
          throws java.io.IOException
Sends a message with id as the message type identifier and body as the body of the message.
Parameters:
id - the type identifier of the message
body - the body of the message
Throws:
java.io.IOException - if an I/O error occurs

send

public void send(int id,
                 int[] body)
          throws java.io.IOException
Sends a message with id as the type identifier of the message and body as the body of the message.
Parameters:
id - the type identifier of the message
body - the body of the message
Throws:
java.io.IOException - if an I/O error occurs

send

public void send(int id,
                 int[][] body)
          throws java.io.IOException
Sends a message with id as the type identifier of the message and body as the body of the message.
Parameters:
id - the type identifier of the message
body - the body of the message
Throws:
java.io.IOException - if an I/O error occurs

send

public void send(int id,
                 java.lang.String body)
          throws java.io.IOException
Sends a message with id as the message type identifier and body as the body of the message.
Parameters:
id - the type identifier of the message
body - the body of the message
Throws:
java.io.IOException - if an I/O error occurs

send

public void send(java.lang.String str)
          throws java.io.IOException
Sends a string.
Parameters:
str - the data to be sent
Throws:
java.io.IOException - if an I/O error occurs

setRunning

public void setRunning(boolean running)
Sets the isRunning property.
Parameters:
running - false if the GameConnection thread is to stop; true otherwise