public final class ServerModel extends java.lang.Object implements ServerModelApi
ServerModel
is the class responsible for tracking the state of the server, including
its current users and the channels they are in.
This class is used by subclasses of Command
to:
1. handle commands from clients, and
2. handle commands from ServerBackend
to coordinate client connection/disconnection.Constructor and Description |
---|
ServerModel()
Constructs a
ServerModel and initializes any collections needed for modeling the
server state. |
Modifier and Type | Method and Description |
---|---|
Broadcast |
deregisterUser(int userId)
Informs the model that the client with the given user ID has disconnected from the server.
|
java.util.Collection<java.lang.String> |
getChannels()
Gets a collection of the names of all the channels that are present on the server.
|
java.lang.String |
getNickname(int userId)
Gets the nickname currently associated with the given user ID.
|
java.lang.String |
getOwner(java.lang.String channelName)
Gets the nickname of the owner of the given channel.
|
java.util.Collection<java.lang.String> |
getRegisteredUsers()
Gets a collection of the nicknames of all users who are registered with the server.
|
int |
getUserId(java.lang.String nickname)
Gets the user ID currently associated with the given nickname.
|
java.util.Collection<java.lang.String> |
getUsers(java.lang.String channelName)
Gets a collection of the nicknames of all the users in a given channel.
|
static boolean |
isValidName(java.lang.String name)
Determines if a given nickname is valid or invalid (contains at least
one alphanumeric character, and no non-alphanumeric characters).
|
Broadcast |
registerUser(int userId)
Informs the model that a client has connected to the server with the given user ID.
|
public ServerModel()
ServerModel
and initializes any collections needed for modeling the
server state.public Broadcast registerUser(int userId)
getRegisteredUsers()
.registerUser
in interface ServerModelApi
userId
- The unique ID created by the backend to represent this userBroadcast
to the user with their new nicknamepublic static boolean isValidName(java.lang.String name)
name
- The channel or nickname string to validatepublic Broadcast deregisterUser(int userId)
getRegisteredUsers()
.deregisterUser
in interface ServerModelApi
userId
- The unique ID of the user to deregisterBroadcast
instructing clients to remove the user from all channelspublic int getUserId(java.lang.String nickname)
getUserId
in interface ServerModelApi
nickname
- The nickname for which to get the associated user IDpublic java.lang.String getNickname(int userId)
getNickname
in interface ServerModelApi
userId
- The user ID for which to get the associated nicknamepublic java.util.Collection<java.lang.String> getRegisteredUsers()
getRegisteredUsers
in interface ServerModelApi
public java.util.Collection<java.lang.String> getChannels()
getChannels
in interface ServerModelApi
public java.util.Collection<java.lang.String> getUsers(java.lang.String channelName)
getUsers
in interface ServerModelApi
channelName
- The channel for which to get member nicknamespublic java.lang.String getOwner(java.lang.String channelName)
null
if no
channel with the given name exists.
This method is provided for testing.getOwner
in interface ServerModelApi
channelName
- The channel for which to get the owner nickname