org.moyoman.framework
Class FSPersister

java.lang.Object
  |
  +--org.moyoman.framework.FSPersister
All Implemented Interfaces:
Persister

class FSPersister
extends Object
implements Persister

This class saves and restores games using the file system. It implements the Persister interface, and uses the Java serialization functionality to accomplish this.


Field Summary
private static String LAST_GAME_FILE_NAME
          This is the name of the last game file.
private static String MOVE_PREFIX
          This string is used as part of the filename of a temporary game at a particular move.
private static String PERMANENT_DIR
          The directory under which permanent games are stored.
private static String PG_SUFFIX
          This is the suffix appended to a persisted game name to get the file name.
private static ServerConfig sc
          The ServerConfig object.
private static String TEMPORARY_DIR
          The directory under which temporary games are stored.
private static String topDir
          The top level directory where the moyoman distribution is installed.
 
Constructor Summary
(package private) FSPersister()
          Create a new FSPersister object.
 
Method Summary
private static void checkTempGames(long cutoff)
          Delete all temporary games which have expired.
private static void checkTempGames(ServerConfig sc)
          Delete all temporary games which have expired.
 void cleanup()
          Delete all temporary game files.
 void delete(PersistedGame game)
          Delete a game in permanent storage.
private  void deleteTemporaryState(GameId id)
           
 void deleteTemporaryState(PersistedGame game)
          Delete a game in temporary storage.
private  Controller getController(String fname)
          Deserialize the file and create a Controller object.
private  String getLastGameFileName()
           
private static String getPermanentDirectory()
          Get the name of the directory where the permanent games are stored.
private  String getPermanentFileName(GameId id)
           
private  String getPermanentFileName(String id)
          Get the filename for the game id in permanent storage.
private  PersistedGame getPersistedGame(String fname)
           
private  String getPersistedGameId(String fname, Color color)
           
private  String getSavedGameFileName(String name)
           
 PersistedGame getSavedPersistedGame(String name)
           
private static String getTemporaryDirectory()
          Get the name of the directory where the temporary games are stored.
private  String getTemporaryDirectoryName(GameId id)
           
private  String getTemporaryDirectoryName(String id)
          Get the directory name for the game id in temporary storage.
private  String getTemporaryFileName(GameId id)
          Get the name of the most recent file for the particular game.
private  String getTemporaryFileName(GameId id, int move)
          Get the filename for the game id in temporary storage.
private  String getTemporaryGameFileName(String name)
           
 PersistedGame getTemporaryPersistedGame(String name)
           
private  PersistedGame[] listGames(String fname)
           
 PersistedGame[] listPermanentGames()
          Return an array of all the games in permanent storage.
 PersistedGame[] listTemporaryGames()
          Return an array of all the games in temporary storage.
 boolean permanentStateExists(GameId id)
           
 boolean permanentStateExists(PersistedGame game)
          Return whether the game exists in permanent storage.
 Controller restore(GameId id)
          Restore a game from permanent storage.
 Controller restoreMostRecentTemporaryState(Color color)
          Restore the most recent game in temporary storage.
 Controller restoreTemporaryState(GameId id)
          Restore a game from temporary storage to the latest saved state.
 Controller restoreTemporaryState(GameId id, int number)
          Restore a game from temporary storage to the specified move.
private  void save(GameId id, Controller cont)
          Save the game to permanent storage.
 void save(PersistedGame game)
          Save the game to permanent storage.
private  void saveController(Controller c, String fname)
          Serialize the Controller object and write it out to disk.
 void saveTemporaryGame(GameId black, GameId white)
           
 void saveTemporaryState(GameId id, Controller cont, int move)
          Save the game to temporary storage for a particular move.
 boolean temporaryStateExists(GameId id)
           
 boolean temporaryStateExists(GameId id, int move)
          Return whether the game exists in temporary storage for the given move.
 boolean temporaryStateExists(PersistedGame game)
          Return whether the game exists in temporary storage.
private  void updateLastMove(GameId id, int move)
          Update the last_move file.
private  void writeGameFile(String fname, GameId black, GameId white)
           
private  void writeLastGameFile(GameId black, GameId white)
           
 void writeLastGameInformation(GameId black, GameId white)
           
private  void writeSavedGameFile(PersistedGame game)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TEMPORARY_DIR

private static final String TEMPORARY_DIR
The directory under which temporary games are stored.


PERMANENT_DIR

private static final String PERMANENT_DIR
The directory under which permanent games are stored.


MOVE_PREFIX

private static final String MOVE_PREFIX
This string is used as part of the filename of a temporary game at a particular move.

See Also:
Constant Field Values

PG_SUFFIX

private static String PG_SUFFIX
This is the suffix appended to a persisted game name to get the file name.


LAST_GAME_FILE_NAME

private static String LAST_GAME_FILE_NAME
This is the name of the last game file.


sc

private static final ServerConfig sc
The ServerConfig object.


topDir

private static final String topDir
The top level directory where the moyoman distribution is installed.

Constructor Detail

FSPersister

FSPersister()
      throws InternalErrorException
Create a new FSPersister object. This operation deletes all temporary games that have expired.

Throws:
InternalErrorException - Thrown if the operation fails for any reason.
Method Detail

getTemporaryDirectory

private static String getTemporaryDirectory()
Get the name of the directory where the temporary games are stored.

Returns:
a String which is the name of the temporary directory.

getPermanentDirectory

private static String getPermanentDirectory()
Get the name of the directory where the permanent games are stored.

Returns:
a String which is the name of the permanent directory.

checkTempGames

private static void checkTempGames(ServerConfig sc)
                            throws InternalErrorException
Delete all temporary games which have expired. This is based on the HOURS_TEMP_FILES_EXPIRED configuration parameter.

Parameters:
sc - The ServerConfig object.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.

checkTempGames

private static void checkTempGames(long cutoff)
                            throws InternalErrorException
Delete all temporary games which have expired.

Parameters:
cutoff - The time before which files should be deleted.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.

delete

public void delete(PersistedGame game)
            throws InternalErrorException,
                   PermissionsException,
                   NoSuchDataException
Delete a game in permanent storage.

Specified by:
delete in interface Persister
Parameters:
game - The game to delete.
Throws:
PermissionsException - Thrown if the delete operation fails.
NoSuchDataException - Thrown if the file does not exist.
InternalErrorException - Thrown if the operation fails for any reason.

deleteTemporaryState

public void deleteTemporaryState(PersistedGame game)
                          throws InternalErrorException,
                                 PermissionsException,
                                 NoSuchDataException
Delete a game in temporary storage.

Specified by:
deleteTemporaryState in interface Persister
Parameters:
game - The game to delete.
Throws:
PermissionsException - Thrown if the delete operation fails.
NoSuchDataException - Thrown if the file does not exist.
InternalErrorException - Thrown if the operation fails for any reason.

deleteTemporaryState

private void deleteTemporaryState(GameId id)
                           throws PermissionsException,
                                  NoSuchDataException
PermissionsException
NoSuchDataException

restore

public Controller restore(GameId id)
                   throws NoSuchDataException,
                          InternalErrorException
Restore a game from permanent storage. This involves reading in the serialized objects, and constructing a Controller object.

Specified by:
restore in interface Persister
Parameters:
id - The id of the game.
Returns:
The Controller object representing the game in its current state.
Throws:
NoSuchDataException - Thrown if the saved game does not exist.
InternalErrorException - Thrown if the operation fails for any reason.

restoreTemporaryState

public Controller restoreTemporaryState(GameId id)
                                 throws NoSuchDataException,
                                        InternalErrorException
Restore a game from temporary storage to the latest saved state. This involves reading in the serialized objects, and constructing a Controller object.

Specified by:
restoreTemporaryState in interface Persister
Parameters:
id - The id of the game.
Returns:
The controller object representing the game in its current state.
Throws:
NoSuchDataException - Thrown if the temporary game does not exist.
InternalErrorException - Thrown if the operation fails for any reason.

restoreMostRecentTemporaryState

public Controller restoreMostRecentTemporaryState(Color color)
                                           throws NoSuchDataException,
                                                  InternalErrorException
Restore the most recent game in temporary storage. This method is used to restart a game if the software exited unexpectedly, as if the computer crashed.

Specified by:
restoreMostRecentTemporaryState in interface Persister
Parameters:
color - The color of the player to restore.
Returns:
A Controller object.
Throws:
NoSuchDataException - Thrown if no temporary game exists.
InternalErrorException - Thrown if the operation fails for any reason.

restoreTemporaryState

public Controller restoreTemporaryState(GameId id,
                                        int number)
                                 throws NoSuchDataException,
                                        InternalErrorException
Restore a game from temporary storage to the specified move. This would be used for taking back one or more moves.

Specified by:
restoreTemporaryState in interface Persister
Parameters:
id - The id of the game.
number - The move number to restore to.
Returns:
The Controller object representing the game in its current state.
Throws:
NoSuchDataException - Thrown if the information for the temporary game and move do not exist.
InternalErrorException - Thrown if the operation fails for any reason.

save

private void save(GameId id,
                  Controller cont)
           throws DataAlreadyExistsException,
                  InternalErrorException
Save the game to permanent storage.

Parameters:
id - The name to save the game as.
cont - The controller with the game information.
Throws:
DataAlreadyExistsException - Thrown if the id is already used.
InternalErrorException - Thrown if the operation fails for any reason.

save

public void save(PersistedGame game)
          throws DataAlreadyExistsException,
                 InternalErrorException
Description copied from interface: Persister
Save the game to permanent storage.

Specified by:
save in interface Persister
Parameters:
game - The game to be saved.
Throws:
DataAlreadyExistsException - Thrown if the new id already exists.
InternalErrorException - Thrown if the operation fails for any reason.

saveTemporaryGame

public void saveTemporaryGame(GameId black,
                              GameId white)
                       throws InternalErrorException
Specified by:
saveTemporaryGame in interface Persister
InternalErrorException

saveTemporaryState

public void saveTemporaryState(GameId id,
                               Controller cont,
                               int move)
                        throws InternalErrorException
Save the game to temporary storage for a particular move. This operation will be called frequently throughout the game, after every move or every other move. Therefore, this method is also responsible for deleting the state information for older moves, depending on how many previous moves are to be saved as defined in the configuration files.

Specified by:
saveTemporaryState in interface Persister
Parameters:
id - The id of the game
cont - The Controller with the state information.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.

updateLastMove

private void updateLastMove(GameId id,
                            int move)
                     throws NoSuchDataException,
                            InternalErrorException
Update the last_move file. This file is only used for temporary storage. When restoring a game, the file with the largest move number is not always the last move. For example, if the user has taken back one or more moves, then the higher numbers are no longer valid. Another example is if the software crashed in the middle of saving the state. This method is called after the serialization operation has completed, so if this file is updated with a move, it is known to be valid.

Throws:
NoSuchDataException - Thrown if the information for the temporary game and move do not exist.
InternalErrorException - Thrown if the operation fails for any reason.

permanentStateExists

public boolean permanentStateExists(GameId id)
Specified by:
permanentStateExists in interface Persister

permanentStateExists

public boolean permanentStateExists(PersistedGame game)
Return whether the game exists in permanent storage.

Specified by:
permanentStateExists in interface Persister
Parameters:
game - The game being checked.
Returns:
true if the game exists, or false.

temporaryStateExists

public boolean temporaryStateExists(GameId id)
                             throws InternalErrorException
Specified by:
temporaryStateExists in interface Persister
InternalErrorException

temporaryStateExists

public boolean temporaryStateExists(PersistedGame game)
                             throws InternalErrorException
Return whether the game exists in temporary storage.

Specified by:
temporaryStateExists in interface Persister
Parameters:
game - The game being checked.
Returns:
true if the game exists, or false.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.

temporaryStateExists

public boolean temporaryStateExists(GameId id,
                                    int move)
Return whether the game exists in temporary storage for the given move.

Specified by:
temporaryStateExists in interface Persister
Parameters:
id - The id of the game.
move - The move number of the game.
Returns:
true if the game exists for the given move, or false.

saveController

private void saveController(Controller c,
                            String fname)
                     throws InternalErrorException
Serialize the Controller object and write it out to disk.

Parameters:
c - The Controller to be serialized.
fname - The filename of the serialized file.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.

getController

private Controller getController(String fname)
                          throws NoSuchDataException,
                                 InternalErrorException
Deserialize the file and create a Controller object.

Parameters:
fname - The file name of the serialized file.
Returns:
A Controller object created from the serialized file.
Throws:
NoSuchDataException - Thrown if the serialized file does not exist.
InternalErrorException - Thrown if the operation fails for any reason.

getPermanentFileName

private String getPermanentFileName(String id)
Get the filename for the game id in permanent storage. Unlike temporary games, the permanently saved game is not saved for multiple moves, that is why a file name is returned instead of a directory name.

Parameters:
id - The game id.
Returns:
A String which is the file name.

getPermanentFileName

private String getPermanentFileName(GameId id)

getTemporaryDirectoryName

private String getTemporaryDirectoryName(String id)
Get the directory name for the game id in temporary storage. A game saved to temporary storage has a different file for different moves which are stored under a single directory. That is why a directory name is returned, instead of a file name as for the permanently stored games.

Parameters:
id - The game id.
Returns:
A String which is the file name.

getTemporaryDirectoryName

private String getTemporaryDirectoryName(GameId id)

getTemporaryFileName

private String getTemporaryFileName(GameId id,
                                    int move)
Get the filename for the game id in temporary storage.

Parameters:
id - The game id.
Returns:
A String which is the file name.

getTemporaryFileName

private String getTemporaryFileName(GameId id)
                             throws NoSuchDataException,
                                    InternalErrorException
Get the name of the most recent file for the particular game.

Parameters:
id - The game id.
Returns:
A String which is a file name of the serialized file.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.
NoSuchDataException

listTemporaryGames

public PersistedGame[] listTemporaryGames()
                                   throws InternalErrorException
Return an array of all the games in temporary storage.

Specified by:
listTemporaryGames in interface Persister
Returns:
An array of GameId objects.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.

listGames

private PersistedGame[] listGames(String fname)
                           throws InternalErrorException
InternalErrorException

listPermanentGames

public PersistedGame[] listPermanentGames()
                                   throws InternalErrorException
Return an array of all the games in permanent storage.

Specified by:
listPermanentGames in interface Persister
Returns:
An array of GameId objects.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.

getTemporaryGameFileName

private String getTemporaryGameFileName(String name)

getLastGameFileName

private String getLastGameFileName()

getSavedGameFileName

private String getSavedGameFileName(String name)

writeLastGameInformation

public void writeLastGameInformation(GameId black,
                                     GameId white)
Specified by:
writeLastGameInformation in interface Persister

writeLastGameFile

private void writeLastGameFile(GameId black,
                               GameId white)

writeSavedGameFile

private void writeSavedGameFile(PersistedGame game)

writeGameFile

private void writeGameFile(String fname,
                           GameId black,
                           GameId white)

getPersistedGameId

private String getPersistedGameId(String fname,
                                  Color color)
                           throws NoSuchDataException,
                                  InternalErrorException
NoSuchDataException
InternalErrorException

getPersistedGame

private PersistedGame getPersistedGame(String fname)
                                throws NoSuchDataException,
                                       InternalErrorException
NoSuchDataException
InternalErrorException

getTemporaryPersistedGame

public PersistedGame getTemporaryPersistedGame(String name)
                                        throws NoSuchDataException,
                                               InternalErrorException
Specified by:
getTemporaryPersistedGame in interface Persister
NoSuchDataException
InternalErrorException

getSavedPersistedGame

public PersistedGame getSavedPersistedGame(String name)
                                    throws NoSuchDataException,
                                           InternalErrorException
Specified by:
getSavedPersistedGame in interface Persister
NoSuchDataException
InternalErrorException

cleanup

public void cleanup()
             throws InternalErrorException
Delete all temporary game files.

Specified by:
cleanup in interface Persister
Throws:
InternalErrorException - Thrown if the operation fails for any reason.