Package net.targetr.rc.room
Class Room
java.lang.Object
net.targetr.rc.room.Room
Allows multiple clients in the same room to communicate.
- Author:
- Dr Michael Gardiner
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intMaximum retained client slots in a room, including the reconnect grace period. -
Constructor Summary
ConstructorsConstructorDescriptionRoom()Constructs a new Room instance.Room(LongSupplier clock) Creates a room using the manager's elapsed-time clock. -
Method Summary
Modifier and TypeMethodDescriptionattachClient(ClientRequest clientReq) Adds the client to this room.intbroadcast(Client source, ClientCommand command) Broadcasts a JavaScript command to designated targets in this room.voidcollectConnections(List<Closeable> connections) Aggregates all open internal network socket connections.protected booleanexpireIfEmpty(long now, long retentionMillis) Marks an empty room expired, preventing attachment through stale references.Retrieves an existing client given their client ID.getClient(ClientRequest clientReq) Retrieves an existing client given their client request.Lists connected browsers without exposing addresses or retained reconnect slots.intReturns the number of client slots, including recently disconnected clients retained for reconnection.getState()Retrieves the current tracking state for all clients in the same room.voidmaintenence(long recent) Removes expired client slots and queues a membership update when the list changes.
-
Field Details
-
MAX_CLIENTS
public static final int MAX_CLIENTSMaximum retained client slots in a room, including the reconnect grace period.- See Also:
-
-
Constructor Details
-
Room
public Room()Constructs a new Room instance. -
Room
Creates a room using the manager's elapsed-time clock.- Parameters:
clock- supplies monotonic elapsed time in milliseconds
-
-
Method Details
-
broadcast
Broadcasts a JavaScript command to designated targets in this room. Optionally sends a notification back to the source client about the delivery status (for special apps such as c).- Parameters:
source- the client sending the command, or nullcommand- the command payload and its desired targets- Returns:
- the number of matching clients offered the command for direct or queued delivery; this does not confirm browser receipt or execution
-
getClient
Retrieves an existing client given their client request.- Parameters:
clientReq- the incoming HTTP client request- Returns:
- the matching Client instance, or null if not found
-
getClient
Retrieves an existing client given their client ID.- Parameters:
clientId- the requested client ID- Returns:
- the matching Client instance, or null if not found
-
attachClient
Adds the client to this room. Reuses an existing client slot if matched by ID. Managed connections should useRoomManager.attachClient(ClientRequest).- Parameters:
clientReq- the request describing the client- Returns:
- the established Client instance, or null if a new client would exceed capacity
- Throws:
IllegalStateException- if this room has already expired
-
expireIfEmpty
protected boolean expireIfEmpty(long now, long retentionMillis) Marks an empty room expired, preventing attachment through stale references.- Parameters:
now- current elapsed time in milliseconds, using the room's clockretentionMillis- minimum time in milliseconds to retain an empty room- Returns:
- true if the room is empty and its retention period has elapsed; false otherwise
-
getLiveClientCount
public int getLiveClientCount()Returns the number of client slots, including recently disconnected clients retained for reconnection.- Returns:
- the size of the client list
-
getConnectedClients
Lists connected browsers without exposing addresses or retained reconnect slots.- Returns:
- snapshots containing each connected browser's client ID and app name
-
maintenence
public void maintenence(long recent) Removes expired client slots and queues a membership update when the list changes.- Parameters:
recent- the cutoff for ended requests, inClock.realtime()milliseconds
-
getState
Retrieves the current tracking state for all clients in the same room.- Returns:
- a map organizing states by client IDs
-
collectConnections
Aggregates all open internal network socket connections. Useful for safely shutting down network services.- Parameters:
connections- the target list in which to append active connections
-