talx x1.0.5

talx.model
Class GameManager

java.lang.Object
  extended bytalx.model.GameManager
All Implemented Interfaces:
java.util.EventListener, GameListener, MailConstants

public class GameManager
extends java.lang.Object
implements GameListener, MailConstants

This class holds all games that are currently being played, as well as reading in and writing out their corresponding states.


Field Summary
static java.lang.String MOVE_MESSAGE_HEADER
          Message Header identifying games moves.
 
Fields inherited from interface talx.util.MailConstants
ARCHIVE_ID, BOUNCE_HEADER, COMPOSE_TIME, DEFAULT_ID, ERASE_COUNTS, EXPIRATION_TIME, NO_DELETE, NO_REPLY, REMINDER
 
Constructor Summary
GameManager(TransportToolkit toolkit, Settings settings)
           
 
Method Summary
 void addService(GameService spi)
          Adds the given service as provider.
static GameState extractState(javax.mail.internet.MimeMessage msg)
          Extracts a game state from a message, if one exists.
 void fireGameChangeEvent(GameState state)
          Fires a GameChangeProposedEvent to the game corresponding to the given state.
 Game getGame(Player p)
          Returns the Game associated with the given StartGame.
static java.lang.String[] getPlayerData(GameState gs)
          Extracts the player email and game from game state ID.
 java.lang.String getPlayerID(Player p)
           
 GameView getView(Game g, Controller controller)
          This method returns the cached view associated with the game passed.
 void localModelChange(GameChangedEvent gse)
          Invoked when a game changes state due to the local user's action.
static javax.mail.internet.MimeMessage packState(GameState gs, java.lang.String localuser)
          Packages a game state into a MimeMessage.
 void remoteModelChange(GameChangedEvent gse)
          Invoked when a game changes state due to the remote user's action.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MOVE_MESSAGE_HEADER

public static final java.lang.String MOVE_MESSAGE_HEADER
Message Header identifying games moves.

See Also:
Constant Field Values
Constructor Detail

GameManager

public GameManager(TransportToolkit toolkit,
                   Settings settings)
            throws java.io.IOException
Throws:
java.lang.IllegalArgumentException - if settings contains two distinct player objects that are .equals() to each other.
java.io.IOException
Method Detail

getPlayerData

public static java.lang.String[] getPlayerData(GameState gs)
Extracts the player email and game from game state ID.

Returns:
getPlayer[0] = player email address, getPlayer[1] = game name.

extractState

public static GameState extractState(javax.mail.internet.MimeMessage msg)
                              throws java.io.IOException,
                                     javax.mail.MessagingException,
                                     java.lang.ClassNotFoundException
Extracts a game state from a message, if one exists.

Parameters:
msg - the message that may contain a game state.
Returns:
the game state from the message, or null if the message contains no game state.
Throws:
javax.mail.MessagingException - if the message could not be read as in input stream.
java.io.IOException - if the message is malformed. This can throw any of the IOExceptions documented under ObjectInputStream.readObject().
java.lang.ClassCastException - if the attachment holds a serialized object that is not a game state.
java.lang.ClassNotFoundException - as documented under ObjectInputStream.readObject().

packState

public static javax.mail.internet.MimeMessage packState(GameState gs,
                                                        java.lang.String localuser)
                                                 throws javax.mail.MessagingException
Packages a game state into a MimeMessage. This method is supplied to assist in server-side game handling. GameHandlers on the client do not need to send the game state as the localModelChange model does that automatically.

Throws:
javax.mail.MessagingException

addService

public void addService(GameService spi)
Adds the given service as provider.

Parameters:
spi - the service that this GameManager will provide.

getPlayerID

public java.lang.String getPlayerID(Player p)

fireGameChangeEvent

public void fireGameChangeEvent(GameState state)
Fires a GameChangeProposedEvent to the game corresponding to the given state. The event is fired at the Game object's handler as returned by getHandler(). This is used by the CommandFilter class to convert remote events into model changes.

Parameters:
state - the proposed change to the game.

getView

public GameView getView(Game g,
                        Controller controller)
This method returns the cached view associated with the game passed. If there is no view, one will be created and returned with the guarantee that the controller passed is a listener to the view returned. This method should always be used instead of the Game.getView() method to obtain game views.

Parameters:
g - the game which will be looked up.
controller - the controller that should be a listener to all views.
Throws:
java.lang.NullPointerException - if g or controller is null.

getGame

public Game getGame(Player p)
Returns the Game associated with the given StartGame. The Game returned will be preloaded with the current state of the described by sg, if that game has been previously started. Otherwise, the game object will be initialized and ready for play.

Returns:
a suitable game object or null if the system does not have a provider for this type of game
Throws:
java.lang.NullPointerException - if p is null.

localModelChange

public void localModelChange(GameChangedEvent gse)
Invoked when a game changes state due to the local user's action. This method writes the change to the backing store, and sends an email to the opponent reflecting the changes we made to the model.

Specified by:
localModelChange in interface GameListener
Parameters:
gse - The event that represents the model change.

remoteModelChange

public void remoteModelChange(GameChangedEvent gse)
Invoked when a game changes state due to the remote user's action. This method writes the change to the backing store, but does not send a notification back to the opponent.

Specified by:
remoteModelChange in interface GameListener
Parameters:
gse - The event that represents the model change.

talx x1.0.5

© 2001-2005 University of Oregon.
Eugene OR 97403, U.S.A. All Rights Reserved.