Class ClientManager

java.lang.Object
nl.andrewl.concord_server.client.ClientManager

public class ClientManager extends Object
The client manager is responsible for managing the set of clients connected to a server.
  • Field Details

    • server

      private final ConcordServer server
      A reference to the server that this client manager is for.
    • clients

      private final Map<UUID,ClientThread> clients
      The set of connected clients, mapped by their id.
    • pendingClients

      private final Map<UUID,ClientThread> pendingClients
      The set of connected pending clients, mapped by their id.
    • userCollection

      private final org.dizitart.no2.NitriteCollection userCollection
      The nitrite collection containing user data.
    • authService

      private final AuthenticationService authService
      The service to use to authenticate incoming connections.
  • Constructor Details

    • ClientManager

      public ClientManager(ConcordServer server)
      Constructs a new client manager for the given server.
      Parameters:
      server - The server that the client manager is for.
  • Method Details

    • handleRegistration

      public void handleRegistration(nl.andrewl.concord_core.msg.types.client_setup.ClientRegistration registration, ClientThread clientThread) throws InvalidIdentificationException
      Handles an attempt by a new client to register as a user for this server. If the server is set to automatically accept all new clients, the new user is registered and the client is sent a RegistrationStatus with the RegistrationStatus.Type.ACCEPTED value, closely followed by a ServerWelcome message. Otherwise, the client is sent a RegistrationStatus.Type.PENDING response, which indicates that the client's registration is pending approval. The client can choose to remain connected and wait for approval, or disconnect and try logging in later.
      Parameters:
      registration - The client's registration information.
      clientThread - The client thread.
      Throws:
      InvalidIdentificationException - If the user's registration info is not valid.
    • handleLogin

      public void handleLogin(nl.andrewl.concord_core.msg.types.client_setup.ClientLogin login, ClientThread clientThread) throws InvalidIdentificationException
      Handles an attempt by a new client to login as an existing user to the server. If the user's credentials are valid, then the following can result:
      • If the user's registration is still pending, they will be sent a RegistrationStatus.Type.PENDING response, to indicate that their registration is still pending approval.
      • For non-pending (normal) users, they will be logged into the server and sent a ServerWelcome message.
      Parameters:
      login - The client's login credentials.
      clientThread - The client thread managing the connection.
      Throws:
      InvalidIdentificationException - If the client's credentials are incorrect.
    • handleSessionResume

      public void handleSessionResume(nl.andrewl.concord_core.msg.types.client_setup.ClientSessionResume sessionResume, ClientThread clientThread) throws InvalidIdentificationException
      Handles an attempt by a new client to login as an existing user to the server with a session token from their previous session. If the token is valid, the user will be logged in and sent a ServerWelcome response.
      Parameters:
      sessionResume - The session token data.
      clientThread - The client thread managing the connection.
      Throws:
      InvalidIdentificationException - If the token is invalid or refers to a non-existent user.
    • decidePendingUser

      public void decidePendingUser(UUID userId, boolean accepted, String reason)
      Used to accept or reject a pending user's registration. If the given user is not pending approval, this method does nothing.
      Parameters:
      userId - The id of the pending user.
      accepted - Whether to accept or reject.
      reason - The reason for rejection (or acceptance). This may be null.
    • initializeClientConnection

      private void initializeClientConnection(ClientConnectionData clientData, ClientThread clientThread)
      Standard flow for initializing a connection to a client who has already sent their identification message, and that has been checked to be valid.
      Parameters:
      clientData - The data about the client that has connected.
      clientThread - The thread managing the client's connection.
    • initializePendingClientConnection

      private void initializePendingClientConnection(UUID clientId, String pendingUsername, ClientThread clientThread)
      Initializes a connection to a client whose registration is pending, thus they should simply keep their connection alive, and receive a RegistrationStatus.Type.PENDING message, instead of a ServerWelcome.
      Parameters:
      clientId - The id of the client.
      pendingUsername - The client's username.
      clientThread - The thread managing the client's connection.
    • handleLogOut

      public void handleLogOut(UUID clientId)
      De-registers a client from the server, removing them from any channel they're currently in.
      Parameters:
      clientId - The id of the client to remove.
    • broadcast

      public void broadcast(nl.andrewl.concord_core.msg.Message message)
      Sends a message to every connected client, ignoring any channels. All clients connected to this server will receive this message.
      Parameters:
      message - The message to send.
    • getConnectedClients

      public List<nl.andrewl.concord_core.msg.types.UserData> getConnectedClients()
      Returns:
      The list of connected clients.
    • getPendingClients

      public List<nl.andrewl.concord_core.msg.types.UserData> getPendingClients()
      Returns:
      The list of connected, pending clients.
    • getConnectedIds

      public Set<UUID> getConnectedIds()
      Returns:
      The set of ids of all connected clients.
    • getClientById

      public Optional<ClientThread> getClientById(UUID id)
      Tries to find a connected client with the given id.
      Parameters:
      id - The id to look for.
      Returns:
      An optional client thread.
    • getPendingClientById

      public Optional<ClientThread> getPendingClientById(UUID id)
      Tries to find a pending client with the given id.
      Parameters:
      id - The id to look for.
      Returns:
      An optional client thread.