Class SocketCommNIO
- java.lang.Object
- com.iizix.nio.SocketCommNIO
- All Implemented Interfaces:
ISocketComm
,IWorkerTimeout
- Direct Known Subclasses:
SocketComm
public class SocketCommNIO extends java.lang.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
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SocketCommNIO.State
The state of the communication link.
Field Summary
Fields Modifier and Type Field Description java.lang.String
bindAddress
The bind address, null for none.boolean
doResolveHostNames
Flag for resolving host names.java.lang.String
host
The host name.int
port
The port.
Constructor Summary
Constructors Modifier Constructor Description protected
SocketCommNIO(WorkerNIO worker, java.nio.channels.SocketChannel socketChannel, SocketCommListener listener, boolean isVerbose)
Creates a new Socket Client Communication channel from a Server Socket Accept.SocketCommNIO(java.lang.String host, int port, java.lang.String bindAddress, WorkerNIO worker, SocketCommListener listener, boolean isVerbose, javax.net.ssl.SSLEngine engine)
Creates a new Socket Client Communication channel as UNINITIALIZED.
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
checkTimeout(long timeNow)
Performs a timeout check for the socket channel.boolean
close()
Call this method to close the socket.boolean
close(boolean rightNow)
Call this method to close the socket.void
enterSecureMode(javax.net.ssl.SSLEngine engine, java.nio.ByteBuffer remainingBuffer)
Enters SSL mode for a plain connection from another protocol, e.g.long
getNonConnectedReadTimeout()
Gets the read time-out value for the connection before the state enters CONNECTED.long
getReadTimeout()
Gets the read time-out value for the connection.long
getRemainingTimeout(long timeNow)
Gets the socket timeout value remaining.java.lang.String
getRemoteAddr()
Returns the Internet Protocol (IP) address of the client or last proxy that sent the request.java.lang.String
getRemoteHost()
Returns the fully qualified name of the client or the last proxy that sent the request.java.net.InetSocketAddress
getSocketAddress()
Returns the remote address to which this channel's socket is connected.SSLCommLayer
getSSLCommLayer()
Gets the SSL communication layer.SocketCommNIO.State
getState()
Gets the state of the connection.long
getWriteTimeout()
Sets the read time-out value for the connection.boolean
hasBeenOpen()
Checks if the session has been opened, and if so cannot be reopened but needs to have a new instance.boolean
isClosed()
Checks if the connection is disposed of.boolean
isSecure()
Checks if this is a secure communication link.void
open()
Opens the communication.void
process(java.nio.ByteBuffer buffer)
Processes the SelectionKey as it has generated an event.void
send(java.nio.ByteBuffer sendBuf)
Adds the byte buffer to the send queue without blocking.static void
setLogger(LoggerNIO logger)
Sets a new logger.void
setNonConnectedReadTimeout(long timeout)
Sets the read time-out value for the connection before the state enters CONNECTED.void
setReadTimeout(long timeout)
Sets the read time-out value for the connection.protected java.net.Socket
setupSocket()
Sets up the socket.void
setWriteTimeout(long timeout)
Sets the read time-out value for the connection.
Constructor Detail
SocketCommNIO
public SocketCommNIO(java.lang.String host, int port, java.lang.String bindAddress, WorkerNIO worker, SocketCommListener listener, boolean isVerbose, javax.net.ssl.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, java.nio.channels.SocketChannel socketChannel, SocketCommListener listener, boolean isVerbose) throws java.io.IOException
Creates a new Socket Client Communication channel from a Server Socket Accept.- Throws:
java.io.IOException
- For I/O errors.
Method Detail
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 java.net.Socket setupSocket() throws java.net.SocketException
Sets up the socket.- Throws:
java.net.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 java.io.IOException
Opens the communication.- Throws:
java.io.IOException
enterSecureMode
public void enterSecureMode(javax.net.ssl.SSLEngine engine, java.nio.ByteBuffer remainingBuffer) throws java.io.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:
java.io.IOException
- if an I/O error occurs.
process
public void process(java.nio.ByteBuffer buffer)
Processes the SelectionKey as it has generated an event.- Specified by:
process
in interfaceISocketComm
- 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 interfaceIWorkerTimeout
- 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 interfaceIWorkerTimeout
- Parameters:
timeNow
- The current time.
send
public void send(java.nio.ByteBuffer sendBuf) throws java.io.IOException
Adds the byte buffer to the send queue without blocking.- Parameters:
sendBuf
- The buffer to send.- Throws:
java.io.IOException
- for failures, e.g. socket is closed, not open, not connected.
getState
public SocketCommNIO.State getState()
Gets the state of the connection.
getRemoteAddr
public java.lang.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 java.lang.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 java.net.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 interfaceISocketComm
- Parameters:
rightNow
- If false, a flush of pending send operations will be performed.- Returns:
- true if closed, false if already closed.