public final class ServerModel extends 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 handle commands from
clients, as well as the ServerBackend
to coordinate client connection
and 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.
|
Collection<String> |
getChannels()
Returns a collection of the names of all the channels that are present
on the server.
|
String |
getNickname(int userId)
Returns the nickname currently associated with the given user ID.
|
String |
getOwner(String channelName)
Returns the nickname of the owner of the current channel.
|
Collection<String> |
getRegisteredUsers()
Returns a collection of the nicknames of all users that are registered
with the server.
|
int |
getUserId(String nickname)
Returns the user ID currently associated with the given nickname.
|
Collection<String> |
getUsers(String channelName)
Returns a collection of the nicknames of all the users in a given
channel.
|
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.
|
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
informing the user of their new nicknamepublic static boolean isValidName(String name)
name
- The channel or nickname string to validatepublic Broadcast deregisterUser(int userId)
getRegisteredUsers()
. The behavior of this method if
the given user ID is not registered with the model is undefined.deregisterUser
in interface ServerModelApi
userId
- the unique ID created by the backend to represent this userBroadcast
informing other clients in the
disconnected user's channels that they have disconnectedpublic int getUserId(String nickname)
getUserId
in interface ServerModelApi
nickname
- The user's nicknamepublic String getNickname(int userId)
getNickname
in interface ServerModelApi
userId
- The ID whose nickname to return.public Collection<String> getRegisteredUsers()
getRegisteredUsers
in interface ServerModelApi
public Collection<String> getChannels()
getChannels
in interface ServerModelApi
public Collection<String> getUsers(String channelName)
getUsers
in interface ServerModelApi
channelName
- The channel whose member nicknames should be returnedpublic String getOwner(String channelName)
null
if no channel with the given name exists. Provided for
testing.getOwner
in interface ServerModelApi
channelName
- The channel whose owner nickname should be returned