Class ConnectionTracker

java.lang.Object
com.oorian.security.ConnectionTracker

public class ConnectionTracker extends Object
Tracks active WebSocket connections per session and per IP address.

Used to enforce connection limits configured via Application.setMaxConnectionsPerSession(int) and Application.setMaxConnectionsPerIp(int). Limits of 0 mean unlimited.

This class is thread-safe. All operations use concurrent data structures and atomic counters.

Usage:


 // Check before allowing a new WebSocket connection
 if (ConnectionTracker.canConnect(sessionId, ipAddress))
 {
     ConnectionTracker.onConnect(sessionId, ipAddress);
     // Allow connection
 }
 else
 {
     // Reject connection
 }

 // When connection closes
 ConnectionTracker.onDisconnect(sessionId, ipAddress);
 
Since:
2.1
Version:
1.0
Author:
Marvin P. Warble Jr.
  • Method Details

    • canConnect

      public static boolean canConnect(String sessionId, String ipAddress)
      Checks whether a new connection is allowed for the given session and IP address.

      A connection is allowed if both the per-session and per-IP limits have not been reached. A limit of 0 means unlimited.

      Parameters:
      sessionId - the session identifier
      ipAddress - the client's IP address
      Returns:
      true if the connection is allowed, false if a limit would be exceeded
    • onConnect

      public static void onConnect(String sessionId, String ipAddress)
      Records a new connection for the given session and IP address.

      Increments the connection counters for both the session and the IP address.

      Parameters:
      sessionId - the session identifier
      ipAddress - the client's IP address
    • onDisconnect

      public static void onDisconnect(String sessionId, String ipAddress)
      Records a closed connection for the given session and IP address.

      Decrements the connection counters. If a counter reaches zero, the entry is removed to prevent memory leaks from accumulated keys.

      Parameters:
      sessionId - the session identifier
      ipAddress - the client's IP address
    • getSessionConnectionCount

      public static int getSessionConnectionCount(String sessionId)
      Returns the current number of active connections for the given session.
      Parameters:
      sessionId - the session identifier
      Returns:
      the number of active connections, or 0 if none
    • getIpConnectionCount

      public static int getIpConnectionCount(String ipAddress)
      Returns the current number of active connections from the given IP address.
      Parameters:
      ipAddress - the IP address
      Returns:
      the number of active connections, or 0 if none
    • clear

      public static void clear()
      Clears all connection tracking data.

      Called during application shutdown to release resources.