Class Database
server's persistent store of information about
the story worlds and agents available for sessions.
All of the worlds and agents in the database are available for all sessions,
but entries either advertise that they exist (listed) or do
not advertise that they exist (unlisted). Listed entries are available when
a new user connects to browse possible worlds and agents. Unlisted entries
must be specifically requested by name.
A database is designed to be small and serializable. World
entries do not store the assets or implementation logic of a world model in the database; they simply store a path to the file
where the world model is serialized. When a database is deserialized, it
should deserialize all world models.
Agent entries can be password protected. This means that
an agent cannot use a protected name unless it provides the correct password.
This can be used to prevent two different agents from using the same name,
either accidentally or on purpose.
A database is not thread safe. Behavior is undefined if it is accessed simultaneously by multiple threads.
- Author:
- Stephen G. Ware
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAdatabase entryfor anagent.static classA database entry represents an individual elements in the database, including its meta-data, such as itstitleanddescription.static classA listableentryis one that can either be set as clearly publicly available (listed) or not clearly available (unlisted).static classAdatabase entryfor astory world. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a new agent entry to the database for a given agent name and writes the database to file.voidaddWorld(WorldModel world) Adds a new world entry to the database for a given world and writes the database to file.voidDeserializes a world model from a given file,addsthe world model to the database, and writes the database to file.Returns the entry for the agent with the given name or null if the database has no agent that name.Returns a randomly chosenlistedworld entry.Returns the entry for the world model with the given name or null if the database has no world model with that name.voidremoveAgent(String name) Removes the entry from this database for the agent with the given name (if one exists) and writes the database to file.voidremoveWorld(String name) Removes the entry from this database for the world model with the given name (if one exists) and writes the database to file.requireAgent(String name) Returns the entry for the agent with the given name or throws an exception if no such entry exists.requireWorld(String name) Returns the entry for the world model with the given name or throws an exception if no such entry exists.voidsetAgentDescription(String name, String description) Sets thedescriptionof an agent's entry and writes the database to file.voidsetAgentLimit(String name, int limit) Sets thelimiton how many agents with the given name may be in sessions simultaneously and writes the database to file.voidsetAgentListed(String name, boolean value) Sets whether a given agent's entry islistedand writes the database to file.voidsetAgentPassword(String name, String password) Sets thepasswordof an agent's entry and writes the database to file.voidsetAgentTitle(String name, String title) Sets thetitleof an agent's entry and writes the database to file.voidsetWorldDescription(String name, String description) Sets thedescriptionof a world model's entry and writes the database to file.voidsetWorldListed(String name, boolean value) Sets whether a given world model's entry islistedand writes the database to file.voidsetWorldTitle(String name, String title) Sets thetitleof a world model's entry and writes the database to file.voidwrite()Writes this database to file.
-
Constructor Details
-
Database
Constructs a new empty database or deserializes it from a file.- Parameters:
file- the file in which the database is serialized, or null if an empty database should be createdlog- the log to which important status messages will be written as the database is created and deserialized- Throws:
IOException- if the file cannot be found or if a problem occurs when deserializing the database
-
-
Method Details
-
getListedWorlds
- Returns:
- a list of worlds that are advertised as available
-
getWorld
Returns the entry for the world model with the given name or null if the database has no world model with that name. This method may return anunlistedworld.- Parameters:
name- the name of the world model whose entry is desired- Returns:
- the entry for the world model with that name, or null if the database has no entry for a world with that name
-
requireWorld
Returns the entry for the world model with the given name or throws an exception if no such entry exists.- Parameters:
name- the name of the world model whose entry is desired- Returns:
- the entry for the world model with that name
- Throws:
IllegalArgumentException- if there is no entry for a world model with that name
-
getRandomWorld
Returns a randomly chosenlistedworld entry.- Returns:
- a random listed world entry
- Throws:
IllegalStateException- if the database contains no listed world entries
-
addWorld
Adds a new world entry to the database for a given world and writes the database to file. If the database already contains an entry for a world with the given world's name, the old entry is replaced.- Parameters:
world- the new world model to add to the database
-
addWorld
Deserializes a world model from a given file,addsthe world model to the database, and writes the database to file.- Parameters:
file- a file that contains a serialized world model- Throws:
IOException- if the file does not exist or if there is a problem reading the world model from the file
-
setWorldTitle
Sets thetitleof a world model's entry and writes the database to file.- Parameters:
name- the system name of the world modeltitle- the new title for the world model- Throws:
IllegalArgumentException- if there is no entry for a world model with that name
-
setWorldDescription
Sets thedescriptionof a world model's entry and writes the database to file.- Parameters:
name- the system name of the world modeldescription- the new description for the world model- Throws:
IllegalArgumentException- if there is no entry for a world model with that name
-
setWorldListed
Sets whether a given world model's entry islistedand writes the database to file.- Parameters:
name- the system name of the world modelvalue- true if the world should be listed, false if it should not be listed- Throws:
IllegalArgumentException- if there is no entry for a world model with that name
-
removeWorld
Removes the entry from this database for the world model with the given name (if one exists) and writes the database to file.- Parameters:
name- the system name of the world model- Throws:
IllegalArgumentException- if there is no entry for a world model with that name
-
getListedAgents
- Returns:
- a list of agents that are advertised as available
-
getAgent
Returns the entry for the agent with the given name or null if the database has no agent that name. This method may return anunlistedagent.- Parameters:
name- the name of the agent whose entry is desired- Returns:
- the entry for the agent with that name, or null if the database has no entry for an agent with that name
-
requireAgent
Returns the entry for the agent with the given name or throws an exception if no such entry exists.- Parameters:
name- the name of the agent whose entry is desired- Returns:
- the entry for the agent with that name
- Throws:
IllegalArgumentException- if there is no entry for an agent with that name
-
addAgent
Adds a new agent entry to the database for a given agent name and writes the database to file. If the database already contains an entry for an agent with the given name, the entry is unchanged.- Parameters:
name- the name of the agent to add to the database
-
setAgentTitle
Sets thetitleof an agent's entry and writes the database to file.- Parameters:
name- the system name of the agenttitle- the new title for the agent- Throws:
IllegalArgumentException- if there is no entry for an agent with that name
-
setAgentDescription
Sets thedescriptionof an agent's entry and writes the database to file.- Parameters:
name- the system name of the agentdescription- the new description for the agent- Throws:
IllegalArgumentException- if there is no entry for an agent with that name
-
setAgentPassword
Sets thepasswordof an agent's entry and writes the database to file. The password will be salted and hashed before it is stored.- Parameters:
name- the system name of the agentpassword- the new password for the agent- Throws:
IllegalArgumentException- if there is no entry for an agent with that name
-
setAgentLimit
Sets thelimiton how many agents with the given name may be in sessions simultaneously and writes the database to file.- Parameters:
name- the system name of the agentlimit- the maximum number of agents with the given name which may be running simultaneously, or 0 for no limit- Throws:
IllegalArgumentException- if there is no entry for an agent with that name
-
setAgentListed
Sets whether a given agent's entry islistedand writes the database to file.- Parameters:
name- the system name of the agentvalue- true if the agent should be listed, false if it should not be listed- Throws:
IllegalArgumentException- if there is no entry for an agent with that name
-
removeAgent
Removes the entry from this database for the agent with the given name (if one exists) and writes the database to file.- Parameters:
name- the system name of the agent- Throws:
IllegalArgumentException- if there is no entry for an agent with that name
-
write
Writes this database to file. The file to which it is written is set in the database's constructor. If this database is temporary (i.e. never written to file), this method does nothing.- Throws:
IOException- if a problem occurs while writing the database to file
-