Class ConnectionHandler

java.lang.Object
org.tentackle.trip.transport.ConnectionHandler
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
LoopbackConnectionHandler

public class ConnectionHandler extends Object implements Runnable
Handler for a new incoming connection.

Each handler runs in a virtual thread, so that the deserialization of remote calls, which takes time depending on the incoming data rate, does not block regular threads. When a call is deserialized, it is dispatched to a regular thread from the call thread pool. This combination of virtual and regular threads reduces the total number of threads, since we don't need a regular thread per incoming connection anymore, but only that many threads that are necessary to handle the remote calls concurrently.

See Also:
  • Constructor Details

    • ConnectionHandler

      public ConnectionHandler(Transport transport, String connectionName, InputStream inputStream, OutputStream outputStream)
      Creates a new handler.
      Parameters:
      transport - the transport associated with this handler
      connectionName - the name of the connection
      inputStream - the input stream associated with this connection
      outputStream - the output stream associated with this connection
    • ConnectionHandler

      public ConnectionHandler(Transport transport, String connectionName, InputStream inputStream, OutputStream outputStream, Supplier<List<X509Certificate>> peerCertificates)
      Creates a new handler for a connection that may provide peer certificates.

      The certificates are requested once the connection request has been read, i.e. when the TLS handshake, if any, is complete. They are passed on to the TransportClientInfo.

      Parameters:
      transport - the transport associated with this handler
      connectionName - the name of the connection
      inputStream - the input stream associated with this connection
      outputStream - the output stream associated with this connection
      peerCertificates - supplies the verified certificate chain of the peer, leaf first, empty if none
  • Method Details

    • getTransport

      public Transport getTransport()
      Gets the transport associated with this handler.
      Returns:
      the transport
    • getConnectionName

      public String getConnectionName()
      Gets the name of the connection.
      Returns:
      the name
    • getInputStream

      public InputStream getInputStream()
      Gets the input stream associated with this connection.
      Returns:
      the input stream
    • getOutputStream

      public OutputStream getOutputStream()
      Gets the output stream associated with this connection.
      Returns:
      the output stream
    • run

      public void run()
      Handles the new connection.
      • reads the connection request: the TRIP magic followed by the UUID of the client's transport and whether the client's dictionary is still empty. An invalid magic, a stale dictionary or any other failure of the handshake closes the connection after the RejectionDelay.
      • the UUID is used to determine the dictionary and a TRIP stream is created
      • reads the remote calls from the stream and dispatches them to the call handler thread pool.
      Specified by:
      run in interface Runnable
    • isAuthenticated

      public boolean isAuthenticated()
      Returns whether the client of this connection is authenticated.
      Returns:
      true if authenticated, false if not or the handshake is not complete yet
    • evict

      protected void evict()
      Closes the connection on behalf of the pre-authentication policy.
      Unblocks the thread reading from the connection.
    • getPeerCertificates

      protected List<X509Certificate> getPeerCertificates()
      Gets the verified certificate chain of the peer.
      Invoked once the connection request has been read.
      Returns:
      the certificates, leaf first, empty if none
      Throws:
      TripRuntimeException - if the certificates cannot be determined
    • isRejectionDelayed

      protected boolean isRejectionDelayed()
      Returns whether rejections are delayed by the RejectionDelay.
      A peer must not tell from the response time why it was rejected, or whether it talks to a TRIP server at all.
      Returns:
      true if delayed (default)
    • isConnectionAlive

      protected boolean isConnectionAlive()
      Checks whether the connection is still alive.
      Returns:
      true if the connection is still alive, false to terminate the handler
    • createStream

      protected TripStream createStream(UUID id, boolean clientDictionaryEmpty)
      Creates a new stream for the given UUID.
      Parameters:
      id - the UUID of the client's transport
      clientDictionaryEmpty - true if the client's dictionary is still empty
      Returns:
      the trip stream
    • isErrorSuppressed

      protected boolean isErrorSuppressed(PreAuthenticationPolicy policy, PreAuthenticationPolicy.Admission admission)
      Returns whether errors are not answered.
      As long as the client is not authenticated by an enforcing policy, the server sends nothing but the responses of successful calls. An error message would tell a prober that it talks to a TRIP server.
      Parameters:
      policy - the pre-authentication policy, null if none
      admission - the admission, null if none or authenticated
      Returns:
      true if errors are not answered