glassfrog.server
Class Room

java.lang.Object
  extended by glassfrog.server.Room
All Implemented Interfaces:
java.lang.Runnable

public class Room
extends java.lang.Object
implements java.lang.Runnable

The Room class is the class that will handle all of the game code. The rooms consist of Players and a Dealer. The room is responsible for handleing all the connections to the Players, The room takes connections until it is "full" then it will start the game with the given specifications. After the game is started, the dealer will then handle all of the game logic, and the Room will be responsible for message passing to the players. Essentially, the Dealer handles the gamestates, and then the room is responsible for sending an action request to the proper player. The response is then sent back to the Dealer. Once the game is over, the room will be shutdown, or restarted, depending on specifications of the room.

Author:
jdavidso

Nested Class Summary
private  class Room.RoomConnectionHandler
          An Inner Class used to handle incoming connections to the room.
 
Field Summary
private  boolean alive
           
private  Dealer dealer
           
private  java.io.FileWriter errorLog
           
private  java.io.BufferedWriter errorLogWriter
           
private  Gamedef gamedef
           
private  java.lang.String name
           
private  int numHands
           
private  int playerCount
           
private  java.util.LinkedList<Player> players
           
private  int port
           
private static int PORTBASE
           
private static int ROOM_TIMEOUT
           
private  java.io.FileWriter roomLog
           
private  java.io.BufferedWriter roomLogWriter
           
private  boolean runOnce
           
private static boolean SAVE_FLAG
           
private  int seed
           
private  java.net.ServerSocket serverSocket
           
private static int TIMEOUT
           
 
Constructor Summary
Room(java.lang.String name, int hands, Gamedef gamedef, int seed)
          Starts up a Room.
 
Method Summary
 java.lang.String getName()
          Getter for the room's name
 int getPort()
          Return the port the room is running on
 java.lang.String getStatus()
          Return the current status of the room.
private  void initLogging()
          Initialize the logs for the game.
private  void initServerSocket()
          Set up the server socket.
 boolean isAlive()
          Check to see if the room is still alive.
private  void listen()
          Listen for incoming connections.
private static void loadSettings()
           
 void logError(java.lang.Exception ex)
          Utility for logging an error message to the errorLogger
 void logInfo(java.lang.String info, boolean toOut)
          Log a gamesate to the roomLogger
 void logWarning(java.lang.String warningMessage)
          Utility for logging a warning message to the errorLogger
 void run()
          Calls the listen method to wait for connections.
private  void saveDealer()
          Save the instance of the dealer to file for reloading of the game
 void shutdown()
          Shutdown a room via request from the server or game is over
private  void startNewDealer()
          Load up a new dealer and save it for the first time.
 java.lang.String toString()
          Return a human readable, parser friendly : delimited representation of the Room's info
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

name

private java.lang.String name

gamedef

private Gamedef gamedef

dealer

private Dealer dealer

serverSocket

private java.net.ServerSocket serverSocket

players

private java.util.LinkedList<Player> players

playerCount

private int playerCount

port

private int port

seed

private int seed

numHands

private int numHands

runOnce

private boolean runOnce

alive

private boolean alive

errorLog

private java.io.FileWriter errorLog

roomLog

private java.io.FileWriter roomLog

errorLogWriter

private java.io.BufferedWriter errorLogWriter

roomLogWriter

private java.io.BufferedWriter roomLogWriter

ROOM_TIMEOUT

private static final int ROOM_TIMEOUT
See Also:
Constant Field Values

TIMEOUT

private static int TIMEOUT

PORTBASE

private static int PORTBASE

SAVE_FLAG

private static boolean SAVE_FLAG
Constructor Detail

Room

public Room(java.lang.String name,
            int hands,
            Gamedef gamedef,
            int seed)
     throws java.io.IOException,
            java.lang.InterruptedException
Starts up a Room. A Room is a place where players connect to in order to get assigned sockets to talk to the dealer. Here the name, game definition, number of hands and seed are specified.

Parameters:
name - The name of the match/room
hands - The number of hands to play
gamedef - The game definition used
seed - a seed for the cards in the game
Throws:
java.io.IOException
java.lang.InterruptedException
Method Detail

initLogging

private void initLogging()
Initialize the logs for the game. The logname will be of the form roomname_timestamp.(log || .err) where roomname is the name passed from the server to the room and the timestamp


logError

public void logError(java.lang.Exception ex)
Utility for logging an error message to the errorLogger

Parameters:
ex -

logWarning

public void logWarning(java.lang.String warningMessage)
Utility for logging a warning message to the errorLogger

Parameters:
warningMessage - A message to log to the error log

logInfo

public void logInfo(java.lang.String info,
                    boolean toOut)
Log a gamesate to the roomLogger

Parameters:
info - the message to log to the room log
toOut - True to print to stdout

initServerSocket

private void initServerSocket()
                       throws java.net.BindException,
                              java.lang.InterruptedException,
                              java.io.IOException
Set up the server socket. Try 999 ports starting from the PORTBASE. If unable, throw BindException

Throws:
java.io.BindException
java.InterruptedException
java.io.IOException
java.net.BindException
java.lang.InterruptedException

listen

private void listen()
             throws java.io.IOException
Listen for incoming connections. Create a thread to handle the connection which will figure out if the connection request is a the dealer, server or player then do the appropriate operations. When handling connections, if we are waiting to start the game, then this method will create and execute a dealer thread to be run in the game This thread will check for new connections every 1s

Throws:
java.io.IOException

startNewDealer

private void startNewDealer()
Load up a new dealer and save it for the first time.


saveDealer

private void saveDealer()
Save the instance of the dealer to file for reloading of the game


run

public void run()
Calls the listen method to wait for connections. Listen will only end in one of two situations. 1) The server tells the room to shutdown 2) The room has finished playing a game and the constructor or server set the runOnce flag for the room. The runOnce flag tells the room to only execute one run of the dealer, and once that ends (either in a disconnect or a gameOver) then the room will shut down

Specified by:
run in interface java.lang.Runnable

getStatus

public java.lang.String getStatus()
Return the current status of the room. The current hand, players, stats, and some vague information about the game

Returns:
The room's current status

getName

public java.lang.String getName()
Getter for the room's name

Returns:
The room name

isAlive

public boolean isAlive()
Check to see if the room is still alive. This is for shutting down the rooms from the server

Returns:
The alive status of the room

getPort

public int getPort()
Return the port the room is running on

Returns:
an int representing the port

shutdown

public void shutdown()
Shutdown a room via request from the server or game is over


toString

public java.lang.String toString()
Return a human readable, parser friendly : delimited representation of the Room's info

Overrides:
toString in class java.lang.Object
Returns:
A String containing all the info relevant to the Room

loadSettings

private static void loadSettings()
                          throws javax.xml.parsers.ParserConfigurationException,
                                 org.xml.sax.SAXParseException,
                                 org.xml.sax.SAXException,
                                 java.io.IOException,
                                 java.lang.InterruptedException
Throws:
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXParseException
org.xml.sax.SAXException
java.io.IOException
java.lang.InterruptedException