Class ServerModel
java.lang.Object
ServerModel
- All Implemented Interfaces:
ServerModelApi
public final class ServerModel extends Object implements ServerModelApi
The
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 Summary
Constructors Constructor Description ServerModel()
Constructs aServerModel
and initializes any collections needed for modeling the server state. -
Method Summary
Modifier and Type Method Description ServerError
addUserToChannel(String userName, String channelName)
Broadcast
changeNickname(NicknameCommand command)
ServerError
createChannel(String channelName, String owner, boolean inviteOnly)
Broadcast
deregisterUser(int userId)
Informs the model that the client with the given user ID has disconnected from the server.Collection<String>
getChannels()
Gets a collection of the names of all the channels that are present on the server.String
getNickname(int userId)
Gets the nickname currently associated with the given user ID.String
getOwner(String channelName)
Gets the nickname of the owner of the given channel.Collection<String>
getRegisteredUsers()
Gets a collection of the nicknames of all users who are registered with the server.int
getUserId(String nickname)
Gets the user ID currently associated with the given nickname.Collection<String>
getUsersInChannel(String channelName)
Gets a collection of the nicknames of all the users in a given channel.boolean
isPublicChannel(String channelName)
static boolean
isValidName(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.ServerError
removeUserFromChannel(String userName, String channelName)
-
Constructor Details
-
ServerModel
public ServerModel()Constructs aServerModel
and initializes any collections needed for modeling the server state.
-
-
Method Details
-
registerUser
Informs the model that a client has connected to the server with the given user ID. The model should update its state so that it can identify this user during later interactions. The newly connected user will not yet have had the chance to set a nickname, and so the model should provide a default nickname for the user. Any user who is registered with the server (without being later deregistered) should appear in the output ofgetRegisteredUsers()
.- Specified by:
registerUser
in interfaceServerModelApi
- Parameters:
userId
- The unique ID created by the backend to represent this user- Returns:
- A
Broadcast
to the user with their new nickname
-
isValidName
Determines if a given nickname is valid or invalid (contains at least one alphanumeric character, and no non-alphanumeric characters).- Parameters:
name
- The channel or nickname string to validate- Returns:
- true if the string is a valid name
-
deregisterUser
Informs the model that the client with the given user ID has disconnected from the server. After a user ID is deregistered, the server backend is free to reassign this user ID to an entirely different client; as such, the model should remove all state of the user associated with the deregistered user ID. The behavior of this method if the given user ID is not registered with the model is undefined. Any user who is deregistered (without later being registered) should not appear in the output ofgetRegisteredUsers()
.- Specified by:
deregisterUser
in interfaceServerModelApi
- Parameters:
userId
- The unique ID of the user to deregister- Returns:
- A
Broadcast
instructing clients to remove the user from all channels
-
createChannel
-
addUserToChannel
-
removeUserFromChannel
-
changeNickname
-
isPublicChannel
-
getUserId
Gets the user ID currently associated with the given nickname. The returned ID is -1 if the nickname is not currently in use.- Specified by:
getUserId
in interfaceServerModelApi
- Parameters:
nickname
- The nickname for which to get the associated user ID- Returns:
- The user ID of the user with the argued nickname if such a user exists, otherwise -1
-
getNickname
Gets the nickname currently associated with the given user ID. The returned nickname is null if the user ID is not currently in use.- Specified by:
getNickname
in interfaceServerModelApi
- Parameters:
userId
- The user ID for which to get the associated nickname- Returns:
- The nickname of the user with the argued user ID if such a user exists, otherwise null
-
getRegisteredUsers
Gets a collection of the nicknames of all users who are registered with the server. Changes to the returned collection should not affect the server state. This method is provided for testing.- Specified by:
getRegisteredUsers
in interfaceServerModelApi
- Returns:
- The collection of registered user nicknames
-
getChannels
Gets a collection of the names of all the channels that are present on the server. Changes to the returned collection should not affect the server state. This method is provided for testing.- Specified by:
getChannels
in interfaceServerModelApi
- Returns:
- The collection of channel names
-
getUsersInChannel
Gets a collection of the nicknames of all the users in a given channel. The collection is empty if no channel with the given name exists. Modifications to the returned collection should not affect the server state. This method is provided for testing.- Specified by:
getUsersInChannel
in interfaceServerModelApi
- Parameters:
channelName
- The channel for which to get member nicknames- Returns:
- The collection of user nicknames in the argued channel
-
getOwner
Gets the nickname of the owner of the given channel. The result isnull
if no channel with the given name exists. This method is provided for testing.- Specified by:
getOwner
in interfaceServerModelApi
- Parameters:
channelName
- The channel for which to get the owner nickname- Returns:
- The nickname of the channel owner if such a channel exists, othewrise null
-