server.framework.models
Class AbstractSession<P extends AbstractPlayer,EH extends EventHandler>

java.lang.Object
  extended by server.framework.models.AbstractSession<P,EH>
Direct Known Subclasses:
BBSession

public abstract class AbstractSession<P extends AbstractPlayer,EH extends EventHandler>
extends java.lang.Object

This class represents a session. It contains a list of the players connected to the session, and keeps track of the settings applying to the session. Also, this model is responsible for controlling the currently active game threads when running a game, and notifying the EventHandler implementation when a message is received that is likely to cause a game event to occur.

Author:
Martin Jarrett, Eivind Sorteberg

Field Summary
static long ALIVE_REQUEST_INTERVAL
           
protected  EH eventHandler
           
protected  AbstractServer<?,P> server
           
 
Constructor Summary
AbstractSession(AbstractServer<?,P> server, TestModule.TestType testtype)
          Creates a session, and initialises the variables.
 
Method Summary
 void addPlayer(P newPlayer)
          Adds a new player to the session.
 void checkResolution()
          Loops through all player's resolution, and finds the maximum resolution that will fit with all connected client phones.
protected abstract  EH createEventHandler()
          Creates an EventHandler instance of the correct type.
 void exit()
          Called when the session is closed.
 void firePlayerListChanged()
          Called when the player list has changed, e.g. because a player has scored.
 void firePositionReceived(AbstractPlayer sender, java.lang.String[] position)
          Called when a position is received from one of the session's players.
 void fireSettingChanged(java.lang.String[][] values)
          Called when one or more setting values have been changed.
 java.lang.Object[] getAttributes()
          Returns the session's id, name, current number of players and maximum number of players.
 java.lang.Integer getId()
           
 int getMinimumHeight()
           
 int getMinimumWidth()
           
 java.lang.String getName()
           
 java.util.List<P> getPlayers()
           
 java.lang.String[] getScreenResolution()
           
 AbstractServer getServer()
           
 SessionPanel getSessionPanel()
           
 SettingsList getSettings()
           
 TestModule.TestType getTestType()
           
 void handleDisconnect(P player)
          Called when a player disconnects from the session.
 boolean isGameStarted()
           
abstract  void notifyAboutEventOccured(Event event, P sender, java.lang.Object[] values)
          Called when an event has occurred that the EventHandler thread should handle.
 void runTest()
          Runs the test when a test has been specified.
 void sendAction(Action action, java.lang.Object[][] values, AbstractPlayer sender, boolean skipSender, boolean confirm)
          Sends the specified action to all connected players, except the sender of the action, if he does not need to receive the message.
 void sendAction(Action action, java.lang.Object[][] values, boolean confirm)
          Sends the specified action to all connected players.
 void setPlayerPosition(P player, int[] position)
          Sets the position of the specified player.
 void setSessionPanel(SessionPanel<P> sessionPanel)
          Sets this session's SessionPanel in the server GUI.
 void startGame()
          Starts a game.
 void stopGame(java.lang.String reason)
          Method that is called when the game has been stopped for some reason.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ALIVE_REQUEST_INTERVAL

public static final long ALIVE_REQUEST_INTERVAL
See Also:
Constant Field Values

server

protected AbstractServer<?,P extends AbstractPlayer> server

eventHandler

protected EH extends EventHandler eventHandler
Constructor Detail

AbstractSession

public AbstractSession(AbstractServer<?,P> server,
                       TestModule.TestType testtype)
Creates a session, and initialises the variables.

Parameters:
server - The server to which this session belongs.
testtype - The test type, if any.
Method Detail

setSessionPanel

public void setSessionPanel(SessionPanel<P> sessionPanel)
Sets this session's SessionPanel in the server GUI.

Parameters:
sessionPanel - This session's SessionPanel

addPlayer

public void addPlayer(P newPlayer)
Adds a new player to the session. If teams are enabled, the player is told which team he belongs to. Also, the new player receives player information on the other player's connected to the session. Finally, the other player's connected to the session are notified that a new player has entered.

Parameters:
newPlayer - The new player.

getTestType

public TestModule.TestType getTestType()
Returns:
The current test type, if any.

handleDisconnect

public void handleDisconnect(P player)
Called when a player disconnects from the session. Notifies the player himself that he has been removed, and also notifies the other connected clients. If the player was the last player in the session, the session is closed, and the resources are released.

Parameters:
player - The disconnected player.

stopGame

public void stopGame(java.lang.String reason)
Method that is called when the game has been stopped for some reason. All connected players are notified about the event, and the game threads are stopped.

Parameters:
reason - The reason why the game was stopped.

fireSettingChanged

public void fireSettingChanged(java.lang.String[][] values)
Called when one or more setting values have been changed. Updates the settings list and the settings panel.

Parameters:
values - The new setting values.

firePlayerListChanged

public void firePlayerListChanged()
Called when the player list has changed, e.g. because a player has scored. Updates the player panel.


getMinimumWidth

public int getMinimumWidth()
Returns:
The width of the slimmest client screen connected to the session.

getMinimumHeight

public int getMinimumHeight()
Returns:
The height of the lowest client screen connected to the session.

getScreenResolution

public java.lang.String[] getScreenResolution()
Returns:
The width and height that shall be used in the game.

exit

public void exit()
Called when the session is closed. Stops the thread responsible for sending alive requests.


getPlayers

public java.util.List<P> getPlayers()
Returns:
The players connected to the session.

getSettings

public SettingsList getSettings()
Returns:
The settings applying to the session.

checkResolution

public void checkResolution()
Loops through all player's resolution, and finds the maximum resolution that will fit with all connected client phones.


sendAction

public void sendAction(Action action,
                       java.lang.Object[][] values,
                       boolean confirm)
Sends the specified action to all connected players.

Parameters:
action - The action to send.
values - The values related to the action.
confirm - Whether the receiver(s) should confirm the receival.

sendAction

public void sendAction(Action action,
                       java.lang.Object[][] values,
                       AbstractPlayer sender,
                       boolean skipSender,
                       boolean confirm)
Sends the specified action to all connected players, except the sender of the action, if he does not need to receive the message.

Parameters:
action - The action to send.
values - The values related to the action.
sender - The sender of the action.
skipSender - Whether the sender should receive the message or not.
confirm - Whether the receiver(s) should confirm the receival.

getId

public java.lang.Integer getId()
Returns:
The session's id.

getAttributes

public java.lang.Object[] getAttributes()
Returns the session's id, name, current number of players and maximum number of players.

Returns:
The session's attributes.

getName

public java.lang.String getName()
Returns:
The session's name.

startGame

public void startGame()
Starts a game. Starts the game thread and event thread. When the event thread has performed its pre-game calculations, a start command is transmitted to all connected clients.


createEventHandler

protected abstract EH createEventHandler()
Creates an EventHandler instance of the correct type.

Returns:
An EventHandler instance.

getServer

public AbstractServer getServer()
Returns:
The server to which this session belongs.

firePositionReceived

public void firePositionReceived(AbstractPlayer sender,
                                 java.lang.String[] position)
Called when a position is received from one of the session's players. Sends this position to the game thread, who will then forward the position next time it sends a position bundle.

Parameters:
sender - The sender of the position.
position - The position that was received.

isGameStarted

public boolean isGameStarted()
Returns:
True if a game is in progress, false if not.

setPlayerPosition

public void setPlayerPosition(P player,
                              int[] position)
Sets the position of the specified player.

Parameters:
player - The player whose position to update.
position - The position of the player.

getSessionPanel

public SessionPanel getSessionPanel()
Returns:
The session's session panel.

runTest

public void runTest()
Runs the test when a test has been specified.


notifyAboutEventOccured

public abstract void notifyAboutEventOccured(Event event,
                                             P sender,
                                             java.lang.Object[] values)
Called when an event has occurred that the EventHandler thread should handle. If the session needs to perform actions because of the event, these actions are specified in the implementation of this method.

Parameters:
event - The event that has occured.
sender - The player that caused the event.
values - The values related to the event.