server.framework.models
Class AbstractServer<S extends AbstractSession<P,?>,P extends AbstractPlayer>

java.lang.Object
  extended by server.framework.models.AbstractServer<S,P>
Direct Known Subclasses:
BBServer

public abstract class AbstractServer<S extends AbstractSession<P,?>,P extends AbstractPlayer>
extends java.lang.Object

This class is the top-level model for the server application. This model contains a list of all the players connected to the server, as well as a list of the currently running sessions. In addition, the server model is responsible for receiving notifications from the communication module when messages are received.


Nested Class Summary
static class AbstractServer.CommType
          This enum contains the support for tcp and udp communication specified in the arguments when starting the server application.
 
Constructor Summary
AbstractServer(int port, AbstractServer.CommType commType, TestModule.TestType testType)
          Creates an AbstractServer object with the specified communication protocol and port number.
 
Method Summary
 P createPlayer(SocketWrapper socket)
          Creates a player object owning the specified SocketWrapper object.
protected abstract  P createPlayerInstance(SocketWrapper wrapper, TestModule.TestType testType)
          Creates a player object owning the specified SocketWrapper object.
protected abstract  S createSessionInstance(TestModule.TestType testtype)
          Creates a new session instance of the correct type.
 P findPlayer(SocketWrapper socket)
          Returns the player owning the specified SocketWrapper object.
 void firePlayerListChanged(AbstractSession session)
          Called when the player list has changed.
 void fireSessionClosed(int sessionId)
          Called when a session has been closed.
 AbstractServer.CommType getCommType()
           
 ServerGUI<S,P> getGUI()
           
 MessageParser getParser()
           
 TestModule.TestType getTestType()
           
 void handleDisconnect(P sender)
          Method that is called whenever a disconnection notification is received.
 void notifyAboutMessageReceived(java.lang.Object word, SocketWrapper socket)
          Method that is called whenever one of the communication objects receives a message from a client.
abstract  boolean notifyAboutSpecialMessageReceived(ActionPair actionPair, P sender, S session)
          Method that must be implemented in classes extendint this class.
 void runTest()
          Starts a test run of the type specified by the arguments when starting the server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractServer

public AbstractServer(int port,
                      AbstractServer.CommType commType,
                      TestModule.TestType testType)
Creates an AbstractServer object with the specified communication protocol and port number. The communication channel is opened, and the server GUI is created. In addition, the player and session lists are initialised.

Parameters:
port - The port number on which the server runs.
commType - The transport protocol to use.
testType - The test type, if any.
Method Detail

createSessionInstance

protected abstract S createSessionInstance(TestModule.TestType testtype)
Creates a new session instance of the correct type.

Parameters:
testtype - The test type, if any.
Returns:
The new session instance.

getParser

public MessageParser getParser()
Returns:
The currently used message parser implementation.

getGUI

public ServerGUI<S,P> getGUI()
Returns:
The server GUI.

getCommType

public AbstractServer.CommType getCommType()
Returns:
The transport protocol (TCP or UDP).

notifyAboutMessageReceived

public void notifyAboutMessageReceived(java.lang.Object word,
                                       SocketWrapper socket)
Method that is called whenever one of the communication objects receives a message from a client. The MessageParser instance is used to parse the message, and actions are taken dependent of the contents of the message.

Parameters:
word - The message that was received.
socket - The SocketWrapper object that received the message.

notifyAboutSpecialMessageReceived

public abstract boolean notifyAboutSpecialMessageReceived(ActionPair actionPair,
                                                          P sender,
                                                          S session)
Method that must be implemented in classes extendint this class. This method handles actions that are not specified among the default framework actions.

Parameters:
actionPair - The contents of the received message.
sender - The sender of the message.
session - The session to which the message applies.
Returns:
True if the message should be forwarded to all clients.

findPlayer

public P findPlayer(SocketWrapper socket)
Returns the player owning the specified SocketWrapper object. Used to determine the sender of a message.

Parameters:
socket - The socket where the message was received.
Returns:
The player "owning" the socket.

handleDisconnect

public void handleDisconnect(P sender)
Method that is called whenever a disconnection notification is received. Removes the player from the player lists and notifies the relevant session of the disconnection.

Parameters:
sender - The player that disconnected.

createPlayer

public P createPlayer(SocketWrapper socket)
Creates a player object owning the specified SocketWrapper object.

Parameters:
socket - The player's socket.
Returns:
A new player instance.

createPlayerInstance

protected abstract P createPlayerInstance(SocketWrapper wrapper,
                                          TestModule.TestType testType)
Creates a player object owning the specified SocketWrapper object. Is used to create the correct kind of player object for the framework implementation.

Parameters:
wrapper - The player's socket wrapper.
testType - The test type, if any.
Returns:
A new player instance.

firePlayerListChanged

public void firePlayerListChanged(AbstractSession session)
Called when the player list has changed. Notifies the relevant session of the change.

Parameters:
session - The session affected by the event.

getTestType

public TestModule.TestType getTestType()
Returns:
The test type currently used.

runTest

public void runTest()
Starts a test run of the type specified by the arguments when starting the server.


fireSessionClosed

public void fireSessionClosed(int sessionId)
Called when a session has been closed. Removes the session from the session list and the server GUI.

Parameters:
sessionId - The session that is to be removed.