Class ServerModel

java.lang.Object
ServerModel
All Implemented Interfaces:
ServerModelApi

public final class ServerModel
extends Object
implements ServerModelApi
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 and initializes any collections needed for modeling the server state.
  • Method Details

    • registerUser

      public Broadcast registerUser​(int userId)
      Informs the model that a client has connected to the server with the given user ID. The model should update its state so that it can identify this user during later interactions. The newly connected user will not yet have had the chance to set a nickname, and so the model should provide a default nickname for the user. Any user who is registered with the server (without being later deregistered) should appear in the output of getRegisteredUsers().
      Specified by:
      registerUser in interface ServerModelApi
      Parameters:
      userId - The unique ID created by the backend to represent this user
      Returns:
      A Broadcast to the user with their new 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).
      Parameters:
      name - The channel or nickname string to validate
      Returns:
      true if the string is a valid name
    • deregisterUser

      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 deregistered, the server backend is free to reassign this user ID to an entirely different client; as such, the model should remove all state of the user associated with the deregistered user ID. The behavior of this method if the given user ID is not registered with the model is undefined. Any user who is deregistered (without later being registered) should not appear in the output of getRegisteredUsers().
      Specified by:
      deregisterUser in interface ServerModelApi
      Parameters:
      userId - The unique ID of the user to deregister
      Returns:
      A Broadcast instructing clients to remove the user from all channels
    • createChannel

      public ServerError createChannel​(String channelName, String owner, boolean inviteOnly)
    • addUserToChannel

      public ServerError addUserToChannel​(String userName, String channelName)
    • removeUserFromChannel

      public ServerError removeUserFromChannel​(String userName, String channelName)
    • changeNickname

      public Broadcast changeNickname​(NicknameCommand command)
    • isPublicChannel

      public boolean isPublicChannel​(String channelName)
    • 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.
      Specified by:
      getUserId in interface ServerModelApi
      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.
      Specified by:
      getNickname in interface ServerModelApi
      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.
      Specified by:
      getRegisteredUsers in interface ServerModelApi
      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.
      Specified by:
      getChannels in interface ServerModelApi
      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.
      Specified by:
      getUsersInChannel in interface ServerModelApi
      Parameters:
      channelName - The channel for which to get member nicknames
      Returns:
      The collection of user nicknames in the argued 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.
      Specified by:
      getOwner in interface ServerModelApi
      Parameters:
      channelName - The channel for which to get the owner nickname
      Returns:
      The nickname of the channel owner if such a channel exists, othewrise null