thegame.server
Class GameServer

java.lang.Object
  |
  +--thegame.server.GameServer
All Implemented Interfaces:
GameConnectionConstants

public class GameServer
extends java.lang.Object
implements GameConnectionConstants

The central server that the GameClients connect to.

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

Field Summary
protected  java.lang.String administratorPassword
          The administrator password.
protected  java.util.ArrayList connections
          Is used to hold all the ongoing GameServerConnections.
static java.lang.String[] FORBIDDEN_PLAYER_NAME_SUFFIXES
           
static int MAXIMUM_PLAYER_NAME_LENGTH
           
static int MINIMUM_PLAYER_NAME_LENGTH
           
static java.lang.String PASSWORD_CORRECT_MESSAGE
          The message displayed if the administrator password was correct.
static java.lang.String PASSWORD_NOT_CORRECT_MESSAGE
          The message displayed if the administrator password was not correct.
static java.lang.String PLAYER_NAME_CONTAINS_FORBIDDEN_TEXT_MESSAGE
          The error message sent if the player entered a name that contains forbidden text.
static java.lang.String PLAYER_NAME_IN_USE_MESSAGE
          The error message sent if the player entered a name that is already in use.
static java.lang.String PLAYER_NAME_OF_FORBIDDEN_LENGTH_MESSAGE
          The error message sent if the player entered a name of forbidden length.
protected  java.net.ServerSocket serverSocket
          The socket that accepts incoming connections.
static java.lang.String STARTUP_MESSAGE
          The message displayed when GameServer starts.
static java.lang.String STARTUP_PASSWORD_PROMPT_MESSAGE
          The password prompt shown at startup.
protected  java.lang.String waitingPlayersNamesList
          Holds the names of the waiting players in a ready-to-send form.
 
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
GameServer(int port)
          Constructs a GameServer listening on the specified port.
 
Method Summary
 void handleCreateGameMessage(GameServerConnection sender, boolean isPlayerDrawingCircles)
          Handles a message to create a new game.
 void handleJoinGameMessage(GameServerConnection sender, java.lang.String remoteName)
          Handles a message to join a game.
 void handlePlayerMessage(GameServerConnection sender, java.lang.String message)
          Handles the messages sent from player to system (i.e. from GameClientConnection to GameServer).
 void handlePlayerNameMessage(GameServerConnection sender, java.lang.String name)
          Handles a message that establishes the name of a player.
 void handlePlayerQuitMessage(GameServerConnection sender)
          Handles a message that reports the departure of a player.
 void handlePlayerReturnedToStartScreenMessage(GameServerConnection sender)
          Handles a message that reports that a player returned to start screen
 void handlePlayerWaitedForNewPlayerMessage(GameServerConnection sender)
           
 void handleReadyForPlayerListMessage(GameServerConnection sender)
          Handles a message that reports that a player is ready to receive a playerlist.
 void handleShutdownMessage(GameServerConnection sender, java.lang.String password)
          Shuts down GameServer if the password is correct.
static void main(java.lang.String[] args)
          Starts up a GameServer on the local machine.
 void sendWaitingPlayersNames()
          Sends the names of the waiting players to all players who are able to see the list of the waiting players.
 void updateWaitingPlayersNames()
          Updates the names of the waiting players.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

administratorPassword

protected java.lang.String administratorPassword
The administrator password. Is entered at startup and used when shutting down GameServer.

connections

protected java.util.ArrayList connections
Is used to hold all the ongoing GameServerConnections.

FORBIDDEN_PLAYER_NAME_SUFFIXES

public static final java.lang.String[] FORBIDDEN_PLAYER_NAME_SUFFIXES

MAXIMUM_PLAYER_NAME_LENGTH

public static final int MAXIMUM_PLAYER_NAME_LENGTH

MINIMUM_PLAYER_NAME_LENGTH

public static final int MINIMUM_PLAYER_NAME_LENGTH

PASSWORD_CORRECT_MESSAGE

public static final java.lang.String PASSWORD_CORRECT_MESSAGE
The message displayed if the administrator password was correct.

PASSWORD_NOT_CORRECT_MESSAGE

public static final java.lang.String PASSWORD_NOT_CORRECT_MESSAGE
The message displayed if the administrator password was not correct.

PLAYER_NAME_CONTAINS_FORBIDDEN_TEXT_MESSAGE

public static final java.lang.String PLAYER_NAME_CONTAINS_FORBIDDEN_TEXT_MESSAGE
The error message sent if the player entered a name that contains forbidden text.

PLAYER_NAME_IN_USE_MESSAGE

public static final java.lang.String PLAYER_NAME_IN_USE_MESSAGE
The error message sent if the player entered a name that is already in use.

PLAYER_NAME_OF_FORBIDDEN_LENGTH_MESSAGE

public static final java.lang.String PLAYER_NAME_OF_FORBIDDEN_LENGTH_MESSAGE
The error message sent if the player entered a name of forbidden length.

serverSocket

protected java.net.ServerSocket serverSocket
The socket that accepts incoming connections.

STARTUP_MESSAGE

public static final java.lang.String STARTUP_MESSAGE
The message displayed when GameServer starts.

STARTUP_PASSWORD_PROMPT_MESSAGE

public static final java.lang.String STARTUP_PASSWORD_PROMPT_MESSAGE
The password prompt shown at startup.

waitingPlayersNamesList

protected java.lang.String waitingPlayersNamesList
Holds the names of the waiting players in a ready-to-send form. Is used as a cache to avoid the overhead of constructing it even when there has been no change.
Constructor Detail

GameServer

public GameServer(int port)
           throws java.io.IOException
Constructs a GameServer listening on the specified port. The constructor prompts for an administrator password, opens a new ServerSocket on the local machine and starts waiting for connections.
Parameters:
port - the port to be listened on
Throws:
java.io.IOException - if an I/O error occurs
Method Detail

handleCreateGameMessage

public void handleCreateGameMessage(GameServerConnection sender,
                                    boolean isPlayerDrawingCircles)
Handles a message to create a new game.
Parameters:
sender - the GameServerConnection that sent the message

handleJoinGameMessage

public void handleJoinGameMessage(GameServerConnection sender,
                                  java.lang.String remoteName)
Handles a message to join a game.
Parameters:
sender - the GameServerConnection that sent the message

handlePlayerMessage

public void handlePlayerMessage(GameServerConnection sender,
                                java.lang.String message)
Handles the messages sent from player to system (i.e. from GameClientConnection to GameServer).
Parameters:
sender - the GameServerConnection that sent the message
message - the message sent by the player

handlePlayerNameMessage

public void handlePlayerNameMessage(GameServerConnection sender,
                                    java.lang.String name)
Handles a message that establishes the name of a player. Tests the validity of the name (its length etc) and checks its uniqueness among the waiting players. The player is informed whether the name was acceptable or not.
Parameters:
sender - the GameServerConnection that sent the message
name - the entered name

handlePlayerQuitMessage

public void handlePlayerQuitMessage(GameServerConnection sender)
Handles a message that reports the departure of a player.
Parameters:
sender - the GameServerConnection that sent the message

handlePlayerReturnedToStartScreenMessage

public void handlePlayerReturnedToStartScreenMessage(GameServerConnection sender)
Handles a message that reports that a player returned to start screen
Parameters:
sender - the GameServerConnection that sent the message

handlePlayerWaitedForNewPlayerMessage

public void handlePlayerWaitedForNewPlayerMessage(GameServerConnection sender)

handleReadyForPlayerListMessage

public void handleReadyForPlayerListMessage(GameServerConnection sender)
Handles a message that reports that a player is ready to receive a playerlist.
Parameters:
sender - the GameServerConnection that sent the message

handleShutdownMessage

public void handleShutdownMessage(GameServerConnection sender,
                                  java.lang.String password)
Shuts down GameServer if the password is correct.
Parameters:
sender - the GameServerConnection that sent the message
password - the administrator password

main

public static void main(java.lang.String[] args)
Starts up a GameServer on the local machine. The server is started on the default port or on the port that is specified in the first argument passed to the method.

sendWaitingPlayersNames

public void sendWaitingPlayersNames()
Sends the names of the waiting players to all players who are able to see the list of the waiting players.

updateWaitingPlayersNames

public void updateWaitingPlayersNames()
Updates the names of the waiting players.