java.lang.Object
nl.andrewl.concord_server.client.ClientManager
The client manager is responsible for managing the set of clients connected
to a server.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AuthenticationService
The service to use to authenticate incoming connections.private final Map<UUID,
ClientThread> The set of connected clients, mapped by their id.private final Map<UUID,
ClientThread> The set of connected pending clients, mapped by their id.private final ConcordServer
A reference to the server that this client manager is for.private final org.dizitart.no2.NitriteCollection
The nitrite collection containing user data. -
Constructor Summary
ConstructorsConstructorDescriptionClientManager
(ConcordServer server) Constructs a new client manager for the given server. -
Method Summary
Modifier and TypeMethodDescriptionvoid
broadcast
(nl.andrewl.concord_core.msg.Message message) Sends a message to every connected client, ignoring any channels.void
decidePendingUser
(UUID userId, boolean accepted, String reason) Used to accept or reject a pending user's registration.getClientById
(UUID id) Tries to find a connected client with the given id.List<nl.andrewl.concord_core.msg.types.UserData>
Tries to find a pending client with the given id.List<nl.andrewl.concord_core.msg.types.UserData>
void
handleLogin
(nl.andrewl.concord_core.msg.types.client_setup.ClientLogin login, ClientThread clientThread) Handles an attempt by a new client to login as an existing user to the server.void
handleLogOut
(UUID clientId) De-registers a client from the server, removing them from any channel they're currently in.void
handleRegistration
(nl.andrewl.concord_core.msg.types.client_setup.ClientRegistration registration, ClientThread clientThread) Handles an attempt by a new client to register as a user for this server.void
handleSessionResume
(nl.andrewl.concord_core.msg.types.client_setup.ClientSessionResume sessionResume, ClientThread clientThread) Handles an attempt by a new client to login as an existing user to the server with a session token from their previous session.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.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 aRegistrationStatus.Type.PENDING
message, instead of aServerWelcome
.
-
Field Details
-
server
A reference to the server that this client manager is for. -
clients
The set of connected clients, mapped by their id. -
pendingClients
The set of connected pending clients, mapped by their id. -
userCollection
private final org.dizitart.no2.NitriteCollection userCollectionThe nitrite collection containing user data. -
authService
The service to use to authenticate incoming connections.
-
-
Constructor Details
-
ClientManager
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 aRegistrationStatus
with theRegistrationStatus.Type.ACCEPTED
value, closely followed by aServerWelcome
message. Otherwise, the client is sent aRegistrationStatus.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.
- If the user's registration is still pending, they will be sent a
-
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 aServerWelcome
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
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
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 aRegistrationStatus.Type.PENDING
message, instead of aServerWelcome
.- Parameters:
clientId
- The id of the client.pendingUsername
- The client's username.clientThread
- The thread managing the client's connection.
-
handleLogOut
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
- Returns:
- The list of connected clients.
-
getPendingClients
- Returns:
- The list of connected, pending clients.
-
getConnectedIds
- Returns:
- The set of ids of all connected clients.
-
getClientById
Tries to find a connected client with the given id.- Parameters:
id
- The id to look for.- Returns:
- An optional client thread.
-
getPendingClientById
Tries to find a pending client with the given id.- Parameters:
id
- The id to look for.- Returns:
- An optional client thread.
-