au.com.terabit.jproactor
Class AsynchChannel

java.lang.Object
  extended by au.com.terabit.jproactor.AsynchChannel
Direct Known Subclasses:
SSLAsynchChannel, TCPAsynchChannel

public abstract class AsynchChannel
extends java.lang.Object

The AsynchChannel is an intermidiate interface that serves as mediator between Demultiplexor and AsynchHandler

All implemented protocols should be based on this interface.

Version:
$Revision$ $Date$
Author:
Yevgeny Libman
See Also:
IOOperation

Nested Class Summary
static class AsynchChannel.State
          Channel states
 
Field Summary
protected  AsynchChannel.State m_channelState
          channelState : OPENED, CONNECTED, CLOSING, CLOSED
protected  java.util.concurrent.locks.ReentrantLock m_lock
          Internal lock for synchronization
 IOStatistic m_readInfo
          queue for the completed write operations
protected  java.util.concurrent.ConcurrentLinkedQueue<IOOperation> m_readQue
          queue for the requested read operations
 IOStatistic m_writeInfo
           
protected  java.util.concurrent.ConcurrentLinkedQueue<IOOperation> m_writeQue
          queue for the requested write operations
 
Constructor Summary
AsynchChannel()
          Creates AsynchChannel instance given specified Demultiplexor, AsynchHandler and SelectableChannel instances.
 
Method Summary
 OpAccept accept(AsynchAcceptHandler handler)
          Request to execute accept operation.
protected  boolean checkForClose()
          this method is should be called by derived implemnations with locked m_lock
 void close()
           
 OpConnect connect(java.net.SocketAddress addr, AsynchConnectHandler handler)
          Request to execute connect operation.
protected  boolean dispatchCompletions(java.util.concurrent.ConcurrentLinkedQueue<IOOperation> completedQue)
           
protected  void enqueueOperation(IOOperation op, java.util.concurrent.ConcurrentLinkedQueue<IOOperation> que, IOStatistic info)
           
protected  IOOperation executeListAndAdd(java.util.concurrent.ConcurrentLinkedQueue<IOOperation> reqQue, boolean flgCancel, java.util.concurrent.ConcurrentLinkedQueue<IOOperation> completedQue)
           
protected  IOOperation executeListAndDispatch(java.util.concurrent.ConcurrentLinkedQueue<IOOperation> reqQue)
           
protected  boolean executeOp(IOOperation op, boolean flgCancel)
           
protected  boolean executeOpAndAdd(IOOperation op, boolean flgCancel, java.util.concurrent.ConcurrentLinkedQueue<IOOperation> completedQue)
           
protected abstract  AsynchChannel finishAccept(OpAccept op)
           
protected abstract  boolean finishConnect(OpConnect op)
           
protected abstract  int finishRead(OpRead op)
           
protected abstract  int finishWrite(OpWrite op)
           
 AsynchChannelHandler getChannelHandler()
          Returns AsynchCloseHandler instance for this AsynchChannel
 boolean isClosed()
           
 boolean isCloseFinished()
           
 boolean isCloseStarted()
           
 boolean isConnected()
           
 boolean isOpened()
           
 OpRead read_n(java.nio.ByteBuffer buf, AsynchReadHandler handler)
          Request to execute non-partitional read.
 OpRead read(java.nio.ByteBuffer buf, AsynchReadHandler handler)
          Request for read operation.
 OpTimer schedulerTimer(long delay, AsynchTimerHandler handler)
          Schedules timeout action to be called back on the associated AsynchHandler instance.
 AsynchChannelHandler setChannelHandler(AsynchChannelHandler handler)
           
protected  boolean setClosing()
           
protected  boolean setConnected()
           
protected abstract  void startAccept(OpAccept op)
           
protected abstract  boolean startClose()
           
protected abstract  void startConnect(OpConnect op)
           
protected abstract  void startRead(OpRead op)
           
abstract  void startTimer(OpTimer op)
           
protected abstract  void startWrite(OpWrite op)
           
 OpWrite write_n(java.nio.ByteBuffer buf, AsynchWriteHandler handler)
          Request to execute non-partitional write.
 OpWrite write(java.nio.ByteBuffer buf, AsynchWriteHandler handler)
          Requets for write operation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_lock

protected java.util.concurrent.locks.ReentrantLock m_lock
Internal lock for synchronization


m_channelState

protected AsynchChannel.State m_channelState
channelState : OPENED, CONNECTED, CLOSING, CLOSED


m_readQue

protected java.util.concurrent.ConcurrentLinkedQueue<IOOperation> m_readQue
queue for the requested read operations


m_writeQue

protected java.util.concurrent.ConcurrentLinkedQueue<IOOperation> m_writeQue
queue for the requested write operations


m_readInfo

public IOStatistic m_readInfo
queue for the completed write operations


m_writeInfo

public IOStatistic m_writeInfo
Constructor Detail

AsynchChannel

public AsynchChannel()
Creates AsynchChannel instance given specified Demultiplexor, AsynchHandler and SelectableChannel instances.

Parameters:
m - Demultiplexor instance this m_asynchChannel works with
p - AsynchHandler protocol instance
sc - SelectableChannel instance this m_asynchChannel operates on
Method Detail

setChannelHandler

public AsynchChannelHandler setChannelHandler(AsynchChannelHandler handler)

getChannelHandler

public AsynchChannelHandler getChannelHandler()
Returns AsynchCloseHandler instance for this AsynchChannel

Returns:
m_closeHandler

startRead

protected abstract void startRead(OpRead op)
                           throws java.lang.Exception
Throws:
java.lang.Exception

startWrite

protected abstract void startWrite(OpWrite op)
                            throws java.lang.Exception
Throws:
java.lang.Exception

startAccept

protected abstract void startAccept(OpAccept op)
                             throws java.lang.Exception
Throws:
java.lang.Exception

startConnect

protected abstract void startConnect(OpConnect op)
                              throws java.lang.Exception
Throws:
java.lang.Exception

startTimer

public abstract void startTimer(OpTimer op)
                         throws java.lang.Exception
Throws:
java.lang.Exception

startClose

protected abstract boolean startClose()

checkForClose

protected boolean checkForClose()
this method is should be called by derived implemnations with locked m_lock

Returns:
true if channel is just or already closed false otherwise

finishRead

protected abstract int finishRead(OpRead op)
                           throws java.lang.Exception
Parameters:
op -
Returns:
> 0 number bytes read 0 not finished yet < 0 errors or end of data
Throws:
java.lang.Exception

finishWrite

protected abstract int finishWrite(OpWrite op)
                            throws java.lang.Exception
Parameters:
op -
Returns:
> 0 number bytes written 0 not finished yet < 0 errors or peer closed
Throws:
java.lang.Exception

finishAccept

protected abstract AsynchChannel finishAccept(OpAccept op)
                                       throws java.lang.Exception
Parameters:
op -
Returns:
new accepted AsynchChannel null not finished yet
Throws:
java.lang.Exception

finishConnect

protected abstract boolean finishConnect(OpConnect op)
                                  throws java.lang.Exception
Parameters:
op -
Returns:
true if connect finished false if not may be to fix: 1 finished OK, 0 not finished yet -1 finished with errors
Throws:
java.lang.Exception

dispatchCompletions

protected boolean dispatchCompletions(java.util.concurrent.ConcurrentLinkedQueue<IOOperation> completedQue)
Parameters:
completedQue -

executeListAndDispatch

protected IOOperation executeListAndDispatch(java.util.concurrent.ConcurrentLinkedQueue<IOOperation> reqQue)
Parameters:
reqQue -
Returns:
null if all operations in reqQue are executed first operation that is still in reqQue and was not executed

executeListAndAdd

protected IOOperation executeListAndAdd(java.util.concurrent.ConcurrentLinkedQueue<IOOperation> reqQue,
                                        boolean flgCancel,
                                        java.util.concurrent.ConcurrentLinkedQueue<IOOperation> completedQue)
Parameters:
reqQue -
completedQue -
Returns:
null if all operations in reqQue are executed first operation that is still in reqQue and was not executed

executeOpAndAdd

protected boolean executeOpAndAdd(IOOperation op,
                                  boolean flgCancel,
                                  java.util.concurrent.ConcurrentLinkedQueue<IOOperation> completedQue)

executeOp

protected boolean executeOp(IOOperation op,
                            boolean flgCancel)

enqueueOperation

protected void enqueueOperation(IOOperation op,
                                java.util.concurrent.ConcurrentLinkedQueue<IOOperation> que,
                                IOStatistic info)
                         throws java.lang.Exception
Throws:
java.lang.Exception

read

public OpRead read(java.nio.ByteBuffer buf,
                   AsynchReadHandler handler)
            throws java.lang.Exception
Request for read operation. This is main interface to start asynchronous read operation.

Parameters:
buf - buffer to read data to
Returns:
OpRead operation - future result
Throws:
exception - if error occurs
java.lang.Exception

read_n

public OpRead read_n(java.nio.ByteBuffer buf,
                     AsynchReadHandler handler)
              throws java.lang.Exception
Request to execute non-partitional read. Read exactly Buffer.limit() bytes to the buffer.

Parameters:
buf -
Returns:
OpRead operation - future result
Throws:
exception - if error occurs
java.lang.Exception

write

public OpWrite write(java.nio.ByteBuffer buf,
                     AsynchWriteHandler handler)
              throws java.lang.Exception
Requets for write operation. This is main interface to start asynchronous write operation.

Parameters:
buf - buffer to write data from
Returns:
OpWrite operation - future result
Throws:
exception - if error occurs
java.lang.Exception

write_n

public OpWrite write_n(java.nio.ByteBuffer buf,
                       AsynchWriteHandler handler)
                throws java.lang.Exception
Request to execute non-partitional write. Write exactly Buffer.limit() bytes to the associated channel.

Parameters:
buf - buffer to write data from
Returns:
OpWrite operation - future result
Throws:
exception - if error occurs
java.lang.Exception

accept

public OpAccept accept(AsynchAcceptHandler handler)
                throws java.lang.Exception
Request to execute accept operation.

Returns:
OpAccept operation - future result
Throws:
java.lang.Exception - if error occurs

connect

public OpConnect connect(java.net.SocketAddress addr,
                         AsynchConnectHandler handler)
                  throws java.lang.Exception
Request to execute connect operation.

Parameters:
SocketAddress - address to connect
Returns:
OpConnect operation - future result
Throws:
java.lang.Exception - if error occurs

schedulerTimer

public OpTimer schedulerTimer(long delay,
                              AsynchTimerHandler handler)
                       throws java.lang.Exception
Schedules timeout action to be called back on the associated AsynchHandler instance.

Note: while IO operations will be delivered to the handler in a serialized way, timer notifications might be delivered concurrently.

Parameters:
delay - delay in milliseconds before timeout action is to be executed.
Returns:
OpTimer operation - future result
Throws:
java.lang.Exception

close

public void close()

isOpened

public boolean isOpened()

isConnected

public boolean isConnected()

isClosed

public boolean isClosed()

isCloseStarted

public boolean isCloseStarted()

isCloseFinished

public boolean isCloseFinished()

setConnected

protected final boolean setConnected()

setClosing

protected final boolean setClosing()