Class ConnectionContext

java.lang.Object
com.loomcache.server.network.ConnectionContext
All Implemented Interfaces:
AutoCloseable

public class ConnectionContext extends Object implements AutoCloseable
Wraps a TCP connection to a peer with automatic resource management and metrics.
Since:
1.0
See Also:
  • Constructor Details

    • ConnectionContext

      public ConnectionContext(String peerId, Socket socket)
      Creates a new connection context for a peer.
      Parameters:
      peerId - the peer identifier (non-null)
      socket - the underlying socket (non-null)
      Throws:
      NullPointerException - if peerId or socket is null
      RuntimeException - if socket streams cannot be obtained
  • Method Details

    • send

      public void send(Message msg) throws IOException
      Send a message to this peer. Thread-safe — uses ReentrantLock (not synchronized) to avoid pinning virtual threads during network I/O.
      Parameters:
      msg - the message to send (non-null)
      Throws:
      IOException - if the connection is closed or send fails
      NullPointerException - if msg is null
    • negotiatedWireCompression

      public WireCompression negotiatedWireCompression()
    • close

      public void close()
      Closes the connection and all associated resources. Safe to call multiple times (idempotent). Properly cleans up socket, input stream, and output stream.

      Use try-with-resources to ensure cleanup: try (var conn = new ConnectionContext(...)) { ... }

      Specified by:
      close in interface AutoCloseable
    • isTransportAuthenticated

      public boolean isTransportAuthenticated()
      Check whether this connection has transport-level (mTLS) client authentication. Returns true only if the underlying socket is an SSLSocket with a verified peer certificate.
      Returns:
      true if the peer's identity was verified via TLS client auth
    • isClusterTransportTrusted

      public boolean isClusterTransportTrusted()
      Returns true only after this connection has completed the cluster-member handshake. A valid mTLS certificate proves transport identity, but it is not enough by itself to authorize Raft, migration, or replication frames.
    • isTransportAuthenticatedAsPeer

      public boolean isTransportAuthenticatedAsPeer(String expectedPeerId)
      Check whether this connection's verified peer certificate matches the expected cluster peer identity. Accepts either a DNS/URI SAN or a subject CN equal to the expected peer ID.
      Parameters:
      expectedPeerId - the expected stable cluster peer ID
      Returns:
      true if mTLS client auth succeeded and the certificate identity matches
    • getCertificateCommonName

      public @Nullable String getCertificateCommonName()
      Extract and cache the Common Name (CN) from the peer's TLS client certificate.

      Returns null if the connection is not TLS, if client auth was not performed, or if the certificate has no CN. The result is cached after the first successful extraction.

      Returns:
      the CN string, or null if unavailable
    • getClientIp

      public @Nullable String getClientIp()
      Get the client's IP address.
      Returns:
      the IP address, or null if not yet assigned
    • setClientIp

      public void setClientIp(@Nullable String ip)
      Set the client's IP address for per-IP tracking.
      Parameters:
      ip - the IP address (may be null)
    • isClosed

      public boolean isClosed()
    • ageMillis

      public long ageMillis()
      Connection age in milliseconds since creation.
    • recordMessageReceived

      public void recordMessageReceived()
      Record an inbound message received on this connection.
    • messagesSent

      public long messagesSent()
      Total messages sent on this connection.
    • messagesReceived

      public long messagesReceived()
      Total messages received on this connection.
    • getBytesReceived

      public long getBytesReceived()
      Total bytes received on this connection.
    • recordBytesReceived

      public void recordBytesReceived(long bytes)
      Record bytes received on this connection.
    • getBytesSent

      public long getBytesSent()
      Total bytes sent on this connection.
    • recordBytesSent

      public void recordBytesSent(long bytes)
      Record bytes sent on this connection.
    • toString

      public String toString()
      Overrides:
      toString in class Object