Class Room

java.lang.Object
net.targetr.rc.room.Room

public class Room extends Object
Allows multiple clients in the same room to communicate.
Author:
Dr Michael Gardiner
  • Field Details

    • MAX_CLIENTS

      public static final int MAX_CLIENTS
      Maximum retained client slots in a room, including the reconnect grace period.
      See Also:
  • Constructor Details

    • Room

      public Room()
      Constructs a new Room instance.
    • Room

      public Room(LongSupplier clock)
      Creates a room using the manager's elapsed-time clock.
      Parameters:
      clock - supplies monotonic elapsed time in milliseconds
  • Method Details

    • broadcast

      public int broadcast(Client source, ClientCommand command)
      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 null
      command - 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

      public Client getClient(ClientRequest clientReq)
      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

      public Client getClient(String clientId)
      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

      public Client attachClient(ClientRequest clientReq)
      Adds the client to this room. Reuses an existing client slot if matched by ID. Managed connections should use RoomManager.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 clock
      retentionMillis - 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

      public List<Map<String,Object>> 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, in Clock.realtime() milliseconds
    • getState

      public Map<String,Object> getState()
      Retrieves the current tracking state for all clients in the same room.
      Returns:
      a map organizing states by client IDs
    • collectConnections

      public void collectConnections(List<Closeable> connections)
      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