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, 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 by the backend when a client disconnects from 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 user ID.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 user ID currently associated with the given nickname.getUsersInChannel
(String channelName) Gets a collection 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 nickname 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 by the backend 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
-
getUserId
Gets the user ID currently associated with the given nickname. The returned ID is -1 if the nickname is not currently in use.- 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.- 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.- 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.- 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.- Parameters:
channelName
- The channel for which to get member nicknames- Returns:
- A collection of all user nicknames in the 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.- Parameters:
channelName
- The channel for which to get the owner nickname- Returns:
- The nickname of the channel owner if such a channel exists; otherwise, return null
-
registerUser
This method is automatically called by the backend when a new client connects to the server. It should generate a default nickname withgenerateUniqueNickname()
, store the new user's ID and username in your data structures forServerModel
state, and construct and return aBroadcast
object usingBroadcast.connected(String)
}.- Parameters:
userId
- The new user's unique ID (automatically created by the backend)- Returns:
- The
Broadcast
object generated by callingBroadcast.connected(String)
with the proper parameter
-
deregisterUser
This method is automatically called by the backend when a client disconnects from the server. This method should take the following actions, not necessarily in this order: (1) All users who shared a channel with the disconnected user should be notified that they left (2) All channels owned by the disconnected user should be deleted (3) The disconnected user's information should be removed fromServerModel
's internal state (4) Construct and return aBroadcast
object usingBroadcast.disconnected(String, Collection)
.- Parameters:
userId
- The unique ID of the user to deregister- Returns:
- The
Broadcast
object generated by callingBroadcast.disconnected(String, Collection)
with the proper parameters
-
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:
- The
Broadcast
object generated byBroadcast.okay(Command, Collection)
if the nickname change is successful. The command should be the original nickCommand and the collection of recipients should be any clients who share at least one channel with the sender, including the sender. If an error occurs, useBroadcast.error(Command, ServerResponse)
with either: (1)ServerResponse.INVALID_NAME
if the proposed nickname is not valid according toisValidName(String)
(2)ServerResponse.NAME_ALREADY_IN_USE
if there is already a user with the proposed nickname
-
isValidName
Determines if a given nickname 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 make sure you come back and implement it in task 5.- Parameters:
createCommand
- TheCreateCommand
object containing all information needed to attempt channel creation- Returns:
- The
Broadcast
object generated byBroadcast.okay(Command, Collection)
if the channel creation is successful. The only recipient should be the new channel's owner. If an error occurs, useBroadcast.error(Command, ServerResponse)
with either: (1)ServerResponse.INVALID_NAME
if the proposed channel name is not valid according toisValidName(String)
(2)ServerResponse.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, but make sure you come back and implement it in task 5.- Parameters:
joinCommand
- TheJoinCommand
object containing all information needed for the user's join attempt- Returns:
- The
Broadcast
object generated byBroadcast.names(Command, Collection, String)
if the user joins the channel successfully. The recipients should be all people in the joined channel (including the sender). If an error occurs, useBroadcast.error(Command, ServerResponse)
with either: (1)ServerResponse.NO_SUCH_CHANNEL
if there is no channel with the specified name (2) (after Task 5)ServerResponse.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:
- The
Broadcast
object generated byBroadcast.okay(Command, Collection)
if the message attempt is successful. The recipients should be all clients in the channel. If an error occurs, useBroadcast.error(Command, ServerResponse)
with either: (1)ServerResponse.NO_SUCH_CHANNEL
if there is no channel with the specified name (2)ServerResponse.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:
- The
Broadcast
object generated byBroadcast.okay(Command, Collection)
if the user leaves the channel successfully. The recipients should be all clients who were in the channel, including the user who left. If an error occurs, useBroadcast.error(Command, ServerResponse)
with either: (1)ServerResponse.NO_SUCH_CHANNEL
if there is no channel with the specified name (2)ServerResponse.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:
- The
Broadcast
object generated byBroadcast.names(Command, Collection, String)
if the user joins the channel successfully as a result of the invite. The recipients should be all people in the joined channel (including the new user). If an error occurs, useBroadcast.error(Command, ServerResponse)
with either: (1)ServerResponse.NO_SUCH_USER
if the invited user does not exist (2)ServerResponse.NO_SUCH_CHANNEL
if there is no channel with the specified name (3)ServerResponse.INVITE_TO_PUBLIC_CHANNEL
if the invite refers to a public channel (4)ServerResponse.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.- Parameters:
kickCommand
- TheKickCommand
object containing all information needed for the kick attempt- Returns:
- The
Broadcast
object generated byBroadcast.okay(Command, Collection)
if the user is successfully kicked from the channel. The recipients should be all clients who were in the channel, including the user who was kicked. If an error occurs, useBroadcast.error(Command, ServerResponse)
with either: (1)ServerResponse.NO_SUCH_USER
if the user being kicked does not exist (2)ServerResponse.NO_SUCH_CHANNEL
if there is no channel with the specified name (3)ServerResponse.USER_NOT_IN_CHANNEL
if the user being kicked is not a member of the channel (4)ServerResponse.USER_NOT_OWNER
if the sender is not the owner of the channel
-