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 class org.xsocket.connection.AbstractNonBlockingStream
AbstractNonBlockingStream.ISink, AbstractNonBlockingStream.ISource
 
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
protected NonBlockingConnection(org.xsocket.connection.ConnectionManager connectionManager, org.xsocket.connection.HandlerAdapter hdlAdapter)
          server-side constructor
  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 autoflush, IConnection.FlushMode flushmode, Object attachment)
          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, Object attachment)
          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(InetSocketAddress remoteAddress, InetSocketAddress localAddress, IHandler appHandler, boolean waitForConnect, int connectTimeoutMillis, Map<String,Object> options, SSLContext sslContext, boolean sslOn)
          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.
 
Method Summary
 void activateSecuredMode()
          ad hoc activation of a secured mode (SSL).
 void close()
          
 void closeQuietly()
          closes the connection quitly
 void deactivateSecuredMode()
          ad hoc deactivation of a secured mode (SSL).
 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.
protected  String getInfo()
          returns a info string
 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 isReceivingSuspended()
          returns true if receiving is suspended
 boolean isSecure()
          returns if the connection is in secured mode
 boolean isSecuredModeActivateable()
          returns if secured mode is activateable
 boolean isServerSide()
          returns true id connection is server side
protected  void onPostAppend()
           
protected  ByteBuffer[] onRead(ByteBuffer[] readBufs)
          notification method which will be called after data has been read internally
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 resumeReceiving()
          resume receiving data from the underlying subsystem
 void setConnectionTimeoutMillis(long timeoutMillis)
          sets the max time for a connections.
 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 suspendReceiving()
          suspend receiving data from the underlying subsystem
 String toString()
          
 long transferFrom(ReadableByteChannel source, int chunkSize)
          transfer the data of the source channel to this data sink
 void write(byte[] bytes, int offset, int length, IWriteCompletionHandler writeCompletionHandler)
          writes bytes to the data sink.
 void write(byte[] bytes, IWriteCompletionHandler writeCompletionHandler)
          writes bytes to the data sink.
 void write(ByteBuffer[] srcs, int offset, int length, IWriteCompletionHandler writeCompletionHandler)
          writes a byte buffer array.
 void write(ByteBuffer[] buffers, IWriteCompletionHandler writeCompletionHandler)
          writes a byte buffer array.
 void write(ByteBuffer buffer, IWriteCompletionHandler writeCompletionHandler)
          writes a byte buffer.
 void write(List<ByteBuffer> buffers, IWriteCompletionHandler writeCompletionHandler)
          writes a list of bytes to the data sink.
 void write(String message, String encoding, IWriteCompletionHandler writeCompletionHandler)
          writes a message.
 
Methods inherited from class org.xsocket.connection.AbstractNonBlockingStream
appendDataToReadBuffer, available, copyReadQueue, drainReadQueue, drainWriteQueue, getAttachment, getEncoding, getFlushmode, getReadBufferVersion, getReadQueueSize, getWriteBufferSize, indexOf, indexOf, isAutoflush, isReadBufferEmpty, isSuppressReuseBufferWarning, 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, setFlushmode, setSuppressReuseBufferWarning, transferFrom, transferFrom, transferTo, unread, unread, unread, unread, 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, setFlushmode, transferFrom, unread, unread, unread, unread, 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,
                             Object attachment)
                      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)
attachment - the attacjment or
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
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(InetSocketAddress remoteAddress,
                             InetSocketAddress localAddress,
                             IHandler appHandler,
                             boolean waitForConnect,
                             int connectTimeoutMillis,
                             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:
remoteAddress - the remote address
localAddress - the localAddress
appHandler - the application handler (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler and IConnectionTimeoutHandler)r
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
sslContext - the ssl context to use
sslOn - true, activate SSL mode. false, ssl can be activated by user (see IReadWriteableConnection#activateSecuredMode())
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

public NonBlockingConnection(InetAddress address,
                             int port,
                             IHandler appHandler,
                             boolean autoflush,
                             IConnection.FlushMode flushmode,
                             Object attachment)
                      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
attachment - the attachment or
Throws:
IOException - if a I/O error occurs

NonBlockingConnection

protected NonBlockingConnection(org.xsocket.connection.ConnectionManager connectionManager,
                                org.xsocket.connection.HandlerAdapter hdlAdapter)
                         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 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

Specified by:
setWorkerpool in interface INonBlockingConnection
Parameters:
workerpool - the worker pool

getWorkerpool

public Executor getWorkerpool()
gets the workerpool

Specified by:
getWorkerpool in interface INonBlockingConnection
Returns:
the workerpool

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 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

isServerSide

public boolean isServerSide()
Description copied from interface: IConnection
returns true id connection is server side

Specified by:
isServerSide in interface IConnection
Returns:
true, if is server side

onPostAppend

protected void onPostAppend()
Overrides:
onPostAppend in class AbstractNonBlockingStream

onRead

protected ByteBuffer[] onRead(ByteBuffer[] readBufs)
                       throws IOException
Description copied from class: AbstractNonBlockingStream
notification method which will be called after data has been read internally

Overrides:
onRead in class AbstractNonBlockingStream
Parameters:
readBufs - the read buffers
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 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 boolean isSecuredModeActivateable()
returns if secured mode is activateable

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

activateSecuredMode

public 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

deactivateSecuredMode

public void deactivateSecuredMode()
                           throws IOException
Description copied from interface: INonBlockingConnection
ad hoc deactivation 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 plain mode

Specified by:
deactivateSecuredMode 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 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 void setIdleTimeoutMillis(long timeoutMillis)
sets the idle timeout in millis

Specified by:
setIdleTimeoutMillis in interface IConnection

setConnectionTimeoutMillis

public 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 long getConnectionTimeoutMillis()
gets the connection timeout

Specified by:
getConnectionTimeoutMillis in interface IConnection
Returns:
connection timeout

getIdleTimeoutMillis

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

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

getLocalAddress

public 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 String getId()
returns the id

Specified by:
getId in interface IConnection
Returns:
id

getLocalPort

public int getLocalPort()
returns the local port

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

getRemoteAddress

public InetAddress getRemoteAddress()
returns the remote address

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

getRemotePort

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

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

getPendingWriteDataSize

public 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

suspendReceiving

public void suspendReceiving()
                      throws IOException
suspend receiving data from the underlying subsystem

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

isReceivingSuspended

public boolean isReceivingSuspended()
returns true if receiving is suspended

Specified by:
isReceivingSuspended in interface INonBlockingConnection
Returns:
true, if receiving is suspended

resumeReceiving

public void resumeReceiving()
                     throws IOException
resume receiving data from the underlying subsystem

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

write

public void write(String message,
                  String encoding,
                  IWriteCompletionHandler writeCompletionHandler)
           throws IOException
writes a message. Typically this write mthod will be used in async flush mode

Specified by:
write in interface INonBlockingConnection
Parameters:
message - the message to write
encoding - the encoding which should be used th encode the chars into byte (e.g. `US-ASCII` or `UTF-8`)
writeCompletionHandler - the completion handler
Throws:
IOException - If some other I/O error occurs

write

public void write(byte[] bytes,
                  IWriteCompletionHandler writeCompletionHandler)
           throws IOException
writes bytes to the data sink. Typically this write mthod will be used in async flush mode

Specified by:
write in interface INonBlockingConnection
Parameters:
bytes - the bytes to write
writeCompletionHandler - the completion handler
Throws:
IOException - If some other I/O error occurs

write

public void write(byte[] bytes,
                  int offset,
                  int length,
                  IWriteCompletionHandler writeCompletionHandler)
           throws IOException
writes bytes to the data sink. Typically this write mthod will be used in async flush mode

Specified by:
write in interface INonBlockingConnection
Parameters:
bytes - the bytes to write
offset - the offset of the sub array to be used; must be non-negative and no larger than array.length. The new buffer`s position will be set to this value.
length - the length of the sub array to be used; must be non-negative and no larger than array.length - offset. The new buffer`s limit will be set to offset + length.
writeCompletionHandler - the completion handler
Throws:
IOException - If some other I/O error occurs

write

public void write(ByteBuffer[] srcs,
                  int offset,
                  int length,
                  IWriteCompletionHandler writeCompletionHandler)
           throws IOException
writes a byte buffer array. Typically this write mthod will be used in async flush mode

Specified by:
write in interface INonBlockingConnection
Parameters:
srcs - the buffers
offset - the offset
length - the length
writeCompletionHandler - the completionHandler
Throws:
IOException - If some I/O error occurs

write

public void write(ByteBuffer buffer,
                  IWriteCompletionHandler writeCompletionHandler)
           throws IOException
writes a byte buffer. Typically this write mthod will be used in async flush mode

Specified by:
write in interface INonBlockingConnection
Parameters:
buffer - the buffer to write
writeCompletionHandler - the completionHandler
Throws:
IOException - If some I/O error occurs

write

public void write(ByteBuffer[] buffers,
                  IWriteCompletionHandler writeCompletionHandler)
           throws IOException
writes a byte buffer array. Typically this write mthod will be used in async flush mode

Specified by:
write in interface INonBlockingConnection
Parameters:
buffers - the buffers to write
writeCompletionHandler - the completionHandler
Throws:
IOException - If some I/O error occurs

write

public void write(List<ByteBuffer> buffers,
                  IWriteCompletionHandler writeCompletionHandler)
           throws IOException
writes a list of bytes to the data sink. Typically this write mthod will be used in async flush mode

Specified by:
write in interface INonBlockingConnection
Parameters:
buffers - the bytes to write
writeCompletionHandler - the completionHandler
Throws:
IOException - If some other I/O error occurs

transferFrom

public long transferFrom(ReadableByteChannel source,
                         int chunkSize)
                  throws IOException,
                         BufferOverflowException
Description copied from class: AbstractNonBlockingStream
transfer the data of the source channel to this data sink

Specified by:
transferFrom in interface IDataSink
Overrides:
transferFrom in class AbstractNonBlockingStream
Parameters:
source - the source channel
chunkSize - the chunk size to use
Returns:
the number of transfered bytes
Throws:
IOException - If some other I/O error occurs
ClosedChannelException - If the stream is closed
BufferOverflowException - If the no enough space is available

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 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 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

closeQuietly

public void closeQuietly()
closes the connection quitly


flush

public 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

getInfo

protected String getInfo()
Description copied from class: AbstractNonBlockingStream
returns a info string

Overrides:
getInfo in class AbstractNonBlockingStream
Returns:
a info string

toString

public String toString()

Overrides:
toString in class Object


Copyright 2010 xSocket.org