Package org.cis1200

Class ServerModel

java.lang.Object
org.cis1200.ServerModel

public final class ServerModel extends Object
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 Details

    • ServerModel

      public ServerModel()
      Constructs a ServerModel. Make sure to initialize any collections used to model the server state here.
  • Method Details

    • getUserId

      public int getUserId(String nickname)
      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

      public String getNickname(int userId)
      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

      public Collection<String> 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

      public Collection<String> 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

      public Collection<String> getUsersInChannel(String channelName)
      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

      public String getOwner(String channelName)
      Gets the nickname of the owner of the given channel. The result is null 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

      public Broadcast registerUser(int userId)
      This method is automatically called by the backend when a new client connects to the server. It should generate a default nickname with generateUniqueNickname(), store the new user's ID and username in your data structures for ServerModel state, and construct and return a Broadcast object using Broadcast.connected(String)}.
      Parameters:
      userId - The new user's unique ID (automatically created by the backend)
      Returns:
      The Broadcast object generated by calling Broadcast.connected(String) with the proper parameter
    • deregisterUser

      public Broadcast deregisterUser(int userId)
      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 from ServerModel's internal state (4) Construct and return a Broadcast object using Broadcast.disconnected(String, Collection).
      Parameters:
      userId - The unique ID of the user to deregister
      Returns:
      The Broadcast object generated by calling Broadcast.disconnected(String, Collection) with the proper parameters
    • changeNickname

      public Broadcast changeNickname(NicknameCommand nickCommand)
      This method is called when a user wants to change their nickname.
      Parameters:
      nickCommand - The NicknameCommand object containing all information needed to attempt a nickname change
      Returns:
      The Broadcast object generated by Broadcast.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, use Broadcast.error(Command, ServerResponse) with either: (1) ServerResponse.INVALID_NAME if the proposed nickname is not valid according to isValidName(String) (2) ServerResponse.NAME_ALREADY_IN_USE if there is already a user with the proposed nickname
    • isValidName

      public 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). (Nothing to do here.)
      Parameters:
      name - The channel or nickname string to validate
      Returns:
      true if the string is a valid name
    • createChannel

      public Broadcast createChannel(CreateCommand createCommand)
      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 - The CreateCommand object containing all information needed to attempt channel creation
      Returns:
      The Broadcast object generated by Broadcast.okay(Command, Collection) if the channel creation is successful. The only recipient should be the new channel's owner. If an error occurs, use Broadcast.error(Command, ServerResponse) with either: (1) ServerResponse.INVALID_NAME if the proposed channel name is not valid according to isValidName(String) (2) ServerResponse.CHANNEL_ALREADY_EXISTS if there is already a channel with the proposed name
    • joinChannel

      public Broadcast joinChannel(JoinCommand joinCommand)
      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 - The JoinCommand object containing all information needed for the user's join attempt
      Returns:
      The Broadcast object generated by Broadcast.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, use Broadcast.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

      public Broadcast sendMessage(MessageCommand messageCommand)
      This method is called when a user wants to send a message to a channel.
      Parameters:
      messageCommand - The MessageCommand object containing all information needed for the messaging attempt
      Returns:
      The Broadcast object generated by Broadcast.okay(Command, Collection) if the message attempt is successful. The recipients should be all clients in the channel. If an error occurs, use Broadcast.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

      public Broadcast leaveChannel(LeaveCommand leaveCommand)
      This method is called when a user wants to leave a channel.
      Parameters:
      leaveCommand - The LeaveCommand object containing all information about the user's leave attempt
      Returns:
      The Broadcast object generated by Broadcast.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, use Broadcast.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

      public Broadcast inviteUser(InviteCommand inviteCommand)
      This method is called when a channel's owner adds a user to that channel.
      Parameters:
      inviteCommand - The InviteCommand object containing all information needed for the invite attempt
      Returns:
      The Broadcast object generated by Broadcast.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, use Broadcast.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

      public Broadcast kickUser(KickCommand kickCommand)
      This method is called when a channel's owner removes a user from that channel.
      Parameters:
      kickCommand - The KickCommand object containing all information needed for the kick attempt
      Returns:
      The Broadcast object generated by Broadcast.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, use Broadcast.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