org.xsocket.connection
Class NonBlockingConnection

Package class diagram package NonBlockingConnection
java.lang.Object
  extended by org.xsocket.connection.AbstractNonBlockingStream
      extended by org.xsocket.connection.NonBlockingConnection
All Implemented Interfaces:
Closeable, Flushable, Channel, GatheringByteChannel, ReadableByteChannel, WritableByteChannel, IConnection, INonBlockingConnection, IDataSink, IDataSource

public final class NonBlockingConnection
extends AbstractNonBlockingStream
implements INonBlockingConnection

Implementation of the INonBlockingConnection interface.

Depending on the constructor parameter waitForConnect a newly created connection is in the open state. Write or read methods can be called immediately. Absence of the waitForConnect parameter is equals to waitForConnect==true.

The methods of this class are not thread-safe.

Author:
grro@xsocket.org

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.xsocket.connection.IConnection
IConnection.FlushMode
 
Field Summary
static long DEFAULT_SEND_TIMEOUT_MILLIS
           
static String SEND_TIMEOUT_KEY
           
 
Fields inherited from interface org.xsocket.connection.INonBlockingConnection
UNLIMITED
 
Fields inherited from interface org.xsocket.connection.IConnection
DEFAULT_AUTOFLUSH, DEFAULT_CONNECTION_TIMEOUT_MILLIS, DEFAULT_FLUSH_MODE, DEFAULT_IDLE_TIMEOUT_MILLIS, INITIAL_DEFAULT_ENCODING, MAX_TIMEOUT_MILLIS, SO_KEEPALIVE, SO_LINGER, SO_RCVBUF, SO_REUSEADDR, SO_SNDBUF, SO_TIMEOUT, TCP_NODELAY
 
Constructor Summary
  NonBlockingConnection(InetAddress address, int port)
          constructor.
  NonBlockingConnection(InetAddress address, int port, IHandler appHandler)
          constructor.
  NonBlockingConnection(InetAddress address, int port, IHandler appHandler, boolean autoflush, IConnection.FlushMode flushmode)
          constructor.
  NonBlockingConnection(InetAddress address, int port, IHandler appHandler, boolean waitForConnect, int connectTimeoutMillis)
          constructor.
  NonBlockingConnection(InetAddress address, int port, IHandler appHandler, boolean waitForConnect, int connectTimeoutMillis, Executor workerPool)
          constructor.
  NonBlockingConnection(InetAddress address, int port, IHandler appHandler, boolean waitForConnect, int connectTimeoutMillis, Map<String,Object> options)
          constructor.
  NonBlockingConnection(InetAddress address, int port, IHandler appHandler, boolean waitForConnect, int connectTimeoutMillis, SSLContext sslContext, boolean sslOn)
          constructor.
  NonBlockingConnection(InetAddress address, int port, IHandler appHandler, boolean waitForConnect, int connectTimeoutMillis, SSLContext sslContext, boolean sslOn, Executor workerPool)
          constructor.
  NonBlockingConnection(InetAddress address, int port, IHandler appHandler, Executor workerPool)
          constructor.
  NonBlockingConnection(InetAddress address, int port, IHandler appHandler, int connectTimeoutMillis)
          constructor.
  NonBlockingConnection(InetAddress address, int port, IHandler appHandler, int connectTimeoutMillis, Executor workerPool)
          constructor.
  NonBlockingConnection(InetAddress address, int port, IHandler appHandler, int connectTimeoutMillis, Map<String,Object> options)
          constructor.
  NonBlockingConnection(InetAddress address, int port, IHandler appHandler, int connectTimeoutMillis, SSLContext sslContext, boolean sslOn)
          constructor.
  NonBlockingConnection(InetAddress address, int port, IHandler appHandler, int connectTimeoutMillis, SSLContext sslContext, boolean sslOn, Executor workerPool)
          constructor.
  NonBlockingConnection(InetAddress address, int port, IHandler appHandler, Map<String,Object> options)
          constructor.
  NonBlockingConnection(InetAddress address, int port, IHandler appHandler, SSLContext sslContext, boolean sslOn)
          constructor.
  NonBlockingConnection(InetAddress address, int port, int connectTimeoutMillis)
          constructor.
  NonBlockingConnection(InetAddress address, int port, int connectTimeoutMillis, Map<String,Object> options)
          constructor.
  NonBlockingConnection(InetAddress address, int port, Map<String,Object> options)
          constructor.
  NonBlockingConnection(InetAddress address, int port, Map<String,Object> options, SSLContext sslContext, boolean sslOn)
          constructor.
  NonBlockingConnection(InetAddress address, int port, SSLContext sslContext, boolean sslOn)
          constructor.
  NonBlockingConnection(InetSocketAddress address)
          constructor.
  NonBlockingConnection(String hostname, int port)
          constructor.
  NonBlockingConnection(String hostname, int port, IHandler appHandler)
          constructor

constructor.
  NonBlockingConnection(String hostname, int port, IHandler appHandler, Executor workerPool)
          constructor

constructor.
  NonBlockingConnection(String hostname, int port, IHandler appHandler, Map<String,Object> options)
          constructor

constructor.
  NonBlockingConnection(String hostname, int port, Map<String,Object> options, SSLContext sslContext, boolean sslOn)
          constructor.
  NonBlockingConnection(String hostname, int port, SSLContext sslContext, boolean sslOn)
          constructor.
protected NonBlockingConnection(org.xsocket.connection.TimeoutManager connectionManager, IHandler handlerAdapter)
          server-side constructor
 
Method Summary
 void activateSecuredMode()
          ad hoc activation of a secured mode (SSL).
 void close()
          
 void flush()
          flush the send buffer.
 long getConnectionTimeoutMillis()
          gets the connection timeout
 IHandler getHandler()
          gets the connection handler
 String getId()
          returns the id
 long getIdleTimeoutMillis()
          returns the idle timeout in millis.
 InetAddress getLocalAddress()
          returns the local address
 int getLocalPort()
          returns the local port
 int getMaxReadBufferThreshold()
          get the max app read buffer size.
 long getNumberOfReceivedBytes()
           
 long getNumberOfSendBytes()
           
 Object getOption(String name)
          returns the value of a option
 Map<String,Class> getOptions()
          Returns an unmodifiable map of the options supported by this end point.
 int getPendingWriteDataSize()
          returns the size of the data which have already been written, but not yet transferred to the underlying socket.
 long getRemainingMillisToConnectionTimeout()
          returns the remaining time before a connection timeout occurs
 long getRemainingMillisToIdleTimeout()
          returns the remaining time before a idle timeout occurs
 InetAddress getRemoteAddress()
          returns the remote address
 int getRemotePort()
          returns the port of the remote end point
 Executor getWorkerpool()
          gets the workerpool
protected  int getWriteTransferChunkeSize()
          returns the default chunk size for writing
 int getWriteTransferRate()
          gets the send delay time.
protected  boolean isDataWriteable()
          returns true, if the underlying data sink is open
protected  boolean isMoreInputDataExpected()
          returns true, if the underlying data source is open
 boolean isOpen()
          return if the data source is open.
 boolean isReadSuspended()
          returns true if read is suspended
 boolean isSecure()
          returns if the connection is in secured mode
 boolean isSecuredModeActivateable()
          returns if secured mode is activateable
protected  void onPostAppend()
           
protected  void onPostRead()
          notification method which will be called after data has been read
protected  void onWriteDataInserted()
          notification, that data has been inserted
 ByteBuffer[] readByteBufferByDelimiter(String delimiter, String encoding, int maxLength)
          read a ByteBuffer by using a delimiter For performance reasons, the ByteBuffer readByteBuffer method is generally preferable to get bytes
 ByteBuffer[] readByteBufferByLength(int length)
          read a ByteBuffer
protected  ByteBuffer readSingleByteBuffer(int length)
          read a byte buffer by length.
protected  boolean reset()
          pool support (the connection- and idle timeout and handler will not be reset)
 void resumeRead()
          resume reading data from the underlying subsystem
 void setConnectionTimeoutMillis(long timeoutMillis)
          sets the max time for a connections.
 void setFlushmode(IConnection.FlushMode flushMode)
          set the flush mode

By setting the flush mode with ASYNC (default is SYNC) the data will be transferred to the underlying connection in a asynchronous way.
 void setHandler(IHandler hdl)
          set the connection handler.
 void setIdleTimeoutMillis(long timeoutMillis)
          sets the idle timeout in millis
 void setMaxReadBufferThreshold(int size)
          set the max app read buffer threshold
 void setOption(String name, Object value)
          sets the value of a option.
 void setWorkerpool(Executor workerpool)
          sets the worker pool
 void setWriteTransferRate(int bytesPerSecond)
          set the send delay time.
 void suspendRead()
          suspend reading data from the underlying subsystem
 String toString()
          
 long transferFrom(ReadableByteChannel sourceChannel)
          transfer the data of the source channel to this data sink
 
Methods inherited from class org.xsocket.connection.AbstractNonBlockingStream
appendDataToReadBuffer, appendDataToReadBuffer, available, drainWriteQueue, getAttachment, getEncoding, getFlushmode, getReadBufferVersion, getReadQueueSize, getWriteBufferSize, indexOf, indexOf, isAutoflush, isReadBufferEmpty, isWriteBufferEmpty, markReadPosition, markWritePosition, onPreWrite, printReadBuffer, printWriteBuffer, read, readByte, readByteBufferByDelimiter, readByteBufferByDelimiter, readByteBufferByDelimiter, readBytesByDelimiter, readBytesByDelimiter, readBytesByDelimiter, readBytesByDelimiter, readBytesByLength, readDouble, readInt, readLong, readShort, readStringByDelimiter, readStringByDelimiter, readStringByDelimiter, readStringByDelimiter, readStringByLength, readStringByLength, removeReadMark, removeWriteMark, resetToReadMark, resetToWriteMark, setAttachment, setAutoflush, setEncoding, transferFrom, transferFrom, transferTo, write, write, write, write, write, write, write, write, write, write, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.xsocket.connection.INonBlockingConnection
available, getEncoding, getFlushmode, getReadBufferVersion, indexOf, indexOf, isAutoflush, markReadPosition, markWritePosition, readByteBufferByDelimiter, readBytesByDelimiter, readBytesByDelimiter, readStringByDelimiter, readStringByDelimiter, readStringByLength, removeReadMark, removeWriteMark, resetToReadMark, resetToWriteMark, setAutoflush, setEncoding, transferFrom, write
 
Methods inherited from interface org.xsocket.connection.IConnection
getAttachment, setAttachment
 
Methods inherited from interface org.xsocket.IDataSource
read, readByte, readByteBufferByDelimiter, readByteBufferByDelimiter, readBytesByDelimiter, readBytesByDelimiter, readBytesByLength, readDouble, readInt, readLong, readShort, readStringByDelimiter, readStringByDelimiter, readStringByLength, transferTo
 
Methods inherited from interface org.xsocket.IDataSink
transferFrom, write, write, write, write, write, write, write, write, write, write, write, write
 
Methods inherited from interface java.nio.channels.GatheringByteChannel
write, write
 
Methods inherited from interface java.nio.channels.WritableByteChannel
write
 
Methods inherited from interface java.nio.channels.ReadableByteChannel
read
 

Field Detail

SEND_TIMEOUT_KEY

public static final String SEND_TIMEOUT_KEY
See Also:
Constant Field Values

DEFAULT_SEND_TIMEOUT_MILLIS

public static final long DEFAULT_SEND_TIMEOUT_MILLIS
See Also:
Constant Field Values
Constructor Detail

NonBlockingConnection

public NonBlockingConnection(String hostname,
                             int port)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
hostname - the remote host
port - the port of the remote host to connect
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote host
port - the port of the remote host to connect
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetSocketAddress address)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote host address
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             int connectTimeoutMillis)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote host
port - the port of the remote host to connect
connectTimeoutMillis - the timeout of the connect procedure
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             Map<String,Object> options)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote host
port - the port of the remote host to connect
options - the socket options
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             int connectTimeoutMillis,
                             Map<String,Object> options)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote host
port - the port of the remote host to connect
connectTimeoutMillis - the timeout of the connect procedure
options - the socket options
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             IHandler appHandler)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote address
port - the remote port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             IHandler appHandler,
                             int connectTimeoutMillis)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote address
port - the remote port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
connectTimeoutMillis - the timeout of the connect procedure
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             IHandler appHandler,
                             boolean waitForConnect,
                             int connectTimeoutMillis)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote address
port - the remote port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
waitForConnect - true, if the constructor should block until the connection is established. If the connect fails, the handler's onData and onDisconnect method will be called (if present)
connectTimeoutMillis - the timeout of the connect procedure
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             IHandler appHandler,
                             SSLContext sslContext,
                             boolean sslOn)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote address
port - the remote port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
sslContext - the ssl context
sslOn - true, if ssl should be activated
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             IHandler appHandler,
                             int connectTimeoutMillis,
                             SSLContext sslContext,
                             boolean sslOn)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote address
port - the remote port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
sslContext - the ssl context
sslOn - true, if ssl should be activated
connectTimeoutMillis - the timeout of the connect procedure
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             IHandler appHandler,
                             boolean waitForConnect,
                             int connectTimeoutMillis,
                             SSLContext sslContext,
                             boolean sslOn)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote address
port - the remote port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
sslContext - the ssl context
sslOn - true, if ssl should be activated
waitForConnect - true, if the constructor should block until the connection is established. If the connect fails, the handler's onData and onDisconnect method will be called (if present)
connectTimeoutMillis - the timeout of the connect procedure
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             IHandler appHandler,
                             Map<String,Object> options)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote address
port - the remote port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
options - the socket options
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             IHandler appHandler,
                             int connectTimeoutMillis,
                             Map<String,Object> options)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote address
port - the remote port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
connectTimeoutMillis - the timeout of the connect procedure
options - the socket options
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             IHandler appHandler,
                             boolean waitForConnect,
                             int connectTimeoutMillis,
                             Map<String,Object> options)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote address
port - the remote port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
waitForConnect - true, if the constructor should block until the connection is established. If the connect fails, the handler's onData and onDisconnect method will be called (if present)
connectTimeoutMillis - the timeout of the connect procedure
options - the socket options
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(String hostname,
                             int port,
                             IHandler appHandler)
                      throws IOException
constructor

constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
hostname - the remote host
port - the remote port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(String hostname,
                             int port,
                             IHandler appHandler,
                             Executor workerPool)
                      throws IOException
constructor

constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
hostname - the remote host
port - the remote port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
workerPool - the worker pool to use
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(String hostname,
                             int port,
                             IHandler appHandler,
                             Map<String,Object> options)
                      throws IOException
constructor

constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
hostname - the remote host
port - the remote port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
options - the socket options
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             SSLContext sslContext,
                             boolean sslOn)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote address
port - the remote port
sslContext - the ssl context to use
sslOn - true, activate SSL mode. false, ssl can be activated by user (see IReadWriteableConnection#activateSecuredMode())
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             Map<String,Object> options,
                             SSLContext sslContext,
                             boolean sslOn)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote address
port - the remote port
options - the socket options
sslContext - the ssl context to use
sslOn - true, activate SSL mode. false, ssl can be activated by user (see IReadWriteableConnection#activateSecuredMode())
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(String hostname,
                             int port,
                             SSLContext sslContext,
                             boolean sslOn)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
hostname - the remote host
port - the remote port
sslContext - the ssl context to use
sslOn - true, activate SSL mode. false, ssl can be activated by user (see IReadWriteableConnection#activateSecuredMode())
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(String hostname,
                             int port,
                             Map<String,Object> options,
                             SSLContext sslContext,
                             boolean sslOn)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
hostname - the remote host
port - the remote port
options - the socket options
sslContext - the ssl context to use
sslOn - true, activate SSL mode. false, ssl can be activated by user (see IReadWriteableConnection#activateSecuredMode())
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             IHandler appHandler,
                             Executor workerPool)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote address
port - the remote port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
workerPool - the worker pool to use
Throws:
IOException - If some other I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             IHandler appHandler,
                             int connectTimeoutMillis,
                             Executor workerPool)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote address
port - the remote port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
connectTimeoutMillis - the timeout of the connect procedure
workerPool - the worker pool to use
Throws:
IOException - if a I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             IHandler appHandler,
                             boolean waitForConnect,
                             int connectTimeoutMillis,
                             Executor workerPool)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote address
port - the remote port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
waitForConnect - true, if the constructor should block until the connection is established. If the connect fails, the handler's onData and onDisconnect method will be called (if present)
connectTimeoutMillis - the timeout of the connect procedure
workerPool - the worker pool to use
Throws:
IOException - if a I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             IHandler appHandler,
                             int connectTimeoutMillis,
                             SSLContext sslContext,
                             boolean sslOn,
                             Executor workerPool)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote address
port - the remote port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)r
connectTimeoutMillis - the timeout of the connect procedure
sslContext - the ssl context to use
sslOn - true, activate SSL mode. false, ssl can be activated by user (see IReadWriteableConnection#activateSecuredMode())
workerPool - the worker pool to use
Throws:
IOException - if a I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             IHandler appHandler,
                             boolean waitForConnect,
                             int connectTimeoutMillis,
                             SSLContext sslContext,
                             boolean sslOn,
                             Executor workerPool)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote address
port - the remote port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)r * @param waitForConnect true, if the constructor should block until the connection is established. If the connect fails, the handler's onData and onDisconnect method will be called (if present)
connectTimeoutMillis - the timeout of the connect procedure
sslContext - the ssl context to use
sslOn - true, activate SSL mode. false, ssl can be activated by user (see IReadWriteableConnection#activateSecuredMode())
workerPool - the worker pool to use
Throws:
IOException - if a I/O error occurs

NonBlockingConnection

public NonBlockingConnection(InetAddress address,
                             int port,
                             IHandler appHandler,
                             boolean autoflush,
                             IConnection.FlushMode flushmode)
                      throws IOException
constructor. This constructor will be used to create a non blocking client-side connection.

Parameters:
address - the remote address
port - the remote port
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)
autoflush - the auto flush mode
flushmode - the flush mode
Throws:
IOException - if a I/O error occurs

NonBlockingConnection

protected NonBlockingConnection(org.xsocket.connection.TimeoutManager connectionManager,
                                IHandler handlerAdapter)
                         throws IOException
server-side constructor

Throws:
IOException
Method Detail

getMaxReadBufferThreshold

public int getMaxReadBufferThreshold()
get the max app read buffer size. If the read buffer size exceeds this limit the connection will stop receiving data. The read buffer size can be higher the limit (max size = maxReadBufferThreshold + socket read buffer size * 2)

Specified by:
getMaxReadBufferThreshold in interface INonBlockingConnection
Returns:
the max read buffer threshold

setMaxReadBufferThreshold

public void setMaxReadBufferThreshold(int size)
set the max app read buffer threshold

Specified by:
setMaxReadBufferThreshold in interface INonBlockingConnection

isMoreInputDataExpected

protected boolean isMoreInputDataExpected()
returns true, if the underlying data source is open

Specified by:
isMoreInputDataExpected in class AbstractNonBlockingStream
Returns:
true, if the underlying data source is open

isDataWriteable

protected boolean isDataWriteable()
returns true, if the underlying data sink is open

Specified by:
isDataWriteable in class AbstractNonBlockingStream
Returns:
true, if the underlying data sink is open

setHandler

public final void setHandler(IHandler hdl)
                      throws IOException
set the connection handler.

Specified by:
setHandler in interface INonBlockingConnection
Parameters:
hdl - the handler
Throws:
IOException - If some other I/O error occurs

getHandler

public IHandler getHandler()
gets the connection handler

Specified by:
getHandler in interface INonBlockingConnection
Returns:
the handler

setWorkerpool

public void setWorkerpool(Executor workerpool)
sets the worker pool

Parameters:
workerpool - the worker pool

getWorkerpool

public Executor getWorkerpool()
gets the workerpool

Specified by:
getWorkerpool in interface INonBlockingConnection
Returns:
the workerpool

setFlushmode

public final void setFlushmode(IConnection.FlushMode flushMode)
set the flush mode

By setting the flush mode with ASYNC (default is SYNC) the data will be transferred to the underlying connection in a asynchronous way.

Specified by:
setFlushmode in interface INonBlockingConnection
Overrides:
setFlushmode in class AbstractNonBlockingStream
Parameters:
flushMode - IConnection.FlushMode.ASYNC if flush should be performed asynchronous, IConnection.FlushMode.SYNC if flush should be perform synchronous

reset

protected boolean reset()
pool support (the connection- and idle timeout and handler will not be reset)

Overrides:
reset in class AbstractNonBlockingStream
Returns:
true, if connection has been reset

isOpen

public final boolean isOpen()
return if the data source is open. Default is true

Specified by:
isOpen in interface Channel
Specified by:
isOpen in interface IConnection
Specified by:
isOpen in interface INonBlockingConnection
Returns:
true, if the data source is open

onPostAppend

protected void onPostAppend()
Overrides:
onPostAppend in class AbstractNonBlockingStream

onPostRead

protected void onPostRead()
                   throws IOException
Description copied from class: AbstractNonBlockingStream
notification method which will be called after data has been read

Overrides:
onPostRead in class AbstractNonBlockingStream
Throws:
IOException - If some other I/O error occurs

getRemainingMillisToConnectionTimeout

public long getRemainingMillisToConnectionTimeout()
returns the remaining time before a connection timeout occurs

Specified by:
getRemainingMillisToConnectionTimeout in interface IConnection
Returns:
the remaining time

getRemainingMillisToIdleTimeout

public long getRemainingMillisToIdleTimeout()
returns the remaining time before a idle timeout occurs

Specified by:
getRemainingMillisToIdleTimeout in interface IConnection
Returns:
the remaining time

getNumberOfReceivedBytes

public long getNumberOfReceivedBytes()

getNumberOfSendBytes

public long getNumberOfSendBytes()

setWriteTransferRate

public final void setWriteTransferRate(int bytesPerSecond)
                                throws ClosedChannelException,
                                       IOException
set the send delay time. Data to write will be buffered internally and be written to the underlying subsystem based on the given write rate. The write methods will not block for this time.
By default the write transfer rate is set with UNLIMITED

Reduced write transfer is only supported for FlushMode.ASYNC. see INonBlockingConnection#setFlushmode(org.xsocket.connection.IConnection.FlushMode))

Specified by:
setWriteTransferRate in interface INonBlockingConnection
Parameters:
bytesPerSecond - the transfer rate of the outgoing data
Throws:
ClosedChannelException - If the underlying socket is already closed
IOException - If some other I/O error occurs

getWriteTransferRate

public int getWriteTransferRate()
                         throws ClosedChannelException,
                                IOException
gets the send delay time.

Specified by:
getWriteTransferRate in interface INonBlockingConnection
Returns:
the transfer rate of the outgoing data
Throws:
ClosedChannelException - If the underlying socket is already closed
IOException - If some other I/O error occurs

isSecuredModeActivateable

public final boolean isSecuredModeActivateable()
returns if secured mode is activateable

Specified by:
isSecuredModeActivateable in interface INonBlockingConnection
Returns:
true, if secured mode is activateable

activateSecuredMode

public final void activateSecuredMode()
                               throws IOException
ad hoc activation of a secured mode (SSL). By performing of this method all remaining data to send will be flushed. After this all data will be sent and received in the secured mode

Specified by:
activateSecuredMode in interface INonBlockingConnection
Throws:
IOException - If some other I/O error occurs

isSecure

public boolean isSecure()
returns if the connection is in secured mode

Specified by:
isSecure in interface INonBlockingConnection
Returns:
true, if the connection is in secured mode

readByteBufferByDelimiter

public final ByteBuffer[] readByteBufferByDelimiter(String delimiter,
                                                    String encoding,
                                                    int maxLength)
                                             throws IOException,
                                                    BufferUnderflowException,
                                                    MaxReadSizeExceededException
read a ByteBuffer by using a delimiter For performance reasons, the ByteBuffer readByteBuffer method is generally preferable to get bytes

Specified by:
readByteBufferByDelimiter in interface INonBlockingConnection
Overrides:
readByteBufferByDelimiter in class AbstractNonBlockingStream
Parameters:
delimiter - the delimiter
encoding - the delimiter encoding
maxLength - the max length of bytes that should be read. If the limit is exceeded a MaxReadSizeExceededException will been thrown
Returns:
the ByteBuffer
Throws:
MaxReadSizeExceededException - If the max read length has been exceeded and the delimiter hasn’t been found
IOException - If some other I/O error occurs
ClosedChannelException - If the stream is closed
BufferUnderflowException - if not enough data is available

readByteBufferByLength

public ByteBuffer[] readByteBufferByLength(int length)
                                    throws IOException,
                                           BufferUnderflowException
read a ByteBuffer

Specified by:
readByteBufferByLength in interface IDataSource
Overrides:
readByteBufferByLength in class AbstractNonBlockingStream
Parameters:
length - the length could be negative, in this case a empty array will be returned
Returns:
the ByteBuffer
Throws:
IOException - If some other I/O error occurs
ClosedChannelException - If the stream is closed
BufferUnderflowException - if not enough data is available

readSingleByteBuffer

protected ByteBuffer readSingleByteBuffer(int length)
                                   throws IOException,
                                          ClosedChannelException,
                                          BufferUnderflowException
read a byte buffer by length. If the underlying data is fragmented over several ByteBuffer, the ByteBuffers will be merged

Overrides:
readSingleByteBuffer in class AbstractNonBlockingStream
Parameters:
length - the length
Returns:
the byte buffer
Throws:
IOException - If some other I/O error occurs
ClosedChannelException - If the stream is closed
BufferUnderflowException - if not enough data is available

setIdleTimeoutMillis

public final void setIdleTimeoutMillis(long timeoutMillis)
sets the idle timeout in millis

Specified by:
setIdleTimeoutMillis in interface IConnection

setConnectionTimeoutMillis

public final void setConnectionTimeoutMillis(long timeoutMillis)
sets the max time for a connections. By exceeding this time the connection will be terminated

Specified by:
setConnectionTimeoutMillis in interface IConnection

getConnectionTimeoutMillis

public final long getConnectionTimeoutMillis()
gets the connection timeout

Specified by:
getConnectionTimeoutMillis in interface IConnection
Returns:
connection timeout

getIdleTimeoutMillis

public final long getIdleTimeoutMillis()
returns the idle timeout in millis.

Specified by:
getIdleTimeoutMillis in interface IConnection
Returns:
idle timeout in millis

getLocalAddress

public final InetAddress getLocalAddress()
returns the local address

Specified by:
getLocalAddress in interface IConnection
Returns:
the local IP address or InetAddress.anyLocalAddress() if the socket is not bound yet.

getId

public final String getId()
returns the id

Specified by:
getId in interface IConnection
Returns:
id

getLocalPort

public final int getLocalPort()
returns the local port

Specified by:
getLocalPort in interface IConnection
Returns:
the local port

getRemoteAddress

public final InetAddress getRemoteAddress()
returns the remote address

Specified by:
getRemoteAddress in interface IConnection
Returns:
the remote address

getRemotePort

public final int getRemotePort()
returns the port of the remote end point

Specified by:
getRemotePort in interface IConnection
Returns:
the remote port

getPendingWriteDataSize

public final int getPendingWriteDataSize()
returns the size of the data which have already been written, but not yet transferred to the underlying socket.

Specified by:
getPendingWriteDataSize in interface INonBlockingConnection
Returns:
the size of the pending data to write

suspendRead

public final void suspendRead()
                       throws IOException
suspend reading data from the underlying subsystem

Specified by:
suspendRead in interface INonBlockingConnection
Throws:
IOException - If some other I/O error occurs

isReadSuspended

public boolean isReadSuspended()
returns true if read is suspended

Specified by:
isReadSuspended in interface INonBlockingConnection
Returns:
true, if read is suspended

resumeRead

public final void resumeRead()
                      throws IOException
resume reading data from the underlying subsystem

Specified by:
resumeRead in interface INonBlockingConnection
Throws:
IOException - If some other I/O error occurs

transferFrom

public long transferFrom(ReadableByteChannel sourceChannel)
                  throws ClosedChannelException,
                         IOException
transfer the data of the source channel to this data sink

Specified by:
transferFrom in interface IDataSink
Overrides:
transferFrom in class AbstractNonBlockingStream
Parameters:
sourceChannel - the source channel
Returns:
the number of transfered bytes
Throws:
ClosedChannelException - If the stream is closed
IOException - If some other I/O error occurs

onWriteDataInserted

protected void onWriteDataInserted()
                            throws IOException,
                                   ClosedChannelException
notification, that data has been inserted

Overrides:
onWriteDataInserted in class AbstractNonBlockingStream
Throws:
IOException - if an exception occurs
ClosedChannelException - if the stream is closed

getOption

public final Object getOption(String name)
                       throws IOException
returns the value of a option

Specified by:
getOption in interface IConnection
Parameters:
name - the name of the option
Returns:
the value of the option
Throws:
IOException - In an I/O error occurs

getOptions

public final Map<String,Class> getOptions()
Returns an unmodifiable map of the options supported by this end point. The key in the returned map is the name of a option, and its value is the type of the option value. The returned map will never contain null keys or values.

Specified by:
getOptions in interface IConnection
Returns:
An unmodifiable map of the options supported by this channel

setOption

public void setOption(String name,
                      Object value)
               throws IOException
sets the value of a option.

A good article for tuning can be found here http://www.onlamp.com/lpt/a/6324

Specified by:
setOption in interface IConnection
Parameters:
name - the name of the option
value - the value of the option
Throws:
IOException - In an I/O error occurs

getWriteTransferChunkeSize

protected int getWriteTransferChunkeSize()
Description copied from class: AbstractNonBlockingStream
returns the default chunk size for writing

Overrides:
getWriteTransferChunkeSize in class AbstractNonBlockingStream
Returns:
write chunk size

close

public void close()
           throws IOException

Specified by:
close in interface Closeable
Specified by:
close in interface Channel
Overrides:
close in class AbstractNonBlockingStream
Throws:
IOException

flush

public final void flush()
                 throws ClosedChannelException,
                        IOException
flush the send buffer. The method call will block until the outgoing data has been flushed into the underlying os-specific send buffer.

Specified by:
flush in interface Flushable
Specified by:
flush in interface INonBlockingConnection
Throws:
ClosedChannelException - if the underlying channel is closed
IOException - If some other I/O error occurs
SocketTimeoutException - If the timeout has been reached

toString

public String toString()

Overrides:
toString in class Object


Copyright 2008 xSocket.org