Package com.iizix.nio

Class SocketCommNIO

java.lang.Object
com.iizix.nio.SocketCommNIO
All Implemented Interfaces:
ISocketComm, IWorkerTimeout
Direct Known Subclasses:
SocketComm

public class SocketCommNIO extends Object implements ISocketComm
The SocketComm class is used to open client sockets with a destination host with optional proxy (HTTP/SOCKS), secure socket (SSL/TLS), and atop WebSocket or Telnet as protocols.

For proxy and/or WebSocket, an authenticator must/should be used to support user authentication with the proxy or destination (for WebSocket).

For SSL/TLS, client certificates can be added, discrete or from system, and if so, an password callback might be have to be registered to provide a password for a keystore or a certificate, unless preset in the properties.

For SSL/TLS, server certificates validator can be provided in order to check server certificates and/or the destinations host name.

Author:
Christopher Mindus
  • Field Details

    • host

      public final String host
      The host name.
    • port

      public final int port
      The port.
    • bindAddress

      public final String bindAddress
      The bind address, null for none.
    • doResolveHostNames

      public boolean doResolveHostNames
      Flag for resolving host names.
  • Constructor Details

    • SocketCommNIO

      public SocketCommNIO(String host, int port, String bindAddress, WorkerNIO worker, SocketCommListener listener, boolean isVerbose, SSLEngine engine)
      Creates a new Socket Client Communication channel as UNINITIALIZED.
      Parameters:
      host - The destination host.
      port - The destination port.
      bindAddress - The bind address or null for none.
      worker - The worker thread for socket processing.
      listener - The listener for socket events.
      isVerbose - Flag for logging.
    • SocketCommNIO

      protected SocketCommNIO(WorkerNIO worker, SocketChannel socketChannel, SocketCommListener listener, boolean isVerbose) throws IOException
      Creates a new Socket Client Communication channel from a Server Socket Accept.
      Throws:
      IOException - For I/O errors.
  • Method Details

    • setLogger

      public static void setLogger(LoggerNIO logger)
      Sets a new logger.
    • isSecure

      public boolean isSecure()
      Checks if this is a secure communication link.
    • getSSLCommLayer

      public SSLCommLayer getSSLCommLayer()
      Gets the SSL communication layer.
      Returns:
      The instance of the SSL communication layer, or null if not secure.
    • setupSocket

      protected Socket setupSocket() throws SocketException
      Sets up the socket.
      Throws:
      SocketException - For socket errors.
    • hasBeenOpen

      public boolean hasBeenOpen()
      Checks if the session has been opened, and if so cannot be reopened but needs to have a new instance.
    • open

      public void open() throws IOException
      Opens the communication.
      Throws:
      IOException
    • enterSecureMode

      public void enterSecureMode(SSLEngine engine, ByteBuffer remainingBuffer) throws IOException
      Enters SSL mode for a plain connection from another protocol, e.g. Telnet.
      Parameters:
      engine - The SSL engine.
      remainingBuffer - Remaining buffer to process, null for none.
      Throws:
      IOException - if an I/O error occurs.
    • process

      public void process(ByteBuffer buffer)
      Processes the SelectionKey as it has generated an event.
      Specified by:
      process in interface ISocketComm
      Parameters:
      buffer - The direct buffer allocated by the worker.
    • setNonConnectedReadTimeout

      public void setNonConnectedReadTimeout(long timeout)
      Sets the read time-out value for the connection before the state enters CONNECTED.
      Parameters:
      timeout - The timeout value in milliseconds, 0 for indefinite.
    • getNonConnectedReadTimeout

      public long getNonConnectedReadTimeout()
      Gets the read time-out value for the connection before the state enters CONNECTED.
      Returns:
      The timeout value in milliseconds, 0 for indefinite.
    • setReadTimeout

      public void setReadTimeout(long timeout)
      Sets the read time-out value for the connection.
      Parameters:
      timeout - The timeout value in milliseconds, 0 for indefinite.
    • getReadTimeout

      public long getReadTimeout()
      Gets the read time-out value for the connection.
      Returns:
      The timeout value in milliseconds, 0 for indefinite.
    • setWriteTimeout

      public void setWriteTimeout(long timeout)
      Sets the read time-out value for the connection.
      Parameters:
      timeout - The timeout value in milliseconds, 0 for indefinite.
    • getWriteTimeout

      public long getWriteTimeout()
      Sets the read time-out value for the connection.
      Returns:
      The timeout value in milliseconds, 0 for indefinite.
    • getRemainingTimeout

      public long getRemainingTimeout(long timeNow)
      Gets the socket timeout value remaining.
      Specified by:
      getRemainingTimeout in interface IWorkerTimeout
      Parameters:
      timeNow - The current time.
      Returns:
      Time in milliseconds until timeout, NOTE! -1 for indefinite timeout.
    • checkTimeout

      public void checkTimeout(long timeNow)
      Performs a timeout check for the socket channel. This is called to all channels, so each channel must check its own state.
      Specified by:
      checkTimeout in interface IWorkerTimeout
      Parameters:
      timeNow - The current time.
    • send

      public void send(ByteBuffer sendBuf) throws IOException
      Adds the byte buffer to the send queue without blocking.
      Parameters:
      sendBuf - The buffer to send.
      Throws:
      IOException - for failures, e.g. socket is closed, not open, not connected.
    • getState

      public State getState()
      Gets the state of the connection.
    • getRemoteAddr

      public String getRemoteAddr()
      Returns the Internet Protocol (IP) address of the client or last proxy that sent the request. The IP address format depends if it is an IPv4 or IPv6 address.
      Returns:
      a String containing the IP address of the client that sent the request.
    • getRemoteHost

      public String getRemoteHost()
      Returns the fully qualified name of the client or the last proxy that sent the request. If the engine cannot or chooses not to resolve the host name (to improve performance), this method returns the dotted-string form of the IP address (if IPv4, colon'ed-string for IPv6).

      The resolving is done once only, if the doResolveHostNames is true.

      Returns:
      a String containing the fully qualified name of the client.
    • getSocketAddress

      public InetSocketAddress getSocketAddress()
      Returns the remote address to which this channel's socket is connected.

      Where the channel is bound and connected to an Internet Protocol socket address then the return value from this method is of type InetSocketAddress.

      Returns:
      The remote address; null if the channel's socket is not connected.
    • isClosed

      public boolean isClosed()
      Checks if the connection is disposed of.
    • close

      public boolean close()
      Call this method to close the socket.

      A call to this method will return true while flushing pending send operations and then close the socket when that is completed. If no send operations are pending, the socket is closed immediately.

      Returns:
      true if closed completed successfully, false if already closed. Note that true is returned even if the socket is entering or is in CLOSING state.
    • close

      public boolean close(boolean rightNow)
      Call this method to close the socket.

      If the parameter "rightNow" is false, a call to this method will return true while flushing pending send operations and then close the socket when that is completed. If no send operations are pending, the socket is closed immediately.

      Specified by:
      close in interface ISocketComm
      Parameters:
      rightNow - If false, a flush of pending send operations will be performed.
      Returns:
      true if closed, false if already closed.