Class Server

java.lang.Object
  extended byjava.rmi.server.RemoteObject
      extended byjava.rmi.server.RemoteServer
          extended byjava.rmi.server.UnicastRemoteObject
              extended byServer
All Implemented Interfaces:
java.rmi.Remote, java.io.Serializable, ServerInterface

public class Server
extends java.rmi.server.UnicastRemoteObject
implements ServerInterface

Class Server implements the server-side of RMI.

This includes all the players, races and tracks. Their creation, deletion, joining, loading and changing.

Implements ServerInterface RMI interface. When the client program is run and it joins to the server, ServerInterface is the interface through which the client can communicate with the server. The client receives an object that implements the ServerInterface and can make direct method invocations to the server through the methods in this interface.

Version:
1.0
Author:
Stefan Brockmann, Tero Kuusela
See Also:
ServerInterface, Player, Game, ServerCommandHandler, Serialized Form

Field Summary
private  java.util.ArrayList games
          List of the races on the server.
private static int next_unique_id
          Counter for the unique id.
private  java.util.ArrayList players
          A list of the players that are on the server
private static java.rmi.registry.Registry registry
          Registry
private  java.lang.String[] track_list
          A list of the names of the tracks that can be used.
static java.lang.String version
           
 
Fields inherited from class java.rmi.server.UnicastRemoteObject
 
Fields inherited from class java.rmi.server.RemoteServer
 
Fields inherited from class java.rmi.server.RemoteObject
ref
 
Constructor Summary
Server()
          The constructor for the server.
 
Method Summary
 void addGame(Game g)
          Adds a race to the list on the server.
 void addPlayer(Player p)
          Adds a player to the list on the server.
 void changeTrack(int game_id, java.lang.String track_name)
          Loads a new track to a race (if it's not in a racing state).
 void cleanup()
          Kicks the players from the server and ends all races.
 int createGame(int player_id, java.lang.String game_name)
          Creates a new race and places the player in it.
 void gameListChanged()
          Sends an updated race list to all players.
 Game getGame(int game_id)
          Returns the specified Game object.
 Game getGameById(int id)
          Returns the race with the given id.
 GameInfo[] getGameList()
          Collects a race list.
 java.util.Iterator getGamesIterator()
          Returns an iterator for the races.
 int getNextUniqueId()
          Returns the next unused unique_id.
 int getNumGames()
          Returns the number of races.
 int getNumPlayers()
          Returns the number of players on the server.
 Player getPlayer(int player_id)
          Returns the specified Player object.
 Player getPlayerById(int player_id)
          Returns the player with the given id.
 java.util.Iterator getPlayersIterator()
          Returns an iterator for the players.
 boolean joinGame(int player_id, int game_id)
          Places the player in a race.
 void leaveGame(int player_id, int game_id)
          Exits a race.
 int loginPlayer(java.lang.String name, ClientInterface cobj)
          Logs the player in to the server.
 void logoutPlayer(int player_id)
          Logs the player out from the server.
static void main(java.lang.String[] args)
          Main.
 void removeGame(Game g)
          Removes a race from the list on the server.
 void removePlayer(Player p)
          Removes a player from the list on the server.
 void sayToAll(java.lang.String message)
          Sends a chat message to everybody.
 void sayToGame(int game_id, java.lang.String message)
          Sends a chat message to the specified race.
 void sayToLobby(java.lang.String message)
          Sends a chat message to everybody in the lobby room.
 void setSail(int player_id, int sail)
          Adjusts a sail.
 void startGame(int game_id)
          Starts racing in a race.
 void turnLeft(int player_id)
          Turns a boat portboard.
 void turnRight(int player_id)
          Turns a boat starboard.
 void updateTrackList()
          Updates the track list by reading the names of all tracks from the tracklist file in the tracks directory.
 
Methods inherited from class java.rmi.server.UnicastRemoteObject
clone, exportObject, exportObject, exportObject, unexportObject
 
Methods inherited from class java.rmi.server.RemoteServer
getClientHost, getLog, setLog
 
Methods inherited from class java.rmi.server.RemoteObject
equals, getRef, hashCode, toString, toStub
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

version

public static final java.lang.String version
See Also:
Constant Field Values

registry

private static java.rmi.registry.Registry registry
Registry


players

private java.util.ArrayList players
A list of the players that are on the server


games

private java.util.ArrayList games
List of the races on the server.


next_unique_id

private static int next_unique_id
Counter for the unique id.

Each new player and race receives a new, unused unique_id value.


track_list

private java.lang.String[] track_list
A list of the names of the tracks that can be used.

Constructor Detail

Server

public Server()
       throws java.rmi.RemoteException
The constructor for the server.

Method Detail

addPlayer

public void addPlayer(Player p)
Adds a player to the list on the server.

Parameters:
p - the handler object of the player to add

removePlayer

public void removePlayer(Player p)
Removes a player from the list on the server.

Parameters:
p - the handler object of the player to remove

getNumPlayers

public int getNumPlayers()
Returns the number of players on the server.

Returns:
The number of players on the server.

getPlayersIterator

public java.util.Iterator getPlayersIterator()
Returns an iterator for the players.

Returns:
Player iterator.

getPlayerById

public Player getPlayerById(int player_id)
Returns the player with the given id.

Parameters:
player_id - the id of the player to return
Returns:
The player object with the given id or null if it doesn't exist.

addGame

public void addGame(Game g)
Adds a race to the list on the server.


removeGame

public void removeGame(Game g)
Removes a race from the list on the server.


getNumGames

public int getNumGames()
Returns the number of races.

Returns:
The number of races.

getGamesIterator

public java.util.Iterator getGamesIterator()
Returns an iterator for the races.

Returns:
Race iterator.

getGameById

public Game getGameById(int id)
Returns the race with the given id.

Parameters:
id - the id of the race to return
Returns:
Reference to the player to return.

gameListChanged

public void gameListChanged()
Sends an updated race list to all players.

This is executed when the race list has changed, i.e. a new race is created or an existing race is closed. Also executed when the state of a race is changed!

See Also:
Game.java

getGameList

public GameInfo[] getGameList()
Collects a race list.

Returns:
An array with information of each race.

getNextUniqueId

public int getNextUniqueId()
Returns the next unused unique_id.

Returns:
next unused unique_id

updateTrackList

public void updateTrackList()
Updates the track list by reading the names of all tracks from the tracklist file in the tracks directory.


main

public static void main(java.lang.String[] args)
Main.

Parameters:
args - command line parameters given

cleanup

public void cleanup()
Kicks the players from the server and ends all races.


loginPlayer

public int loginPlayer(java.lang.String name,
                       ClientInterface cobj)
                throws java.rmi.RemoteException
Logs the player in to the server.

Specified by:
loginPlayer in interface ServerInterface
Parameters:
name - player name
cobj - client's player object
Returns:
The unique id for this player.
Throws:
java.rmi.RemoteException

logoutPlayer

public void logoutPlayer(int player_id)
                  throws java.rmi.RemoteException
Logs the player out from the server.

Specified by:
logoutPlayer in interface ServerInterface
Parameters:
player_id - player id
Throws:
java.rmi.RemoteException

joinGame

public boolean joinGame(int player_id,
                        int game_id)
                 throws java.rmi.RemoteException
Places the player in a race.

Specified by:
joinGame in interface ServerInterface
Parameters:
player_id - player id
game_id - race id
Returns:
true if the player successfully joined the race, false otherwise.
Throws:
java.rmi.RemoteException

createGame

public int createGame(int player_id,
                      java.lang.String game_name)
               throws java.rmi.RemoteException
Creates a new race and places the player in it.

Specified by:
createGame in interface ServerInterface
Parameters:
player_id - player id
game_name - name of the race to create
Returns:
-1 if failed, game_id(<=0) if a new race was created and the player placed in it.
Throws:
java.rmi.RemoteException

leaveGame

public void leaveGame(int player_id,
                      int game_id)
               throws java.rmi.RemoteException
Exits a race.

Specified by:
leaveGame in interface ServerInterface
Parameters:
player_id - player id
game_id - race id
Throws:
java.rmi.RemoteException

startGame

public void startGame(int game_id)
               throws java.rmi.RemoteException
Starts racing in a race.

Only race master calls this.

Specified by:
startGame in interface ServerInterface
Parameters:
game_id - race id
Throws:
java.rmi.RemoteException

turnLeft

public void turnLeft(int player_id)
              throws java.rmi.RemoteException
Turns a boat portboard.

Specified by:
turnLeft in interface ServerInterface
Parameters:
player_id - player id
Throws:
java.rmi.RemoteException

turnRight

public void turnRight(int player_id)
               throws java.rmi.RemoteException
Turns a boat starboard.

Specified by:
turnRight in interface ServerInterface
Parameters:
player_id - player id
Throws:
java.rmi.RemoteException

setSail

public void setSail(int player_id,
                    int sail)
             throws java.rmi.RemoteException
Adjusts a sail.

Specified by:
setSail in interface ServerInterface
Parameters:
player_id - player id
sail - new sail value
Throws:
java.rmi.RemoteException

getGame

public Game getGame(int game_id)
             throws java.rmi.RemoteException
Returns the specified Game object.

Specified by:
getGame in interface ServerInterface
Parameters:
game_id - race id
Returns:
Game object.
Throws:
java.rmi.RemoteException

getPlayer

public Player getPlayer(int player_id)
                 throws java.rmi.RemoteException
Returns the specified Player object.

Specified by:
getPlayer in interface ServerInterface
Parameters:
player_id - player id
Returns:
Player object.
Throws:
java.rmi.RemoteException

sayToAll

public void sayToAll(java.lang.String message)
              throws java.rmi.RemoteException
Sends a chat message to everybody.

Specified by:
sayToAll in interface ServerInterface
Parameters:
message - chat message
Throws:
java.rmi.RemoteException

sayToLobby

public void sayToLobby(java.lang.String message)
                throws java.rmi.RemoteException
Sends a chat message to everybody in the lobby room.

Specified by:
sayToLobby in interface ServerInterface
Parameters:
message - chat message
Throws:
java.rmi.RemoteException

sayToGame

public void sayToGame(int game_id,
                      java.lang.String message)
               throws java.rmi.RemoteException
Sends a chat message to the specified race.

Specified by:
sayToGame in interface ServerInterface
Parameters:
game_id - the race to send the message to
message - chat message
Throws:
java.rmi.RemoteException

changeTrack

public void changeTrack(int game_id,
                        java.lang.String track_name)
                 throws java.rmi.RemoteException
Loads a new track to a race (if it's not in a racing state).

Specified by:
changeTrack in interface ServerInterface
Parameters:
game_id - race id
track_name - the name of the track to load
Throws:
java.rmi.RemoteException