Package org.cis1200
Class ServerModel
java.lang.Object
org.cis1200.ServerModel
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, to create channels, send messages, etc.
and
2. handle commands from ServerBackend
to coordinate
client connection/disconnection.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionchangeNickname
(NicknameCommand nickCommand) This method is called when a user wants to change their nickname.createChannel
(CreateCommand createCommand) This method is called when a user wants to create a channel.deregisterUser
(int userId) This method is automatically called when a client disconnects from the server.boolean
existingNickname
(String nickname) Is this an existing nickname?boolean
existingUserId
(Integer userId) Is this a userId that is known to the server?Gets a collection of the names of all the channels that are present on the server.getNickname
(int userId) Gets the nickname currently associated with the given userId.Gets the nickname of the owner of the given channel.Gets a collection of the nicknames of all users who are registered with the server.int
Gets the userId currently associated with the given nickname.getUserIdsInChannel
(String channelName) Gets a collection of the userIds of all users who are in the channel.getUserNicknamesInChannel
(String channelName) Gets an alphabetically sorted set of the nicknames of all the users in a given channel.inviteUser
(InviteCommand inviteCommand) This method is called when a channel's owner adds a user to that channel.static boolean
isValidName
(String name) Determines if a given name is valid or invalid (contains at least one alphanumeric character, and no non-alphanumeric characters).joinChannel
(JoinCommand joinCommand) This method is called when a user wants to join a channel.kickUser
(KickCommand kickCommand) This method is called when a channel's owner removes a user from that channel.leaveChannel
(LeaveCommand leaveCommand) This method is called when a user wants to leave a channel.registerUser
(int userId) This method is automatically called when a new client connects to the server.sendMessage
(MessageCommand messageCommand) This method is called when a user wants to send a message to a channel.
-
Constructor Details
-
ServerModel
public ServerModel()Constructs aServerModel
. Make sure to initialize any collections used to model the server state here.
-
-
Method Details
-
existingNickname
Is this an existing nickname?- Parameters:
nickname
- any string- Returns:
- whether the nickname is currently in use by any user in any channel
-
existingUserId
Is this a userId that is known to the server?- Parameters:
userId
- any integer- Returns:
- whether the userId has been registered for any client
-
getUserId
Gets the userId currently associated with the given nickname.- Parameters:
userNickname
- The nickname for which to get the associated userId- Returns:
- The userId of the user with the provided nickname if such a user exists
- Throws:
IllegalArgumentException
- if there is no userId associated with the nickname.
-
getNickname
Gets the nickname currently associated with the given userId.- Parameters:
userId
- The userId for which to get the associated nickname- Returns:
- The nickname of the user with that userId if such a user exists
- Throws:
IllegalArgumentException
- if the userId is not in use
-
getRegisteredUsers
Gets a collection of the nicknames of all users who are registered with the server. Modifications to the returned collection should not affect the server state. This method is provided for testing.- Returns:
- The collection of registered user nicknames
-
getChannels
Gets a collection of the names of all the channels that are present on the server. Modifications to the returned collection should not affect the server state. This method is public for testing.- Returns:
- The collection of channel names
-
getUserIdsInChannel
Gets a collection of the userIds of all users who are in the channel. Modifications to the returned collection should not affect the server state. This method is public for testing.- Parameters:
channelName
- The channel- Returns:
- The collection of IDs
- Throws:
IllegalArgumentException
- if the channel does not exist
-
getUserNicknamesInChannel
Gets an alphabetically sorted set of the nicknames of all the users in a given channel. Modifications to the returned collection should not affect the server state. This method is public for testing.- Parameters:
channelName
- The channel for which to get member nicknames- Returns:
- A collection of all user nicknames in the channel
- Throws:
IllegalArgumentException
- if there is no channel with the given name
-
getOwner
Gets the nickname of the owner of the given channel. This method is provided for testing.- Parameters:
channelName
- The channel for which to get the owner nickname- Returns:
- The nickname of the channel owner if such a channel exists
- Throws:
IllegalArgumentException
- if there is no channel with the given name
-
registerUser
This method is automatically called when a new client connects to the server. It should generate a default nickname withgenerateUniqueNickname()
, store the new userId and nickname in yourServerModel
state. The method should return a singleResponse.connected(java.lang.Integer, java.lang.String)
response to the client.- Parameters:
userId
- The new user's unique identifier (created by the backend)- Returns:
- A
ResponseSet
object indicating that the connection was successful.
-
deregisterUser
This method is automatically called when a client disconnects from the server. This method should take the following actions, not necessarily in this order, and notify other users that the client has left. (1) The disconnected user's information should be deleted from theServerModel
's internal state (2) All channels owned by the disconnected user should be deleted theServerModel
's internal state- Parameters:
userId
- The unique userId of the user to deregister- Returns:
- a
ResponseSet
object containingResponse.disconnected(java.lang.Integer, java.lang.String)
responses addressed to all users who shared a channel with the disconnected user, excluding the disconnected user. - Throws:
IllegalArgumentException
- if the userID is not currently registered. (If you correctly use previous methods you've written, this could happen automatically!)
-
changeNickname
This method is called when a user wants to change their nickname.- Parameters:
nickCommand
- TheNicknameCommand
object containing all information needed to attempt a nickname change- Returns:
- If the nickname change is successful,
a
ResponseSet
containing aResponse.okay(java.lang.Integer, java.lang.String, org.cis1200.Command)
response for each user that shares at least one channel with the sender, including the sender. Each response should contain the originalNicknameCommand
, and the old nickname. If an error occurs, useResponse.error(org.cis1200.Command, org.cis1200.ErrorCode)
with either: (1)ErrorCode.INVALID_NAME
if the proposed nickname is not valid according toisValidName(String)
(2)ErrorCode.NAME_ALREADY_IN_USE
if there is already a user with the proposed nickname. This includes the current nickname of the user requesting the change.
-
isValidName
Determines if a given name is valid or invalid (contains at least one alphanumeric character, and no non-alphanumeric characters). (Nothing to do here.)- Parameters:
name
- The channel or nickname string to validate- Returns:
- true if the string is a valid name
-
createChannel
This method is called when a user wants to create a channel. You can ignore the privacy aspect of this method for task 4, but if you choose to do the task 5 Kudos problem, make sure you come back and implement the privacy aspect.- Parameters:
createCommand
- TheCreateCommand
object containing all information needed to attempt channel creation- Returns:
- If the channel creation is successful,
ResponseSet
containing a singleResponse.okay(java.lang.Integer, java.lang.String, org.cis1200.Command)
message. The recipient should be owner of the new channel. If an error occurs, useResponse.error(org.cis1200.Command, org.cis1200.ErrorCode)
with either: (1)ErrorCode.INVALID_NAME
if the proposed channel name is not valid according toisValidName(String)
(2)ErrorCode.CHANNEL_ALREADY_EXISTS
if there is already a channel with the proposed name
-
joinChannel
This method is called when a user wants to join a channel. You can ignore the privacy aspect of this method for task 4. If you choose to do the task 5 Kudos problem, make sure you come back and implement the privacy aspect.- Parameters:
joinCommand
- TheJoinCommand
object containing all information needed for the user's join attempt- Returns:
- If the user can successfully join the channel, the
ResponseSet
should include aResponse.okay(java.lang.Integer, java.lang.String, org.cis1200.Command)
response sent to all users in the channel (including the newly joined user). Furthermore, theResponseSet
should include aResponse.names(java.lang.Integer, java.lang.String, java.lang.String, java.util.SortedSet<java.lang.String>, java.lang.String)
response to the newly joined user containing the names of all users in the channel (also including the newly joined user). If an error occurs, useResponse.error(org.cis1200.Command, org.cis1200.ErrorCode)
with either: (1)ErrorCode.NO_SUCH_CHANNEL
if there is no channel with the specified name (2) (after Task 5)ErrorCode.JOIN_PRIVATE_CHANNEL
if the sender is attempting to join a private channel
-
sendMessage
This method is called when a user wants to send a message to a channel.- Parameters:
messageCommand
- TheMessageCommand
object containing all information needed for the messaging attempt.- Returns:
- If the message can be sent, the
ResponseSet
should includeResponse.okay(java.lang.Integer, java.lang.String, org.cis1200.Command)
responses addressed to all clients in the channel. If an error occurs, useResponse.error(org.cis1200.Command, org.cis1200.ErrorCode)
with either: (1)ErrorCode.NO_SUCH_CHANNEL
if there is no channel with the specified name (2)ErrorCode.USER_NOT_IN_CHANNEL
if the sender is not in the channel they are trying to send the message to
-
leaveChannel
This method is called when a user wants to leave a channel.- Parameters:
leaveCommand
- TheLeaveCommand
object containing all information about the user's leave attempt- Returns:
- A
ResponseSet
object containingResponse.okay(java.lang.Integer, java.lang.String, org.cis1200.Command)
responses to all users in the channel (including the leaving user), informing them that the leave command was successful. If the user was the last member of the channel, then the channel is also removed. If an error occurs, useResponse.error(org.cis1200.Command, org.cis1200.ErrorCode)
with either: (1)ErrorCode.NO_SUCH_CHANNEL
if there is no channel with the specified name (2)ErrorCode.USER_NOT_IN_CHANNEL
if the sender is not in the channel they are trying to leave
-
inviteUser
This method is called when a channel's owner adds a user to that channel.- Parameters:
inviteCommand
- TheInviteCommand
object containing all information needed for the invite attempt- Returns:
- If the user joins the channel successfully as a result of the invite,
a
ResponseSet
containing aResponse.names(java.lang.Integer, java.lang.String, java.lang.String, java.util.SortedSet<java.lang.String>, java.lang.String)
for all people in the joined channel (including the new user). Furthermore, theResponseSet
should include aResponse.names(java.lang.Integer, java.lang.String, java.lang.String, java.util.SortedSet<java.lang.String>, java.lang.String)
response to the newly joined user containing the names of all users in the channel (also including the newly joined user). If an error occurs, useResponse.error(org.cis1200.Command, org.cis1200.ErrorCode)
with either: (1)ErrorCode.NO_SUCH_USER
if the invited user does not exist (2)ErrorCode.NO_SUCH_CHANNEL
if there is no channel with the specified name (3)ErrorCode.INVITE_TO_PUBLIC_CHANNEL
if the invite refers to a public channel (4)ErrorCode.USER_NOT_OWNER
if the sender is not the owner of the channel
-
kickUser
This method is called when a channel's owner removes a user from that channel. If the user being kicked is the owner, then the channel should be deleted and all its users removed.- Parameters:
kickCommand
- TheKickCommand
object containing all information needed for the kick attempt- Returns:
- If the user is successfully kicked from the channel,
ResponseSet
containing aResponse.okay(java.lang.Integer, java.lang.String, org.cis1200.Command)
for each user in the channel, including the user who was kicked. If an error occurs, useResponse.error(org.cis1200.Command, org.cis1200.ErrorCode)
with either: (1)ErrorCode.NO_SUCH_USER
if the user being kicked does not exist (2)ErrorCode.NO_SUCH_CHANNEL
if there is no channel with the specified name (3)ErrorCode.USER_NOT_IN_CHANNEL
if the user being kicked is not a member of the channel (4)ErrorCode.USER_NOT_OWNER
if the sender is not the owner of the channel
-