public final class ServerModel extends Object
Represents the model for a chat server, storing information about users and channels on the server.
The ServerBackend
interacts with this class via the registerUser(int)
, deregisterUser(int)
, and handle(int,
Command)
methods. Each of these methods generates a Broadcast
representing the set of commands that should be sent to other clients
connected to the server as a result of the interaction. See the protocol specification for an overview of the
ServerModel
's role in the program.
You may add private
fields and helper methods to this file if you
wish, but you may not modify any public
method names or
signatures.
Constructor and Description |
---|
ServerModel()
Constructs a
ServerModel with any collections needed for
modeling the server state initialized. |
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 channels that are present on
the server.
|
String |
getOwner(String channelName)
Returns the nickname of the owner of the given channel.
|
Collection<String> |
getRegisteredUsers()
Returns a collection of the nicknames of all users that are registered
with the server.
|
Collection<String> |
getUsers(String channelName)
Returns a collection of the nicknames of all users in a given channel.
|
Broadcast |
handle(int userId,
Command command)
Passes a command along to the model for processing.
|
Broadcast |
registerUser(int userId)
Informs the model that a client has connected to the server with the
given user ID.
|
public ServerModel()
Constructs a ServerModel
with any collections needed for
modeling the server state initialized.
public Broadcast registerUser(int userId)
getRegisteredUsers()
.userId
- the unique user ID created by the backend to represent the user.public Broadcast deregisterUser(int userId)
Informs the model that the client with the given user ID has
disconnected from the server. After a user ID is deregisted, the server
backend is free to re-assign this user ID to an entirely different
client. As such, the model should take care to expunge any state
pertaining to a user who is deregistered. Any user that is deregistered
(without later being registered) should not appear in the output of
getRegisteredUsers()
.
The behavior of this method if the given user ID is not registered with the model is undefined.
userId
- the unique user ID created by the backend to represent the user.public Broadcast handle(int userId, Command command)
Passes a command along to the model for processing. In response, the
model should process the command, updating its internal state and
generating a Broadcast
representing any messages that should
be sent to clients in response to the message. The exact behavior is
dependent on the internal state of the model and the exact nature of the
input command; these behaviors are specified in the protocol
specification.
The behavior of this method if the given user ID is not registered with the model is undefined.
userId
- the unique user ID created by the backend to represent the user.command
- the command to be processed by the modelcommand
public Collection<String> getRegisteredUsers()
public Collection<String> getChannels()
public Collection<String> getUsers(String channelName)
channelName
- the channel whose member nicknames should be returnedpublic String getOwner(String channelName)
null
if no channel with the given channel name exists.
Provided for testing.channelName
- the channel whose owner nickname should be returned