com.sun.sgs.impl.nio
Class Reactor

java.lang.Object
  extended by com.sun.sgs.impl.nio.Reactor

 class Reactor
extends Object

Reactive implementation of the Reactor pattern; an asynchronous IO dispatcher. When an asynchronous IO operation is initiated, the reactor enables interest in that operation with a Selector, returning a future that will be completed when the operation becomes ready and the IO is performed.

The actual behavior of completing the IO operation is provided by the asynchronous channel implementations; the reactor merely signals readiness and invokes the completion handler for the operation as the operations complete or are canceled.


Nested Class Summary
(package private) static class Reactor.AsyncOp<R>
          A FutureTask that can be canceled by a timeout exception.
(package private)  class Reactor.PendingOperation
          Manages a single asynchronous IO operation for a Reactor.ReactiveAsyncKey.
(package private)  class Reactor.ReactiveAsyncKey
          Provides support for initiating asynchronous IO operations on an underlying reactive channel registered with this Reactor.
 
Field Summary
protected static int DONE
          State: terminated
(package private)  Executor executor
          The executor for this Reactor.
(package private)  ReactiveChannelGroup group
          The channel group for this reactor, used to obtain completion handler runners.
protected  int lifecycleState
          The lifecycle state of this reactor.
(package private) static Logger log
          The logger for this class.
protected static int RUNNING
          State: open and running
(package private)  Selector selector
          The Selector that waits for available IO operations on registered channels.
(package private)  Object selectorLock
          Selector guard.
protected static int SHUTDOWN
          State: graceful shutdown in progress
protected static int SHUTDOWN_NOW
          State: forced shutdown in progress
(package private)  DelayQueue<com.sun.sgs.impl.nio.Reactor.TimeoutHandler> timeouts
          Operations that having pending timeouts.
 
Constructor Summary
Reactor(ReactiveChannelGroup group, Executor executor)
          Creates a new reactor instance with the given channel group and executor.
 
Method Summary
(package private)
<R> void
awaitReady(Reactor.ReactiveAsyncKey asyncKey, int op, Reactor.AsyncOp<R> task)
          Registers interest in an IO operation on the channel associated with the given AsyncKey, returning a future representing the result of the operation.
(package private)  boolean performWork()
          Performs a single iteration of the reactor's event loop, and returns a flag indicating whether the reactor is still running.
(package private)  Reactor.ReactiveAsyncKey register(SelectableChannel ch)
          Registers the given SelectableChannel with this reactor, returning an AsyncKey that can be used to initiate asynchronous operations on that channel.
(package private)  void shutdown()
          Notifies this reactor that it should shutdown when it has no registered channels.
(package private)  void shutdownNow()
          Notifies this reactor that it should shutdown immediately, closing any open channels registered with it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

static final Logger log
The logger for this class.


selectorLock

final Object selectorLock
Selector guard. Any code that accesses selector data structures, (e.g., selection keys and their interest sets), must obtain this lock before waking the selector. Doing so prevents the selector from blocking on select() again until the code that awakened it has released this guard.

The selector must obtain this lock and release it before blocking on select().

If both the selectorLock and AsyncKey need to be locked, the selectorLock must be locked first.


lifecycleState

protected int lifecycleState
The lifecycle state of this reactor. Increases monotonically. It may only be accessed with selectorLock held.


RUNNING

protected static final int RUNNING
State: open and running

See Also:
Constant Field Values

SHUTDOWN

protected static final int SHUTDOWN
State: graceful shutdown in progress

See Also:
Constant Field Values

SHUTDOWN_NOW

protected static final int SHUTDOWN_NOW
State: forced shutdown in progress

See Also:
Constant Field Values

DONE

protected static final int DONE
State: terminated

See Also:
Constant Field Values

group

final ReactiveChannelGroup group
The channel group for this reactor, used to obtain completion handler runners.


selector

final Selector selector
The Selector that waits for available IO operations on registered channels.


executor

final Executor executor
The executor for this Reactor. Typically an executor is shared by all reactors in a group, but each reactor may have its own logical executor.


timeouts

final DelayQueue<com.sun.sgs.impl.nio.Reactor.TimeoutHandler> timeouts
Operations that having pending timeouts.

Constructor Detail

Reactor

Reactor(ReactiveChannelGroup group,
        Executor executor)
  throws IOException
Creates a new reactor instance with the given channel group and executor.

Parameters:
group - the channel group for this reactor
executor - the executor for tasks in this reactor
Throws:
IOException - if an I/O error occurs, e.g. while opening the Selector for this reactor
Method Detail

shutdown

void shutdown()
Notifies this reactor that it should shutdown when it has no registered channels. If this reactor is already marked for shutdown, this method has no effect.

See Also:
AsynchronousChannelGroup.shutdown()

shutdownNow

void shutdownNow()
           throws IOException
Notifies this reactor that it should shutdown immediately, closing any open channels registered with it. If this reactor is already marked for immediate shutdown, this method has no effect.

Throws:
IOException - if an I/O error occurs
See Also:
AsynchronousChannelGroup.shutdownNow()

performWork

boolean performWork()
              throws IOException
Performs a single iteration of the reactor's event loop, and returns a flag indicating whether the reactor is still running. Only one call may be active on a Reactor instance at a time.

Returns:
false if this reactor is stopped, otherwise true
Throws:
IOException - if an I/O error occurs

register

Reactor.ReactiveAsyncKey register(SelectableChannel ch)
                            throws IOException
Registers the given SelectableChannel with this reactor, returning an AsyncKey that can be used to initiate asynchronous operations on that channel.

Parameters:
ch - the SelectableChannel to register
Returns:
an AsyncKey for the given channel
Throws:
ShutdownChannelGroupException - if the reactor is shutdown
IOException - if an IO error occurs

awaitReady

<R> void awaitReady(Reactor.ReactiveAsyncKey asyncKey,
                    int op,
                    Reactor.AsyncOp<R> task)
Registers interest in an IO operation on the channel associated with the given AsyncKey, returning a future representing the result of the operation.

When the requested operation becomes ready, the given task is invoked so that it may perform the IO operation. The selector's interest in all ready operations is cleared before dispatching to the task.

Several checks are performed on the channel at this point to avoid race conditions where the check succeeds but the condition immediately becomes false. We lock both the selectorLock and the asyncKey to ensure that we get a proper view of the state when registering the operation, and so that if the state later changes the operation will be terminated properly.

If the channel is closed, ClosedAsynchronousChannelException is thrown.

Additional checks are performed on SocketChannels:

Type Parameters:
R - the result type
Parameters:
asyncKey - the key for async operations on the channel
op - the SelectionKey operation requested
task - the task to invoke when the operation becomes ready
Throws:
ClosedAsynchronousChannelException - if the channel is closed
NotYetConnectedException - if a read or write operation is requested on an unconnected SocketChannel
AlreadyConnectedException - if a connect operation is requested on a connected SocketChannel

RedDwarf, Version 0.10.1
2010-03-14 10:56:12

Copyright © 2010 The RedDwarf Authors. All rights reserved
Copyright © 2007-2010 Sun Microsystems, Inc. All rights reserved