jfreerails.network
Class FreerailsGameServer

java.lang.Object
  extended by jfreerails.network.FreerailsGameServer
All Implemented Interfaces:
java.lang.Runnable, ServerControlInterface, GameServer, GameModel

public class FreerailsGameServer
extends java.lang.Object
implements ServerControlInterface, GameServer, java.lang.Runnable

When executed by a thread, this class does the following: reads and executes moves and commands received from connected clients; sends moves and commands to connected clients.

Author:
Luke
See Also:
InetConnectionAccepter, Connection2Client

Field Summary
private  java.util.HashMap<NameAndPassword,Connection2Client> acceptedConnections
           
private  int commandID
           
private  int confirmationID
          ID of the last SetWorldMessage2Client sent out.
private  java.util.HashSet<NameAndPassword> confirmedPlayers
          The players who have confirmed that they have received the last copy of the world object sent.
static java.lang.String CONNECTED_PLAYERS
          Used as a property name for property change events.
private  java.util.HashSet<NameAndPassword> currentlyLoggedOn
           
private static java.util.logging.Logger logger
           
private  boolean newPlayersAllowed
           
private  java.util.ArrayList<NameAndPassword> players
           
private  java.beans.PropertyChangeSupport propertyChangeSupport
           
private  SavedGamesManager savedGamesManager
           
private  ServerGameModel serverGameModel
           
private  SynchronizedFlag status
           
 
Fields inherited from interface jfreerails.controller.ServerControlInterface
FREERAILS_SAV, VERSION
 
Constructor Summary
FreerailsGameServer(SavedGamesManager gamesManager)
           
 
Method Summary
 void addConnection(Connection2Client connection)
           
 void addPropertyChangeListener(java.beans.PropertyChangeListener l)
           
 int countOpenConnections()
           
(package private)  World getCopyOfWorld()
           
private  int getNextClientCommandId()
           
 java.lang.String[] getPlayerNames()
           
private  World getWorld()
           
(package private)  boolean isConfirmed(int player)
           
 boolean isNewPlayersAllowed()
           
private  boolean isPlayer(java.lang.String username)
           
 void loadgame(java.lang.String saveGameName)
           
 void logoff(int player)
           
 LogOnResponse logon(LogOnRequest lor)
           
 void newGame(java.lang.String mapName)
           
 void newGame(java.lang.String mapName, int numAI)
           
 void refreshSavedGames()
           
private  void removeConnection(NameAndPassword p)
           
 void removePropertyChangeListener(java.beans.PropertyChangeListener l)
           
 void run()
           
 void savegame(java.lang.String saveGameName)
           
private  void send2All(FreerailsSerializable message)
           
private  void send2AllExcept(Connection2Client dontSend2, FreerailsSerializable message)
          Sends the specified message to all connections except the specified one.
private  void sendListOfConnectedPlayers2Clients()
           
private  void sendWorldUpdatedCommand()
           
 void setNewPlayersAllowed(boolean newPlayersAllowed)
           
 void setServerGameModel(ServerGameModel serverGameModel)
           
static FreerailsGameServer startServer(SavedGamesManager gamesManager)
           
 void stop()
           
 void stopGame()
           
 void update()
          Updates the game model, then reads and deals with the outstanding messages from each of the connected clients.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONNECTED_PLAYERS

public static final java.lang.String CONNECTED_PLAYERS
Used as a property name for property change events.

See Also:
Constant Field Values

logger

private static final java.util.logging.Logger logger

acceptedConnections

private final java.util.HashMap<NameAndPassword,Connection2Client> acceptedConnections

commandID

private int commandID

confirmationID

private int confirmationID
ID of the last SetWorldMessage2Client sent out. Used to keep track of which clients have updated their world object to the current version.


confirmedPlayers

private java.util.HashSet<NameAndPassword> confirmedPlayers
The players who have confirmed that they have received the last copy of the world object sent.


currentlyLoggedOn

private java.util.HashSet<NameAndPassword> currentlyLoggedOn

newPlayersAllowed

private boolean newPlayersAllowed

players

private java.util.ArrayList<NameAndPassword> players

propertyChangeSupport

private final java.beans.PropertyChangeSupport propertyChangeSupport

savedGamesManager

private final SavedGamesManager savedGamesManager

serverGameModel

private ServerGameModel serverGameModel

status

private final SynchronizedFlag status
Constructor Detail

FreerailsGameServer

public FreerailsGameServer(SavedGamesManager gamesManager)
Method Detail

startServer

public static FreerailsGameServer startServer(SavedGamesManager gamesManager)

addConnection

public void addConnection(Connection2Client connection)
Specified by:
addConnection in interface GameServer

addPropertyChangeListener

public void addPropertyChangeListener(java.beans.PropertyChangeListener l)

countOpenConnections

public int countOpenConnections()
Specified by:
countOpenConnections in interface GameServer

getCopyOfWorld

World getCopyOfWorld()

getNextClientCommandId

private int getNextClientCommandId()

getPlayerNames

public java.lang.String[] getPlayerNames()

getWorld

private World getWorld()

isConfirmed

boolean isConfirmed(int player)

isNewPlayersAllowed

public boolean isNewPlayersAllowed()

isPlayer

private boolean isPlayer(java.lang.String username)

loadgame

public void loadgame(java.lang.String saveGameName)
              throws java.io.IOException
Specified by:
loadgame in interface ServerControlInterface
Throws:
java.io.IOException

logoff

public void logoff(int player)

logon

public LogOnResponse logon(LogOnRequest lor)

newGame

public void newGame(java.lang.String mapName,
                    int numAI)

newGame

public void newGame(java.lang.String mapName)
Specified by:
newGame in interface ServerControlInterface

removeConnection

private void removeConnection(NameAndPassword p)
                       throws java.io.IOException
Throws:
java.io.IOException

removePropertyChangeListener

public void removePropertyChangeListener(java.beans.PropertyChangeListener l)

run

public void run()
Specified by:
run in interface java.lang.Runnable

savegame

public void savegame(java.lang.String saveGameName)
Specified by:
savegame in interface ServerControlInterface

send2All

private void send2All(FreerailsSerializable message)

send2AllExcept

private void send2AllExcept(Connection2Client dontSend2,
                            FreerailsSerializable message)
Sends the specified message to all connections except the specified one.


sendListOfConnectedPlayers2Clients

private void sendListOfConnectedPlayers2Clients()
                                         throws java.io.IOException
Throws:
java.io.IOException

sendWorldUpdatedCommand

private void sendWorldUpdatedCommand()

setNewPlayersAllowed

public void setNewPlayersAllowed(boolean newPlayersAllowed)

setServerGameModel

public void setServerGameModel(ServerGameModel serverGameModel)

stop

public void stop()
Specified by:
stop in interface GameServer

stopGame

public void stopGame()
Specified by:
stopGame in interface ServerControlInterface

update

public void update()
Updates the game model, then reads and deals with the outstanding messages from each of the connected clients. This method is synchronized to prevent moves being sent out while addConnection(.) is executing.

Specified by:
update in interface GameModel

refreshSavedGames

public void refreshSavedGames()
Specified by:
refreshSavedGames in interface ServerControlInterface