Class AbstractConnection

java.lang.Object
net.messagevortex.transport.AbstractConnection
Direct Known Subclasses:
ClientConnection, ServerConnection

public abstract class AbstractConnection extends Object
Abstract functions for creating a TLS channel socket.
Author:
Martin Gwerder
  • Field Details

  • Constructor Details

    • AbstractConnection

      public AbstractConnection(InetSocketAddress remoteAddress, SecurityContext context)
    • AbstractConnection

      public AbstractConnection(AbstractConnection ac)

      This copy constructor enables duplication of a connection.

      Parameters:
      ac - A connection to be copied
    • AbstractConnection

      public AbstractConnection(SocketChannel sock, SecurityContext context, boolean isClient)

      Create a connection with the given context.

      Parameters:
      sock - the channel to connect to
      context - the predefined security context
      isClient - true if the connection is a client connection
    • AbstractConnection

      public AbstractConnection(SocketChannel sock, SecurityContext context)

      Create a connection with the given context.

      Parameters:
      sock - the channel to connect to
      context - the predefined security context
  • Method Details

    • getHostName

      public String getHostName()

      Get the hostname of the remote host.

      Returns:
      the hostname
    • getPort

      public int getPort()

      Gets the port of the remote host.

      Returns:
      the remote port number (if known; otherwise -1)
    • setSocketChannel

      protected final SocketChannel setSocketChannel(SocketChannel s)
    • getSocketChannel

      public SocketChannel getSocketChannel() throws IOException

      Returns the socket channel in use for this connection.

      Returns:
      the socket channel of this connection
      Throws:
      IOException - if socket is not connected
    • setSecurityContext

      public final SecurityContext setSecurityContext(SecurityContext context)

      Sets the security context to be used with the socket channel.

      Parameters:
      context - the security context to be used
      Returns:
      the previously set security context
    • getSecurityContext

      public SecurityContext getSecurityContext()

      Gets the security context used with the socket channel.

      Returns:
      the security context
    • getEngine

      protected SSLEngine getEngine()
    • setEngine

      protected final SSLEngine setEngine(SSLEngine engine)
    • connect

      public void connect() throws IOException

      Connects to the remote host with respective security context.

      Throws:
      IOException - if connecting fails
    • startTls

      public void startTls() throws IOException

      Make a TLS handshake on the connection with the default timeout.

      Throws:
      IOException - if handshake fails
    • startTls

      public void startTls(long timeout) throws IOException

      Make a TLS handshake on the connection with a specified timeout.

      Parameters:
      timeout - the timeout in milliseconds
      Throws:
      IOException - if handshake fails
    • setProtocol

      public String setProtocol(String protocol)

      Sets the protocol to be used (mainly for logger messages).

      Parameters:
      protocol - the protocol name or abbreviation
      Returns:
      the previously set protocol name
    • getProtocol

      public String getProtocol()

      Gets the protocol name used.

      Returns:
      the protocol name
    • do_handshake

      protected void do_handshake(long timeout) throws IOException
      Throws:
      IOException
    • do_teardown

      protected void do_teardown(long timeout) throws IOException
      Throws:
      IOException
    • isTls

      public boolean isTls()

      returns true if a TLS handshake has been successfully done.

      Returns:
      the TLS state
    • setDefaultTimeout

      public static long setDefaultTimeout(long timeout)

      Sets the default timeout for all connections not having an own timeout.

      Parameters:
      timeout - the timeout in milliseconds
      Returns:
      the previously set timeout
    • getDefaultTimeout

      public static long getDefaultTimeout()

      Gets the default timeout for all connections not having an own timeout.

      Returns:
      the previously set timeout
    • setTimeout

      public long setTimeout(long timeout)

      Sets the default timeout for this connection.

      Parameters:
      timeout - the timeout in milliseconds
      Returns:
      the previously set timeout
    • getTimeout

      public long getTimeout()

      Gets the default timeout for this connection.

      Returns:
      the timeout in milliseconds
    • writeln

      public void writeln(String message) throws IOException
      Throws:
      IOException
    • writeln

      public void writeln(String message, long timeout) throws IOException
      Throws:
      IOException
    • write

      public void write(String message) throws IOException
      Throws:
      IOException
    • write

      public void write(String message, long timeout) throws IOException

      Write a message string to the peer partner.

      Parameters:
      message - the message string to be sent
      timeout - the timeout in milliseconds
      Throws:
      IOException - if communication or encryption fails
    • read

      public String read() throws IOException, TimeoutException
      Throws:
      IOException
      TimeoutException
    • read

      public String read(long timeout) throws IOException, TimeoutException

      Read a string from the socket channel.

      Parameters:
      timeout - the timeout to be applied before unblocking
      Returns:
      the string read
      Throws:
      IOException - if decryption fails or host is unexpectedly disconnected
      TimeoutException - if reaching a timeout while reading
    • readln

      public String readln() throws IOException, TimeoutException
      Throws:
      IOException
      TimeoutException
    • readln

      public String readln(long timeout) throws IOException, TimeoutException

      Read a string up until CRLF from the socket channel.

      Parameters:
      timeout - the timeout to be applied before unblocking
      Returns:
      the string read
      Throws:
      IOException - if decryption fails or host is unexpectedly disconnected
      TimeoutException - if reaching a timeout while reading
    • enlargePacketBuffer

      protected ByteBuffer enlargePacketBuffer(SSLEngine engine, ByteBuffer buffer)
    • enlargeApplicationBuffer

      protected ByteBuffer enlargeApplicationBuffer(SSLEngine engine, ByteBuffer buffer)
    • enlargeBuffer

      protected ByteBuffer enlargeBuffer(ByteBuffer buffer, int sessionProposedCapacity)
    • handleBufferUnderflow

      protected ByteBuffer handleBufferUnderflow(SSLEngine engine, ByteBuffer buffer)
    • closeConnection

      protected void closeConnection() throws IOException
      Throws:
      IOException
    • handleEndOfStream

      protected void handleEndOfStream() throws IOException
      Throws:
      IOException
    • shutdown

      public void shutdown() throws IOException
      Throws:
      IOException
    • isShutdown

      public boolean isShutdown()