org.lightwolf
Class IOActivator

java.lang.Object
  extended by org.lightwolf.IOActivator

public class IOActivator
extends Object

An object that activate flows when a NIO channel operation becomes available.

Author:
Fernando Colombo

Constructor Summary
IOActivator()
           
 
Method Summary
 SocketChannel accept(ServerSocketChannel socket)
          Accepts a single incoming connection without holding any thread during the wait.
 SocketChannel acceptMany(ServerSocketChannel socket)
          Accepts multiple incoming connections without holding any thread during the wait.
 void cancelAccepts(ServerSocketChannel socket)
           
 void close()
           
 int read(ReadableByteChannel channel, ByteBuffer dst)
          Reads a sequence of bytes from the channel without holding any thread during the wait.
 int wait(SelectableChannel channel, int ops)
           
 int write(WritableByteChannel channel, ByteBuffer src)
          Writes a sequence of bytes from the channel without holding any thread during the wait.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IOActivator

public IOActivator()
            throws IOException
Throws:
IOException
Method Detail

acceptMany

public SocketChannel acceptMany(ServerSocketChannel socket)
                         throws IOException
Accepts multiple incoming connections without holding any thread during the wait. This method returns every time a new incoming connection is established on the informed socket, which means that it can return multiple times, to concurrent threads. The following example illustrates this behavior:
    @FlowMethod
    void example() {
        ServerSocketChannel serverSocket = ServerSocketChannel.open();
        serverSocket.socket().bind(new InetSocketAddress(8080));
        serverSocket.configureBlocking(false);
        IOActivator activator = new IOActivator();
        // The following will run for each incoming connection.
        SocketChannel clientSocket = activator.acceptMany(serverSocket);
        // Process this connection. May run in parallel with other connections.
        clientSocket.close();
    }
 
The informed socket must be in non-blocking mode, so this class can call register() without triggering an exception. Despite this fact, this method never returns null nor it blocks the current thread, as specified in on Flow.suspend(Object).

This method always returns to a new flow. That is, it never returns to the invoker's flow.

Parameters:
socket - The server socket. Must be already bound to an address, and must be configured to non-blocking mode.
Returns:
A SockedChannel that just connected to the specified server socket. This method may return multiple times.
Throws:
ClosedChannelException - If this channel is closed.
AsynchronousCloseException - If another thread closes this channel while the accept operation is in progress.
ClosedByInterruptException - If another thread interrupts the current thread while the accept operation is in progress, thereby closing the channel and setting the current thread's interrupt status.
IOException - If some other I/O error occurs.
IllegalBlockingModeException - If this channel is not in non-blocking mode.
NotYetBoundException - If this channel's socket has not yet been bound.
SecurityException - If a security manager has been installed and it does not permit access to the remote endpoint of the new connection.
See Also:
ServerSocketChannel.accept()

accept

public SocketChannel accept(ServerSocketChannel socket)
                     throws IOException
Accepts a single incoming connection without holding any thread during the wait. If the informed channel is ready to perform an ServerSocketChannel.accept() operation, this method returns immediately. Otherwise the channel is registered on this object's selector and the current flow is suspended until an incoming connection is established.

This method always returns to the invoker's flow.

Parameters:
socket - The server socket. Must be already bound to an address, and must be configured to non-blocking mode.
Returns:
A SockedChannel that just connected to the specified server socket. This method may return multiple times.
Throws:
ClosedChannelException - If this channel is closed.
AsynchronousCloseException - If another thread closes this channel while the accept operation is in progress.
ClosedByInterruptException - If another thread interrupts the current thread while the accept operation is in progress, thereby closing the channel and setting the current thread's interrupt status.
IOException - If some other I/O error occurs.
IllegalBlockingModeException - If this channel is not in non-blocking mode.
NotYetBoundException - If this channel's socket has not yet been bound.
SecurityException - If a security manager has been installed and it does not permit access to the remote endpoint of the new connection.
See Also:
ServerSocketChannel.accept()

read

public int read(ReadableByteChannel channel,
                ByteBuffer dst)
         throws IOException
Reads a sequence of bytes from the channel without holding any thread during the wait. If the informed channel is ready to perform a read operation, this method returns immediately. Otherwise the channel is registered on this object's selector and the current flow is suspended until the read operation becomes available.

This method always returns to the invoker's flow.

Parameters:
channel - The channel on which the read operation is to be performed.
dst - The buffer into which bytes are to be transferred
Returns:
The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws:
ClassCastException - If the channel is not a SelectableChannel.
NonReadableChannelException - If this channel was not opened for reading
ClosedChannelException - If this channel is closed
AsynchronousCloseException - If another thread closes this channel while the read operation is in progress
ClosedByInterruptException - If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException - If some other I/O error occurs
See Also:
ReadableByteChannel.read(ByteBuffer)

write

public int write(WritableByteChannel channel,
                 ByteBuffer src)
          throws IOException
Writes a sequence of bytes from the channel without holding any thread during the wait. If the informed channel is ready to perform a write operation, this method returns immediately. Otherwise the channel is registered on this object's selector and the current flow is suspended until the read operation becomes available.

This method always returns to the invoker's flow.

Parameters:
channel - The channel on which the write operation is to be performed.
src - The buffer from which bytes are to be retrieved
Returns:
The number of bytes written, possibly zero
Throws:
ClassCastException - If the channel is not a SelectableChannel.
NonWritableChannelException - If this channel was not opened for writing
ClosedChannelException - If this channel is closed
AsynchronousCloseException - If another thread closes this channel while the write operation is in progress
ClosedByInterruptException - If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException - If some other I/O error occurs
See Also:
WritableByteChannel.write(ByteBuffer)

wait

public int wait(SelectableChannel channel,
                int ops)
         throws IOException
Throws:
IOException

cancelAccepts

public void cancelAccepts(ServerSocketChannel socket)

close

public void close()
           throws IOException
Throws:
IOException