Module is.codion.common.rmi
Package is.codion.common.rmi.server
Class AbstractServer<T extends Remote,A extends ServerAdmin>
java.lang.Object
is.codion.common.rmi.server.AbstractServer<T,A>
- Type Parameters:
T- the type of remote interface served by this serverA- the type of the admin interface this server provides
- Direct Known Subclasses:
EntityServer
public abstract class AbstractServer<T extends Remote,A extends ServerAdmin>
extends Object
implements Server<T,A>
A default Server implementation.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static final classRepresents a remote client connection.Nested classes/interfaces inherited from interface is.codion.common.rmi.server.Server
Server.Locator -
Field Summary
Fields inherited from interface is.codion.common.rmi.server.Server
CLIENT_HOST -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractServer(ServerConfiguration configuration) Instantiates a new AbstractServer. -
Method Summary
Modifier and TypeMethodDescriptionprotected final voidaddShutdownListener(Runnable listener) protected final Collection<AuxiliaryServer> final Tconnect(ConnectionRequest connectionRequest) Establishes a connection to this Server.protected abstract Tconnect(RemoteClient remoteClient) Establishes the actual client connection.final Tconnection(UUID clientId) final intfinal intfinal voidconnectionLimit(int connectionLimit) final Map<RemoteClient, T> final booleanfinal voiddisconnect(UUID clientId) Disconnects the connection identified by the given id.protected abstract voiddisconnect(T connection) Disconnects the given connection.protected final AgetAdmin()final ServerInformationprotected final <T extends Throwable>
TlogShutdownAndReturn(T exception) Logs the given exception and shuts down this serverprotected abstract voidmaintainConnections(Collection<AbstractServer.ClientConnection<T>> connections) Maintains the given connections, that is, disconnects inactive or invalid connections, if required.final intfinal voidmaintenanceInterval(int maintenanceInterval) protected final Registryregistry()protected final voidSets the admin instance for this serverfinal voidshutdown()Shuts down this server.protected static voidvalidateUserCredentials(User userToCheck, User requiredUser) Validates the given user credentials.
-
Constructor Details
-
AbstractServer
Instantiates a new AbstractServer.Note that this constructor starts the connection maintenance scheduler, registers a JVM shutdown hook and creates the configured auxiliary servers (passing
this), all before any subclass constructor body runs. Subclass constructors must therefore complete quickly and must not assume that maintenance, shutdown or auxiliary-server callbacks observe fully initialized subclass state.- Parameters:
configuration- the configuration- Throws:
RemoteException- in case of an exception
-
-
Method Details
-
connections
- Returns:
- a map containing the current connections
-
connection
- Parameters:
clientId- the client id- Returns:
- the connection associated with the given client
- Throws:
IllegalArgumentException- in case no such client is connected
-
connectionCount
public final int connectionCount()- Returns:
- the current number of connections
-
connectionLimit
public final int connectionLimit()- Returns:
- the maximum number of concurrent connections accepted by this server, a negative number means no limit while 0 means the server is closed.
-
connectionLimit
public final void connectionLimit(int connectionLimit) - Parameters:
connectionLimit- the maximum number of concurrent connections accepted by this server, a negative number means no limit while 0 means the server is closed.
-
maintenanceInterval
public final int maintenanceInterval()- Returns:
- the maintenance check interval in ms
-
maintenanceInterval
public final void maintenanceInterval(int maintenanceInterval) - Parameters:
maintenanceInterval- the new maintenance interval in ms
-
information
- Specified by:
informationin interfaceServer<T extends Remote,A extends ServerAdmin> - Returns:
- static information about this server
-
connectionsAvailable
public final boolean connectionsAvailable()- Specified by:
connectionsAvailablein interfaceServer<T extends Remote,A extends ServerAdmin> - Returns:
- true if there are connections available
-
connect
public final T connect(ConnectionRequest connectionRequest) throws RemoteException, ConnectionNotAvailableException, LoginException Description copied from interface:ServerEstablishes a connection to this Server.If a connection already exists for the request's
clientId, that existing connection is returned after re-validating the supplied credentials (username case-insensitively, password exactly), rather than establishing a new one.- Specified by:
connectin interfaceServer<T extends Remote,A extends ServerAdmin> - Parameters:
connectionRequest- the information required for establishing a connection- Returns:
- a remote connection instance
- Throws:
RemoteException- in case of a communication errorConnectionNotAvailableException- in case the server isn't accepting more connectionsLoginException- in case the login fails, or if the server is shutting down
-
disconnect
Description copied from interface:ServerDisconnects the connection identified by the given id.- Specified by:
disconnectin interfaceServer<T extends Remote,A extends ServerAdmin> - Parameters:
clientId- the UUID identifying the client that should be disconnected
-
shutdown
public final void shutdown()Shuts down this server. -
setAdmin
Sets the admin instance for this server- Parameters:
admin- the admin instance- Throws:
IllegalStateException- in case an admin instance has already been set
-
getAdmin
- Returns:
- the admin instance associated with this server
- Throws:
IllegalStateException- in case no admin instance has been set- See Also:
-
addShutdownListener
- Parameters:
listener- a listener notified when this server is shutting down.
-
connect
Establishes the actual client connection.- Parameters:
remoteClient- the remote client- Returns:
- a connection for the given client
- Throws:
RemoteException- in case of an exceptionLoginException- in case of an error during the login
-
disconnect
Disconnects the given connection.- Parameters:
connection- the connection to disconnect- Throws:
RemoteException- in case of an exception
-
maintainConnections
protected abstract void maintainConnections(Collection<AbstractServer.ClientConnection<T>> connections) throws RemoteException Maintains the given connections, that is, disconnects inactive or invalid connections, if required.- Parameters:
connections- all current connections- Throws:
RemoteException- in case of an exception
-
registry
- Returns:
- the Registry instance
- Throws:
RemoteException- in case of an exception
-
logShutdownAndReturn
Logs the given exception and shuts down this server- Type Parameters:
T- the exception type- Parameters:
exception- the exception- Returns:
- the exception
-
auxiliaryServers
- Returns:
- an unmodifiable view of the auxiliary servers running alongside this server
-
validateUserCredentials
protected static void validateUserCredentials(User userToCheck, User requiredUser) throws ServerAuthenticationException Validates the given user credentials.Usernames are compared case-insensitively, passwords byte-exactly.
- Parameters:
userToCheck- the credentials to checkrequiredUser- the required credentials- Throws:
ServerAuthenticationException- in case either User instance is null or if the username or password do not match
-